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, /**/);
1535 if (!getTokenValue(line, "version", version))
1537 LASSERT(version >= 2, /**/);
1540 if (!getTokenValue(line, "rows", rows_arg))
1543 if (!getTokenValue(line, "columns", columns_arg))
1545 init(buffer_, rows_arg, columns_arg);
1546 l_getline(is, line);
1547 if (!prefixIs(line, "<features")) {
1548 lyxerr << "Wrong tabular format (expected <features ...> got"
1549 << line << ')' << endl;
1552 getTokenValue(line, "rotate", rotate);
1553 getTokenValue(line, "booktabs", use_booktabs);
1554 getTokenValue(line, "islongtable", is_long_tabular);
1555 getTokenValue(line, "tabularvalignment", tabular_valignment);
1556 getTokenValue(line, "tabularwidth", tabular_width);
1557 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1558 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1559 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1560 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1561 getTokenValue(line, "headTopDL", endhead.topDL);
1562 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1563 getTokenValue(line, "footTopDL", endfoot.topDL);
1564 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1565 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1566 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1567 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1569 for (col_type c = 0; c < ncols(); ++c) {
1571 if (!prefixIs(line,"<column")) {
1572 lyxerr << "Wrong tabular format (expected <column ...> got"
1573 << line << ')' << endl;
1576 getTokenValue(line, "alignment", column_info[c].alignment);
1577 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1578 getTokenValue(line, "valignment", column_info[c].valignment);
1579 getTokenValue(line, "width", column_info[c].p_width);
1580 getTokenValue(line, "special", column_info[c].align_special);
1583 for (row_type i = 0; i < nrows(); ++i) {
1584 l_getline(is, line);
1585 if (!prefixIs(line, "<row")) {
1586 lyxerr << "Wrong tabular format (expected <row ...> got"
1587 << line << ')' << endl;
1590 getTokenValue(line, "topspace", row_info[i].top_space,
1591 row_info[i].top_space_default);
1592 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1593 row_info[i].bottom_space_default);
1594 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1595 row_info[i].interline_space_default);
1596 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1597 getTokenValue(line, "endhead", row_info[i].endhead);
1598 getTokenValue(line, "endfoot", row_info[i].endfoot);
1599 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1600 getTokenValue(line, "newpage", row_info[i].newpage);
1601 getTokenValue(line, "caption", row_info[i].caption);
1602 for (col_type j = 0; j < ncols(); ++j) {
1603 l_getline(is, line);
1604 if (!prefixIs(line, "<cell")) {
1605 lyxerr << "Wrong tabular format (expected <cell ...> got"
1606 << line << ')' << endl;
1609 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1610 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1611 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1612 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1613 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1614 getTokenValue(line, "topline", cell_info[i][j].top_line);
1615 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1616 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1617 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1618 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1619 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1620 getTokenValue(line, "width", cell_info[i][j].p_width);
1622 getTokenValue(line, "special", cell_info[i][j].align_special);
1623 l_getline(is, line);
1624 if (prefixIs(line, "\\begin_inset")) {
1625 cell_info[i][j].inset->setBuffer(*buffer_);
1626 cell_info[i][j].inset->read(lex);
1627 l_getline(is, line);
1629 if (!prefixIs(line, "</cell>")) {
1630 lyxerr << "Wrong tabular format (expected </cell> got"
1631 << line << ')' << endl;
1635 l_getline(is, line);
1636 if (!prefixIs(line, "</row>")) {
1637 lyxerr << "Wrong tabular format (expected </row> got"
1638 << line << ')' << endl;
1642 while (!prefixIs(line, "</lyxtabular>")) {
1643 l_getline(is, line);
1649 bool Tabular::isMultiColumn(idx_type cell) const
1651 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1652 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1656 bool Tabular::hasMultiColumn(col_type c) const
1658 for (row_type r = 0; r < nrows(); ++r) {
1659 if (isMultiColumn(cellIndex(r, c)))
1666 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1668 return cell_info[cellRow(cell)][cellColumn(cell)];
1672 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1673 bool const right_border)
1675 idx_type const col = cellColumn(cell);
1676 idx_type const row = cellRow(cell);
1677 for (idx_type i = 0; i < number; ++i)
1678 unsetMultiRow(cellIndex(row, col + i));
1680 // unsetting of multirow may have invalidated cell index
1681 cell = cellIndex(row, col);
1682 CellData & cs = cellInfo(cell);
1683 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1684 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1685 cs.alignment = column_info[col].alignment;
1687 setRightLine(cell, right_border);
1689 for (idx_type i = 1; i < number; ++i) {
1690 CellData & cs1 = cellInfo(cell + i);
1691 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1692 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1700 bool Tabular::isMultiRow(idx_type cell) const
1702 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1703 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1706 bool Tabular::hasMultiRow(row_type r) const
1708 for (col_type c = 0; c < ncols(); ++c) {
1709 if (isMultiRow(cellIndex(r, c)))
1715 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1716 bool const bottom_border)
1718 idx_type const col = cellColumn(cell);
1719 idx_type const row = cellRow(cell);
1720 for (idx_type i = 0; i < number; ++i)
1721 unsetMultiColumn(cellIndex(row + i, col));
1723 // unsetting of multirow may have invalidated cell index
1724 cell = cellIndex(row, col);
1725 CellData & cs = cellInfo(cell);
1726 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1727 cs.valignment = LYX_VALIGN_MIDDLE;
1728 // the horizontal alignment of multirow cells can only
1729 // be changed for the whole table row,
1730 // support changing this only for the multirow cell can be done via
1732 // this feature would be a fileformat change
1733 // until LyX supports this, use the deault alignment of multirow
1735 cs.alignment = LYX_ALIGN_LEFT;
1737 // set the bottom line of the last selected cell
1738 setBottomLine(cell, bottom_border);
1740 for (idx_type i = 1; i < number; ++i) {
1741 CellData & cs1 = cell_info[row + i][col];
1742 cs1.multirow = CELL_PART_OF_MULTIROW;
1743 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1751 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1753 row_type const row = cellRow(cell);
1754 col_type const col = cellColumn(cell);
1756 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1763 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1765 col_type const column = cellColumn(cell);
1766 col_type row = cellRow(cell) + 1;
1767 while (row < nrows() && isPartOfMultiRow(row, column))
1770 return row - cellRow(cell);
1774 void Tabular::unsetMultiColumn(idx_type cell)
1776 if (!isMultiColumn(cell))
1779 row_type const row = cellRow(cell);
1780 col_type const col = cellColumn(cell);
1781 row_type const span = columnSpan(cell);
1782 for (col_type c = 0; c < span; ++c) {
1783 // in the table dialog the lines are set in every case
1784 // when unsetting a multicolumn this leads to an additional right
1785 // line for every cell that was part of the former multicolumn cell,
1786 // except if the cell is in the last column
1787 // therefore remove this line
1788 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1789 && (col + c) < (col + span - 1))
1790 cell_info[row][col + c].right_line = false;
1791 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1797 void Tabular::unsetMultiRow(idx_type cell)
1799 if (!isMultiRow(cell))
1802 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1803 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1804 row_type const row = cellRow(cell);
1805 col_type const col = cellColumn(cell);
1806 row_type const span = rowSpan(cell);
1807 for (row_type r = 0; r < span; ++r)
1808 cell_info[row + r][col].multirow = CELL_NORMAL;
1813 void Tabular::setRotateCell(idx_type cell, int value)
1815 cellInfo(cell).rotate = value;
1819 int Tabular::getRotateCell(idx_type cell) const
1821 return cellInfo(cell).rotate;
1825 bool Tabular::needRotating() const
1829 for (row_type r = 0; r < nrows(); ++r)
1830 for (col_type c = 0; c < ncols(); ++c)
1831 if (cell_info[r][c].rotate != 0)
1837 bool Tabular::isLastCell(idx_type cell) const
1839 if (cell + 1 < numberofcells)
1845 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1847 if (cellRow(cell) == 0)
1850 col_type const col = cellColumn(cell);
1851 row_type r = cellRow(cell) - 1;
1852 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1855 return cell_info[r][col].cellno;
1859 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1861 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1862 if (nextrow < nrows())
1863 return cell_info[nextrow][cellColumn(cell)].cellno;
1868 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1870 LASSERT(column != npos && column < ncols()
1871 && row != npos && row < nrows(), /**/);
1872 return cell_info[row][column].cellno;
1876 void Tabular::setUsebox(idx_type cell, BoxType type)
1878 cellInfo(cell).usebox = type;
1882 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1883 // adds line breaks, see bug 4886.
1884 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1886 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1887 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1889 if (cellInfo(cell).usebox > 1)
1890 return cellInfo(cell).usebox;
1891 return useParbox(cell);
1896 // This are functions used for the longtable support
1898 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1904 row_info[row].endfirsthead = flag;
1908 row_info[row].endhead = flag;
1913 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1916 hd.set = haveLTHead();
1917 return row_info[row].endhead;
1921 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1924 hd.set = haveLTFirstHead();
1925 return row_info[row].endfirsthead;
1929 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1935 row_info[row].endlastfoot = flag;
1939 row_info[row].endfoot = flag;
1944 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1947 fd.set = haveLTFoot();
1948 return row_info[row].endfoot;
1952 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1955 fd.set = haveLTLastFoot();
1956 return row_info[row].endlastfoot;
1960 void Tabular::setLTNewPage(row_type row, bool what)
1962 row_info[row].newpage = what;
1966 bool Tabular::getLTNewPage(row_type row) const
1968 return row_info[row].newpage;
1972 bool Tabular::haveLTHead(bool withcaptions) const
1974 if (!is_long_tabular)
1976 for (row_type i = 0; i < nrows(); ++i)
1977 if (row_info[i].endhead &&
1978 (withcaptions || !row_info[i].caption))
1984 bool Tabular::haveLTFirstHead(bool withcaptions) const
1986 if (!is_long_tabular || endfirsthead.empty)
1988 for (row_type r = 0; r < nrows(); ++r)
1989 if (row_info[r].endfirsthead &&
1990 (withcaptions || !row_info[r].caption))
1996 bool Tabular::haveLTFoot(bool withcaptions) const
1998 if (!is_long_tabular)
2000 for (row_type r = 0; r < nrows(); ++r)
2001 if (row_info[r].endfoot &&
2002 (withcaptions || !row_info[r].caption))
2008 bool Tabular::haveLTLastFoot(bool withcaptions) const
2010 if (!is_long_tabular || endlastfoot.empty)
2012 for (row_type r = 0; r < nrows(); ++r)
2013 if (row_info[r].endlastfoot &&
2014 (withcaptions || !row_info[r].caption))
2020 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2022 idx_type i = getFirstCellInRow(row);
2024 setMultiColumn(i, numberOfCellsInRow(row), false);
2025 setTopLine(i, false);
2026 setBottomLine(i, false);
2027 setLeftLine(i, false);
2028 setRightLine(i, false);
2029 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2030 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2031 setLTHead(row, true, endfirsthead, true);
2032 row_info[row].endfirsthead = true;
2035 unsetMultiColumn(i);
2036 // When unsetting a caption row, also all existing
2037 // captions in this row must be dissolved.
2039 row_info[row].caption = what;
2044 bool Tabular::ltCaption(row_type row) const
2046 return row_info[row].caption;
2050 bool Tabular::haveLTCaption(CaptionType captiontype) const
2052 if (!is_long_tabular)
2054 for (row_type r = 0; r < nrows(); ++r) {
2055 if (row_info[r].caption) {
2056 switch (captiontype) {
2057 case CAPTION_FIRSTHEAD:
2058 if (row_info[r].endfirsthead)
2062 if (row_info[r].endhead)
2066 if (row_info[r].endfoot)
2069 case CAPTION_LASTFOOT:
2070 if (row_info[r].endlastfoot)
2082 // end longtable support functions
2084 void Tabular::setRowAscent(row_type row, int height)
2086 if (row >= nrows() || row_info[row].ascent == height)
2088 row_info[row].ascent = height;
2092 void Tabular::setRowDescent(row_type row, int height)
2094 if (row >= nrows() || row_info[row].descent == height)
2096 row_info[row].descent = height;
2100 int Tabular::rowAscent(row_type row) const
2102 LASSERT(row < nrows(), /**/);
2103 return row_info[row].ascent;
2107 int Tabular::rowDescent(row_type row) const
2109 LASSERT(row < nrows(), /**/);
2110 return row_info[row].descent;
2114 int Tabular::height() const
2117 for (row_type row = 0; row < nrows(); ++row)
2118 height += rowAscent(row) + rowDescent(row) +
2124 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2126 LASSERT(row < nrows(), /**/);
2127 LASSERT(column < ncols(), /**/);
2128 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2132 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2134 LASSERT(row < nrows(), /**/);
2135 LASSERT(column < ncols(), /**/);
2136 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2140 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2142 // we only output complete row lines and the 1st row here, the rest
2143 // is done in Tabular::TeXBottomHLine(...)
2145 // get for each column the topline (if any)
2146 vector<bool> topline;
2148 for (col_type c = 0; c < ncols(); ++c) {
2149 topline.push_back(topLine(cellIndex(row, c)));
2150 // If cell is part of a multirow and not the first cell of the
2151 // multirow, no line must be drawn.
2153 if (isMultiRow(cellIndex(row, c))
2154 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2160 // do nothing if empty first row, or incomplete row line after
2161 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2164 // only output complete row lines and the 1st row's clines
2165 if (nset == ncols()) {
2167 os << (row == 0 ? "\\toprule " : "\\midrule ");
2171 } else if (row == 0) {
2172 for (col_type c = 0; c < ncols(); ++c) {
2174 col_type offset = 0;
2175 for (col_type j = 0 ; j < c; ++j)
2176 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2179 //babel makes the "-" character an active one, so we have to suppress this here
2180 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2181 if (lang == "slovak" || lang == "czech")
2182 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2183 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2185 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2187 col_type cstart = c;
2188 for ( ; c < ncols() && topline[c]; ++c) {}
2190 for (col_type j = cstart ; j < c ; ++j)
2191 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2194 os << c + offset << "} ";
2202 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2204 // we output bottomlines of row r and the toplines of row r+1
2205 // if the latter do not span the whole tabular
2207 // get the bottomlines of row r, and toplines in next row
2208 bool lastrow = row == nrows() - 1;
2209 vector<bool> bottomline, topline;
2210 bool nextrowset = true;
2211 for (col_type c = 0; c < ncols(); ++c) {
2212 bottomline.push_back(bottomLine(cellIndex(row, c)));
2213 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2214 // If cell is part of a multirow and not the last cell of the
2215 // multirow, no line must be drawn.
2217 if (isMultiRow(cellIndex(row, c))
2218 && isMultiRow(cellIndex(row + 1, c))
2219 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2220 bottomline[c] = false;
2223 nextrowset &= topline[c];
2226 // combine this row's bottom lines and next row's toplines if necessary
2228 for (col_type c = 0; c < ncols(); ++c) {
2230 bottomline[c] = bottomline[c] || topline[c];
2235 // do nothing if empty, OR incomplete row line with a topline in next row
2236 if (nset == 0 || (nextrowset && nset != ncols()))
2239 if (nset == ncols()) {
2241 os << (lastrow ? "\\bottomrule" : "\\midrule");
2245 for (col_type c = 0; c < ncols(); ++c) {
2246 if (bottomline[c]) {
2247 col_type offset = 0;
2248 for (col_type j = 0 ; j < c; ++j)
2249 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2252 //babel makes the "-" character an active one, so we have to suppress this here
2253 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2254 if (lang == "slovak" || lang == "czech")
2255 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2256 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2258 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2260 col_type cstart = c;
2261 for ( ; c < ncols() && bottomline[c]; ++c) {}
2263 for (col_type j = cstart ; j < c ; ++j)
2264 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2267 os << c + offset << "} ";
2275 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2276 bool & ismulticol, bool & ismultirow) const
2278 row_type const r = cellRow(cell);
2279 if (is_long_tabular && row_info[r].caption)
2282 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2283 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2284 // figure out how to set the lines
2285 // we always set double lines to the right of the cell
2286 col_type const c = cellColumn(cell);
2287 col_type const nextcol = c + columnSpan(cell);
2288 bool colright = columnRightLine(c);
2289 bool colleft = columnLeftLine(c);
2290 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2291 bool nextcellleft = nextcol < ncols()
2292 && leftLine(cellIndex(r, nextcol));
2293 bool coldouble = colright && nextcolleft;
2294 bool celldouble = rightLine(cell) && nextcellleft;
2296 ismulticol = isMultiColumn(cell)
2297 || (c == 0 && colleft != leftLine(cell))
2298 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2299 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2300 || (coldouble != celldouble);
2302 // we center in multicol when no decimal point
2303 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2304 docstring const align_d = column_info[c].decimal_point;
2305 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2309 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2310 int latexcolspan = columnSpan(cell);
2311 for(col_type col = c; col < c + columnSpan(cell); ++col)
2312 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2316 os << "\\multicolumn{" << latexcolspan << "}{";
2317 if (c ==0 && leftLine(cell))
2319 if (!cellInfo(cell).align_special.empty()) {
2320 os << cellInfo(cell).align_special;
2322 if (!getPWidth(cell).zero()) {
2324 case LYX_ALIGN_LEFT:
2325 os << ">{\\raggedright}";
2327 case LYX_ALIGN_RIGHT:
2328 os << ">{\\raggedleft}";
2330 case LYX_ALIGN_CENTER:
2331 os << ">{\\centering}";
2337 case LYX_VALIGN_TOP:
2340 case LYX_VALIGN_MIDDLE:
2343 case LYX_VALIGN_BOTTOM:
2348 << from_ascii(getPWidth(cell).asLatexString())
2352 case LYX_ALIGN_LEFT:
2355 case LYX_ALIGN_RIGHT:
2362 } // end if else !getPWidth
2363 } // end if else !cellinfo_of_cell
2364 if (rightLine(cell) || nextcellleft)
2367 // add extra vertical line if we want a double one
2370 } // end if ismulticol
2372 // we only need code for the first multirow cell
2373 ismultirow = isMultiRow(cell);
2375 os << "\\multirow{" << rowSpan(cell) << "}{";
2376 if (!getPWidth(cell).zero())
2377 os << from_ascii(getPWidth(cell).asLatexString());
2379 // we need to set a default value
2382 if (!getMROffset(cell).zero())
2383 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2385 } // end if ismultirow
2387 if (getRotateCell(cell) != 0)
2388 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2390 if (getUsebox(cell) == BOX_PARBOX) {
2393 case LYX_VALIGN_TOP:
2396 case LYX_VALIGN_MIDDLE:
2399 case LYX_VALIGN_BOTTOM:
2403 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2405 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2406 os << "\\begin{minipage}[";
2408 case LYX_VALIGN_TOP:
2411 case LYX_VALIGN_MIDDLE:
2414 case LYX_VALIGN_BOTTOM:
2418 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2424 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2425 bool ismulticol, bool ismultirow) const
2427 row_type const r = cellRow(cell);
2428 if (is_long_tabular && row_info[r].caption)
2432 if (getUsebox(cell) == BOX_PARBOX)
2434 else if (getUsebox(cell) == BOX_MINIPAGE)
2435 os << breakln << "\\end{minipage}";
2436 if (getRotateCell(cell) != 0)
2437 os << breakln << "\\end{turn}";
2445 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2446 OutputParams const & runparams) const
2448 if (!is_long_tabular)
2451 // output first header info
2452 if (haveLTFirstHead()) {
2453 if (endfirsthead.topDL)
2455 for (row_type r = 0; r < nrows(); ++r) {
2456 if (row_info[r].endfirsthead)
2457 TeXRow(os, r, runparams);
2459 if (endfirsthead.bottomDL)
2461 os << "\\endfirsthead\n";
2463 // output header info
2465 if (endfirsthead.empty && !haveLTFirstHead())
2466 os << "\\endfirsthead\n";
2469 for (row_type r = 0; r < nrows(); ++r) {
2470 if (row_info[r].endhead)
2471 TeXRow(os, r, runparams);
2473 if (endhead.bottomDL)
2475 os << "\\endhead\n";
2477 // output footer info
2481 for (row_type r = 0; r < nrows(); ++r) {
2482 if (row_info[r].endfoot)
2483 TeXRow(os, r, runparams);
2485 if (endfoot.bottomDL)
2487 os << "\\endfoot\n";
2488 if (endlastfoot.empty && !haveLTLastFoot())
2489 os << "\\endlastfoot\n";
2491 // output lastfooter info
2492 if (haveLTLastFoot()) {
2493 if (endlastfoot.topDL)
2495 for (row_type r = 0; r < nrows(); ++r) {
2496 if (row_info[r].endlastfoot)
2497 TeXRow(os, r, runparams);
2499 if (endlastfoot.bottomDL)
2501 os << "\\endlastfoot\n";
2506 bool Tabular::isValidRow(row_type row) const
2508 if (!is_long_tabular)
2510 return !row_info[row].endhead && !row_info[row].endfirsthead
2511 && !row_info[row].endfoot && !row_info[row].endlastfoot
2512 && !row_info[row].caption;
2516 void Tabular::TeXRow(otexstream & os, row_type row,
2517 OutputParams const & runparams) const
2519 idx_type cell = cellIndex(row, 0);
2520 shared_ptr<InsetTableCell> inset = cellInset(cell);
2521 Paragraph const & par = inset->paragraphs().front();
2522 string const lang = par.getParLanguage(buffer().params())->lang();
2524 //output the top line
2525 TeXTopHLine(os, row, lang);
2527 if (row_info[row].top_space_default) {
2529 os << "\\addlinespace\n";
2531 os << "\\noalign{\\vskip\\doublerulesep}\n";
2532 } else if(!row_info[row].top_space.zero()) {
2534 os << "\\addlinespace["
2535 << from_ascii(row_info[row].top_space.asLatexString())
2538 os << "\\noalign{\\vskip"
2539 << from_ascii(row_info[row].top_space.asLatexString())
2543 bool ismulticol = false;
2544 bool ismultirow = false;
2545 for (col_type c = 0; c < ncols(); ++c) {
2546 if (isPartOfMultiColumn(row, c))
2549 cell = cellIndex(row, c);
2551 if (isPartOfMultiRow(row, c)
2552 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2553 if (cell != getLastCellInRow(row))
2558 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2559 shared_ptr<InsetTableCell> inset = cellInset(cell);
2561 Paragraph const & par = inset->paragraphs().front();
2562 bool rtl = par.isRTL(buffer().params())
2564 && getPWidth(cell).zero()
2565 && !runparams.use_polyglossia;
2569 par.getParLanguage(buffer().params())->lang();
2570 if (lang == "farsi")
2572 else if (lang == "arabic_arabi")
2574 // currently, remaining RTL languages are
2575 // arabic_arabtex and hebrew
2579 // pass to the OutputParams that we are in a cell and
2580 // which alignment we have set.
2581 // InsetNewline needs this context information.
2582 OutputParams newrp(runparams);
2583 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2584 ? OutputParams::PLAIN
2585 : OutputParams::ALIGNED;
2587 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2588 // copy cell and split in 2
2589 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2590 head.setBuffer(buffer());
2591 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2593 dit.push_back(CursorSlice(head));
2594 head.setMacrocontextPositionRecursive(dit);
2595 bool hassep = false;
2596 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2597 head.latex(os, newrp);
2600 tail.setBuffer(head.buffer());
2602 dit.push_back(CursorSlice(tail));
2603 tail.setMacrocontextPositionRecursive(dit);
2604 tail.latex(os, newrp);
2606 } else if (!isPartOfMultiRow(row, c)) {
2607 if (!runparams.nice)
2608 os.texrow().start(par.id(), 0);
2609 inset->latex(os, newrp);
2612 runparams.encoding = newrp.encoding;
2616 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2617 if (cell != getLastCellInRow(row)) { // not last cell in row
2624 os << "\\tabularnewline";
2625 if (row_info[row].bottom_space_default) {
2627 os << "\\addlinespace";
2629 os << "[\\doublerulesep]";
2630 } else if (!row_info[row].bottom_space.zero()) {
2632 os << "\\addlinespace";
2634 << from_ascii(row_info[row].bottom_space.asLatexString())
2639 //output the bottom line
2640 TeXBottomHLine(os, row, lang);
2642 if (row_info[row].interline_space_default) {
2644 os << "\\addlinespace\n";
2646 os << "\\noalign{\\vskip\\doublerulesep}\n";
2647 } else if (!row_info[row].interline_space.zero()) {
2649 os << "\\addlinespace["
2650 << from_ascii(row_info[row].interline_space.asLatexString())
2653 os << "\\noalign{\\vskip"
2654 << from_ascii(row_info[row].interline_space.asLatexString())
2660 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2662 bool const is_tabular_star = !tabular_width.zero();
2664 //+---------------------------------------------------------------------
2665 //+ first the opening preamble +
2666 //+---------------------------------------------------------------------
2669 if (runparams.lastid != -1)
2670 os.texrow().start(runparams.lastid, runparams.lastpos);
2673 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2675 if (is_long_tabular) {
2676 os << "\\begin{longtable}";
2677 switch (longtabular_alignment) {
2678 case LYX_LONGTABULAR_ALIGN_LEFT:
2681 case LYX_LONGTABULAR_ALIGN_CENTER:
2683 case LYX_LONGTABULAR_ALIGN_RIGHT:
2688 if (is_tabular_star)
2689 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2691 os << "\\begin{tabular}";
2692 switch (tabular_valignment) {
2693 case LYX_VALIGN_TOP:
2696 case LYX_VALIGN_MIDDLE:
2698 case LYX_VALIGN_BOTTOM:
2706 if (is_tabular_star)
2707 os << "@{\\extracolsep{\\fill}}";
2709 for (col_type c = 0; c < ncols(); ++c) {
2710 if (columnLeftLine(c))
2712 if (!column_info[c].align_special.empty()) {
2713 os << column_info[c].align_special;
2715 if (!column_info[c].p_width.zero()) {
2716 switch (column_info[c].alignment) {
2717 case LYX_ALIGN_LEFT:
2718 os << ">{\\raggedright}";
2720 case LYX_ALIGN_RIGHT:
2721 os << ">{\\raggedleft}";
2723 case LYX_ALIGN_CENTER:
2724 os << ">{\\centering}";
2726 case LYX_ALIGN_NONE:
2727 case LYX_ALIGN_BLOCK:
2728 case LYX_ALIGN_LAYOUT:
2729 case LYX_ALIGN_SPECIAL:
2730 case LYX_ALIGN_DECIMAL:
2734 switch (column_info[c].valignment) {
2735 case LYX_VALIGN_TOP:
2738 case LYX_VALIGN_MIDDLE:
2741 case LYX_VALIGN_BOTTOM:
2746 << from_ascii(column_info[c].p_width.asLatexString())
2749 switch (column_info[c].alignment) {
2750 case LYX_ALIGN_LEFT:
2753 case LYX_ALIGN_RIGHT:
2756 case LYX_ALIGN_DECIMAL:
2757 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2763 } // end if else !column_info[i].p_width
2764 } // end if else !column_info[i].align_special
2765 if (columnRightLine(c))
2770 TeXLongtableHeaderFooter(os, runparams);
2772 //+---------------------------------------------------------------------
2773 //+ the single row and columns (cells) +
2774 //+---------------------------------------------------------------------
2776 for (row_type r = 0; r < nrows(); ++r) {
2777 if (isValidRow(r)) {
2778 TeXRow(os, r, runparams);
2779 if (is_long_tabular && row_info[r].newpage)
2780 os << "\\newpage\n";
2784 //+---------------------------------------------------------------------
2785 //+ the closing of the tabular +
2786 //+---------------------------------------------------------------------
2788 if (is_long_tabular)
2789 os << "\\end{longtable}";
2791 if (is_tabular_star)
2792 os << "\\end{tabular*}";
2794 os << "\\end{tabular}";
2798 os << breakln << "\\end{turn}";
2802 int Tabular::docbookRow(odocstream & os, row_type row,
2803 OutputParams const & runparams) const
2806 idx_type cell = getFirstCellInRow(row);
2809 for (col_type c = 0; c < ncols(); ++c) {
2810 if (isPartOfMultiColumn(row, c))
2813 os << "<entry align=\"";
2814 switch (getAlignment(cell)) {
2815 case LYX_ALIGN_LEFT:
2818 case LYX_ALIGN_RIGHT:
2826 os << "\" valign=\"";
2827 switch (getVAlignment(cell)) {
2828 case LYX_VALIGN_TOP:
2831 case LYX_VALIGN_BOTTOM:
2834 case LYX_VALIGN_MIDDLE:
2839 if (isMultiColumn(cell)) {
2840 os << " namest=\"col" << c << "\" ";
2841 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2845 ret += cellInset(cell)->docbook(os, runparams);
2854 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2858 //+---------------------------------------------------------------------
2859 //+ first the opening preamble +
2860 //+---------------------------------------------------------------------
2862 os << "<tgroup cols=\"" << ncols()
2863 << "\" colsep=\"1\" rowsep=\"1\">\n";
2865 for (col_type c = 0; c < ncols(); ++c) {
2866 os << "<colspec colname=\"col" << c << "\" align=\"";
2867 switch (column_info[c].alignment) {
2868 case LYX_ALIGN_LEFT:
2871 case LYX_ALIGN_RIGHT:
2879 if (runparams.flavor == OutputParams::XML)
2885 //+---------------------------------------------------------------------
2886 //+ Long Tabular case +
2887 //+---------------------------------------------------------------------
2889 // output caption info
2890 // The caption flag wins over head/foot
2891 if (haveLTCaption()) {
2892 os << "<caption>\n";
2894 for (row_type r = 0; r < nrows(); ++r) {
2895 if (row_info[r].caption) {
2896 ret += docbookRow(os, r, runparams);
2899 os << "</caption>\n";
2902 // output header info
2903 if (haveLTHead(false) || haveLTFirstHead(false)) {
2906 for (row_type r = 0; r < nrows(); ++r) {
2907 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2908 !row_info[r].caption) {
2909 ret += docbookRow(os, r, runparams);
2915 // output footer info
2916 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2919 for (row_type r = 0; r < nrows(); ++r) {
2920 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2921 !row_info[r].caption) {
2922 ret += docbookRow(os, r, runparams);
2929 //+---------------------------------------------------------------------
2930 //+ the single row and columns (cells) +
2931 //+---------------------------------------------------------------------
2935 for (row_type r = 0; r < nrows(); ++r) {
2936 if (isValidRow(r)) {
2937 ret += docbookRow(os, r, runparams);
2942 //+---------------------------------------------------------------------
2943 //+ the closing of the tabular +
2944 //+---------------------------------------------------------------------
2953 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2954 OutputParams const & runparams, bool header) const
2957 string const celltag = header ? "th" : "td";
2958 idx_type cell = getFirstCellInRow(row);
2960 xs << html::StartTag("tr");
2961 for (col_type c = 0; c < ncols(); ++c) {
2962 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2967 switch (getAlignment(cell)) {
2968 case LYX_ALIGN_LEFT:
2971 case LYX_ALIGN_RIGHT:
2979 attr << " valign='";
2980 switch (getVAlignment(cell)) {
2981 case LYX_VALIGN_TOP:
2984 case LYX_VALIGN_BOTTOM:
2987 case LYX_VALIGN_MIDDLE:
2992 if (isMultiColumn(cell))
2993 attr << " colspan='" << columnSpan(cell) << "'";
2994 else if (isMultiRow(cell))
2995 attr << " rowspan='" << rowSpan(cell) << "'";
2997 xs << html::StartTag(celltag, attr.str()) << html::CR();
2998 ret += cellInset(cell)->xhtml(xs, runparams);
2999 xs << html::EndTag(celltag) << html::CR();
3002 xs << html::EndTag("tr");
3007 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3011 if (is_long_tabular) {
3012 // we'll wrap it in a div, so as to deal with alignment
3014 switch (longtabular_alignment) {
3015 case LYX_LONGTABULAR_ALIGN_LEFT:
3018 case LYX_LONGTABULAR_ALIGN_CENTER:
3021 case LYX_LONGTABULAR_ALIGN_RIGHT:
3025 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3027 // The caption flag wins over head/foot
3028 if (haveLTCaption()) {
3029 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3031 for (row_type r = 0; r < nrows(); ++r)
3032 if (row_info[r].caption)
3033 ret += xhtmlRow(xs, r, runparams);
3034 xs << html::EndTag("div") << html::CR();
3038 xs << html::StartTag("table") << html::CR();
3040 // output header info
3041 bool const havefirsthead = haveLTFirstHead(false);
3042 // if we have a first head, then we are going to ignore the
3043 // headers for the additional pages, since there aren't any
3044 // in XHTML. this test accomplishes that.
3045 bool const havehead = !havefirsthead && haveLTHead(false);
3046 if (havehead || havefirsthead) {
3047 xs << html::StartTag("thead") << html::CR();
3048 for (row_type r = 0; r < nrows(); ++r) {
3049 if (((havefirsthead && row_info[r].endfirsthead) ||
3050 (havehead && row_info[r].endhead)) &&
3051 !row_info[r].caption) {
3052 ret += xhtmlRow(xs, r, runparams, true);
3055 xs << html::EndTag("thead") << html::CR();
3057 // output footer info
3058 bool const havelastfoot = haveLTLastFoot(false);
3060 bool const havefoot = !havelastfoot && haveLTFoot(false);
3061 if (havefoot || havelastfoot) {
3062 xs << html::StartTag("tfoot") << html::CR();
3063 for (row_type r = 0; r < nrows(); ++r) {
3064 if (((havelastfoot && row_info[r].endlastfoot) ||
3065 (havefoot && row_info[r].endfoot)) &&
3066 !row_info[r].caption) {
3067 ret += xhtmlRow(xs, r, runparams);
3070 xs << html::EndTag("tfoot") << html::CR();
3073 xs << html::StartTag("tbody") << html::CR();
3074 for (row_type r = 0; r < nrows(); ++r) {
3075 if (isValidRow(r)) {
3076 ret += xhtmlRow(xs, r, runparams);
3079 xs << html::EndTag("tbody")
3081 << html::EndTag("table")
3083 if (is_long_tabular)
3084 xs << html::EndTag("div") << html::CR();
3089 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3090 vector<unsigned int> const & clen) const
3092 idx_type const fcell = getFirstCellInRow(row);
3093 idx_type const n = numberOfCellsInRow(row) + fcell;
3096 for (idx_type i = fcell; i < n; ++i) {
3106 for (idx_type i = fcell; i < n; ++i) {
3117 col_type column = cellColumn(i);
3118 int len = clen[column];
3119 while (column < ncols() - 1
3120 && isPartOfMultiColumn(row, ++column))
3121 len += clen[column] + 4;
3122 os << docstring(len, ch);
3137 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3138 vector<unsigned int> const & clen) const
3140 idx_type const fcell = getFirstCellInRow(row);
3141 idx_type const n = numberOfCellsInRow(row) + fcell;
3144 for (idx_type i = fcell; i < n; ++i) {
3145 if (bottomLine(i)) {
3154 for (idx_type i = fcell; i < n; ++i) {
3155 if (bottomLine(i)) {
3165 col_type column = cellColumn(i);
3166 int len = clen[column];
3167 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3168 len += clen[column] + 4;
3169 os << docstring(len, ch);
3170 if (bottomLine(i)) {
3184 void Tabular::plaintextPrintCell(odocstream & os,
3185 OutputParams const & runparams,
3186 idx_type cell, row_type row, col_type column,
3187 vector<unsigned int> const & clen,
3188 bool onlydata) const
3190 odocstringstream sstr;
3191 cellInset(cell)->plaintext(sstr, runparams);
3203 unsigned int len1 = sstr.str().length();
3204 unsigned int len2 = clen[column];
3205 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3206 len2 += clen[column] + 4;
3209 switch (getAlignment(cell)) {
3211 case LYX_ALIGN_LEFT:
3214 case LYX_ALIGN_RIGHT:
3218 case LYX_ALIGN_CENTER:
3224 os << docstring(len1, ' ') << sstr.str()
3225 << docstring(len2, ' ');
3227 if (rightLine(cell))
3234 void Tabular::plaintext(odocstream & os,
3235 OutputParams const & runparams, int const depth,
3236 bool onlydata, char_type delim) const
3238 // first calculate the width of the single columns
3239 vector<unsigned int> clen(ncols());
3242 // first all non multicolumn cells!
3243 for (col_type c = 0; c < ncols(); ++c) {
3245 for (row_type r = 0; r < nrows(); ++r) {
3246 idx_type cell = cellIndex(r, c);
3247 if (isMultiColumn(cell))
3249 odocstringstream sstr;
3250 cellInset(cell)->plaintext(sstr, runparams);
3251 if (clen[c] < sstr.str().length())
3252 clen[c] = sstr.str().length();
3255 // then all multicolumn cells!
3256 for (col_type c = 0; c < ncols(); ++c) {
3257 for (row_type r = 0; r < nrows(); ++r) {
3258 idx_type cell = cellIndex(r, c);
3259 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3261 odocstringstream sstr;
3262 cellInset(cell)->plaintext(sstr, runparams);
3263 int len = int(sstr.str().length());
3264 idx_type const n = columnSpan(cell);
3265 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3267 if (len > int(clen[c + n - 1]))
3268 clen[c + n - 1] = len;
3273 for (row_type r = 0; r < nrows(); ++r) {
3274 if (!onlydata && plaintextTopHLine(os, r, clen))
3275 os << docstring(depth * 2, ' ');
3276 for (col_type c = 0; c < ncols(); ++c) {
3277 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3279 if (onlydata && c > 0)
3280 // we don't use operator<< for single UCS4 character.
3281 // see explanation in docstream.h
3283 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3288 os << docstring(depth * 2, ' ');
3289 if (plaintextBottomHLine(os, r, clen))
3290 os << docstring(depth * 2, ' ');
3296 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3298 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3302 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3303 col_type column) const
3305 return cell_info[row][column].inset;
3309 void Tabular::setCellInset(row_type row, col_type column,
3310 shared_ptr<InsetTableCell> ins) const
3312 CellData & cd = cell_info[row][column];
3317 void Tabular::validate(LaTeXFeatures & features) const
3319 features.require("NeedTabularnewline");
3321 features.require("booktabs");
3322 if (is_long_tabular)
3323 features.require("longtable");
3325 features.require("rotating");
3326 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3327 if (isMultiRow(cell))
3328 features.require("multirow");
3329 if (getVAlignment(cell) != LYX_VALIGN_TOP
3330 || !getPWidth(cell).zero())
3331 features.require("array");
3332 cellInset(cell)->validate(features);
3337 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3339 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3340 ParagraphList::const_iterator cit = parlist.begin();
3341 ParagraphList::const_iterator end = parlist.end();
3343 for (; cit != end; ++cit)
3344 for (int i = 0; i < cit->size(); ++i)
3345 if (cit->isNewline(i))
3352 /////////////////////////////////////////////////////////////////////
3356 /////////////////////////////////////////////////////////////////////
3358 InsetTableCell::InsetTableCell(Buffer * buf)
3359 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3360 contentAlign(LYX_ALIGN_CENTER)
3364 bool InsetTableCell::forcePlainLayout(idx_type) const
3366 return !isFixedWidth;
3370 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3372 return isFixedWidth;
3376 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3377 FuncStatus & status) const
3379 bool enabled = true;
3380 switch (cmd.action()) {
3382 enabled = !forcePlainLayout();
3384 case LFUN_LAYOUT_PARAGRAPH:
3385 enabled = allowParagraphCustomization();
3388 case LFUN_MATH_DISPLAY:
3389 if (!hasFixedWidth()) {
3394 return InsetText::getStatus(cur, cmd, status);
3396 status.setEnabled(enabled);
3400 docstring InsetTableCell::asString(bool intoInsets)
3403 if (paragraphs().empty())
3405 ParagraphList::const_iterator it = paragraphs().begin();
3406 ParagraphList::const_iterator en = paragraphs().end();
3408 for (; it != en; ++it) {
3413 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3419 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3422 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3423 return InsetText::xhtml(xs, rp);
3428 /////////////////////////////////////////////////////////////////////
3432 /////////////////////////////////////////////////////////////////////
3434 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3436 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3437 rowselect_(false), colselect_(false)
3442 InsetTabular::InsetTabular(InsetTabular const & tab)
3443 : Inset(tab), tabular(tab.tabular), scx_(0)
3448 InsetTabular::~InsetTabular()
3450 hideDialogs("tabular", this);
3454 void InsetTabular::setBuffer(Buffer & buf)
3456 tabular.setBuffer(buf);
3457 Inset::setBuffer(buf);
3461 bool InsetTabular::insetAllowed(InsetCode code) const
3466 case MATHMACRO_CODE:
3471 return tabular.is_long_tabular;
3479 void InsetTabular::write(ostream & os) const
3481 os << "Tabular" << endl;
3486 string InsetTabular::contextMenu(BufferView const &, int, int) const
3488 // FIXME: depending on the selection state,
3489 // we could offer a different menu.
3490 return cell(0)->contextMenuName() + ";" + contextMenuName();
3494 string InsetTabular::contextMenuName() const
3496 return "context-tabular";
3500 void InsetTabular::read(Lexer & lex)
3502 //bool const old_format = (lex.getString() == "\\LyXTable");
3510 string token = lex.getString();
3511 while (lex && token != "\\end_inset") {
3513 token = lex.getString();
3516 lex.printError("Missing \\end_inset at this point. ");
3520 int InsetTabular::rowFromY(Cursor & cur, int y) const
3522 // top y coordinate of tabular
3523 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3525 for (; r < tabular.nrows() && y > h; ++r)
3526 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3527 + tabular.interRowSpace(r);
3533 int InsetTabular::columnFromX(Cursor & cur, int x) const
3535 // left x coordinate of tabular
3536 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3538 for (; c < tabular.ncols() && x > w; ++c)
3539 w += tabular.cellWidth(c);
3544 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3546 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3547 // mi.base.textwidth << "\n";
3550 LASSERT(false, /**/);
3553 for (row_type r = 0; r < tabular.nrows(); ++r) {
3556 for (col_type c = 0; c < tabular.ncols(); ++c) {
3557 if (tabular.isPartOfMultiColumn(r, c)
3558 || tabular.isPartOfMultiRow(r, c))
3559 // multicolumn or multirow cell, but not first one
3561 idx_type const cell = tabular.cellIndex(r, c);
3564 Length const p_width = tabular.getPWidth(cell);
3565 if (!p_width.zero())
3566 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3567 tabular.cellInset(cell)->metrics(m, dim);
3568 if (!p_width.zero())
3569 dim.wid = m.base.textwidth;
3570 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3571 + tabular.interColumnSpace(cell);
3573 // FIXME(?): do we need a second metrics call?
3574 TextMetrics const & tm =
3575 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3577 // determine horizontal offset because of decimal align (if necessary)
3578 int decimal_width = 0;
3579 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3580 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3581 tail.setBuffer(tabular.buffer());
3582 // we need to set macrocontext position everywhere
3583 // otherwise we crash with nested insets (e.g. footnotes)
3584 // after decimal point
3585 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3587 dit.push_back(CursorSlice(tail));
3588 tail.setMacrocontextPositionRecursive(dit);
3590 // remove text leading decimal point
3591 docstring const align_d = tabular.column_info[c].decimal_point;
3592 dit = separatorPos(&tail, align_d);
3594 pit_type const psize = tail.paragraphs().front().size();
3596 tail.paragraphs().front().eraseChars(0,
3597 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3599 tail.metrics(m, dim1);
3600 decimal_width = dim1.width();
3603 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3604 tabular.cell_info[r][c].decimal_width = decimal_width;
3606 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3607 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3608 int const lastpardes = tm.last().second->descent()
3609 + TEXT_TO_INSET_OFFSET;
3611 switch (tabular.getVAlignment(cell)) {
3612 case Tabular::LYX_VALIGN_TOP:
3614 case Tabular::LYX_VALIGN_MIDDLE:
3615 offset = -(dim.des - lastpardes)/2;
3617 case Tabular::LYX_VALIGN_BOTTOM:
3618 offset = -(dim.des - lastpardes);
3621 tabular.cell_info[r][c].voffset = offset;
3622 maxasc = max(maxasc, dim.asc - offset);
3623 maxdes = max(maxdes, dim.des + offset);
3625 int const top_space = tabular.row_info[r].top_space_default ?
3626 default_line_space :
3627 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3628 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3629 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3630 default_line_space :
3631 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3632 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3635 // for top-alignment the first horizontal table line must be exactly at
3636 // the position of the base line of the surrounding text line
3637 // for bottom alignment, the same is for the last table line
3638 switch (tabular.tabular_valignment) {
3639 case Tabular::LYX_VALIGN_BOTTOM:
3640 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3642 case Tabular::LYX_VALIGN_MIDDLE:
3643 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3645 case Tabular::LYX_VALIGN_TOP:
3646 offset_valign_ = tabular.rowAscent(0);
3650 tabular.updateColumnWidths();
3651 dim.asc = tabular.rowAscent(0) - offset_valign_;
3652 dim.des = tabular.height() - dim.asc;
3653 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3657 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3660 if (&cur.inset() == this && cur.selection()) {
3661 if (cur.selIsMultiCell()) {
3664 getSelection(cur, rs, re, cs, ce);
3666 idx_type const cell = tabular.cellIndex(row, col);
3667 col_type const cspan = tabular.columnSpan(cell);
3668 row_type const rspan = tabular.rowSpan(cell);
3669 if (col + cspan - 1 >= cs && col <= ce
3670 && row + rspan - 1 >= rs && row <= re)
3673 if (col == tabular.cellColumn(cur.idx())
3674 && row == tabular.cellRow(cur.idx())) {
3675 CursorSlice const & beg = cur.selBegin();
3676 CursorSlice const & end = cur.selEnd();
3678 if ((end.lastpos() > 0 || end.lastpit() > 0)
3679 && end.pos() == end.lastpos() && beg.pos() == 0
3680 && end.pit() == end.lastpit() && beg.pit() == 0)
3688 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3690 x += scx_ + ADD_TO_TABULAR_WIDTH;
3692 BufferView * bv = pi.base.bv;
3693 Cursor & cur = pi.base.bv->cursor();
3696 // FIXME: As the full background is painted in drawBackground(),
3697 // we have no choice but to do a full repaint for the Text cells.
3698 pi.full_repaint = true;
3700 bool const original_selection_state = pi.selected;
3703 first_visible_cell = Tabular::npos;
3705 int yy = y + offset_valign_;
3706 for (row_type r = 0; r < tabular.nrows(); ++r) {
3708 for (col_type c = 0; c < tabular.ncols(); ++c) {
3709 if (tabular.isPartOfMultiColumn(r, c))
3712 idx = tabular.cellIndex(r, c);
3714 if (tabular.isPartOfMultiRow(r, c)) {
3715 nx += tabular.cellWidth(idx);
3719 if (first_visible_cell == Tabular::npos)
3720 first_visible_cell = idx;
3722 pi.selected |= isCellSelected(cur, r, c);
3723 int const cx = nx + tabular.textHOffset(idx);
3724 int const cy = yy + tabular.textVOffset(idx);
3725 // Cache the Inset position.
3726 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3727 cell(idx)->draw(pi, cx, cy);
3728 drawCellLines(pi, nx, yy, r, idx);
3729 nx += tabular.cellWidth(idx);
3730 pi.selected = original_selection_state;
3733 if (r + 1 < tabular.nrows())
3734 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3735 + tabular.interRowSpace(r + 1);
3740 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3742 x += scx_ + ADD_TO_TABULAR_WIDTH;
3743 y += offset_valign_ - tabular.rowAscent(0);
3744 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3745 pi.backgroundColor(this));
3749 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3751 Cursor & cur = pi.base.bv->cursor();
3754 x += scx_ + ADD_TO_TABULAR_WIDTH;
3756 if (!cur.selection())
3758 if (&cur.inset() != this)
3763 bool const full_cell_selected = isCellSelected(cur,
3764 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3766 if (cur.selIsMultiCell() || full_cell_selected) {
3767 for (row_type r = 0; r < tabular.nrows(); ++r) {
3769 for (col_type c = 0; c < tabular.ncols(); ++c) {
3770 if (tabular.isPartOfMultiColumn(r, c))
3773 idx_type const cell = tabular.cellIndex(r, c);
3775 if (tabular.isPartOfMultiRow(r, c)) {
3776 xx += tabular.cellWidth(cell);
3779 int const w = tabular.cellWidth(cell);
3780 int const h = tabular.cellHeight(cell);
3781 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3782 if (isCellSelected(cur, r, c))
3783 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3786 if (r + 1 < tabular.nrows())
3787 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3788 + tabular.interRowSpace(r + 1);
3792 // FIXME: This code has no effect because InsetTableCell does not handle
3793 // drawSelection other than the trivial implementation in Inset.
3795 // x += cellXPos(cur.idx());
3796 // x += tabular.textHOffset(cur.idx());
3797 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3802 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3803 row_type row, idx_type cell) const
3805 y -= tabular.rowAscent(row);
3806 int const w = tabular.cellWidth(cell);
3807 int const h = tabular.cellHeight(cell);
3808 Color const linecolor = pi.textColor(Color_tabularline);
3809 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3812 bool drawline = tabular.topLine(cell)
3813 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3814 pi.pain.line(x, y, x + w, y,
3815 drawline ? linecolor : gridcolor,
3816 drawline ? Painter::line_solid : Painter::line_onoffdash);
3819 drawline = tabular.bottomLine(cell);
3820 pi.pain.line(x, y + h, x + w, y + h,
3821 drawline ? linecolor : gridcolor,
3822 drawline ? Painter::line_solid : Painter::line_onoffdash);
3825 col_type const col = tabular.cellColumn(cell);
3826 drawline = tabular.leftLine(cell)
3827 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3828 pi.pain.line(x, y, x, y + h,
3829 drawline ? linecolor : gridcolor,
3830 drawline ? Painter::line_solid : Painter::line_onoffdash);
3833 x -= tabular.interColumnSpace(cell);
3834 drawline = tabular.rightLine(cell)
3835 || (col + 1 < tabular.ncols()
3836 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3837 pi.pain.line(x + w, y, x + w, y + h,
3838 drawline ? linecolor : gridcolor,
3839 drawline ? Painter::line_solid : Painter::line_onoffdash);
3843 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3845 //lyxerr << "InsetTabular::edit: " << this << endl;
3847 cur.setSelection(false);
3850 if (isRightToLeft(cur))
3851 cur.idx() = tabular.getLastCellInRow(0);
3857 if (isRightToLeft(cur))
3858 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3860 cur.idx() = tabular.numberofcells - 1;
3862 cur.pos() = cur.lastpos(); // FIXME crude guess
3864 cur.setCurrentFont();
3865 // FIXME: this accesses the position cache before it is initialized
3867 //cur.bv().fitCursor();
3871 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3873 // In a longtable, tell captions what the current float is
3874 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3875 string const saveflt = cnts.current_float();
3876 if (tabular.is_long_tabular)
3877 cnts.current_float("table");
3879 ParIterator it2 = it;
3881 size_t const end = it2.nargs();
3882 for ( ; it2.idx() < end; it2.top().forwardIdx())
3883 buffer().updateBuffer(it2, utype);
3886 if (tabular.is_long_tabular)
3887 cnts.current_float(saveflt);
3891 void InsetTabular::addToToc(DocIterator const & cpit) const
3893 DocIterator dit = cpit;
3895 size_t const end = dit.nargs();
3896 for ( ; dit.idx() < end; dit.top().forwardIdx())
3897 cell(dit.idx())->addToToc(dit);
3901 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3903 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3904 << "\n cur:" << cur);
3905 CursorSlice sl = cur.top();
3906 Cursor & bvcur = cur.bv().cursor();
3908 FuncCode const act = cmd.action();
3912 case LFUN_MOUSE_PRESS: {
3913 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3915 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3916 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3917 row_type r = rowFromY(cur, cmd.y());
3918 cur.idx() = tabular.getFirstCellInRow(r);
3921 cur.idx() = tabular.getLastCellInRow(r);
3922 cur.pos() = cur.lastpos();
3923 cur.setSelection(true);
3929 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3930 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3931 || cmd.y() > y0 + tabular.height()) {
3932 col_type c = columnFromX(cur, cmd.x());
3933 cur.idx() = tabular.cellIndex(0, c);
3936 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3937 cur.pos() = cur.lastpos();
3938 cur.setSelection(true);
3943 // do not reset cursor/selection if we have selected
3944 // some cells (bug 2715).
3945 if (cmd.button() == mouse_button::button3
3946 && &bvcur.selBegin().inset() == this
3947 && bvcur.selIsMultiCell())
3950 // Let InsetTableCell do it
3951 cell(cur.idx())->dispatch(cur, cmd);
3954 case LFUN_MOUSE_MOTION:
3955 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3956 if (cmd.button() == mouse_button::button1) {
3957 // only accept motions to places not deeper nested than the real anchor
3958 if (!bvcur.realAnchor().hasPart(cur)) {
3962 // select (additional) row
3964 row_type r = rowFromY(cur, cmd.y());
3965 cur.idx() = tabular.getLastCellInRow(r);
3966 // we need to reset the cursor's pit and pos now, as the old ones
3967 // may no longer be valid.
3970 bvcur.setCursor(cur);
3971 bvcur.setSelection(true);
3974 // select (additional) column
3976 col_type c = columnFromX(cur, cmd.x());
3977 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3978 // we need to reset the cursor's pit and pos now, as the old ones
3979 // may no longer be valid.
3982 bvcur.setCursor(cur);
3983 bvcur.setSelection(true);
3986 // only update if selection changes
3987 if (bvcur.idx() == cur.idx() &&
3988 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3989 cur.noScreenUpdate();
3990 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3991 bvcur.setCursor(cur);
3992 bvcur.setSelection(true);
3993 // if this is a multicell selection, we just set the cursor to
3994 // the beginning of the cell's text.
3995 if (bvcur.selIsMultiCell()) {
3996 bvcur.pit() = bvcur.lastpit();
3997 bvcur.pos() = bvcur.lastpos();
4002 case LFUN_MOUSE_RELEASE:
4007 case LFUN_CELL_BACKWARD:
4009 cur.setSelection(false);
4012 case LFUN_CELL_FORWARD:
4014 cur.setSelection(false);
4017 case LFUN_CHAR_FORWARD_SELECT:
4018 case LFUN_CHAR_FORWARD:
4019 case LFUN_CHAR_BACKWARD_SELECT:
4020 case LFUN_CHAR_BACKWARD:
4021 case LFUN_CHAR_RIGHT_SELECT:
4022 case LFUN_CHAR_RIGHT:
4023 case LFUN_CHAR_LEFT_SELECT:
4024 case LFUN_CHAR_LEFT:
4025 case LFUN_WORD_FORWARD:
4026 case LFUN_WORD_FORWARD_SELECT:
4027 case LFUN_WORD_BACKWARD:
4028 case LFUN_WORD_BACKWARD_SELECT:
4029 case LFUN_WORD_RIGHT:
4030 case LFUN_WORD_RIGHT_SELECT:
4031 case LFUN_WORD_LEFT:
4032 case LFUN_WORD_LEFT_SELECT: {
4033 // determine whether we move to next or previous cell, where to enter
4034 // the new cell from, and which command to "finish" (i.e., exit the
4037 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4038 FuncCode finish_lfun;
4040 if (act == LFUN_CHAR_FORWARD
4041 || act == LFUN_CHAR_FORWARD_SELECT
4042 || act == LFUN_WORD_FORWARD
4043 || act == LFUN_WORD_FORWARD_SELECT) {
4045 finish_lfun = LFUN_FINISHED_FORWARD;
4047 else if (act == LFUN_CHAR_BACKWARD
4048 || act == LFUN_CHAR_BACKWARD_SELECT
4049 || act == LFUN_WORD_BACKWARD
4050 || act == LFUN_WORD_BACKWARD_SELECT) {
4052 finish_lfun = LFUN_FINISHED_BACKWARD;
4054 // LEFT or RIGHT commands --- the interpretation will depend on the
4055 // table's direction.
4057 bool const right = act == LFUN_CHAR_RIGHT
4058 || act == LFUN_CHAR_RIGHT_SELECT
4059 || act == LFUN_WORD_RIGHT
4060 || act == LFUN_WORD_RIGHT_SELECT;
4061 next_cell = isRightToLeft(cur) != right;
4063 if (lyxrc.visual_cursor)
4064 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4066 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4069 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4070 || act == LFUN_CHAR_BACKWARD_SELECT
4071 || act == LFUN_CHAR_RIGHT_SELECT
4072 || act == LFUN_CHAR_LEFT_SELECT
4073 || act == LFUN_WORD_FORWARD_SELECT
4074 || act == LFUN_WORD_RIGHT_SELECT
4075 || act == LFUN_WORD_BACKWARD_SELECT
4076 || act == LFUN_WORD_LEFT_SELECT;
4078 // If we have a multicell selection or we're
4079 // not doing some LFUN_*_SELECT thing anyway...
4080 if (!cur.selIsMultiCell() || !select) {
4081 col_type const c = tabular.cellColumn(cur.idx());
4082 row_type const r = tabular.cellRow(cur.idx());
4083 // Are we trying to select the whole cell and is the whole cell
4084 // not yet selected?
4085 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4086 ((next_cell && cur.pit() == cur.lastpit()
4087 && cur.pos() == cur.lastpos())
4088 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4090 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4092 // ...try to dispatch to the cell's inset.
4093 cell(cur.idx())->dispatch(cur, cmd);
4095 // When we already have a selection we want to select the whole cell
4096 // before going to the next cell.
4097 if (select_whole && !empty_cell){
4098 getText(cur.idx())->selectAll(cur);
4100 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4104 // FIXME: When we support the selection of an empty cell, remove
4105 // the !empty_cell from this condition. For now we jump to the next
4106 // cell if the current cell is empty.
4107 if (cur.result().dispatched() && !empty_cell)
4111 // move to next/prev cell, as appropriate
4112 // note that we will always do this if we're selecting and we have
4113 // a multicell selection
4114 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4115 << " cell from: " << int(entry_from));
4117 moveNextCell(cur, entry_from);
4119 movePrevCell(cur, entry_from);
4120 // if we're exiting the table, call the appropriate FINISHED lfun
4121 if (sl == cur.top())
4122 cmd = FuncRequest(finish_lfun);
4126 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4131 case LFUN_DOWN_SELECT:
4133 if (!(cur.selection() && cur.selIsMultiCell()))
4134 cell(cur.idx())->dispatch(cur, cmd);
4136 cur.dispatched(); // override the cell's decision
4137 if (sl == cur.top()) {
4138 // if our Text didn't do anything to the cursor
4139 // then we try to put the cursor into the cell below
4140 // setting also the right targetX.
4141 cur.selHandle(act == LFUN_DOWN_SELECT);
4142 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4143 int const xtarget = cur.targetX();
4144 // WARNING: Once cur.idx() has been reset, the cursor is in
4145 // an inconsistent state until pos() has been set. Be careful
4146 // what you do with it!
4147 cur.idx() = tabular.cellBelow(cur.idx());
4149 TextMetrics const & tm =
4150 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4151 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4152 cur.setCurrentFont();
4155 if (sl == cur.top()) {
4156 // we trick it to go to forward after leaving the
4158 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4161 if (cur.selIsMultiCell()) {
4162 cur.pit() = cur.lastpit();
4163 cur.pos() = cur.lastpos();
4164 cur.setCurrentFont();
4165 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4168 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4171 case LFUN_UP_SELECT:
4173 if (!(cur.selection() && cur.selIsMultiCell()))
4174 cell(cur.idx())->dispatch(cur, cmd);
4175 cur.dispatched(); // override the cell's decision
4176 if (sl == cur.top()) {
4177 // if our Text didn't do anything to the cursor
4178 // then we try to put the cursor into the cell above
4179 // setting also the right targetX.
4180 cur.selHandle(act == LFUN_UP_SELECT);
4181 if (tabular.cellRow(cur.idx()) != 0) {
4182 int const xtarget = cur.targetX();
4183 // WARNING: Once cur.idx() has been reset, the cursor is in
4184 // an inconsistent state until pos() has been set. Be careful
4185 // what you do with it!
4186 cur.idx() = tabular.cellAbove(cur.idx());
4187 cur.pit() = cur.lastpit();
4188 Text const * text = cell(cur.idx())->getText(0);
4189 TextMetrics const & tm = cur.bv().textMetrics(text);
4190 ParagraphMetrics const & pm =
4191 tm.parMetrics(cur.lastpit());
4192 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4193 cur.setCurrentFont();
4196 if (sl == cur.top()) {
4197 cmd = FuncRequest(LFUN_UP);
4200 if (cur.selIsMultiCell()) {
4202 cur.pos() = cur.lastpos();
4203 cur.setCurrentFont();
4204 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4207 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4210 // case LFUN_SCREEN_DOWN: {
4211 // //if (hasSelection())
4212 // // cur.selection() = false;
4213 // col_type const col = tabular.cellColumn(cur.idx());
4214 // int const t = cur.bv().top_y() + cur.bv().height();
4215 // if (t < yo() + tabular.getHeightOfTabular()) {
4216 // cur.bv().scrollDocView(t, true);
4217 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4219 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4226 // case LFUN_SCREEN_UP: {
4227 // //if (hasSelection())
4228 // // cur.selection() = false;
4229 // col_type const col = tabular.cellColumn(cur.idx());
4230 // int const t = cur.bv().top_y() + cur.bv().height();
4232 // cur.bv().scrollDocView(t, true);
4236 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4240 // cur.par() = cur.lastpar();
4241 // cur.pos() = cur.lastpos();
4245 case LFUN_LAYOUT_TABULAR:
4246 cur.bv().showDialog("tabular");
4249 case LFUN_INSET_MODIFY: {
4251 if (cmd.getArg(1) == "from-dialog")
4252 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4254 arg = to_utf8(cmd.argument());
4255 if (!tabularFeatures(cur, arg))
4260 // insert file functions
4261 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4262 case LFUN_FILE_INSERT_PLAINTEXT:
4264 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4265 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4266 FileName(to_utf8(cmd.argument())));
4269 cur.recordUndoInset(INSERT_UNDO);
4270 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4271 // content has been replaced,
4272 // so cursor might be invalid
4273 cur.pos() = cur.lastpos();
4274 cur.pit() = cur.lastpit();
4275 bvcur.setCursor(cur);
4282 if (cur.selIsMultiCell()) {
4283 if (copySelection(cur)) {
4284 cur.recordUndoInset(DELETE_UNDO);
4288 cell(cur.idx())->dispatch(cur, cmd);
4291 case LFUN_SELF_INSERT:
4292 if (cur.selIsMultiCell()) {
4293 cur.recordUndoInset(DELETE_UNDO);
4296 cell(cur.idx())->dispatch(cur, cmd);
4299 case LFUN_CHAR_DELETE_BACKWARD:
4300 case LFUN_CHAR_DELETE_FORWARD:
4301 if (cur.selIsMultiCell()) {
4302 cur.recordUndoInset(DELETE_UNDO);
4305 cell(cur.idx())->dispatch(cur, cmd);
4309 if (!cur.selection())
4311 if (cur.selIsMultiCell()) {
4315 cell(cur.idx())->dispatch(cur, cmd);
4318 case LFUN_CLIPBOARD_PASTE:
4319 case LFUN_PRIMARY_SELECTION_PASTE: {
4320 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4321 theClipboard().getAsText() :
4322 theSelection().get();
4325 // pass to InsertPlaintextString, but
4326 // only if we have multi-cell content
4327 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4328 cur.recordUndoInset(INSERT_UNDO);
4329 if (insertPlaintextString(cur.bv(), clip, false)) {
4330 // content has been replaced,
4331 // so cursor might be invalid
4332 cur.pos() = cur.lastpos();
4333 cur.pit() = cur.lastpit();
4334 bvcur.setCursor(cur);
4338 // Let the cell handle normal text
4339 cell(cur.idx())->dispatch(cur, cmd);
4344 if (!tabularStackDirty()) {
4345 if (!cur.selIsMultiCell())
4346 cell(cur.idx())->dispatch(cur, cmd);
4349 if (theClipboard().isInternal()) {
4350 cur.recordUndoInset(INSERT_UNDO);
4351 pasteClipboard(cur);
4355 case LFUN_FONT_EMPH:
4356 case LFUN_FONT_BOLD:
4357 case LFUN_FONT_BOLDSYMBOL:
4358 case LFUN_FONT_ROMAN:
4359 case LFUN_FONT_NOUN:
4360 case LFUN_FONT_ITAL:
4361 case LFUN_FONT_FRAK:
4362 case LFUN_FONT_TYPEWRITER:
4363 case LFUN_FONT_SANS:
4364 case LFUN_TEXTSTYLE_APPLY:
4365 case LFUN_TEXTSTYLE_UPDATE:
4366 case LFUN_FONT_SIZE:
4367 case LFUN_FONT_UNDERLINE:
4368 case LFUN_FONT_STRIKEOUT:
4369 case LFUN_FONT_UULINE:
4370 case LFUN_FONT_UWAVE:
4372 case LFUN_WORD_CAPITALIZE:
4373 case LFUN_WORD_UPCASE:
4374 case LFUN_WORD_LOWCASE:
4375 case LFUN_CHARS_TRANSPOSE:
4376 if (cur.selIsMultiCell()) {
4379 getSelection(cur, rs, re, cs, ce);
4380 Cursor tmpcur = cur;
4381 for (row_type r = rs; r <= re; ++r) {
4382 for (col_type c = cs; c <= ce; ++c) {
4383 // cursor follows cell:
4384 tmpcur.idx() = tabular.cellIndex(r, c);
4385 // select this cell only:
4388 tmpcur.resetAnchor();
4389 tmpcur.pit() = tmpcur.lastpit();
4390 tmpcur.pos() = tmpcur.top().lastpos();
4391 tmpcur.setCursor(tmpcur);
4392 tmpcur.setSelection();
4393 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4398 cell(cur.idx())->dispatch(cur, cmd);
4402 case LFUN_INSET_SETTINGS:
4403 // relay this lfun to Inset, not to the cell.
4404 Inset::doDispatch(cur, cmd);
4408 // we try to handle this event in the insets dispatch function.
4409 cell(cur.idx())->dispatch(cur, cmd);
4415 // function sets an object as defined in func_status.h:
4416 // states OK, Unknown, Disabled, On, Off.
4417 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4418 FuncStatus & status) const
4420 switch (cmd.action()) {
4421 case LFUN_INSET_MODIFY: {
4422 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4425 // FIXME: We only check for the very first argument...
4426 string const s = cmd.getArg(1);
4427 // We always enable the lfun if it is coming from the dialog
4428 // because the dialog makes sure all the settings are valid,
4429 // even though the first argument might not be valid now.
4430 if (s == "from-dialog") {
4431 status.setEnabled(true);
4435 int action = Tabular::LAST_ACTION;
4437 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4438 if (tabularFeature[i].feature == s) {
4439 action = tabularFeature[i].action;
4443 if (action == Tabular::LAST_ACTION) {
4445 status.setUnknown(true);
4449 string const argument = cmd.getLongArg(2);
4451 row_type sel_row_start = 0;
4452 row_type sel_row_end = 0;
4453 col_type sel_col_start = 0;
4454 col_type sel_col_end = 0;
4455 Tabular::ltType dummyltt;
4458 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4461 case Tabular::SET_PWIDTH:
4462 case Tabular::SET_MPWIDTH:
4463 case Tabular::SET_SPECIAL_COLUMN:
4464 case Tabular::SET_SPECIAL_MULTICOLUMN:
4465 case Tabular::APPEND_ROW:
4466 case Tabular::APPEND_COLUMN:
4467 case Tabular::DELETE_ROW:
4468 case Tabular::DELETE_COLUMN:
4469 case Tabular::COPY_ROW:
4470 case Tabular::COPY_COLUMN:
4471 case Tabular::SET_TOP_SPACE:
4472 case Tabular::SET_BOTTOM_SPACE:
4473 case Tabular::SET_INTERLINE_SPACE:
4477 case Tabular::SET_TABULAR_WIDTH:
4478 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4479 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4482 case Tabular::MOVE_COLUMN_RIGHT:
4483 case Tabular::MOVE_COLUMN_LEFT:
4484 case Tabular::MOVE_ROW_DOWN:
4485 case Tabular::MOVE_ROW_UP: {
4486 if (cur.selection()) {
4487 status.message(_("Selections not supported."));
4488 status.setEnabled(false);
4492 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4493 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4494 (action == Tabular::MOVE_COLUMN_LEFT &&
4495 tabular.cellColumn(cur.idx()) == 0) ||
4496 (action == Tabular::MOVE_ROW_DOWN &&
4497 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4498 (action == Tabular::MOVE_ROW_UP &&
4499 tabular.cellRow(cur.idx()) == 0)) {
4500 status.setEnabled(false);
4504 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4505 action == Tabular::MOVE_COLUMN_LEFT) {
4506 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4507 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4508 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4509 status.message(_("Multi-column in current or"
4510 " destination column."));
4511 status.setEnabled(false);
4516 if (action == Tabular::MOVE_ROW_DOWN ||
4517 action == Tabular::MOVE_ROW_UP) {
4518 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4519 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4520 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4521 status.message(_("Multi-row in current or"
4522 " destination row."));
4523 status.setEnabled(false);
4528 status.setEnabled(true);
4532 case Tabular::SET_DECIMAL_POINT:
4534 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4537 case Tabular::SET_MULTICOLUMN:
4538 case Tabular::UNSET_MULTICOLUMN:
4539 case Tabular::MULTICOLUMN:
4540 // If a row is set as longtable caption, it must not be allowed
4541 // to unset that this row is a multicolumn.
4542 status.setEnabled(sel_row_start == sel_row_end
4543 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4544 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4547 case Tabular::SET_MULTIROW:
4548 case Tabular::UNSET_MULTIROW:
4549 case Tabular::MULTIROW:
4550 // If a row is set as longtable caption, it must not be allowed
4551 // to unset that this row is a multirow.
4552 status.setEnabled(sel_col_start == sel_col_end
4553 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4554 status.setOnOff(tabular.isMultiRow(cur.idx()));
4557 case Tabular::SET_ALL_LINES:
4558 case Tabular::UNSET_ALL_LINES:
4559 case Tabular::SET_BORDER_LINES:
4560 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4563 case Tabular::SET_LINE_TOP:
4564 case Tabular::SET_LINE_BOTTOM:
4565 case Tabular::SET_LINE_LEFT:
4566 case Tabular::SET_LINE_RIGHT:
4567 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4570 case Tabular::TOGGLE_LINE_TOP:
4571 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4572 status.setOnOff(tabular.topLine(cur.idx()));
4575 case Tabular::TOGGLE_LINE_BOTTOM:
4576 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4577 status.setOnOff(tabular.bottomLine(cur.idx()));
4580 case Tabular::TOGGLE_LINE_LEFT:
4581 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4582 status.setOnOff(tabular.leftLine(cur.idx()));
4585 case Tabular::TOGGLE_LINE_RIGHT:
4586 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4587 status.setOnOff(tabular.rightLine(cur.idx()));
4590 // multirow cells only inherit the alignment of the column if the column has
4591 // no width, otherwise they are left-aligned
4592 // therefore allow always left but right and center only if there is no width
4593 case Tabular::M_ALIGN_LEFT:
4595 case Tabular::ALIGN_LEFT:
4596 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4599 case Tabular::M_ALIGN_RIGHT:
4601 case Tabular::ALIGN_RIGHT:
4602 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4603 && !tabular.getPWidth(cur.idx()).zero()));
4604 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4607 case Tabular::M_ALIGN_CENTER:
4609 case Tabular::ALIGN_CENTER:
4610 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4611 && !tabular.getPWidth(cur.idx()).zero()));
4612 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4615 case Tabular::ALIGN_BLOCK:
4616 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4617 && !tabular.isMultiRow(cur.idx()));
4618 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4621 case Tabular::ALIGN_DECIMAL:
4622 status.setEnabled(!tabular.isMultiRow(cur.idx())
4623 && !tabular.isMultiColumn(cur.idx()));
4624 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4627 case Tabular::M_VALIGN_TOP:
4629 case Tabular::VALIGN_TOP:
4630 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4631 && !tabular.isMultiRow(cur.idx()));
4633 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4636 case Tabular::M_VALIGN_BOTTOM:
4638 case Tabular::VALIGN_BOTTOM:
4639 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4640 && !tabular.isMultiRow(cur.idx()));
4642 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4645 case Tabular::M_VALIGN_MIDDLE:
4647 case Tabular::VALIGN_MIDDLE:
4648 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4649 && !tabular.isMultiRow(cur.idx()));
4651 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4654 case Tabular::SET_LONGTABULAR:
4655 // setting as longtable is not allowed when table is inside a float
4656 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4657 || cur.innerInsetOfType(WRAP_CODE) != 0)
4658 status.setEnabled(false);
4660 status.setEnabled(true);
4661 status.setOnOff(tabular.is_long_tabular);
4664 case Tabular::UNSET_LONGTABULAR:
4665 status.setOnOff(!tabular.is_long_tabular);
4668 case Tabular::TOGGLE_ROTATE_TABULAR:
4669 case Tabular::SET_ROTATE_TABULAR:
4670 status.setOnOff(tabular.rotate != 0);
4673 case Tabular::TABULAR_VALIGN_TOP:
4674 status.setEnabled(tabular.tabular_width.zero());
4675 status.setOnOff(tabular.tabular_valignment
4676 == Tabular::LYX_VALIGN_TOP);
4678 case Tabular::TABULAR_VALIGN_MIDDLE:
4679 status.setEnabled(tabular.tabular_width.zero());
4680 status.setOnOff(tabular.tabular_valignment
4681 == Tabular::LYX_VALIGN_MIDDLE);
4683 case Tabular::TABULAR_VALIGN_BOTTOM:
4684 status.setEnabled(tabular.tabular_width.zero());
4685 status.setOnOff(tabular.tabular_valignment
4686 == Tabular::LYX_VALIGN_BOTTOM);
4689 case Tabular::LONGTABULAR_ALIGN_LEFT:
4690 status.setOnOff(tabular.longtabular_alignment
4691 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4693 case Tabular::LONGTABULAR_ALIGN_CENTER:
4694 status.setOnOff(tabular.longtabular_alignment
4695 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4697 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4698 status.setOnOff(tabular.longtabular_alignment
4699 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4702 case Tabular::UNSET_ROTATE_TABULAR:
4703 status.setOnOff(tabular.rotate == 0);
4706 case Tabular::TOGGLE_ROTATE_CELL:
4707 case Tabular::SET_ROTATE_CELL:
4708 status.setOnOff(!oneCellHasRotationState(false,
4709 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4712 case Tabular::UNSET_ROTATE_CELL:
4713 status.setOnOff(!oneCellHasRotationState(true,
4714 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4717 case Tabular::SET_USEBOX:
4718 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4721 // every row can only be one thing:
4722 // either a footer or header
4723 case Tabular::SET_LTFIRSTHEAD:
4724 status.setEnabled(sel_row_start == sel_row_end);
4725 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4728 case Tabular::UNSET_LTFIRSTHEAD:
4729 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4730 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4733 case Tabular::SET_LTHEAD:
4734 status.setEnabled(sel_row_start == sel_row_end);
4735 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4738 case Tabular::UNSET_LTHEAD:
4739 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4740 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4743 case Tabular::SET_LTFOOT:
4744 status.setEnabled(sel_row_start == sel_row_end);
4745 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4748 case Tabular::UNSET_LTFOOT:
4749 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4750 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4753 case Tabular::SET_LTLASTFOOT:
4754 status.setEnabled(sel_row_start == sel_row_end);
4755 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4758 case Tabular::UNSET_LTLASTFOOT:
4759 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4760 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4763 case Tabular::SET_LTNEWPAGE:
4764 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4766 case Tabular::UNSET_LTNEWPAGE:
4767 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4770 // only one row in head/firsthead/foot/lasthead can be the caption
4771 // and a multirow cannot be set as caption
4772 case Tabular::SET_LTCAPTION:
4773 status.setEnabled(sel_row_start == sel_row_end
4774 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4775 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4776 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4777 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4778 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4779 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4780 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4781 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4782 && !tabular.isMultiRow(sel_row_start));
4783 status.setOnOff(tabular.ltCaption(sel_row_start));
4786 case Tabular::UNSET_LTCAPTION:
4787 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4790 case Tabular::TOGGLE_LTCAPTION:
4791 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4792 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4793 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4794 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4795 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4796 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4797 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4798 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4799 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4800 status.setOnOff(tabular.ltCaption(sel_row_start));
4803 case Tabular::SET_BOOKTABS:
4804 status.setOnOff(tabular.use_booktabs);
4807 case Tabular::UNSET_BOOKTABS:
4808 status.setOnOff(!tabular.use_booktabs);
4813 status.setEnabled(false);
4819 case LFUN_CAPTION_INSERT: {
4820 // caption is only allowed in caption cell of longtable
4821 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4822 status.setEnabled(false);
4825 // only standard caption is allowed
4826 string arg = cmd.getArg(0);
4827 if (!arg.empty() && arg != "Standard") {
4828 status.setEnabled(false);
4831 // check if there is already a caption
4832 bool have_caption = false;
4833 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4834 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4835 ParagraphList::const_iterator pend = itc.paragraphs().end();
4836 for (; pit != pend; ++pit) {
4837 InsetList::const_iterator it = pit->insetList().begin();
4838 InsetList::const_iterator end = pit->insetList().end();
4839 for (; it != end; ++it) {
4840 if (it->inset->lyxCode() == CAPTION_CODE) {
4841 have_caption = true;
4846 status.setEnabled(!have_caption);
4850 // These are only enabled inside tabular
4851 case LFUN_CELL_BACKWARD:
4852 case LFUN_CELL_FORWARD:
4853 status.setEnabled(true);
4856 // disable these with multiple cells selected
4857 case LFUN_INSET_INSERT:
4858 case LFUN_TABULAR_INSERT:
4859 case LFUN_FLEX_INSERT:
4860 case LFUN_FLOAT_INSERT:
4861 case LFUN_FLOAT_WIDE_INSERT:
4862 case LFUN_FOOTNOTE_INSERT:
4863 case LFUN_MARGINALNOTE_INSERT:
4864 case LFUN_MATH_INSERT:
4865 case LFUN_MATH_MODE:
4866 case LFUN_MATH_MUTATE:
4867 case LFUN_MATH_DISPLAY:
4868 case LFUN_NOTE_INSERT:
4869 case LFUN_ARGUMENT_INSERT:
4870 case LFUN_BOX_INSERT:
4871 case LFUN_BRANCH_INSERT:
4872 case LFUN_PHANTOM_INSERT:
4873 case LFUN_WRAP_INSERT:
4874 case LFUN_PREVIEW_INSERT:
4875 case LFUN_ERT_INSERT: {
4876 if (cur.selIsMultiCell()) {
4877 status.setEnabled(false);
4880 return cell(cur.idx())->getStatus(cur, cmd, status);
4883 // disable in non-fixed-width cells
4884 case LFUN_PARAGRAPH_BREAK:
4885 // multirow does not allow paragraph breaks
4886 if (tabular.isMultiRow(cur.idx())) {
4887 status.setEnabled(false);
4890 case LFUN_NEWLINE_INSERT: {
4891 if (tabular.getPWidth(cur.idx()).zero()) {
4892 status.setEnabled(false);
4895 return cell(cur.idx())->getStatus(cur, cmd, status);
4898 case LFUN_NEWPAGE_INSERT:
4899 status.setEnabled(false);
4903 if (tabularStackDirty() && theClipboard().isInternal()) {
4904 if (cur.selIsMultiCell()) {
4907 getSelection(cur, rs, re, cs, ce);
4908 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4909 && paste_tabular->nrows() == re - rs + 1)
4910 status.setEnabled(true);
4912 status.setEnabled(false);
4913 status.message(_("Selection size should match clipboard content."));
4916 status.setEnabled(true);
4919 return cell(cur.idx())->getStatus(cur, cmd, status);
4921 case LFUN_INSET_SETTINGS:
4922 // relay this lfun to Inset, not to the cell.
4923 return Inset::getStatus(cur, cmd, status);
4926 // we try to handle this event in the insets dispatch function.
4927 return cell(cur.idx())->getStatus(cur, cmd, status);
4933 Inset::DisplayType InsetTabular::display() const
4935 if (tabular.is_long_tabular) {
4936 switch (tabular.longtabular_alignment) {
4937 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4939 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4941 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4951 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4953 tabular.latex(os, runparams);
4957 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4959 os << '\n'; // output table on a new line
4960 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4961 tabular.plaintext(os, runparams, dp, false, 0);
4962 return PLAINTEXT_NEWLINE;
4966 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4971 // FIXME: Why not pass a proper DocIterator here?
4973 // if the table is inside a float it doesn't need the informaltable
4974 // wrapper. Search for it.
4975 for (master = owner(); master; master = master->owner())
4976 if (master->lyxCode() == FLOAT_CODE)
4981 os << "<informaltable>";
4984 ret += tabular.docbook(os, runparams);
4986 os << "</informaltable>";
4993 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4995 return tabular.xhtml(xs, rp);
4999 void InsetTabular::validate(LaTeXFeatures & features) const
5001 tabular.validate(features);
5003 // It'd be better to be able to get this from an InsetLayout, but at present
5004 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5005 if (features.runparams().flavor == OutputParams::HTML)
5006 features.addCSSSnippet(
5007 "table { border: 1px solid black; display: inline-block; }\n"
5008 "td { border: 1px solid black; padding: 0.5ex; }");
5012 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5014 return tabular.cellInset(idx);
5018 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5020 return tabular.cellInset(idx);
5024 void InsetTabular::cursorPos(BufferView const & bv,
5025 CursorSlice const & sl, bool boundary, int & x, int & y) const
5027 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5029 // y offset correction
5030 y += cellYPos(sl.idx());
5031 y += tabular.textVOffset(sl.idx());
5032 y += offset_valign_;
5034 // x offset correction
5035 x += cellXPos(sl.idx());
5036 x += tabular.textHOffset(sl.idx());
5037 x += ADD_TO_TABULAR_WIDTH;
5042 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5046 Inset const & inset = *tabular.cellInset(cell);
5047 Point o = bv.coordCache().getInsets().xy(&inset);
5048 int const xbeg = o.x_ - tabular.textHOffset(cell);
5049 int const xend = xbeg + tabular.cellWidth(cell);
5050 row_type const row = tabular.cellRow(cell);
5051 int const ybeg = o.y_ - tabular.rowAscent(row)
5052 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5053 int const yend = ybeg + tabular.cellHeight(cell);
5065 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5066 // << " ybeg=" << ybeg << " yend=" << yend
5067 // << " xx=" << xx << " yy=" << yy
5068 // << " dist=" << xx + yy << endl;
5073 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5075 //lyxerr << "InsetTabular::editXY: " << this << endl;
5076 cur.setSelection(false);
5078 cur.idx() = getNearestCell(cur.bv(), x, y);
5080 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5084 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5086 cur.idx() = getNearestCell(cur.bv(), x, y);
5087 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5091 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5093 idx_type idx_min = 0;
5094 int dist_min = numeric_limits<int>::max();
5095 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5096 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5097 int const d = dist(bv, i, x, y);
5108 int InsetTabular::cellYPos(idx_type const cell) const
5110 row_type row = tabular.cellRow(cell);
5112 for (row_type r = 0; r < row; ++r)
5113 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5114 + tabular.interRowSpace(r + 1);
5119 int InsetTabular::cellXPos(idx_type const cell) const
5121 col_type col = tabular.cellColumn(cell);
5123 for (col_type c = 0; c < col; ++c)
5124 lx += tabular.column_info[c].width;
5129 void InsetTabular::resetPos(Cursor & cur) const
5131 BufferView & bv = cur.bv();
5132 int const maxwidth = bv.workWidth();
5134 int const scx_old = scx_;
5135 int const i = cur.find(this);
5140 int const X2 = maxwidth;
5141 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5142 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5143 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5146 scx_ = X1 + 20 - x1;
5148 scx_ = X2 - 20 - x2;
5153 // only update if offset changed
5154 if (scx_ != scx_old)
5155 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5159 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5161 row_type const row = tabular.cellRow(cur.idx());
5162 col_type const col = tabular.cellColumn(cur.idx());
5164 if (isRightToLeft(cur)) {
5165 if (tabular.cellColumn(cur.idx()) == 0) {
5166 if (row == tabular.nrows() - 1)
5168 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5173 cur.idx() = tabular.getLastCellInRow(row - 1);
5175 cur.idx() = tabular.cellIndex(row, col - 1);
5178 if (tabular.isLastCell(cur.idx()))
5180 if (cur.idx() == tabular.getLastCellInRow(row))
5181 cur.idx() = tabular.cellIndex(row + 1, 0);
5183 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5184 cur.idx() = tabular.cellIndex(row, colnextcell);
5188 cur.boundary(false);
5190 if (cur.selIsMultiCell()) {
5191 cur.pit() = cur.lastpit();
5192 cur.pos() = cur.lastpos();
5200 // in visual mode, place cursor at extreme left or right
5202 switch(entry_from) {
5204 case ENTRY_DIRECTION_RIGHT:
5205 cur.posVisToRowExtremity(false /* !left */);
5207 case ENTRY_DIRECTION_LEFT:
5208 cur.posVisToRowExtremity(true /* left */);
5210 case ENTRY_DIRECTION_IGNORE:
5211 // nothing to do in this case
5215 cur.setCurrentFont();
5220 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5222 row_type const row = tabular.cellRow(cur.idx());
5223 col_type const col = tabular.cellColumn(cur.idx());
5225 if (isRightToLeft(cur)) {
5226 if (cur.idx() == tabular.getLastCellInRow(row)) {
5229 cur.idx() = tabular.getFirstCellInRow(row);
5230 cur.idx() = tabular.cellAbove(cur.idx());
5232 if (tabular.isLastCell(cur.idx()))
5234 if (cur.idx() == tabular.getLastCellInRow(row))
5235 cur.idx() = tabular.cellIndex(row + 1, 0);
5237 cur.idx() = tabular.cellIndex(row, col + 1);
5240 if (cur.idx() == 0) // first cell
5243 cur.idx() = tabular.getLastCellInRow(row - 1);
5245 cur.idx() = tabular.cellIndex(row, col - 1);
5248 if (cur.selIsMultiCell()) {
5249 cur.pit() = cur.lastpit();
5250 cur.pos() = cur.lastpos();
5255 cur.pit() = cur.lastpit();
5256 cur.pos() = cur.lastpos();
5258 // in visual mode, place cursor at extreme left or right
5260 switch(entry_from) {
5262 case ENTRY_DIRECTION_RIGHT:
5263 cur.posVisToRowExtremity(false /* !left */);
5265 case ENTRY_DIRECTION_LEFT:
5266 cur.posVisToRowExtremity(true /* left */);
5268 case ENTRY_DIRECTION_IGNORE:
5269 // nothing to do in this case
5273 cur.setCurrentFont();
5278 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5280 istringstream is(argument);
5283 if (insetCode(s) != TABULAR_CODE)
5287 size_t safe_guard = 0;
5292 if (safe_guard > 1000) {
5293 LYXERR0("parameter max count reached!");
5297 Tabular::Feature action = Tabular::LAST_ACTION;
5300 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5301 if (s != tabularFeature[i].feature)
5303 action = tabularFeature[i].action;
5306 if (action == Tabular::LAST_ACTION) {
5307 LYXERR0("Feature not found " << s);
5311 if (tabularFeature[i].need_value)
5313 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5314 tabularFeatures(cur, action, val);
5320 static void checkLongtableSpecial(Tabular::ltType & ltt,
5321 string const & special, bool & flag)
5323 if (special == "dl_above") {
5326 } else if (special == "dl_below") {
5327 ltt.bottomDL = flag;
5329 } else if (special == "empty") {
5339 bool InsetTabular::oneCellHasRotationState(bool rotated,
5340 row_type row_start, row_type row_end,
5341 col_type col_start, col_type col_end) const
5343 for (row_type r = row_start; r <= row_end; ++r)
5344 for (col_type c = col_start; c <= col_end; ++c)
5346 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5349 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5356 void InsetTabular::tabularFeatures(Cursor & cur,
5357 Tabular::Feature feature, string const & value)
5359 col_type sel_col_start;
5360 col_type sel_col_end;
5361 row_type sel_row_start;
5362 row_type sel_row_end;
5363 bool setLines = false;
5364 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5365 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5369 case Tabular::M_ALIGN_LEFT:
5370 case Tabular::ALIGN_LEFT:
5371 setAlign = LYX_ALIGN_LEFT;
5374 case Tabular::M_ALIGN_RIGHT:
5375 case Tabular::ALIGN_RIGHT:
5376 setAlign = LYX_ALIGN_RIGHT;
5379 case Tabular::M_ALIGN_CENTER:
5380 case Tabular::ALIGN_CENTER:
5381 setAlign = LYX_ALIGN_CENTER;
5384 case Tabular::ALIGN_BLOCK:
5385 setAlign = LYX_ALIGN_BLOCK;
5388 case Tabular::ALIGN_DECIMAL:
5389 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5390 setAlign = LYX_ALIGN_CENTER;
5392 setAlign = LYX_ALIGN_DECIMAL;
5395 case Tabular::M_VALIGN_TOP:
5396 case Tabular::VALIGN_TOP:
5397 setVAlign = Tabular::LYX_VALIGN_TOP;
5400 case Tabular::M_VALIGN_BOTTOM:
5401 case Tabular::VALIGN_BOTTOM:
5402 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5405 case Tabular::M_VALIGN_MIDDLE:
5406 case Tabular::VALIGN_MIDDLE:
5407 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5414 cur.recordUndoInset(ATOMIC_UNDO, this);
5416 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5417 row_type const row = tabular.cellRow(cur.idx());
5418 col_type const column = tabular.cellColumn(cur.idx());
5420 Tabular::ltType ltt;
5424 case Tabular::SET_TABULAR_WIDTH:
5425 tabular.setTabularWidth(Length(value));
5428 case Tabular::SET_PWIDTH: {
5429 Length const len(value);
5430 tabular.setColumnPWidth(cur, cur.idx(), len);
5432 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5433 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5437 case Tabular::SET_MPWIDTH:
5438 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5441 case Tabular::SET_MROFFSET:
5442 tabular.setMROffset(cur, cur.idx(), Length(value));
5445 case Tabular::SET_SPECIAL_COLUMN:
5446 case Tabular::SET_SPECIAL_MULTICOLUMN:
5447 if (value == "none")
5448 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5450 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5453 case Tabular::APPEND_ROW:
5454 // append the row into the tabular
5455 tabular.appendRow(row);
5458 case Tabular::APPEND_COLUMN:
5459 // append the column into the tabular
5460 tabular.appendColumn(column);
5461 cur.idx() = tabular.cellIndex(row, column);
5464 case Tabular::DELETE_ROW:
5465 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5466 for (col_type c = 0; c < tabular.ncols(); c++)
5467 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5468 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5471 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5472 tabular.deleteRow(sel_row_start);
5473 if (sel_row_start >= tabular.nrows())
5475 cur.idx() = tabular.cellIndex(sel_row_start, column);
5478 cur.setSelection(false);
5481 case Tabular::DELETE_COLUMN:
5482 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5483 for (row_type r = 0; r < tabular.nrows(); r++)
5484 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5485 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5488 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5489 for (row_type r = 0; r < tabular.nrows(); r++)
5490 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5491 tabular.leftLine(tabular.cellIndex(r, 0)));
5494 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5495 tabular.deleteColumn(sel_col_start);
5496 if (sel_col_start >= tabular.ncols())
5498 cur.idx() = tabular.cellIndex(row, sel_col_start);
5501 cur.setSelection(false);
5504 case Tabular::COPY_ROW:
5505 tabular.copyRow(row);
5508 case Tabular::COPY_COLUMN:
5509 tabular.copyColumn(column);
5510 cur.idx() = tabular.cellIndex(row, column);
5513 case Tabular::MOVE_COLUMN_RIGHT:
5514 tabular.moveColumn(column, Tabular::RIGHT);
5515 cur.idx() = tabular.cellIndex(row, column + 1);
5518 case Tabular::MOVE_COLUMN_LEFT:
5519 tabular.moveColumn(column, Tabular::LEFT);
5520 cur.idx() = tabular.cellIndex(row, column - 1);
5523 case Tabular::MOVE_ROW_DOWN:
5524 tabular.moveRow(row, Tabular::DOWN);
5525 cur.idx() = tabular.cellIndex(row + 1, column);
5528 case Tabular::MOVE_ROW_UP:
5529 tabular.moveRow(row, Tabular::UP);
5530 cur.idx() = tabular.cellIndex(row - 1, column);
5533 case Tabular::SET_LINE_TOP:
5534 case Tabular::TOGGLE_LINE_TOP: {
5535 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5536 ? (value == "true") : !tabular.topLine(cur.idx());
5537 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5538 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5539 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5543 case Tabular::SET_LINE_BOTTOM:
5544 case Tabular::TOGGLE_LINE_BOTTOM: {
5545 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5546 ? (value == "true") : !tabular.bottomLine(cur.idx());
5547 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5548 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5549 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5553 case Tabular::SET_LINE_LEFT:
5554 case Tabular::TOGGLE_LINE_LEFT: {
5555 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5556 ? (value == "true") : !tabular.leftLine(cur.idx());
5557 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5558 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5559 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5563 case Tabular::SET_LINE_RIGHT:
5564 case Tabular::TOGGLE_LINE_RIGHT: {
5565 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5566 ? (value == "true") : !tabular.rightLine(cur.idx());
5567 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5568 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5569 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5573 case Tabular::M_ALIGN_LEFT:
5574 case Tabular::M_ALIGN_RIGHT:
5575 case Tabular::M_ALIGN_CENTER:
5576 case Tabular::ALIGN_LEFT:
5577 case Tabular::ALIGN_RIGHT:
5578 case Tabular::ALIGN_CENTER:
5579 case Tabular::ALIGN_BLOCK:
5580 case Tabular::ALIGN_DECIMAL:
5581 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5582 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5583 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5584 !tabular.getPWidth(c).zero());
5587 case Tabular::M_VALIGN_TOP:
5588 case Tabular::M_VALIGN_BOTTOM:
5589 case Tabular::M_VALIGN_MIDDLE:
5591 case Tabular::VALIGN_TOP:
5592 case Tabular::VALIGN_BOTTOM:
5593 case Tabular::VALIGN_MIDDLE:
5594 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5595 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5596 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5599 case Tabular::SET_MULTICOLUMN: {
5600 if (!cur.selection()) {
5601 // just multicol for one single cell
5602 // check whether we are completely in a multicol
5603 if (!tabular.isMultiColumn(cur.idx()))
5604 tabular.setMultiColumn(cur.idx(), 1,
5605 tabular.rightLine(cur.idx()));
5608 // we have a selection so this means we just add all these
5609 // cells to form a multicolumn cell
5610 idx_type const s_start = cur.selBegin().idx();
5611 row_type const col_start = tabular.cellColumn(s_start);
5612 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5613 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5614 tabular.rightLine(cur.selEnd().idx()));
5617 cur.setSelection(false);
5621 case Tabular::UNSET_MULTICOLUMN: {
5622 if (!cur.selection()) {
5623 if (tabular.isMultiColumn(cur.idx()))
5624 tabular.unsetMultiColumn(cur.idx());
5629 case Tabular::MULTICOLUMN: {
5630 if (!cur.selection()) {
5631 if (tabular.isMultiColumn(cur.idx()))
5632 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5634 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5638 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5639 row_type const r = sel_row_start;
5640 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5641 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5644 // If the selection contains at least one singlecol cell
5645 // or multiple multicol cells,
5646 // we assume the user will merge is to a single multicol
5648 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5650 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5654 case Tabular::SET_MULTIROW: {
5655 if (!cur.selection()) {
5656 // just multirow for one single cell
5657 // check whether we are completely in a multirow
5658 if (!tabular.isMultiRow(cur.idx()))
5659 tabular.setMultiRow(cur.idx(), 1,
5660 tabular.bottomLine(cur.idx()));
5663 // we have a selection so this means we just add all this
5664 // cells to form a multirow cell
5665 idx_type const s_start = cur.selBegin().idx();
5666 row_type const row_start = tabular.cellRow(s_start);
5667 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5668 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5669 tabular.bottomLine(cur.selEnd().idx()));
5672 cur.setSelection(false);
5676 case Tabular::UNSET_MULTIROW: {
5677 if (!cur.selection()) {
5678 if (tabular.isMultiRow(cur.idx()))
5679 tabular.unsetMultiRow(cur.idx());
5684 case Tabular::MULTIROW: {
5685 if (!cur.selection()) {
5686 if (tabular.isMultiRow(cur.idx()))
5687 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5689 tabularFeatures(cur, Tabular::SET_MULTIROW);
5693 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5694 col_type const c = sel_col_start;
5695 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5696 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5699 // If the selection contains at least one singlerow cell
5700 // or multiple multirow cells,
5701 // we assume the user will merge is to a single multirow
5703 tabularFeatures(cur, Tabular::SET_MULTIROW);
5705 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5709 case Tabular::SET_ALL_LINES:
5711 case Tabular::UNSET_ALL_LINES:
5712 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5713 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5714 idx_type const cell = tabular.cellIndex(r, c);
5715 tabular.setTopLine(cell, setLines);
5716 tabular.setBottomLine(cell, setLines);
5717 tabular.setRightLine(cell, setLines);
5718 tabular.setLeftLine(cell, setLines);
5722 case Tabular::SET_BORDER_LINES:
5723 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5724 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5725 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5727 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5728 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5729 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5733 case Tabular::SET_LONGTABULAR:
5734 tabular.is_long_tabular = true;
5737 case Tabular::UNSET_LONGTABULAR:
5738 for (row_type r = 0; r < tabular.nrows(); ++r) {
5739 if (tabular.ltCaption(r)) {
5740 cur.idx() = tabular.cellIndex(r, 0);
5743 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5746 tabular.is_long_tabular = false;
5749 case Tabular::SET_ROTATE_TABULAR:
5750 tabular.rotate = convert<int>(value);
5753 case Tabular::UNSET_ROTATE_TABULAR:
5757 case Tabular::TOGGLE_ROTATE_TABULAR:
5758 // when pressing the rotate button we default to 90° rotation
5759 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5762 case Tabular::TABULAR_VALIGN_TOP:
5763 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5766 case Tabular::TABULAR_VALIGN_MIDDLE:
5767 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5770 case Tabular::TABULAR_VALIGN_BOTTOM:
5771 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5774 case Tabular::LONGTABULAR_ALIGN_LEFT:
5775 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5778 case Tabular::LONGTABULAR_ALIGN_CENTER:
5779 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5782 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5783 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5788 case Tabular::SET_ROTATE_CELL:
5789 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5790 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5791 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5794 case Tabular::UNSET_ROTATE_CELL:
5795 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5796 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5797 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5800 case Tabular::TOGGLE_ROTATE_CELL:
5802 bool oneNotRotated = oneCellHasRotationState(false,
5803 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5805 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5806 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5807 // when pressing the rotate cell button we default to 90° rotation
5809 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5811 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5816 case Tabular::SET_USEBOX: {
5817 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5818 if (val == tabular.getUsebox(cur.idx()))
5819 val = Tabular::BOX_NONE;
5820 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5821 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5822 tabular.setUsebox(tabular.cellIndex(r, c), val);
5826 case Tabular::UNSET_LTFIRSTHEAD:
5828 case Tabular::SET_LTFIRSTHEAD:
5829 tabular.getRowOfLTFirstHead(row, ltt);
5830 checkLongtableSpecial(ltt, value, flag);
5831 tabular.setLTHead(row, flag, ltt, true);
5834 case Tabular::UNSET_LTHEAD:
5836 case Tabular::SET_LTHEAD:
5837 tabular.getRowOfLTHead(row, ltt);
5838 checkLongtableSpecial(ltt, value, flag);
5839 tabular.setLTHead(row, flag, ltt, false);
5842 case Tabular::UNSET_LTFOOT:
5844 case Tabular::SET_LTFOOT:
5845 tabular.getRowOfLTFoot(row, ltt);
5846 checkLongtableSpecial(ltt, value, flag);
5847 tabular.setLTFoot(row, flag, ltt, false);
5850 case Tabular::UNSET_LTLASTFOOT:
5852 case Tabular::SET_LTLASTFOOT:
5853 tabular.getRowOfLTLastFoot(row, ltt);
5854 checkLongtableSpecial(ltt, value, flag);
5855 tabular.setLTFoot(row, flag, ltt, true);
5858 case Tabular::UNSET_LTNEWPAGE:
5860 case Tabular::SET_LTNEWPAGE:
5861 tabular.setLTNewPage(row, flag);
5864 case Tabular::SET_LTCAPTION: {
5865 if (tabular.ltCaption(row))
5867 cur.idx() = tabular.setLTCaption(row, true);
5870 cur.setSelection(false);
5871 // If a row is set as caption, then also insert
5872 // a caption. Otherwise the LaTeX output is broken.
5873 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5874 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5878 case Tabular::UNSET_LTCAPTION: {
5879 if (!tabular.ltCaption(row))
5881 cur.idx() = tabular.setLTCaption(row, false);
5884 cur.setSelection(false);
5885 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5886 if (lyx::getStatus(fr).enabled())
5891 case Tabular::TOGGLE_LTCAPTION: {
5892 if (tabular.ltCaption(row))
5893 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5895 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5899 case Tabular::SET_BOOKTABS:
5900 tabular.use_booktabs = true;
5903 case Tabular::UNSET_BOOKTABS:
5904 tabular.use_booktabs = false;
5907 case Tabular::SET_TOP_SPACE: {
5909 if (value == "default")
5910 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5911 tabular.row_info[r].top_space_default = true;
5912 else if (value == "none")
5913 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5914 tabular.row_info[r].top_space_default = false;
5915 tabular.row_info[r].top_space = len;
5917 else if (isValidLength(value, &len))
5918 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5919 tabular.row_info[r].top_space_default = false;
5920 tabular.row_info[r].top_space = len;
5925 case Tabular::SET_BOTTOM_SPACE: {
5927 if (value == "default")
5928 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5929 tabular.row_info[r].bottom_space_default = true;
5930 else if (value == "none")
5931 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5932 tabular.row_info[r].bottom_space_default = false;
5933 tabular.row_info[r].bottom_space = len;
5935 else if (isValidLength(value, &len))
5936 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5937 tabular.row_info[r].bottom_space_default = false;
5938 tabular.row_info[r].bottom_space = len;
5943 case Tabular::SET_INTERLINE_SPACE: {
5945 if (value == "default")
5946 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5947 tabular.row_info[r].interline_space_default = true;
5948 else if (value == "none")
5949 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5950 tabular.row_info[r].interline_space_default = false;
5951 tabular.row_info[r].interline_space = len;
5953 else if (isValidLength(value, &len))
5954 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5955 tabular.row_info[r].interline_space_default = false;
5956 tabular.row_info[r].interline_space = len;
5961 case Tabular::SET_DECIMAL_POINT:
5962 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5963 tabular.column_info[c].decimal_point = from_utf8(value);
5966 // dummy stuff just to avoid warnings
5967 case Tabular::LAST_ACTION:
5973 bool InsetTabular::copySelection(Cursor & cur)
5975 if (!cur.selection())
5980 getSelection(cur, rs, re, cs, ce);
5982 paste_tabular.reset(new Tabular(tabular));
5984 for (row_type r = 0; r < rs; ++r)
5985 paste_tabular->deleteRow(0);
5987 row_type const rows = re - rs + 1;
5988 while (paste_tabular->nrows() > rows)
5989 paste_tabular->deleteRow(rows);
5991 for (col_type c = 0; c < cs; ++c)
5992 paste_tabular->deleteColumn(0);
5994 col_type const columns = ce - cs + 1;
5995 while (paste_tabular->ncols() > columns)
5996 paste_tabular->deleteColumn(columns);
5998 paste_tabular->setBuffer(tabular.buffer());
6000 odocstringstream os;
6001 OutputParams const runparams(0);
6002 paste_tabular->plaintext(os, runparams, 0, true, '\t');
6003 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6004 cap::copySelection(cur, os.str());
6006 // mark tabular stack dirty
6007 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6008 // when we (hopefully) have a one-for-all paste mechanism.
6009 // This must be called after cap::copySelection.
6010 dirtyTabularStack(true);
6016 bool InsetTabular::pasteClipboard(Cursor & cur)
6020 col_type actcol = tabular.cellColumn(cur.idx());
6021 row_type actrow = tabular.cellRow(cur.idx());
6023 if (cur.selIsMultiCell()) {
6026 getSelection(cur, actrow, re, actcol, ce);
6029 for (row_type r1 = 0, r2 = actrow;
6030 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6032 for (col_type c1 = 0, c2 = actcol;
6033 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6035 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6036 tabular.isPartOfMultiColumn(r2, c2))
6038 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6042 if (tabular.isPartOfMultiColumn(r2, c2)) {
6046 shared_ptr<InsetTableCell> inset(
6047 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6048 tabular.setCellInset(r2, c2, inset);
6049 // FIXME?: why do we need to do this explicitly? (EL)
6050 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6052 // FIXME: change tracking (MG)
6053 inset->setChange(Change(buffer().params().trackChanges ?
6054 Change::INSERTED : Change::UNCHANGED));
6062 void InsetTabular::cutSelection(Cursor & cur)
6064 if (!cur.selection())
6069 getSelection(cur, rs, re, cs, ce);
6070 for (row_type r = rs; r <= re; ++r) {
6071 for (col_type c = cs; c <= ce; ++c) {
6072 shared_ptr<InsetTableCell> t
6073 = cell(tabular.cellIndex(r, c));
6074 if (buffer().params().trackChanges)
6075 // FIXME: Change tracking (MG)
6076 t->setChange(Change(Change::DELETED));
6082 // cursor position might be invalid now
6083 if (cur.pit() > cur.lastpit())
6084 cur.pit() = cur.lastpit();
6085 if (cur.pos() > cur.lastpos())
6086 cur.pos() = cur.lastpos();
6087 cur.clearSelection();
6091 bool InsetTabular::isRightToLeft(Cursor & cur) const
6093 LASSERT(cur.depth() > 1, /**/);
6094 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6095 pos_type const parentpos = cur[cur.depth() - 2].pos();
6096 return parentpar.getFontSettings(buffer().params(),
6097 parentpos).language()->rightToLeft();
6101 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6104 LASSERT(stidx <= enidx, return docstring());
6106 col_type const col1 = tabular.cellColumn(stidx);
6107 col_type const col2 = tabular.cellColumn(enidx);
6108 row_type const row1 = tabular.cellRow(stidx);
6109 row_type const row2 = tabular.cellRow(enidx);
6111 for (col_type col = col1; col <= col2; col++)
6112 for (row_type row = row1; row <= row2; row++) {
6117 retval += tabular.cellInset(row, col)->asString(intoInsets);
6123 void InsetTabular::getSelection(Cursor & cur,
6124 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6126 CursorSlice const & beg = cur.selBegin();
6127 CursorSlice const & end = cur.selEnd();
6128 cs = tabular.cellColumn(beg.idx());
6129 ce = tabular.cellColumn(end.idx());
6133 rs = tabular.cellRow(beg.idx());
6134 re = tabular.cellRow(end.idx());
6140 Text * InsetTabular::getText(int idx) const
6142 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6146 void InsetTabular::setChange(Change const & change)
6148 for (idx_type idx = 0; idx < nargs(); ++idx)
6149 cell(idx)->setChange(change);
6153 void InsetTabular::acceptChanges()
6155 for (idx_type idx = 0; idx < nargs(); ++idx)
6156 cell(idx)->acceptChanges();
6160 void InsetTabular::rejectChanges()
6162 for (idx_type idx = 0; idx < nargs(); ++idx)
6163 cell(idx)->rejectChanges();
6167 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6169 return tabular.getPWidth(cell).zero();
6173 bool InsetTabular::forcePlainLayout(idx_type cell) const
6175 return !tabular.getPWidth(cell).zero();
6179 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6182 if (buf.length() <= 0)
6187 col_type maxCols = 1;
6188 size_t const len = buf.length();
6192 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6200 maxCols = max(cols, maxCols);
6206 maxCols = max(cols, maxCols);
6212 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6213 loctab = paste_tabular.get();
6215 dirtyTabularStack(true);
6218 cell = bv.cursor().idx();
6219 ocol = tabular.cellColumn(cell);
6220 row = tabular.cellRow(cell);
6224 idx_type const cells = loctab->numberofcells;
6227 rows = loctab->nrows();
6228 col_type const columns = loctab->ncols();
6230 while (cell < cells && p < len && row < rows &&
6231 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6237 // we can only set this if we are not too far right
6238 if (cols < columns) {
6239 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6240 Font const font = bv.textMetrics(&inset->text()).
6242 inset->setText(buf.substr(op, p - op), font,
6243 buffer().params().trackChanges);
6249 // we can only set this if we are not too far right
6250 if (cols < columns) {
6251 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6252 Font const font = bv.textMetrics(&inset->text()).
6254 inset->setText(buf.substr(op, p - op), font,
6255 buffer().params().trackChanges);
6260 cell = loctab->cellIndex(row, cols);
6266 // check for the last cell if there is no trailing '\n'
6267 if (cell < cells && op < len) {
6268 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6269 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6270 inset->setText(buf.substr(op, len - op), font,
6271 buffer().params().trackChanges);
6277 void InsetTabular::addPreview(DocIterator const & inset_pos,
6278 PreviewLoader & loader) const
6280 DocIterator cell_pos = inset_pos;
6282 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6283 for (row_type r = 0; r < tabular.nrows(); ++r) {
6284 for (col_type c = 0; c < tabular.ncols(); ++c) {
6285 cell_pos.top().idx() = tabular.cellIndex(r, c);
6286 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6292 bool InsetTabular::completionSupported(Cursor const & cur) const
6294 Cursor const & bvCur = cur.bv().cursor();
6295 if (&bvCur.inset() != this)
6297 return cur.text()->completionSupported(cur);
6301 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6303 return completionSupported(cur);
6307 bool InsetTabular::automaticInlineCompletion() const
6309 return lyxrc.completion_inline_text;
6313 bool InsetTabular::automaticPopupCompletion() const
6315 return lyxrc.completion_popup_text;
6319 bool InsetTabular::showCompletionCursor() const
6321 return lyxrc.completion_cursor_text;
6325 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6327 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6331 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6333 if (!completionSupported(cur))
6335 return cur.text()->completionPrefix(cur);
6339 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6341 if (!completionSupported(cur))
6344 return cur.text()->insertCompletion(cur, s, finished);
6348 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6349 Dimension & dim) const
6351 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6352 tm.completionPosAndDim(cur, x, y, dim);
6356 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6358 istringstream data(in);
6360 lex.setStream(data);
6367 if (!lex || token != "tabular") {
6368 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6372 // This is part of the inset proper that is usually swallowed
6373 // by Buffer::readInset
6375 if (!lex || token != "Tabular") {
6376 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6384 string InsetTabular::params2string(InsetTabular const & inset)
6387 data << "tabular" << ' ';
6389 data << "\\end_inset\n";