2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetList.h"
38 #include "LaTeXFeatures.h"
42 #include "MetricsInfo.h"
43 #include "OutputParams.h"
44 #include "output_xhtml.h"
45 #include "Paragraph.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
48 #include "TextClass.h"
49 #include "TextMetrics.h"
51 #include "frontends/Application.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "support/convert.h"
58 #include "support/debug.h"
59 #include "support/docstream.h"
60 #include "support/FileName.h"
61 #include "support/gettext.h"
62 #include "support/lassert.h"
63 #include "support/lstrings.h"
65 #include <boost/scoped_ptr.hpp>
73 using namespace lyx::support;
79 using cap::dirtyTabularStack;
80 using cap::tabularStackDirty;
82 using graphics::PreviewLoader;
84 using frontend::Painter;
85 using frontend::Clipboard;
87 namespace Alert = frontend::Alert;
92 int const ADD_TO_HEIGHT = 2; // in cell
93 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
94 int const default_line_space = 10; // ?
95 int const WIDTH_OF_LINE = 5; // space between double lines
99 boost::scoped_ptr<Tabular> paste_tabular;
102 struct TabularFeature {
103 Tabular::Feature action;
109 TabularFeature tabularFeature[] =
111 // the SET/UNSET actions are used by the table dialog,
112 // the TOGGLE actions by the table toolbar buttons
113 { Tabular::APPEND_ROW, "append-row", false },
114 { Tabular::APPEND_COLUMN, "append-column", false },
115 { Tabular::DELETE_ROW, "delete-row", false },
116 { Tabular::DELETE_COLUMN, "delete-column", false },
117 { Tabular::COPY_ROW, "copy-row", false },
118 { Tabular::COPY_COLUMN, "copy-column", false },
119 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
120 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
121 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
122 { Tabular::MOVE_ROW_UP, "move-row-up", false },
123 { Tabular::SET_LINE_TOP, "set-line-top", true },
124 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
125 { Tabular::SET_LINE_LEFT, "set-line-left", true },
126 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
127 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
128 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
129 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
130 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
131 { Tabular::ALIGN_LEFT, "align-left", false },
132 { Tabular::ALIGN_RIGHT, "align-right", false },
133 { Tabular::ALIGN_CENTER, "align-center", false },
134 { Tabular::ALIGN_BLOCK, "align-block", false },
135 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
136 { Tabular::VALIGN_TOP, "valign-top", false },
137 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
138 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
139 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
140 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
141 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
142 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
143 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
144 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
145 { Tabular::MULTICOLUMN, "multicolumn", false },
146 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
147 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
148 { Tabular::MULTIROW, "multirow", false },
149 { Tabular::SET_MULTIROW, "set-multirow", false },
150 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
151 { Tabular::SET_MROFFSET, "set-mroffset", true },
152 { Tabular::SET_ALL_LINES, "set-all-lines", false },
153 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
154 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
155 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
156 { Tabular::SET_PWIDTH, "set-pwidth", true },
157 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
158 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
159 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
160 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
161 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
162 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
163 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
164 { Tabular::SET_USEBOX, "set-usebox", true },
165 { Tabular::SET_LTHEAD, "set-lthead", true },
166 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
167 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
168 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
169 { Tabular::SET_LTFOOT, "set-ltfoot", true },
170 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
171 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
172 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
173 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
174 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
175 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
176 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
177 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
178 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
179 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
180 { Tabular::SET_BOOKTABS, "set-booktabs", false },
181 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
182 { Tabular::SET_TOP_SPACE, "set-top-space", true },
183 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
184 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
185 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
186 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
187 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
188 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
189 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
190 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
191 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
192 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
193 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
194 { Tabular::LAST_ACTION, "", false }
198 string const tostr(LyXAlignment const & num)
203 case LYX_ALIGN_BLOCK:
207 case LYX_ALIGN_CENTER:
209 case LYX_ALIGN_RIGHT:
211 case LYX_ALIGN_LAYOUT:
213 case LYX_ALIGN_SPECIAL:
215 case LYX_ALIGN_DECIMAL:
222 string const tostr(Tabular::HAlignment const & num)
225 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
227 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
229 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
236 string const tostr(Tabular::VAlignment const & num)
239 case Tabular::LYX_VALIGN_TOP:
241 case Tabular::LYX_VALIGN_MIDDLE:
243 case Tabular::LYX_VALIGN_BOTTOM:
250 string const tostr(Tabular::BoxType const & num)
253 case Tabular::BOX_NONE:
255 case Tabular::BOX_PARBOX:
257 case Tabular::BOX_MINIPAGE:
264 // I would have liked a fromstr template a lot better. (Lgb)
265 bool string2type(string const str, LyXAlignment & num)
268 num = LYX_ALIGN_NONE;
269 else if (str == "block")
270 num = LYX_ALIGN_BLOCK;
271 else if (str == "left")
272 num = LYX_ALIGN_LEFT;
273 else if (str == "center")
274 num = LYX_ALIGN_CENTER;
275 else if (str == "right")
276 num = LYX_ALIGN_RIGHT;
277 else if (str == "decimal")
278 num = LYX_ALIGN_DECIMAL;
285 bool string2type(string const str, Tabular::HAlignment & num)
288 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
289 else if (str == "center" )
290 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
291 else if (str == "right")
292 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
299 bool string2type(string const str, Tabular::VAlignment & num)
302 num = Tabular::LYX_VALIGN_TOP;
303 else if (str == "middle" )
304 num = Tabular::LYX_VALIGN_MIDDLE;
305 else if (str == "bottom")
306 num = Tabular::LYX_VALIGN_BOTTOM;
313 bool string2type(string const str, Tabular::BoxType & num)
316 num = Tabular::BOX_NONE;
317 else if (str == "parbox")
318 num = Tabular::BOX_PARBOX;
319 else if (str == "minipage")
320 num = Tabular::BOX_MINIPAGE;
327 bool string2type(string const str, bool & num)
331 else if (str == "false")
339 bool getTokenValue(string const & str, char const * token, string & ret)
342 size_t token_length = strlen(token);
343 size_t pos = str.find(token);
345 if (pos == string::npos || pos + token_length + 1 >= str.length()
346 || str[pos + token_length] != '=')
348 pos += token_length + 1;
350 if (ch != '"' && ch != '\'') { // only read till next space
354 while (pos < str.length() - 1 && str[++pos] != ch)
361 bool getTokenValue(string const & str, char const * token, docstring & ret)
364 bool const success = getTokenValue(str, token, tmp);
365 ret = from_utf8(tmp);
370 bool getTokenValue(string const & str, char const * token, int & num)
374 if (!getTokenValue(str, token, tmp))
376 num = convert<int>(tmp);
381 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
384 return getTokenValue(str, token, tmp) && string2type(tmp, num);
388 bool getTokenValue(string const & str, char const * token,
389 Tabular::HAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::VAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::BoxType & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token, bool & flag)
414 // set the flag always to false as this should be the default for bools
415 // not in the file-format.
418 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
422 bool getTokenValue(string const & str, char const * token, Length & len)
424 // set the length to be zero() as default as this it should be if not
425 // in the file format.
428 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
432 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
437 if (!getTokenValue(str, token, tmp))
439 if (tmp == "default") {
443 return isValidLength(tmp, &len);
447 void l_getline(istream & is, string & str)
450 while (str.empty()) {
452 if (!str.empty() && str[str.length() - 1] == '\r')
453 str.erase(str.length() - 1);
458 string const write_attribute(string const & name, T const & t)
460 string const s = tostr(t);
461 return s.empty() ? s : " " + name + "=\"" + s + "\"";
465 string const write_attribute(string const & name, string const & t)
467 return t.empty() ? t : " " + name + "=\"" + t + "\"";
472 string const write_attribute(string const & name, docstring const & t)
474 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
479 string const write_attribute(string const & name, bool const & b)
481 // we write only true attribute values so we remove a bit of the
482 // file format bloat for tabulars.
483 return b ? write_attribute(name, convert<string>(b)) : string();
488 string const write_attribute(string const & name, int const & i)
490 // we write only true attribute values so we remove a bit of the
491 // file format bloat for tabulars.
492 return i ? write_attribute(name, convert<string>(i)) : string();
497 string const write_attribute(string const & name, Tabular::idx_type const & i)
499 // we write only true attribute values so we remove a bit of the
500 // file format bloat for tabulars.
501 return i ? write_attribute(name, convert<string>(i)) : string();
506 string const write_attribute(string const & name, Length const & value)
508 // we write only the value if we really have one same reson as above.
509 return value.zero() ? string() : write_attribute(name, value.asString());
515 string const featureAsString(Tabular::Feature action)
517 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
518 if (tabularFeature[i].action == action)
519 return tabularFeature[i].feature;
525 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
527 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
528 for (; dit; dit.forwardChar())
529 if (dit.inTexted() && dit.depth() == 1
530 && dit.paragraph().find(align_d, false, false, dit.pos()))
537 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
539 InsetTableCell tail = InsetTableCell(head);
540 DocIterator const dit = separatorPos(&head, align_d);
543 pit_type const psize = head.paragraphs().front().size();
544 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
545 tail.paragraphs().front().eraseChars(0,
546 dit.pos() < psize ? dit.pos() + 1 : psize, false);
553 /////////////////////////////////////////////////////////////////////
557 /////////////////////////////////////////////////////////////////////
560 Tabular::CellData::CellData(Buffer * buf)
563 multicolumn(Tabular::CELL_NORMAL),
564 multirow(Tabular::CELL_NORMAL),
565 alignment(LYX_ALIGN_CENTER),
566 valignment(LYX_VALIGN_TOP),
576 inset(new InsetTableCell(buf))
578 inset->setBuffer(*buf);
582 Tabular::CellData::CellData(CellData const & cs)
585 multicolumn(cs.multicolumn),
586 multirow(cs.multirow),
587 mroffset(cs.mroffset),
588 alignment(cs.alignment),
589 valignment(cs.valignment),
590 decimal_hoffset(cs.decimal_hoffset),
591 decimal_width(cs.decimal_width),
593 top_line(cs.top_line),
594 bottom_line(cs.bottom_line),
595 left_line(cs.left_line),
596 right_line(cs.right_line),
599 align_special(cs.align_special),
601 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
605 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
611 void Tabular::CellData::swap(CellData & rhs)
613 std::swap(cellno, rhs.cellno);
614 std::swap(width, rhs.width);
615 std::swap(multicolumn, rhs.multicolumn);
616 std::swap(multirow, rhs.multirow);
617 std::swap(mroffset, rhs.mroffset);
618 std::swap(alignment, rhs.alignment);
619 std::swap(valignment, rhs.valignment);
620 std::swap(decimal_hoffset, rhs.decimal_hoffset);
621 std::swap(decimal_width, rhs.decimal_width);
622 std::swap(voffset, rhs.voffset);
623 std::swap(top_line, rhs.top_line);
624 std::swap(bottom_line, rhs.bottom_line);
625 std::swap(left_line, rhs.left_line);
626 std::swap(right_line, rhs.right_line);
627 std::swap(usebox, rhs.usebox);
628 std::swap(rotate, rhs.rotate);
629 std::swap(align_special, rhs.align_special);
630 p_width.swap(rhs.p_width);
631 inset.swap(rhs.inset);
635 Tabular::RowData::RowData()
638 top_space_default(false),
639 bottom_space_default(false),
640 interline_space_default(false),
650 Tabular::ColumnData::ColumnData()
651 : alignment(LYX_ALIGN_CENTER),
652 valignment(LYX_VALIGN_TOP),
658 Tabular::ltType::ltType()
665 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
667 init(buffer, rows_arg, columns_arg);
671 void Tabular::setBuffer(Buffer & buffer)
674 for (row_type i = 0; i < nrows(); ++i)
675 for (col_type j = 0; j < ncols(); ++j)
676 cell_info[i][j].inset->setBuffer(*buffer_);
680 // activates all lines and sets all widths to 0
681 void Tabular::init(Buffer * buf, row_type rows_arg,
682 col_type columns_arg)
685 row_info = row_vector(rows_arg);
686 column_info = column_vector(columns_arg);
687 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
688 row_info.reserve(10);
689 column_info.reserve(10);
690 cell_info.reserve(100);
692 is_long_tabular = false;
693 tabular_valignment = LYX_VALIGN_MIDDLE;
694 tabular_width = Length();
695 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
697 use_booktabs = false;
698 // set silly default lines
699 for (row_type r = 0; r < nrows(); ++r)
700 for (col_type c = 0; c < ncols(); ++c) {
701 cell_info[r][c].inset->setBuffer(*buffer_);
702 cell_info[r][c].top_line = true;
703 cell_info[r][c].left_line = true;
704 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
705 cell_info[r][c].right_line = c == ncols() - 1;
710 void Tabular::deleteRow(row_type const row)
712 // Not allowed to delete last row
716 for (col_type c = 0; c < ncols(); ++c) {
717 // Care about multirow cells
718 if (row + 1 < nrows() &&
719 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
720 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
721 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
724 row_info.erase(row_info.begin() + row);
725 cell_info.erase(cell_info.begin() + row);
730 void Tabular::copyRow(row_type const row)
732 insertRow(row, true);
736 void Tabular::appendRow(row_type row)
738 insertRow(row, false);
742 void Tabular::insertRow(row_type const row, bool copy)
744 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
745 cell_info.insert(cell_info.begin() + row + 1,
746 cell_vector(0, CellData(buffer_)));
748 for (col_type c = 0; c < ncols(); ++c) {
749 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
750 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
751 if (buffer().params().trackChanges)
752 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
753 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
754 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
758 for (col_type c = 0; c < ncols(); ++c) {
759 if (isPartOfMultiRow(row, c))
761 // inherit line settings
762 idx_type const i = cellIndex(row + 1, c);
763 idx_type const j = cellIndex(row, c);
764 setLeftLine(i, leftLine(j));
765 setRightLine(i, rightLine(j));
766 setTopLine(i, topLine(j));
767 if (topLine(j) && bottomLine(j)) {
768 setBottomLine(i, true);
769 setBottomLine(j, false);
771 // mark track changes
772 if (buffer().params().trackChanges)
773 cellInfo(i).inset->setChange(Change(Change::INSERTED));
778 void Tabular::moveColumn(col_type col, ColDirection direction)
780 if (direction == Tabular::LEFT)
783 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().trackChanges) {
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().trackChanges) {
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_));
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(j) && rightLine(j)) {
880 setRightLine(i, true);
881 setRightLine(j, false);
883 if (buffer().params().trackChanges)
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 with
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());
1168 // cur paragraph can become invalid after paragraphs were merged
1169 if (cur.pit() > cur.lastpit())
1170 cur.pit() = cur.lastpit();
1171 // cur position can become invalid after newlines were removed
1172 if (cur.pos() > cur.lastpos())
1173 cur.pos() = cur.lastpos();
1177 bool Tabular::setFixedWidth(row_type r, col_type c)
1179 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1180 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1181 || (multicol && !cell_info[r][c].p_width.zero());
1182 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1187 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1188 Length const & width)
1190 if (!isMultiColumn(cell))
1193 cellInfo(cell).p_width = width;
1194 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1195 // cur paragraph can become invalid after paragraphs were merged
1196 if (cur.pit() > cur.lastpit())
1197 cur.pit() = cur.lastpit();
1198 // cur position can become invalid after newlines were removed
1199 if (cur.pos() > cur.lastpos())
1200 cur.pos() = cur.lastpos();
1205 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1207 cellInfo(cell).mroffset = mroffset;
1212 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1213 Tabular::Feature what)
1215 if (what == SET_SPECIAL_MULTICOLUMN)
1216 cellInfo(cell).align_special = special;
1218 column_info[cellColumn(cell)].align_special = special;
1222 void Tabular::setTopLine(idx_type i, bool line)
1224 cellInfo(i).top_line = line;
1228 void Tabular::setBottomLine(idx_type i, bool line)
1230 cellInfo(i).bottom_line = line;
1234 void Tabular::setLeftLine(idx_type cell, bool line)
1236 cellInfo(cell).left_line = line;
1240 void Tabular::setRightLine(idx_type cell, bool line)
1242 cellInfo(cell).right_line = line;
1245 bool Tabular::rowTopLine(row_type r) const
1247 bool all_rows_set = true;
1248 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1249 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1250 return all_rows_set;
1254 bool Tabular::rowBottomLine(row_type r) const
1256 bool all_rows_set = true;
1257 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1258 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1259 return all_rows_set;
1263 bool Tabular::columnLeftLine(col_type c) const
1270 for (row_type r = 0; r < nrows(); ++r) {
1271 idx_type const i = cellIndex(r, c);
1272 if (c == cellColumn(i)) {
1274 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1275 if (cellInfo(i).left_line || right)
1279 return 2 * nrows_left >= total;
1283 bool Tabular::columnRightLine(col_type c) const
1288 int nrows_right = 0;
1290 for (row_type r = 0; r < nrows(); ++r) {
1291 idx_type i = cellIndex(r, c);
1292 if (c == cellColumn(i) + columnSpan(i) - 1) {
1294 bool left = (c + 1 < ncols()
1295 && cellInfo(cellIndex(r, c + 1)).left_line)
1296 || c + 1 == ncols();
1297 if (cellInfo(i).right_line && left)
1301 return 2 * nrows_right >= total;
1305 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1307 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1308 return cellInfo(cell).alignment;
1310 return column_info[cellColumn(cell)].alignment;
1315 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1317 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1318 return cellInfo(cell).valignment;
1319 return column_info[cellColumn(cell)].valignment;
1323 Length const Tabular::getPWidth(idx_type cell) const
1325 if (isMultiColumn(cell))
1326 return cellInfo(cell).p_width;
1327 return column_info[cellColumn(cell)].p_width;
1331 Length const Tabular::getMROffset(idx_type cell) const
1333 return cellInfo(cell).mroffset;
1337 int Tabular::textHOffset(idx_type cell) const
1339 // the LaTeX Way :-(
1340 int x = WIDTH_OF_LINE;
1342 int const w = cellWidth(cell) - cellInfo(cell).width;
1344 switch (getAlignment(cell)) {
1345 case LYX_ALIGN_CENTER:
1348 case LYX_ALIGN_RIGHT:
1351 case LYX_ALIGN_DECIMAL: {
1352 // we center when no decimal point
1353 if (cellInfo(cell).decimal_width == 0) {
1357 col_type const c = cellColumn(cell);
1358 int max_dhoffset = 0;
1359 for(row_type r = 0; r < row_info.size() ; ++r) {
1360 idx_type const i = cellIndex(r, c);
1361 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1362 && cellInfo(i).decimal_width != 0)
1363 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1365 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1368 // LYX_ALIGN_LEFT: nothing :-)
1376 int Tabular::textVOffset(idx_type cell) const
1378 int voffset = cellInfo(cell).voffset;
1379 if (isMultiRow(cell)) {
1380 row_type const row = cellRow(cell);
1381 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1387 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1390 idx_type const numcells = numberOfCellsInRow(row);
1391 // we check against numcells to make sure we do not crash if all the
1392 // cells are multirow (bug #7535), but in that case our return value
1393 // is really invalid, i.e., it is NOT the first cell in the row. but
1394 // i do not know what to do here. (rgh)
1395 while (c < numcells - 1
1396 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1398 return cell_info[row][c].cellno;
1402 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1404 col_type c = ncols() - 1;
1405 // of course we check against 0 so we don't crash. but we have the same
1406 // problem as in the previous routine: if all the cells are part of a
1407 // multirow or part of a multi column, then our return value is invalid.
1409 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1410 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1412 return cell_info[row][c].cellno;
1416 Tabular::row_type Tabular::cellRow(idx_type cell) const
1418 if (cell >= numberofcells)
1422 return rowofcell[cell];
1426 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1428 if (cell >= numberofcells)
1432 return columnofcell[cell];
1436 void Tabular::write(ostream & os) const
1440 << write_attribute("version", 3)
1441 << write_attribute("rows", nrows())
1442 << write_attribute("columns", ncols())
1444 // global longtable options
1446 << write_attribute("rotate", convert<string>(rotate))
1447 << write_attribute("booktabs", use_booktabs)
1448 << write_attribute("islongtable", is_long_tabular)
1449 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1450 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1451 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1452 << write_attribute("headTopDL", endhead.topDL)
1453 << write_attribute("headBottomDL", endhead.bottomDL)
1454 << write_attribute("footTopDL", endfoot.topDL)
1455 << write_attribute("footBottomDL", endfoot.bottomDL)
1456 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1457 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1458 << write_attribute("lastFootEmpty", endlastfoot.empty);
1459 // longtables cannot be aligned vertically
1460 if (!is_long_tabular) {
1461 os << write_attribute("tabularvalignment", tabular_valignment);
1462 os << write_attribute("tabularwidth", tabular_width);
1464 if (is_long_tabular)
1465 os << write_attribute("longtabularalignment",
1466 longtabular_alignment);
1468 for (col_type c = 0; c < ncols(); ++c) {
1470 << write_attribute("alignment", column_info[c].alignment);
1471 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1472 os << write_attribute("decimal_point", column_info[c].decimal_point);
1473 os << write_attribute("valignment", column_info[c].valignment)
1474 << write_attribute("width", column_info[c].p_width.asString())
1475 << write_attribute("special", column_info[c].align_special)
1478 for (row_type r = 0; r < nrows(); ++r) {
1479 static const string def("default");
1481 if (row_info[r].top_space_default)
1482 os << write_attribute("topspace", def);
1484 os << write_attribute("topspace", row_info[r].top_space);
1485 if (row_info[r].bottom_space_default)
1486 os << write_attribute("bottomspace", def);
1488 os << write_attribute("bottomspace", row_info[r].bottom_space);
1489 if (row_info[r].interline_space_default)
1490 os << write_attribute("interlinespace", def);
1492 os << write_attribute("interlinespace", row_info[r].interline_space);
1493 os << write_attribute("endhead", row_info[r].endhead)
1494 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1495 << write_attribute("endfoot", row_info[r].endfoot)
1496 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1497 << write_attribute("newpage", row_info[r].newpage)
1498 << write_attribute("caption", row_info[r].caption)
1500 for (col_type c = 0; c < ncols(); ++c) {
1502 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1503 << write_attribute("multirow", cell_info[r][c].multirow)
1504 << write_attribute("mroffset", cell_info[r][c].mroffset)
1505 << write_attribute("alignment", cell_info[r][c].alignment)
1506 << write_attribute("valignment", cell_info[r][c].valignment)
1507 << write_attribute("topline", cell_info[r][c].top_line)
1508 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1509 << write_attribute("leftline", cell_info[r][c].left_line)
1510 << write_attribute("rightline", cell_info[r][c].right_line)
1511 << write_attribute("rotate", cell_info[r][c].rotate)
1512 << write_attribute("usebox", cell_info[r][c].usebox)
1513 << write_attribute("width", cell_info[r][c].p_width)
1514 << write_attribute("special", cell_info[r][c].align_special)
1516 os << "\\begin_inset ";
1517 cell_info[r][c].inset->write(os);
1518 os << "\n\\end_inset\n"
1523 os << "</lyxtabular>\n";
1527 void Tabular::read(Lexer & lex)
1530 istream & is = lex.getStream();
1532 l_getline(is, line);
1533 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1534 LASSERT(false, return);
1538 if (!getTokenValue(line, "version", version))
1540 LATTEST(version >= 2);
1543 if (!getTokenValue(line, "rows", rows_arg))
1546 if (!getTokenValue(line, "columns", columns_arg))
1548 init(buffer_, rows_arg, columns_arg);
1549 l_getline(is, line);
1550 if (!prefixIs(line, "<features")) {
1551 lyxerr << "Wrong tabular format (expected <features ...> got"
1552 << line << ')' << endl;
1555 getTokenValue(line, "rotate", rotate);
1556 getTokenValue(line, "booktabs", use_booktabs);
1557 getTokenValue(line, "islongtable", is_long_tabular);
1558 getTokenValue(line, "tabularvalignment", tabular_valignment);
1559 getTokenValue(line, "tabularwidth", tabular_width);
1560 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1561 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1562 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1563 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1564 getTokenValue(line, "headTopDL", endhead.topDL);
1565 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1566 getTokenValue(line, "footTopDL", endfoot.topDL);
1567 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1568 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1569 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1570 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1572 for (col_type c = 0; c < ncols(); ++c) {
1574 if (!prefixIs(line,"<column")) {
1575 lyxerr << "Wrong tabular format (expected <column ...> got"
1576 << line << ')' << endl;
1579 getTokenValue(line, "alignment", column_info[c].alignment);
1580 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1581 getTokenValue(line, "valignment", column_info[c].valignment);
1582 getTokenValue(line, "width", column_info[c].p_width);
1583 getTokenValue(line, "special", column_info[c].align_special);
1586 for (row_type i = 0; i < nrows(); ++i) {
1587 l_getline(is, line);
1588 if (!prefixIs(line, "<row")) {
1589 lyxerr << "Wrong tabular format (expected <row ...> got"
1590 << line << ')' << endl;
1593 getTokenValue(line, "topspace", row_info[i].top_space,
1594 row_info[i].top_space_default);
1595 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1596 row_info[i].bottom_space_default);
1597 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1598 row_info[i].interline_space_default);
1599 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1600 getTokenValue(line, "endhead", row_info[i].endhead);
1601 getTokenValue(line, "endfoot", row_info[i].endfoot);
1602 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1603 getTokenValue(line, "newpage", row_info[i].newpage);
1604 getTokenValue(line, "caption", row_info[i].caption);
1605 for (col_type j = 0; j < ncols(); ++j) {
1606 l_getline(is, line);
1607 if (!prefixIs(line, "<cell")) {
1608 lyxerr << "Wrong tabular format (expected <cell ...> got"
1609 << line << ')' << endl;
1612 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1613 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1614 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1615 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1616 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1617 getTokenValue(line, "topline", cell_info[i][j].top_line);
1618 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1619 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1620 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1621 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1622 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1623 getTokenValue(line, "width", cell_info[i][j].p_width);
1625 getTokenValue(line, "special", cell_info[i][j].align_special);
1626 l_getline(is, line);
1627 if (prefixIs(line, "\\begin_inset")) {
1628 cell_info[i][j].inset->setBuffer(*buffer_);
1629 cell_info[i][j].inset->read(lex);
1630 l_getline(is, line);
1632 if (!prefixIs(line, "</cell>")) {
1633 lyxerr << "Wrong tabular format (expected </cell> got"
1634 << line << ')' << endl;
1638 l_getline(is, line);
1639 if (!prefixIs(line, "</row>")) {
1640 lyxerr << "Wrong tabular format (expected </row> got"
1641 << line << ')' << endl;
1645 while (!prefixIs(line, "</lyxtabular>")) {
1646 l_getline(is, line);
1652 bool Tabular::isMultiColumn(idx_type cell) const
1654 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1655 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1659 bool Tabular::hasMultiColumn(col_type c) const
1661 for (row_type r = 0; r < nrows(); ++r) {
1662 if (isMultiColumn(cellIndex(r, c)))
1669 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1671 return cell_info[cellRow(cell)][cellColumn(cell)];
1675 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1676 bool const right_border)
1678 idx_type const col = cellColumn(cell);
1679 idx_type const row = cellRow(cell);
1680 for (idx_type i = 0; i < number; ++i)
1681 unsetMultiRow(cellIndex(row, col + i));
1683 // unsetting of multirow may have invalidated cell index
1684 cell = cellIndex(row, col);
1685 CellData & cs = cellInfo(cell);
1686 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1687 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1688 cs.alignment = column_info[col].alignment;
1689 setRightLine(cell, right_border);
1691 for (idx_type i = 1; i < number; ++i) {
1692 CellData & cs1 = cellInfo(cell + i);
1693 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1694 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1702 bool Tabular::isMultiRow(idx_type cell) const
1704 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1705 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1708 bool Tabular::hasMultiRow(row_type r) const
1710 for (col_type c = 0; c < ncols(); ++c) {
1711 if (isMultiRow(cellIndex(r, c)))
1717 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1718 bool const bottom_border)
1720 idx_type const col = cellColumn(cell);
1721 idx_type const row = cellRow(cell);
1722 for (idx_type i = 0; i < number; ++i)
1723 unsetMultiColumn(cellIndex(row + i, col));
1725 // unsetting of multirow may have invalidated cell index
1726 cell = cellIndex(row, col);
1727 CellData & cs = cellInfo(cell);
1728 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1729 cs.valignment = LYX_VALIGN_MIDDLE;
1730 // the horizontal alignment of multirow cells can only
1731 // be changed for the whole table row,
1732 // support changing this only for the multirow cell can be done via
1734 // this feature would be a fileformat change
1735 // until LyX supports this, use the deault alignment of multirow
1737 cs.alignment = LYX_ALIGN_LEFT;
1739 // set the bottom line of the last selected cell
1740 setBottomLine(cell, bottom_border);
1742 for (idx_type i = 1; i < number; ++i) {
1743 CellData & cs1 = cell_info[row + i][col];
1744 cs1.multirow = CELL_PART_OF_MULTIROW;
1745 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1753 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1755 row_type const row = cellRow(cell);
1756 col_type const col = cellColumn(cell);
1758 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1765 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1767 col_type const column = cellColumn(cell);
1768 col_type row = cellRow(cell) + 1;
1769 while (row < nrows() && isPartOfMultiRow(row, column))
1772 return row - cellRow(cell);
1776 void Tabular::unsetMultiColumn(idx_type cell)
1778 if (!isMultiColumn(cell))
1781 row_type const row = cellRow(cell);
1782 col_type const col = cellColumn(cell);
1783 row_type const span = columnSpan(cell);
1784 for (col_type c = 0; c < span; ++c) {
1785 // in the table dialog the lines are set in every case
1786 // when unsetting a multicolumn this leads to an additional right
1787 // line for every cell that was part of the former multicolumn cell,
1788 // except if the cell is in the last column
1789 // therefore remove this line
1790 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1791 && (col + c) < (col + span - 1))
1792 cell_info[row][col + c].right_line = false;
1793 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1799 void Tabular::unsetMultiRow(idx_type cell)
1801 if (!isMultiRow(cell))
1804 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1805 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1806 row_type const row = cellRow(cell);
1807 col_type const col = cellColumn(cell);
1808 row_type const span = rowSpan(cell);
1809 for (row_type r = 0; r < span; ++r)
1810 cell_info[row + r][col].multirow = CELL_NORMAL;
1815 void Tabular::setRotateCell(idx_type cell, int value)
1817 cellInfo(cell).rotate = value;
1821 int Tabular::getRotateCell(idx_type cell) const
1823 return cellInfo(cell).rotate;
1827 bool Tabular::needRotating() const
1831 for (row_type r = 0; r < nrows(); ++r)
1832 for (col_type c = 0; c < ncols(); ++c)
1833 if (cell_info[r][c].rotate != 0)
1839 bool Tabular::isLastCell(idx_type cell) const
1841 if (cell + 1 < numberofcells)
1847 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1849 if (cellRow(cell) == 0)
1852 col_type const col = cellColumn(cell);
1853 row_type r = cellRow(cell) - 1;
1854 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1857 return cell_info[r][col].cellno;
1861 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1863 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1864 if (nextrow < nrows())
1865 return cell_info[nextrow][cellColumn(cell)].cellno;
1870 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1872 LASSERT(column != npos && column < ncols(), column = 0);
1873 LASSERT(row != npos && row < nrows(), row = 0);
1874 return cell_info[row][column].cellno;
1878 void Tabular::setUsebox(idx_type cell, BoxType type)
1880 cellInfo(cell).usebox = type;
1884 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1885 // adds line breaks, see bug 4886.
1886 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1888 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1889 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1891 if (cellInfo(cell).usebox > 1)
1892 return cellInfo(cell).usebox;
1893 return useParbox(cell);
1898 // This are functions used for the longtable support
1900 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1906 row_info[row].endfirsthead = flag;
1910 row_info[row].endhead = flag;
1915 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1918 hd.set = haveLTHead();
1919 return row_info[row].endhead;
1923 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1926 hd.set = haveLTFirstHead();
1927 return row_info[row].endfirsthead;
1931 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1937 row_info[row].endlastfoot = flag;
1941 row_info[row].endfoot = flag;
1946 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1949 fd.set = haveLTFoot();
1950 return row_info[row].endfoot;
1954 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1957 fd.set = haveLTLastFoot();
1958 return row_info[row].endlastfoot;
1962 void Tabular::setLTNewPage(row_type row, bool what)
1964 row_info[row].newpage = what;
1968 bool Tabular::getLTNewPage(row_type row) const
1970 return row_info[row].newpage;
1974 bool Tabular::haveLTHead(bool withcaptions) const
1976 if (!is_long_tabular)
1978 for (row_type i = 0; i < nrows(); ++i)
1979 if (row_info[i].endhead &&
1980 (withcaptions || !row_info[i].caption))
1986 bool Tabular::haveLTFirstHead(bool withcaptions) const
1988 if (!is_long_tabular || endfirsthead.empty)
1990 for (row_type r = 0; r < nrows(); ++r)
1991 if (row_info[r].endfirsthead &&
1992 (withcaptions || !row_info[r].caption))
1998 bool Tabular::haveLTFoot(bool withcaptions) const
2000 if (!is_long_tabular)
2002 for (row_type r = 0; r < nrows(); ++r)
2003 if (row_info[r].endfoot &&
2004 (withcaptions || !row_info[r].caption))
2010 bool Tabular::haveLTLastFoot(bool withcaptions) const
2012 if (!is_long_tabular || endlastfoot.empty)
2014 for (row_type r = 0; r < nrows(); ++r)
2015 if (row_info[r].endlastfoot &&
2016 (withcaptions || !row_info[r].caption))
2022 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2024 idx_type i = getFirstCellInRow(row);
2026 setMultiColumn(i, numberOfCellsInRow(row), false);
2027 setTopLine(i, false);
2028 setBottomLine(i, false);
2029 setLeftLine(i, false);
2030 setRightLine(i, false);
2031 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2032 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2033 setLTHead(row, true, endfirsthead, true);
2034 row_info[row].endfirsthead = true;
2037 unsetMultiColumn(i);
2038 // When unsetting a caption row, also all existing
2039 // captions in this row must be dissolved.
2041 row_info[row].caption = what;
2046 bool Tabular::ltCaption(row_type row) const
2048 return row_info[row].caption;
2052 bool Tabular::haveLTCaption(CaptionType captiontype) const
2054 if (!is_long_tabular)
2056 for (row_type r = 0; r < nrows(); ++r) {
2057 if (row_info[r].caption) {
2058 switch (captiontype) {
2059 case CAPTION_FIRSTHEAD:
2060 if (row_info[r].endfirsthead)
2064 if (row_info[r].endhead)
2068 if (row_info[r].endfoot)
2071 case CAPTION_LASTFOOT:
2072 if (row_info[r].endlastfoot)
2084 // end longtable support functions
2086 void Tabular::setRowAscent(row_type row, int height)
2088 if (row >= nrows() || row_info[row].ascent == height)
2090 row_info[row].ascent = height;
2094 void Tabular::setRowDescent(row_type row, int height)
2096 if (row >= nrows() || row_info[row].descent == height)
2098 row_info[row].descent = height;
2102 int Tabular::rowAscent(row_type row) const
2104 LASSERT(row < nrows(), row = 0);
2105 return row_info[row].ascent;
2109 int Tabular::rowDescent(row_type row) const
2111 LASSERT(row < nrows(), row = 0);
2112 return row_info[row].descent;
2116 int Tabular::height() const
2119 for (row_type row = 0; row < nrows(); ++row)
2120 height += rowAscent(row) + rowDescent(row) +
2126 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2128 LASSERT(row < nrows(), return false);
2129 LASSERT(column < ncols(), return false);
2130 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2134 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2136 LASSERT(row < nrows(), return false);
2137 LASSERT(column < ncols(), return false);
2138 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2142 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2144 // we only output complete row lines and the 1st row here, the rest
2145 // is done in Tabular::TeXBottomHLine(...)
2147 // get for each column the topline (if any)
2148 vector<bool> topline;
2150 for (col_type c = 0; c < ncols(); ++c) {
2151 topline.push_back(topLine(cellIndex(row, c)));
2152 // If cell is part of a multirow and not the first cell of the
2153 // multirow, no line must be drawn.
2155 if (isMultiRow(cellIndex(row, c))
2156 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2162 // do nothing if empty first row, or incomplete row line after
2163 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2166 // only output complete row lines and the 1st row's clines
2167 if (nset == ncols()) {
2169 os << (row == 0 ? "\\toprule " : "\\midrule ");
2173 } else if (row == 0) {
2174 for (col_type c = 0; c < ncols(); ++c) {
2176 col_type offset = 0;
2177 for (col_type j = 0 ; j < c; ++j)
2178 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2181 //babel makes the "-" character an active one, so we have to suppress this here
2182 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2183 if (lang == "slovak" || lang == "czech")
2184 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2185 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2187 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2189 col_type cstart = c;
2190 for ( ; c < ncols() && topline[c]; ++c) {}
2192 for (col_type j = cstart ; j < c ; ++j)
2193 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2196 os << c + offset << "} ";
2204 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2206 // we output bottomlines of row r and the toplines of row r+1
2207 // if the latter do not span the whole tabular
2209 // get the bottomlines of row r, and toplines in next row
2210 bool lastrow = row == nrows() - 1;
2211 vector<bool> bottomline, topline;
2212 bool nextrowset = true;
2213 for (col_type c = 0; c < ncols(); ++c) {
2214 bottomline.push_back(bottomLine(cellIndex(row, c)));
2215 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2216 // If cell is part of a multirow and not the last cell of the
2217 // multirow, no line must be drawn.
2219 if (isMultiRow(cellIndex(row, c))
2220 && isMultiRow(cellIndex(row + 1, c))
2221 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2222 bottomline[c] = false;
2225 nextrowset &= topline[c];
2228 // combine this row's bottom lines and next row's toplines if necessary
2230 for (col_type c = 0; c < ncols(); ++c) {
2232 bottomline[c] = bottomline[c] || topline[c];
2237 // do nothing if empty, OR incomplete row line with a topline in next row
2238 if (nset == 0 || (nextrowset && nset != ncols()))
2241 if (nset == ncols()) {
2243 os << (lastrow ? "\\bottomrule" : "\\midrule");
2247 for (col_type c = 0; c < ncols(); ++c) {
2248 if (bottomline[c]) {
2249 col_type offset = 0;
2250 for (col_type j = 0 ; j < c; ++j)
2251 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2254 //babel makes the "-" character an active one, so we have to suppress this here
2255 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2256 if (lang == "slovak" || lang == "czech")
2257 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2258 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2260 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2262 col_type cstart = c;
2263 for ( ; c < ncols() && bottomline[c]; ++c) {}
2265 for (col_type j = cstart ; j < c ; ++j)
2266 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2269 os << c + offset << "} ";
2277 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2278 bool & ismulticol, bool & ismultirow) const
2280 row_type const r = cellRow(cell);
2281 if (is_long_tabular && row_info[r].caption)
2284 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2285 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2286 // figure out how to set the lines
2287 // we always set double lines to the right of the cell
2288 col_type const c = cellColumn(cell);
2289 col_type const nextcol = c + columnSpan(cell);
2290 bool colright = columnRightLine(c);
2291 bool colleft = columnLeftLine(c);
2292 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2293 bool nextcellleft = nextcol < ncols()
2294 && leftLine(cellIndex(r, nextcol));
2295 bool coldouble = colright && nextcolleft;
2296 bool celldouble = rightLine(cell) && nextcellleft;
2298 ismulticol = isMultiColumn(cell)
2299 || (c == 0 && colleft != leftLine(cell))
2300 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2301 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2302 || (coldouble != celldouble);
2304 // we center in multicol when no decimal point
2305 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2306 docstring const align_d = column_info[c].decimal_point;
2307 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2311 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2312 int latexcolspan = columnSpan(cell);
2313 for(col_type col = c; col < c + columnSpan(cell); ++col)
2314 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2318 os << "\\multicolumn{" << latexcolspan << "}{";
2319 if (c ==0 && leftLine(cell))
2321 if (!cellInfo(cell).align_special.empty()) {
2322 os << cellInfo(cell).align_special;
2324 if (!getPWidth(cell).zero()) {
2326 case LYX_ALIGN_LEFT:
2327 os << ">{\\raggedright}";
2329 case LYX_ALIGN_RIGHT:
2330 os << ">{\\raggedleft}";
2332 case LYX_ALIGN_CENTER:
2333 os << ">{\\centering}";
2339 case LYX_VALIGN_TOP:
2342 case LYX_VALIGN_MIDDLE:
2345 case LYX_VALIGN_BOTTOM:
2350 << from_ascii(getPWidth(cell).asLatexString())
2354 case LYX_ALIGN_LEFT:
2357 case LYX_ALIGN_RIGHT:
2364 } // end if else !getPWidth
2365 } // end if else !cellinfo_of_cell
2366 if (rightLine(cell) || nextcellleft)
2369 // add extra vertical line if we want a double one
2372 } // end if ismulticol
2374 // we only need code for the first multirow cell
2375 ismultirow = isMultiRow(cell);
2377 os << "\\multirow{" << rowSpan(cell) << "}{";
2378 if (!getPWidth(cell).zero())
2379 os << from_ascii(getPWidth(cell).asLatexString());
2381 // we need to set a default value
2384 if (!getMROffset(cell).zero())
2385 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2387 } // end if ismultirow
2389 if (getRotateCell(cell) != 0)
2390 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2392 if (getUsebox(cell) == BOX_PARBOX) {
2395 case LYX_VALIGN_TOP:
2398 case LYX_VALIGN_MIDDLE:
2401 case LYX_VALIGN_BOTTOM:
2405 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2407 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2408 os << "\\begin{minipage}[";
2410 case LYX_VALIGN_TOP:
2413 case LYX_VALIGN_MIDDLE:
2416 case LYX_VALIGN_BOTTOM:
2420 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2426 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2427 bool ismulticol, bool ismultirow) const
2429 row_type const r = cellRow(cell);
2430 if (is_long_tabular && row_info[r].caption)
2434 if (getUsebox(cell) == BOX_PARBOX)
2436 else if (getUsebox(cell) == BOX_MINIPAGE)
2437 os << breakln << "\\end{minipage}";
2438 if (getRotateCell(cell) != 0)
2439 os << breakln << "\\end{turn}";
2447 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2448 OutputParams const & runparams) const
2450 if (!is_long_tabular)
2454 // the caption must be output before the headers
2455 if (haveLTCaption()) {
2456 for (row_type r = 0; r < nrows(); ++r) {
2457 if (row_info[r].caption)
2458 TeXRow(os, r, runparams);
2461 // output first header info
2462 if (haveLTFirstHead()) {
2463 if (endfirsthead.topDL)
2465 for (row_type r = 0; r < nrows(); ++r) {
2466 if (row_info[r].endfirsthead)
2467 TeXRow(os, r, runparams);
2469 if (endfirsthead.bottomDL)
2471 os << "\\endfirsthead\n";
2473 // output header info
2475 if (endfirsthead.empty && !haveLTFirstHead())
2476 os << "\\endfirsthead\n";
2479 for (row_type r = 0; r < nrows(); ++r) {
2480 if (row_info[r].endhead)
2481 TeXRow(os, r, runparams);
2483 if (endhead.bottomDL)
2485 os << "\\endhead\n";
2487 // output footer info
2491 for (row_type r = 0; r < nrows(); ++r) {
2492 if (row_info[r].endfoot)
2493 TeXRow(os, r, runparams);
2495 if (endfoot.bottomDL)
2497 os << "\\endfoot\n";
2498 if (endlastfoot.empty && !haveLTLastFoot())
2499 os << "\\endlastfoot\n";
2501 // output lastfooter info
2502 if (haveLTLastFoot()) {
2503 if (endlastfoot.topDL)
2505 for (row_type r = 0; r < nrows(); ++r) {
2506 if (row_info[r].endlastfoot)
2507 TeXRow(os, r, runparams);
2509 if (endlastfoot.bottomDL)
2511 os << "\\endlastfoot\n";
2516 bool Tabular::isValidRow(row_type row) const
2518 if (!is_long_tabular)
2520 return !row_info[row].endhead && !row_info[row].endfirsthead
2521 && !row_info[row].endfoot && !row_info[row].endlastfoot
2522 && !row_info[row].caption;
2526 void Tabular::TeXRow(otexstream & os, row_type row,
2527 OutputParams const & runparams) const
2529 idx_type cell = cellIndex(row, 0);
2530 shared_ptr<InsetTableCell> inset = cellInset(cell);
2531 Paragraph const & par = inset->paragraphs().front();
2532 string const lang = par.getParLanguage(buffer().params())->lang();
2534 //output the top line
2535 TeXTopHLine(os, row, lang);
2537 if (row_info[row].top_space_default) {
2539 os << "\\addlinespace\n";
2541 os << "\\noalign{\\vskip\\doublerulesep}\n";
2542 } else if(!row_info[row].top_space.zero()) {
2544 os << "\\addlinespace["
2545 << from_ascii(row_info[row].top_space.asLatexString())
2548 os << "\\noalign{\\vskip"
2549 << from_ascii(row_info[row].top_space.asLatexString())
2553 bool ismulticol = false;
2554 bool ismultirow = false;
2555 for (col_type c = 0; c < ncols(); ++c) {
2556 if (isPartOfMultiColumn(row, c))
2559 cell = cellIndex(row, c);
2561 if (isPartOfMultiRow(row, c)
2562 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2563 if (cell != getLastCellInRow(row))
2568 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2569 shared_ptr<InsetTableCell> inset = cellInset(cell);
2571 Paragraph const & par = inset->paragraphs().front();
2572 bool rtl = par.isRTL(buffer().params())
2574 && getPWidth(cell).zero()
2575 && !runparams.use_polyglossia;
2579 par.getParLanguage(buffer().params())->lang();
2580 if (lang == "farsi")
2582 else if (lang == "arabic_arabi")
2584 // currently, remaining RTL languages are
2585 // arabic_arabtex and hebrew
2589 // pass to the OutputParams that we are in a cell and
2590 // which alignment we have set.
2591 // InsetNewline needs this context information.
2592 OutputParams newrp(runparams);
2593 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2594 ? OutputParams::PLAIN
2595 : OutputParams::ALIGNED;
2597 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2598 // copy cell and split in 2
2599 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2600 head.setBuffer(buffer());
2601 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2603 dit.push_back(CursorSlice(head));
2604 head.setMacrocontextPositionRecursive(dit);
2605 bool hassep = false;
2606 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2607 head.latex(os, newrp);
2610 tail.setBuffer(head.buffer());
2612 dit.push_back(CursorSlice(tail));
2613 tail.setMacrocontextPositionRecursive(dit);
2614 tail.latex(os, newrp);
2616 } else if (!isPartOfMultiRow(row, c)) {
2617 if (!runparams.nice)
2618 os.texrow().start(par.id(), 0);
2619 inset->latex(os, newrp);
2622 runparams.encoding = newrp.encoding;
2626 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2627 if (cell != getLastCellInRow(row)) { // not last cell in row
2634 os << "\\tabularnewline";
2635 if (row_info[row].bottom_space_default) {
2637 os << "\\addlinespace";
2639 os << "[\\doublerulesep]";
2640 } else if (!row_info[row].bottom_space.zero()) {
2642 os << "\\addlinespace";
2644 << from_ascii(row_info[row].bottom_space.asLatexString())
2649 //output the bottom line
2650 TeXBottomHLine(os, row, lang);
2652 if (row_info[row].interline_space_default) {
2654 os << "\\addlinespace\n";
2656 os << "\\noalign{\\vskip\\doublerulesep}\n";
2657 } else if (!row_info[row].interline_space.zero()) {
2659 os << "\\addlinespace["
2660 << from_ascii(row_info[row].interline_space.asLatexString())
2663 os << "\\noalign{\\vskip"
2664 << from_ascii(row_info[row].interline_space.asLatexString())
2670 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2672 bool const is_tabular_star = !tabular_width.zero();
2674 //+---------------------------------------------------------------------
2675 //+ first the opening preamble +
2676 //+---------------------------------------------------------------------
2679 if (runparams.lastid != -1)
2680 os.texrow().start(runparams.lastid, runparams.lastpos);
2683 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2685 if (is_long_tabular) {
2686 os << "\\begin{longtable}";
2687 switch (longtabular_alignment) {
2688 case LYX_LONGTABULAR_ALIGN_LEFT:
2691 case LYX_LONGTABULAR_ALIGN_CENTER:
2693 case LYX_LONGTABULAR_ALIGN_RIGHT:
2698 if (is_tabular_star)
2699 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2701 os << "\\begin{tabular}";
2702 switch (tabular_valignment) {
2703 case LYX_VALIGN_TOP:
2706 case LYX_VALIGN_MIDDLE:
2708 case LYX_VALIGN_BOTTOM:
2716 if (is_tabular_star)
2717 os << "@{\\extracolsep{\\fill}}";
2719 for (col_type c = 0; c < ncols(); ++c) {
2720 if (columnLeftLine(c))
2722 if (!column_info[c].align_special.empty()) {
2723 os << column_info[c].align_special;
2725 if (!column_info[c].p_width.zero()) {
2726 switch (column_info[c].alignment) {
2727 case LYX_ALIGN_LEFT:
2728 os << ">{\\raggedright}";
2730 case LYX_ALIGN_RIGHT:
2731 os << ">{\\raggedleft}";
2733 case LYX_ALIGN_CENTER:
2734 os << ">{\\centering}";
2736 case LYX_ALIGN_NONE:
2737 case LYX_ALIGN_BLOCK:
2738 case LYX_ALIGN_LAYOUT:
2739 case LYX_ALIGN_SPECIAL:
2740 case LYX_ALIGN_DECIMAL:
2744 switch (column_info[c].valignment) {
2745 case LYX_VALIGN_TOP:
2748 case LYX_VALIGN_MIDDLE:
2751 case LYX_VALIGN_BOTTOM:
2756 << from_ascii(column_info[c].p_width.asLatexString())
2759 switch (column_info[c].alignment) {
2760 case LYX_ALIGN_LEFT:
2763 case LYX_ALIGN_RIGHT:
2766 case LYX_ALIGN_DECIMAL:
2767 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2773 } // end if else !column_info[i].p_width
2774 } // end if else !column_info[i].align_special
2775 if (columnRightLine(c))
2780 TeXLongtableHeaderFooter(os, runparams);
2782 //+---------------------------------------------------------------------
2783 //+ the single row and columns (cells) +
2784 //+---------------------------------------------------------------------
2786 for (row_type r = 0; r < nrows(); ++r) {
2787 if (isValidRow(r)) {
2788 TeXRow(os, r, runparams);
2789 if (is_long_tabular && row_info[r].newpage)
2790 os << "\\newpage\n";
2794 //+---------------------------------------------------------------------
2795 //+ the closing of the tabular +
2796 //+---------------------------------------------------------------------
2798 if (is_long_tabular)
2799 os << "\\end{longtable}";
2801 if (is_tabular_star)
2802 os << "\\end{tabular*}";
2804 os << "\\end{tabular}";
2808 os << breakln << "\\end{turn}";
2812 int Tabular::docbookRow(odocstream & os, row_type row,
2813 OutputParams const & runparams) const
2816 idx_type cell = getFirstCellInRow(row);
2819 for (col_type c = 0; c < ncols(); ++c) {
2820 if (isPartOfMultiColumn(row, c))
2823 os << "<entry align=\"";
2824 switch (getAlignment(cell)) {
2825 case LYX_ALIGN_LEFT:
2828 case LYX_ALIGN_RIGHT:
2836 os << "\" valign=\"";
2837 switch (getVAlignment(cell)) {
2838 case LYX_VALIGN_TOP:
2841 case LYX_VALIGN_BOTTOM:
2844 case LYX_VALIGN_MIDDLE:
2849 if (isMultiColumn(cell)) {
2850 os << " namest=\"col" << c << "\" ";
2851 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2855 ret += cellInset(cell)->docbook(os, runparams);
2864 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2868 //+---------------------------------------------------------------------
2869 //+ first the opening preamble +
2870 //+---------------------------------------------------------------------
2872 os << "<tgroup cols=\"" << ncols()
2873 << "\" colsep=\"1\" rowsep=\"1\">\n";
2875 for (col_type c = 0; c < ncols(); ++c) {
2876 os << "<colspec colname=\"col" << c << "\" align=\"";
2877 switch (column_info[c].alignment) {
2878 case LYX_ALIGN_LEFT:
2881 case LYX_ALIGN_RIGHT:
2889 if (runparams.flavor == OutputParams::XML)
2895 //+---------------------------------------------------------------------
2896 //+ Long Tabular case +
2897 //+---------------------------------------------------------------------
2899 // output caption info
2900 // The caption flag wins over head/foot
2901 if (haveLTCaption()) {
2902 os << "<caption>\n";
2904 for (row_type r = 0; r < nrows(); ++r) {
2905 if (row_info[r].caption) {
2906 ret += docbookRow(os, r, runparams);
2909 os << "</caption>\n";
2912 // output header info
2913 if (haveLTHead(false) || haveLTFirstHead(false)) {
2916 for (row_type r = 0; r < nrows(); ++r) {
2917 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2918 !row_info[r].caption) {
2919 ret += docbookRow(os, r, runparams);
2925 // output footer info
2926 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2929 for (row_type r = 0; r < nrows(); ++r) {
2930 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2931 !row_info[r].caption) {
2932 ret += docbookRow(os, r, runparams);
2939 //+---------------------------------------------------------------------
2940 //+ the single row and columns (cells) +
2941 //+---------------------------------------------------------------------
2945 for (row_type r = 0; r < nrows(); ++r) {
2946 if (isValidRow(r)) {
2947 ret += docbookRow(os, r, runparams);
2952 //+---------------------------------------------------------------------
2953 //+ the closing of the tabular +
2954 //+---------------------------------------------------------------------
2963 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2964 OutputParams const & runparams, bool header) const
2967 string const celltag = header ? "th" : "td";
2968 idx_type cell = getFirstCellInRow(row);
2970 xs << html::StartTag("tr");
2971 for (col_type c = 0; c < ncols(); ++c) {
2972 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2977 switch (getAlignment(cell)) {
2978 case LYX_ALIGN_LEFT:
2981 case LYX_ALIGN_RIGHT:
2989 attr << " valign='";
2990 switch (getVAlignment(cell)) {
2991 case LYX_VALIGN_TOP:
2994 case LYX_VALIGN_BOTTOM:
2997 case LYX_VALIGN_MIDDLE:
3002 if (isMultiColumn(cell))
3003 attr << " colspan='" << columnSpan(cell) << "'";
3004 else if (isMultiRow(cell))
3005 attr << " rowspan='" << rowSpan(cell) << "'";
3007 xs << html::StartTag(celltag, attr.str()) << html::CR();
3008 ret += cellInset(cell)->xhtml(xs, runparams);
3009 xs << html::EndTag(celltag) << html::CR();
3012 xs << html::EndTag("tr");
3017 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3021 if (is_long_tabular) {
3022 // we'll wrap it in a div, so as to deal with alignment
3024 switch (longtabular_alignment) {
3025 case LYX_LONGTABULAR_ALIGN_LEFT:
3028 case LYX_LONGTABULAR_ALIGN_CENTER:
3031 case LYX_LONGTABULAR_ALIGN_RIGHT:
3035 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3037 // The caption flag wins over head/foot
3038 if (haveLTCaption()) {
3039 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3041 for (row_type r = 0; r < nrows(); ++r)
3042 if (row_info[r].caption)
3043 ret += xhtmlRow(xs, r, runparams);
3044 xs << html::EndTag("div") << html::CR();
3048 xs << html::StartTag("table") << html::CR();
3050 // output header info
3051 bool const havefirsthead = haveLTFirstHead(false);
3052 // if we have a first head, then we are going to ignore the
3053 // headers for the additional pages, since there aren't any
3054 // in XHTML. this test accomplishes that.
3055 bool const havehead = !havefirsthead && haveLTHead(false);
3056 if (havehead || havefirsthead) {
3057 xs << html::StartTag("thead") << html::CR();
3058 for (row_type r = 0; r < nrows(); ++r) {
3059 if (((havefirsthead && row_info[r].endfirsthead) ||
3060 (havehead && row_info[r].endhead)) &&
3061 !row_info[r].caption) {
3062 ret += xhtmlRow(xs, r, runparams, true);
3065 xs << html::EndTag("thead") << html::CR();
3067 // output footer info
3068 bool const havelastfoot = haveLTLastFoot(false);
3070 bool const havefoot = !havelastfoot && haveLTFoot(false);
3071 if (havefoot || havelastfoot) {
3072 xs << html::StartTag("tfoot") << html::CR();
3073 for (row_type r = 0; r < nrows(); ++r) {
3074 if (((havelastfoot && row_info[r].endlastfoot) ||
3075 (havefoot && row_info[r].endfoot)) &&
3076 !row_info[r].caption) {
3077 ret += xhtmlRow(xs, r, runparams);
3080 xs << html::EndTag("tfoot") << html::CR();
3083 xs << html::StartTag("tbody") << html::CR();
3084 for (row_type r = 0; r < nrows(); ++r) {
3085 if (isValidRow(r)) {
3086 ret += xhtmlRow(xs, r, runparams);
3089 xs << html::EndTag("tbody")
3091 << html::EndTag("table")
3093 if (is_long_tabular)
3094 xs << html::EndTag("div") << html::CR();
3099 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3100 vector<unsigned int> const & clen) const
3102 idx_type const fcell = getFirstCellInRow(row);
3103 idx_type const n = numberOfCellsInRow(row) + fcell;
3106 for (idx_type i = fcell; i < n; ++i) {
3116 for (idx_type i = fcell; i < n; ++i) {
3127 col_type column = cellColumn(i);
3128 int len = clen[column];
3129 while (column < ncols() - 1
3130 && isPartOfMultiColumn(row, ++column))
3131 len += clen[column] + 4;
3132 os << docstring(len, ch);
3147 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3148 vector<unsigned int> const & clen) const
3150 idx_type const fcell = getFirstCellInRow(row);
3151 idx_type const n = numberOfCellsInRow(row) + fcell;
3154 for (idx_type i = fcell; i < n; ++i) {
3155 if (bottomLine(i)) {
3164 for (idx_type i = fcell; i < n; ++i) {
3165 if (bottomLine(i)) {
3175 col_type column = cellColumn(i);
3176 int len = clen[column];
3177 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3178 len += clen[column] + 4;
3179 os << docstring(len, ch);
3180 if (bottomLine(i)) {
3194 void Tabular::plaintextPrintCell(odocstringstream & os,
3195 OutputParams const & runparams,
3196 idx_type cell, row_type row, col_type column,
3197 vector<unsigned int> const & clen,
3198 bool onlydata, size_t max_length) const
3200 odocstringstream sstr;
3201 cellInset(cell)->plaintext(sstr, runparams, max_length);
3213 unsigned int len1 = sstr.str().length();
3214 unsigned int len2 = clen[column];
3215 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3216 len2 += clen[column] + 4;
3219 switch (getAlignment(cell)) {
3221 case LYX_ALIGN_LEFT:
3224 case LYX_ALIGN_RIGHT:
3228 case LYX_ALIGN_CENTER:
3234 os << docstring(len1, ' ') << sstr.str()
3235 << docstring(len2, ' ');
3237 if (rightLine(cell))
3244 void Tabular::plaintext(odocstringstream & os,
3245 OutputParams const & runparams, int const depth,
3246 bool onlydata, char_type delim, size_t max_length) const
3248 // first calculate the width of the single columns
3249 vector<unsigned int> clen(ncols());
3252 // first all non multicolumn cells!
3253 for (col_type c = 0; c < ncols(); ++c) {
3255 for (row_type r = 0; r < nrows(); ++r) {
3256 idx_type cell = cellIndex(r, c);
3257 if (isMultiColumn(cell))
3259 odocstringstream sstr;
3260 cellInset(cell)->plaintext(sstr, runparams, max_length);
3261 if (clen[c] < sstr.str().length())
3262 clen[c] = sstr.str().length();
3265 // then all multicolumn cells!
3266 for (col_type c = 0; c < ncols(); ++c) {
3267 for (row_type r = 0; r < nrows(); ++r) {
3268 idx_type cell = cellIndex(r, c);
3269 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3271 odocstringstream sstr;
3272 cellInset(cell)->plaintext(sstr, runparams, max_length);
3273 int len = int(sstr.str().length());
3274 idx_type const n = columnSpan(cell);
3275 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3277 if (len > int(clen[c + n - 1]))
3278 clen[c + n - 1] = len;
3283 for (row_type r = 0; r < nrows(); ++r) {
3284 if (!onlydata && plaintextTopHLine(os, r, clen))
3285 os << docstring(depth * 2, ' ');
3286 for (col_type c = 0; c < ncols(); ++c) {
3287 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3289 if (onlydata && c > 0)
3290 // we don't use operator<< for single UCS4 character.
3291 // see explanation in docstream.h
3293 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3295 if (os.str().size() > max_length)
3300 os << docstring(depth * 2, ' ');
3301 if (plaintextBottomHLine(os, r, clen))
3302 os << docstring(depth * 2, ' ');
3304 if (os.str().size() > max_length)
3310 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3312 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3316 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3317 col_type column) const
3319 return cell_info[row][column].inset;
3323 void Tabular::setCellInset(row_type row, col_type column,
3324 shared_ptr<InsetTableCell> ins) const
3326 CellData & cd = cell_info[row][column];
3331 void Tabular::validate(LaTeXFeatures & features) const
3333 features.require("NeedTabularnewline");
3335 features.require("booktabs");
3336 if (is_long_tabular)
3337 features.require("longtable");
3339 features.require("rotating");
3340 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3341 if (isMultiRow(cell))
3342 features.require("multirow");
3343 if (getVAlignment(cell) != LYX_VALIGN_TOP
3344 || !getPWidth(cell).zero())
3345 features.require("array");
3346 cellInset(cell)->validate(features);
3351 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3353 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3354 ParagraphList::const_iterator cit = parlist.begin();
3355 ParagraphList::const_iterator end = parlist.end();
3357 for (; cit != end; ++cit)
3358 for (int i = 0; i < cit->size(); ++i)
3359 if (cit->isNewline(i))
3366 /////////////////////////////////////////////////////////////////////
3370 /////////////////////////////////////////////////////////////////////
3372 InsetTableCell::InsetTableCell(Buffer * buf)
3373 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3374 contentAlign(LYX_ALIGN_CENTER)
3378 bool InsetTableCell::forcePlainLayout(idx_type) const
3380 return !isFixedWidth;
3384 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3386 return isFixedWidth;
3390 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3391 FuncStatus & status) const
3393 bool enabled = true;
3394 switch (cmd.action()) {
3396 enabled = !forcePlainLayout();
3398 case LFUN_LAYOUT_PARAGRAPH:
3399 enabled = allowParagraphCustomization();
3402 case LFUN_MATH_DISPLAY:
3403 if (!hasFixedWidth()) {
3408 return InsetText::getStatus(cur, cmd, status);
3410 status.setEnabled(enabled);
3414 docstring InsetTableCell::asString(bool intoInsets)
3417 if (paragraphs().empty())
3419 ParagraphList::const_iterator it = paragraphs().begin();
3420 ParagraphList::const_iterator en = paragraphs().end();
3422 for (; it != en; ++it) {
3427 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3433 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3436 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3437 return InsetText::xhtml(xs, rp);
3442 /////////////////////////////////////////////////////////////////////
3446 /////////////////////////////////////////////////////////////////////
3448 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3450 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3451 rowselect_(false), colselect_(false)
3456 InsetTabular::InsetTabular(InsetTabular const & tab)
3457 : Inset(tab), tabular(tab.tabular), scx_(0)
3462 InsetTabular::~InsetTabular()
3464 hideDialogs("tabular", this);
3468 void InsetTabular::setBuffer(Buffer & buf)
3470 tabular.setBuffer(buf);
3471 Inset::setBuffer(buf);
3475 bool InsetTabular::insetAllowed(InsetCode code) const
3480 case MATHMACRO_CODE:
3485 return tabular.is_long_tabular;
3493 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3495 return tabular.is_long_tabular &&
3496 (newtype == "Standard" || newtype == "LongTableNoNumber");
3500 void InsetTabular::write(ostream & os) const
3502 os << "Tabular" << endl;
3507 string InsetTabular::contextMenu(BufferView const &, int, int) const
3509 // FIXME: depending on the selection state,
3510 // we could offer a different menu.
3511 return cell(0)->contextMenuName() + ";" + contextMenuName();
3515 string InsetTabular::contextMenuName() const
3517 return "context-tabular";
3521 void InsetTabular::read(Lexer & lex)
3523 //bool const old_format = (lex.getString() == "\\LyXTable");
3531 string token = lex.getString();
3532 while (lex && token != "\\end_inset") {
3534 token = lex.getString();
3537 lex.printError("Missing \\end_inset at this point. ");
3541 int InsetTabular::rowFromY(Cursor & cur, int y) const
3543 // top y coordinate of tabular
3544 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3546 for (; r < tabular.nrows() && y > h; ++r)
3547 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3548 + tabular.interRowSpace(r);
3554 int InsetTabular::columnFromX(Cursor & cur, int x) const
3556 // left x coordinate of tabular
3557 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3559 for (; c < tabular.ncols() && x > w; ++c)
3560 w += tabular.cellWidth(c);
3565 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3567 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3568 // mi.base.textwidth << "\n";
3569 LBUFERR(mi.base.bv);
3571 for (row_type r = 0; r < tabular.nrows(); ++r) {
3574 for (col_type c = 0; c < tabular.ncols(); ++c) {
3575 if (tabular.isPartOfMultiColumn(r, c)
3576 || tabular.isPartOfMultiRow(r, c))
3577 // multicolumn or multirow cell, but not first one
3579 idx_type const cell = tabular.cellIndex(r, c);
3582 Length const p_width = tabular.getPWidth(cell);
3583 if (!p_width.zero())
3584 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3585 tabular.cellInset(cell)->metrics(m, dim);
3586 if (!p_width.zero())
3587 dim.wid = m.base.textwidth;
3588 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3589 + tabular.interColumnSpace(cell);
3591 // FIXME(?): do we need a second metrics call?
3592 TextMetrics const & tm =
3593 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3595 // determine horizontal offset because of decimal align (if necessary)
3596 int decimal_width = 0;
3597 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3598 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3599 tail.setBuffer(tabular.buffer());
3600 // we need to set macrocontext position everywhere
3601 // otherwise we crash with nested insets (e.g. footnotes)
3602 // after decimal point
3603 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3605 dit.push_back(CursorSlice(tail));
3606 tail.setMacrocontextPositionRecursive(dit);
3608 // remove text leading decimal point
3609 docstring const align_d = tabular.column_info[c].decimal_point;
3610 dit = separatorPos(&tail, align_d);
3612 pit_type const psize = tail.paragraphs().front().size();
3614 tail.paragraphs().front().eraseChars(0,
3615 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3617 tail.metrics(m, dim1);
3618 decimal_width = dim1.width();
3621 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3622 tabular.cell_info[r][c].decimal_width = decimal_width;
3624 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3625 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3626 int const lastpardes = tm.last().second->descent()
3627 + TEXT_TO_INSET_OFFSET;
3629 switch (tabular.getVAlignment(cell)) {
3630 case Tabular::LYX_VALIGN_TOP:
3632 case Tabular::LYX_VALIGN_MIDDLE:
3633 offset = -(dim.des - lastpardes)/2;
3635 case Tabular::LYX_VALIGN_BOTTOM:
3636 offset = -(dim.des - lastpardes);
3639 tabular.cell_info[r][c].voffset = offset;
3640 maxasc = max(maxasc, dim.asc - offset);
3641 maxdes = max(maxdes, dim.des + offset);
3643 int const top_space = tabular.row_info[r].top_space_default ?
3644 default_line_space :
3645 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3646 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3647 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3648 default_line_space :
3649 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3650 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3653 // for top-alignment the first horizontal table line must be exactly at
3654 // the position of the base line of the surrounding text line
3655 // for bottom alignment, the same is for the last table line
3656 switch (tabular.tabular_valignment) {
3657 case Tabular::LYX_VALIGN_BOTTOM:
3658 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3660 case Tabular::LYX_VALIGN_MIDDLE:
3661 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3663 case Tabular::LYX_VALIGN_TOP:
3664 offset_valign_ = tabular.rowAscent(0);
3668 tabular.updateColumnWidths();
3669 dim.asc = tabular.rowAscent(0) - offset_valign_;
3670 dim.des = tabular.height() - dim.asc;
3671 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3675 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3678 if (&cur.inset() == this && cur.selection()) {
3679 if (cur.selIsMultiCell()) {
3682 getSelection(cur, rs, re, cs, ce);
3684 idx_type const cell = tabular.cellIndex(row, col);
3685 col_type const cspan = tabular.columnSpan(cell);
3686 row_type const rspan = tabular.rowSpan(cell);
3687 if (col + cspan - 1 >= cs && col <= ce
3688 && row + rspan - 1 >= rs && row <= re)
3691 if (col == tabular.cellColumn(cur.idx())
3692 && row == tabular.cellRow(cur.idx())) {
3693 CursorSlice const & beg = cur.selBegin();
3694 CursorSlice const & end = cur.selEnd();
3696 if ((end.lastpos() > 0 || end.lastpit() > 0)
3697 && end.pos() == end.lastpos() && beg.pos() == 0
3698 && end.pit() == end.lastpit() && beg.pit() == 0)
3706 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3708 x += scx_ + ADD_TO_TABULAR_WIDTH;
3710 BufferView * bv = pi.base.bv;
3711 Cursor & cur = pi.base.bv->cursor();
3714 // FIXME: As the full background is painted in drawBackground(),
3715 // we have no choice but to do a full repaint for the Text cells.
3716 pi.full_repaint = true;
3718 bool const original_selection_state = pi.selected;
3721 first_visible_cell = Tabular::npos;
3723 int yy = y + offset_valign_;
3724 for (row_type r = 0; r < tabular.nrows(); ++r) {
3726 for (col_type c = 0; c < tabular.ncols(); ++c) {
3727 if (tabular.isPartOfMultiColumn(r, c))
3730 idx = tabular.cellIndex(r, c);
3732 if (tabular.isPartOfMultiRow(r, c)) {
3733 nx += tabular.cellWidth(idx);
3737 if (first_visible_cell == Tabular::npos)
3738 first_visible_cell = idx;
3740 pi.selected |= isCellSelected(cur, r, c);
3741 int const cx = nx + tabular.textHOffset(idx);
3742 int const cy = yy + tabular.textVOffset(idx);
3743 // Cache the Inset position.
3744 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3745 cell(idx)->draw(pi, cx, cy);
3746 drawCellLines(pi, nx, yy, r, idx);
3747 nx += tabular.cellWidth(idx);
3748 pi.selected = original_selection_state;
3751 if (r + 1 < tabular.nrows())
3752 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3753 + tabular.interRowSpace(r + 1);
3758 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3760 x += scx_ + ADD_TO_TABULAR_WIDTH;
3761 y += offset_valign_ - tabular.rowAscent(0);
3762 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3763 pi.backgroundColor(this));
3767 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3769 Cursor & cur = pi.base.bv->cursor();
3772 x += scx_ + ADD_TO_TABULAR_WIDTH;
3774 if (!cur.selection())
3776 if (&cur.inset() != this)
3781 bool const full_cell_selected = isCellSelected(cur,
3782 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3784 if (cur.selIsMultiCell() || full_cell_selected) {
3785 for (row_type r = 0; r < tabular.nrows(); ++r) {
3787 for (col_type c = 0; c < tabular.ncols(); ++c) {
3788 if (tabular.isPartOfMultiColumn(r, c))
3791 idx_type const cell = tabular.cellIndex(r, c);
3793 if (tabular.isPartOfMultiRow(r, c)) {
3794 xx += tabular.cellWidth(cell);
3797 int const w = tabular.cellWidth(cell);
3798 int const h = tabular.cellHeight(cell);
3799 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3800 if (isCellSelected(cur, r, c))
3801 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3804 if (r + 1 < tabular.nrows())
3805 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3806 + tabular.interRowSpace(r + 1);
3810 // FIXME: This code has no effect because InsetTableCell does not handle
3811 // drawSelection other than the trivial implementation in Inset.
3813 // x += cellXPos(cur.idx());
3814 // x += tabular.textHOffset(cur.idx());
3815 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3820 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3821 row_type row, idx_type cell) const
3823 y -= tabular.rowAscent(row);
3824 int const w = tabular.cellWidth(cell);
3825 int const h = tabular.cellHeight(cell);
3826 Color const linecolor = pi.textColor(Color_tabularline);
3827 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3830 bool drawline = tabular.topLine(cell)
3831 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3832 pi.pain.line(x, y, x + w, y,
3833 drawline ? linecolor : gridcolor,
3834 drawline ? Painter::line_solid : Painter::line_onoffdash);
3837 drawline = tabular.bottomLine(cell);
3838 pi.pain.line(x, y + h, x + w, y + h,
3839 drawline ? linecolor : gridcolor,
3840 drawline ? Painter::line_solid : Painter::line_onoffdash);
3843 col_type const col = tabular.cellColumn(cell);
3844 drawline = tabular.leftLine(cell)
3845 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3846 pi.pain.line(x, y, x, y + h,
3847 drawline ? linecolor : gridcolor,
3848 drawline ? Painter::line_solid : Painter::line_onoffdash);
3851 x -= tabular.interColumnSpace(cell);
3852 col_type next_cell_col = col + 1;
3853 while (next_cell_col < tabular.ncols()
3854 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3856 drawline = tabular.rightLine(cell)
3857 || (next_cell_col < tabular.ncols()
3858 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3859 pi.pain.line(x + w, y, x + w, y + h,
3860 drawline ? linecolor : gridcolor,
3861 drawline ? Painter::line_solid : Painter::line_onoffdash);
3865 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3867 //lyxerr << "InsetTabular::edit: " << this << endl;
3869 cur.setSelection(false);
3872 if (isRightToLeft(cur))
3873 cur.idx() = tabular.getLastCellInRow(0);
3879 if (isRightToLeft(cur))
3880 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3882 cur.idx() = tabular.numberofcells - 1;
3884 cur.pos() = cur.lastpos(); // FIXME crude guess
3886 cur.setCurrentFont();
3887 // FIXME: this accesses the position cache before it is initialized
3889 //cur.bv().fitCursor();
3893 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3895 // In a longtable, tell captions what the current float is
3896 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3897 string const saveflt = cnts.current_float();
3898 if (tabular.is_long_tabular)
3899 cnts.current_float("table");
3901 ParIterator it2 = it;
3903 size_t const end = it2.nargs();
3904 for ( ; it2.idx() < end; it2.top().forwardIdx())
3905 buffer().updateBuffer(it2, utype);
3908 if (tabular.is_long_tabular)
3909 cnts.current_float(saveflt);
3913 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active) const
3915 DocIterator dit = cpit;
3917 size_t const end = dit.nargs();
3918 for ( ; dit.idx() < end; dit.top().forwardIdx())
3919 cell(dit.idx())->addToToc(dit, output_active);
3923 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3925 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3926 << "\n cur:" << cur);
3927 CursorSlice sl = cur.top();
3928 Cursor & bvcur = cur.bv().cursor();
3930 FuncCode const act = cmd.action();
3934 case LFUN_MOUSE_PRESS: {
3935 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3937 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3938 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3939 row_type r = rowFromY(cur, cmd.y());
3940 cur.idx() = tabular.getFirstCellInRow(r);
3943 cur.idx() = tabular.getLastCellInRow(r);
3944 cur.pos() = cur.lastpos();
3945 cur.setSelection(true);
3951 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3952 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3953 || cmd.y() > y0 + tabular.height()) {
3954 col_type c = columnFromX(cur, cmd.x());
3955 cur.idx() = tabular.cellIndex(0, c);
3958 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3959 cur.pos() = cur.lastpos();
3960 cur.setSelection(true);
3965 // do not reset cursor/selection if we have selected
3966 // some cells (bug 2715).
3967 if (cmd.button() == mouse_button::button3
3968 && &bvcur.selBegin().inset() == this
3969 && bvcur.selIsMultiCell())
3972 // Let InsetTableCell do it
3973 cell(cur.idx())->dispatch(cur, cmd);
3976 case LFUN_MOUSE_MOTION:
3977 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3978 if (cmd.button() == mouse_button::button1) {
3979 // only accept motions to places not deeper nested than the real anchor
3980 if (!bvcur.realAnchor().hasPart(cur)) {
3984 // select (additional) row
3986 row_type r = rowFromY(cur, cmd.y());
3987 cur.idx() = tabular.getLastCellInRow(r);
3988 // we need to reset the cursor's pit and pos now, as the old ones
3989 // may no longer be valid.
3992 bvcur.setCursor(cur);
3993 bvcur.setSelection(true);
3996 // select (additional) column
3998 col_type c = columnFromX(cur, cmd.x());
3999 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4000 // we need to reset the cursor's pit and pos now, as the old ones
4001 // may no longer be valid.
4004 bvcur.setCursor(cur);
4005 bvcur.setSelection(true);
4008 // only update if selection changes
4009 if (bvcur.idx() == cur.idx() &&
4010 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4011 cur.noScreenUpdate();
4012 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4013 bvcur.setCursor(cur);
4014 bvcur.setSelection(true);
4015 // if this is a multicell selection, we just set the cursor to
4016 // the beginning of the cell's text.
4017 if (bvcur.selIsMultiCell()) {
4018 bvcur.pit() = bvcur.lastpit();
4019 bvcur.pos() = bvcur.lastpos();
4024 case LFUN_MOUSE_RELEASE:
4029 case LFUN_CELL_BACKWARD:
4031 cur.setSelection(false);
4034 case LFUN_CELL_FORWARD:
4036 cur.setSelection(false);
4039 case LFUN_CHAR_FORWARD_SELECT:
4040 case LFUN_CHAR_FORWARD:
4041 case LFUN_CHAR_BACKWARD_SELECT:
4042 case LFUN_CHAR_BACKWARD:
4043 case LFUN_CHAR_RIGHT_SELECT:
4044 case LFUN_CHAR_RIGHT:
4045 case LFUN_CHAR_LEFT_SELECT:
4046 case LFUN_CHAR_LEFT:
4047 case LFUN_WORD_FORWARD:
4048 case LFUN_WORD_FORWARD_SELECT:
4049 case LFUN_WORD_BACKWARD:
4050 case LFUN_WORD_BACKWARD_SELECT:
4051 case LFUN_WORD_RIGHT:
4052 case LFUN_WORD_RIGHT_SELECT:
4053 case LFUN_WORD_LEFT:
4054 case LFUN_WORD_LEFT_SELECT: {
4055 // determine whether we move to next or previous cell, where to enter
4056 // the new cell from, and which command to "finish" (i.e., exit the
4059 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4060 FuncCode finish_lfun;
4062 if (act == LFUN_CHAR_FORWARD
4063 || act == LFUN_CHAR_FORWARD_SELECT
4064 || act == LFUN_WORD_FORWARD
4065 || act == LFUN_WORD_FORWARD_SELECT) {
4067 finish_lfun = LFUN_FINISHED_FORWARD;
4069 else if (act == LFUN_CHAR_BACKWARD
4070 || act == LFUN_CHAR_BACKWARD_SELECT
4071 || act == LFUN_WORD_BACKWARD
4072 || act == LFUN_WORD_BACKWARD_SELECT) {
4074 finish_lfun = LFUN_FINISHED_BACKWARD;
4076 // LEFT or RIGHT commands --- the interpretation will depend on the
4077 // table's direction.
4079 bool const right = act == LFUN_CHAR_RIGHT
4080 || act == LFUN_CHAR_RIGHT_SELECT
4081 || act == LFUN_WORD_RIGHT
4082 || act == LFUN_WORD_RIGHT_SELECT;
4083 next_cell = isRightToLeft(cur) != right;
4085 if (lyxrc.visual_cursor)
4086 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4088 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4091 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4092 || act == LFUN_CHAR_BACKWARD_SELECT
4093 || act == LFUN_CHAR_RIGHT_SELECT
4094 || act == LFUN_CHAR_LEFT_SELECT
4095 || act == LFUN_WORD_FORWARD_SELECT
4096 || act == LFUN_WORD_RIGHT_SELECT
4097 || act == LFUN_WORD_BACKWARD_SELECT
4098 || act == LFUN_WORD_LEFT_SELECT;
4100 // If we have a multicell selection or we're
4101 // not doing some LFUN_*_SELECT thing anyway...
4102 if (!cur.selIsMultiCell() || !select) {
4103 col_type const c = tabular.cellColumn(cur.idx());
4104 row_type const r = tabular.cellRow(cur.idx());
4105 // Are we trying to select the whole cell and is the whole cell
4106 // not yet selected?
4107 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4108 ((next_cell && cur.pit() == cur.lastpit()
4109 && cur.pos() == cur.lastpos())
4110 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4112 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4114 // ...try to dispatch to the cell's inset.
4115 cell(cur.idx())->dispatch(cur, cmd);
4117 // When we already have a selection we want to select the whole cell
4118 // before going to the next cell.
4119 if (select_whole && !empty_cell){
4120 getText(cur.idx())->selectAll(cur);
4122 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4126 // FIXME: When we support the selection of an empty cell, remove
4127 // the !empty_cell from this condition. For now we jump to the next
4128 // cell if the current cell is empty.
4129 if (cur.result().dispatched() && !empty_cell)
4133 // move to next/prev cell, as appropriate
4134 // note that we will always do this if we're selecting and we have
4135 // a multicell selection
4136 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4137 << " cell from: " << int(entry_from));
4139 moveNextCell(cur, entry_from);
4141 movePrevCell(cur, entry_from);
4142 // if we're exiting the table, call the appropriate FINISHED lfun
4143 if (sl == cur.top())
4144 cmd = FuncRequest(finish_lfun);
4148 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4153 case LFUN_DOWN_SELECT:
4155 if (!(cur.selection() && cur.selIsMultiCell()))
4156 cell(cur.idx())->dispatch(cur, cmd);
4158 cur.dispatched(); // override the cell's decision
4159 if (sl == cur.top()) {
4160 // if our Text didn't do anything to the cursor
4161 // then we try to put the cursor into the cell below
4162 // setting also the right targetX.
4163 cur.selHandle(act == LFUN_DOWN_SELECT);
4164 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4165 int const xtarget = cur.targetX();
4166 // WARNING: Once cur.idx() has been reset, the cursor is in
4167 // an inconsistent state until pos() has been set. Be careful
4168 // what you do with it!
4169 cur.idx() = tabular.cellBelow(cur.idx());
4171 TextMetrics const & tm =
4172 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4173 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4174 cur.setCurrentFont();
4177 if (sl == cur.top()) {
4178 // we trick it to go to forward after leaving the
4180 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4183 if (cur.selIsMultiCell()) {
4184 cur.pit() = cur.lastpit();
4185 cur.pos() = cur.lastpos();
4186 cur.setCurrentFont();
4187 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4190 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4193 case LFUN_UP_SELECT:
4195 if (!(cur.selection() && cur.selIsMultiCell()))
4196 cell(cur.idx())->dispatch(cur, cmd);
4197 cur.dispatched(); // override the cell's decision
4198 if (sl == cur.top()) {
4199 // if our Text didn't do anything to the cursor
4200 // then we try to put the cursor into the cell above
4201 // setting also the right targetX.
4202 cur.selHandle(act == LFUN_UP_SELECT);
4203 if (tabular.cellRow(cur.idx()) != 0) {
4204 int const xtarget = cur.targetX();
4205 // WARNING: Once cur.idx() has been reset, the cursor is in
4206 // an inconsistent state until pos() has been set. Be careful
4207 // what you do with it!
4208 cur.idx() = tabular.cellAbove(cur.idx());
4209 cur.pit() = cur.lastpit();
4210 Text const * text = cell(cur.idx())->getText(0);
4211 TextMetrics const & tm = cur.bv().textMetrics(text);
4212 ParagraphMetrics const & pm =
4213 tm.parMetrics(cur.lastpit());
4214 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4215 cur.setCurrentFont();
4218 if (sl == cur.top()) {
4219 cmd = FuncRequest(LFUN_UP);
4222 if (cur.selIsMultiCell()) {
4224 cur.pos() = cur.lastpos();
4225 cur.setCurrentFont();
4226 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4229 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4232 // case LFUN_SCREEN_DOWN: {
4233 // //if (hasSelection())
4234 // // cur.selection() = false;
4235 // col_type const col = tabular.cellColumn(cur.idx());
4236 // int const t = cur.bv().top_y() + cur.bv().height();
4237 // if (t < yo() + tabular.getHeightOfTabular()) {
4238 // cur.bv().scrollDocView(t, true);
4239 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4241 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4248 // case LFUN_SCREEN_UP: {
4249 // //if (hasSelection())
4250 // // cur.selection() = false;
4251 // col_type const col = tabular.cellColumn(cur.idx());
4252 // int const t = cur.bv().top_y() + cur.bv().height();
4254 // cur.bv().scrollDocView(t, true);
4258 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4262 // cur.par() = cur.lastpar();
4263 // cur.pos() = cur.lastpos();
4267 case LFUN_LAYOUT_TABULAR:
4268 cur.bv().showDialog("tabular");
4271 case LFUN_INSET_MODIFY: {
4273 if (cmd.getArg(1) == "from-dialog")
4274 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4276 arg = to_utf8(cmd.argument());
4277 if (!tabularFeatures(cur, arg))
4282 // insert file functions
4283 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4284 case LFUN_FILE_INSERT_PLAINTEXT:
4286 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4287 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4288 FileName(to_utf8(cmd.argument())));
4291 cur.recordUndoInset(INSERT_UNDO);
4292 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4293 // content has been replaced,
4294 // so cursor might be invalid
4295 cur.pos() = cur.lastpos();
4296 cur.pit() = cur.lastpit();
4297 bvcur.setCursor(cur);
4304 if (cur.selIsMultiCell()) {
4305 if (copySelection(cur)) {
4306 cur.recordUndoInset(DELETE_UNDO);
4310 cell(cur.idx())->dispatch(cur, cmd);
4313 case LFUN_SELF_INSERT:
4314 if (cur.selIsMultiCell()) {
4315 cur.recordUndoInset(DELETE_UNDO);
4318 cell(cur.idx())->dispatch(cur, cmd);
4321 case LFUN_CHAR_DELETE_BACKWARD:
4322 case LFUN_CHAR_DELETE_FORWARD:
4323 if (cur.selIsMultiCell()) {
4324 cur.recordUndoInset(DELETE_UNDO);
4327 cell(cur.idx())->dispatch(cur, cmd);
4331 if (!cur.selection())
4333 if (cur.selIsMultiCell()) {
4337 cell(cur.idx())->dispatch(cur, cmd);
4340 case LFUN_CLIPBOARD_PASTE:
4341 case LFUN_PRIMARY_SELECTION_PASTE: {
4342 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4343 theClipboard().getAsText(Clipboard::PlainTextType) :
4344 theSelection().get();
4347 // pass to InsertPlaintextString, but
4348 // only if we have multi-cell content
4349 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4350 cur.recordUndoInset(INSERT_UNDO);
4351 if (insertPlaintextString(cur.bv(), clip, false)) {
4352 // content has been replaced,
4353 // so cursor might be invalid
4354 cur.pos() = cur.lastpos();
4355 cur.pit() = cur.lastpit();
4356 bvcur.setCursor(cur);
4360 // Let the cell handle normal text
4361 cell(cur.idx())->dispatch(cur, cmd);
4366 if (!tabularStackDirty()) {
4367 if (!cur.selIsMultiCell())
4368 cell(cur.idx())->dispatch(cur, cmd);
4371 if (theClipboard().isInternal()) {
4372 cur.recordUndoInset(INSERT_UNDO);
4373 pasteClipboard(cur);
4377 case LFUN_FONT_EMPH:
4378 case LFUN_FONT_BOLD:
4379 case LFUN_FONT_BOLDSYMBOL:
4380 case LFUN_FONT_ROMAN:
4381 case LFUN_FONT_NOUN:
4382 case LFUN_FONT_ITAL:
4383 case LFUN_FONT_FRAK:
4384 case LFUN_FONT_TYPEWRITER:
4385 case LFUN_FONT_SANS:
4386 case LFUN_TEXTSTYLE_APPLY:
4387 case LFUN_TEXTSTYLE_UPDATE:
4388 case LFUN_FONT_SIZE:
4389 case LFUN_FONT_UNDERLINE:
4390 case LFUN_FONT_STRIKEOUT:
4391 case LFUN_FONT_UNDERUNDERLINE:
4392 case LFUN_FONT_UNDERWAVE:
4394 case LFUN_PARAGRAPH_PARAMS_APPLY:
4395 case LFUN_PARAGRAPH_PARAMS:
4396 case LFUN_WORD_CAPITALIZE:
4397 case LFUN_WORD_UPCASE:
4398 case LFUN_WORD_LOWCASE:
4399 case LFUN_CHARS_TRANSPOSE:
4400 if (cur.selIsMultiCell()) {
4403 getSelection(cur, rs, re, cs, ce);
4404 Cursor tmpcur = cur;
4405 for (row_type r = rs; r <= re; ++r) {
4406 for (col_type c = cs; c <= ce; ++c) {
4407 // cursor follows cell:
4408 tmpcur.idx() = tabular.cellIndex(r, c);
4409 // select this cell only:
4412 tmpcur.resetAnchor();
4413 tmpcur.pit() = tmpcur.lastpit();
4414 tmpcur.pos() = tmpcur.top().lastpos();
4415 tmpcur.setCursor(tmpcur);
4416 tmpcur.setSelection();
4417 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4422 cell(cur.idx())->dispatch(cur, cmd);
4426 case LFUN_INSET_SETTINGS:
4427 // relay this lfun to Inset, not to the cell.
4428 Inset::doDispatch(cur, cmd);
4432 // we try to handle this event in the insets dispatch function.
4433 cell(cur.idx())->dispatch(cur, cmd);
4439 // function sets an object as defined in func_status.h:
4440 // states OK, Unknown, Disabled, On, Off.
4441 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4442 FuncStatus & status) const
4444 switch (cmd.action()) {
4445 case LFUN_INSET_MODIFY: {
4446 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4449 // FIXME: We only check for the very first argument...
4450 string const s = cmd.getArg(1);
4451 // We always enable the lfun if it is coming from the dialog
4452 // because the dialog makes sure all the settings are valid,
4453 // even though the first argument might not be valid now.
4454 if (s == "from-dialog") {
4455 status.setEnabled(true);
4459 int action = Tabular::LAST_ACTION;
4461 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4462 if (tabularFeature[i].feature == s) {
4463 action = tabularFeature[i].action;
4467 if (action == Tabular::LAST_ACTION) {
4469 status.setUnknown(true);
4473 string const argument = cmd.getLongArg(2);
4475 row_type sel_row_start = 0;
4476 row_type sel_row_end = 0;
4477 col_type sel_col_start = 0;
4478 col_type sel_col_end = 0;
4479 Tabular::ltType dummyltt;
4482 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4485 case Tabular::SET_PWIDTH:
4486 case Tabular::SET_MPWIDTH:
4487 case Tabular::SET_SPECIAL_COLUMN:
4488 case Tabular::SET_SPECIAL_MULTICOLUMN:
4489 case Tabular::APPEND_ROW:
4490 case Tabular::APPEND_COLUMN:
4491 case Tabular::DELETE_ROW:
4492 case Tabular::DELETE_COLUMN:
4493 case Tabular::COPY_ROW:
4494 case Tabular::COPY_COLUMN:
4495 case Tabular::SET_TOP_SPACE:
4496 case Tabular::SET_BOTTOM_SPACE:
4497 case Tabular::SET_INTERLINE_SPACE:
4501 case Tabular::SET_TABULAR_WIDTH:
4502 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4503 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4506 case Tabular::MOVE_COLUMN_RIGHT:
4507 case Tabular::MOVE_COLUMN_LEFT:
4508 case Tabular::MOVE_ROW_DOWN:
4509 case Tabular::MOVE_ROW_UP: {
4510 if (cur.selection()) {
4511 status.message(_("Selections not supported."));
4512 status.setEnabled(false);
4516 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4517 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4518 (action == Tabular::MOVE_COLUMN_LEFT &&
4519 tabular.cellColumn(cur.idx()) == 0) ||
4520 (action == Tabular::MOVE_ROW_DOWN &&
4521 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4522 (action == Tabular::MOVE_ROW_UP &&
4523 tabular.cellRow(cur.idx()) == 0)) {
4524 status.setEnabled(false);
4528 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4529 action == Tabular::MOVE_COLUMN_LEFT) {
4530 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4531 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4532 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4533 status.message(_("Multi-column in current or"
4534 " destination column."));
4535 status.setEnabled(false);
4540 if (action == Tabular::MOVE_ROW_DOWN ||
4541 action == Tabular::MOVE_ROW_UP) {
4542 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4543 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4544 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4545 status.message(_("Multi-row in current or"
4546 " destination row."));
4547 status.setEnabled(false);
4552 status.setEnabled(true);
4556 case Tabular::SET_DECIMAL_POINT:
4558 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4561 case Tabular::SET_MULTICOLUMN:
4562 case Tabular::UNSET_MULTICOLUMN:
4563 case Tabular::MULTICOLUMN:
4564 // If a row is set as longtable caption, it must not be allowed
4565 // to unset that this row is a multicolumn.
4566 status.setEnabled(sel_row_start == sel_row_end
4567 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4568 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4571 case Tabular::SET_MULTIROW:
4572 case Tabular::UNSET_MULTIROW:
4573 case Tabular::MULTIROW:
4574 // If a row is set as longtable caption, it must not be allowed
4575 // to unset that this row is a multirow.
4576 status.setEnabled(sel_col_start == sel_col_end
4577 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4578 status.setOnOff(tabular.isMultiRow(cur.idx()));
4581 case Tabular::SET_ALL_LINES:
4582 case Tabular::UNSET_ALL_LINES:
4583 case Tabular::SET_BORDER_LINES:
4584 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4587 case Tabular::SET_LINE_TOP:
4588 case Tabular::SET_LINE_BOTTOM:
4589 case Tabular::SET_LINE_LEFT:
4590 case Tabular::SET_LINE_RIGHT:
4591 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4594 case Tabular::TOGGLE_LINE_TOP:
4595 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4596 status.setOnOff(tabular.topLine(cur.idx()));
4599 case Tabular::TOGGLE_LINE_BOTTOM:
4600 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4601 status.setOnOff(tabular.bottomLine(cur.idx()));
4604 case Tabular::TOGGLE_LINE_LEFT:
4605 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4606 status.setOnOff(tabular.leftLine(cur.idx()));
4609 case Tabular::TOGGLE_LINE_RIGHT:
4610 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4611 status.setOnOff(tabular.rightLine(cur.idx()));
4614 // multirow cells only inherit the alignment of the column if the column has
4615 // no width, otherwise they are left-aligned
4616 // therefore allow always left but right and center only if there is no width
4617 case Tabular::M_ALIGN_LEFT:
4619 case Tabular::ALIGN_LEFT:
4620 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4623 case Tabular::M_ALIGN_RIGHT:
4625 case Tabular::ALIGN_RIGHT:
4626 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4627 && !tabular.getPWidth(cur.idx()).zero()));
4628 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4631 case Tabular::M_ALIGN_CENTER:
4633 case Tabular::ALIGN_CENTER:
4634 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4635 && !tabular.getPWidth(cur.idx()).zero()));
4636 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4639 case Tabular::ALIGN_BLOCK:
4640 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4641 && !tabular.isMultiRow(cur.idx()));
4642 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4645 case Tabular::ALIGN_DECIMAL:
4646 status.setEnabled(!tabular.isMultiRow(cur.idx())
4647 && !tabular.isMultiColumn(cur.idx()));
4648 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4651 case Tabular::M_VALIGN_TOP:
4653 case Tabular::VALIGN_TOP:
4654 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4655 && !tabular.isMultiRow(cur.idx()));
4657 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4660 case Tabular::M_VALIGN_BOTTOM:
4662 case Tabular::VALIGN_BOTTOM:
4663 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4664 && !tabular.isMultiRow(cur.idx()));
4666 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4669 case Tabular::M_VALIGN_MIDDLE:
4671 case Tabular::VALIGN_MIDDLE:
4672 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4673 && !tabular.isMultiRow(cur.idx()));
4675 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4678 case Tabular::SET_LONGTABULAR:
4679 // setting as longtable is not allowed when table is inside a float
4680 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4681 || cur.innerInsetOfType(WRAP_CODE) != 0)
4682 status.setEnabled(false);
4684 status.setEnabled(true);
4685 status.setOnOff(tabular.is_long_tabular);
4688 case Tabular::UNSET_LONGTABULAR:
4689 status.setOnOff(!tabular.is_long_tabular);
4692 case Tabular::TOGGLE_ROTATE_TABULAR:
4693 case Tabular::SET_ROTATE_TABULAR:
4694 status.setOnOff(tabular.rotate != 0);
4697 case Tabular::TABULAR_VALIGN_TOP:
4698 status.setEnabled(tabular.tabular_width.zero());
4699 status.setOnOff(tabular.tabular_valignment
4700 == Tabular::LYX_VALIGN_TOP);
4702 case Tabular::TABULAR_VALIGN_MIDDLE:
4703 status.setEnabled(tabular.tabular_width.zero());
4704 status.setOnOff(tabular.tabular_valignment
4705 == Tabular::LYX_VALIGN_MIDDLE);
4707 case Tabular::TABULAR_VALIGN_BOTTOM:
4708 status.setEnabled(tabular.tabular_width.zero());
4709 status.setOnOff(tabular.tabular_valignment
4710 == Tabular::LYX_VALIGN_BOTTOM);
4713 case Tabular::LONGTABULAR_ALIGN_LEFT:
4714 status.setOnOff(tabular.longtabular_alignment
4715 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4717 case Tabular::LONGTABULAR_ALIGN_CENTER:
4718 status.setOnOff(tabular.longtabular_alignment
4719 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4721 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4722 status.setOnOff(tabular.longtabular_alignment
4723 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4726 case Tabular::UNSET_ROTATE_TABULAR:
4727 status.setOnOff(tabular.rotate == 0);
4730 case Tabular::TOGGLE_ROTATE_CELL:
4731 case Tabular::SET_ROTATE_CELL:
4732 status.setOnOff(!oneCellHasRotationState(false,
4733 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4736 case Tabular::UNSET_ROTATE_CELL:
4737 status.setOnOff(!oneCellHasRotationState(true,
4738 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4741 case Tabular::SET_USEBOX:
4742 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4745 // every row can only be one thing:
4746 // either a footer or header
4747 case Tabular::SET_LTFIRSTHEAD:
4748 status.setEnabled(sel_row_start == sel_row_end);
4749 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4752 case Tabular::UNSET_LTFIRSTHEAD:
4753 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4754 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4757 case Tabular::SET_LTHEAD:
4758 status.setEnabled(sel_row_start == sel_row_end);
4759 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4762 case Tabular::UNSET_LTHEAD:
4763 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4764 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4767 case Tabular::SET_LTFOOT:
4768 status.setEnabled(sel_row_start == sel_row_end);
4769 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4772 case Tabular::UNSET_LTFOOT:
4773 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4774 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4777 case Tabular::SET_LTLASTFOOT:
4778 status.setEnabled(sel_row_start == sel_row_end);
4779 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4782 case Tabular::UNSET_LTLASTFOOT:
4783 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4784 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4787 case Tabular::SET_LTNEWPAGE:
4788 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4790 case Tabular::UNSET_LTNEWPAGE:
4791 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4794 // only one row in head/firsthead/foot/lasthead can be the caption
4795 // and a multirow cannot be set as caption
4796 case Tabular::SET_LTCAPTION:
4797 status.setEnabled(sel_row_start == sel_row_end
4798 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4799 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4800 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4801 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4802 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4803 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4804 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4805 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4806 && !tabular.isMultiRow(sel_row_start));
4807 status.setOnOff(tabular.ltCaption(sel_row_start));
4810 case Tabular::UNSET_LTCAPTION:
4811 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4814 case Tabular::TOGGLE_LTCAPTION:
4815 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4816 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4817 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4818 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4819 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4820 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4821 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4822 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4823 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4824 status.setOnOff(tabular.ltCaption(sel_row_start));
4827 case Tabular::SET_BOOKTABS:
4828 status.setOnOff(tabular.use_booktabs);
4831 case Tabular::UNSET_BOOKTABS:
4832 status.setOnOff(!tabular.use_booktabs);
4837 status.setEnabled(false);
4843 case LFUN_CAPTION_INSERT: {
4844 // caption is only allowed in caption cell of longtable
4845 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4846 status.setEnabled(false);
4849 // only standard caption is allowed
4850 string arg = cmd.getArg(0);
4851 if (!arg.empty() && arg != "Standard") {
4852 status.setEnabled(false);
4855 // check if there is already a caption
4856 bool have_caption = false;
4857 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4858 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4859 ParagraphList::const_iterator pend = itc.paragraphs().end();
4860 for (; pit != pend; ++pit) {
4861 InsetList::const_iterator it = pit->insetList().begin();
4862 InsetList::const_iterator end = pit->insetList().end();
4863 for (; it != end; ++it) {
4864 if (it->inset->lyxCode() == CAPTION_CODE) {
4865 have_caption = true;
4870 status.setEnabled(!have_caption);
4874 // These are only enabled inside tabular
4875 case LFUN_CELL_BACKWARD:
4876 case LFUN_CELL_FORWARD:
4877 status.setEnabled(true);
4880 // disable these with multiple cells selected
4881 case LFUN_INSET_INSERT:
4882 case LFUN_TABULAR_INSERT:
4883 case LFUN_FLEX_INSERT:
4884 case LFUN_FLOAT_INSERT:
4885 case LFUN_FLOAT_WIDE_INSERT:
4886 case LFUN_FOOTNOTE_INSERT:
4887 case LFUN_MARGINALNOTE_INSERT:
4888 case LFUN_MATH_INSERT:
4889 case LFUN_MATH_MODE:
4890 case LFUN_MATH_MUTATE:
4891 case LFUN_MATH_DISPLAY:
4892 case LFUN_NOTE_INSERT:
4893 case LFUN_ARGUMENT_INSERT:
4894 case LFUN_BOX_INSERT:
4895 case LFUN_BRANCH_INSERT:
4896 case LFUN_PHANTOM_INSERT:
4897 case LFUN_WRAP_INSERT:
4898 case LFUN_PREVIEW_INSERT:
4899 case LFUN_ERT_INSERT: {
4900 if (cur.selIsMultiCell()) {
4901 status.setEnabled(false);
4904 return cell(cur.idx())->getStatus(cur, cmd, status);
4907 // disable in non-fixed-width cells
4908 case LFUN_PARAGRAPH_BREAK:
4909 // multirow does not allow paragraph breaks
4910 if (tabular.isMultiRow(cur.idx())) {
4911 status.setEnabled(false);
4914 case LFUN_NEWLINE_INSERT: {
4915 if (tabular.getPWidth(cur.idx()).zero()) {
4916 status.setEnabled(false);
4919 return cell(cur.idx())->getStatus(cur, cmd, status);
4922 case LFUN_NEWPAGE_INSERT:
4923 status.setEnabled(false);
4927 if (tabularStackDirty() && theClipboard().isInternal()) {
4928 if (cur.selIsMultiCell()) {
4931 getSelection(cur, rs, re, cs, ce);
4932 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4933 && paste_tabular->nrows() == re - rs + 1)
4934 status.setEnabled(true);
4936 status.setEnabled(false);
4937 status.message(_("Selection size should match clipboard content."));
4940 status.setEnabled(true);
4943 return cell(cur.idx())->getStatus(cur, cmd, status);
4945 case LFUN_INSET_SETTINGS:
4946 // relay this lfun to Inset, not to the cell.
4947 return Inset::getStatus(cur, cmd, status);
4950 // we try to handle this event in the insets dispatch function.
4951 return cell(cur.idx())->getStatus(cur, cmd, status);
4957 Inset::DisplayType InsetTabular::display() const
4959 if (tabular.is_long_tabular) {
4960 switch (tabular.longtabular_alignment) {
4961 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4963 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4965 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4975 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4977 tabular.latex(os, runparams);
4981 int InsetTabular::plaintext(odocstringstream & os,
4982 OutputParams const & runparams, size_t max_length) const
4984 os << '\n'; // output table on a new line
4985 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4986 tabular.plaintext(os, runparams, dp, false, 0, max_length);
4987 return PLAINTEXT_NEWLINE;
4991 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4996 // FIXME: Why not pass a proper DocIterator here?
4998 // if the table is inside a float it doesn't need the informaltable
4999 // wrapper. Search for it.
5000 for (master = owner(); master; master = master->owner())
5001 if (master->lyxCode() == FLOAT_CODE)
5006 os << "<informaltable>";
5009 ret += tabular.docbook(os, runparams);
5011 os << "</informaltable>";
5018 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5020 return tabular.xhtml(xs, rp);
5024 void InsetTabular::validate(LaTeXFeatures & features) const
5026 tabular.validate(features);
5028 // It'd be better to be able to get this from an InsetLayout, but at present
5029 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5030 if (features.runparams().flavor == OutputParams::HTML)
5031 features.addCSSSnippet(
5032 "table { border: 1px solid black; display: inline-block; }\n"
5033 "td { border: 1px solid black; padding: 0.5ex; }");
5037 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5039 return tabular.cellInset(idx);
5043 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5045 return tabular.cellInset(idx);
5049 void InsetTabular::cursorPos(BufferView const & bv,
5050 CursorSlice const & sl, bool boundary, int & x, int & y) const
5052 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5054 // y offset correction
5055 y += cellYPos(sl.idx());
5056 y += tabular.textVOffset(sl.idx());
5057 y += offset_valign_;
5059 // x offset correction
5060 x += cellXPos(sl.idx());
5061 x += tabular.textHOffset(sl.idx());
5062 x += ADD_TO_TABULAR_WIDTH;
5067 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5071 Inset const & inset = *tabular.cellInset(cell);
5072 Point o = bv.coordCache().getInsets().xy(&inset);
5073 int const xbeg = o.x_ - tabular.textHOffset(cell);
5074 int const xend = xbeg + tabular.cellWidth(cell);
5075 row_type const row = tabular.cellRow(cell);
5076 int const ybeg = o.y_ - tabular.rowAscent(row)
5077 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5078 int const yend = ybeg + tabular.cellHeight(cell);
5090 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5091 // << " ybeg=" << ybeg << " yend=" << yend
5092 // << " xx=" << xx << " yy=" << yy
5093 // << " dist=" << xx + yy << endl;
5098 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5100 //lyxerr << "InsetTabular::editXY: " << this << endl;
5101 cur.setSelection(false);
5103 cur.idx() = getNearestCell(cur.bv(), x, y);
5105 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5109 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5111 cur.idx() = getNearestCell(cur.bv(), x, y);
5112 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5116 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5118 idx_type idx_min = 0;
5119 int dist_min = numeric_limits<int>::max();
5120 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5121 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5122 int const d = dist(bv, i, x, y);
5133 int InsetTabular::cellYPos(idx_type const cell) const
5135 row_type row = tabular.cellRow(cell);
5137 for (row_type r = 0; r < row; ++r)
5138 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5139 + tabular.interRowSpace(r + 1);
5144 int InsetTabular::cellXPos(idx_type const cell) const
5146 col_type col = tabular.cellColumn(cell);
5148 for (col_type c = 0; c < col; ++c)
5149 lx += tabular.column_info[c].width;
5154 void InsetTabular::resetPos(Cursor & cur) const
5156 BufferView & bv = cur.bv();
5157 int const maxwidth = bv.workWidth();
5159 int const scx_old = scx_;
5160 int const i = cur.find(this);
5165 int const X2 = maxwidth;
5166 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5167 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5168 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5171 scx_ = X1 + 20 - x1;
5173 scx_ = X2 - 20 - x2;
5178 // only update if offset changed
5179 if (scx_ != scx_old)
5180 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5184 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5186 row_type const row = tabular.cellRow(cur.idx());
5187 col_type const col = tabular.cellColumn(cur.idx());
5189 if (isRightToLeft(cur)) {
5190 if (tabular.cellColumn(cur.idx()) == 0) {
5191 if (row == tabular.nrows() - 1)
5193 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5198 cur.idx() = tabular.getLastCellInRow(row - 1);
5200 cur.idx() = tabular.cellIndex(row, col - 1);
5203 if (tabular.isLastCell(cur.idx()))
5205 if (cur.idx() == tabular.getLastCellInRow(row))
5206 cur.idx() = tabular.cellIndex(row + 1, 0);
5208 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5209 cur.idx() = tabular.cellIndex(row, colnextcell);
5213 cur.boundary(false);
5215 if (cur.selIsMultiCell()) {
5216 cur.pit() = cur.lastpit();
5217 cur.pos() = cur.lastpos();
5225 // in visual mode, place cursor at extreme left or right
5227 switch(entry_from) {
5229 case ENTRY_DIRECTION_RIGHT:
5230 cur.posVisToRowExtremity(false /* !left */);
5232 case ENTRY_DIRECTION_LEFT:
5233 cur.posVisToRowExtremity(true /* left */);
5235 case ENTRY_DIRECTION_IGNORE:
5236 // nothing to do in this case
5240 cur.setCurrentFont();
5245 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5247 row_type const row = tabular.cellRow(cur.idx());
5248 col_type const col = tabular.cellColumn(cur.idx());
5250 if (isRightToLeft(cur)) {
5251 if (cur.idx() == tabular.getLastCellInRow(row)) {
5254 cur.idx() = tabular.getFirstCellInRow(row);
5255 cur.idx() = tabular.cellAbove(cur.idx());
5257 if (tabular.isLastCell(cur.idx()))
5259 if (cur.idx() == tabular.getLastCellInRow(row))
5260 cur.idx() = tabular.cellIndex(row + 1, 0);
5262 cur.idx() = tabular.cellIndex(row, col + 1);
5265 if (cur.idx() == 0) // first cell
5268 cur.idx() = tabular.getLastCellInRow(row - 1);
5270 cur.idx() = tabular.cellIndex(row, col - 1);
5273 if (cur.selIsMultiCell()) {
5274 cur.pit() = cur.lastpit();
5275 cur.pos() = cur.lastpos();
5280 cur.pit() = cur.lastpit();
5281 cur.pos() = cur.lastpos();
5283 // in visual mode, place cursor at extreme left or right
5285 switch(entry_from) {
5287 case ENTRY_DIRECTION_RIGHT:
5288 cur.posVisToRowExtremity(false /* !left */);
5290 case ENTRY_DIRECTION_LEFT:
5291 cur.posVisToRowExtremity(true /* left */);
5293 case ENTRY_DIRECTION_IGNORE:
5294 // nothing to do in this case
5298 cur.setCurrentFont();
5303 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5305 istringstream is(argument);
5308 if (insetCode(s) != TABULAR_CODE)
5312 size_t safe_guard = 0;
5317 if (safe_guard > 1000) {
5318 LYXERR0("parameter max count reached!");
5322 Tabular::Feature action = Tabular::LAST_ACTION;
5325 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5326 if (s != tabularFeature[i].feature)
5328 action = tabularFeature[i].action;
5331 if (action == Tabular::LAST_ACTION) {
5332 LYXERR0("Feature not found " << s);
5336 if (tabularFeature[i].need_value)
5338 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5339 tabularFeatures(cur, action, val);
5345 static void checkLongtableSpecial(Tabular::ltType & ltt,
5346 string const & special, bool & flag)
5348 if (special == "dl_above") {
5351 } else if (special == "dl_below") {
5352 ltt.bottomDL = flag;
5354 } else if (special == "empty") {
5364 bool InsetTabular::oneCellHasRotationState(bool rotated,
5365 row_type row_start, row_type row_end,
5366 col_type col_start, col_type col_end) const
5368 for (row_type r = row_start; r <= row_end; ++r)
5369 for (col_type c = col_start; c <= col_end; ++c)
5371 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5374 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5381 void InsetTabular::tabularFeatures(Cursor & cur,
5382 Tabular::Feature feature, string const & value)
5384 col_type sel_col_start;
5385 col_type sel_col_end;
5386 row_type sel_row_start;
5387 row_type sel_row_end;
5388 bool setLines = false;
5389 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5390 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5394 case Tabular::M_ALIGN_LEFT:
5395 case Tabular::ALIGN_LEFT:
5396 setAlign = LYX_ALIGN_LEFT;
5399 case Tabular::M_ALIGN_RIGHT:
5400 case Tabular::ALIGN_RIGHT:
5401 setAlign = LYX_ALIGN_RIGHT;
5404 case Tabular::M_ALIGN_CENTER:
5405 case Tabular::ALIGN_CENTER:
5406 setAlign = LYX_ALIGN_CENTER;
5409 case Tabular::ALIGN_BLOCK:
5410 setAlign = LYX_ALIGN_BLOCK;
5413 case Tabular::ALIGN_DECIMAL:
5414 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5415 setAlign = LYX_ALIGN_CENTER;
5417 setAlign = LYX_ALIGN_DECIMAL;
5420 case Tabular::M_VALIGN_TOP:
5421 case Tabular::VALIGN_TOP:
5422 setVAlign = Tabular::LYX_VALIGN_TOP;
5425 case Tabular::M_VALIGN_BOTTOM:
5426 case Tabular::VALIGN_BOTTOM:
5427 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5430 case Tabular::M_VALIGN_MIDDLE:
5431 case Tabular::VALIGN_MIDDLE:
5432 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5439 cur.recordUndoInset(ATOMIC_UNDO, this);
5441 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5442 row_type const row = tabular.cellRow(cur.idx());
5443 col_type const column = tabular.cellColumn(cur.idx());
5445 Tabular::ltType ltt;
5449 case Tabular::SET_TABULAR_WIDTH:
5450 tabular.setTabularWidth(Length(value));
5453 case Tabular::SET_PWIDTH: {
5454 Length const len(value);
5455 tabular.setColumnPWidth(cur, cur.idx(), len);
5457 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5458 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5462 case Tabular::SET_MPWIDTH:
5463 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5466 case Tabular::SET_MROFFSET:
5467 tabular.setMROffset(cur, cur.idx(), Length(value));
5470 case Tabular::SET_SPECIAL_COLUMN:
5471 case Tabular::SET_SPECIAL_MULTICOLUMN:
5472 if (value == "none")
5473 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5475 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5478 case Tabular::APPEND_ROW:
5479 // append the row into the tabular
5480 tabular.appendRow(row);
5483 case Tabular::APPEND_COLUMN:
5484 // append the column into the tabular
5485 tabular.appendColumn(column);
5486 cur.idx() = tabular.cellIndex(row, column);
5489 case Tabular::DELETE_ROW:
5490 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5491 for (col_type c = 0; c < tabular.ncols(); c++)
5492 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5493 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5496 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5497 tabular.deleteRow(sel_row_start);
5498 if (sel_row_start >= tabular.nrows())
5500 cur.idx() = tabular.cellIndex(sel_row_start, column);
5503 cur.setSelection(false);
5506 case Tabular::DELETE_COLUMN:
5507 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5508 for (row_type r = 0; r < tabular.nrows(); r++)
5509 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5510 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5513 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5514 for (row_type r = 0; r < tabular.nrows(); r++)
5515 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5516 tabular.leftLine(tabular.cellIndex(r, 0)));
5519 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5520 tabular.deleteColumn(sel_col_start);
5521 if (sel_col_start >= tabular.ncols())
5523 cur.idx() = tabular.cellIndex(row, sel_col_start);
5526 cur.setSelection(false);
5529 case Tabular::COPY_ROW:
5530 tabular.copyRow(row);
5533 case Tabular::COPY_COLUMN:
5534 tabular.copyColumn(column);
5535 cur.idx() = tabular.cellIndex(row, column);
5538 case Tabular::MOVE_COLUMN_RIGHT:
5539 tabular.moveColumn(column, Tabular::RIGHT);
5540 cur.idx() = tabular.cellIndex(row, column + 1);
5543 case Tabular::MOVE_COLUMN_LEFT:
5544 tabular.moveColumn(column, Tabular::LEFT);
5545 cur.idx() = tabular.cellIndex(row, column - 1);
5548 case Tabular::MOVE_ROW_DOWN:
5549 tabular.moveRow(row, Tabular::DOWN);
5550 cur.idx() = tabular.cellIndex(row + 1, column);
5553 case Tabular::MOVE_ROW_UP:
5554 tabular.moveRow(row, Tabular::UP);
5555 cur.idx() = tabular.cellIndex(row - 1, column);
5558 case Tabular::SET_LINE_TOP:
5559 case Tabular::TOGGLE_LINE_TOP: {
5560 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5561 ? (value == "true") : !tabular.topLine(cur.idx());
5562 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5563 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5564 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5568 case Tabular::SET_LINE_BOTTOM:
5569 case Tabular::TOGGLE_LINE_BOTTOM: {
5570 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5571 ? (value == "true") : !tabular.bottomLine(cur.idx());
5572 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5573 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5574 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5578 case Tabular::SET_LINE_LEFT:
5579 case Tabular::TOGGLE_LINE_LEFT: {
5580 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5581 ? (value == "true") : !tabular.leftLine(cur.idx());
5582 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5583 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5584 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5588 case Tabular::SET_LINE_RIGHT:
5589 case Tabular::TOGGLE_LINE_RIGHT: {
5590 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5591 ? (value == "true") : !tabular.rightLine(cur.idx());
5592 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5593 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5594 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5598 case Tabular::M_ALIGN_LEFT:
5599 case Tabular::M_ALIGN_RIGHT:
5600 case Tabular::M_ALIGN_CENTER:
5601 case Tabular::ALIGN_LEFT:
5602 case Tabular::ALIGN_RIGHT:
5603 case Tabular::ALIGN_CENTER:
5604 case Tabular::ALIGN_BLOCK:
5605 case Tabular::ALIGN_DECIMAL:
5606 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5607 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5608 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5609 !tabular.getPWidth(c).zero());
5612 case Tabular::M_VALIGN_TOP:
5613 case Tabular::M_VALIGN_BOTTOM:
5614 case Tabular::M_VALIGN_MIDDLE:
5616 case Tabular::VALIGN_TOP:
5617 case Tabular::VALIGN_BOTTOM:
5618 case Tabular::VALIGN_MIDDLE:
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.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5624 case Tabular::SET_MULTICOLUMN: {
5625 if (!cur.selection()) {
5626 // just multicol for one single cell
5627 // check whether we are completely in a multicol
5628 if (!tabular.isMultiColumn(cur.idx()))
5629 tabular.setMultiColumn(cur.idx(), 1,
5630 tabular.rightLine(cur.idx()));
5633 // we have a selection so this means we just add all these
5634 // cells to form a multicolumn cell
5635 idx_type const s_start = cur.selBegin().idx();
5636 row_type const col_start = tabular.cellColumn(s_start);
5637 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5638 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5639 tabular.rightLine(cur.selEnd().idx()));
5642 cur.setSelection(false);
5646 case Tabular::UNSET_MULTICOLUMN: {
5647 if (!cur.selection()) {
5648 if (tabular.isMultiColumn(cur.idx()))
5649 tabular.unsetMultiColumn(cur.idx());
5654 case Tabular::MULTICOLUMN: {
5655 if (!cur.selection()) {
5656 if (tabular.isMultiColumn(cur.idx()))
5657 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5659 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5663 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5664 row_type const r = sel_row_start;
5665 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5666 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5669 // If the selection contains at least one singlecol cell
5670 // or multiple multicol cells,
5671 // we assume the user will merge is to a single multicol
5673 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5675 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5679 case Tabular::SET_MULTIROW: {
5680 if (!cur.selection()) {
5681 // just multirow for one single cell
5682 // check whether we are completely in a multirow
5683 if (!tabular.isMultiRow(cur.idx()))
5684 tabular.setMultiRow(cur.idx(), 1,
5685 tabular.bottomLine(cur.idx()));
5688 // we have a selection so this means we just add all this
5689 // cells to form a multirow cell
5690 idx_type const s_start = cur.selBegin().idx();
5691 row_type const row_start = tabular.cellRow(s_start);
5692 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5693 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5694 tabular.bottomLine(cur.selEnd().idx()));
5697 cur.setSelection(false);
5701 case Tabular::UNSET_MULTIROW: {
5702 if (!cur.selection()) {
5703 if (tabular.isMultiRow(cur.idx()))
5704 tabular.unsetMultiRow(cur.idx());
5709 case Tabular::MULTIROW: {
5710 if (!cur.selection()) {
5711 if (tabular.isMultiRow(cur.idx()))
5712 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5714 tabularFeatures(cur, Tabular::SET_MULTIROW);
5718 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5719 col_type const c = sel_col_start;
5720 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5721 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5724 // If the selection contains at least one singlerow cell
5725 // or multiple multirow cells,
5726 // we assume the user will merge is to a single multirow
5728 tabularFeatures(cur, Tabular::SET_MULTIROW);
5730 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5734 case Tabular::SET_ALL_LINES:
5736 case Tabular::UNSET_ALL_LINES:
5737 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5738 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5739 idx_type const cell = tabular.cellIndex(r, c);
5740 tabular.setTopLine(cell, setLines);
5741 tabular.setBottomLine(cell, setLines);
5742 tabular.setRightLine(cell, setLines);
5743 tabular.setLeftLine(cell, setLines);
5747 case Tabular::SET_BORDER_LINES:
5748 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5749 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5750 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5752 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5753 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5754 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5758 case Tabular::SET_LONGTABULAR:
5759 tabular.is_long_tabular = true;
5762 case Tabular::UNSET_LONGTABULAR:
5763 for (row_type r = 0; r < tabular.nrows(); ++r) {
5764 if (tabular.ltCaption(r)) {
5765 cur.idx() = tabular.cellIndex(r, 0);
5768 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5771 tabular.is_long_tabular = false;
5774 case Tabular::SET_ROTATE_TABULAR:
5775 tabular.rotate = convert<int>(value);
5778 case Tabular::UNSET_ROTATE_TABULAR:
5782 case Tabular::TOGGLE_ROTATE_TABULAR:
5783 // when pressing the rotate button we default to 90° rotation
5784 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5787 case Tabular::TABULAR_VALIGN_TOP:
5788 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5791 case Tabular::TABULAR_VALIGN_MIDDLE:
5792 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5795 case Tabular::TABULAR_VALIGN_BOTTOM:
5796 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5799 case Tabular::LONGTABULAR_ALIGN_LEFT:
5800 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5803 case Tabular::LONGTABULAR_ALIGN_CENTER:
5804 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5807 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5808 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5813 case Tabular::SET_ROTATE_CELL:
5814 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5815 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5816 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5819 case Tabular::UNSET_ROTATE_CELL:
5820 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5821 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5822 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5825 case Tabular::TOGGLE_ROTATE_CELL:
5827 bool oneNotRotated = oneCellHasRotationState(false,
5828 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5830 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5831 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5832 // when pressing the rotate cell button we default to 90° rotation
5834 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5836 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5841 case Tabular::SET_USEBOX: {
5842 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5843 if (val == tabular.getUsebox(cur.idx()))
5844 val = Tabular::BOX_NONE;
5845 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5846 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5847 tabular.setUsebox(tabular.cellIndex(r, c), val);
5851 case Tabular::UNSET_LTFIRSTHEAD:
5853 case Tabular::SET_LTFIRSTHEAD:
5854 tabular.getRowOfLTFirstHead(row, ltt);
5855 checkLongtableSpecial(ltt, value, flag);
5856 tabular.setLTHead(row, flag, ltt, true);
5859 case Tabular::UNSET_LTHEAD:
5861 case Tabular::SET_LTHEAD:
5862 tabular.getRowOfLTHead(row, ltt);
5863 checkLongtableSpecial(ltt, value, flag);
5864 tabular.setLTHead(row, flag, ltt, false);
5867 case Tabular::UNSET_LTFOOT:
5869 case Tabular::SET_LTFOOT:
5870 tabular.getRowOfLTFoot(row, ltt);
5871 checkLongtableSpecial(ltt, value, flag);
5872 tabular.setLTFoot(row, flag, ltt, false);
5875 case Tabular::UNSET_LTLASTFOOT:
5877 case Tabular::SET_LTLASTFOOT:
5878 tabular.getRowOfLTLastFoot(row, ltt);
5879 checkLongtableSpecial(ltt, value, flag);
5880 tabular.setLTFoot(row, flag, ltt, true);
5883 case Tabular::UNSET_LTNEWPAGE:
5885 case Tabular::SET_LTNEWPAGE:
5886 tabular.setLTNewPage(row, flag);
5889 case Tabular::SET_LTCAPTION: {
5890 if (tabular.ltCaption(row))
5892 cur.idx() = tabular.setLTCaption(row, true);
5895 cur.setSelection(false);
5896 // If a row is set as caption, then also insert
5897 // a caption. Otherwise the LaTeX output is broken.
5898 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5899 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5903 case Tabular::UNSET_LTCAPTION: {
5904 if (!tabular.ltCaption(row))
5906 cur.idx() = tabular.setLTCaption(row, false);
5909 cur.setSelection(false);
5910 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5911 if (lyx::getStatus(fr).enabled())
5916 case Tabular::TOGGLE_LTCAPTION: {
5917 if (tabular.ltCaption(row))
5918 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5920 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5924 case Tabular::SET_BOOKTABS:
5925 tabular.use_booktabs = true;
5928 case Tabular::UNSET_BOOKTABS:
5929 tabular.use_booktabs = false;
5932 case Tabular::SET_TOP_SPACE: {
5934 if (value == "default")
5935 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5936 tabular.row_info[r].top_space_default = true;
5937 else if (value == "none")
5938 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5939 tabular.row_info[r].top_space_default = false;
5940 tabular.row_info[r].top_space = len;
5942 else if (isValidLength(value, &len))
5943 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5944 tabular.row_info[r].top_space_default = false;
5945 tabular.row_info[r].top_space = len;
5950 case Tabular::SET_BOTTOM_SPACE: {
5952 if (value == "default")
5953 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5954 tabular.row_info[r].bottom_space_default = true;
5955 else if (value == "none")
5956 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5957 tabular.row_info[r].bottom_space_default = false;
5958 tabular.row_info[r].bottom_space = len;
5960 else if (isValidLength(value, &len))
5961 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5962 tabular.row_info[r].bottom_space_default = false;
5963 tabular.row_info[r].bottom_space = len;
5968 case Tabular::SET_INTERLINE_SPACE: {
5970 if (value == "default")
5971 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5972 tabular.row_info[r].interline_space_default = true;
5973 else if (value == "none")
5974 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5975 tabular.row_info[r].interline_space_default = false;
5976 tabular.row_info[r].interline_space = len;
5978 else if (isValidLength(value, &len))
5979 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5980 tabular.row_info[r].interline_space_default = false;
5981 tabular.row_info[r].interline_space = len;
5986 case Tabular::SET_DECIMAL_POINT:
5987 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5988 tabular.column_info[c].decimal_point = from_utf8(value);
5991 // dummy stuff just to avoid warnings
5992 case Tabular::LAST_ACTION:
5998 bool InsetTabular::copySelection(Cursor & cur)
6000 if (!cur.selection())
6005 getSelection(cur, rs, re, cs, ce);
6007 paste_tabular.reset(new Tabular(tabular));
6009 for (row_type r = 0; r < rs; ++r)
6010 paste_tabular->deleteRow(0);
6012 row_type const rows = re - rs + 1;
6013 while (paste_tabular->nrows() > rows)
6014 paste_tabular->deleteRow(rows);
6016 for (col_type c = 0; c < cs; ++c)
6017 paste_tabular->deleteColumn(0);
6019 col_type const columns = ce - cs + 1;
6020 while (paste_tabular->ncols() > columns)
6021 paste_tabular->deleteColumn(columns);
6023 paste_tabular->setBuffer(tabular.buffer());
6025 odocstringstream os;
6026 OutputParams const runparams(0);
6027 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6028 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6029 cap::copySelection(cur, os.str());
6031 // mark tabular stack dirty
6032 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6033 // when we (hopefully) have a one-for-all paste mechanism.
6034 // This must be called after cap::copySelection.
6035 dirtyTabularStack(true);
6041 bool InsetTabular::pasteClipboard(Cursor & cur)
6045 col_type actcol = tabular.cellColumn(cur.idx());
6046 row_type actrow = tabular.cellRow(cur.idx());
6048 if (cur.selIsMultiCell()) {
6051 getSelection(cur, actrow, re, actcol, ce);
6054 for (row_type r1 = 0, r2 = actrow;
6055 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6057 for (col_type c1 = 0, c2 = actcol;
6058 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6060 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6061 tabular.isPartOfMultiColumn(r2, c2))
6063 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6067 if (tabular.isPartOfMultiColumn(r2, c2)) {
6071 shared_ptr<InsetTableCell> inset(
6072 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6073 tabular.setCellInset(r2, c2, inset);
6074 // FIXME?: why do we need to do this explicitly? (EL)
6075 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6077 // FIXME: change tracking (MG)
6078 inset->setChange(Change(buffer().params().trackChanges ?
6079 Change::INSERTED : Change::UNCHANGED));
6087 void InsetTabular::cutSelection(Cursor & cur)
6089 if (!cur.selection())
6094 getSelection(cur, rs, re, cs, ce);
6095 for (row_type r = rs; r <= re; ++r) {
6096 for (col_type c = cs; c <= ce; ++c) {
6097 shared_ptr<InsetTableCell> t
6098 = cell(tabular.cellIndex(r, c));
6099 if (buffer().params().trackChanges)
6100 // FIXME: Change tracking (MG)
6101 t->setChange(Change(Change::DELETED));
6107 // cursor position might be invalid now
6108 if (cur.pit() > cur.lastpit())
6109 cur.pit() = cur.lastpit();
6110 if (cur.pos() > cur.lastpos())
6111 cur.pos() = cur.lastpos();
6112 cur.clearSelection();
6116 bool InsetTabular::isRightToLeft(Cursor & cur) const
6118 // LASSERT: It might be better to abandon this Buffer.
6119 LASSERT(cur.depth() > 1, return false);
6120 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6121 pos_type const parentpos = cur[cur.depth() - 2].pos();
6122 return parentpar.getFontSettings(buffer().params(),
6123 parentpos).language()->rightToLeft();
6127 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6130 LASSERT(stidx <= enidx, return docstring());
6132 col_type const col1 = tabular.cellColumn(stidx);
6133 col_type const col2 = tabular.cellColumn(enidx);
6134 row_type const row1 = tabular.cellRow(stidx);
6135 row_type const row2 = tabular.cellRow(enidx);
6137 for (col_type col = col1; col <= col2; col++)
6138 for (row_type row = row1; row <= row2; row++) {
6143 retval += tabular.cellInset(row, col)->asString(intoInsets);
6149 void InsetTabular::getSelection(Cursor & cur,
6150 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6152 CursorSlice const & beg = cur.selBegin();
6153 CursorSlice const & end = cur.selEnd();
6154 cs = tabular.cellColumn(beg.idx());
6155 ce = tabular.cellColumn(end.idx());
6159 rs = tabular.cellRow(beg.idx());
6160 re = tabular.cellRow(end.idx());
6166 Text * InsetTabular::getText(int idx) const
6168 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6172 void InsetTabular::setChange(Change const & change)
6174 for (idx_type idx = 0; idx < nargs(); ++idx)
6175 cell(idx)->setChange(change);
6179 void InsetTabular::acceptChanges()
6181 for (idx_type idx = 0; idx < nargs(); ++idx)
6182 cell(idx)->acceptChanges();
6186 void InsetTabular::rejectChanges()
6188 for (idx_type idx = 0; idx < nargs(); ++idx)
6189 cell(idx)->rejectChanges();
6193 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6195 return tabular.getPWidth(cell).zero();
6199 bool InsetTabular::forcePlainLayout(idx_type cell) const
6201 return !tabular.getPWidth(cell).zero();
6205 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6208 if (buf.length() <= 0)
6213 col_type maxCols = 1;
6214 size_t const len = buf.length();
6218 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6226 maxCols = max(cols, maxCols);
6232 maxCols = max(cols, maxCols);
6238 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6239 loctab = paste_tabular.get();
6241 dirtyTabularStack(true);
6244 cell = bv.cursor().idx();
6245 ocol = tabular.cellColumn(cell);
6246 row = tabular.cellRow(cell);
6250 idx_type const cells = loctab->numberofcells;
6253 rows = loctab->nrows();
6254 col_type const columns = loctab->ncols();
6256 while (cell < cells && p < len && row < rows &&
6257 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6263 // we can only set this if we are not too far right
6264 if (cols < columns) {
6265 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6266 Font const font = bv.textMetrics(&inset->text()).
6268 inset->setText(buf.substr(op, p - op), font,
6269 buffer().params().trackChanges);
6275 // we can only set this if we are not too far right
6276 if (cols < columns) {
6277 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6278 Font const font = bv.textMetrics(&inset->text()).
6280 inset->setText(buf.substr(op, p - op), font,
6281 buffer().params().trackChanges);
6286 cell = loctab->cellIndex(row, cols);
6292 // check for the last cell if there is no trailing '\n'
6293 if (cell < cells && op < len) {
6294 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6295 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6296 inset->setText(buf.substr(op, len - op), font,
6297 buffer().params().trackChanges);
6303 void InsetTabular::addPreview(DocIterator const & inset_pos,
6304 PreviewLoader & loader) const
6306 DocIterator cell_pos = inset_pos;
6308 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6309 for (row_type r = 0; r < tabular.nrows(); ++r) {
6310 for (col_type c = 0; c < tabular.ncols(); ++c) {
6311 cell_pos.top().idx() = tabular.cellIndex(r, c);
6312 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6318 bool InsetTabular::completionSupported(Cursor const & cur) const
6320 Cursor const & bvCur = cur.bv().cursor();
6321 if (&bvCur.inset() != this)
6323 return cur.text()->completionSupported(cur);
6327 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6329 return completionSupported(cur);
6333 bool InsetTabular::automaticInlineCompletion() const
6335 return lyxrc.completion_inline_text;
6339 bool InsetTabular::automaticPopupCompletion() const
6341 return lyxrc.completion_popup_text;
6345 bool InsetTabular::showCompletionCursor() const
6347 return lyxrc.completion_cursor_text;
6351 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6353 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6357 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6359 if (!completionSupported(cur))
6361 return cur.text()->completionPrefix(cur);
6365 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6367 if (!completionSupported(cur))
6370 return cur.text()->insertCompletion(cur, s, finished);
6374 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6375 Dimension & dim) const
6377 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6378 tm.completionPosAndDim(cur, x, y, dim);
6382 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6384 istringstream data(in);
6386 lex.setStream(data);
6393 if (!lex || token != "tabular") {
6394 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6398 // This is part of the inset proper that is usually swallowed
6399 // by Buffer::readInset
6401 if (!lex || token != "Tabular") {
6402 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6410 string InsetTabular::params2string(InsetTabular const & inset)
6413 data << "tabular" << ' ';
6415 data << "\\end_inset\n";