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;
1685 setRightLine(cell, right_border);
1687 for (idx_type i = 1; i < number; ++i) {
1688 CellData & cs1 = cellInfo(cell + i);
1689 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1690 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1698 bool Tabular::isMultiRow(idx_type cell) const
1700 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1701 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1704 bool Tabular::hasMultiRow(row_type r) const
1706 for (col_type c = 0; c < ncols(); ++c) {
1707 if (isMultiRow(cellIndex(r, c)))
1713 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1714 bool const bottom_border)
1716 idx_type const col = cellColumn(cell);
1717 idx_type const row = cellRow(cell);
1718 for (idx_type i = 0; i < number; ++i)
1719 unsetMultiColumn(cellIndex(row + i, col));
1721 // unsetting of multirow may have invalidated cell index
1722 cell = cellIndex(row, col);
1723 CellData & cs = cellInfo(cell);
1724 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1725 cs.valignment = LYX_VALIGN_MIDDLE;
1726 // the horizontal alignment of multirow cells can only
1727 // be changed for the whole table row,
1728 // support changing this only for the multirow cell can be done via
1730 // this feature would be a fileformat change
1731 // until LyX supports this, use the deault alignment of multirow
1733 cs.alignment = LYX_ALIGN_LEFT;
1735 // set the bottom line of the last selected cell
1736 setBottomLine(cell, bottom_border);
1738 for (idx_type i = 1; i < number; ++i) {
1739 CellData & cs1 = cell_info[row + i][col];
1740 cs1.multirow = CELL_PART_OF_MULTIROW;
1741 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1749 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1751 row_type const row = cellRow(cell);
1752 col_type const col = cellColumn(cell);
1754 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1761 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1763 col_type const column = cellColumn(cell);
1764 col_type row = cellRow(cell) + 1;
1765 while (row < nrows() && isPartOfMultiRow(row, column))
1768 return row - cellRow(cell);
1772 void Tabular::unsetMultiColumn(idx_type cell)
1774 if (!isMultiColumn(cell))
1777 row_type const row = cellRow(cell);
1778 col_type const col = cellColumn(cell);
1779 row_type const span = columnSpan(cell);
1780 for (col_type c = 0; c < span; ++c) {
1781 // in the table dialog the lines are set in every case
1782 // when unsetting a multicolumn this leads to an additional right
1783 // line for every cell that was part of the former multicolumn cell,
1784 // except if the cell is in the last column
1785 // therefore remove this line
1786 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1787 && (col + c) < (col + span - 1))
1788 cell_info[row][col + c].right_line = false;
1789 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1795 void Tabular::unsetMultiRow(idx_type cell)
1797 if (!isMultiRow(cell))
1800 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1801 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1802 row_type const row = cellRow(cell);
1803 col_type const col = cellColumn(cell);
1804 row_type const span = rowSpan(cell);
1805 for (row_type r = 0; r < span; ++r)
1806 cell_info[row + r][col].multirow = CELL_NORMAL;
1811 void Tabular::setRotateCell(idx_type cell, int value)
1813 cellInfo(cell).rotate = value;
1817 int Tabular::getRotateCell(idx_type cell) const
1819 return cellInfo(cell).rotate;
1823 bool Tabular::needRotating() const
1827 for (row_type r = 0; r < nrows(); ++r)
1828 for (col_type c = 0; c < ncols(); ++c)
1829 if (cell_info[r][c].rotate != 0)
1835 bool Tabular::isLastCell(idx_type cell) const
1837 if (cell + 1 < numberofcells)
1843 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1845 if (cellRow(cell) == 0)
1848 col_type const col = cellColumn(cell);
1849 row_type r = cellRow(cell) - 1;
1850 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1853 return cell_info[r][col].cellno;
1857 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1859 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1860 if (nextrow < nrows())
1861 return cell_info[nextrow][cellColumn(cell)].cellno;
1866 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1868 LASSERT(column != npos && column < ncols(), column = 0);
1869 LASSERT(row != npos && row < nrows(), row = 0);
1870 return cell_info[row][column].cellno;
1874 void Tabular::setUsebox(idx_type cell, BoxType type)
1876 cellInfo(cell).usebox = type;
1880 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1881 // adds line breaks, see bug 4886.
1882 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1884 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1885 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1887 if (cellInfo(cell).usebox > 1)
1888 return cellInfo(cell).usebox;
1889 return useParbox(cell);
1894 // This are functions used for the longtable support
1896 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1902 row_info[row].endfirsthead = flag;
1906 row_info[row].endhead = flag;
1911 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1914 hd.set = haveLTHead();
1915 return row_info[row].endhead;
1919 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1922 hd.set = haveLTFirstHead();
1923 return row_info[row].endfirsthead;
1927 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1933 row_info[row].endlastfoot = flag;
1937 row_info[row].endfoot = flag;
1942 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1945 fd.set = haveLTFoot();
1946 return row_info[row].endfoot;
1950 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1953 fd.set = haveLTLastFoot();
1954 return row_info[row].endlastfoot;
1958 void Tabular::setLTNewPage(row_type row, bool what)
1960 row_info[row].newpage = what;
1964 bool Tabular::getLTNewPage(row_type row) const
1966 return row_info[row].newpage;
1970 bool Tabular::haveLTHead(bool withcaptions) const
1972 if (!is_long_tabular)
1974 for (row_type i = 0; i < nrows(); ++i)
1975 if (row_info[i].endhead &&
1976 (withcaptions || !row_info[i].caption))
1982 bool Tabular::haveLTFirstHead(bool withcaptions) const
1984 if (!is_long_tabular || endfirsthead.empty)
1986 for (row_type r = 0; r < nrows(); ++r)
1987 if (row_info[r].endfirsthead &&
1988 (withcaptions || !row_info[r].caption))
1994 bool Tabular::haveLTFoot(bool withcaptions) const
1996 if (!is_long_tabular)
1998 for (row_type r = 0; r < nrows(); ++r)
1999 if (row_info[r].endfoot &&
2000 (withcaptions || !row_info[r].caption))
2006 bool Tabular::haveLTLastFoot(bool withcaptions) const
2008 if (!is_long_tabular || endlastfoot.empty)
2010 for (row_type r = 0; r < nrows(); ++r)
2011 if (row_info[r].endlastfoot &&
2012 (withcaptions || !row_info[r].caption))
2018 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2020 idx_type i = getFirstCellInRow(row);
2022 setMultiColumn(i, numberOfCellsInRow(row), false);
2023 setTopLine(i, false);
2024 setBottomLine(i, false);
2025 setLeftLine(i, false);
2026 setRightLine(i, false);
2027 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2028 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2029 setLTHead(row, true, endfirsthead, true);
2030 row_info[row].endfirsthead = true;
2033 unsetMultiColumn(i);
2034 // When unsetting a caption row, also all existing
2035 // captions in this row must be dissolved.
2037 row_info[row].caption = what;
2042 bool Tabular::ltCaption(row_type row) const
2044 return row_info[row].caption;
2048 bool Tabular::haveLTCaption(CaptionType captiontype) const
2050 if (!is_long_tabular)
2052 for (row_type r = 0; r < nrows(); ++r) {
2053 if (row_info[r].caption) {
2054 switch (captiontype) {
2055 case CAPTION_FIRSTHEAD:
2056 if (row_info[r].endfirsthead)
2060 if (row_info[r].endhead)
2064 if (row_info[r].endfoot)
2067 case CAPTION_LASTFOOT:
2068 if (row_info[r].endlastfoot)
2080 // end longtable support functions
2082 void Tabular::setRowAscent(row_type row, int height)
2084 if (row >= nrows() || row_info[row].ascent == height)
2086 row_info[row].ascent = height;
2090 void Tabular::setRowDescent(row_type row, int height)
2092 if (row >= nrows() || row_info[row].descent == height)
2094 row_info[row].descent = height;
2098 int Tabular::rowAscent(row_type row) const
2100 LASSERT(row < nrows(), row = 0);
2101 return row_info[row].ascent;
2105 int Tabular::rowDescent(row_type row) const
2107 LASSERT(row < nrows(), row = 0);
2108 return row_info[row].descent;
2112 int Tabular::height() const
2115 for (row_type row = 0; row < nrows(); ++row)
2116 height += rowAscent(row) + rowDescent(row) +
2122 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2124 LASSERT(row < nrows(), return false);
2125 LASSERT(column < ncols(), return false);
2126 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2130 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2132 LASSERT(row < nrows(), return false);
2133 LASSERT(column < ncols(), return false);
2134 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2138 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2140 // we only output complete row lines and the 1st row here, the rest
2141 // is done in Tabular::TeXBottomHLine(...)
2143 // get for each column the topline (if any)
2144 vector<bool> topline;
2146 for (col_type c = 0; c < ncols(); ++c) {
2147 topline.push_back(topLine(cellIndex(row, c)));
2148 // If cell is part of a multirow and not the first cell of the
2149 // multirow, no line must be drawn.
2151 if (isMultiRow(cellIndex(row, c))
2152 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2158 // do nothing if empty first row, or incomplete row line after
2159 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2162 // only output complete row lines and the 1st row's clines
2163 if (nset == ncols()) {
2165 os << (row == 0 ? "\\toprule " : "\\midrule ");
2169 } else if (row == 0) {
2170 for (col_type c = 0; c < ncols(); ++c) {
2172 col_type offset = 0;
2173 for (col_type j = 0 ; j < c; ++j)
2174 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2177 //babel makes the "-" character an active one, so we have to suppress this here
2178 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2179 if (lang == "slovak" || lang == "czech")
2180 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2181 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2183 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2185 col_type cstart = c;
2186 for ( ; c < ncols() && topline[c]; ++c) {}
2188 for (col_type j = cstart ; j < c ; ++j)
2189 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2192 os << c + offset << "} ";
2200 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2202 // we output bottomlines of row r and the toplines of row r+1
2203 // if the latter do not span the whole tabular
2205 // get the bottomlines of row r, and toplines in next row
2206 bool lastrow = row == nrows() - 1;
2207 vector<bool> bottomline, topline;
2208 bool nextrowset = true;
2209 for (col_type c = 0; c < ncols(); ++c) {
2210 bottomline.push_back(bottomLine(cellIndex(row, c)));
2211 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2212 // If cell is part of a multirow and not the last cell of the
2213 // multirow, no line must be drawn.
2215 if (isMultiRow(cellIndex(row, c))
2216 && isMultiRow(cellIndex(row + 1, c))
2217 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2218 bottomline[c] = false;
2221 nextrowset &= topline[c];
2224 // combine this row's bottom lines and next row's toplines if necessary
2226 for (col_type c = 0; c < ncols(); ++c) {
2228 bottomline[c] = bottomline[c] || topline[c];
2233 // do nothing if empty, OR incomplete row line with a topline in next row
2234 if (nset == 0 || (nextrowset && nset != ncols()))
2237 if (nset == ncols()) {
2239 os << (lastrow ? "\\bottomrule" : "\\midrule");
2243 for (col_type c = 0; c < ncols(); ++c) {
2244 if (bottomline[c]) {
2245 col_type offset = 0;
2246 for (col_type j = 0 ; j < c; ++j)
2247 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2250 //babel makes the "-" character an active one, so we have to suppress this here
2251 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2252 if (lang == "slovak" || lang == "czech")
2253 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2254 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2256 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2258 col_type cstart = c;
2259 for ( ; c < ncols() && bottomline[c]; ++c) {}
2261 for (col_type j = cstart ; j < c ; ++j)
2262 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2265 os << c + offset << "} ";
2273 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2274 bool & ismulticol, bool & ismultirow) const
2276 row_type const r = cellRow(cell);
2277 if (is_long_tabular && row_info[r].caption)
2280 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2281 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2282 // figure out how to set the lines
2283 // we always set double lines to the right of the cell
2284 col_type const c = cellColumn(cell);
2285 col_type const nextcol = c + columnSpan(cell);
2286 bool colright = columnRightLine(c);
2287 bool colleft = columnLeftLine(c);
2288 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2289 bool nextcellleft = nextcol < ncols()
2290 && leftLine(cellIndex(r, nextcol));
2291 bool coldouble = colright && nextcolleft;
2292 bool celldouble = rightLine(cell) && nextcellleft;
2294 ismulticol = isMultiColumn(cell)
2295 || (c == 0 && colleft != leftLine(cell))
2296 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2297 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2298 || (coldouble != celldouble);
2300 // we center in multicol when no decimal point
2301 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2302 docstring const align_d = column_info[c].decimal_point;
2303 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2307 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2308 int latexcolspan = columnSpan(cell);
2309 for(col_type col = c; col < c + columnSpan(cell); ++col)
2310 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2314 os << "\\multicolumn{" << latexcolspan << "}{";
2315 if (c ==0 && leftLine(cell))
2317 if (!cellInfo(cell).align_special.empty()) {
2318 os << cellInfo(cell).align_special;
2320 if (!getPWidth(cell).zero()) {
2322 case LYX_ALIGN_LEFT:
2323 os << ">{\\raggedright}";
2325 case LYX_ALIGN_RIGHT:
2326 os << ">{\\raggedleft}";
2328 case LYX_ALIGN_CENTER:
2329 os << ">{\\centering}";
2335 case LYX_VALIGN_TOP:
2338 case LYX_VALIGN_MIDDLE:
2341 case LYX_VALIGN_BOTTOM:
2346 << from_ascii(getPWidth(cell).asLatexString())
2350 case LYX_ALIGN_LEFT:
2353 case LYX_ALIGN_RIGHT:
2360 } // end if else !getPWidth
2361 } // end if else !cellinfo_of_cell
2362 if (rightLine(cell) || nextcellleft)
2365 // add extra vertical line if we want a double one
2368 } // end if ismulticol
2370 // we only need code for the first multirow cell
2371 ismultirow = isMultiRow(cell);
2373 os << "\\multirow{" << rowSpan(cell) << "}{";
2374 if (!getPWidth(cell).zero())
2375 os << from_ascii(getPWidth(cell).asLatexString());
2377 // we need to set a default value
2380 if (!getMROffset(cell).zero())
2381 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2383 } // end if ismultirow
2385 if (getRotateCell(cell) != 0)
2386 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2388 if (getUsebox(cell) == BOX_PARBOX) {
2391 case LYX_VALIGN_TOP:
2394 case LYX_VALIGN_MIDDLE:
2397 case LYX_VALIGN_BOTTOM:
2401 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2403 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2404 os << "\\begin{minipage}[";
2406 case LYX_VALIGN_TOP:
2409 case LYX_VALIGN_MIDDLE:
2412 case LYX_VALIGN_BOTTOM:
2416 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2422 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2423 bool ismulticol, bool ismultirow) const
2425 row_type const r = cellRow(cell);
2426 if (is_long_tabular && row_info[r].caption)
2430 if (getUsebox(cell) == BOX_PARBOX)
2432 else if (getUsebox(cell) == BOX_MINIPAGE)
2433 os << breakln << "\\end{minipage}";
2434 if (getRotateCell(cell) != 0)
2435 os << breakln << "\\end{turn}";
2443 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2444 OutputParams const & runparams) const
2446 if (!is_long_tabular)
2450 // the caption must be output before the headers
2451 if (haveLTCaption()) {
2452 for (row_type r = 0; r < nrows(); ++r) {
2453 if (row_info[r].caption)
2454 TeXRow(os, r, runparams);
2457 // output first header info
2458 if (haveLTFirstHead()) {
2459 if (endfirsthead.topDL)
2461 for (row_type r = 0; r < nrows(); ++r) {
2462 if (row_info[r].endfirsthead)
2463 TeXRow(os, r, runparams);
2465 if (endfirsthead.bottomDL)
2467 os << "\\endfirsthead\n";
2469 // output header info
2471 if (endfirsthead.empty && !haveLTFirstHead())
2472 os << "\\endfirsthead\n";
2475 for (row_type r = 0; r < nrows(); ++r) {
2476 if (row_info[r].endhead)
2477 TeXRow(os, r, runparams);
2479 if (endhead.bottomDL)
2481 os << "\\endhead\n";
2483 // output footer info
2487 for (row_type r = 0; r < nrows(); ++r) {
2488 if (row_info[r].endfoot)
2489 TeXRow(os, r, runparams);
2491 if (endfoot.bottomDL)
2493 os << "\\endfoot\n";
2494 if (endlastfoot.empty && !haveLTLastFoot())
2495 os << "\\endlastfoot\n";
2497 // output lastfooter info
2498 if (haveLTLastFoot()) {
2499 if (endlastfoot.topDL)
2501 for (row_type r = 0; r < nrows(); ++r) {
2502 if (row_info[r].endlastfoot)
2503 TeXRow(os, r, runparams);
2505 if (endlastfoot.bottomDL)
2507 os << "\\endlastfoot\n";
2512 bool Tabular::isValidRow(row_type row) const
2514 if (!is_long_tabular)
2516 return !row_info[row].endhead && !row_info[row].endfirsthead
2517 && !row_info[row].endfoot && !row_info[row].endlastfoot
2518 && !row_info[row].caption;
2522 void Tabular::TeXRow(otexstream & os, row_type row,
2523 OutputParams const & runparams) const
2525 idx_type cell = cellIndex(row, 0);
2526 shared_ptr<InsetTableCell> inset = cellInset(cell);
2527 Paragraph const & par = inset->paragraphs().front();
2528 string const lang = par.getParLanguage(buffer().params())->lang();
2530 //output the top line
2531 TeXTopHLine(os, row, lang);
2533 if (row_info[row].top_space_default) {
2535 os << "\\addlinespace\n";
2537 os << "\\noalign{\\vskip\\doublerulesep}\n";
2538 } else if(!row_info[row].top_space.zero()) {
2540 os << "\\addlinespace["
2541 << from_ascii(row_info[row].top_space.asLatexString())
2544 os << "\\noalign{\\vskip"
2545 << from_ascii(row_info[row].top_space.asLatexString())
2549 bool ismulticol = false;
2550 bool ismultirow = false;
2551 for (col_type c = 0; c < ncols(); ++c) {
2552 if (isPartOfMultiColumn(row, c))
2555 cell = cellIndex(row, c);
2557 if (isPartOfMultiRow(row, c)
2558 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2559 if (cell != getLastCellInRow(row))
2564 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2565 shared_ptr<InsetTableCell> inset = cellInset(cell);
2567 Paragraph const & par = inset->paragraphs().front();
2568 bool rtl = par.isRTL(buffer().params())
2570 && getPWidth(cell).zero()
2571 && !runparams.use_polyglossia;
2575 par.getParLanguage(buffer().params())->lang();
2576 if (lang == "farsi")
2578 else if (lang == "arabic_arabi")
2580 // currently, remaining RTL languages are
2581 // arabic_arabtex and hebrew
2585 // pass to the OutputParams that we are in a cell and
2586 // which alignment we have set.
2587 // InsetNewline needs this context information.
2588 OutputParams newrp(runparams);
2589 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2590 ? OutputParams::PLAIN
2591 : OutputParams::ALIGNED;
2593 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2594 // copy cell and split in 2
2595 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2596 head.setBuffer(buffer());
2597 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2599 dit.push_back(CursorSlice(head));
2600 head.setMacrocontextPositionRecursive(dit);
2601 bool hassep = false;
2602 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2603 head.latex(os, newrp);
2606 tail.setBuffer(head.buffer());
2608 dit.push_back(CursorSlice(tail));
2609 tail.setMacrocontextPositionRecursive(dit);
2610 tail.latex(os, newrp);
2612 } else if (!isPartOfMultiRow(row, c)) {
2613 if (!runparams.nice)
2614 os.texrow().start(par.id(), 0);
2615 inset->latex(os, newrp);
2618 runparams.encoding = newrp.encoding;
2622 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2623 if (cell != getLastCellInRow(row)) { // not last cell in row
2630 os << "\\tabularnewline";
2631 if (row_info[row].bottom_space_default) {
2633 os << "\\addlinespace";
2635 os << "[\\doublerulesep]";
2636 } else if (!row_info[row].bottom_space.zero()) {
2638 os << "\\addlinespace";
2640 << from_ascii(row_info[row].bottom_space.asLatexString())
2645 //output the bottom line
2646 TeXBottomHLine(os, row, lang);
2648 if (row_info[row].interline_space_default) {
2650 os << "\\addlinespace\n";
2652 os << "\\noalign{\\vskip\\doublerulesep}\n";
2653 } else if (!row_info[row].interline_space.zero()) {
2655 os << "\\addlinespace["
2656 << from_ascii(row_info[row].interline_space.asLatexString())
2659 os << "\\noalign{\\vskip"
2660 << from_ascii(row_info[row].interline_space.asLatexString())
2666 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2668 bool const is_tabular_star = !tabular_width.zero();
2670 //+---------------------------------------------------------------------
2671 //+ first the opening preamble +
2672 //+---------------------------------------------------------------------
2675 if (runparams.lastid != -1)
2676 os.texrow().start(runparams.lastid, runparams.lastpos);
2679 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2681 if (is_long_tabular) {
2682 os << "\\begin{longtable}";
2683 switch (longtabular_alignment) {
2684 case LYX_LONGTABULAR_ALIGN_LEFT:
2687 case LYX_LONGTABULAR_ALIGN_CENTER:
2689 case LYX_LONGTABULAR_ALIGN_RIGHT:
2694 if (is_tabular_star)
2695 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2697 os << "\\begin{tabular}";
2698 switch (tabular_valignment) {
2699 case LYX_VALIGN_TOP:
2702 case LYX_VALIGN_MIDDLE:
2704 case LYX_VALIGN_BOTTOM:
2712 if (is_tabular_star)
2713 os << "@{\\extracolsep{\\fill}}";
2715 for (col_type c = 0; c < ncols(); ++c) {
2716 if (columnLeftLine(c))
2718 if (!column_info[c].align_special.empty()) {
2719 os << column_info[c].align_special;
2721 if (!column_info[c].p_width.zero()) {
2722 switch (column_info[c].alignment) {
2723 case LYX_ALIGN_LEFT:
2724 os << ">{\\raggedright}";
2726 case LYX_ALIGN_RIGHT:
2727 os << ">{\\raggedleft}";
2729 case LYX_ALIGN_CENTER:
2730 os << ">{\\centering}";
2732 case LYX_ALIGN_NONE:
2733 case LYX_ALIGN_BLOCK:
2734 case LYX_ALIGN_LAYOUT:
2735 case LYX_ALIGN_SPECIAL:
2736 case LYX_ALIGN_DECIMAL:
2740 switch (column_info[c].valignment) {
2741 case LYX_VALIGN_TOP:
2744 case LYX_VALIGN_MIDDLE:
2747 case LYX_VALIGN_BOTTOM:
2752 << from_ascii(column_info[c].p_width.asLatexString())
2755 switch (column_info[c].alignment) {
2756 case LYX_ALIGN_LEFT:
2759 case LYX_ALIGN_RIGHT:
2762 case LYX_ALIGN_DECIMAL:
2763 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2769 } // end if else !column_info[i].p_width
2770 } // end if else !column_info[i].align_special
2771 if (columnRightLine(c))
2776 TeXLongtableHeaderFooter(os, runparams);
2778 //+---------------------------------------------------------------------
2779 //+ the single row and columns (cells) +
2780 //+---------------------------------------------------------------------
2782 for (row_type r = 0; r < nrows(); ++r) {
2783 if (isValidRow(r)) {
2784 TeXRow(os, r, runparams);
2785 if (is_long_tabular && row_info[r].newpage)
2786 os << "\\newpage\n";
2790 //+---------------------------------------------------------------------
2791 //+ the closing of the tabular +
2792 //+---------------------------------------------------------------------
2794 if (is_long_tabular)
2795 os << "\\end{longtable}";
2797 if (is_tabular_star)
2798 os << "\\end{tabular*}";
2800 os << "\\end{tabular}";
2804 os << breakln << "\\end{turn}";
2808 int Tabular::docbookRow(odocstream & os, row_type row,
2809 OutputParams const & runparams) const
2812 idx_type cell = getFirstCellInRow(row);
2815 for (col_type c = 0; c < ncols(); ++c) {
2816 if (isPartOfMultiColumn(row, c))
2819 os << "<entry align=\"";
2820 switch (getAlignment(cell)) {
2821 case LYX_ALIGN_LEFT:
2824 case LYX_ALIGN_RIGHT:
2832 os << "\" valign=\"";
2833 switch (getVAlignment(cell)) {
2834 case LYX_VALIGN_TOP:
2837 case LYX_VALIGN_BOTTOM:
2840 case LYX_VALIGN_MIDDLE:
2845 if (isMultiColumn(cell)) {
2846 os << " namest=\"col" << c << "\" ";
2847 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2851 ret += cellInset(cell)->docbook(os, runparams);
2860 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2864 //+---------------------------------------------------------------------
2865 //+ first the opening preamble +
2866 //+---------------------------------------------------------------------
2868 os << "<tgroup cols=\"" << ncols()
2869 << "\" colsep=\"1\" rowsep=\"1\">\n";
2871 for (col_type c = 0; c < ncols(); ++c) {
2872 os << "<colspec colname=\"col" << c << "\" align=\"";
2873 switch (column_info[c].alignment) {
2874 case LYX_ALIGN_LEFT:
2877 case LYX_ALIGN_RIGHT:
2885 if (runparams.flavor == OutputParams::XML)
2891 //+---------------------------------------------------------------------
2892 //+ Long Tabular case +
2893 //+---------------------------------------------------------------------
2895 // output caption info
2896 // The caption flag wins over head/foot
2897 if (haveLTCaption()) {
2898 os << "<caption>\n";
2900 for (row_type r = 0; r < nrows(); ++r) {
2901 if (row_info[r].caption) {
2902 ret += docbookRow(os, r, runparams);
2905 os << "</caption>\n";
2908 // output header info
2909 if (haveLTHead(false) || haveLTFirstHead(false)) {
2912 for (row_type r = 0; r < nrows(); ++r) {
2913 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2914 !row_info[r].caption) {
2915 ret += docbookRow(os, r, runparams);
2921 // output footer info
2922 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2925 for (row_type r = 0; r < nrows(); ++r) {
2926 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2927 !row_info[r].caption) {
2928 ret += docbookRow(os, r, runparams);
2935 //+---------------------------------------------------------------------
2936 //+ the single row and columns (cells) +
2937 //+---------------------------------------------------------------------
2941 for (row_type r = 0; r < nrows(); ++r) {
2942 if (isValidRow(r)) {
2943 ret += docbookRow(os, r, runparams);
2948 //+---------------------------------------------------------------------
2949 //+ the closing of the tabular +
2950 //+---------------------------------------------------------------------
2959 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2960 OutputParams const & runparams, bool header) const
2963 string const celltag = header ? "th" : "td";
2964 idx_type cell = getFirstCellInRow(row);
2966 xs << html::StartTag("tr");
2967 for (col_type c = 0; c < ncols(); ++c) {
2968 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2973 switch (getAlignment(cell)) {
2974 case LYX_ALIGN_LEFT:
2977 case LYX_ALIGN_RIGHT:
2985 attr << " valign='";
2986 switch (getVAlignment(cell)) {
2987 case LYX_VALIGN_TOP:
2990 case LYX_VALIGN_BOTTOM:
2993 case LYX_VALIGN_MIDDLE:
2998 if (isMultiColumn(cell))
2999 attr << " colspan='" << columnSpan(cell) << "'";
3000 else if (isMultiRow(cell))
3001 attr << " rowspan='" << rowSpan(cell) << "'";
3003 xs << html::StartTag(celltag, attr.str()) << html::CR();
3004 ret += cellInset(cell)->xhtml(xs, runparams);
3005 xs << html::EndTag(celltag) << html::CR();
3008 xs << html::EndTag("tr");
3013 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3017 if (is_long_tabular) {
3018 // we'll wrap it in a div, so as to deal with alignment
3020 switch (longtabular_alignment) {
3021 case LYX_LONGTABULAR_ALIGN_LEFT:
3024 case LYX_LONGTABULAR_ALIGN_CENTER:
3027 case LYX_LONGTABULAR_ALIGN_RIGHT:
3031 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3033 // The caption flag wins over head/foot
3034 if (haveLTCaption()) {
3035 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3037 for (row_type r = 0; r < nrows(); ++r)
3038 if (row_info[r].caption)
3039 ret += xhtmlRow(xs, r, runparams);
3040 xs << html::EndTag("div") << html::CR();
3044 xs << html::StartTag("table") << html::CR();
3046 // output header info
3047 bool const havefirsthead = haveLTFirstHead(false);
3048 // if we have a first head, then we are going to ignore the
3049 // headers for the additional pages, since there aren't any
3050 // in XHTML. this test accomplishes that.
3051 bool const havehead = !havefirsthead && haveLTHead(false);
3052 if (havehead || havefirsthead) {
3053 xs << html::StartTag("thead") << html::CR();
3054 for (row_type r = 0; r < nrows(); ++r) {
3055 if (((havefirsthead && row_info[r].endfirsthead) ||
3056 (havehead && row_info[r].endhead)) &&
3057 !row_info[r].caption) {
3058 ret += xhtmlRow(xs, r, runparams, true);
3061 xs << html::EndTag("thead") << html::CR();
3063 // output footer info
3064 bool const havelastfoot = haveLTLastFoot(false);
3066 bool const havefoot = !havelastfoot && haveLTFoot(false);
3067 if (havefoot || havelastfoot) {
3068 xs << html::StartTag("tfoot") << html::CR();
3069 for (row_type r = 0; r < nrows(); ++r) {
3070 if (((havelastfoot && row_info[r].endlastfoot) ||
3071 (havefoot && row_info[r].endfoot)) &&
3072 !row_info[r].caption) {
3073 ret += xhtmlRow(xs, r, runparams);
3076 xs << html::EndTag("tfoot") << html::CR();
3079 xs << html::StartTag("tbody") << html::CR();
3080 for (row_type r = 0; r < nrows(); ++r) {
3081 if (isValidRow(r)) {
3082 ret += xhtmlRow(xs, r, runparams);
3085 xs << html::EndTag("tbody")
3087 << html::EndTag("table")
3089 if (is_long_tabular)
3090 xs << html::EndTag("div") << html::CR();
3095 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3096 vector<unsigned int> const & clen) const
3098 idx_type const fcell = getFirstCellInRow(row);
3099 idx_type const n = numberOfCellsInRow(row) + fcell;
3102 for (idx_type i = fcell; i < n; ++i) {
3112 for (idx_type i = fcell; i < n; ++i) {
3123 col_type column = cellColumn(i);
3124 int len = clen[column];
3125 while (column < ncols() - 1
3126 && isPartOfMultiColumn(row, ++column))
3127 len += clen[column] + 4;
3128 os << docstring(len, ch);
3143 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3144 vector<unsigned int> const & clen) const
3146 idx_type const fcell = getFirstCellInRow(row);
3147 idx_type const n = numberOfCellsInRow(row) + fcell;
3150 for (idx_type i = fcell; i < n; ++i) {
3151 if (bottomLine(i)) {
3160 for (idx_type i = fcell; i < n; ++i) {
3161 if (bottomLine(i)) {
3171 col_type column = cellColumn(i);
3172 int len = clen[column];
3173 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3174 len += clen[column] + 4;
3175 os << docstring(len, ch);
3176 if (bottomLine(i)) {
3190 void Tabular::plaintextPrintCell(odocstringstream & os,
3191 OutputParams const & runparams,
3192 idx_type cell, row_type row, col_type column,
3193 vector<unsigned int> const & clen,
3194 bool onlydata, size_t max_length) const
3196 odocstringstream sstr;
3197 cellInset(cell)->plaintext(sstr, runparams, max_length);
3209 unsigned int len1 = sstr.str().length();
3210 unsigned int len2 = clen[column];
3211 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3212 len2 += clen[column] + 4;
3215 switch (getAlignment(cell)) {
3217 case LYX_ALIGN_LEFT:
3220 case LYX_ALIGN_RIGHT:
3224 case LYX_ALIGN_CENTER:
3230 os << docstring(len1, ' ') << sstr.str()
3231 << docstring(len2, ' ');
3233 if (rightLine(cell))
3240 void Tabular::plaintext(odocstringstream & os,
3241 OutputParams const & runparams, int const depth,
3242 bool onlydata, char_type delim, size_t max_length) const
3244 // first calculate the width of the single columns
3245 vector<unsigned int> clen(ncols());
3248 // first all non multicolumn cells!
3249 for (col_type c = 0; c < ncols(); ++c) {
3251 for (row_type r = 0; r < nrows(); ++r) {
3252 idx_type cell = cellIndex(r, c);
3253 if (isMultiColumn(cell))
3255 odocstringstream sstr;
3256 cellInset(cell)->plaintext(sstr, runparams, max_length);
3257 if (clen[c] < sstr.str().length())
3258 clen[c] = sstr.str().length();
3261 // then all multicolumn cells!
3262 for (col_type c = 0; c < ncols(); ++c) {
3263 for (row_type r = 0; r < nrows(); ++r) {
3264 idx_type cell = cellIndex(r, c);
3265 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3267 odocstringstream sstr;
3268 cellInset(cell)->plaintext(sstr, runparams, max_length);
3269 int len = int(sstr.str().length());
3270 idx_type const n = columnSpan(cell);
3271 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3273 if (len > int(clen[c + n - 1]))
3274 clen[c + n - 1] = len;
3279 for (row_type r = 0; r < nrows(); ++r) {
3280 if (!onlydata && plaintextTopHLine(os, r, clen))
3281 os << docstring(depth * 2, ' ');
3282 for (col_type c = 0; c < ncols(); ++c) {
3283 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3285 if (onlydata && c > 0)
3286 // we don't use operator<< for single UCS4 character.
3287 // see explanation in docstream.h
3289 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3291 if (os.str().size() > max_length)
3296 os << docstring(depth * 2, ' ');
3297 if (plaintextBottomHLine(os, r, clen))
3298 os << docstring(depth * 2, ' ');
3300 if (os.str().size() > max_length)
3306 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3308 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3312 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3313 col_type column) const
3315 return cell_info[row][column].inset;
3319 void Tabular::setCellInset(row_type row, col_type column,
3320 shared_ptr<InsetTableCell> ins) const
3322 CellData & cd = cell_info[row][column];
3327 void Tabular::validate(LaTeXFeatures & features) const
3329 features.require("NeedTabularnewline");
3331 features.require("booktabs");
3332 if (is_long_tabular)
3333 features.require("longtable");
3335 features.require("rotating");
3336 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3337 if (isMultiRow(cell))
3338 features.require("multirow");
3339 if (getVAlignment(cell) != LYX_VALIGN_TOP
3340 || !getPWidth(cell).zero())
3341 features.require("array");
3342 cellInset(cell)->validate(features);
3347 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3349 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3350 ParagraphList::const_iterator cit = parlist.begin();
3351 ParagraphList::const_iterator end = parlist.end();
3353 for (; cit != end; ++cit)
3354 for (int i = 0; i < cit->size(); ++i)
3355 if (cit->isNewline(i))
3362 /////////////////////////////////////////////////////////////////////
3366 /////////////////////////////////////////////////////////////////////
3368 InsetTableCell::InsetTableCell(Buffer * buf)
3369 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3370 contentAlign(LYX_ALIGN_CENTER)
3374 bool InsetTableCell::forcePlainLayout(idx_type) const
3376 return !isFixedWidth;
3380 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3382 return isFixedWidth;
3386 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3387 FuncStatus & status) const
3389 bool enabled = true;
3390 switch (cmd.action()) {
3392 enabled = !forcePlainLayout();
3394 case LFUN_LAYOUT_PARAGRAPH:
3395 enabled = allowParagraphCustomization();
3398 case LFUN_MATH_DISPLAY:
3399 if (!hasFixedWidth()) {
3404 return InsetText::getStatus(cur, cmd, status);
3406 status.setEnabled(enabled);
3410 docstring InsetTableCell::asString(bool intoInsets)
3413 if (paragraphs().empty())
3415 ParagraphList::const_iterator it = paragraphs().begin();
3416 ParagraphList::const_iterator en = paragraphs().end();
3418 for (; it != en; ++it) {
3423 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3429 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3432 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3433 return InsetText::xhtml(xs, rp);
3438 /////////////////////////////////////////////////////////////////////
3442 /////////////////////////////////////////////////////////////////////
3444 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3446 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3447 rowselect_(false), colselect_(false)
3452 InsetTabular::InsetTabular(InsetTabular const & tab)
3453 : Inset(tab), tabular(tab.tabular), scx_(0)
3458 InsetTabular::~InsetTabular()
3460 hideDialogs("tabular", this);
3464 void InsetTabular::setBuffer(Buffer & buf)
3466 tabular.setBuffer(buf);
3467 Inset::setBuffer(buf);
3471 bool InsetTabular::insetAllowed(InsetCode code) const
3476 case MATHMACRO_CODE:
3481 return tabular.is_long_tabular;
3489 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3491 return tabular.is_long_tabular &&
3492 (newtype == "Standard" || newtype == "LongTableNoNumber");
3496 void InsetTabular::write(ostream & os) const
3498 os << "Tabular" << endl;
3503 string InsetTabular::contextMenu(BufferView const &, int, int) const
3505 // FIXME: depending on the selection state,
3506 // we could offer a different menu.
3507 return cell(0)->contextMenuName() + ";" + contextMenuName();
3511 string InsetTabular::contextMenuName() const
3513 return "context-tabular";
3517 void InsetTabular::read(Lexer & lex)
3519 //bool const old_format = (lex.getString() == "\\LyXTable");
3527 string token = lex.getString();
3528 while (lex && token != "\\end_inset") {
3530 token = lex.getString();
3533 lex.printError("Missing \\end_inset at this point. ");
3537 int InsetTabular::rowFromY(Cursor & cur, int y) const
3539 // top y coordinate of tabular
3540 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3542 for (; r < tabular.nrows() && y > h; ++r)
3543 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3544 + tabular.interRowSpace(r);
3550 int InsetTabular::columnFromX(Cursor & cur, int x) const
3552 // left x coordinate of tabular
3553 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3555 for (; c < tabular.ncols() && x > w; ++c)
3556 w += tabular.cellWidth(c);
3561 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3563 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3564 // mi.base.textwidth << "\n";
3565 LBUFERR(mi.base.bv);
3567 for (row_type r = 0; r < tabular.nrows(); ++r) {
3570 for (col_type c = 0; c < tabular.ncols(); ++c) {
3571 if (tabular.isPartOfMultiColumn(r, c)
3572 || tabular.isPartOfMultiRow(r, c))
3573 // multicolumn or multirow cell, but not first one
3575 idx_type const cell = tabular.cellIndex(r, c);
3578 Length const p_width = tabular.getPWidth(cell);
3579 if (!p_width.zero())
3580 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3581 tabular.cellInset(cell)->metrics(m, dim);
3582 if (!p_width.zero())
3583 dim.wid = m.base.textwidth;
3584 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3585 + tabular.interColumnSpace(cell);
3587 // FIXME(?): do we need a second metrics call?
3588 TextMetrics const & tm =
3589 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3591 // determine horizontal offset because of decimal align (if necessary)
3592 int decimal_width = 0;
3593 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3594 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3595 tail.setBuffer(tabular.buffer());
3596 // we need to set macrocontext position everywhere
3597 // otherwise we crash with nested insets (e.g. footnotes)
3598 // after decimal point
3599 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3601 dit.push_back(CursorSlice(tail));
3602 tail.setMacrocontextPositionRecursive(dit);
3604 // remove text leading decimal point
3605 docstring const align_d = tabular.column_info[c].decimal_point;
3606 dit = separatorPos(&tail, align_d);
3608 pit_type const psize = tail.paragraphs().front().size();
3610 tail.paragraphs().front().eraseChars(0,
3611 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3613 tail.metrics(m, dim1);
3614 decimal_width = dim1.width();
3617 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3618 tabular.cell_info[r][c].decimal_width = decimal_width;
3620 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3621 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3622 int const lastpardes = tm.last().second->descent()
3623 + TEXT_TO_INSET_OFFSET;
3625 switch (tabular.getVAlignment(cell)) {
3626 case Tabular::LYX_VALIGN_TOP:
3628 case Tabular::LYX_VALIGN_MIDDLE:
3629 offset = -(dim.des - lastpardes)/2;
3631 case Tabular::LYX_VALIGN_BOTTOM:
3632 offset = -(dim.des - lastpardes);
3635 tabular.cell_info[r][c].voffset = offset;
3636 maxasc = max(maxasc, dim.asc - offset);
3637 maxdes = max(maxdes, dim.des + offset);
3639 int const top_space = tabular.row_info[r].top_space_default ?
3640 default_line_space :
3641 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3642 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3643 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3644 default_line_space :
3645 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3646 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3649 // for top-alignment the first horizontal table line must be exactly at
3650 // the position of the base line of the surrounding text line
3651 // for bottom alignment, the same is for the last table line
3652 switch (tabular.tabular_valignment) {
3653 case Tabular::LYX_VALIGN_BOTTOM:
3654 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3656 case Tabular::LYX_VALIGN_MIDDLE:
3657 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3659 case Tabular::LYX_VALIGN_TOP:
3660 offset_valign_ = tabular.rowAscent(0);
3664 tabular.updateColumnWidths();
3665 dim.asc = tabular.rowAscent(0) - offset_valign_;
3666 dim.des = tabular.height() - dim.asc;
3667 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3671 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3674 if (&cur.inset() == this && cur.selection()) {
3675 if (cur.selIsMultiCell()) {
3678 getSelection(cur, rs, re, cs, ce);
3680 idx_type const cell = tabular.cellIndex(row, col);
3681 col_type const cspan = tabular.columnSpan(cell);
3682 row_type const rspan = tabular.rowSpan(cell);
3683 if (col + cspan - 1 >= cs && col <= ce
3684 && row + rspan - 1 >= rs && row <= re)
3687 if (col == tabular.cellColumn(cur.idx())
3688 && row == tabular.cellRow(cur.idx())) {
3689 CursorSlice const & beg = cur.selBegin();
3690 CursorSlice const & end = cur.selEnd();
3692 if ((end.lastpos() > 0 || end.lastpit() > 0)
3693 && end.pos() == end.lastpos() && beg.pos() == 0
3694 && end.pit() == end.lastpit() && beg.pit() == 0)
3702 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3704 x += scx_ + ADD_TO_TABULAR_WIDTH;
3706 BufferView * bv = pi.base.bv;
3707 Cursor & cur = pi.base.bv->cursor();
3710 // FIXME: As the full background is painted in drawBackground(),
3711 // we have no choice but to do a full repaint for the Text cells.
3712 pi.full_repaint = true;
3714 bool const original_selection_state = pi.selected;
3717 first_visible_cell = Tabular::npos;
3719 int yy = y + offset_valign_;
3720 for (row_type r = 0; r < tabular.nrows(); ++r) {
3722 for (col_type c = 0; c < tabular.ncols(); ++c) {
3723 if (tabular.isPartOfMultiColumn(r, c))
3726 idx = tabular.cellIndex(r, c);
3728 if (tabular.isPartOfMultiRow(r, c)) {
3729 nx += tabular.cellWidth(idx);
3733 if (first_visible_cell == Tabular::npos)
3734 first_visible_cell = idx;
3736 pi.selected |= isCellSelected(cur, r, c);
3737 int const cx = nx + tabular.textHOffset(idx);
3738 int const cy = yy + tabular.textVOffset(idx);
3739 // Cache the Inset position.
3740 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3741 cell(idx)->draw(pi, cx, cy);
3742 drawCellLines(pi, nx, yy, r, idx);
3743 nx += tabular.cellWidth(idx);
3744 pi.selected = original_selection_state;
3747 if (r + 1 < tabular.nrows())
3748 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3749 + tabular.interRowSpace(r + 1);
3754 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3756 x += scx_ + ADD_TO_TABULAR_WIDTH;
3757 y += offset_valign_ - tabular.rowAscent(0);
3758 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3759 pi.backgroundColor(this));
3763 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3765 Cursor & cur = pi.base.bv->cursor();
3768 x += scx_ + ADD_TO_TABULAR_WIDTH;
3770 if (!cur.selection())
3772 if (&cur.inset() != this)
3777 bool const full_cell_selected = isCellSelected(cur,
3778 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3780 if (cur.selIsMultiCell() || full_cell_selected) {
3781 for (row_type r = 0; r < tabular.nrows(); ++r) {
3783 for (col_type c = 0; c < tabular.ncols(); ++c) {
3784 if (tabular.isPartOfMultiColumn(r, c))
3787 idx_type const cell = tabular.cellIndex(r, c);
3789 if (tabular.isPartOfMultiRow(r, c)) {
3790 xx += tabular.cellWidth(cell);
3793 int const w = tabular.cellWidth(cell);
3794 int const h = tabular.cellHeight(cell);
3795 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3796 if (isCellSelected(cur, r, c))
3797 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3800 if (r + 1 < tabular.nrows())
3801 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3802 + tabular.interRowSpace(r + 1);
3806 // FIXME: This code has no effect because InsetTableCell does not handle
3807 // drawSelection other than the trivial implementation in Inset.
3809 // x += cellXPos(cur.idx());
3810 // x += tabular.textHOffset(cur.idx());
3811 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3816 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3817 row_type row, idx_type cell) const
3819 y -= tabular.rowAscent(row);
3820 int const w = tabular.cellWidth(cell);
3821 int const h = tabular.cellHeight(cell);
3822 Color const linecolor = pi.textColor(Color_tabularline);
3823 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3826 bool drawline = tabular.topLine(cell)
3827 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3828 pi.pain.line(x, y, x + w, y,
3829 drawline ? linecolor : gridcolor,
3830 drawline ? Painter::line_solid : Painter::line_onoffdash);
3833 drawline = tabular.bottomLine(cell);
3834 pi.pain.line(x, y + h, x + w, y + h,
3835 drawline ? linecolor : gridcolor,
3836 drawline ? Painter::line_solid : Painter::line_onoffdash);
3839 col_type const col = tabular.cellColumn(cell);
3840 drawline = tabular.leftLine(cell)
3841 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3842 pi.pain.line(x, y, x, y + h,
3843 drawline ? linecolor : gridcolor,
3844 drawline ? Painter::line_solid : Painter::line_onoffdash);
3847 x -= tabular.interColumnSpace(cell);
3848 col_type next_cell_col = col + 1;
3849 while (next_cell_col < tabular.ncols()
3850 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3852 drawline = tabular.rightLine(cell)
3853 || (next_cell_col < tabular.ncols()
3854 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3855 pi.pain.line(x + w, y, x + w, y + h,
3856 drawline ? linecolor : gridcolor,
3857 drawline ? Painter::line_solid : Painter::line_onoffdash);
3861 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3863 //lyxerr << "InsetTabular::edit: " << this << endl;
3865 cur.setSelection(false);
3868 if (isRightToLeft(cur))
3869 cur.idx() = tabular.getLastCellInRow(0);
3875 if (isRightToLeft(cur))
3876 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3878 cur.idx() = tabular.numberofcells - 1;
3880 cur.pos() = cur.lastpos(); // FIXME crude guess
3882 cur.setCurrentFont();
3883 // FIXME: this accesses the position cache before it is initialized
3885 //cur.bv().fitCursor();
3889 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3891 // In a longtable, tell captions what the current float is
3892 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3893 string const saveflt = cnts.current_float();
3894 if (tabular.is_long_tabular)
3895 cnts.current_float("table");
3897 ParIterator it2 = it;
3899 size_t const end = it2.nargs();
3900 for ( ; it2.idx() < end; it2.top().forwardIdx())
3901 buffer().updateBuffer(it2, utype);
3904 if (tabular.is_long_tabular)
3905 cnts.current_float(saveflt);
3909 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active) const
3911 DocIterator dit = cpit;
3913 size_t const end = dit.nargs();
3914 for ( ; dit.idx() < end; dit.top().forwardIdx())
3915 cell(dit.idx())->addToToc(dit, output_active);
3919 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3921 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3922 << "\n cur:" << cur);
3923 CursorSlice sl = cur.top();
3924 Cursor & bvcur = cur.bv().cursor();
3926 FuncCode const act = cmd.action();
3930 case LFUN_MOUSE_PRESS: {
3931 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3933 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3934 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3935 row_type r = rowFromY(cur, cmd.y());
3936 cur.idx() = tabular.getFirstCellInRow(r);
3939 cur.idx() = tabular.getLastCellInRow(r);
3940 cur.pos() = cur.lastpos();
3941 cur.setSelection(true);
3947 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3948 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3949 || cmd.y() > y0 + tabular.height()) {
3950 col_type c = columnFromX(cur, cmd.x());
3951 cur.idx() = tabular.cellIndex(0, c);
3954 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3955 cur.pos() = cur.lastpos();
3956 cur.setSelection(true);
3961 // do not reset cursor/selection if we have selected
3962 // some cells (bug 2715).
3963 if (cmd.button() == mouse_button::button3
3964 && &bvcur.selBegin().inset() == this
3965 && bvcur.selIsMultiCell())
3968 // Let InsetTableCell do it
3969 cell(cur.idx())->dispatch(cur, cmd);
3972 case LFUN_MOUSE_MOTION:
3973 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3974 if (cmd.button() == mouse_button::button1) {
3975 // only accept motions to places not deeper nested than the real anchor
3976 if (!bvcur.realAnchor().hasPart(cur)) {
3980 // select (additional) row
3982 row_type r = rowFromY(cur, cmd.y());
3983 cur.idx() = tabular.getLastCellInRow(r);
3984 // we need to reset the cursor's pit and pos now, as the old ones
3985 // may no longer be valid.
3988 bvcur.setCursor(cur);
3989 bvcur.setSelection(true);
3992 // select (additional) column
3994 col_type c = columnFromX(cur, cmd.x());
3995 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3996 // we need to reset the cursor's pit and pos now, as the old ones
3997 // may no longer be valid.
4000 bvcur.setCursor(cur);
4001 bvcur.setSelection(true);
4004 // only update if selection changes
4005 if (bvcur.idx() == cur.idx() &&
4006 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4007 cur.noScreenUpdate();
4008 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4009 bvcur.setCursor(cur);
4010 bvcur.setSelection(true);
4011 // if this is a multicell selection, we just set the cursor to
4012 // the beginning of the cell's text.
4013 if (bvcur.selIsMultiCell()) {
4014 bvcur.pit() = bvcur.lastpit();
4015 bvcur.pos() = bvcur.lastpos();
4020 case LFUN_MOUSE_RELEASE:
4025 case LFUN_CELL_BACKWARD:
4027 cur.setSelection(false);
4030 case LFUN_CELL_FORWARD:
4032 cur.setSelection(false);
4035 case LFUN_CHAR_FORWARD_SELECT:
4036 case LFUN_CHAR_FORWARD:
4037 case LFUN_CHAR_BACKWARD_SELECT:
4038 case LFUN_CHAR_BACKWARD:
4039 case LFUN_CHAR_RIGHT_SELECT:
4040 case LFUN_CHAR_RIGHT:
4041 case LFUN_CHAR_LEFT_SELECT:
4042 case LFUN_CHAR_LEFT:
4043 case LFUN_WORD_FORWARD:
4044 case LFUN_WORD_FORWARD_SELECT:
4045 case LFUN_WORD_BACKWARD:
4046 case LFUN_WORD_BACKWARD_SELECT:
4047 case LFUN_WORD_RIGHT:
4048 case LFUN_WORD_RIGHT_SELECT:
4049 case LFUN_WORD_LEFT:
4050 case LFUN_WORD_LEFT_SELECT: {
4051 // determine whether we move to next or previous cell, where to enter
4052 // the new cell from, and which command to "finish" (i.e., exit the
4055 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4056 FuncCode finish_lfun;
4058 if (act == LFUN_CHAR_FORWARD
4059 || act == LFUN_CHAR_FORWARD_SELECT
4060 || act == LFUN_WORD_FORWARD
4061 || act == LFUN_WORD_FORWARD_SELECT) {
4063 finish_lfun = LFUN_FINISHED_FORWARD;
4065 else if (act == LFUN_CHAR_BACKWARD
4066 || act == LFUN_CHAR_BACKWARD_SELECT
4067 || act == LFUN_WORD_BACKWARD
4068 || act == LFUN_WORD_BACKWARD_SELECT) {
4070 finish_lfun = LFUN_FINISHED_BACKWARD;
4072 // LEFT or RIGHT commands --- the interpretation will depend on the
4073 // table's direction.
4075 bool const right = act == LFUN_CHAR_RIGHT
4076 || act == LFUN_CHAR_RIGHT_SELECT
4077 || act == LFUN_WORD_RIGHT
4078 || act == LFUN_WORD_RIGHT_SELECT;
4079 next_cell = isRightToLeft(cur) != right;
4081 if (lyxrc.visual_cursor)
4082 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4084 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4087 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4088 || act == LFUN_CHAR_BACKWARD_SELECT
4089 || act == LFUN_CHAR_RIGHT_SELECT
4090 || act == LFUN_CHAR_LEFT_SELECT
4091 || act == LFUN_WORD_FORWARD_SELECT
4092 || act == LFUN_WORD_RIGHT_SELECT
4093 || act == LFUN_WORD_BACKWARD_SELECT
4094 || act == LFUN_WORD_LEFT_SELECT;
4096 // If we have a multicell selection or we're
4097 // not doing some LFUN_*_SELECT thing anyway...
4098 if (!cur.selIsMultiCell() || !select) {
4099 col_type const c = tabular.cellColumn(cur.idx());
4100 row_type const r = tabular.cellRow(cur.idx());
4101 // Are we trying to select the whole cell and is the whole cell
4102 // not yet selected?
4103 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4104 ((next_cell && cur.pit() == cur.lastpit()
4105 && cur.pos() == cur.lastpos())
4106 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4108 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4110 // ...try to dispatch to the cell's inset.
4111 cell(cur.idx())->dispatch(cur, cmd);
4113 // When we already have a selection we want to select the whole cell
4114 // before going to the next cell.
4115 if (select_whole && !empty_cell){
4116 getText(cur.idx())->selectAll(cur);
4118 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4122 // FIXME: When we support the selection of an empty cell, remove
4123 // the !empty_cell from this condition. For now we jump to the next
4124 // cell if the current cell is empty.
4125 if (cur.result().dispatched() && !empty_cell)
4129 // move to next/prev cell, as appropriate
4130 // note that we will always do this if we're selecting and we have
4131 // a multicell selection
4132 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4133 << " cell from: " << int(entry_from));
4135 moveNextCell(cur, entry_from);
4137 movePrevCell(cur, entry_from);
4138 // if we're exiting the table, call the appropriate FINISHED lfun
4139 if (sl == cur.top())
4140 cmd = FuncRequest(finish_lfun);
4144 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4149 case LFUN_DOWN_SELECT:
4151 if (!(cur.selection() && cur.selIsMultiCell()))
4152 cell(cur.idx())->dispatch(cur, cmd);
4154 cur.dispatched(); // override the cell's decision
4155 if (sl == cur.top()) {
4156 // if our Text didn't do anything to the cursor
4157 // then we try to put the cursor into the cell below
4158 // setting also the right targetX.
4159 cur.selHandle(act == LFUN_DOWN_SELECT);
4160 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4161 int const xtarget = cur.targetX();
4162 // WARNING: Once cur.idx() has been reset, the cursor is in
4163 // an inconsistent state until pos() has been set. Be careful
4164 // what you do with it!
4165 cur.idx() = tabular.cellBelow(cur.idx());
4167 TextMetrics const & tm =
4168 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4169 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4170 cur.setCurrentFont();
4173 if (sl == cur.top()) {
4174 // we trick it to go to forward after leaving the
4176 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4179 if (cur.selIsMultiCell()) {
4180 cur.pit() = cur.lastpit();
4181 cur.pos() = cur.lastpos();
4182 cur.setCurrentFont();
4183 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4186 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4189 case LFUN_UP_SELECT:
4191 if (!(cur.selection() && cur.selIsMultiCell()))
4192 cell(cur.idx())->dispatch(cur, cmd);
4193 cur.dispatched(); // override the cell's decision
4194 if (sl == cur.top()) {
4195 // if our Text didn't do anything to the cursor
4196 // then we try to put the cursor into the cell above
4197 // setting also the right targetX.
4198 cur.selHandle(act == LFUN_UP_SELECT);
4199 if (tabular.cellRow(cur.idx()) != 0) {
4200 int const xtarget = cur.targetX();
4201 // WARNING: Once cur.idx() has been reset, the cursor is in
4202 // an inconsistent state until pos() has been set. Be careful
4203 // what you do with it!
4204 cur.idx() = tabular.cellAbove(cur.idx());
4205 cur.pit() = cur.lastpit();
4206 Text const * text = cell(cur.idx())->getText(0);
4207 TextMetrics const & tm = cur.bv().textMetrics(text);
4208 ParagraphMetrics const & pm =
4209 tm.parMetrics(cur.lastpit());
4210 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4211 cur.setCurrentFont();
4214 if (sl == cur.top()) {
4215 cmd = FuncRequest(LFUN_UP);
4218 if (cur.selIsMultiCell()) {
4220 cur.pos() = cur.lastpos();
4221 cur.setCurrentFont();
4222 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4225 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4228 // case LFUN_SCREEN_DOWN: {
4229 // //if (hasSelection())
4230 // // cur.selection() = false;
4231 // col_type const col = tabular.cellColumn(cur.idx());
4232 // int const t = cur.bv().top_y() + cur.bv().height();
4233 // if (t < yo() + tabular.getHeightOfTabular()) {
4234 // cur.bv().scrollDocView(t, true);
4235 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4237 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4244 // case LFUN_SCREEN_UP: {
4245 // //if (hasSelection())
4246 // // cur.selection() = false;
4247 // col_type const col = tabular.cellColumn(cur.idx());
4248 // int const t = cur.bv().top_y() + cur.bv().height();
4250 // cur.bv().scrollDocView(t, true);
4254 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4258 // cur.par() = cur.lastpar();
4259 // cur.pos() = cur.lastpos();
4263 case LFUN_LAYOUT_TABULAR:
4264 cur.bv().showDialog("tabular");
4267 case LFUN_INSET_MODIFY: {
4269 if (cmd.getArg(1) == "from-dialog")
4270 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4272 arg = to_utf8(cmd.argument());
4273 if (!tabularFeatures(cur, arg))
4278 // insert file functions
4279 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4280 case LFUN_FILE_INSERT_PLAINTEXT:
4282 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4283 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4284 FileName(to_utf8(cmd.argument())));
4287 cur.recordUndoInset(INSERT_UNDO);
4288 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4289 // content has been replaced,
4290 // so cursor might be invalid
4291 cur.pos() = cur.lastpos();
4292 cur.pit() = cur.lastpit();
4293 bvcur.setCursor(cur);
4300 if (cur.selIsMultiCell()) {
4301 if (copySelection(cur)) {
4302 cur.recordUndoInset(DELETE_UNDO);
4306 cell(cur.idx())->dispatch(cur, cmd);
4309 case LFUN_SELF_INSERT:
4310 if (cur.selIsMultiCell()) {
4311 cur.recordUndoInset(DELETE_UNDO);
4314 cell(cur.idx())->dispatch(cur, cmd);
4317 case LFUN_CHAR_DELETE_BACKWARD:
4318 case LFUN_CHAR_DELETE_FORWARD:
4319 if (cur.selIsMultiCell()) {
4320 cur.recordUndoInset(DELETE_UNDO);
4323 cell(cur.idx())->dispatch(cur, cmd);
4327 if (!cur.selection())
4329 if (cur.selIsMultiCell()) {
4333 cell(cur.idx())->dispatch(cur, cmd);
4336 case LFUN_CLIPBOARD_PASTE:
4337 case LFUN_PRIMARY_SELECTION_PASTE: {
4338 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4339 theClipboard().getAsText(Clipboard::PlainTextType) :
4340 theSelection().get();
4343 // pass to InsertPlaintextString, but
4344 // only if we have multi-cell content
4345 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4346 cur.recordUndoInset(INSERT_UNDO);
4347 if (insertPlaintextString(cur.bv(), clip, false)) {
4348 // content has been replaced,
4349 // so cursor might be invalid
4350 cur.pos() = cur.lastpos();
4351 cur.pit() = cur.lastpit();
4352 bvcur.setCursor(cur);
4356 // Let the cell handle normal text
4357 cell(cur.idx())->dispatch(cur, cmd);
4362 if (!tabularStackDirty()) {
4363 if (!cur.selIsMultiCell())
4364 cell(cur.idx())->dispatch(cur, cmd);
4367 if (theClipboard().isInternal()) {
4368 cur.recordUndoInset(INSERT_UNDO);
4369 pasteClipboard(cur);
4373 case LFUN_FONT_EMPH:
4374 case LFUN_FONT_BOLD:
4375 case LFUN_FONT_BOLDSYMBOL:
4376 case LFUN_FONT_ROMAN:
4377 case LFUN_FONT_NOUN:
4378 case LFUN_FONT_ITAL:
4379 case LFUN_FONT_FRAK:
4380 case LFUN_FONT_TYPEWRITER:
4381 case LFUN_FONT_SANS:
4382 case LFUN_TEXTSTYLE_APPLY:
4383 case LFUN_TEXTSTYLE_UPDATE:
4384 case LFUN_FONT_SIZE:
4385 case LFUN_FONT_UNDERLINE:
4386 case LFUN_FONT_STRIKEOUT:
4387 case LFUN_FONT_UNDERUNDERLINE:
4388 case LFUN_FONT_UNDERWAVE:
4390 case LFUN_PARAGRAPH_PARAMS_APPLY:
4391 case LFUN_PARAGRAPH_PARAMS:
4392 case LFUN_WORD_CAPITALIZE:
4393 case LFUN_WORD_UPCASE:
4394 case LFUN_WORD_LOWCASE:
4395 case LFUN_CHARS_TRANSPOSE:
4396 if (cur.selIsMultiCell()) {
4399 getSelection(cur, rs, re, cs, ce);
4400 Cursor tmpcur = cur;
4401 for (row_type r = rs; r <= re; ++r) {
4402 for (col_type c = cs; c <= ce; ++c) {
4403 // cursor follows cell:
4404 tmpcur.idx() = tabular.cellIndex(r, c);
4405 // select this cell only:
4408 tmpcur.resetAnchor();
4409 tmpcur.pit() = tmpcur.lastpit();
4410 tmpcur.pos() = tmpcur.top().lastpos();
4411 tmpcur.setCursor(tmpcur);
4412 tmpcur.setSelection();
4413 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4418 cell(cur.idx())->dispatch(cur, cmd);
4422 case LFUN_INSET_SETTINGS:
4423 // relay this lfun to Inset, not to the cell.
4424 Inset::doDispatch(cur, cmd);
4428 // we try to handle this event in the insets dispatch function.
4429 cell(cur.idx())->dispatch(cur, cmd);
4435 // function sets an object as defined in func_status.h:
4436 // states OK, Unknown, Disabled, On, Off.
4437 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4438 FuncStatus & status) const
4440 switch (cmd.action()) {
4441 case LFUN_INSET_MODIFY: {
4442 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4445 // FIXME: We only check for the very first argument...
4446 string const s = cmd.getArg(1);
4447 // We always enable the lfun if it is coming from the dialog
4448 // because the dialog makes sure all the settings are valid,
4449 // even though the first argument might not be valid now.
4450 if (s == "from-dialog") {
4451 status.setEnabled(true);
4455 int action = Tabular::LAST_ACTION;
4457 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4458 if (tabularFeature[i].feature == s) {
4459 action = tabularFeature[i].action;
4463 if (action == Tabular::LAST_ACTION) {
4465 status.setUnknown(true);
4469 string const argument = cmd.getLongArg(2);
4471 row_type sel_row_start = 0;
4472 row_type sel_row_end = 0;
4473 col_type sel_col_start = 0;
4474 col_type sel_col_end = 0;
4475 Tabular::ltType dummyltt;
4478 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4481 case Tabular::SET_PWIDTH:
4482 case Tabular::SET_MPWIDTH:
4483 case Tabular::SET_SPECIAL_COLUMN:
4484 case Tabular::SET_SPECIAL_MULTICOLUMN:
4485 case Tabular::APPEND_ROW:
4486 case Tabular::APPEND_COLUMN:
4487 case Tabular::DELETE_ROW:
4488 case Tabular::DELETE_COLUMN:
4489 case Tabular::COPY_ROW:
4490 case Tabular::COPY_COLUMN:
4491 case Tabular::SET_TOP_SPACE:
4492 case Tabular::SET_BOTTOM_SPACE:
4493 case Tabular::SET_INTERLINE_SPACE:
4497 case Tabular::SET_TABULAR_WIDTH:
4498 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4499 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4502 case Tabular::MOVE_COLUMN_RIGHT:
4503 case Tabular::MOVE_COLUMN_LEFT:
4504 case Tabular::MOVE_ROW_DOWN:
4505 case Tabular::MOVE_ROW_UP: {
4506 if (cur.selection()) {
4507 status.message(_("Selections not supported."));
4508 status.setEnabled(false);
4512 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4513 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4514 (action == Tabular::MOVE_COLUMN_LEFT &&
4515 tabular.cellColumn(cur.idx()) == 0) ||
4516 (action == Tabular::MOVE_ROW_DOWN &&
4517 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4518 (action == Tabular::MOVE_ROW_UP &&
4519 tabular.cellRow(cur.idx()) == 0)) {
4520 status.setEnabled(false);
4524 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4525 action == Tabular::MOVE_COLUMN_LEFT) {
4526 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4527 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4528 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4529 status.message(_("Multi-column in current or"
4530 " destination column."));
4531 status.setEnabled(false);
4536 if (action == Tabular::MOVE_ROW_DOWN ||
4537 action == Tabular::MOVE_ROW_UP) {
4538 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4539 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4540 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4541 status.message(_("Multi-row in current or"
4542 " destination row."));
4543 status.setEnabled(false);
4548 status.setEnabled(true);
4552 case Tabular::SET_DECIMAL_POINT:
4554 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4557 case Tabular::SET_MULTICOLUMN:
4558 case Tabular::UNSET_MULTICOLUMN:
4559 case Tabular::MULTICOLUMN:
4560 // If a row is set as longtable caption, it must not be allowed
4561 // to unset that this row is a multicolumn.
4562 status.setEnabled(sel_row_start == sel_row_end
4563 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4564 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4567 case Tabular::SET_MULTIROW:
4568 case Tabular::UNSET_MULTIROW:
4569 case Tabular::MULTIROW:
4570 // If a row is set as longtable caption, it must not be allowed
4571 // to unset that this row is a multirow.
4572 status.setEnabled(sel_col_start == sel_col_end
4573 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4574 status.setOnOff(tabular.isMultiRow(cur.idx()));
4577 case Tabular::SET_ALL_LINES:
4578 case Tabular::UNSET_ALL_LINES:
4579 case Tabular::SET_BORDER_LINES:
4580 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4583 case Tabular::SET_LINE_TOP:
4584 case Tabular::SET_LINE_BOTTOM:
4585 case Tabular::SET_LINE_LEFT:
4586 case Tabular::SET_LINE_RIGHT:
4587 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4590 case Tabular::TOGGLE_LINE_TOP:
4591 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4592 status.setOnOff(tabular.topLine(cur.idx()));
4595 case Tabular::TOGGLE_LINE_BOTTOM:
4596 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4597 status.setOnOff(tabular.bottomLine(cur.idx()));
4600 case Tabular::TOGGLE_LINE_LEFT:
4601 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4602 status.setOnOff(tabular.leftLine(cur.idx()));
4605 case Tabular::TOGGLE_LINE_RIGHT:
4606 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4607 status.setOnOff(tabular.rightLine(cur.idx()));
4610 // multirow cells only inherit the alignment of the column if the column has
4611 // no width, otherwise they are left-aligned
4612 // therefore allow always left but right and center only if there is no width
4613 case Tabular::M_ALIGN_LEFT:
4615 case Tabular::ALIGN_LEFT:
4616 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4619 case Tabular::M_ALIGN_RIGHT:
4621 case Tabular::ALIGN_RIGHT:
4622 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4623 && !tabular.getPWidth(cur.idx()).zero()));
4624 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4627 case Tabular::M_ALIGN_CENTER:
4629 case Tabular::ALIGN_CENTER:
4630 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4631 && !tabular.getPWidth(cur.idx()).zero()));
4632 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4635 case Tabular::ALIGN_BLOCK:
4636 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4637 && !tabular.isMultiRow(cur.idx()));
4638 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4641 case Tabular::ALIGN_DECIMAL:
4642 status.setEnabled(!tabular.isMultiRow(cur.idx())
4643 && !tabular.isMultiColumn(cur.idx()));
4644 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4647 case Tabular::M_VALIGN_TOP:
4649 case Tabular::VALIGN_TOP:
4650 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4651 && !tabular.isMultiRow(cur.idx()));
4653 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4656 case Tabular::M_VALIGN_BOTTOM:
4658 case Tabular::VALIGN_BOTTOM:
4659 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4660 && !tabular.isMultiRow(cur.idx()));
4662 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4665 case Tabular::M_VALIGN_MIDDLE:
4667 case Tabular::VALIGN_MIDDLE:
4668 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4669 && !tabular.isMultiRow(cur.idx()));
4671 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4674 case Tabular::SET_LONGTABULAR:
4675 // setting as longtable is not allowed when table is inside a float
4676 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4677 || cur.innerInsetOfType(WRAP_CODE) != 0)
4678 status.setEnabled(false);
4680 status.setEnabled(true);
4681 status.setOnOff(tabular.is_long_tabular);
4684 case Tabular::UNSET_LONGTABULAR:
4685 status.setOnOff(!tabular.is_long_tabular);
4688 case Tabular::TOGGLE_ROTATE_TABULAR:
4689 case Tabular::SET_ROTATE_TABULAR:
4690 status.setOnOff(tabular.rotate != 0);
4693 case Tabular::TABULAR_VALIGN_TOP:
4694 status.setEnabled(tabular.tabular_width.zero());
4695 status.setOnOff(tabular.tabular_valignment
4696 == Tabular::LYX_VALIGN_TOP);
4698 case Tabular::TABULAR_VALIGN_MIDDLE:
4699 status.setEnabled(tabular.tabular_width.zero());
4700 status.setOnOff(tabular.tabular_valignment
4701 == Tabular::LYX_VALIGN_MIDDLE);
4703 case Tabular::TABULAR_VALIGN_BOTTOM:
4704 status.setEnabled(tabular.tabular_width.zero());
4705 status.setOnOff(tabular.tabular_valignment
4706 == Tabular::LYX_VALIGN_BOTTOM);
4709 case Tabular::LONGTABULAR_ALIGN_LEFT:
4710 status.setOnOff(tabular.longtabular_alignment
4711 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4713 case Tabular::LONGTABULAR_ALIGN_CENTER:
4714 status.setOnOff(tabular.longtabular_alignment
4715 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4717 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4718 status.setOnOff(tabular.longtabular_alignment
4719 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4722 case Tabular::UNSET_ROTATE_TABULAR:
4723 status.setOnOff(tabular.rotate == 0);
4726 case Tabular::TOGGLE_ROTATE_CELL:
4727 case Tabular::SET_ROTATE_CELL:
4728 status.setOnOff(!oneCellHasRotationState(false,
4729 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4732 case Tabular::UNSET_ROTATE_CELL:
4733 status.setOnOff(!oneCellHasRotationState(true,
4734 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4737 case Tabular::SET_USEBOX:
4738 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4741 // every row can only be one thing:
4742 // either a footer or header
4743 case Tabular::SET_LTFIRSTHEAD:
4744 status.setEnabled(sel_row_start == sel_row_end);
4745 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4748 case Tabular::UNSET_LTFIRSTHEAD:
4749 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4750 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4753 case Tabular::SET_LTHEAD:
4754 status.setEnabled(sel_row_start == sel_row_end);
4755 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4758 case Tabular::UNSET_LTHEAD:
4759 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4760 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4763 case Tabular::SET_LTFOOT:
4764 status.setEnabled(sel_row_start == sel_row_end);
4765 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4768 case Tabular::UNSET_LTFOOT:
4769 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4770 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4773 case Tabular::SET_LTLASTFOOT:
4774 status.setEnabled(sel_row_start == sel_row_end);
4775 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4778 case Tabular::UNSET_LTLASTFOOT:
4779 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4780 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4783 case Tabular::SET_LTNEWPAGE:
4784 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4786 case Tabular::UNSET_LTNEWPAGE:
4787 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4790 // only one row in head/firsthead/foot/lasthead can be the caption
4791 // and a multirow cannot be set as caption
4792 case Tabular::SET_LTCAPTION:
4793 status.setEnabled(sel_row_start == sel_row_end
4794 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4795 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4796 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4797 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4798 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4799 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4800 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4801 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4802 && !tabular.isMultiRow(sel_row_start));
4803 status.setOnOff(tabular.ltCaption(sel_row_start));
4806 case Tabular::UNSET_LTCAPTION:
4807 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4810 case Tabular::TOGGLE_LTCAPTION:
4811 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4812 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4813 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4814 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4815 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4816 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4817 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4818 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4819 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4820 status.setOnOff(tabular.ltCaption(sel_row_start));
4823 case Tabular::SET_BOOKTABS:
4824 status.setOnOff(tabular.use_booktabs);
4827 case Tabular::UNSET_BOOKTABS:
4828 status.setOnOff(!tabular.use_booktabs);
4833 status.setEnabled(false);
4839 case LFUN_CAPTION_INSERT: {
4840 // caption is only allowed in caption cell of longtable
4841 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4842 status.setEnabled(false);
4845 // only standard caption is allowed
4846 string arg = cmd.getArg(0);
4847 if (!arg.empty() && arg != "Standard") {
4848 status.setEnabled(false);
4851 // check if there is already a caption
4852 bool have_caption = false;
4853 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4854 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4855 ParagraphList::const_iterator pend = itc.paragraphs().end();
4856 for (; pit != pend; ++pit) {
4857 InsetList::const_iterator it = pit->insetList().begin();
4858 InsetList::const_iterator end = pit->insetList().end();
4859 for (; it != end; ++it) {
4860 if (it->inset->lyxCode() == CAPTION_CODE) {
4861 have_caption = true;
4866 status.setEnabled(!have_caption);
4870 // These are only enabled inside tabular
4871 case LFUN_CELL_BACKWARD:
4872 case LFUN_CELL_FORWARD:
4873 status.setEnabled(true);
4876 // disable these with multiple cells selected
4877 case LFUN_INSET_INSERT:
4878 case LFUN_TABULAR_INSERT:
4879 case LFUN_FLEX_INSERT:
4880 case LFUN_FLOAT_INSERT:
4881 case LFUN_FLOAT_WIDE_INSERT:
4882 case LFUN_FOOTNOTE_INSERT:
4883 case LFUN_MARGINALNOTE_INSERT:
4884 case LFUN_MATH_INSERT:
4885 case LFUN_MATH_MODE:
4886 case LFUN_MATH_MUTATE:
4887 case LFUN_MATH_DISPLAY:
4888 case LFUN_NOTE_INSERT:
4889 case LFUN_ARGUMENT_INSERT:
4890 case LFUN_BOX_INSERT:
4891 case LFUN_BRANCH_INSERT:
4892 case LFUN_PHANTOM_INSERT:
4893 case LFUN_WRAP_INSERT:
4894 case LFUN_PREVIEW_INSERT:
4895 case LFUN_ERT_INSERT: {
4896 if (cur.selIsMultiCell()) {
4897 status.setEnabled(false);
4900 return cell(cur.idx())->getStatus(cur, cmd, status);
4903 // disable in non-fixed-width cells
4904 case LFUN_PARAGRAPH_BREAK:
4905 // multirow does not allow paragraph breaks
4906 if (tabular.isMultiRow(cur.idx())) {
4907 status.setEnabled(false);
4910 case LFUN_NEWLINE_INSERT: {
4911 if (tabular.getPWidth(cur.idx()).zero()) {
4912 status.setEnabled(false);
4915 return cell(cur.idx())->getStatus(cur, cmd, status);
4918 case LFUN_NEWPAGE_INSERT:
4919 status.setEnabled(false);
4923 if (tabularStackDirty() && theClipboard().isInternal()) {
4924 if (cur.selIsMultiCell()) {
4927 getSelection(cur, rs, re, cs, ce);
4928 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4929 && paste_tabular->nrows() == re - rs + 1)
4930 status.setEnabled(true);
4932 status.setEnabled(false);
4933 status.message(_("Selection size should match clipboard content."));
4936 status.setEnabled(true);
4939 return cell(cur.idx())->getStatus(cur, cmd, status);
4941 case LFUN_INSET_SETTINGS:
4942 // relay this lfun to Inset, not to the cell.
4943 return Inset::getStatus(cur, cmd, status);
4946 // we try to handle this event in the insets dispatch function.
4947 return cell(cur.idx())->getStatus(cur, cmd, status);
4953 Inset::DisplayType InsetTabular::display() const
4955 if (tabular.is_long_tabular) {
4956 switch (tabular.longtabular_alignment) {
4957 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4959 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4961 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4971 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4973 tabular.latex(os, runparams);
4977 int InsetTabular::plaintext(odocstringstream & os,
4978 OutputParams const & runparams, size_t max_length) const
4980 os << '\n'; // output table on a new line
4981 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4982 tabular.plaintext(os, runparams, dp, false, 0, max_length);
4983 return PLAINTEXT_NEWLINE;
4987 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4992 // FIXME: Why not pass a proper DocIterator here?
4994 // if the table is inside a float it doesn't need the informaltable
4995 // wrapper. Search for it.
4996 for (master = owner(); master; master = master->owner())
4997 if (master->lyxCode() == FLOAT_CODE)
5002 os << "<informaltable>";
5005 ret += tabular.docbook(os, runparams);
5007 os << "</informaltable>";
5014 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5016 return tabular.xhtml(xs, rp);
5020 void InsetTabular::validate(LaTeXFeatures & features) const
5022 tabular.validate(features);
5024 // It'd be better to be able to get this from an InsetLayout, but at present
5025 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5026 if (features.runparams().flavor == OutputParams::HTML)
5027 features.addCSSSnippet(
5028 "table { border: 1px solid black; display: inline-block; }\n"
5029 "td { border: 1px solid black; padding: 0.5ex; }");
5033 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5035 return tabular.cellInset(idx);
5039 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5041 return tabular.cellInset(idx);
5045 void InsetTabular::cursorPos(BufferView const & bv,
5046 CursorSlice const & sl, bool boundary, int & x, int & y) const
5048 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5050 // y offset correction
5051 y += cellYPos(sl.idx());
5052 y += tabular.textVOffset(sl.idx());
5053 y += offset_valign_;
5055 // x offset correction
5056 x += cellXPos(sl.idx());
5057 x += tabular.textHOffset(sl.idx());
5058 x += ADD_TO_TABULAR_WIDTH;
5063 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5067 Inset const & inset = *tabular.cellInset(cell);
5068 Point o = bv.coordCache().getInsets().xy(&inset);
5069 int const xbeg = o.x_ - tabular.textHOffset(cell);
5070 int const xend = xbeg + tabular.cellWidth(cell);
5071 row_type const row = tabular.cellRow(cell);
5072 int const ybeg = o.y_ - tabular.rowAscent(row)
5073 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5074 int const yend = ybeg + tabular.cellHeight(cell);
5086 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5087 // << " ybeg=" << ybeg << " yend=" << yend
5088 // << " xx=" << xx << " yy=" << yy
5089 // << " dist=" << xx + yy << endl;
5094 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5096 //lyxerr << "InsetTabular::editXY: " << this << endl;
5097 cur.setSelection(false);
5099 cur.idx() = getNearestCell(cur.bv(), x, y);
5101 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5105 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5107 cur.idx() = getNearestCell(cur.bv(), x, y);
5108 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5112 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5114 idx_type idx_min = 0;
5115 int dist_min = numeric_limits<int>::max();
5116 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5117 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5118 int const d = dist(bv, i, x, y);
5129 int InsetTabular::cellYPos(idx_type const cell) const
5131 row_type row = tabular.cellRow(cell);
5133 for (row_type r = 0; r < row; ++r)
5134 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5135 + tabular.interRowSpace(r + 1);
5140 int InsetTabular::cellXPos(idx_type const cell) const
5142 col_type col = tabular.cellColumn(cell);
5144 for (col_type c = 0; c < col; ++c)
5145 lx += tabular.column_info[c].width;
5150 void InsetTabular::resetPos(Cursor & cur) const
5152 BufferView & bv = cur.bv();
5153 int const maxwidth = bv.workWidth();
5155 int const scx_old = scx_;
5156 int const i = cur.find(this);
5161 int const X2 = maxwidth;
5162 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5163 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5164 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5167 scx_ = X1 + 20 - x1;
5169 scx_ = X2 - 20 - x2;
5174 // only update if offset changed
5175 if (scx_ != scx_old)
5176 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5180 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5182 row_type const row = tabular.cellRow(cur.idx());
5183 col_type const col = tabular.cellColumn(cur.idx());
5185 if (isRightToLeft(cur)) {
5186 if (tabular.cellColumn(cur.idx()) == 0) {
5187 if (row == tabular.nrows() - 1)
5189 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5194 cur.idx() = tabular.getLastCellInRow(row - 1);
5196 cur.idx() = tabular.cellIndex(row, col - 1);
5199 if (tabular.isLastCell(cur.idx()))
5201 if (cur.idx() == tabular.getLastCellInRow(row))
5202 cur.idx() = tabular.cellIndex(row + 1, 0);
5204 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5205 cur.idx() = tabular.cellIndex(row, colnextcell);
5209 cur.boundary(false);
5211 if (cur.selIsMultiCell()) {
5212 cur.pit() = cur.lastpit();
5213 cur.pos() = cur.lastpos();
5221 // in visual mode, place cursor at extreme left or right
5223 switch(entry_from) {
5225 case ENTRY_DIRECTION_RIGHT:
5226 cur.posVisToRowExtremity(false /* !left */);
5228 case ENTRY_DIRECTION_LEFT:
5229 cur.posVisToRowExtremity(true /* left */);
5231 case ENTRY_DIRECTION_IGNORE:
5232 // nothing to do in this case
5236 cur.setCurrentFont();
5241 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5243 row_type const row = tabular.cellRow(cur.idx());
5244 col_type const col = tabular.cellColumn(cur.idx());
5246 if (isRightToLeft(cur)) {
5247 if (cur.idx() == tabular.getLastCellInRow(row)) {
5250 cur.idx() = tabular.getFirstCellInRow(row);
5251 cur.idx() = tabular.cellAbove(cur.idx());
5253 if (tabular.isLastCell(cur.idx()))
5255 if (cur.idx() == tabular.getLastCellInRow(row))
5256 cur.idx() = tabular.cellIndex(row + 1, 0);
5258 cur.idx() = tabular.cellIndex(row, col + 1);
5261 if (cur.idx() == 0) // first cell
5264 cur.idx() = tabular.getLastCellInRow(row - 1);
5266 cur.idx() = tabular.cellIndex(row, col - 1);
5269 if (cur.selIsMultiCell()) {
5270 cur.pit() = cur.lastpit();
5271 cur.pos() = cur.lastpos();
5276 cur.pit() = cur.lastpit();
5277 cur.pos() = cur.lastpos();
5279 // in visual mode, place cursor at extreme left or right
5281 switch(entry_from) {
5283 case ENTRY_DIRECTION_RIGHT:
5284 cur.posVisToRowExtremity(false /* !left */);
5286 case ENTRY_DIRECTION_LEFT:
5287 cur.posVisToRowExtremity(true /* left */);
5289 case ENTRY_DIRECTION_IGNORE:
5290 // nothing to do in this case
5294 cur.setCurrentFont();
5299 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5301 istringstream is(argument);
5304 if (insetCode(s) != TABULAR_CODE)
5308 size_t safe_guard = 0;
5313 if (safe_guard > 1000) {
5314 LYXERR0("parameter max count reached!");
5318 Tabular::Feature action = Tabular::LAST_ACTION;
5321 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5322 if (s != tabularFeature[i].feature)
5324 action = tabularFeature[i].action;
5327 if (action == Tabular::LAST_ACTION) {
5328 LYXERR0("Feature not found " << s);
5332 if (tabularFeature[i].need_value)
5334 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5335 tabularFeatures(cur, action, val);
5341 static void checkLongtableSpecial(Tabular::ltType & ltt,
5342 string const & special, bool & flag)
5344 if (special == "dl_above") {
5347 } else if (special == "dl_below") {
5348 ltt.bottomDL = flag;
5350 } else if (special == "empty") {
5360 bool InsetTabular::oneCellHasRotationState(bool rotated,
5361 row_type row_start, row_type row_end,
5362 col_type col_start, col_type col_end) const
5364 for (row_type r = row_start; r <= row_end; ++r)
5365 for (col_type c = col_start; c <= col_end; ++c)
5367 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5370 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5377 void InsetTabular::tabularFeatures(Cursor & cur,
5378 Tabular::Feature feature, string const & value)
5380 col_type sel_col_start;
5381 col_type sel_col_end;
5382 row_type sel_row_start;
5383 row_type sel_row_end;
5384 bool setLines = false;
5385 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5386 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5390 case Tabular::M_ALIGN_LEFT:
5391 case Tabular::ALIGN_LEFT:
5392 setAlign = LYX_ALIGN_LEFT;
5395 case Tabular::M_ALIGN_RIGHT:
5396 case Tabular::ALIGN_RIGHT:
5397 setAlign = LYX_ALIGN_RIGHT;
5400 case Tabular::M_ALIGN_CENTER:
5401 case Tabular::ALIGN_CENTER:
5402 setAlign = LYX_ALIGN_CENTER;
5405 case Tabular::ALIGN_BLOCK:
5406 setAlign = LYX_ALIGN_BLOCK;
5409 case Tabular::ALIGN_DECIMAL:
5410 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5411 setAlign = LYX_ALIGN_CENTER;
5413 setAlign = LYX_ALIGN_DECIMAL;
5416 case Tabular::M_VALIGN_TOP:
5417 case Tabular::VALIGN_TOP:
5418 setVAlign = Tabular::LYX_VALIGN_TOP;
5421 case Tabular::M_VALIGN_BOTTOM:
5422 case Tabular::VALIGN_BOTTOM:
5423 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5426 case Tabular::M_VALIGN_MIDDLE:
5427 case Tabular::VALIGN_MIDDLE:
5428 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5435 cur.recordUndoInset(ATOMIC_UNDO, this);
5437 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5438 row_type const row = tabular.cellRow(cur.idx());
5439 col_type const column = tabular.cellColumn(cur.idx());
5441 Tabular::ltType ltt;
5445 case Tabular::SET_TABULAR_WIDTH:
5446 tabular.setTabularWidth(Length(value));
5449 case Tabular::SET_PWIDTH: {
5450 Length const len(value);
5451 tabular.setColumnPWidth(cur, cur.idx(), len);
5453 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5454 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5458 case Tabular::SET_MPWIDTH:
5459 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5462 case Tabular::SET_MROFFSET:
5463 tabular.setMROffset(cur, cur.idx(), Length(value));
5466 case Tabular::SET_SPECIAL_COLUMN:
5467 case Tabular::SET_SPECIAL_MULTICOLUMN:
5468 if (value == "none")
5469 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5471 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5474 case Tabular::APPEND_ROW:
5475 // append the row into the tabular
5476 tabular.appendRow(row);
5479 case Tabular::APPEND_COLUMN:
5480 // append the column into the tabular
5481 tabular.appendColumn(column);
5482 cur.idx() = tabular.cellIndex(row, column);
5485 case Tabular::DELETE_ROW:
5486 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5487 for (col_type c = 0; c < tabular.ncols(); c++)
5488 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5489 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5492 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5493 tabular.deleteRow(sel_row_start);
5494 if (sel_row_start >= tabular.nrows())
5496 cur.idx() = tabular.cellIndex(sel_row_start, column);
5499 cur.setSelection(false);
5502 case Tabular::DELETE_COLUMN:
5503 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5504 for (row_type r = 0; r < tabular.nrows(); r++)
5505 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5506 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5509 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5510 for (row_type r = 0; r < tabular.nrows(); r++)
5511 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5512 tabular.leftLine(tabular.cellIndex(r, 0)));
5515 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5516 tabular.deleteColumn(sel_col_start);
5517 if (sel_col_start >= tabular.ncols())
5519 cur.idx() = tabular.cellIndex(row, sel_col_start);
5522 cur.setSelection(false);
5525 case Tabular::COPY_ROW:
5526 tabular.copyRow(row);
5529 case Tabular::COPY_COLUMN:
5530 tabular.copyColumn(column);
5531 cur.idx() = tabular.cellIndex(row, column);
5534 case Tabular::MOVE_COLUMN_RIGHT:
5535 tabular.moveColumn(column, Tabular::RIGHT);
5536 cur.idx() = tabular.cellIndex(row, column + 1);
5539 case Tabular::MOVE_COLUMN_LEFT:
5540 tabular.moveColumn(column, Tabular::LEFT);
5541 cur.idx() = tabular.cellIndex(row, column - 1);
5544 case Tabular::MOVE_ROW_DOWN:
5545 tabular.moveRow(row, Tabular::DOWN);
5546 cur.idx() = tabular.cellIndex(row + 1, column);
5549 case Tabular::MOVE_ROW_UP:
5550 tabular.moveRow(row, Tabular::UP);
5551 cur.idx() = tabular.cellIndex(row - 1, column);
5554 case Tabular::SET_LINE_TOP:
5555 case Tabular::TOGGLE_LINE_TOP: {
5556 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5557 ? (value == "true") : !tabular.topLine(cur.idx());
5558 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5559 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5560 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5564 case Tabular::SET_LINE_BOTTOM:
5565 case Tabular::TOGGLE_LINE_BOTTOM: {
5566 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5567 ? (value == "true") : !tabular.bottomLine(cur.idx());
5568 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5569 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5570 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5574 case Tabular::SET_LINE_LEFT:
5575 case Tabular::TOGGLE_LINE_LEFT: {
5576 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5577 ? (value == "true") : !tabular.leftLine(cur.idx());
5578 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5579 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5580 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5584 case Tabular::SET_LINE_RIGHT:
5585 case Tabular::TOGGLE_LINE_RIGHT: {
5586 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5587 ? (value == "true") : !tabular.rightLine(cur.idx());
5588 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5589 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5590 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5594 case Tabular::M_ALIGN_LEFT:
5595 case Tabular::M_ALIGN_RIGHT:
5596 case Tabular::M_ALIGN_CENTER:
5597 case Tabular::ALIGN_LEFT:
5598 case Tabular::ALIGN_RIGHT:
5599 case Tabular::ALIGN_CENTER:
5600 case Tabular::ALIGN_BLOCK:
5601 case Tabular::ALIGN_DECIMAL:
5602 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5603 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5604 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5605 !tabular.getPWidth(c).zero());
5608 case Tabular::M_VALIGN_TOP:
5609 case Tabular::M_VALIGN_BOTTOM:
5610 case Tabular::M_VALIGN_MIDDLE:
5612 case Tabular::VALIGN_TOP:
5613 case Tabular::VALIGN_BOTTOM:
5614 case Tabular::VALIGN_MIDDLE:
5615 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5616 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5617 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5620 case Tabular::SET_MULTICOLUMN: {
5621 if (!cur.selection()) {
5622 // just multicol for one single cell
5623 // check whether we are completely in a multicol
5624 if (!tabular.isMultiColumn(cur.idx()))
5625 tabular.setMultiColumn(cur.idx(), 1,
5626 tabular.rightLine(cur.idx()));
5629 // we have a selection so this means we just add all these
5630 // cells to form a multicolumn cell
5631 idx_type const s_start = cur.selBegin().idx();
5632 row_type const col_start = tabular.cellColumn(s_start);
5633 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5634 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5635 tabular.rightLine(cur.selEnd().idx()));
5638 cur.setSelection(false);
5642 case Tabular::UNSET_MULTICOLUMN: {
5643 if (!cur.selection()) {
5644 if (tabular.isMultiColumn(cur.idx()))
5645 tabular.unsetMultiColumn(cur.idx());
5650 case Tabular::MULTICOLUMN: {
5651 if (!cur.selection()) {
5652 if (tabular.isMultiColumn(cur.idx()))
5653 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5655 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5659 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5660 row_type const r = sel_row_start;
5661 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5662 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5665 // If the selection contains at least one singlecol cell
5666 // or multiple multicol cells,
5667 // we assume the user will merge is to a single multicol
5669 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5671 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5675 case Tabular::SET_MULTIROW: {
5676 if (!cur.selection()) {
5677 // just multirow for one single cell
5678 // check whether we are completely in a multirow
5679 if (!tabular.isMultiRow(cur.idx()))
5680 tabular.setMultiRow(cur.idx(), 1,
5681 tabular.bottomLine(cur.idx()));
5684 // we have a selection so this means we just add all this
5685 // cells to form a multirow cell
5686 idx_type const s_start = cur.selBegin().idx();
5687 row_type const row_start = tabular.cellRow(s_start);
5688 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5689 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5690 tabular.bottomLine(cur.selEnd().idx()));
5693 cur.setSelection(false);
5697 case Tabular::UNSET_MULTIROW: {
5698 if (!cur.selection()) {
5699 if (tabular.isMultiRow(cur.idx()))
5700 tabular.unsetMultiRow(cur.idx());
5705 case Tabular::MULTIROW: {
5706 if (!cur.selection()) {
5707 if (tabular.isMultiRow(cur.idx()))
5708 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5710 tabularFeatures(cur, Tabular::SET_MULTIROW);
5714 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5715 col_type const c = sel_col_start;
5716 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5717 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5720 // If the selection contains at least one singlerow cell
5721 // or multiple multirow cells,
5722 // we assume the user will merge is to a single multirow
5724 tabularFeatures(cur, Tabular::SET_MULTIROW);
5726 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5730 case Tabular::SET_ALL_LINES:
5732 case Tabular::UNSET_ALL_LINES:
5733 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5734 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5735 idx_type const cell = tabular.cellIndex(r, c);
5736 tabular.setTopLine(cell, setLines);
5737 tabular.setBottomLine(cell, setLines);
5738 tabular.setRightLine(cell, setLines);
5739 tabular.setLeftLine(cell, setLines);
5743 case Tabular::SET_BORDER_LINES:
5744 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5745 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5746 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5748 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5749 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5750 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5754 case Tabular::SET_LONGTABULAR:
5755 tabular.is_long_tabular = true;
5758 case Tabular::UNSET_LONGTABULAR:
5759 for (row_type r = 0; r < tabular.nrows(); ++r) {
5760 if (tabular.ltCaption(r)) {
5761 cur.idx() = tabular.cellIndex(r, 0);
5764 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5767 tabular.is_long_tabular = false;
5770 case Tabular::SET_ROTATE_TABULAR:
5771 tabular.rotate = convert<int>(value);
5774 case Tabular::UNSET_ROTATE_TABULAR:
5778 case Tabular::TOGGLE_ROTATE_TABULAR:
5779 // when pressing the rotate button we default to 90° rotation
5780 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5783 case Tabular::TABULAR_VALIGN_TOP:
5784 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5787 case Tabular::TABULAR_VALIGN_MIDDLE:
5788 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5791 case Tabular::TABULAR_VALIGN_BOTTOM:
5792 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5795 case Tabular::LONGTABULAR_ALIGN_LEFT:
5796 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5799 case Tabular::LONGTABULAR_ALIGN_CENTER:
5800 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5803 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5804 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5809 case Tabular::SET_ROTATE_CELL:
5810 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5811 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5812 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5815 case Tabular::UNSET_ROTATE_CELL:
5816 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5817 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5818 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5821 case Tabular::TOGGLE_ROTATE_CELL:
5823 bool oneNotRotated = oneCellHasRotationState(false,
5824 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5826 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5827 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5828 // when pressing the rotate cell button we default to 90° rotation
5830 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5832 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5837 case Tabular::SET_USEBOX: {
5838 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5839 if (val == tabular.getUsebox(cur.idx()))
5840 val = Tabular::BOX_NONE;
5841 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5842 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5843 tabular.setUsebox(tabular.cellIndex(r, c), val);
5847 case Tabular::UNSET_LTFIRSTHEAD:
5849 case Tabular::SET_LTFIRSTHEAD:
5850 tabular.getRowOfLTFirstHead(row, ltt);
5851 checkLongtableSpecial(ltt, value, flag);
5852 tabular.setLTHead(row, flag, ltt, true);
5855 case Tabular::UNSET_LTHEAD:
5857 case Tabular::SET_LTHEAD:
5858 tabular.getRowOfLTHead(row, ltt);
5859 checkLongtableSpecial(ltt, value, flag);
5860 tabular.setLTHead(row, flag, ltt, false);
5863 case Tabular::UNSET_LTFOOT:
5865 case Tabular::SET_LTFOOT:
5866 tabular.getRowOfLTFoot(row, ltt);
5867 checkLongtableSpecial(ltt, value, flag);
5868 tabular.setLTFoot(row, flag, ltt, false);
5871 case Tabular::UNSET_LTLASTFOOT:
5873 case Tabular::SET_LTLASTFOOT:
5874 tabular.getRowOfLTLastFoot(row, ltt);
5875 checkLongtableSpecial(ltt, value, flag);
5876 tabular.setLTFoot(row, flag, ltt, true);
5879 case Tabular::UNSET_LTNEWPAGE:
5881 case Tabular::SET_LTNEWPAGE:
5882 tabular.setLTNewPage(row, flag);
5885 case Tabular::SET_LTCAPTION: {
5886 if (tabular.ltCaption(row))
5888 cur.idx() = tabular.setLTCaption(row, true);
5891 cur.setSelection(false);
5892 // If a row is set as caption, then also insert
5893 // a caption. Otherwise the LaTeX output is broken.
5894 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5895 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5899 case Tabular::UNSET_LTCAPTION: {
5900 if (!tabular.ltCaption(row))
5902 cur.idx() = tabular.setLTCaption(row, false);
5905 cur.setSelection(false);
5906 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5907 if (lyx::getStatus(fr).enabled())
5912 case Tabular::TOGGLE_LTCAPTION: {
5913 if (tabular.ltCaption(row))
5914 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5916 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5920 case Tabular::SET_BOOKTABS:
5921 tabular.use_booktabs = true;
5924 case Tabular::UNSET_BOOKTABS:
5925 tabular.use_booktabs = false;
5928 case Tabular::SET_TOP_SPACE: {
5930 if (value == "default")
5931 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5932 tabular.row_info[r].top_space_default = true;
5933 else if (value == "none")
5934 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5935 tabular.row_info[r].top_space_default = false;
5936 tabular.row_info[r].top_space = len;
5938 else if (isValidLength(value, &len))
5939 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5940 tabular.row_info[r].top_space_default = false;
5941 tabular.row_info[r].top_space = len;
5946 case Tabular::SET_BOTTOM_SPACE: {
5948 if (value == "default")
5949 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5950 tabular.row_info[r].bottom_space_default = true;
5951 else if (value == "none")
5952 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5953 tabular.row_info[r].bottom_space_default = false;
5954 tabular.row_info[r].bottom_space = len;
5956 else if (isValidLength(value, &len))
5957 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5958 tabular.row_info[r].bottom_space_default = false;
5959 tabular.row_info[r].bottom_space = len;
5964 case Tabular::SET_INTERLINE_SPACE: {
5966 if (value == "default")
5967 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5968 tabular.row_info[r].interline_space_default = true;
5969 else if (value == "none")
5970 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5971 tabular.row_info[r].interline_space_default = false;
5972 tabular.row_info[r].interline_space = len;
5974 else if (isValidLength(value, &len))
5975 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5976 tabular.row_info[r].interline_space_default = false;
5977 tabular.row_info[r].interline_space = len;
5982 case Tabular::SET_DECIMAL_POINT:
5983 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5984 tabular.column_info[c].decimal_point = from_utf8(value);
5987 // dummy stuff just to avoid warnings
5988 case Tabular::LAST_ACTION:
5994 bool InsetTabular::copySelection(Cursor & cur)
5996 if (!cur.selection())
6001 getSelection(cur, rs, re, cs, ce);
6003 paste_tabular.reset(new Tabular(tabular));
6005 for (row_type r = 0; r < rs; ++r)
6006 paste_tabular->deleteRow(0);
6008 row_type const rows = re - rs + 1;
6009 while (paste_tabular->nrows() > rows)
6010 paste_tabular->deleteRow(rows);
6012 for (col_type c = 0; c < cs; ++c)
6013 paste_tabular->deleteColumn(0);
6015 col_type const columns = ce - cs + 1;
6016 while (paste_tabular->ncols() > columns)
6017 paste_tabular->deleteColumn(columns);
6019 paste_tabular->setBuffer(tabular.buffer());
6021 odocstringstream os;
6022 OutputParams const runparams(0);
6023 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6024 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6025 cap::copySelection(cur, os.str());
6027 // mark tabular stack dirty
6028 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6029 // when we (hopefully) have a one-for-all paste mechanism.
6030 // This must be called after cap::copySelection.
6031 dirtyTabularStack(true);
6037 bool InsetTabular::pasteClipboard(Cursor & cur)
6041 col_type actcol = tabular.cellColumn(cur.idx());
6042 row_type actrow = tabular.cellRow(cur.idx());
6044 if (cur.selIsMultiCell()) {
6047 getSelection(cur, actrow, re, actcol, ce);
6050 for (row_type r1 = 0, r2 = actrow;
6051 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6053 for (col_type c1 = 0, c2 = actcol;
6054 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6056 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6057 tabular.isPartOfMultiColumn(r2, c2))
6059 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6063 if (tabular.isPartOfMultiColumn(r2, c2)) {
6067 shared_ptr<InsetTableCell> inset(
6068 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6069 tabular.setCellInset(r2, c2, inset);
6070 // FIXME?: why do we need to do this explicitly? (EL)
6071 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6073 // FIXME: change tracking (MG)
6074 inset->setChange(Change(buffer().params().trackChanges ?
6075 Change::INSERTED : Change::UNCHANGED));
6083 void InsetTabular::cutSelection(Cursor & cur)
6085 if (!cur.selection())
6090 getSelection(cur, rs, re, cs, ce);
6091 for (row_type r = rs; r <= re; ++r) {
6092 for (col_type c = cs; c <= ce; ++c) {
6093 shared_ptr<InsetTableCell> t
6094 = cell(tabular.cellIndex(r, c));
6095 if (buffer().params().trackChanges)
6096 // FIXME: Change tracking (MG)
6097 t->setChange(Change(Change::DELETED));
6103 // cursor position might be invalid now
6104 if (cur.pit() > cur.lastpit())
6105 cur.pit() = cur.lastpit();
6106 if (cur.pos() > cur.lastpos())
6107 cur.pos() = cur.lastpos();
6108 cur.clearSelection();
6112 bool InsetTabular::isRightToLeft(Cursor & cur) const
6114 // LASSERT: It might be better to abandon this Buffer.
6115 LASSERT(cur.depth() > 1, return false);
6116 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6117 pos_type const parentpos = cur[cur.depth() - 2].pos();
6118 return parentpar.getFontSettings(buffer().params(),
6119 parentpos).language()->rightToLeft();
6123 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6126 LASSERT(stidx <= enidx, return docstring());
6128 col_type const col1 = tabular.cellColumn(stidx);
6129 col_type const col2 = tabular.cellColumn(enidx);
6130 row_type const row1 = tabular.cellRow(stidx);
6131 row_type const row2 = tabular.cellRow(enidx);
6133 for (col_type col = col1; col <= col2; col++)
6134 for (row_type row = row1; row <= row2; row++) {
6139 retval += tabular.cellInset(row, col)->asString(intoInsets);
6145 void InsetTabular::getSelection(Cursor & cur,
6146 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6148 CursorSlice const & beg = cur.selBegin();
6149 CursorSlice const & end = cur.selEnd();
6150 cs = tabular.cellColumn(beg.idx());
6151 ce = tabular.cellColumn(end.idx());
6155 rs = tabular.cellRow(beg.idx());
6156 re = tabular.cellRow(end.idx());
6162 Text * InsetTabular::getText(int idx) const
6164 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6168 void InsetTabular::setChange(Change const & change)
6170 for (idx_type idx = 0; idx < nargs(); ++idx)
6171 cell(idx)->setChange(change);
6175 void InsetTabular::acceptChanges()
6177 for (idx_type idx = 0; idx < nargs(); ++idx)
6178 cell(idx)->acceptChanges();
6182 void InsetTabular::rejectChanges()
6184 for (idx_type idx = 0; idx < nargs(); ++idx)
6185 cell(idx)->rejectChanges();
6189 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6191 return tabular.getPWidth(cell).zero();
6195 bool InsetTabular::forcePlainLayout(idx_type cell) const
6197 return !tabular.getPWidth(cell).zero();
6201 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6204 if (buf.length() <= 0)
6209 col_type maxCols = 1;
6210 size_t const len = buf.length();
6214 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6222 maxCols = max(cols, maxCols);
6228 maxCols = max(cols, maxCols);
6234 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6235 loctab = paste_tabular.get();
6237 dirtyTabularStack(true);
6240 cell = bv.cursor().idx();
6241 ocol = tabular.cellColumn(cell);
6242 row = tabular.cellRow(cell);
6246 idx_type const cells = loctab->numberofcells;
6249 rows = loctab->nrows();
6250 col_type const columns = loctab->ncols();
6252 while (cell < cells && p < len && row < rows &&
6253 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6259 // we can only set this if we are not too far right
6260 if (cols < columns) {
6261 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6262 Font const font = bv.textMetrics(&inset->text()).
6264 inset->setText(buf.substr(op, p - op), font,
6265 buffer().params().trackChanges);
6271 // we can only set this if we are not too far right
6272 if (cols < columns) {
6273 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6274 Font const font = bv.textMetrics(&inset->text()).
6276 inset->setText(buf.substr(op, p - op), font,
6277 buffer().params().trackChanges);
6282 cell = loctab->cellIndex(row, cols);
6288 // check for the last cell if there is no trailing '\n'
6289 if (cell < cells && op < len) {
6290 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6291 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6292 inset->setText(buf.substr(op, len - op), font,
6293 buffer().params().trackChanges);
6299 void InsetTabular::addPreview(DocIterator const & inset_pos,
6300 PreviewLoader & loader) const
6302 DocIterator cell_pos = inset_pos;
6304 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6305 for (row_type r = 0; r < tabular.nrows(); ++r) {
6306 for (col_type c = 0; c < tabular.ncols(); ++c) {
6307 cell_pos.top().idx() = tabular.cellIndex(r, c);
6308 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6314 bool InsetTabular::completionSupported(Cursor const & cur) const
6316 Cursor const & bvCur = cur.bv().cursor();
6317 if (&bvCur.inset() != this)
6319 return cur.text()->completionSupported(cur);
6323 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6325 return completionSupported(cur);
6329 bool InsetTabular::automaticInlineCompletion() const
6331 return lyxrc.completion_inline_text;
6335 bool InsetTabular::automaticPopupCompletion() const
6337 return lyxrc.completion_popup_text;
6341 bool InsetTabular::showCompletionCursor() const
6343 return lyxrc.completion_cursor_text;
6347 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6349 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6353 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6355 if (!completionSupported(cur))
6357 return cur.text()->completionPrefix(cur);
6361 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6363 if (!completionSupported(cur))
6366 return cur.text()->insertCompletion(cur, s, finished);
6370 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6371 Dimension & dim) const
6373 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6374 tm.completionPosAndDim(cur, x, y, dim);
6378 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6380 istringstream data(in);
6382 lex.setStream(data);
6389 if (!lex || token != "tabular") {
6390 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6394 // This is part of the inset proper that is usually swallowed
6395 // by Buffer::readInset
6397 if (!lex || token != "Tabular") {
6398 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6406 string InsetTabular::params2string(InsetTabular const & inset)
6409 data << "tabular" << ' ';
6411 data << "\\end_inset\n";