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().track_changes)
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().track_changes)
773 cellInfo(i).inset->setChange(Change(Change::INSERTED));
778 void Tabular::moveColumn(col_type col, ColDirection direction)
780 if (direction == Tabular::LEFT)
783 std::swap(column_info[col], column_info[col + 1]);
785 for (row_type r = 0; r < nrows(); ++r) {
786 std::swap(cell_info[r][col], cell_info[r][col + 1]);
787 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
788 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
790 // FIXME track changes is broken for tabular features (#8469)
791 idx_type const i = cellIndex(r, col);
792 idx_type const j = cellIndex(r, col + 1);
793 if (buffer().params().track_changes) {
794 cellInfo(i).inset->setChange(Change(Change::INSERTED));
795 cellInfo(j).inset->setChange(Change(Change::INSERTED));
802 void Tabular::moveRow(row_type row, RowDirection direction)
804 if (direction == Tabular::UP)
807 std::swap(row_info[row], row_info[row + 1]);
809 for (col_type c = 0; c < ncols(); ++c) {
810 std::swap(cell_info[row][c], cell_info[row + 1][c]);
811 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
812 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
814 // FIXME track changes is broken for tabular features (#8469)
815 idx_type const i = cellIndex(row, c);
816 idx_type const j = cellIndex(row + 1, c);
817 if (buffer().params().track_changes) {
818 cellInfo(i).inset->setChange(Change(Change::INSERTED));
819 cellInfo(j).inset->setChange(Change(Change::INSERTED));
826 void Tabular::deleteColumn(col_type const col)
828 // Not allowed to delete last column
832 for (row_type r = 0; r < nrows(); ++r) {
833 // Care about multicolumn cells
834 if (col + 1 < ncols() &&
835 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
836 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
837 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
839 cell_info[r].erase(cell_info[r].begin() + col);
841 column_info.erase(column_info.begin() + col);
846 void Tabular::copyColumn(col_type const col)
848 insertColumn(col, true);
852 void Tabular::appendColumn(col_type col)
854 insertColumn(col, false);
858 void Tabular::insertColumn(col_type const col, bool copy)
860 BufferParams const & bp = buffer().params();
861 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
863 for (row_type r = 0; r < nrows(); ++r) {
864 cell_info[r].insert(cell_info[r].begin() + col + 1,
865 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
866 if (bp.track_changes)
867 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
868 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
869 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
872 for (row_type r = 0; r < nrows(); ++r) {
873 // inherit line settings
874 idx_type const i = cellIndex(r, col + 1);
875 idx_type const j = cellIndex(r, col);
876 setBottomLine(i, bottomLine(j));
877 setTopLine(i, topLine(j));
878 setLeftLine(i, leftLine(j));
879 if (rightLine(i) && rightLine(j)) {
880 setRightLine(i, true);
881 setRightLine(j, false);
883 if (buffer().params().track_changes)
884 cellInfo(i).inset->setChange(Change(Change::INSERTED));
889 void Tabular::updateIndexes()
894 for (row_type row = 0; row < nrows(); ++row)
895 for (col_type column = 0; column < ncols(); ++column) {
896 if (!isPartOfMultiColumn(row, column)
897 && !isPartOfMultiRow(row, column))
899 if (isPartOfMultiRow(row, column))
900 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
902 cell_info[row][column].cellno = numberofcells - 1;
905 rowofcell.resize(numberofcells);
906 columnofcell.resize(numberofcells);
908 // reset column and row of cells and update their width and alignment
909 for (row_type row = 0; row < nrows(); ++row)
910 for (col_type column = 0; column < ncols(); ++column) {
911 if (isPartOfMultiColumn(row, column))
913 // columnofcell needs to be called before setting width and aligment
914 // multirow cells inherit the width from the column width
915 if (!isPartOfMultiRow(row, column)) {
916 columnofcell[i] = column;
919 setFixedWidth(row, column);
920 if (isPartOfMultiRow(row, column))
922 cell_info[row][column].inset->setContentAlignment(
923 getAlignment(cellIndex(row, column)));
929 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
932 for (col_type c = 0; c < ncols(); ++c)
933 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
939 bool Tabular::topLine(idx_type const cell) const
941 return cellInfo(cell).top_line;
945 bool Tabular::bottomLine(idx_type const cell) const
947 return cellInfo(cell).bottom_line;
951 bool Tabular::leftLine(idx_type cell) const
955 return cellInfo(cell).left_line;
959 bool Tabular::rightLine(idx_type cell) const
963 return cellInfo(cell).right_line;
967 int Tabular::interRowSpace(row_type row) const
969 if (!row || row >= nrows())
972 int const interline_space = row_info[row - 1].interline_space_default ?
974 row_info[row - 1].interline_space.inPixels(width());
975 if (rowTopLine(row) && rowBottomLine(row - 1))
976 return interline_space + WIDTH_OF_LINE;
977 return interline_space;
981 int Tabular::interColumnSpace(idx_type cell) const
983 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
984 if (rightLine(cell) && nextcol < ncols()
985 && leftLine(cellIndex(cellRow(cell), nextcol)))
986 return WIDTH_OF_LINE;
991 int Tabular::cellWidth(idx_type cell) const
994 col_type const span = columnSpan(cell);
995 col_type const col = cellColumn(cell);
996 for(col_type c = col; c < col + span ; ++c)
997 w += column_info[c].width;
1002 int Tabular::cellHeight(idx_type cell) const
1004 row_type const span = rowSpan(cell);
1005 row_type const row = cellRow(cell);
1007 for(row_type r = row; r < row + span ; ++r) {
1008 h += rowAscent(r) + rowDescent(r);
1009 if (r != row + span - 1)
1010 h += interRowSpace(r + 1);
1017 bool Tabular::updateColumnWidths()
1019 vector<int> max_dwidth(ncols(), 0);
1020 for(col_type c = 0; c < ncols(); ++c)
1021 for(row_type r = 0; r < nrows(); ++r) {
1022 idx_type const i = cellIndex(r, c);
1023 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1024 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1027 bool update = false;
1028 // for each col get max of single col cells
1029 for(col_type c = 0; c < ncols(); ++c) {
1031 for(row_type r = 0; r < nrows(); ++r) {
1032 idx_type const i = cellIndex(r, c);
1033 if (columnSpan(i) == 1) {
1034 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1035 && cell_info[r][c].decimal_width!=0)
1036 new_width = max(new_width, cellInfo(i).width
1037 + max_dwidth[c] - cellInfo(i).decimal_width);
1039 new_width = max(new_width, cellInfo(i).width);
1043 if (column_info[c].width != new_width) {
1044 column_info[c].width = new_width;
1048 // update col widths to fit merged cells
1049 for(col_type c = 0; c < ncols(); ++c)
1050 for(row_type r = 0; r < nrows(); ++r) {
1051 idx_type const i = cellIndex(r, c);
1052 int const span = columnSpan(i);
1053 if (span == 1 || c > cellColumn(i))
1057 for(col_type j = c; j < c + span ; ++j)
1058 old_width += column_info[j].width;
1060 if (cellInfo(i).width > old_width) {
1061 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1070 int Tabular::width() const
1073 for (col_type c = 0; c < ncols(); ++c)
1074 width += column_info[c].width;
1079 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1082 col_type const col = cellColumn(cell);
1083 // set alignment for the whole row if we are not in a multicolumn cell,
1084 // exclude possible multicolumn cells in the row
1085 if (!isMultiColumn(cell)) {
1086 for (row_type r = 0; r < nrows(); ++r) {
1087 // only if the column has no width the multirow inherits the
1088 // alignment of the column, otherwise it is left aligned
1089 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1090 && !isMultiColumn(cellIndex(r, col))) {
1091 cell_info[r][col].alignment = align;
1092 cell_info[r][col].inset->setContentAlignment(align);
1094 if ((isMultiRow(cellIndex(r, col)) && has_width)
1095 && !isMultiColumn(cellIndex(r, col))) {
1096 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1097 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1100 column_info[col].alignment = align;
1101 docstring & dpoint = column_info[col].decimal_point;
1102 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1103 dpoint = from_utf8(lyxrc.default_decimal_point);
1105 cellInfo(cell).alignment = align;
1106 cellInset(cell).get()->setContentAlignment(align);
1111 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1114 if (!isMultiColumn(cell) || onlycolumn)
1115 column_info[cellColumn(cell)].valignment = align;
1117 cellInfo(cell).valignment = align;
1124 * Allow line and paragraph breaks for fixed width cells or disallow them,
1125 * merge cell paragraphs and reset layout to standard for variable width
1128 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1130 inset->setAutoBreakRows(fixedWidth);
1131 inset->toggleFixedWidth(fixedWidth);
1135 // merge all paragraphs to one
1136 BufferParams const & bp = cur.bv().buffer().params();
1137 while (inset->paragraphs().size() > 1)
1138 mergeParagraph(bp, inset->paragraphs(), 0);
1142 // undo information has already been recorded
1143 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1144 bp.documentClass().plainLayoutName());
1151 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1152 Length const & width)
1154 col_type const c = cellColumn(cell);
1156 column_info[c].p_width = width;
1157 // reset the vertical alignment to top if the fixed width
1158 // is removed or zero because only fixed width columns can
1159 // have a vertical alignment
1160 if (column_info[c].p_width.zero())
1161 column_info[c].valignment = LYX_VALIGN_TOP;
1162 for (row_type r = 0; r < nrows(); ++r) {
1163 idx_type const cell = cellIndex(r, c);
1164 // because of multicolumns
1165 toggleFixedWidth(cur, cellInset(cell).get(),
1166 !getPWidth(cell).zero());
1167 if (isMultiRow(cell))
1168 setAlignment(cell, LYX_ALIGN_LEFT, false);
1170 // cur paragraph can become invalid after paragraphs were merged
1171 if (cur.pit() > cur.lastpit())
1172 cur.pit() = cur.lastpit();
1173 // cur position can become invalid after newlines were removed
1174 if (cur.pos() > cur.lastpos())
1175 cur.pos() = cur.lastpos();
1179 bool Tabular::setFixedWidth(row_type r, col_type c)
1181 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1182 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1183 || (multicol && !cell_info[r][c].p_width.zero());
1184 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1189 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1190 Length const & width)
1192 if (!isMultiColumn(cell))
1195 cellInfo(cell).p_width = width;
1196 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1197 // cur paragraph can become invalid after paragraphs were merged
1198 if (cur.pit() > cur.lastpit())
1199 cur.pit() = cur.lastpit();
1200 // cur position can become invalid after newlines were removed
1201 if (cur.pos() > cur.lastpos())
1202 cur.pos() = cur.lastpos();
1207 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1209 cellInfo(cell).mroffset = mroffset;
1214 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1215 Tabular::Feature what)
1217 if (what == SET_SPECIAL_MULTICOLUMN)
1218 cellInfo(cell).align_special = special;
1220 column_info[cellColumn(cell)].align_special = special;
1224 void Tabular::setTopLine(idx_type i, bool line)
1226 cellInfo(i).top_line = line;
1230 void Tabular::setBottomLine(idx_type i, bool line)
1232 cellInfo(i).bottom_line = line;
1236 void Tabular::setLeftLine(idx_type cell, bool line)
1238 cellInfo(cell).left_line = line;
1242 void Tabular::setRightLine(idx_type cell, bool line)
1244 cellInfo(cell).right_line = line;
1247 bool Tabular::rowTopLine(row_type r) const
1249 bool all_rows_set = true;
1250 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1251 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1252 return all_rows_set;
1256 bool Tabular::rowBottomLine(row_type r) const
1258 bool all_rows_set = true;
1259 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1260 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1261 return all_rows_set;
1265 bool Tabular::columnLeftLine(col_type c) const
1272 for (row_type r = 0; r < nrows(); ++r) {
1273 idx_type const i = cellIndex(r, c);
1274 if (c == cellColumn(i)) {
1276 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1277 if (cellInfo(i).left_line || right)
1281 return 2 * nrows_left >= total;
1285 bool Tabular::columnRightLine(col_type c) const
1290 int nrows_right = 0;
1292 for (row_type r = 0; r < nrows(); ++r) {
1293 idx_type i = cellIndex(r, c);
1294 if (c == cellColumn(i) + columnSpan(i) - 1) {
1296 bool left = (c + 1 < ncols()
1297 && cellInfo(cellIndex(r, c + 1)).left_line)
1298 || c + 1 == ncols();
1299 if (cellInfo(i).right_line && left)
1303 return 2 * nrows_right >= total;
1307 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1309 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1310 return cellInfo(cell).alignment;
1312 return column_info[cellColumn(cell)].alignment;
1317 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1319 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1320 return cellInfo(cell).valignment;
1321 return column_info[cellColumn(cell)].valignment;
1325 Length const Tabular::getPWidth(idx_type cell) const
1327 if (isMultiColumn(cell))
1328 return cellInfo(cell).p_width;
1329 return column_info[cellColumn(cell)].p_width;
1333 Length const Tabular::getMROffset(idx_type cell) const
1335 return cellInfo(cell).mroffset;
1339 int Tabular::textHOffset(idx_type cell) const
1341 // the LaTeX Way :-(
1342 int x = WIDTH_OF_LINE;
1344 int const w = cellWidth(cell) - cellInfo(cell).width;
1346 switch (getAlignment(cell)) {
1347 case LYX_ALIGN_CENTER:
1350 case LYX_ALIGN_RIGHT:
1353 case LYX_ALIGN_DECIMAL: {
1354 // we center when no decimal point
1355 if (cellInfo(cell).decimal_width == 0) {
1359 col_type const c = cellColumn(cell);
1360 int max_dhoffset = 0;
1361 for(row_type r = 0; r < row_info.size() ; ++r) {
1362 idx_type const i = cellIndex(r, c);
1363 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1364 && cellInfo(i).decimal_width != 0)
1365 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1367 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1370 // LYX_ALIGN_LEFT: nothing :-)
1378 int Tabular::textVOffset(idx_type cell) const
1380 int voffset = cellInfo(cell).voffset;
1381 if (isMultiRow(cell)) {
1382 row_type const row = cellRow(cell);
1383 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1389 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1392 idx_type const numcells = numberOfCellsInRow(row);
1393 // we check against numcells to make sure we do not crash if all the
1394 // cells are multirow (bug #7535), but in that case our return value
1395 // is really invalid, i.e., it is NOT the first cell in the row. but
1396 // i do not know what to do here. (rgh)
1397 while (c < numcells - 1
1398 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1400 return cell_info[row][c].cellno;
1404 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1406 col_type c = ncols() - 1;
1407 // of course we check against 0 so we don't crash. but we have the same
1408 // problem as in the previous routine: if all the cells are part of a
1409 // multirow or part of a multi column, then our return value is invalid.
1411 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1412 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1414 return cell_info[row][c].cellno;
1418 Tabular::row_type Tabular::cellRow(idx_type cell) const
1420 if (cell >= numberofcells)
1424 return rowofcell[cell];
1428 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1430 if (cell >= numberofcells)
1434 return columnofcell[cell];
1438 void Tabular::write(ostream & os) const
1442 << write_attribute("version", 3)
1443 << write_attribute("rows", nrows())
1444 << write_attribute("columns", ncols())
1446 // global longtable options
1448 << write_attribute("rotate", rotate)
1449 << write_attribute("booktabs", use_booktabs)
1450 << write_attribute("islongtable", is_long_tabular)
1451 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1452 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1453 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1454 << write_attribute("headTopDL", endhead.topDL)
1455 << write_attribute("headBottomDL", endhead.bottomDL)
1456 << write_attribute("footTopDL", endfoot.topDL)
1457 << write_attribute("footBottomDL", endfoot.bottomDL)
1458 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1459 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1460 << write_attribute("lastFootEmpty", endlastfoot.empty);
1461 // longtables cannot be aligned vertically
1462 if (!is_long_tabular) {
1463 os << write_attribute("tabularvalignment", tabular_valignment);
1464 os << write_attribute("tabularwidth", tabular_width);
1466 if (is_long_tabular)
1467 os << write_attribute("longtabularalignment", longtabular_alignment);
1469 for (col_type c = 0; c < ncols(); ++c) {
1471 << write_attribute("alignment", column_info[c].alignment);
1472 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1473 os << write_attribute("decimal_point", column_info[c].decimal_point);
1474 os << write_attribute("valignment", column_info[c].valignment)
1475 << write_attribute("width", column_info[c].p_width.asString())
1476 << write_attribute("special", column_info[c].align_special)
1479 for (row_type r = 0; r < nrows(); ++r) {
1480 static const string def("default");
1482 if (row_info[r].top_space_default)
1483 os << write_attribute("topspace", def);
1485 os << write_attribute("topspace", row_info[r].top_space);
1486 if (row_info[r].bottom_space_default)
1487 os << write_attribute("bottomspace", def);
1489 os << write_attribute("bottomspace", row_info[r].bottom_space);
1490 if (row_info[r].interline_space_default)
1491 os << write_attribute("interlinespace", def);
1493 os << write_attribute("interlinespace", row_info[r].interline_space);
1494 os << write_attribute("endhead", row_info[r].endhead)
1495 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1496 << write_attribute("endfoot", row_info[r].endfoot)
1497 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1498 << write_attribute("newpage", row_info[r].newpage)
1499 << write_attribute("caption", row_info[r].caption)
1501 for (col_type c = 0; c < ncols(); ++c) {
1503 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1504 << write_attribute("multirow", cell_info[r][c].multirow)
1505 << write_attribute("mroffset", cell_info[r][c].mroffset)
1506 << write_attribute("alignment", cell_info[r][c].alignment)
1507 << write_attribute("valignment", cell_info[r][c].valignment)
1508 << write_attribute("topline", cell_info[r][c].top_line)
1509 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1510 << write_attribute("leftline", cell_info[r][c].left_line)
1511 << write_attribute("rightline", cell_info[r][c].right_line)
1512 << write_attribute("rotate", cell_info[r][c].rotate)
1513 << write_attribute("usebox", cell_info[r][c].usebox)
1514 << write_attribute("width", cell_info[r][c].p_width)
1515 << write_attribute("special", cell_info[r][c].align_special)
1517 os << "\\begin_inset ";
1518 cell_info[r][c].inset->write(os);
1519 os << "\n\\end_inset\n"
1521 // FIXME This can be removed again once the mystery
1522 // crash has been resolved.
1527 os << "</lyxtabular>\n";
1531 void Tabular::read(Lexer & lex)
1534 istream & is = lex.getStream();
1536 l_getline(is, line);
1537 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1538 LASSERT(false, return);
1542 if (!getTokenValue(line, "version", version))
1544 LATTEST(version >= 2);
1547 if (!getTokenValue(line, "rows", rows_arg))
1550 if (!getTokenValue(line, "columns", columns_arg))
1552 init(buffer_, rows_arg, columns_arg);
1553 l_getline(is, line);
1554 if (!prefixIs(line, "<features")) {
1555 lyxerr << "Wrong tabular format (expected <features ...> got"
1556 << line << ')' << endl;
1559 getTokenValue(line, "rotate", rotate);
1560 getTokenValue(line, "booktabs", use_booktabs);
1561 getTokenValue(line, "islongtable", is_long_tabular);
1562 getTokenValue(line, "tabularvalignment", tabular_valignment);
1563 getTokenValue(line, "tabularwidth", tabular_width);
1564 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1565 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1566 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1567 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1568 getTokenValue(line, "headTopDL", endhead.topDL);
1569 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1570 getTokenValue(line, "footTopDL", endfoot.topDL);
1571 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1572 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1573 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1574 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1576 for (col_type c = 0; c < ncols(); ++c) {
1578 if (!prefixIs(line,"<column")) {
1579 lyxerr << "Wrong tabular format (expected <column ...> got"
1580 << line << ')' << endl;
1583 getTokenValue(line, "alignment", column_info[c].alignment);
1584 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1585 getTokenValue(line, "valignment", column_info[c].valignment);
1586 getTokenValue(line, "width", column_info[c].p_width);
1587 getTokenValue(line, "special", column_info[c].align_special);
1590 for (row_type i = 0; i < nrows(); ++i) {
1591 l_getline(is, line);
1592 if (!prefixIs(line, "<row")) {
1593 lyxerr << "Wrong tabular format (expected <row ...> got"
1594 << line << ')' << endl;
1597 getTokenValue(line, "topspace", row_info[i].top_space,
1598 row_info[i].top_space_default);
1599 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1600 row_info[i].bottom_space_default);
1601 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1602 row_info[i].interline_space_default);
1603 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1604 getTokenValue(line, "endhead", row_info[i].endhead);
1605 getTokenValue(line, "endfoot", row_info[i].endfoot);
1606 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1607 getTokenValue(line, "newpage", row_info[i].newpage);
1608 getTokenValue(line, "caption", row_info[i].caption);
1609 for (col_type j = 0; j < ncols(); ++j) {
1610 l_getline(is, line);
1611 if (!prefixIs(line, "<cell")) {
1612 lyxerr << "Wrong tabular format (expected <cell ...> got"
1613 << line << ')' << endl;
1616 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1617 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1618 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1619 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1620 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1621 getTokenValue(line, "topline", cell_info[i][j].top_line);
1622 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1623 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1624 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1625 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1626 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1627 getTokenValue(line, "width", cell_info[i][j].p_width);
1629 getTokenValue(line, "special", cell_info[i][j].align_special);
1630 l_getline(is, line);
1631 if (prefixIs(line, "\\begin_inset")) {
1632 cell_info[i][j].inset->setBuffer(*buffer_);
1633 cell_info[i][j].inset->read(lex);
1634 l_getline(is, line);
1636 if (!prefixIs(line, "</cell>")) {
1637 lyxerr << "Wrong tabular format (expected </cell> got"
1638 << line << ')' << endl;
1642 l_getline(is, line);
1643 if (!prefixIs(line, "</row>")) {
1644 lyxerr << "Wrong tabular format (expected </row> got"
1645 << line << ')' << endl;
1649 while (!prefixIs(line, "</lyxtabular>")) {
1650 l_getline(is, line);
1656 bool Tabular::isMultiColumn(idx_type cell) const
1658 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1659 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1663 bool Tabular::hasMultiColumn(col_type c) const
1665 for (row_type r = 0; r < nrows(); ++r) {
1666 if (isMultiColumn(cellIndex(r, c)))
1673 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1675 return cell_info[cellRow(cell)][cellColumn(cell)];
1679 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1680 bool const right_border)
1682 idx_type const col = cellColumn(cell);
1683 idx_type const row = cellRow(cell);
1684 for (idx_type i = 0; i < number; ++i)
1685 unsetMultiRow(cellIndex(row, col + i));
1687 // unsetting of multirow may have invalidated cell index
1688 cell = cellIndex(row, col);
1689 CellData & cs = cellInfo(cell);
1690 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1691 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1692 cs.alignment = column_info[col].alignment;
1693 setRightLine(cell, right_border);
1695 for (idx_type i = 1; i < number; ++i) {
1696 CellData & cs1 = cellInfo(cell + i);
1697 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1698 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1706 bool Tabular::isMultiRow(idx_type cell) const
1708 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1709 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1712 bool Tabular::hasMultiRow(row_type r) const
1714 for (col_type c = 0; c < ncols(); ++c) {
1715 if (isMultiRow(cellIndex(r, c)))
1721 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1722 bool const bottom_border,
1723 LyXAlignment const halign)
1725 idx_type const col = cellColumn(cell);
1726 idx_type const row = cellRow(cell);
1727 for (idx_type i = 0; i < number; ++i)
1728 unsetMultiColumn(cellIndex(row + i, col));
1730 // unsetting of multirow may have invalidated cell index
1731 cell = cellIndex(row, col);
1732 CellData & cs = cellInfo(cell);
1733 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1734 cs.valignment = LYX_VALIGN_MIDDLE;
1735 // the horizontal alignment of multirow cells can only
1736 // be changed for the whole table row,
1737 // support changing this only for the multirow cell can be done via
1739 if (getPWidth(cell).zero())
1740 cs.alignment = halign;
1742 cs.alignment = LYX_ALIGN_LEFT;
1744 // set the bottom line of the last selected cell
1745 setBottomLine(cell, bottom_border);
1747 for (idx_type i = 1; i < number; ++i) {
1748 CellData & cs1 = cell_info[row + i][col];
1749 cs1.multirow = CELL_PART_OF_MULTIROW;
1750 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1758 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1760 row_type const row = cellRow(cell);
1761 col_type const col = cellColumn(cell);
1763 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1770 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1772 col_type const column = cellColumn(cell);
1773 col_type row = cellRow(cell) + 1;
1774 while (row < nrows() && isPartOfMultiRow(row, column))
1777 return row - cellRow(cell);
1781 void Tabular::unsetMultiColumn(idx_type cell)
1783 if (!isMultiColumn(cell))
1786 row_type const row = cellRow(cell);
1787 col_type const col = cellColumn(cell);
1788 row_type const span = columnSpan(cell);
1789 for (col_type c = 0; c < span; ++c) {
1790 // in the table dialog the lines are set in every case
1791 // when unsetting a multicolumn this leads to an additional right
1792 // line for every cell that was part of the former multicolumn cell,
1793 // except if the cell is in the last column
1794 // therefore remove this line
1795 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1796 && (col + c) < (col + span - 1))
1797 cell_info[row][col + c].right_line = false;
1798 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1804 void Tabular::unsetMultiRow(idx_type cell)
1806 if (!isMultiRow(cell))
1809 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1810 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1811 row_type const row = cellRow(cell);
1812 col_type const col = cellColumn(cell);
1813 row_type const span = rowSpan(cell);
1814 for (row_type r = 0; r < span; ++r)
1815 cell_info[row + r][col].multirow = CELL_NORMAL;
1820 void Tabular::setRotateCell(idx_type cell, int value)
1822 cellInfo(cell).rotate = value;
1826 int Tabular::getRotateCell(idx_type cell) const
1828 return cellInfo(cell).rotate;
1832 bool Tabular::needRotating() const
1836 for (row_type r = 0; r < nrows(); ++r)
1837 for (col_type c = 0; c < ncols(); ++c)
1838 if (cell_info[r][c].rotate != 0)
1844 bool Tabular::isLastCell(idx_type cell) const
1846 if (cell + 1 < numberofcells)
1852 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1854 if (cellRow(cell) == 0)
1857 col_type const col = cellColumn(cell);
1858 row_type r = cellRow(cell) - 1;
1859 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1862 return cell_info[r][col].cellno;
1866 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1868 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1869 if (nextrow < nrows())
1870 return cell_info[nextrow][cellColumn(cell)].cellno;
1875 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1877 LASSERT(column != npos && column < ncols(), column = 0);
1878 LASSERT(row != npos && row < nrows(), row = 0);
1879 return cell_info[row][column].cellno;
1883 void Tabular::setUsebox(idx_type cell, BoxType type)
1885 cellInfo(cell).usebox = type;
1889 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1890 // adds line breaks, see bug 4886.
1891 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1893 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1894 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1896 if (cellInfo(cell).usebox > 1)
1897 return cellInfo(cell).usebox;
1898 return useParbox(cell);
1903 // This are functions used for the longtable support
1905 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1911 row_info[row].endfirsthead = flag;
1915 row_info[row].endhead = flag;
1920 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1923 hd.set = haveLTHead();
1924 return row_info[row].endhead;
1928 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1931 hd.set = haveLTFirstHead();
1932 return row_info[row].endfirsthead;
1936 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1942 row_info[row].endlastfoot = flag;
1946 row_info[row].endfoot = flag;
1951 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1954 fd.set = haveLTFoot();
1955 return row_info[row].endfoot;
1959 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1962 fd.set = haveLTLastFoot();
1963 return row_info[row].endlastfoot;
1967 void Tabular::setLTNewPage(row_type row, bool what)
1969 row_info[row].newpage = what;
1973 bool Tabular::getLTNewPage(row_type row) const
1975 return row_info[row].newpage;
1979 bool Tabular::haveLTHead(bool withcaptions) const
1981 if (!is_long_tabular)
1983 for (row_type i = 0; i < nrows(); ++i)
1984 if (row_info[i].endhead &&
1985 (withcaptions || !row_info[i].caption))
1991 bool Tabular::haveLTFirstHead(bool withcaptions) const
1993 if (!is_long_tabular || endfirsthead.empty)
1995 for (row_type r = 0; r < nrows(); ++r)
1996 if (row_info[r].endfirsthead &&
1997 (withcaptions || !row_info[r].caption))
2003 bool Tabular::haveLTFoot(bool withcaptions) const
2005 if (!is_long_tabular)
2007 for (row_type r = 0; r < nrows(); ++r)
2008 if (row_info[r].endfoot &&
2009 (withcaptions || !row_info[r].caption))
2015 bool Tabular::haveLTLastFoot(bool withcaptions) const
2017 if (!is_long_tabular || endlastfoot.empty)
2019 for (row_type r = 0; r < nrows(); ++r)
2020 if (row_info[r].endlastfoot &&
2021 (withcaptions || !row_info[r].caption))
2027 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2029 idx_type i = getFirstCellInRow(row);
2031 setMultiColumn(i, numberOfCellsInRow(row), false);
2032 setTopLine(i, false);
2033 setBottomLine(i, false);
2034 setLeftLine(i, false);
2035 setRightLine(i, false);
2036 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2037 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2038 setLTHead(row, true, endfirsthead, true);
2039 row_info[row].endfirsthead = true;
2042 unsetMultiColumn(i);
2043 // When unsetting a caption row, also all existing
2044 // captions in this row must be dissolved.
2046 row_info[row].caption = what;
2051 bool Tabular::ltCaption(row_type row) const
2053 return row_info[row].caption;
2057 bool Tabular::haveLTCaption(CaptionType captiontype) const
2059 if (!is_long_tabular)
2061 for (row_type r = 0; r < nrows(); ++r) {
2062 if (row_info[r].caption) {
2063 switch (captiontype) {
2064 case CAPTION_FIRSTHEAD:
2065 if (row_info[r].endfirsthead)
2069 if (row_info[r].endhead)
2073 if (row_info[r].endfoot)
2076 case CAPTION_LASTFOOT:
2077 if (row_info[r].endlastfoot)
2089 // end longtable support functions
2091 void Tabular::setRowAscent(row_type row, int height)
2093 if (row >= nrows() || row_info[row].ascent == height)
2095 row_info[row].ascent = height;
2099 void Tabular::setRowDescent(row_type row, int height)
2101 if (row >= nrows() || row_info[row].descent == height)
2103 row_info[row].descent = height;
2107 int Tabular::rowAscent(row_type row) const
2109 LASSERT(row < nrows(), row = 0);
2110 return row_info[row].ascent;
2114 int Tabular::rowDescent(row_type row) const
2116 LASSERT(row < nrows(), row = 0);
2117 return row_info[row].descent;
2121 int Tabular::height() const
2124 for (row_type row = 0; row < nrows(); ++row)
2125 height += rowAscent(row) + rowDescent(row) +
2131 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2133 LASSERT(row < nrows(), return false);
2134 LASSERT(column < ncols(), return false);
2135 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2139 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2141 LASSERT(row < nrows(), return false);
2142 LASSERT(column < ncols(), return false);
2143 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2147 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2149 // we only output complete row lines and the 1st row here, the rest
2150 // is done in Tabular::TeXBottomHLine(...)
2152 // get for each column the topline (if any)
2153 vector<bool> topline;
2155 for (col_type c = 0; c < ncols(); ++c) {
2156 topline.push_back(topLine(cellIndex(row, c)));
2157 // If cell is part of a multirow and not the first cell of the
2158 // multirow, no line must be drawn.
2160 if (isMultiRow(cellIndex(row, c))
2161 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2167 // do nothing if empty first row, or incomplete row line after
2168 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2171 // only output complete row lines and the 1st row's clines
2172 if (nset == ncols()) {
2174 os << (row == 0 ? "\\toprule " : "\\midrule ");
2178 } else if (row == 0) {
2179 for (col_type c = 0; c < ncols(); ++c) {
2181 col_type offset = 0;
2182 for (col_type j = 0 ; j < c; ++j)
2183 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2186 //babel makes the "-" character an active one, so we have to suppress this here
2187 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2188 if (lang == "slovak" || lang == "czech")
2189 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2190 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2192 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2194 col_type cstart = c;
2195 for ( ; c < ncols() && topline[c]; ++c) {}
2197 for (col_type j = cstart ; j < c ; ++j)
2198 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2201 os << c + offset << "} ";
2209 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2211 // we output bottomlines of row r and the toplines of row r+1
2212 // if the latter do not span the whole tabular
2214 // get the bottomlines of row r, and toplines in next row
2215 bool lastrow = row == nrows() - 1;
2216 vector<bool> bottomline, topline;
2217 bool nextrowset = true;
2218 for (col_type c = 0; c < ncols(); ++c) {
2219 bottomline.push_back(bottomLine(cellIndex(row, c)));
2220 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2221 // If cell is part of a multirow and not the last cell of the
2222 // multirow, no line must be drawn.
2224 if (isMultiRow(cellIndex(row, c))
2225 && isMultiRow(cellIndex(row + 1, c))
2226 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2227 bottomline[c] = false;
2230 nextrowset &= topline[c];
2233 // combine this row's bottom lines and next row's toplines if necessary
2235 for (col_type c = 0; c < ncols(); ++c) {
2237 bottomline[c] = bottomline[c] || topline[c];
2242 // do nothing if empty, OR incomplete row line with a topline in next row
2243 if (nset == 0 || (nextrowset && nset != ncols()))
2246 if (nset == ncols()) {
2248 os << (lastrow ? "\\bottomrule" : "\\midrule");
2252 for (col_type c = 0; c < ncols(); ++c) {
2253 if (bottomline[c]) {
2254 col_type offset = 0;
2255 for (col_type j = 0 ; j < c; ++j)
2256 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2259 //babel makes the "-" character an active one, so we have to suppress this here
2260 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2261 if (lang == "slovak" || lang == "czech")
2262 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2263 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2265 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2267 col_type cstart = c;
2268 for ( ; c < ncols() && bottomline[c]; ++c) {}
2270 for (col_type j = cstart ; j < c ; ++j)
2271 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2274 os << c + offset << "} ";
2282 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2283 bool & ismulticol, bool & ismultirow) const
2285 row_type const r = cellRow(cell);
2286 if (is_long_tabular && row_info[r].caption)
2289 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2290 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2291 // figure out how to set the lines
2292 // we always set double lines to the right of the cell
2293 col_type const c = cellColumn(cell);
2294 col_type const nextcol = c + columnSpan(cell);
2295 bool colright = columnRightLine(c);
2296 bool colleft = columnLeftLine(c);
2297 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2298 bool nextcellleft = nextcol < ncols()
2299 && leftLine(cellIndex(r, nextcol));
2300 bool coldouble = colright && nextcolleft;
2301 bool celldouble = rightLine(cell) && nextcellleft;
2303 ismulticol = isMultiColumn(cell)
2304 || (c == 0 && colleft != leftLine(cell))
2305 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2306 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2307 || (coldouble != celldouble);
2309 // we center in multicol when no decimal point
2310 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2311 docstring const align_d = column_info[c].decimal_point;
2312 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2316 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2317 int latexcolspan = columnSpan(cell);
2318 for(col_type col = c; col < c + columnSpan(cell); ++col)
2319 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2323 os << "\\multicolumn{" << latexcolspan << "}{";
2324 if (c ==0 && leftLine(cell))
2326 if (!cellInfo(cell).align_special.empty()) {
2327 os << cellInfo(cell).align_special;
2329 if (!getPWidth(cell).zero()) {
2331 case LYX_ALIGN_LEFT:
2332 os << ">{\\raggedright}";
2334 case LYX_ALIGN_RIGHT:
2335 os << ">{\\raggedleft}";
2337 case LYX_ALIGN_CENTER:
2338 os << ">{\\centering}";
2344 case LYX_VALIGN_TOP:
2347 case LYX_VALIGN_MIDDLE:
2350 case LYX_VALIGN_BOTTOM:
2355 << from_ascii(getPWidth(cell).asLatexString())
2359 case LYX_ALIGN_LEFT:
2362 case LYX_ALIGN_RIGHT:
2369 } // end if else !getPWidth
2370 } // end if else !cellinfo_of_cell
2371 if (rightLine(cell) || nextcellleft)
2374 // add extra vertical line if we want a double one
2377 } // end if ismulticol
2379 // we only need code for the first multirow cell
2380 ismultirow = isMultiRow(cell);
2382 os << "\\multirow{" << rowSpan(cell) << "}{";
2383 if (!getPWidth(cell).zero())
2384 os << from_ascii(getPWidth(cell).asLatexString());
2386 // we need to set a default value
2389 if (!getMROffset(cell).zero())
2390 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2392 } // end if ismultirow
2394 if (getRotateCell(cell) != 0)
2395 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2397 if (getUsebox(cell) == BOX_PARBOX) {
2400 case LYX_VALIGN_TOP:
2403 case LYX_VALIGN_MIDDLE:
2406 case LYX_VALIGN_BOTTOM:
2410 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2412 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2413 os << "\\begin{minipage}[";
2415 case LYX_VALIGN_TOP:
2418 case LYX_VALIGN_MIDDLE:
2421 case LYX_VALIGN_BOTTOM:
2425 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2431 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2432 bool ismulticol, bool ismultirow) const
2434 row_type const r = cellRow(cell);
2435 if (is_long_tabular && row_info[r].caption)
2439 if (getUsebox(cell) == BOX_PARBOX)
2441 else if (getUsebox(cell) == BOX_MINIPAGE)
2442 os << breakln << "\\end{minipage}";
2443 if (getRotateCell(cell) != 0)
2444 os << breakln << "\\end{turn}";
2452 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2453 OutputParams const & runparams) const
2455 if (!is_long_tabular)
2459 // output caption which is in no header or footer
2460 if (haveLTCaption()) {
2461 for (row_type r = 0; r < nrows(); ++r) {
2462 if (row_info[r].caption &&
2463 !row_info[r].endfirsthead && !row_info[r].endhead &&
2464 !row_info[r].endfoot && !row_info[r].endlastfoot)
2465 TeXRow(os, r, runparams);
2468 // output first header info
2469 if (haveLTFirstHead()) {
2470 if (endfirsthead.topDL)
2472 for (row_type r = 0; r < nrows(); ++r) {
2473 if (row_info[r].endfirsthead)
2474 TeXRow(os, r, runparams);
2476 if (endfirsthead.bottomDL)
2478 os << "\\endfirsthead\n";
2480 // output header info
2482 if (endfirsthead.empty && !haveLTFirstHead())
2483 os << "\\endfirsthead\n";
2486 for (row_type r = 0; r < nrows(); ++r) {
2487 if (row_info[r].endhead)
2488 TeXRow(os, r, runparams);
2490 if (endhead.bottomDL)
2492 os << "\\endhead\n";
2494 // output footer info
2498 for (row_type r = 0; r < nrows(); ++r) {
2499 if (row_info[r].endfoot)
2500 TeXRow(os, r, runparams);
2502 if (endfoot.bottomDL)
2504 os << "\\endfoot\n";
2505 if (endlastfoot.empty && !haveLTLastFoot())
2506 os << "\\endlastfoot\n";
2508 // output lastfooter info
2509 if (haveLTLastFoot()) {
2510 if (endlastfoot.topDL)
2512 for (row_type r = 0; r < nrows(); ++r) {
2513 if (row_info[r].endlastfoot)
2514 TeXRow(os, r, runparams);
2516 if (endlastfoot.bottomDL)
2518 os << "\\endlastfoot\n";
2523 bool Tabular::isValidRow(row_type row) const
2525 if (!is_long_tabular)
2527 return !row_info[row].endhead && !row_info[row].endfirsthead
2528 && !row_info[row].endfoot && !row_info[row].endlastfoot
2529 && !row_info[row].caption;
2533 void Tabular::TeXRow(otexstream & os, row_type row,
2534 OutputParams const & runparams) const
2536 idx_type cell = cellIndex(row, 0);
2537 shared_ptr<InsetTableCell> inset = cellInset(cell);
2538 Paragraph const & par = inset->paragraphs().front();
2539 string const lang = par.getParLanguage(buffer().params())->lang();
2541 //output the top line
2542 TeXTopHLine(os, row, lang);
2544 if (row_info[row].top_space_default) {
2546 os << "\\addlinespace\n";
2548 os << "\\noalign{\\vskip\\doublerulesep}\n";
2549 } else if(!row_info[row].top_space.zero()) {
2551 os << "\\addlinespace["
2552 << from_ascii(row_info[row].top_space.asLatexString())
2555 os << "\\noalign{\\vskip"
2556 << from_ascii(row_info[row].top_space.asLatexString())
2560 bool ismulticol = false;
2561 bool ismultirow = false;
2562 for (col_type c = 0; c < ncols(); ++c) {
2563 if (isPartOfMultiColumn(row, c))
2566 cell = cellIndex(row, c);
2568 if (isPartOfMultiRow(row, c)
2569 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2570 if (cell != getLastCellInRow(row))
2575 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2576 shared_ptr<InsetTableCell> inset = cellInset(cell);
2578 Paragraph const & par = inset->paragraphs().front();
2579 bool rtl = par.isRTL(buffer().params())
2581 && getPWidth(cell).zero()
2582 && !runparams.use_polyglossia;
2586 par.getParLanguage(buffer().params())->lang();
2587 if (lang == "farsi")
2589 else if (lang == "arabic_arabi")
2591 // currently, remaining RTL languages are
2592 // arabic_arabtex and hebrew
2596 // pass to the OutputParams that we are in a cell and
2597 // which alignment we have set.
2598 // InsetNewline needs this context information.
2599 OutputParams newrp(runparams);
2600 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2601 ? OutputParams::PLAIN
2602 : OutputParams::ALIGNED;
2604 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2605 // copy cell and split in 2
2606 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2607 head.setBuffer(buffer());
2608 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2610 dit.push_back(CursorSlice(head));
2611 head.setMacrocontextPositionRecursive(dit);
2612 bool hassep = false;
2613 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2614 head.latex(os, newrp);
2617 tail.setBuffer(head.buffer());
2619 dit.push_back(CursorSlice(tail));
2620 tail.setMacrocontextPositionRecursive(dit);
2621 tail.latex(os, newrp);
2623 } else if (!isPartOfMultiRow(row, c)) {
2624 if (!runparams.nice)
2625 os.texrow().start(par.id(), 0);
2626 inset->latex(os, newrp);
2629 runparams.encoding = newrp.encoding;
2633 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2634 if (cell != getLastCellInRow(row)) { // not last cell in row
2641 os << "\\tabularnewline";
2642 if (row_info[row].bottom_space_default) {
2644 os << "\\addlinespace";
2646 os << "[\\doublerulesep]";
2647 } else if (!row_info[row].bottom_space.zero()) {
2649 os << "\\addlinespace";
2651 << from_ascii(row_info[row].bottom_space.asLatexString())
2656 //output the bottom line
2657 TeXBottomHLine(os, row, lang);
2659 if (row_info[row].interline_space_default) {
2661 os << "\\addlinespace\n";
2663 os << "\\noalign{\\vskip\\doublerulesep}\n";
2664 } else if (!row_info[row].interline_space.zero()) {
2666 os << "\\addlinespace["
2667 << from_ascii(row_info[row].interline_space.asLatexString())
2670 os << "\\noalign{\\vskip"
2671 << from_ascii(row_info[row].interline_space.asLatexString())
2677 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2679 bool const is_tabular_star = !tabular_width.zero();
2681 //+---------------------------------------------------------------------
2682 //+ first the opening preamble +
2683 //+---------------------------------------------------------------------
2686 if (runparams.lastid != -1)
2687 os.texrow().start(runparams.lastid, runparams.lastpos);
2690 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2692 if (is_long_tabular) {
2693 os << "\\begin{longtable}";
2694 switch (longtabular_alignment) {
2695 case LYX_LONGTABULAR_ALIGN_LEFT:
2698 case LYX_LONGTABULAR_ALIGN_CENTER:
2700 case LYX_LONGTABULAR_ALIGN_RIGHT:
2705 if (is_tabular_star)
2706 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2708 os << "\\begin{tabular}";
2709 switch (tabular_valignment) {
2710 case LYX_VALIGN_TOP:
2713 case LYX_VALIGN_MIDDLE:
2715 case LYX_VALIGN_BOTTOM:
2723 if (is_tabular_star)
2724 os << "@{\\extracolsep{\\fill}}";
2726 for (col_type c = 0; c < ncols(); ++c) {
2727 if (columnLeftLine(c))
2729 if (!column_info[c].align_special.empty()) {
2730 os << column_info[c].align_special;
2732 if (!column_info[c].p_width.zero()) {
2733 switch (column_info[c].alignment) {
2734 case LYX_ALIGN_LEFT:
2735 os << ">{\\raggedright}";
2737 case LYX_ALIGN_RIGHT:
2738 os << ">{\\raggedleft}";
2740 case LYX_ALIGN_CENTER:
2741 os << ">{\\centering}";
2743 case LYX_ALIGN_NONE:
2744 case LYX_ALIGN_BLOCK:
2745 case LYX_ALIGN_LAYOUT:
2746 case LYX_ALIGN_SPECIAL:
2747 case LYX_ALIGN_DECIMAL:
2751 switch (column_info[c].valignment) {
2752 case LYX_VALIGN_TOP:
2755 case LYX_VALIGN_MIDDLE:
2758 case LYX_VALIGN_BOTTOM:
2763 << from_ascii(column_info[c].p_width.asLatexString())
2766 switch (column_info[c].alignment) {
2767 case LYX_ALIGN_LEFT:
2770 case LYX_ALIGN_RIGHT:
2773 case LYX_ALIGN_DECIMAL:
2774 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2780 } // end if else !column_info[i].p_width
2781 } // end if else !column_info[i].align_special
2782 if (columnRightLine(c))
2787 TeXLongtableHeaderFooter(os, runparams);
2789 //+---------------------------------------------------------------------
2790 //+ the single row and columns (cells) +
2791 //+---------------------------------------------------------------------
2793 for (row_type r = 0; r < nrows(); ++r) {
2794 if (isValidRow(r)) {
2795 TeXRow(os, r, runparams);
2796 if (is_long_tabular && row_info[r].newpage)
2797 os << "\\newpage\n";
2801 //+---------------------------------------------------------------------
2802 //+ the closing of the tabular +
2803 //+---------------------------------------------------------------------
2805 if (is_long_tabular)
2806 os << "\\end{longtable}";
2808 if (is_tabular_star)
2809 os << "\\end{tabular*}";
2811 os << "\\end{tabular}";
2815 os << breakln << "\\end{turn}";
2819 int Tabular::docbookRow(odocstream & os, row_type row,
2820 OutputParams const & runparams) const
2823 idx_type cell = getFirstCellInRow(row);
2826 for (col_type c = 0; c < ncols(); ++c) {
2827 if (isPartOfMultiColumn(row, c))
2830 os << "<entry align=\"";
2831 switch (getAlignment(cell)) {
2832 case LYX_ALIGN_LEFT:
2835 case LYX_ALIGN_RIGHT:
2843 os << "\" valign=\"";
2844 switch (getVAlignment(cell)) {
2845 case LYX_VALIGN_TOP:
2848 case LYX_VALIGN_BOTTOM:
2851 case LYX_VALIGN_MIDDLE:
2856 if (isMultiColumn(cell)) {
2857 os << " namest=\"col" << c << "\" ";
2858 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2862 ret += cellInset(cell)->docbook(os, runparams);
2871 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2875 //+---------------------------------------------------------------------
2876 //+ first the opening preamble +
2877 //+---------------------------------------------------------------------
2879 os << "<tgroup cols=\"" << ncols()
2880 << "\" colsep=\"1\" rowsep=\"1\">\n";
2882 for (col_type c = 0; c < ncols(); ++c) {
2883 os << "<colspec colname=\"col" << c << "\" align=\"";
2884 switch (column_info[c].alignment) {
2885 case LYX_ALIGN_LEFT:
2888 case LYX_ALIGN_RIGHT:
2896 if (runparams.flavor == OutputParams::XML)
2902 //+---------------------------------------------------------------------
2903 //+ Long Tabular case +
2904 //+---------------------------------------------------------------------
2906 // output caption info
2907 // The caption flag wins over head/foot
2908 if (haveLTCaption()) {
2909 os << "<caption>\n";
2911 for (row_type r = 0; r < nrows(); ++r) {
2912 if (row_info[r].caption) {
2913 ret += docbookRow(os, r, runparams);
2916 os << "</caption>\n";
2919 // output header info
2920 if (haveLTHead(false) || haveLTFirstHead(false)) {
2923 for (row_type r = 0; r < nrows(); ++r) {
2924 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2925 !row_info[r].caption) {
2926 ret += docbookRow(os, r, runparams);
2932 // output footer info
2933 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2936 for (row_type r = 0; r < nrows(); ++r) {
2937 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2938 !row_info[r].caption) {
2939 ret += docbookRow(os, r, runparams);
2946 //+---------------------------------------------------------------------
2947 //+ the single row and columns (cells) +
2948 //+---------------------------------------------------------------------
2952 for (row_type r = 0; r < nrows(); ++r) {
2953 if (isValidRow(r)) {
2954 ret += docbookRow(os, r, runparams);
2959 //+---------------------------------------------------------------------
2960 //+ the closing of the tabular +
2961 //+---------------------------------------------------------------------
2970 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2971 OutputParams const & runparams, bool header) const
2974 string const celltag = header ? "th" : "td";
2975 idx_type cell = getFirstCellInRow(row);
2977 xs << html::StartTag("tr");
2978 for (col_type c = 0; c < ncols(); ++c) {
2979 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2984 switch (getAlignment(cell)) {
2985 case LYX_ALIGN_LEFT:
2988 case LYX_ALIGN_RIGHT:
2996 attr << " valign='";
2997 switch (getVAlignment(cell)) {
2998 case LYX_VALIGN_TOP:
3001 case LYX_VALIGN_BOTTOM:
3004 case LYX_VALIGN_MIDDLE:
3009 if (isMultiColumn(cell))
3010 attr << " colspan='" << columnSpan(cell) << "'";
3011 else if (isMultiRow(cell))
3012 attr << " rowspan='" << rowSpan(cell) << "'";
3014 xs << html::StartTag(celltag, attr.str()) << html::CR();
3015 ret += cellInset(cell)->xhtml(xs, runparams);
3016 xs << html::EndTag(celltag) << html::CR();
3019 xs << html::EndTag("tr");
3024 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3028 if (is_long_tabular) {
3029 // we'll wrap it in a div, so as to deal with alignment
3031 switch (longtabular_alignment) {
3032 case LYX_LONGTABULAR_ALIGN_LEFT:
3035 case LYX_LONGTABULAR_ALIGN_CENTER:
3038 case LYX_LONGTABULAR_ALIGN_RIGHT:
3042 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3044 // The caption flag wins over head/foot
3045 if (haveLTCaption()) {
3046 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3048 for (row_type r = 0; r < nrows(); ++r)
3049 if (row_info[r].caption)
3050 ret += xhtmlRow(xs, r, runparams);
3051 xs << html::EndTag("div") << html::CR();
3055 xs << html::StartTag("table") << html::CR();
3057 // output header info
3058 bool const havefirsthead = haveLTFirstHead(false);
3059 // if we have a first head, then we are going to ignore the
3060 // headers for the additional pages, since there aren't any
3061 // in XHTML. this test accomplishes that.
3062 bool const havehead = !havefirsthead && haveLTHead(false);
3063 if (havehead || havefirsthead) {
3064 xs << html::StartTag("thead") << html::CR();
3065 for (row_type r = 0; r < nrows(); ++r) {
3066 if (((havefirsthead && row_info[r].endfirsthead) ||
3067 (havehead && row_info[r].endhead)) &&
3068 !row_info[r].caption) {
3069 ret += xhtmlRow(xs, r, runparams, true);
3072 xs << html::EndTag("thead") << html::CR();
3074 // output footer info
3075 bool const havelastfoot = haveLTLastFoot(false);
3077 bool const havefoot = !havelastfoot && haveLTFoot(false);
3078 if (havefoot || havelastfoot) {
3079 xs << html::StartTag("tfoot") << html::CR();
3080 for (row_type r = 0; r < nrows(); ++r) {
3081 if (((havelastfoot && row_info[r].endlastfoot) ||
3082 (havefoot && row_info[r].endfoot)) &&
3083 !row_info[r].caption) {
3084 ret += xhtmlRow(xs, r, runparams);
3087 xs << html::EndTag("tfoot") << html::CR();
3090 xs << html::StartTag("tbody") << html::CR();
3091 for (row_type r = 0; r < nrows(); ++r) {
3092 if (isValidRow(r)) {
3093 ret += xhtmlRow(xs, r, runparams);
3096 xs << html::EndTag("tbody")
3098 << html::EndTag("table")
3100 if (is_long_tabular)
3101 xs << html::EndTag("div") << html::CR();
3106 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3107 vector<unsigned int> const & clen) const
3109 idx_type const fcell = getFirstCellInRow(row);
3110 idx_type const n = numberOfCellsInRow(row) + fcell;
3113 for (idx_type i = fcell; i < n; ++i) {
3123 for (idx_type i = fcell; i < n; ++i) {
3134 col_type column = cellColumn(i);
3135 int len = clen[column];
3136 while (column < ncols() - 1
3137 && isPartOfMultiColumn(row, ++column))
3138 len += clen[column] + 4;
3139 os << docstring(len, ch);
3154 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3155 vector<unsigned int> const & clen) const
3157 idx_type const fcell = getFirstCellInRow(row);
3158 idx_type const n = numberOfCellsInRow(row) + fcell;
3161 for (idx_type i = fcell; i < n; ++i) {
3162 if (bottomLine(i)) {
3171 for (idx_type i = fcell; i < n; ++i) {
3172 if (bottomLine(i)) {
3182 col_type column = cellColumn(i);
3183 int len = clen[column];
3184 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3185 len += clen[column] + 4;
3186 os << docstring(len, ch);
3187 if (bottomLine(i)) {
3201 void Tabular::plaintextPrintCell(odocstringstream & os,
3202 OutputParams const & runparams,
3203 idx_type cell, row_type row, col_type column,
3204 vector<unsigned int> const & clen,
3205 bool onlydata, size_t max_length) const
3207 odocstringstream sstr;
3208 cellInset(cell)->plaintext(sstr, runparams, max_length);
3220 unsigned int len1 = sstr.str().length();
3221 unsigned int len2 = clen[column];
3222 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3223 len2 += clen[column] + 4;
3226 switch (getAlignment(cell)) {
3228 case LYX_ALIGN_LEFT:
3231 case LYX_ALIGN_RIGHT:
3235 case LYX_ALIGN_CENTER:
3241 os << docstring(len1, ' ') << sstr.str()
3242 << docstring(len2, ' ');
3244 if (rightLine(cell))
3251 void Tabular::plaintext(odocstringstream & os,
3252 OutputParams const & runparams, int const depth,
3253 bool onlydata, char_type delim, size_t max_length) const
3255 // first calculate the width of the single columns
3256 vector<unsigned int> clen(ncols());
3259 // first all non multicolumn cells!
3260 for (col_type c = 0; c < ncols(); ++c) {
3262 for (row_type r = 0; r < nrows(); ++r) {
3263 idx_type cell = cellIndex(r, c);
3264 if (isMultiColumn(cell))
3266 odocstringstream sstr;
3267 cellInset(cell)->plaintext(sstr, runparams, max_length);
3268 if (clen[c] < sstr.str().length())
3269 clen[c] = sstr.str().length();
3272 // then all multicolumn cells!
3273 for (col_type c = 0; c < ncols(); ++c) {
3274 for (row_type r = 0; r < nrows(); ++r) {
3275 idx_type cell = cellIndex(r, c);
3276 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3278 odocstringstream sstr;
3279 cellInset(cell)->plaintext(sstr, runparams, max_length);
3280 int len = int(sstr.str().length());
3281 idx_type const n = columnSpan(cell);
3282 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3284 if (len > int(clen[c + n - 1]))
3285 clen[c + n - 1] = len;
3290 for (row_type r = 0; r < nrows(); ++r) {
3291 if (!onlydata && plaintextTopHLine(os, r, clen))
3292 os << docstring(depth * 2, ' ');
3293 for (col_type c = 0; c < ncols(); ++c) {
3294 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3296 if (onlydata && c > 0)
3297 // we don't use operator<< for single UCS4 character.
3298 // see explanation in docstream.h
3300 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3302 if (os.str().size() > max_length)
3307 os << docstring(depth * 2, ' ');
3308 if (plaintextBottomHLine(os, r, clen))
3309 os << docstring(depth * 2, ' ');
3311 if (os.str().size() > max_length)
3317 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3319 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3323 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3324 col_type column) const
3326 return cell_info[row][column].inset;
3330 void Tabular::setCellInset(row_type row, col_type column,
3331 shared_ptr<InsetTableCell> ins) const
3333 CellData & cd = cell_info[row][column];
3338 void Tabular::validate(LaTeXFeatures & features) const
3340 features.require("NeedTabularnewline");
3342 features.require("booktabs");
3343 if (is_long_tabular)
3344 features.require("longtable");
3346 features.require("rotating");
3347 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3348 if (isMultiRow(cell))
3349 features.require("multirow");
3350 if (getVAlignment(cell) != LYX_VALIGN_TOP
3351 || !getPWidth(cell).zero())
3352 features.require("array");
3353 cellInset(cell)->validate(features);
3358 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3360 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3361 ParagraphList::const_iterator cit = parlist.begin();
3362 ParagraphList::const_iterator end = parlist.end();
3364 for (; cit != end; ++cit)
3365 for (int i = 0; i < cit->size(); ++i)
3366 if (cit->isNewline(i))
3373 /////////////////////////////////////////////////////////////////////
3377 /////////////////////////////////////////////////////////////////////
3379 InsetTableCell::InsetTableCell(Buffer * buf)
3380 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3381 contentAlign(LYX_ALIGN_CENTER)
3385 bool InsetTableCell::forcePlainLayout(idx_type) const
3387 return !isFixedWidth;
3391 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3393 return isFixedWidth;
3397 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3398 FuncStatus & status) const
3400 bool enabled = true;
3401 switch (cmd.action()) {
3403 enabled = !forcePlainLayout();
3405 case LFUN_LAYOUT_PARAGRAPH:
3406 enabled = allowParagraphCustomization();
3409 case LFUN_MATH_DISPLAY:
3410 if (!hasFixedWidth()) {
3415 return InsetText::getStatus(cur, cmd, status);
3417 status.setEnabled(enabled);
3421 docstring InsetTableCell::asString(bool intoInsets)
3424 if (paragraphs().empty())
3426 ParagraphList::const_iterator it = paragraphs().begin();
3427 ParagraphList::const_iterator en = paragraphs().end();
3429 for (; it != en; ++it) {
3434 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3440 void InsetTableCell::addToToc(DocIterator const & di, bool output_active) const
3442 InsetText::iterateForToc(di, output_active);
3446 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3449 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3450 return InsetText::xhtml(xs, rp);
3455 /////////////////////////////////////////////////////////////////////
3459 /////////////////////////////////////////////////////////////////////
3461 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3463 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3464 rowselect_(false), colselect_(false)
3469 InsetTabular::InsetTabular(InsetTabular const & tab)
3470 : Inset(tab), tabular(tab.tabular), scx_(0)
3475 InsetTabular::~InsetTabular()
3477 hideDialogs("tabular", this);
3481 void InsetTabular::setBuffer(Buffer & buf)
3483 tabular.setBuffer(buf);
3484 Inset::setBuffer(buf);
3488 bool InsetTabular::insetAllowed(InsetCode code) const
3493 case MATHMACRO_CODE:
3498 return tabular.is_long_tabular;
3506 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3508 return tabular.is_long_tabular &&
3509 (newtype == "Standard" || newtype == "LongTableNoNumber");
3513 void InsetTabular::write(ostream & os) const
3515 os << "Tabular" << endl;
3520 string InsetTabular::contextMenu(BufferView const &, int, int) const
3522 // FIXME: depending on the selection state,
3523 // we could offer a different menu.
3524 return cell(0)->contextMenuName() + ";" + contextMenuName();
3528 string InsetTabular::contextMenuName() const
3530 return "context-tabular";
3534 void InsetTabular::read(Lexer & lex)
3536 //bool const old_format = (lex.getString() == "\\LyXTable");
3544 string token = lex.getString();
3545 while (lex && token != "\\end_inset") {
3547 token = lex.getString();
3550 lex.printError("Missing \\end_inset at this point. ");
3554 int InsetTabular::rowFromY(Cursor & cur, int y) const
3556 // top y coordinate of tabular
3557 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3559 for (; r < tabular.nrows() && y > h; ++r)
3560 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3561 + tabular.interRowSpace(r);
3567 int InsetTabular::columnFromX(Cursor & cur, int x) const
3569 // left x coordinate of tabular
3570 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3572 for (; c < tabular.ncols() && x > w; ++c)
3573 w += tabular.cellWidth(c);
3578 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3580 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3581 // mi.base.textwidth << "\n";
3582 LBUFERR(mi.base.bv);
3584 for (row_type r = 0; r < tabular.nrows(); ++r) {
3587 for (col_type c = 0; c < tabular.ncols(); ++c) {
3588 if (tabular.isPartOfMultiColumn(r, c)
3589 || tabular.isPartOfMultiRow(r, c))
3590 // multicolumn or multirow cell, but not first one
3592 idx_type const cell = tabular.cellIndex(r, c);
3595 Length const p_width = tabular.getPWidth(cell);
3596 if (!p_width.zero())
3597 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3598 tabular.cellInset(cell)->metrics(m, dim);
3599 if (!p_width.zero())
3600 dim.wid = m.base.textwidth;
3601 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3602 + tabular.interColumnSpace(cell);
3604 // FIXME(?): do we need a second metrics call?
3605 TextMetrics const & tm =
3606 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3608 // determine horizontal offset because of decimal align (if necessary)
3609 int decimal_width = 0;
3610 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3611 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3612 tail.setBuffer(tabular.buffer());
3613 // we need to set macrocontext position everywhere
3614 // otherwise we crash with nested insets (e.g. footnotes)
3615 // after decimal point
3616 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3618 dit.push_back(CursorSlice(tail));
3619 tail.setMacrocontextPositionRecursive(dit);
3621 // remove text leading decimal point
3622 docstring const align_d = tabular.column_info[c].decimal_point;
3623 dit = separatorPos(&tail, align_d);
3625 pit_type const psize = tail.paragraphs().front().size();
3627 tail.paragraphs().front().eraseChars(0,
3628 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3630 tail.metrics(m, dim1);
3631 decimal_width = dim1.width();
3634 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3635 tabular.cell_info[r][c].decimal_width = decimal_width;
3637 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3638 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3639 int const lastpardes = tm.last().second->descent()
3640 + TEXT_TO_INSET_OFFSET;
3642 switch (tabular.getVAlignment(cell)) {
3643 case Tabular::LYX_VALIGN_TOP:
3645 case Tabular::LYX_VALIGN_MIDDLE:
3646 offset = -(dim.des - lastpardes)/2;
3648 case Tabular::LYX_VALIGN_BOTTOM:
3649 offset = -(dim.des - lastpardes);
3652 tabular.cell_info[r][c].voffset = offset;
3653 maxasc = max(maxasc, dim.asc - offset);
3654 maxdes = max(maxdes, dim.des + offset);
3656 int const top_space = tabular.row_info[r].top_space_default ?
3657 default_line_space :
3658 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3659 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3660 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3661 default_line_space :
3662 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3663 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3666 // for top-alignment the first horizontal table line must be exactly at
3667 // the position of the base line of the surrounding text line
3668 // for bottom alignment, the same is for the last table line
3669 switch (tabular.tabular_valignment) {
3670 case Tabular::LYX_VALIGN_BOTTOM:
3671 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3673 case Tabular::LYX_VALIGN_MIDDLE:
3674 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3676 case Tabular::LYX_VALIGN_TOP:
3677 offset_valign_ = tabular.rowAscent(0);
3681 tabular.updateColumnWidths();
3682 dim.asc = tabular.rowAscent(0) - offset_valign_;
3683 dim.des = tabular.height() - dim.asc;
3684 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3688 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3691 if (&cur.inset() == this && cur.selection()) {
3692 if (cur.selIsMultiCell()) {
3695 getSelection(cur, rs, re, cs, ce);
3697 idx_type const cell = tabular.cellIndex(row, col);
3698 col_type const cspan = tabular.columnSpan(cell);
3699 row_type const rspan = tabular.rowSpan(cell);
3700 if (col + cspan - 1 >= cs && col <= ce
3701 && row + rspan - 1 >= rs && row <= re)
3704 if (col == tabular.cellColumn(cur.idx())
3705 && row == tabular.cellRow(cur.idx())) {
3706 CursorSlice const & beg = cur.selBegin();
3707 CursorSlice const & end = cur.selEnd();
3709 if ((end.lastpos() > 0 || end.lastpit() > 0)
3710 && end.pos() == end.lastpos() && beg.pos() == 0
3711 && end.pit() == end.lastpit() && beg.pit() == 0)
3719 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3721 x += scx_ + ADD_TO_TABULAR_WIDTH;
3723 BufferView * bv = pi.base.bv;
3724 Cursor & cur = pi.base.bv->cursor();
3727 // FIXME: As the full background is painted in drawBackground(),
3728 // we have no choice but to do a full repaint for the Text cells.
3729 pi.full_repaint = true;
3731 bool const original_selection_state = pi.selected;
3734 first_visible_cell = Tabular::npos;
3736 int yy = y + offset_valign_;
3737 for (row_type r = 0; r < tabular.nrows(); ++r) {
3739 for (col_type c = 0; c < tabular.ncols(); ++c) {
3740 if (tabular.isPartOfMultiColumn(r, c))
3743 idx = tabular.cellIndex(r, c);
3745 if (tabular.isPartOfMultiRow(r, c)) {
3746 nx += tabular.cellWidth(idx);
3750 if (first_visible_cell == Tabular::npos)
3751 first_visible_cell = idx;
3753 pi.selected |= isCellSelected(cur, r, c);
3754 int const cx = nx + tabular.textHOffset(idx);
3755 int const cy = yy + tabular.textVOffset(idx);
3756 // Cache the Inset position.
3757 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3758 cell(idx)->draw(pi, cx, cy);
3759 drawCellLines(pi, nx, yy, r, idx);
3760 nx += tabular.cellWidth(idx);
3761 pi.selected = original_selection_state;
3764 if (r + 1 < tabular.nrows())
3765 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3766 + tabular.interRowSpace(r + 1);
3771 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3773 x += scx_ + ADD_TO_TABULAR_WIDTH;
3774 y += offset_valign_ - tabular.rowAscent(0);
3775 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3776 pi.backgroundColor(this));
3780 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3782 Cursor & cur = pi.base.bv->cursor();
3785 x += scx_ + ADD_TO_TABULAR_WIDTH;
3787 if (!cur.selection())
3789 if (&cur.inset() != this)
3794 bool const full_cell_selected = isCellSelected(cur,
3795 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3797 if (cur.selIsMultiCell() || full_cell_selected) {
3798 for (row_type r = 0; r < tabular.nrows(); ++r) {
3800 for (col_type c = 0; c < tabular.ncols(); ++c) {
3801 if (tabular.isPartOfMultiColumn(r, c))
3804 idx_type const cell = tabular.cellIndex(r, c);
3806 if (tabular.isPartOfMultiRow(r, c)) {
3807 xx += tabular.cellWidth(cell);
3810 int const w = tabular.cellWidth(cell);
3811 int const h = tabular.cellHeight(cell);
3812 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3813 if (isCellSelected(cur, r, c))
3814 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3817 if (r + 1 < tabular.nrows())
3818 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3819 + tabular.interRowSpace(r + 1);
3823 // FIXME: This code has no effect because InsetTableCell does not handle
3824 // drawSelection other than the trivial implementation in Inset.
3826 // x += cellXPos(cur.idx());
3827 // x += tabular.textHOffset(cur.idx());
3828 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3833 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3834 row_type row, idx_type cell) const
3836 y -= tabular.rowAscent(row);
3837 int const w = tabular.cellWidth(cell);
3838 int const h = tabular.cellHeight(cell);
3839 Color const linecolor = pi.textColor(Color_tabularline);
3840 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3843 bool drawline = tabular.topLine(cell)
3844 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3845 pi.pain.line(x, y, x + w, y,
3846 drawline ? linecolor : gridcolor,
3847 drawline ? Painter::line_solid : Painter::line_onoffdash);
3850 drawline = tabular.bottomLine(cell);
3851 pi.pain.line(x, y + h, x + w, y + h,
3852 drawline ? linecolor : gridcolor,
3853 drawline ? Painter::line_solid : Painter::line_onoffdash);
3856 col_type const col = tabular.cellColumn(cell);
3857 drawline = tabular.leftLine(cell)
3858 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3859 pi.pain.line(x, y, x, y + h,
3860 drawline ? linecolor : gridcolor,
3861 drawline ? Painter::line_solid : Painter::line_onoffdash);
3864 x -= tabular.interColumnSpace(cell);
3865 col_type next_cell_col = col + 1;
3866 while (next_cell_col < tabular.ncols()
3867 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3869 drawline = tabular.rightLine(cell)
3870 || (next_cell_col < tabular.ncols()
3871 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3872 pi.pain.line(x + w, y, x + w, y + h,
3873 drawline ? linecolor : gridcolor,
3874 drawline ? Painter::line_solid : Painter::line_onoffdash);
3878 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3880 //lyxerr << "InsetTabular::edit: " << this << endl;
3882 cur.setSelection(false);
3885 if (isRightToLeft(cur))
3886 cur.idx() = tabular.getLastCellInRow(0);
3892 if (isRightToLeft(cur))
3893 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3895 cur.idx() = tabular.numberofcells - 1;
3897 cur.pos() = cur.lastpos(); // FIXME crude guess
3899 cur.setCurrentFont();
3900 // FIXME: this accesses the position cache before it is initialized
3902 //cur.bv().fitCursor();
3906 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3908 // In a longtable, tell captions what the current float is
3909 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3910 string const saveflt = cnts.current_float();
3911 if (tabular.is_long_tabular) {
3912 cnts.current_float("table");
3913 // in longtables, we only step the counter once
3914 cnts.step(from_ascii("table"), utype);
3915 cnts.isLongtable(true);
3918 ParIterator it2 = it;
3920 size_t const end = it2.nargs();
3921 for ( ; it2.idx() < end; it2.top().forwardIdx())
3922 buffer().updateBuffer(it2, utype);
3925 if (tabular.is_long_tabular) {
3926 cnts.current_float(saveflt);
3927 cnts.isLongtable(false);
3932 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active) const
3934 DocIterator dit = cpit;
3936 size_t const end = dit.nargs();
3937 for ( ; dit.idx() < end; dit.top().forwardIdx())
3938 cell(dit.idx())->addToToc(dit, output_active);
3942 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3944 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3945 << "\n cur:" << cur);
3946 CursorSlice sl = cur.top();
3947 Cursor & bvcur = cur.bv().cursor();
3949 FuncCode const act = cmd.action();
3953 case LFUN_MOUSE_PRESS: {
3954 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3956 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3957 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3958 row_type r = rowFromY(cur, cmd.y());
3959 cur.idx() = tabular.getFirstCellInRow(r);
3962 cur.idx() = tabular.getLastCellInRow(r);
3963 cur.pos() = cur.lastpos();
3964 cur.setSelection(true);
3970 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3971 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3972 || cmd.y() > y0 + tabular.height()) {
3973 col_type c = columnFromX(cur, cmd.x());
3974 cur.idx() = tabular.cellIndex(0, c);
3977 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3978 cur.pos() = cur.lastpos();
3979 cur.setSelection(true);
3984 // do not reset cursor/selection if we have selected
3985 // some cells (bug 2715).
3986 if (cmd.button() == mouse_button::button3
3987 && &bvcur.selBegin().inset() == this
3988 && bvcur.selIsMultiCell())
3991 // Let InsetTableCell do it
3992 cell(cur.idx())->dispatch(cur, cmd);
3995 case LFUN_MOUSE_MOTION:
3996 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3997 if (cmd.button() == mouse_button::button1) {
3998 // only accept motions to places not deeper nested than the real anchor
3999 if (!bvcur.realAnchor().hasPart(cur)) {
4003 // select (additional) row
4005 row_type r = rowFromY(cur, cmd.y());
4006 cur.idx() = tabular.getLastCellInRow(r);
4007 // we need to reset the cursor's pit and pos now, as the old ones
4008 // may no longer be valid.
4011 bvcur.setCursor(cur);
4012 bvcur.setSelection(true);
4015 // select (additional) column
4017 col_type c = columnFromX(cur, cmd.x());
4018 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4019 // we need to reset the cursor's pit and pos now, as the old ones
4020 // may no longer be valid.
4023 bvcur.setCursor(cur);
4024 bvcur.setSelection(true);
4027 // only update if selection changes
4028 if (bvcur.idx() == cur.idx() &&
4029 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4030 cur.noScreenUpdate();
4031 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4032 bvcur.setCursor(cur);
4033 bvcur.setSelection(true);
4034 // if this is a multicell selection, we just set the cursor to
4035 // the beginning of the cell's text.
4036 if (bvcur.selIsMultiCell()) {
4037 bvcur.pit() = bvcur.lastpit();
4038 bvcur.pos() = bvcur.lastpos();
4043 case LFUN_MOUSE_RELEASE:
4048 case LFUN_CELL_BACKWARD:
4050 cur.setSelection(false);
4053 case LFUN_CELL_FORWARD:
4055 cur.setSelection(false);
4058 case LFUN_CHAR_FORWARD_SELECT:
4059 case LFUN_CHAR_FORWARD:
4060 case LFUN_CHAR_BACKWARD_SELECT:
4061 case LFUN_CHAR_BACKWARD:
4062 case LFUN_CHAR_RIGHT_SELECT:
4063 case LFUN_CHAR_RIGHT:
4064 case LFUN_CHAR_LEFT_SELECT:
4065 case LFUN_CHAR_LEFT:
4066 case LFUN_WORD_FORWARD:
4067 case LFUN_WORD_FORWARD_SELECT:
4068 case LFUN_WORD_BACKWARD:
4069 case LFUN_WORD_BACKWARD_SELECT:
4070 case LFUN_WORD_RIGHT:
4071 case LFUN_WORD_RIGHT_SELECT:
4072 case LFUN_WORD_LEFT:
4073 case LFUN_WORD_LEFT_SELECT: {
4074 // determine whether we move to next or previous cell, where to enter
4075 // the new cell from, and which command to "finish" (i.e., exit the
4078 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4079 FuncCode finish_lfun;
4081 if (act == LFUN_CHAR_FORWARD
4082 || act == LFUN_CHAR_FORWARD_SELECT
4083 || act == LFUN_WORD_FORWARD
4084 || act == LFUN_WORD_FORWARD_SELECT) {
4086 finish_lfun = LFUN_FINISHED_FORWARD;
4088 else if (act == LFUN_CHAR_BACKWARD
4089 || act == LFUN_CHAR_BACKWARD_SELECT
4090 || act == LFUN_WORD_BACKWARD
4091 || act == LFUN_WORD_BACKWARD_SELECT) {
4093 finish_lfun = LFUN_FINISHED_BACKWARD;
4095 // LEFT or RIGHT commands --- the interpretation will depend on the
4096 // table's direction.
4098 bool const right = act == LFUN_CHAR_RIGHT
4099 || act == LFUN_CHAR_RIGHT_SELECT
4100 || act == LFUN_WORD_RIGHT
4101 || act == LFUN_WORD_RIGHT_SELECT;
4102 next_cell = isRightToLeft(cur) != right;
4104 if (lyxrc.visual_cursor)
4105 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4107 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4110 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4111 || act == LFUN_CHAR_BACKWARD_SELECT
4112 || act == LFUN_CHAR_RIGHT_SELECT
4113 || act == LFUN_CHAR_LEFT_SELECT
4114 || act == LFUN_WORD_FORWARD_SELECT
4115 || act == LFUN_WORD_RIGHT_SELECT
4116 || act == LFUN_WORD_BACKWARD_SELECT
4117 || act == LFUN_WORD_LEFT_SELECT;
4119 // If we have a multicell selection or we're
4120 // not doing some LFUN_*_SELECT thing anyway...
4121 if (!cur.selIsMultiCell() || !select) {
4122 col_type const c = tabular.cellColumn(cur.idx());
4123 row_type const r = tabular.cellRow(cur.idx());
4124 // Are we trying to select the whole cell and is the whole cell
4125 // not yet selected?
4126 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4127 ((next_cell && cur.pit() == cur.lastpit()
4128 && cur.pos() == cur.lastpos())
4129 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4131 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4133 // ...try to dispatch to the cell's inset.
4134 cell(cur.idx())->dispatch(cur, cmd);
4136 // When we already have a selection we want to select the whole cell
4137 // before going to the next cell.
4138 if (select_whole && !empty_cell){
4139 getText(cur.idx())->selectAll(cur);
4141 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4145 // FIXME: When we support the selection of an empty cell, remove
4146 // the !empty_cell from this condition. For now we jump to the next
4147 // cell if the current cell is empty.
4148 if (cur.result().dispatched() && !empty_cell)
4152 // move to next/prev cell, as appropriate
4153 // note that we will always do this if we're selecting and we have
4154 // a multicell selection
4155 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4156 << " cell from: " << int(entry_from));
4158 moveNextCell(cur, entry_from);
4160 movePrevCell(cur, entry_from);
4161 // if we're exiting the table, call the appropriate FINISHED lfun
4162 if (sl == cur.top())
4163 cmd = FuncRequest(finish_lfun);
4167 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4172 case LFUN_DOWN_SELECT:
4174 if (!(cur.selection() && cur.selIsMultiCell()))
4175 cell(cur.idx())->dispatch(cur, cmd);
4177 cur.dispatched(); // override the cell's decision
4178 if (sl == cur.top()) {
4179 // if our Text didn't do anything to the cursor
4180 // then we try to put the cursor into the cell below
4181 // setting also the right targetX.
4182 cur.selHandle(act == LFUN_DOWN_SELECT);
4183 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4184 int const xtarget = cur.targetX();
4185 // WARNING: Once cur.idx() has been reset, the cursor is in
4186 // an inconsistent state until pos() has been set. Be careful
4187 // what you do with it!
4188 cur.idx() = tabular.cellBelow(cur.idx());
4190 TextMetrics const & tm =
4191 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4192 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4193 cur.setCurrentFont();
4196 if (sl == cur.top()) {
4197 // we trick it to go to forward after leaving the
4199 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4202 if (cur.selIsMultiCell()) {
4203 cur.pit() = cur.lastpit();
4204 cur.pos() = cur.lastpos();
4205 cur.setCurrentFont();
4206 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4209 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4212 case LFUN_UP_SELECT:
4214 if (!(cur.selection() && cur.selIsMultiCell()))
4215 cell(cur.idx())->dispatch(cur, cmd);
4216 cur.dispatched(); // override the cell's decision
4217 if (sl == cur.top()) {
4218 // if our Text didn't do anything to the cursor
4219 // then we try to put the cursor into the cell above
4220 // setting also the right targetX.
4221 cur.selHandle(act == LFUN_UP_SELECT);
4222 if (tabular.cellRow(cur.idx()) != 0) {
4223 int const xtarget = cur.targetX();
4224 // WARNING: Once cur.idx() has been reset, the cursor is in
4225 // an inconsistent state until pos() has been set. Be careful
4226 // what you do with it!
4227 cur.idx() = tabular.cellAbove(cur.idx());
4228 cur.pit() = cur.lastpit();
4229 Text const * text = cell(cur.idx())->getText(0);
4230 TextMetrics const & tm = cur.bv().textMetrics(text);
4231 ParagraphMetrics const & pm =
4232 tm.parMetrics(cur.lastpit());
4233 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4234 cur.setCurrentFont();
4237 if (sl == cur.top()) {
4238 cmd = FuncRequest(LFUN_UP);
4241 if (cur.selIsMultiCell()) {
4243 cur.pos() = cur.lastpos();
4244 cur.setCurrentFont();
4245 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4248 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4251 // case LFUN_SCREEN_DOWN: {
4252 // //if (hasSelection())
4253 // // cur.selection() = false;
4254 // col_type const col = tabular.cellColumn(cur.idx());
4255 // int const t = cur.bv().top_y() + cur.bv().height();
4256 // if (t < yo() + tabular.getHeightOfTabular()) {
4257 // cur.bv().scrollDocView(t, true);
4258 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4260 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4267 // case LFUN_SCREEN_UP: {
4268 // //if (hasSelection())
4269 // // cur.selection() = false;
4270 // col_type const col = tabular.cellColumn(cur.idx());
4271 // int const t = cur.bv().top_y() + cur.bv().height();
4273 // cur.bv().scrollDocView(t, true);
4277 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4281 // cur.par() = cur.lastpar();
4282 // cur.pos() = cur.lastpos();
4286 case LFUN_LAYOUT_TABULAR:
4287 cur.bv().showDialog("tabular");
4290 case LFUN_INSET_MODIFY: {
4292 if (cmd.getArg(1) == "from-dialog")
4293 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4295 arg = to_utf8(cmd.argument());
4296 if (!tabularFeatures(cur, arg))
4301 // insert file functions
4302 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4303 case LFUN_FILE_INSERT_PLAINTEXT:
4305 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4306 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4307 FileName(to_utf8(cmd.argument())));
4310 cur.recordUndoInset(INSERT_UNDO);
4311 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4312 // content has been replaced,
4313 // so cursor might be invalid
4314 cur.pos() = cur.lastpos();
4315 cur.pit() = cur.lastpit();
4316 bvcur.setCursor(cur);
4323 if (cur.selIsMultiCell()) {
4324 if (copySelection(cur)) {
4325 cur.recordUndoInset(DELETE_UNDO);
4329 cell(cur.idx())->dispatch(cur, cmd);
4332 case LFUN_SELF_INSERT:
4333 if (cur.selIsMultiCell()) {
4334 cur.recordUndoInset(DELETE_UNDO);
4336 BufferView * bv = &cur.bv();
4337 docstring::const_iterator cit = cmd.argument().begin();
4338 docstring::const_iterator const end = cmd.argument().end();
4339 for (; cit != end; ++cit)
4340 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4343 bv->bookmarkEditPosition();
4345 cell(cur.idx())->dispatch(cur, cmd);
4348 case LFUN_CHAR_DELETE_BACKWARD:
4349 case LFUN_CHAR_DELETE_FORWARD:
4350 if (cur.selIsMultiCell()) {
4351 cur.recordUndoInset(DELETE_UNDO);
4354 cell(cur.idx())->dispatch(cur, cmd);
4358 if (!cur.selection())
4360 if (cur.selIsMultiCell()) {
4364 cell(cur.idx())->dispatch(cur, cmd);
4367 case LFUN_CLIPBOARD_PASTE:
4368 case LFUN_PRIMARY_SELECTION_PASTE: {
4369 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4370 theClipboard().getAsText(Clipboard::PlainTextType) :
4371 theSelection().get();
4374 // pass to InsertPlaintextString, but
4375 // only if we have multi-cell content
4376 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4377 cur.recordUndoInset(INSERT_UNDO);
4378 if (insertPlaintextString(cur.bv(), clip, false)) {
4379 // content has been replaced,
4380 // so cursor might be invalid
4381 cur.pos() = cur.lastpos();
4382 cur.pit() = cur.lastpit();
4383 bvcur.setCursor(cur);
4387 // Let the cell handle normal text
4388 cell(cur.idx())->dispatch(cur, cmd);
4393 if (!tabularStackDirty()) {
4394 if (!cur.selIsMultiCell())
4395 cell(cur.idx())->dispatch(cur, cmd);
4398 if (theClipboard().isInternal()) {
4399 cur.recordUndoInset(INSERT_UNDO);
4400 pasteClipboard(cur);
4404 case LFUN_FONT_EMPH:
4405 case LFUN_FONT_BOLD:
4406 case LFUN_FONT_BOLDSYMBOL:
4407 case LFUN_FONT_ROMAN:
4408 case LFUN_FONT_NOUN:
4409 case LFUN_FONT_ITAL:
4410 case LFUN_FONT_FRAK:
4411 case LFUN_FONT_TYPEWRITER:
4412 case LFUN_FONT_SANS:
4413 case LFUN_TEXTSTYLE_APPLY:
4414 case LFUN_TEXTSTYLE_UPDATE:
4415 case LFUN_FONT_SIZE:
4416 case LFUN_FONT_UNDERLINE:
4417 case LFUN_FONT_STRIKEOUT:
4418 case LFUN_FONT_UNDERUNDERLINE:
4419 case LFUN_FONT_UNDERWAVE:
4421 case LFUN_PARAGRAPH_PARAMS_APPLY:
4422 case LFUN_PARAGRAPH_PARAMS:
4423 case LFUN_WORD_CAPITALIZE:
4424 case LFUN_WORD_UPCASE:
4425 case LFUN_WORD_LOWCASE:
4426 case LFUN_CHARS_TRANSPOSE:
4427 if (cur.selIsMultiCell()) {
4430 getSelection(cur, rs, re, cs, ce);
4431 Cursor tmpcur = cur;
4432 for (row_type r = rs; r <= re; ++r) {
4433 for (col_type c = cs; c <= ce; ++c) {
4434 // cursor follows cell:
4435 tmpcur.idx() = tabular.cellIndex(r, c);
4436 // select this cell only:
4439 tmpcur.resetAnchor();
4440 tmpcur.pit() = tmpcur.lastpit();
4441 tmpcur.pos() = tmpcur.top().lastpos();
4442 tmpcur.setCursor(tmpcur);
4443 tmpcur.setSelection();
4444 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4449 cell(cur.idx())->dispatch(cur, cmd);
4453 case LFUN_INSET_SETTINGS:
4454 // relay this lfun to Inset, not to the cell.
4455 Inset::doDispatch(cur, cmd);
4459 // we try to handle this event in the insets dispatch function.
4460 cell(cur.idx())->dispatch(cur, cmd);
4466 // function sets an object as defined in func_status.h:
4467 // states OK, Unknown, Disabled, On, Off.
4468 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4469 FuncStatus & status) const
4471 switch (cmd.action()) {
4472 case LFUN_INSET_MODIFY: {
4473 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4476 // FIXME: We only check for the very first argument...
4477 string const s = cmd.getArg(1);
4478 // We always enable the lfun if it is coming from the dialog
4479 // because the dialog makes sure all the settings are valid,
4480 // even though the first argument might not be valid now.
4481 if (s == "from-dialog") {
4482 status.setEnabled(true);
4486 int action = Tabular::LAST_ACTION;
4488 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4489 if (tabularFeature[i].feature == s) {
4490 action = tabularFeature[i].action;
4494 if (action == Tabular::LAST_ACTION) {
4496 status.setUnknown(true);
4500 string const argument = cmd.getLongArg(2);
4502 row_type sel_row_start = 0;
4503 row_type sel_row_end = 0;
4504 col_type sel_col_start = 0;
4505 col_type sel_col_end = 0;
4506 Tabular::ltType dummyltt;
4509 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4512 case Tabular::SET_PWIDTH:
4513 case Tabular::SET_MPWIDTH:
4514 case Tabular::SET_SPECIAL_COLUMN:
4515 case Tabular::SET_SPECIAL_MULTICOLUMN:
4516 case Tabular::APPEND_ROW:
4517 case Tabular::APPEND_COLUMN:
4518 case Tabular::DELETE_ROW:
4519 case Tabular::DELETE_COLUMN:
4520 case Tabular::COPY_ROW:
4521 case Tabular::COPY_COLUMN:
4522 case Tabular::SET_TOP_SPACE:
4523 case Tabular::SET_BOTTOM_SPACE:
4524 case Tabular::SET_INTERLINE_SPACE:
4528 case Tabular::SET_TABULAR_WIDTH:
4529 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4530 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4533 case Tabular::MOVE_COLUMN_RIGHT:
4534 case Tabular::MOVE_COLUMN_LEFT:
4535 case Tabular::MOVE_ROW_DOWN:
4536 case Tabular::MOVE_ROW_UP: {
4537 if (cur.selection()) {
4538 status.message(_("Selections not supported."));
4539 status.setEnabled(false);
4543 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4544 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4545 (action == Tabular::MOVE_COLUMN_LEFT &&
4546 tabular.cellColumn(cur.idx()) == 0) ||
4547 (action == Tabular::MOVE_ROW_DOWN &&
4548 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4549 (action == Tabular::MOVE_ROW_UP &&
4550 tabular.cellRow(cur.idx()) == 0)) {
4551 status.setEnabled(false);
4555 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4556 action == Tabular::MOVE_COLUMN_LEFT) {
4557 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4558 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4559 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4560 status.message(_("Multi-column in current or"
4561 " destination column."));
4562 status.setEnabled(false);
4567 if (action == Tabular::MOVE_ROW_DOWN ||
4568 action == Tabular::MOVE_ROW_UP) {
4569 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4570 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4571 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4572 status.message(_("Multi-row in current or"
4573 " destination row."));
4574 status.setEnabled(false);
4579 status.setEnabled(true);
4583 case Tabular::SET_DECIMAL_POINT:
4585 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4588 case Tabular::SET_MULTICOLUMN:
4589 case Tabular::UNSET_MULTICOLUMN:
4590 case Tabular::MULTICOLUMN:
4591 // If a row is set as longtable caption, it must not be allowed
4592 // to unset that this row is a multicolumn.
4593 status.setEnabled(sel_row_start == sel_row_end
4594 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4595 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4598 case Tabular::SET_MULTIROW:
4599 case Tabular::UNSET_MULTIROW:
4600 case Tabular::MULTIROW:
4601 // If a row is set as longtable caption, it must not be allowed
4602 // to unset that this row is a multirow.
4603 status.setEnabled(sel_col_start == sel_col_end
4604 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4605 status.setOnOff(tabular.isMultiRow(cur.idx()));
4608 case Tabular::SET_ALL_LINES:
4609 case Tabular::UNSET_ALL_LINES:
4610 case Tabular::SET_BORDER_LINES:
4611 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4614 case Tabular::SET_LINE_TOP:
4615 case Tabular::SET_LINE_BOTTOM:
4616 case Tabular::SET_LINE_LEFT:
4617 case Tabular::SET_LINE_RIGHT:
4618 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4621 case Tabular::TOGGLE_LINE_TOP:
4622 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4623 status.setOnOff(tabular.topLine(cur.idx()));
4626 case Tabular::TOGGLE_LINE_BOTTOM:
4627 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4628 status.setOnOff(tabular.bottomLine(cur.idx()));
4631 case Tabular::TOGGLE_LINE_LEFT:
4632 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4633 status.setOnOff(tabular.leftLine(cur.idx()));
4636 case Tabular::TOGGLE_LINE_RIGHT:
4637 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4638 status.setOnOff(tabular.rightLine(cur.idx()));
4641 // multirow cells only inherit the alignment of the column if the column has
4642 // no width, otherwise they are left-aligned
4643 // therefore allow always left but right and center only if there is no width
4644 case Tabular::M_ALIGN_LEFT:
4646 case Tabular::ALIGN_LEFT:
4647 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4650 case Tabular::M_ALIGN_RIGHT:
4652 case Tabular::ALIGN_RIGHT:
4653 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4654 && !tabular.getPWidth(cur.idx()).zero()));
4655 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4658 case Tabular::M_ALIGN_CENTER:
4660 case Tabular::ALIGN_CENTER:
4661 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4662 && !tabular.getPWidth(cur.idx()).zero()));
4663 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4666 case Tabular::ALIGN_BLOCK:
4667 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4668 && !tabular.isMultiRow(cur.idx()));
4669 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4672 case Tabular::ALIGN_DECIMAL:
4673 status.setEnabled(!tabular.isMultiRow(cur.idx())
4674 && !tabular.isMultiColumn(cur.idx()));
4675 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4678 case Tabular::M_VALIGN_TOP:
4680 case Tabular::VALIGN_TOP:
4681 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4682 && !tabular.isMultiRow(cur.idx()));
4684 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4687 case Tabular::M_VALIGN_BOTTOM:
4689 case Tabular::VALIGN_BOTTOM:
4690 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4691 && !tabular.isMultiRow(cur.idx()));
4693 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4696 case Tabular::M_VALIGN_MIDDLE:
4698 case Tabular::VALIGN_MIDDLE:
4699 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4700 && !tabular.isMultiRow(cur.idx()));
4702 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4705 case Tabular::SET_LONGTABULAR:
4706 // setting as longtable is not allowed when table is inside a float
4707 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4708 || cur.innerInsetOfType(WRAP_CODE) != 0)
4709 status.setEnabled(false);
4711 status.setEnabled(true);
4712 status.setOnOff(tabular.is_long_tabular);
4715 case Tabular::UNSET_LONGTABULAR:
4716 status.setOnOff(!tabular.is_long_tabular);
4719 case Tabular::TOGGLE_ROTATE_TABULAR:
4720 case Tabular::SET_ROTATE_TABULAR:
4721 status.setOnOff(tabular.rotate != 0);
4724 case Tabular::TABULAR_VALIGN_TOP:
4725 status.setEnabled(tabular.tabular_width.zero());
4726 status.setOnOff(tabular.tabular_valignment
4727 == Tabular::LYX_VALIGN_TOP);
4729 case Tabular::TABULAR_VALIGN_MIDDLE:
4730 status.setEnabled(tabular.tabular_width.zero());
4731 status.setOnOff(tabular.tabular_valignment
4732 == Tabular::LYX_VALIGN_MIDDLE);
4734 case Tabular::TABULAR_VALIGN_BOTTOM:
4735 status.setEnabled(tabular.tabular_width.zero());
4736 status.setOnOff(tabular.tabular_valignment
4737 == Tabular::LYX_VALIGN_BOTTOM);
4740 case Tabular::LONGTABULAR_ALIGN_LEFT:
4741 status.setOnOff(tabular.longtabular_alignment
4742 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4744 case Tabular::LONGTABULAR_ALIGN_CENTER:
4745 status.setOnOff(tabular.longtabular_alignment
4746 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4748 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4749 status.setOnOff(tabular.longtabular_alignment
4750 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4753 case Tabular::UNSET_ROTATE_TABULAR:
4754 status.setOnOff(tabular.rotate == 0);
4757 case Tabular::TOGGLE_ROTATE_CELL:
4758 case Tabular::SET_ROTATE_CELL:
4759 status.setOnOff(!oneCellHasRotationState(false,
4760 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4763 case Tabular::UNSET_ROTATE_CELL:
4764 status.setOnOff(!oneCellHasRotationState(true,
4765 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4768 case Tabular::SET_USEBOX:
4769 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4772 // every row can only be one thing:
4773 // either a footer or header
4774 case Tabular::SET_LTFIRSTHEAD:
4775 status.setEnabled(sel_row_start == sel_row_end);
4776 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4779 case Tabular::UNSET_LTFIRSTHEAD:
4780 status.setEnabled(sel_row_start == sel_row_end);
4781 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4784 case Tabular::SET_LTHEAD:
4785 status.setEnabled(sel_row_start == sel_row_end);
4786 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4789 case Tabular::UNSET_LTHEAD:
4790 status.setEnabled(sel_row_start == sel_row_end);
4791 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4794 case Tabular::SET_LTFOOT:
4795 status.setEnabled(sel_row_start == sel_row_end);
4796 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4799 case Tabular::UNSET_LTFOOT:
4800 status.setEnabled(sel_row_start == sel_row_end);
4801 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4804 case Tabular::SET_LTLASTFOOT:
4805 status.setEnabled(sel_row_start == sel_row_end);
4806 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4809 case Tabular::UNSET_LTLASTFOOT:
4810 status.setEnabled(sel_row_start == sel_row_end);
4811 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4814 case Tabular::SET_LTNEWPAGE:
4815 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4817 case Tabular::UNSET_LTNEWPAGE:
4818 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4821 // only one row in head/firsthead/foot/lasthead can be the caption
4822 // and a multirow cannot be set as caption
4823 case Tabular::SET_LTCAPTION:
4824 status.setEnabled(sel_row_start == sel_row_end
4825 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4826 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4827 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4828 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4829 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4830 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4831 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4832 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4833 && !tabular.isMultiRow(sel_row_start));
4834 status.setOnOff(tabular.ltCaption(sel_row_start));
4837 case Tabular::UNSET_LTCAPTION:
4838 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4841 case Tabular::TOGGLE_LTCAPTION:
4842 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4843 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4844 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4845 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4846 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4847 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4848 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4849 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4850 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4851 status.setOnOff(tabular.ltCaption(sel_row_start));
4854 case Tabular::SET_BOOKTABS:
4855 status.setOnOff(tabular.use_booktabs);
4858 case Tabular::UNSET_BOOKTABS:
4859 status.setOnOff(!tabular.use_booktabs);
4864 status.setEnabled(false);
4870 case LFUN_CAPTION_INSERT: {
4871 // caption is only allowed in caption cell of longtable
4872 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4873 status.setEnabled(false);
4876 // only standard caption is allowed
4877 string arg = cmd.getArg(0);
4878 if (!arg.empty() && arg != "Standard") {
4879 status.setEnabled(false);
4882 // check if there is already a caption
4883 bool have_caption = false;
4884 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4885 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4886 ParagraphList::const_iterator pend = itc.paragraphs().end();
4887 for (; pit != pend; ++pit) {
4888 InsetList::const_iterator it = pit->insetList().begin();
4889 InsetList::const_iterator end = pit->insetList().end();
4890 for (; it != end; ++it) {
4891 if (it->inset->lyxCode() == CAPTION_CODE) {
4892 have_caption = true;
4897 status.setEnabled(!have_caption);
4901 // These are only enabled inside tabular
4902 case LFUN_CELL_BACKWARD:
4903 case LFUN_CELL_FORWARD:
4904 status.setEnabled(true);
4907 // disable these with multiple cells selected
4908 case LFUN_INSET_INSERT:
4909 case LFUN_TABULAR_INSERT:
4910 case LFUN_FLEX_INSERT:
4911 case LFUN_FLOAT_INSERT:
4912 case LFUN_FLOAT_WIDE_INSERT:
4913 case LFUN_FOOTNOTE_INSERT:
4914 case LFUN_MARGINALNOTE_INSERT:
4915 case LFUN_MATH_INSERT:
4916 case LFUN_MATH_MODE:
4917 case LFUN_MATH_MUTATE:
4918 case LFUN_MATH_DISPLAY:
4919 case LFUN_NOTE_INSERT:
4920 case LFUN_ARGUMENT_INSERT:
4921 case LFUN_BOX_INSERT:
4922 case LFUN_BRANCH_INSERT:
4923 case LFUN_PHANTOM_INSERT:
4924 case LFUN_WRAP_INSERT:
4925 case LFUN_PREVIEW_INSERT:
4926 case LFUN_ERT_INSERT: {
4927 if (cur.selIsMultiCell()) {
4928 status.setEnabled(false);
4931 return cell(cur.idx())->getStatus(cur, cmd, status);
4934 // disable in non-fixed-width cells
4935 case LFUN_PARAGRAPH_BREAK:
4936 // multirow does not allow paragraph breaks
4937 if (tabular.isMultiRow(cur.idx())) {
4938 status.setEnabled(false);
4941 case LFUN_NEWLINE_INSERT: {
4942 if (tabular.getPWidth(cur.idx()).zero()) {
4943 status.setEnabled(false);
4946 return cell(cur.idx())->getStatus(cur, cmd, status);
4949 case LFUN_NEWPAGE_INSERT:
4950 status.setEnabled(false);
4954 if (tabularStackDirty() && theClipboard().isInternal()) {
4955 if (cur.selIsMultiCell()) {
4958 getSelection(cur, rs, re, cs, ce);
4959 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4960 && paste_tabular->nrows() == re - rs + 1)
4961 status.setEnabled(true);
4963 status.setEnabled(false);
4964 status.message(_("Selection size should match clipboard content."));
4967 status.setEnabled(true);
4970 return cell(cur.idx())->getStatus(cur, cmd, status);
4972 case LFUN_INSET_SETTINGS:
4973 // relay this lfun to Inset, not to the cell.
4974 return Inset::getStatus(cur, cmd, status);
4977 // we try to handle this event in the insets dispatch function.
4978 return cell(cur.idx())->getStatus(cur, cmd, status);
4984 Inset::DisplayType InsetTabular::display() const
4986 if (tabular.is_long_tabular) {
4987 switch (tabular.longtabular_alignment) {
4988 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4990 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4992 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5002 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5004 tabular.latex(os, runparams);
5008 int InsetTabular::plaintext(odocstringstream & os,
5009 OutputParams const & runparams, size_t max_length) const
5011 os << '\n'; // output table on a new line
5012 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5013 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5014 return PLAINTEXT_NEWLINE;
5018 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5023 // FIXME: Why not pass a proper DocIterator here?
5025 // if the table is inside a float it doesn't need the informaltable
5026 // wrapper. Search for it.
5027 for (master = owner(); master; master = master->owner())
5028 if (master->lyxCode() == FLOAT_CODE)
5033 os << "<informaltable>";
5036 ret += tabular.docbook(os, runparams);
5038 os << "</informaltable>";
5045 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5047 return tabular.xhtml(xs, rp);
5051 void InsetTabular::validate(LaTeXFeatures & features) const
5053 tabular.validate(features);
5055 // It'd be better to be able to get this from an InsetLayout, but at present
5056 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5057 if (features.runparams().flavor == OutputParams::HTML)
5058 features.addCSSSnippet(
5059 "table { border: 1px solid black; display: inline-block; }\n"
5060 "td { border: 1px solid black; padding: 0.5ex; }");
5064 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5066 return tabular.cellInset(idx);
5070 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5072 return tabular.cellInset(idx);
5076 void InsetTabular::cursorPos(BufferView const & bv,
5077 CursorSlice const & sl, bool boundary, int & x, int & y) const
5079 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5081 // y offset correction
5082 y += cellYPos(sl.idx());
5083 y += tabular.textVOffset(sl.idx());
5084 y += offset_valign_;
5086 // x offset correction
5087 x += cellXPos(sl.idx());
5088 x += tabular.textHOffset(sl.idx());
5089 x += ADD_TO_TABULAR_WIDTH;
5094 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5098 Inset const & inset = *tabular.cellInset(cell);
5099 Point o = bv.coordCache().getInsets().xy(&inset);
5100 int const xbeg = o.x_ - tabular.textHOffset(cell);
5101 int const xend = xbeg + tabular.cellWidth(cell);
5102 row_type const row = tabular.cellRow(cell);
5103 int const ybeg = o.y_ - tabular.rowAscent(row)
5104 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5105 int const yend = ybeg + tabular.cellHeight(cell);
5117 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5118 // << " ybeg=" << ybeg << " yend=" << yend
5119 // << " xx=" << xx << " yy=" << yy
5120 // << " dist=" << xx + yy << endl;
5125 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5127 //lyxerr << "InsetTabular::editXY: " << this << endl;
5128 cur.setSelection(false);
5130 cur.idx() = getNearestCell(cur.bv(), x, y);
5132 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5136 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5138 cur.idx() = getNearestCell(cur.bv(), x, y);
5139 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5143 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5145 idx_type idx_min = 0;
5146 int dist_min = numeric_limits<int>::max();
5147 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5148 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5149 int const d = dist(bv, i, x, y);
5160 int InsetTabular::cellYPos(idx_type const cell) const
5162 row_type row = tabular.cellRow(cell);
5164 for (row_type r = 0; r < row; ++r)
5165 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5166 + tabular.interRowSpace(r + 1);
5171 int InsetTabular::cellXPos(idx_type const cell) const
5173 col_type col = tabular.cellColumn(cell);
5175 for (col_type c = 0; c < col; ++c)
5176 lx += tabular.column_info[c].width;
5181 void InsetTabular::resetPos(Cursor & cur) const
5183 BufferView & bv = cur.bv();
5184 int const maxwidth = bv.workWidth();
5186 int const scx_old = scx_;
5187 int const i = cur.find(this);
5192 int const X2 = maxwidth;
5193 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5194 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5195 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5198 scx_ = X1 + 20 - x1;
5200 scx_ = X2 - 20 - x2;
5205 // only update if offset changed
5206 if (scx_ != scx_old)
5207 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5211 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5213 row_type const row = tabular.cellRow(cur.idx());
5214 col_type const col = tabular.cellColumn(cur.idx());
5216 if (isRightToLeft(cur)) {
5217 if (tabular.cellColumn(cur.idx()) == 0) {
5218 if (row == tabular.nrows() - 1)
5220 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5225 cur.idx() = tabular.getLastCellInRow(row - 1);
5227 cur.idx() = tabular.cellIndex(row, col - 1);
5230 if (tabular.isLastCell(cur.idx()))
5232 if (cur.idx() == tabular.getLastCellInRow(row))
5233 cur.idx() = tabular.cellIndex(row + 1, 0);
5235 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5236 cur.idx() = tabular.cellIndex(row, colnextcell);
5240 cur.boundary(false);
5242 if (cur.selIsMultiCell()) {
5243 cur.pit() = cur.lastpit();
5244 cur.pos() = cur.lastpos();
5252 // in visual mode, place cursor at extreme left or right
5254 switch(entry_from) {
5256 case ENTRY_DIRECTION_RIGHT:
5257 cur.posVisToRowExtremity(false /* !left */);
5259 case ENTRY_DIRECTION_LEFT:
5260 cur.posVisToRowExtremity(true /* left */);
5262 case ENTRY_DIRECTION_IGNORE:
5263 // nothing to do in this case
5267 cur.setCurrentFont();
5272 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5274 row_type const row = tabular.cellRow(cur.idx());
5275 col_type const col = tabular.cellColumn(cur.idx());
5277 if (isRightToLeft(cur)) {
5278 if (cur.idx() == tabular.getLastCellInRow(row)) {
5281 cur.idx() = tabular.getFirstCellInRow(row);
5282 cur.idx() = tabular.cellAbove(cur.idx());
5284 if (tabular.isLastCell(cur.idx()))
5286 if (cur.idx() == tabular.getLastCellInRow(row))
5287 cur.idx() = tabular.cellIndex(row + 1, 0);
5289 cur.idx() = tabular.cellIndex(row, col + 1);
5292 if (cur.idx() == 0) // first cell
5295 cur.idx() = tabular.getLastCellInRow(row - 1);
5297 cur.idx() = tabular.cellIndex(row, col - 1);
5300 if (cur.selIsMultiCell()) {
5301 cur.pit() = cur.lastpit();
5302 cur.pos() = cur.lastpos();
5307 cur.pit() = cur.lastpit();
5308 cur.pos() = cur.lastpos();
5310 // in visual mode, place cursor at extreme left or right
5312 switch(entry_from) {
5314 case ENTRY_DIRECTION_RIGHT:
5315 cur.posVisToRowExtremity(false /* !left */);
5317 case ENTRY_DIRECTION_LEFT:
5318 cur.posVisToRowExtremity(true /* left */);
5320 case ENTRY_DIRECTION_IGNORE:
5321 // nothing to do in this case
5325 cur.setCurrentFont();
5330 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5332 istringstream is(argument);
5335 if (insetCode(s) != TABULAR_CODE)
5339 size_t safe_guard = 0;
5344 if (safe_guard > 1000) {
5345 LYXERR0("parameter max count reached!");
5349 Tabular::Feature action = Tabular::LAST_ACTION;
5352 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5353 if (s != tabularFeature[i].feature)
5355 action = tabularFeature[i].action;
5358 if (action == Tabular::LAST_ACTION) {
5359 LYXERR0("Feature not found " << s);
5363 if (tabularFeature[i].need_value)
5365 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5366 tabularFeatures(cur, action, val);
5372 static void checkLongtableSpecial(Tabular::ltType & ltt,
5373 string const & special, bool & flag)
5375 if (special == "dl_above") {
5378 } else if (special == "dl_below") {
5379 ltt.bottomDL = flag;
5381 } else if (special == "empty") {
5391 bool InsetTabular::oneCellHasRotationState(bool rotated,
5392 row_type row_start, row_type row_end,
5393 col_type col_start, col_type col_end) const
5395 for (row_type r = row_start; r <= row_end; ++r)
5396 for (col_type c = col_start; c <= col_end; ++c)
5398 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5401 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5408 void InsetTabular::tabularFeatures(Cursor & cur,
5409 Tabular::Feature feature, string const & value)
5411 col_type sel_col_start;
5412 col_type sel_col_end;
5413 row_type sel_row_start;
5414 row_type sel_row_end;
5415 bool setLines = false;
5416 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5417 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5421 case Tabular::M_ALIGN_LEFT:
5422 case Tabular::ALIGN_LEFT:
5423 setAlign = LYX_ALIGN_LEFT;
5426 case Tabular::M_ALIGN_RIGHT:
5427 case Tabular::ALIGN_RIGHT:
5428 setAlign = LYX_ALIGN_RIGHT;
5431 case Tabular::M_ALIGN_CENTER:
5432 case Tabular::ALIGN_CENTER:
5433 setAlign = LYX_ALIGN_CENTER;
5436 case Tabular::ALIGN_BLOCK:
5437 setAlign = LYX_ALIGN_BLOCK;
5440 case Tabular::ALIGN_DECIMAL:
5441 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5442 setAlign = LYX_ALIGN_CENTER;
5444 setAlign = LYX_ALIGN_DECIMAL;
5447 case Tabular::M_VALIGN_TOP:
5448 case Tabular::VALIGN_TOP:
5449 setVAlign = Tabular::LYX_VALIGN_TOP;
5452 case Tabular::M_VALIGN_BOTTOM:
5453 case Tabular::VALIGN_BOTTOM:
5454 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5457 case Tabular::M_VALIGN_MIDDLE:
5458 case Tabular::VALIGN_MIDDLE:
5459 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5466 cur.recordUndoInset(ATOMIC_UNDO, this);
5468 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5469 row_type const row = tabular.cellRow(cur.idx());
5470 col_type const column = tabular.cellColumn(cur.idx());
5472 Tabular::ltType ltt;
5476 case Tabular::SET_TABULAR_WIDTH:
5477 tabular.setTabularWidth(Length(value));
5480 case Tabular::SET_PWIDTH: {
5481 Length const len(value);
5482 tabular.setColumnPWidth(cur, cur.idx(), len);
5484 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5485 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5489 case Tabular::SET_MPWIDTH:
5490 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5493 case Tabular::SET_MROFFSET:
5494 tabular.setMROffset(cur, cur.idx(), Length(value));
5497 case Tabular::SET_SPECIAL_COLUMN:
5498 case Tabular::SET_SPECIAL_MULTICOLUMN:
5499 if (value == "none")
5500 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5502 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5505 case Tabular::APPEND_ROW:
5506 // append the row into the tabular
5507 tabular.appendRow(row);
5510 case Tabular::APPEND_COLUMN:
5511 // append the column into the tabular
5512 tabular.appendColumn(column);
5513 cur.idx() = tabular.cellIndex(row, column);
5516 case Tabular::DELETE_ROW:
5517 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5518 for (col_type c = 0; c < tabular.ncols(); c++)
5519 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5520 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5523 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5524 tabular.deleteRow(sel_row_start);
5525 if (sel_row_start >= tabular.nrows())
5527 cur.idx() = tabular.cellIndex(sel_row_start, column);
5530 cur.setSelection(false);
5533 case Tabular::DELETE_COLUMN:
5534 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5535 for (row_type r = 0; r < tabular.nrows(); r++)
5536 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5537 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5540 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5541 for (row_type r = 0; r < tabular.nrows(); r++)
5542 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5543 tabular.leftLine(tabular.cellIndex(r, 0)));
5546 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5547 tabular.deleteColumn(sel_col_start);
5548 if (sel_col_start >= tabular.ncols())
5550 cur.idx() = tabular.cellIndex(row, sel_col_start);
5553 cur.setSelection(false);
5556 case Tabular::COPY_ROW:
5557 tabular.copyRow(row);
5560 case Tabular::COPY_COLUMN:
5561 tabular.copyColumn(column);
5562 cur.idx() = tabular.cellIndex(row, column);
5565 case Tabular::MOVE_COLUMN_RIGHT:
5566 tabular.moveColumn(column, Tabular::RIGHT);
5567 cur.idx() = tabular.cellIndex(row, column + 1);
5570 case Tabular::MOVE_COLUMN_LEFT:
5571 tabular.moveColumn(column, Tabular::LEFT);
5572 cur.idx() = tabular.cellIndex(row, column - 1);
5575 case Tabular::MOVE_ROW_DOWN:
5576 tabular.moveRow(row, Tabular::DOWN);
5577 cur.idx() = tabular.cellIndex(row + 1, column);
5580 case Tabular::MOVE_ROW_UP:
5581 tabular.moveRow(row, Tabular::UP);
5582 cur.idx() = tabular.cellIndex(row - 1, column);
5585 case Tabular::SET_LINE_TOP:
5586 case Tabular::TOGGLE_LINE_TOP: {
5587 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5588 ? (value == "true") : !tabular.topLine(cur.idx());
5589 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5590 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5591 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5595 case Tabular::SET_LINE_BOTTOM:
5596 case Tabular::TOGGLE_LINE_BOTTOM: {
5597 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5598 ? (value == "true") : !tabular.bottomLine(cur.idx());
5599 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5600 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5601 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5605 case Tabular::SET_LINE_LEFT:
5606 case Tabular::TOGGLE_LINE_LEFT: {
5607 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5608 ? (value == "true") : !tabular.leftLine(cur.idx());
5609 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5610 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5611 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5615 case Tabular::SET_LINE_RIGHT:
5616 case Tabular::TOGGLE_LINE_RIGHT: {
5617 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5618 ? (value == "true") : !tabular.rightLine(cur.idx());
5619 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5620 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5621 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5625 case Tabular::M_ALIGN_LEFT:
5626 case Tabular::M_ALIGN_RIGHT:
5627 case Tabular::M_ALIGN_CENTER:
5628 case Tabular::ALIGN_LEFT:
5629 case Tabular::ALIGN_RIGHT:
5630 case Tabular::ALIGN_CENTER:
5631 case Tabular::ALIGN_BLOCK:
5632 case Tabular::ALIGN_DECIMAL:
5633 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5634 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5635 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5636 !tabular.getPWidth(c).zero());
5639 case Tabular::M_VALIGN_TOP:
5640 case Tabular::M_VALIGN_BOTTOM:
5641 case Tabular::M_VALIGN_MIDDLE:
5643 case Tabular::VALIGN_TOP:
5644 case Tabular::VALIGN_BOTTOM:
5645 case Tabular::VALIGN_MIDDLE:
5646 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5647 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5648 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5651 case Tabular::SET_MULTICOLUMN: {
5652 if (!cur.selection()) {
5653 // just multicol for one single cell
5654 // check whether we are completely in a multicol
5655 if (!tabular.isMultiColumn(cur.idx()))
5656 tabular.setMultiColumn(cur.idx(), 1,
5657 tabular.rightLine(cur.idx()));
5660 // we have a selection so this means we just add all these
5661 // cells to form a multicolumn cell
5662 idx_type const s_start = cur.selBegin().idx();
5663 row_type const col_start = tabular.cellColumn(s_start);
5664 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5665 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5666 tabular.rightLine(cur.selEnd().idx()));
5669 cur.setSelection(false);
5673 case Tabular::UNSET_MULTICOLUMN: {
5674 if (!cur.selection()) {
5675 if (tabular.isMultiColumn(cur.idx()))
5676 tabular.unsetMultiColumn(cur.idx());
5681 case Tabular::MULTICOLUMN: {
5682 if (!cur.selection()) {
5683 if (tabular.isMultiColumn(cur.idx()))
5684 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5686 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5690 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5691 row_type const r = sel_row_start;
5692 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5693 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5696 // If the selection contains at least one singlecol cell
5697 // or multiple multicol cells,
5698 // we assume the user will merge is to a single multicol
5700 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5702 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5706 case Tabular::SET_MULTIROW: {
5707 if (!cur.selection()) {
5708 // just multirow for one single cell
5709 // check whether we are completely in a multirow
5710 if (!tabular.isMultiRow(cur.idx()))
5711 tabular.setMultiRow(cur.idx(), 1,
5712 tabular.bottomLine(cur.idx()),
5713 tabular.getAlignment(cur.idx()));
5716 // we have a selection so this means we just add all this
5717 // cells to form a multirow cell
5718 idx_type const s_start = cur.selBegin().idx();
5719 row_type const row_start = tabular.cellRow(s_start);
5720 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5721 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5722 tabular.bottomLine(cur.selEnd().idx()),
5723 tabular.getAlignment(cur.selEnd().idx()));
5726 cur.setSelection(false);
5730 case Tabular::UNSET_MULTIROW: {
5731 if (!cur.selection()) {
5732 if (tabular.isMultiRow(cur.idx()))
5733 tabular.unsetMultiRow(cur.idx());
5738 case Tabular::MULTIROW: {
5739 if (!cur.selection()) {
5740 if (tabular.isMultiRow(cur.idx()))
5741 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5743 tabularFeatures(cur, Tabular::SET_MULTIROW);
5747 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5748 col_type const c = sel_col_start;
5749 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5750 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5753 // If the selection contains at least one singlerow cell
5754 // or multiple multirow cells,
5755 // we assume the user will merge is to a single multirow
5757 tabularFeatures(cur, Tabular::SET_MULTIROW);
5759 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5763 case Tabular::SET_ALL_LINES:
5765 case Tabular::UNSET_ALL_LINES:
5766 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5767 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5768 idx_type const cell = tabular.cellIndex(r, c);
5769 tabular.setTopLine(cell, setLines);
5770 tabular.setBottomLine(cell, setLines);
5771 tabular.setRightLine(cell, setLines);
5772 tabular.setLeftLine(cell, setLines);
5776 case Tabular::SET_BORDER_LINES:
5777 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5778 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5779 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5781 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5782 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5783 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5787 case Tabular::SET_LONGTABULAR:
5788 tabular.is_long_tabular = true;
5791 case Tabular::UNSET_LONGTABULAR:
5792 for (row_type r = 0; r < tabular.nrows(); ++r) {
5793 if (tabular.ltCaption(r)) {
5794 cur.idx() = tabular.cellIndex(r, 0);
5797 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5800 tabular.is_long_tabular = false;
5803 case Tabular::SET_ROTATE_TABULAR:
5804 tabular.rotate = convert<int>(value);
5807 case Tabular::UNSET_ROTATE_TABULAR:
5811 case Tabular::TOGGLE_ROTATE_TABULAR:
5812 // when pressing the rotate button we default to 90° rotation
5813 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5816 case Tabular::TABULAR_VALIGN_TOP:
5817 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5820 case Tabular::TABULAR_VALIGN_MIDDLE:
5821 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5824 case Tabular::TABULAR_VALIGN_BOTTOM:
5825 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5828 case Tabular::LONGTABULAR_ALIGN_LEFT:
5829 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5832 case Tabular::LONGTABULAR_ALIGN_CENTER:
5833 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5836 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5837 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5842 case Tabular::SET_ROTATE_CELL:
5843 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5844 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5845 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5848 case Tabular::UNSET_ROTATE_CELL:
5849 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5850 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5851 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5854 case Tabular::TOGGLE_ROTATE_CELL:
5856 bool oneNotRotated = oneCellHasRotationState(false,
5857 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5859 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5860 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5861 // when pressing the rotate cell button we default to 90° rotation
5863 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5865 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5870 case Tabular::SET_USEBOX: {
5871 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5872 if (val == tabular.getUsebox(cur.idx()))
5873 val = Tabular::BOX_NONE;
5874 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5875 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5876 tabular.setUsebox(tabular.cellIndex(r, c), val);
5880 case Tabular::UNSET_LTFIRSTHEAD:
5882 case Tabular::SET_LTFIRSTHEAD:
5883 tabular.getRowOfLTFirstHead(row, ltt);
5884 checkLongtableSpecial(ltt, value, flag);
5885 tabular.setLTHead(row, flag, ltt, true);
5888 case Tabular::UNSET_LTHEAD:
5890 case Tabular::SET_LTHEAD:
5891 tabular.getRowOfLTHead(row, ltt);
5892 checkLongtableSpecial(ltt, value, flag);
5893 tabular.setLTHead(row, flag, ltt, false);
5896 case Tabular::UNSET_LTFOOT:
5898 case Tabular::SET_LTFOOT:
5899 tabular.getRowOfLTFoot(row, ltt);
5900 checkLongtableSpecial(ltt, value, flag);
5901 tabular.setLTFoot(row, flag, ltt, false);
5904 case Tabular::UNSET_LTLASTFOOT:
5906 case Tabular::SET_LTLASTFOOT:
5907 tabular.getRowOfLTLastFoot(row, ltt);
5908 checkLongtableSpecial(ltt, value, flag);
5909 tabular.setLTFoot(row, flag, ltt, true);
5912 case Tabular::UNSET_LTNEWPAGE:
5914 case Tabular::SET_LTNEWPAGE:
5915 tabular.setLTNewPage(row, flag);
5918 case Tabular::SET_LTCAPTION: {
5919 if (tabular.ltCaption(row))
5921 cur.idx() = tabular.setLTCaption(row, true);
5924 cur.setSelection(false);
5925 // If a row is set as caption, then also insert
5926 // a caption. Otherwise the LaTeX output is broken.
5927 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5928 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5932 case Tabular::UNSET_LTCAPTION: {
5933 if (!tabular.ltCaption(row))
5935 cur.idx() = tabular.setLTCaption(row, false);
5938 cur.setSelection(false);
5939 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5940 if (lyx::getStatus(fr).enabled())
5945 case Tabular::TOGGLE_LTCAPTION: {
5946 if (tabular.ltCaption(row))
5947 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5949 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5953 case Tabular::SET_BOOKTABS:
5954 tabular.use_booktabs = true;
5957 case Tabular::UNSET_BOOKTABS:
5958 tabular.use_booktabs = false;
5961 case Tabular::SET_TOP_SPACE: {
5963 if (value == "default")
5964 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5965 tabular.row_info[r].top_space_default = true;
5966 else if (value == "none")
5967 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5968 tabular.row_info[r].top_space_default = false;
5969 tabular.row_info[r].top_space = len;
5971 else if (isValidLength(value, &len))
5972 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5973 tabular.row_info[r].top_space_default = false;
5974 tabular.row_info[r].top_space = len;
5979 case Tabular::SET_BOTTOM_SPACE: {
5981 if (value == "default")
5982 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5983 tabular.row_info[r].bottom_space_default = true;
5984 else if (value == "none")
5985 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5986 tabular.row_info[r].bottom_space_default = false;
5987 tabular.row_info[r].bottom_space = len;
5989 else if (isValidLength(value, &len))
5990 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5991 tabular.row_info[r].bottom_space_default = false;
5992 tabular.row_info[r].bottom_space = len;
5997 case Tabular::SET_INTERLINE_SPACE: {
5999 if (value == "default")
6000 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6001 tabular.row_info[r].interline_space_default = true;
6002 else if (value == "none")
6003 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6004 tabular.row_info[r].interline_space_default = false;
6005 tabular.row_info[r].interline_space = len;
6007 else if (isValidLength(value, &len))
6008 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6009 tabular.row_info[r].interline_space_default = false;
6010 tabular.row_info[r].interline_space = len;
6015 case Tabular::SET_DECIMAL_POINT:
6016 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6017 tabular.column_info[c].decimal_point = from_utf8(value);
6020 // dummy stuff just to avoid warnings
6021 case Tabular::LAST_ACTION:
6027 bool InsetTabular::copySelection(Cursor & cur)
6029 if (!cur.selection())
6034 getSelection(cur, rs, re, cs, ce);
6036 paste_tabular.reset(new Tabular(tabular));
6038 for (row_type r = 0; r < rs; ++r)
6039 paste_tabular->deleteRow(0);
6041 row_type const rows = re - rs + 1;
6042 while (paste_tabular->nrows() > rows)
6043 paste_tabular->deleteRow(rows);
6045 for (col_type c = 0; c < cs; ++c)
6046 paste_tabular->deleteColumn(0);
6048 col_type const columns = ce - cs + 1;
6049 while (paste_tabular->ncols() > columns)
6050 paste_tabular->deleteColumn(columns);
6052 paste_tabular->setBuffer(tabular.buffer());
6054 odocstringstream os;
6055 OutputParams const runparams(0);
6056 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6057 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6058 cap::copySelection(cur, os.str());
6060 // mark tabular stack dirty
6061 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6062 // when we (hopefully) have a one-for-all paste mechanism.
6063 // This must be called after cap::copySelection.
6064 dirtyTabularStack(true);
6070 bool InsetTabular::pasteClipboard(Cursor & cur)
6074 col_type actcol = tabular.cellColumn(cur.idx());
6075 row_type actrow = tabular.cellRow(cur.idx());
6077 if (cur.selIsMultiCell()) {
6080 getSelection(cur, actrow, re, actcol, ce);
6083 for (row_type r1 = 0, r2 = actrow;
6084 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6086 for (col_type c1 = 0, c2 = actcol;
6087 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6089 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6090 tabular.isPartOfMultiColumn(r2, c2))
6092 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6096 if (tabular.isPartOfMultiColumn(r2, c2)) {
6100 shared_ptr<InsetTableCell> inset(
6101 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6102 tabular.setCellInset(r2, c2, inset);
6103 // FIXME?: why do we need to do this explicitly? (EL)
6104 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6106 // FIXME: change tracking (MG)
6107 inset->setChange(Change(buffer().params().track_changes ?
6108 Change::INSERTED : Change::UNCHANGED));
6116 void InsetTabular::cutSelection(Cursor & cur)
6118 if (!cur.selection())
6123 getSelection(cur, rs, re, cs, ce);
6124 for (row_type r = rs; r <= re; ++r) {
6125 for (col_type c = cs; c <= ce; ++c) {
6126 shared_ptr<InsetTableCell> t
6127 = cell(tabular.cellIndex(r, c));
6128 if (buffer().params().track_changes)
6129 // FIXME: Change tracking (MG)
6130 t->setChange(Change(Change::DELETED));
6136 // cursor position might be invalid now
6137 if (cur.pit() > cur.lastpit())
6138 cur.pit() = cur.lastpit();
6139 if (cur.pos() > cur.lastpos())
6140 cur.pos() = cur.lastpos();
6141 cur.clearSelection();
6145 bool InsetTabular::isRightToLeft(Cursor & cur) const
6147 // LASSERT: It might be better to abandon this Buffer.
6148 LASSERT(cur.depth() > 1, return false);
6149 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6150 pos_type const parentpos = cur[cur.depth() - 2].pos();
6151 return parentpar.getFontSettings(buffer().params(),
6152 parentpos).language()->rightToLeft();
6156 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6159 LASSERT(stidx <= enidx, return docstring());
6161 col_type const col1 = tabular.cellColumn(stidx);
6162 col_type const col2 = tabular.cellColumn(enidx);
6163 row_type const row1 = tabular.cellRow(stidx);
6164 row_type const row2 = tabular.cellRow(enidx);
6166 for (col_type col = col1; col <= col2; col++)
6167 for (row_type row = row1; row <= row2; row++) {
6172 retval += tabular.cellInset(row, col)->asString(intoInsets);
6178 void InsetTabular::getSelection(Cursor & cur,
6179 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6181 CursorSlice const & beg = cur.selBegin();
6182 CursorSlice const & end = cur.selEnd();
6183 cs = tabular.cellColumn(beg.idx());
6184 ce = tabular.cellColumn(end.idx());
6188 rs = tabular.cellRow(beg.idx());
6189 re = tabular.cellRow(end.idx());
6195 Text * InsetTabular::getText(int idx) const
6197 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6201 void InsetTabular::setChange(Change const & change)
6203 for (idx_type idx = 0; idx < nargs(); ++idx)
6204 cell(idx)->setChange(change);
6208 void InsetTabular::acceptChanges()
6210 for (idx_type idx = 0; idx < nargs(); ++idx)
6211 cell(idx)->acceptChanges();
6215 void InsetTabular::rejectChanges()
6217 for (idx_type idx = 0; idx < nargs(); ++idx)
6218 cell(idx)->rejectChanges();
6222 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6224 return tabular.getPWidth(cell).zero();
6228 bool InsetTabular::forcePlainLayout(idx_type cell) const
6230 return !tabular.getPWidth(cell).zero();
6234 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6237 if (buf.length() <= 0)
6242 col_type maxCols = 1;
6243 size_t const len = buf.length();
6247 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6255 maxCols = max(cols, maxCols);
6261 maxCols = max(cols, maxCols);
6267 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6268 loctab = paste_tabular.get();
6270 dirtyTabularStack(true);
6273 cell = bv.cursor().idx();
6274 ocol = tabular.cellColumn(cell);
6275 row = tabular.cellRow(cell);
6279 idx_type const cells = loctab->numberofcells;
6282 rows = loctab->nrows();
6283 col_type const columns = loctab->ncols();
6285 while (cell < cells && p < len && row < rows &&
6286 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6292 // we can only set this if we are not too far right
6293 if (cols < columns) {
6294 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6295 Font const font = bv.textMetrics(&inset->text()).
6297 inset->setText(buf.substr(op, p - op), font,
6298 buffer().params().track_changes);
6304 // we can only set this if we are not too far right
6305 if (cols < columns) {
6306 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6307 Font const font = bv.textMetrics(&inset->text()).
6309 inset->setText(buf.substr(op, p - op), font,
6310 buffer().params().track_changes);
6315 cell = loctab->cellIndex(row, cols);
6321 // check for the last cell if there is no trailing '\n'
6322 if (cell < cells && op < len) {
6323 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6324 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6325 inset->setText(buf.substr(op, len - op), font,
6326 buffer().params().track_changes);
6332 void InsetTabular::addPreview(DocIterator const & inset_pos,
6333 PreviewLoader & loader) const
6335 DocIterator cell_pos = inset_pos;
6337 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6338 for (row_type r = 0; r < tabular.nrows(); ++r) {
6339 for (col_type c = 0; c < tabular.ncols(); ++c) {
6340 cell_pos.top().idx() = tabular.cellIndex(r, c);
6341 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6347 bool InsetTabular::completionSupported(Cursor const & cur) const
6349 Cursor const & bvCur = cur.bv().cursor();
6350 if (&bvCur.inset() != this)
6352 return cur.text()->completionSupported(cur);
6356 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6358 return completionSupported(cur);
6362 bool InsetTabular::automaticInlineCompletion() const
6364 return lyxrc.completion_inline_text;
6368 bool InsetTabular::automaticPopupCompletion() const
6370 return lyxrc.completion_popup_text;
6374 bool InsetTabular::showCompletionCursor() const
6376 return lyxrc.completion_cursor_text;
6380 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6382 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6386 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6388 if (!completionSupported(cur))
6390 return cur.text()->completionPrefix(cur);
6394 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6396 if (!completionSupported(cur))
6399 return cur.text()->insertCompletion(cur, s, finished);
6403 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6404 Dimension & dim) const
6406 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6407 tm.completionPosAndDim(cur, x, y, dim);
6411 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6413 istringstream data(in);
6415 lex.setStream(data);
6422 if (!lex || token != "tabular") {
6423 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6427 // This is part of the inset proper that is usually swallowed
6428 // by Buffer::readInset
6430 if (!lex || token != "Tabular") {
6431 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6439 string InsetTabular::params2string(InsetTabular const & inset)
6442 data << "tabular" << ' ';
6444 data << "\\end_inset\n";