2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetList.h"
38 #include "LaTeXFeatures.h"
42 #include "MetricsInfo.h"
43 #include "OutputParams.h"
44 #include "output_xhtml.h"
45 #include "Paragraph.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
48 #include "TextClass.h"
49 #include "TextMetrics.h"
51 #include "frontends/Application.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "support/convert.h"
58 #include "support/debug.h"
59 #include "support/docstream.h"
60 #include "support/FileName.h"
61 #include "support/gettext.h"
62 #include "support/lassert.h"
63 #include "support/lstrings.h"
65 #include <boost/scoped_ptr.hpp>
73 using namespace lyx::support;
79 using cap::dirtyTabularStack;
80 using cap::tabularStackDirty;
82 using graphics::PreviewLoader;
84 using frontend::Painter;
85 using frontend::Clipboard;
87 namespace Alert = frontend::Alert;
92 int const ADD_TO_HEIGHT = 2; // in cell
93 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
94 int const default_line_space = 10; // ?
95 int const WIDTH_OF_LINE = 5; // space between double lines
99 boost::scoped_ptr<Tabular> paste_tabular;
102 struct TabularFeature {
103 Tabular::Feature action;
109 TabularFeature tabularFeature[] =
111 // the SET/UNSET actions are used by the table dialog,
112 // the TOGGLE actions by the table toolbar buttons
113 { Tabular::APPEND_ROW, "append-row", false },
114 { Tabular::APPEND_COLUMN, "append-column", false },
115 { Tabular::DELETE_ROW, "delete-row", false },
116 { Tabular::DELETE_COLUMN, "delete-column", false },
117 { Tabular::COPY_ROW, "copy-row", false },
118 { Tabular::COPY_COLUMN, "copy-column", false },
119 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
120 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
121 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
122 { Tabular::MOVE_ROW_UP, "move-row-up", false },
123 { Tabular::SET_LINE_TOP, "set-line-top", true },
124 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
125 { Tabular::SET_LINE_LEFT, "set-line-left", true },
126 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
127 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
128 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
129 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
130 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
131 { Tabular::ALIGN_LEFT, "align-left", false },
132 { Tabular::ALIGN_RIGHT, "align-right", false },
133 { Tabular::ALIGN_CENTER, "align-center", false },
134 { Tabular::ALIGN_BLOCK, "align-block", false },
135 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
136 { Tabular::VALIGN_TOP, "valign-top", false },
137 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
138 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
139 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
140 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
141 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
142 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
143 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
144 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
145 { Tabular::MULTICOLUMN, "multicolumn", false },
146 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
147 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
148 { Tabular::MULTIROW, "multirow", false },
149 { Tabular::SET_MULTIROW, "set-multirow", false },
150 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
151 { Tabular::SET_MROFFSET, "set-mroffset", true },
152 { Tabular::SET_ALL_LINES, "set-all-lines", false },
153 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
154 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
155 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
156 { Tabular::SET_PWIDTH, "set-pwidth", true },
157 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
158 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
159 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
160 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
161 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
162 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
163 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
164 { Tabular::SET_USEBOX, "set-usebox", true },
165 { Tabular::SET_LTHEAD, "set-lthead", true },
166 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
167 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
168 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
169 { Tabular::SET_LTFOOT, "set-ltfoot", true },
170 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
171 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
172 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
173 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
174 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
175 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
176 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
177 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
178 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
179 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
180 { Tabular::SET_BOOKTABS, "set-booktabs", false },
181 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
182 { Tabular::SET_TOP_SPACE, "set-top-space", true },
183 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
184 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
185 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
186 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
187 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
188 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
189 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
190 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
191 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
192 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
193 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
194 { Tabular::LAST_ACTION, "", false }
198 string const tostr(LyXAlignment const & num)
203 case LYX_ALIGN_BLOCK:
207 case LYX_ALIGN_CENTER:
209 case LYX_ALIGN_RIGHT:
211 case LYX_ALIGN_LAYOUT:
213 case LYX_ALIGN_SPECIAL:
215 case LYX_ALIGN_DECIMAL:
222 string const tostr(Tabular::HAlignment const & num)
225 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
227 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
229 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
236 string const tostr(Tabular::VAlignment const & num)
239 case Tabular::LYX_VALIGN_TOP:
241 case Tabular::LYX_VALIGN_MIDDLE:
243 case Tabular::LYX_VALIGN_BOTTOM:
250 string const tostr(Tabular::BoxType const & num)
253 case Tabular::BOX_NONE:
255 case Tabular::BOX_PARBOX:
257 case Tabular::BOX_MINIPAGE:
264 // I would have liked a fromstr template a lot better. (Lgb)
265 bool string2type(string const str, LyXAlignment & num)
268 num = LYX_ALIGN_NONE;
269 else if (str == "block")
270 num = LYX_ALIGN_BLOCK;
271 else if (str == "left")
272 num = LYX_ALIGN_LEFT;
273 else if (str == "center")
274 num = LYX_ALIGN_CENTER;
275 else if (str == "right")
276 num = LYX_ALIGN_RIGHT;
277 else if (str == "decimal")
278 num = LYX_ALIGN_DECIMAL;
285 bool string2type(string const str, Tabular::HAlignment & num)
288 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
289 else if (str == "center" )
290 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
291 else if (str == "right")
292 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
299 bool string2type(string const str, Tabular::VAlignment & num)
302 num = Tabular::LYX_VALIGN_TOP;
303 else if (str == "middle" )
304 num = Tabular::LYX_VALIGN_MIDDLE;
305 else if (str == "bottom")
306 num = Tabular::LYX_VALIGN_BOTTOM;
313 bool string2type(string const str, Tabular::BoxType & num)
316 num = Tabular::BOX_NONE;
317 else if (str == "parbox")
318 num = Tabular::BOX_PARBOX;
319 else if (str == "minipage")
320 num = Tabular::BOX_MINIPAGE;
327 bool string2type(string const str, bool & num)
331 else if (str == "false")
339 bool getTokenValue(string const & str, char const * token, string & ret)
342 size_t token_length = strlen(token);
343 size_t pos = str.find(token);
345 if (pos == string::npos || pos + token_length + 1 >= str.length()
346 || str[pos + token_length] != '=')
348 pos += token_length + 1;
350 if (ch != '"' && ch != '\'') { // only read till next space
354 while (pos < str.length() - 1 && str[++pos] != ch)
361 bool getTokenValue(string const & str, char const * token, docstring & ret)
364 bool const success = getTokenValue(str, token, tmp);
365 ret = from_utf8(tmp);
370 bool getTokenValue(string const & str, char const * token, int & num)
374 if (!getTokenValue(str, token, tmp))
376 num = convert<int>(tmp);
381 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
384 return getTokenValue(str, token, tmp) && string2type(tmp, num);
388 bool getTokenValue(string const & str, char const * token,
389 Tabular::HAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::VAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::BoxType & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token, bool & flag)
414 // set the flag always to false as this should be the default for bools
415 // not in the file-format.
418 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
422 bool getTokenValue(string const & str, char const * token, Length & len)
424 // set the length to be zero() as default as this it should be if not
425 // in the file format.
428 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
432 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
437 if (!getTokenValue(str, token, tmp))
439 if (tmp == "default") {
443 return isValidLength(tmp, &len);
447 void l_getline(istream & is, string & str)
450 while (str.empty()) {
452 if (!str.empty() && str[str.length() - 1] == '\r')
453 str.erase(str.length() - 1);
458 string const write_attribute(string const & name, T const & t)
460 string const s = tostr(t);
461 return s.empty() ? s : " " + name + "=\"" + s + "\"";
465 string const write_attribute(string const & name, string const & t)
467 return t.empty() ? t : " " + name + "=\"" + t + "\"";
472 string const write_attribute(string const & name, docstring const & t)
474 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
479 string const write_attribute(string const & name, bool const & b)
481 // we write only true attribute values so we remove a bit of the
482 // file format bloat for tabulars.
483 return b ? write_attribute(name, convert<string>(b)) : string();
488 string const write_attribute(string const & name, int const & i)
490 // we write only true attribute values so we remove a bit of the
491 // file format bloat for tabulars.
492 return i ? write_attribute(name, convert<string>(i)) : string();
497 string const write_attribute(string const & name, Tabular::idx_type const & i)
499 // we write only true attribute values so we remove a bit of the
500 // file format bloat for tabulars.
501 return i ? write_attribute(name, convert<string>(i)) : string();
506 string const write_attribute(string const & name, Length const & value)
508 // we write only the value if we really have one same reson as above.
509 return value.zero() ? string() : write_attribute(name, value.asString());
515 string const featureAsString(Tabular::Feature action)
517 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
518 if (tabularFeature[i].action == action)
519 return tabularFeature[i].feature;
525 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
527 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
528 for (; dit; dit.forwardChar())
529 if (dit.inTexted() && dit.depth() == 1
530 && dit.paragraph().find(align_d, false, false, dit.pos()))
537 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
539 InsetTableCell tail = InsetTableCell(head);
540 DocIterator const dit = separatorPos(&head, align_d);
543 pit_type const psize = head.paragraphs().front().size();
544 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
545 tail.paragraphs().front().eraseChars(0,
546 dit.pos() < psize ? dit.pos() + 1 : psize, false);
553 /////////////////////////////////////////////////////////////////////
557 /////////////////////////////////////////////////////////////////////
560 Tabular::CellData::CellData(Buffer * buf)
563 multicolumn(Tabular::CELL_NORMAL),
564 multirow(Tabular::CELL_NORMAL),
565 alignment(LYX_ALIGN_CENTER),
566 valignment(LYX_VALIGN_TOP),
576 inset(new InsetTableCell(buf))
578 inset->setBuffer(*buf);
582 Tabular::CellData::CellData(CellData const & cs)
585 multicolumn(cs.multicolumn),
586 multirow(cs.multirow),
587 mroffset(cs.mroffset),
588 alignment(cs.alignment),
589 valignment(cs.valignment),
590 decimal_hoffset(cs.decimal_hoffset),
591 decimal_width(cs.decimal_width),
593 top_line(cs.top_line),
594 bottom_line(cs.bottom_line),
595 left_line(cs.left_line),
596 right_line(cs.right_line),
599 align_special(cs.align_special),
601 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
605 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
611 void Tabular::CellData::swap(CellData & rhs)
613 std::swap(cellno, rhs.cellno);
614 std::swap(width, rhs.width);
615 std::swap(multicolumn, rhs.multicolumn);
616 std::swap(multirow, rhs.multirow);
617 std::swap(mroffset, rhs.mroffset);
618 std::swap(alignment, rhs.alignment);
619 std::swap(valignment, rhs.valignment);
620 std::swap(decimal_hoffset, rhs.decimal_hoffset);
621 std::swap(decimal_width, rhs.decimal_width);
622 std::swap(voffset, rhs.voffset);
623 std::swap(top_line, rhs.top_line);
624 std::swap(bottom_line, rhs.bottom_line);
625 std::swap(left_line, rhs.left_line);
626 std::swap(right_line, rhs.right_line);
627 std::swap(usebox, rhs.usebox);
628 std::swap(rotate, rhs.rotate);
629 std::swap(align_special, rhs.align_special);
630 p_width.swap(rhs.p_width);
631 inset.swap(rhs.inset);
635 Tabular::RowData::RowData()
638 top_space_default(false),
639 bottom_space_default(false),
640 interline_space_default(false),
650 Tabular::ColumnData::ColumnData()
651 : alignment(LYX_ALIGN_CENTER),
652 valignment(LYX_VALIGN_TOP),
658 Tabular::ltType::ltType()
665 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
667 init(buffer, rows_arg, columns_arg);
671 void Tabular::setBuffer(Buffer & buffer)
674 for (row_type i = 0; i < nrows(); ++i)
675 for (col_type j = 0; j < ncols(); ++j)
676 cell_info[i][j].inset->setBuffer(*buffer_);
680 // activates all lines and sets all widths to 0
681 void Tabular::init(Buffer * buf, row_type rows_arg,
682 col_type columns_arg)
685 row_info = row_vector(rows_arg);
686 column_info = column_vector(columns_arg);
687 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
688 row_info.reserve(10);
689 column_info.reserve(10);
690 cell_info.reserve(100);
692 is_long_tabular = false;
693 tabular_valignment = LYX_VALIGN_MIDDLE;
694 tabular_width = Length();
695 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
697 use_booktabs = false;
698 // set silly default lines
699 for (row_type r = 0; r < nrows(); ++r)
700 for (col_type c = 0; c < ncols(); ++c) {
701 cell_info[r][c].inset->setBuffer(*buffer_);
702 cell_info[r][c].top_line = true;
703 cell_info[r][c].left_line = true;
704 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
705 cell_info[r][c].right_line = c == ncols() - 1;
710 void Tabular::deleteRow(row_type const row)
712 // Not allowed to delete last row
716 for (col_type c = 0; c < ncols(); ++c) {
717 // Care about multirow cells
718 if (row + 1 < nrows() &&
719 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
720 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
721 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
724 row_info.erase(row_info.begin() + row);
725 cell_info.erase(cell_info.begin() + row);
730 void Tabular::copyRow(row_type const row)
732 insertRow(row, true);
736 void Tabular::appendRow(row_type row)
738 insertRow(row, false);
742 void Tabular::insertRow(row_type const row, bool copy)
744 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
745 cell_info.insert(cell_info.begin() + row + 1,
746 cell_vector(0, CellData(buffer_)));
748 for (col_type c = 0; c < ncols(); ++c) {
749 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
750 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
751 if (buffer().params().track_changes)
752 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
753 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
754 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
758 for (col_type c = 0; c < ncols(); ++c) {
759 if (isPartOfMultiRow(row, c))
761 // inherit line settings
762 idx_type const i = cellIndex(row + 1, c);
763 idx_type const j = cellIndex(row, c);
764 setLeftLine(i, leftLine(j));
765 setRightLine(i, rightLine(j));
766 setTopLine(i, topLine(j));
767 if (topLine(j) && bottomLine(j)) {
768 setBottomLine(i, true);
769 setBottomLine(j, false);
771 // mark track changes
772 if (buffer().params().track_changes)
773 cellInfo(i).inset->setChange(Change(Change::INSERTED));
778 void Tabular::moveColumn(col_type col, ColDirection direction)
780 if (direction == Tabular::LEFT)
783 std::swap(column_info[col], column_info[col + 1]);
785 for (row_type r = 0; r < nrows(); ++r) {
786 std::swap(cell_info[r][col], cell_info[r][col + 1]);
787 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
788 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
790 // FIXME track changes is broken for tabular features (#8469)
791 idx_type const i = cellIndex(r, col);
792 idx_type const j = cellIndex(r, col + 1);
793 if (buffer().params().track_changes) {
794 cellInfo(i).inset->setChange(Change(Change::INSERTED));
795 cellInfo(j).inset->setChange(Change(Change::INSERTED));
802 void Tabular::moveRow(row_type row, RowDirection direction)
804 if (direction == Tabular::UP)
807 std::swap(row_info[row], row_info[row + 1]);
809 for (col_type c = 0; c < ncols(); ++c) {
810 std::swap(cell_info[row][c], cell_info[row + 1][c]);
811 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
812 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
814 // FIXME track changes is broken for tabular features (#8469)
815 idx_type const i = cellIndex(row, c);
816 idx_type const j = cellIndex(row + 1, c);
817 if (buffer().params().track_changes) {
818 cellInfo(i).inset->setChange(Change(Change::INSERTED));
819 cellInfo(j).inset->setChange(Change(Change::INSERTED));
826 void Tabular::deleteColumn(col_type const col)
828 // Not allowed to delete last column
832 for (row_type r = 0; r < nrows(); ++r) {
833 // Care about multicolumn cells
834 if (col + 1 < ncols() &&
835 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
836 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
837 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
839 cell_info[r].erase(cell_info[r].begin() + col);
841 column_info.erase(column_info.begin() + col);
846 void Tabular::copyColumn(col_type const col)
848 insertColumn(col, true);
852 void Tabular::appendColumn(col_type col)
854 insertColumn(col, false);
858 void Tabular::insertColumn(col_type const col, bool copy)
860 BufferParams const & bp = buffer().params();
861 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
863 for (row_type r = 0; r < nrows(); ++r) {
864 cell_info[r].insert(cell_info[r].begin() + col + 1,
865 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
866 if (bp.track_changes)
867 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
868 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
869 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
872 for (row_type r = 0; r < nrows(); ++r) {
873 // inherit line settings
874 idx_type const i = cellIndex(r, col + 1);
875 idx_type const j = cellIndex(r, col);
876 setBottomLine(i, bottomLine(j));
877 setTopLine(i, topLine(j));
878 setLeftLine(i, leftLine(j));
879 if (rightLine(i) && rightLine(j)) {
880 setRightLine(i, true);
881 setRightLine(j, false);
883 if (buffer().params().track_changes)
884 cellInfo(i).inset->setChange(Change(Change::INSERTED));
889 void Tabular::updateIndexes()
894 for (row_type row = 0; row < nrows(); ++row)
895 for (col_type column = 0; column < ncols(); ++column) {
896 if (!isPartOfMultiColumn(row, column)
897 && !isPartOfMultiRow(row, column))
899 if (isPartOfMultiRow(row, column))
900 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
902 cell_info[row][column].cellno = numberofcells - 1;
905 rowofcell.resize(numberofcells);
906 columnofcell.resize(numberofcells);
908 // reset column and row of cells and update their width and alignment
909 for (row_type row = 0; row < nrows(); ++row)
910 for (col_type column = 0; column < ncols(); ++column) {
911 if (isPartOfMultiColumn(row, column))
913 // columnofcell needs to be called before setting width and aligment
914 // multirow cells inherit the width from the column width
915 if (!isPartOfMultiRow(row, column)) {
916 columnofcell[i] = column;
919 setFixedWidth(row, column);
920 if (isPartOfMultiRow(row, column))
922 cell_info[row][column].inset->setContentAlignment(
923 getAlignment(cellIndex(row, column)));
929 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
932 for (col_type c = 0; c < ncols(); ++c)
933 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
939 bool Tabular::topLine(idx_type const cell) const
941 return cellInfo(cell).top_line;
945 bool Tabular::bottomLine(idx_type const cell) const
947 return cellInfo(cell).bottom_line;
951 bool Tabular::leftLine(idx_type cell) const
955 return cellInfo(cell).left_line;
959 bool Tabular::rightLine(idx_type cell) const
963 return cellInfo(cell).right_line;
967 int Tabular::interRowSpace(row_type row) const
969 if (!row || row >= nrows())
972 int const interline_space = row_info[row - 1].interline_space_default ?
974 row_info[row - 1].interline_space.inPixels(width());
975 if (rowTopLine(row) && rowBottomLine(row - 1))
976 return interline_space + WIDTH_OF_LINE;
977 return interline_space;
981 int Tabular::interColumnSpace(idx_type cell) const
983 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
984 if (rightLine(cell) && nextcol < ncols()
985 && leftLine(cellIndex(cellRow(cell), nextcol)))
986 return WIDTH_OF_LINE;
991 int Tabular::cellWidth(idx_type cell) const
994 col_type const span = columnSpan(cell);
995 col_type const col = cellColumn(cell);
996 for(col_type c = col; c < col + span ; ++c)
997 w += column_info[c].width;
1002 int Tabular::cellHeight(idx_type cell) const
1004 row_type const span = rowSpan(cell);
1005 row_type const row = cellRow(cell);
1007 for(row_type r = row; r < row + span ; ++r) {
1008 h += rowAscent(r) + rowDescent(r);
1009 if (r != row + span - 1)
1010 h += interRowSpace(r + 1);
1017 bool Tabular::updateColumnWidths()
1019 vector<int> max_dwidth(ncols(), 0);
1020 for(col_type c = 0; c < ncols(); ++c)
1021 for(row_type r = 0; r < nrows(); ++r) {
1022 idx_type const i = cellIndex(r, c);
1023 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1024 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1027 bool update = false;
1028 // for each col get max of single col cells
1029 for(col_type c = 0; c < ncols(); ++c) {
1031 for(row_type r = 0; r < nrows(); ++r) {
1032 idx_type const i = cellIndex(r, c);
1033 if (columnSpan(i) == 1) {
1034 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1035 && cell_info[r][c].decimal_width!=0)
1036 new_width = max(new_width, cellInfo(i).width
1037 + max_dwidth[c] - cellInfo(i).decimal_width);
1039 new_width = max(new_width, cellInfo(i).width);
1043 if (column_info[c].width != new_width) {
1044 column_info[c].width = new_width;
1048 // update col widths to fit merged cells
1049 for(col_type c = 0; c < ncols(); ++c)
1050 for(row_type r = 0; r < nrows(); ++r) {
1051 idx_type const i = cellIndex(r, c);
1052 int const span = columnSpan(i);
1053 if (span == 1 || c > cellColumn(i))
1057 for(col_type j = c; j < c + span ; ++j)
1058 old_width += column_info[j].width;
1060 if (cellInfo(i).width > old_width) {
1061 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1070 int Tabular::width() const
1073 for (col_type c = 0; c < ncols(); ++c)
1074 width += column_info[c].width;
1079 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1082 col_type const col = cellColumn(cell);
1083 // set alignment for the whole row if we are not in a multicolumn cell,
1084 // exclude possible multicolumn cells in the row
1085 if (!isMultiColumn(cell)) {
1086 for (row_type r = 0; r < nrows(); ++r) {
1087 // only if the column has no width the multirow inherits the
1088 // alignment of the column, otherwise it is left aligned
1089 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1090 && !isMultiColumn(cellIndex(r, col))) {
1091 cell_info[r][col].alignment = align;
1092 cell_info[r][col].inset->setContentAlignment(align);
1094 if ((isMultiRow(cellIndex(r, col)) && has_width)
1095 && !isMultiColumn(cellIndex(r, col))) {
1096 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1097 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1100 column_info[col].alignment = align;
1101 docstring & dpoint = column_info[col].decimal_point;
1102 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1103 dpoint = from_utf8(lyxrc.default_decimal_point);
1105 cellInfo(cell).alignment = align;
1106 cellInset(cell).get()->setContentAlignment(align);
1111 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1114 if (!isMultiColumn(cell) || onlycolumn)
1115 column_info[cellColumn(cell)].valignment = align;
1117 cellInfo(cell).valignment = align;
1124 * Allow line and paragraph breaks for fixed width cells or disallow them,
1125 * merge cell paragraphs and reset layout to standard for variable width
1128 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1130 inset->setAutoBreakRows(fixedWidth);
1131 inset->toggleFixedWidth(fixedWidth);
1135 // merge all paragraphs to one
1136 BufferParams const & bp = cur.bv().buffer().params();
1137 while (inset->paragraphs().size() > 1)
1138 mergeParagraph(bp, inset->paragraphs(), 0);
1142 // undo information has already been recorded
1143 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1144 bp.documentClass().plainLayoutName());
1151 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1152 Length const & width)
1154 col_type const c = cellColumn(cell);
1156 column_info[c].p_width = width;
1157 // reset the vertical alignment to top if the fixed width
1158 // is removed or zero because only fixed width columns can
1159 // have a vertical alignment
1160 if (column_info[c].p_width.zero())
1161 column_info[c].valignment = LYX_VALIGN_TOP;
1162 for (row_type r = 0; r < nrows(); ++r) {
1163 idx_type const cell = cellIndex(r, c);
1164 // because of multicolumns
1165 toggleFixedWidth(cur, cellInset(cell).get(),
1166 !getPWidth(cell).zero());
1167 if (isMultiRow(cell))
1168 setAlignment(cell, LYX_ALIGN_LEFT, false);
1170 // cur paragraph can become invalid after paragraphs were merged
1171 if (cur.pit() > cur.lastpit())
1172 cur.pit() = cur.lastpit();
1173 // cur position can become invalid after newlines were removed
1174 if (cur.pos() > cur.lastpos())
1175 cur.pos() = cur.lastpos();
1179 bool Tabular::setFixedWidth(row_type r, col_type c)
1181 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1182 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1183 || (multicol && !cell_info[r][c].p_width.zero());
1184 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1189 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1190 Length const & width)
1192 if (!isMultiColumn(cell))
1195 cellInfo(cell).p_width = width;
1196 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1197 // cur paragraph can become invalid after paragraphs were merged
1198 if (cur.pit() > cur.lastpit())
1199 cur.pit() = cur.lastpit();
1200 // cur position can become invalid after newlines were removed
1201 if (cur.pos() > cur.lastpos())
1202 cur.pos() = cur.lastpos();
1207 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1209 cellInfo(cell).mroffset = mroffset;
1214 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1215 Tabular::Feature what)
1217 if (what == SET_SPECIAL_MULTICOLUMN)
1218 cellInfo(cell).align_special = special;
1220 column_info[cellColumn(cell)].align_special = special;
1224 void Tabular::setTopLine(idx_type i, bool line)
1226 cellInfo(i).top_line = line;
1230 void Tabular::setBottomLine(idx_type i, bool line)
1232 cellInfo(i).bottom_line = line;
1236 void Tabular::setLeftLine(idx_type cell, bool line)
1238 cellInfo(cell).left_line = line;
1242 void Tabular::setRightLine(idx_type cell, bool line)
1244 cellInfo(cell).right_line = line;
1247 bool Tabular::rowTopLine(row_type r) const
1249 bool all_rows_set = true;
1250 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1251 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1252 return all_rows_set;
1256 bool Tabular::rowBottomLine(row_type r) const
1258 bool all_rows_set = true;
1259 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1260 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1261 return all_rows_set;
1265 bool Tabular::columnLeftLine(col_type c) const
1272 for (row_type r = 0; r < nrows(); ++r) {
1273 idx_type const i = cellIndex(r, c);
1274 if (c == cellColumn(i)) {
1276 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1277 if (cellInfo(i).left_line || right)
1281 return 2 * nrows_left >= total;
1285 bool Tabular::columnRightLine(col_type c) const
1290 int nrows_right = 0;
1292 for (row_type r = 0; r < nrows(); ++r) {
1293 idx_type i = cellIndex(r, c);
1294 if (c == cellColumn(i) + columnSpan(i) - 1) {
1296 bool left = (c + 1 < ncols()
1297 && cellInfo(cellIndex(r, c + 1)).left_line)
1298 || c + 1 == ncols();
1299 if (cellInfo(i).right_line && left)
1303 return 2 * nrows_right >= total;
1307 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1309 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1310 return cellInfo(cell).alignment;
1312 return column_info[cellColumn(cell)].alignment;
1317 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1319 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1320 return cellInfo(cell).valignment;
1321 return column_info[cellColumn(cell)].valignment;
1325 Length const Tabular::getPWidth(idx_type cell) const
1327 if (isMultiColumn(cell))
1328 return cellInfo(cell).p_width;
1329 return column_info[cellColumn(cell)].p_width;
1333 Length const Tabular::getMROffset(idx_type cell) const
1335 return cellInfo(cell).mroffset;
1339 int Tabular::textHOffset(idx_type cell) const
1341 // the LaTeX Way :-(
1342 int x = WIDTH_OF_LINE;
1344 int const w = cellWidth(cell) - cellInfo(cell).width;
1346 switch (getAlignment(cell)) {
1347 case LYX_ALIGN_CENTER:
1350 case LYX_ALIGN_RIGHT:
1353 case LYX_ALIGN_DECIMAL: {
1354 // we center when no decimal point
1355 if (cellInfo(cell).decimal_width == 0) {
1359 col_type const c = cellColumn(cell);
1360 int max_dhoffset = 0;
1361 for(row_type r = 0; r < row_info.size() ; ++r) {
1362 idx_type const i = cellIndex(r, c);
1363 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1364 && cellInfo(i).decimal_width != 0)
1365 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1367 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1370 // LYX_ALIGN_LEFT: nothing :-)
1378 int Tabular::textVOffset(idx_type cell) const
1380 int voffset = cellInfo(cell).voffset;
1381 if (isMultiRow(cell)) {
1382 row_type const row = cellRow(cell);
1383 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1389 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1392 idx_type const numcells = numberOfCellsInRow(row);
1393 // we check against numcells to make sure we do not crash if all the
1394 // cells are multirow (bug #7535), but in that case our return value
1395 // is really invalid, i.e., it is NOT the first cell in the row. but
1396 // i do not know what to do here. (rgh)
1397 while (c < numcells - 1
1398 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1400 return cell_info[row][c].cellno;
1404 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1406 col_type c = ncols() - 1;
1407 // of course we check against 0 so we don't crash. but we have the same
1408 // problem as in the previous routine: if all the cells are part of a
1409 // multirow or part of a multi column, then our return value is invalid.
1411 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1412 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1414 return cell_info[row][c].cellno;
1418 Tabular::row_type Tabular::cellRow(idx_type cell) const
1420 if (cell >= numberofcells)
1424 return rowofcell[cell];
1428 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1430 if (cell >= numberofcells)
1434 return columnofcell[cell];
1438 void Tabular::write(ostream & os) const
1442 << write_attribute("version", 3)
1443 << write_attribute("rows", nrows())
1444 << write_attribute("columns", ncols())
1446 // global longtable options
1448 << write_attribute("rotate", convert<string>(rotate))
1449 << write_attribute("booktabs", use_booktabs)
1450 << write_attribute("islongtable", is_long_tabular)
1451 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1452 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1453 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1454 << write_attribute("headTopDL", endhead.topDL)
1455 << write_attribute("headBottomDL", endhead.bottomDL)
1456 << write_attribute("footTopDL", endfoot.topDL)
1457 << write_attribute("footBottomDL", endfoot.bottomDL)
1458 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1459 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1460 << write_attribute("lastFootEmpty", endlastfoot.empty);
1461 // longtables cannot be aligned vertically
1462 if (!is_long_tabular) {
1463 os << write_attribute("tabularvalignment", tabular_valignment);
1464 os << write_attribute("tabularwidth", tabular_width);
1466 if (is_long_tabular)
1467 os << write_attribute("longtabularalignment", longtabular_alignment);
1469 for (col_type c = 0; c < ncols(); ++c) {
1471 << write_attribute("alignment", column_info[c].alignment);
1472 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1473 os << write_attribute("decimal_point", column_info[c].decimal_point);
1474 os << write_attribute("valignment", column_info[c].valignment)
1475 << write_attribute("width", column_info[c].p_width.asString())
1476 << write_attribute("special", column_info[c].align_special)
1479 for (row_type r = 0; r < nrows(); ++r) {
1480 static const string def("default");
1482 if (row_info[r].top_space_default)
1483 os << write_attribute("topspace", def);
1485 os << write_attribute("topspace", row_info[r].top_space);
1486 if (row_info[r].bottom_space_default)
1487 os << write_attribute("bottomspace", def);
1489 os << write_attribute("bottomspace", row_info[r].bottom_space);
1490 if (row_info[r].interline_space_default)
1491 os << write_attribute("interlinespace", def);
1493 os << write_attribute("interlinespace", row_info[r].interline_space);
1494 os << write_attribute("endhead", row_info[r].endhead)
1495 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1496 << write_attribute("endfoot", row_info[r].endfoot)
1497 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1498 << write_attribute("newpage", row_info[r].newpage)
1499 << write_attribute("caption", row_info[r].caption)
1501 for (col_type c = 0; c < ncols(); ++c) {
1503 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1504 << write_attribute("multirow", cell_info[r][c].multirow)
1505 << write_attribute("mroffset", cell_info[r][c].mroffset)
1506 << write_attribute("alignment", cell_info[r][c].alignment)
1507 << write_attribute("valignment", cell_info[r][c].valignment)
1508 << write_attribute("topline", cell_info[r][c].top_line)
1509 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1510 << write_attribute("leftline", cell_info[r][c].left_line)
1511 << write_attribute("rightline", cell_info[r][c].right_line)
1512 << write_attribute("rotate", cell_info[r][c].rotate)
1513 << write_attribute("usebox", cell_info[r][c].usebox)
1514 << write_attribute("width", cell_info[r][c].p_width)
1515 << write_attribute("special", cell_info[r][c].align_special)
1517 os << "\\begin_inset ";
1518 cell_info[r][c].inset->write(os);
1519 os << "\n\\end_inset\n"
1524 os << "</lyxtabular>\n";
1528 void Tabular::read(Lexer & lex)
1531 istream & is = lex.getStream();
1533 l_getline(is, line);
1534 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1535 LASSERT(false, return);
1539 if (!getTokenValue(line, "version", version))
1541 LATTEST(version >= 2);
1544 if (!getTokenValue(line, "rows", rows_arg))
1547 if (!getTokenValue(line, "columns", columns_arg))
1549 init(buffer_, rows_arg, columns_arg);
1550 l_getline(is, line);
1551 if (!prefixIs(line, "<features")) {
1552 lyxerr << "Wrong tabular format (expected <features ...> got"
1553 << line << ')' << endl;
1556 getTokenValue(line, "rotate", rotate);
1557 getTokenValue(line, "booktabs", use_booktabs);
1558 getTokenValue(line, "islongtable", is_long_tabular);
1559 getTokenValue(line, "tabularvalignment", tabular_valignment);
1560 getTokenValue(line, "tabularwidth", tabular_width);
1561 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1562 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1563 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1564 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1565 getTokenValue(line, "headTopDL", endhead.topDL);
1566 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1567 getTokenValue(line, "footTopDL", endfoot.topDL);
1568 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1569 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1570 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1571 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1573 for (col_type c = 0; c < ncols(); ++c) {
1575 if (!prefixIs(line,"<column")) {
1576 lyxerr << "Wrong tabular format (expected <column ...> got"
1577 << line << ')' << endl;
1580 getTokenValue(line, "alignment", column_info[c].alignment);
1581 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1582 getTokenValue(line, "valignment", column_info[c].valignment);
1583 getTokenValue(line, "width", column_info[c].p_width);
1584 getTokenValue(line, "special", column_info[c].align_special);
1587 for (row_type i = 0; i < nrows(); ++i) {
1588 l_getline(is, line);
1589 if (!prefixIs(line, "<row")) {
1590 lyxerr << "Wrong tabular format (expected <row ...> got"
1591 << line << ')' << endl;
1594 getTokenValue(line, "topspace", row_info[i].top_space,
1595 row_info[i].top_space_default);
1596 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1597 row_info[i].bottom_space_default);
1598 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1599 row_info[i].interline_space_default);
1600 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1601 getTokenValue(line, "endhead", row_info[i].endhead);
1602 getTokenValue(line, "endfoot", row_info[i].endfoot);
1603 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1604 getTokenValue(line, "newpage", row_info[i].newpage);
1605 getTokenValue(line, "caption", row_info[i].caption);
1606 for (col_type j = 0; j < ncols(); ++j) {
1607 l_getline(is, line);
1608 if (!prefixIs(line, "<cell")) {
1609 lyxerr << "Wrong tabular format (expected <cell ...> got"
1610 << line << ')' << endl;
1613 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1614 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1615 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1616 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1617 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1618 getTokenValue(line, "topline", cell_info[i][j].top_line);
1619 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1620 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1621 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1622 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1623 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1624 getTokenValue(line, "width", cell_info[i][j].p_width);
1626 getTokenValue(line, "special", cell_info[i][j].align_special);
1627 l_getline(is, line);
1628 if (prefixIs(line, "\\begin_inset")) {
1629 cell_info[i][j].inset->setBuffer(*buffer_);
1630 cell_info[i][j].inset->read(lex);
1631 l_getline(is, line);
1633 if (!prefixIs(line, "</cell>")) {
1634 lyxerr << "Wrong tabular format (expected </cell> got"
1635 << line << ')' << endl;
1639 l_getline(is, line);
1640 if (!prefixIs(line, "</row>")) {
1641 lyxerr << "Wrong tabular format (expected </row> got"
1642 << line << ')' << endl;
1646 while (!prefixIs(line, "</lyxtabular>")) {
1647 l_getline(is, line);
1653 bool Tabular::isMultiColumn(idx_type cell) const
1655 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1656 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1660 bool Tabular::hasMultiColumn(col_type c) const
1662 for (row_type r = 0; r < nrows(); ++r) {
1663 if (isMultiColumn(cellIndex(r, c)))
1670 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1672 return cell_info[cellRow(cell)][cellColumn(cell)];
1676 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1677 bool const right_border)
1679 idx_type const col = cellColumn(cell);
1680 idx_type const row = cellRow(cell);
1681 for (idx_type i = 0; i < number; ++i)
1682 unsetMultiRow(cellIndex(row, col + i));
1684 // unsetting of multirow may have invalidated cell index
1685 cell = cellIndex(row, col);
1686 CellData & cs = cellInfo(cell);
1687 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1688 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1689 cs.alignment = column_info[col].alignment;
1690 setRightLine(cell, right_border);
1692 for (idx_type i = 1; i < number; ++i) {
1693 CellData & cs1 = cellInfo(cell + i);
1694 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1695 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1703 bool Tabular::isMultiRow(idx_type cell) const
1705 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1706 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1709 bool Tabular::hasMultiRow(row_type r) const
1711 for (col_type c = 0; c < ncols(); ++c) {
1712 if (isMultiRow(cellIndex(r, c)))
1718 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1719 bool const bottom_border,
1720 LyXAlignment const halign)
1722 idx_type const col = cellColumn(cell);
1723 idx_type const row = cellRow(cell);
1724 for (idx_type i = 0; i < number; ++i)
1725 unsetMultiColumn(cellIndex(row + i, col));
1727 // unsetting of multirow may have invalidated cell index
1728 cell = cellIndex(row, col);
1729 CellData & cs = cellInfo(cell);
1730 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1731 cs.valignment = LYX_VALIGN_MIDDLE;
1732 // the horizontal alignment of multirow cells can only
1733 // be changed for the whole table row,
1734 // support changing this only for the multirow cell can be done via
1736 if (getPWidth(cell).zero())
1737 cs.alignment = halign;
1739 cs.alignment = LYX_ALIGN_LEFT;
1741 // set the bottom line of the last selected cell
1742 setBottomLine(cell, bottom_border);
1744 for (idx_type i = 1; i < number; ++i) {
1745 CellData & cs1 = cell_info[row + i][col];
1746 cs1.multirow = CELL_PART_OF_MULTIROW;
1747 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1755 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1757 row_type const row = cellRow(cell);
1758 col_type const col = cellColumn(cell);
1760 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1767 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1769 col_type const column = cellColumn(cell);
1770 col_type row = cellRow(cell) + 1;
1771 while (row < nrows() && isPartOfMultiRow(row, column))
1774 return row - cellRow(cell);
1778 void Tabular::unsetMultiColumn(idx_type cell)
1780 if (!isMultiColumn(cell))
1783 row_type const row = cellRow(cell);
1784 col_type const col = cellColumn(cell);
1785 row_type const span = columnSpan(cell);
1786 for (col_type c = 0; c < span; ++c) {
1787 // in the table dialog the lines are set in every case
1788 // when unsetting a multicolumn this leads to an additional right
1789 // line for every cell that was part of the former multicolumn cell,
1790 // except if the cell is in the last column
1791 // therefore remove this line
1792 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1793 && (col + c) < (col + span - 1))
1794 cell_info[row][col + c].right_line = false;
1795 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1801 void Tabular::unsetMultiRow(idx_type cell)
1803 if (!isMultiRow(cell))
1806 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1807 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1808 row_type const row = cellRow(cell);
1809 col_type const col = cellColumn(cell);
1810 row_type const span = rowSpan(cell);
1811 for (row_type r = 0; r < span; ++r)
1812 cell_info[row + r][col].multirow = CELL_NORMAL;
1817 void Tabular::setRotateCell(idx_type cell, int value)
1819 cellInfo(cell).rotate = value;
1823 int Tabular::getRotateCell(idx_type cell) const
1825 return cellInfo(cell).rotate;
1829 bool Tabular::needRotating() const
1833 for (row_type r = 0; r < nrows(); ++r)
1834 for (col_type c = 0; c < ncols(); ++c)
1835 if (cell_info[r][c].rotate != 0)
1841 bool Tabular::isLastCell(idx_type cell) const
1843 if (cell + 1 < numberofcells)
1849 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1851 if (cellRow(cell) == 0)
1854 col_type const col = cellColumn(cell);
1855 row_type r = cellRow(cell) - 1;
1856 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1859 return cell_info[r][col].cellno;
1863 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1865 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1866 if (nextrow < nrows())
1867 return cell_info[nextrow][cellColumn(cell)].cellno;
1872 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1874 LASSERT(column != npos && column < ncols(), column = 0);
1875 LASSERT(row != npos && row < nrows(), row = 0);
1876 return cell_info[row][column].cellno;
1880 void Tabular::setUsebox(idx_type cell, BoxType type)
1882 cellInfo(cell).usebox = type;
1886 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1887 // adds line breaks, see bug 4886.
1888 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1890 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1891 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1893 if (cellInfo(cell).usebox > 1)
1894 return cellInfo(cell).usebox;
1895 return useParbox(cell);
1900 // This are functions used for the longtable support
1902 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1908 row_info[row].endfirsthead = flag;
1912 row_info[row].endhead = flag;
1917 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1920 hd.set = haveLTHead();
1921 return row_info[row].endhead;
1925 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1928 hd.set = haveLTFirstHead();
1929 return row_info[row].endfirsthead;
1933 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1939 row_info[row].endlastfoot = flag;
1943 row_info[row].endfoot = flag;
1948 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1951 fd.set = haveLTFoot();
1952 return row_info[row].endfoot;
1956 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1959 fd.set = haveLTLastFoot();
1960 return row_info[row].endlastfoot;
1964 void Tabular::setLTNewPage(row_type row, bool what)
1966 row_info[row].newpage = what;
1970 bool Tabular::getLTNewPage(row_type row) const
1972 return row_info[row].newpage;
1976 bool Tabular::haveLTHead(bool withcaptions) const
1978 if (!is_long_tabular)
1980 for (row_type i = 0; i < nrows(); ++i)
1981 if (row_info[i].endhead &&
1982 (withcaptions || !row_info[i].caption))
1988 bool Tabular::haveLTFirstHead(bool withcaptions) const
1990 if (!is_long_tabular || endfirsthead.empty)
1992 for (row_type r = 0; r < nrows(); ++r)
1993 if (row_info[r].endfirsthead &&
1994 (withcaptions || !row_info[r].caption))
2000 bool Tabular::haveLTFoot(bool withcaptions) const
2002 if (!is_long_tabular)
2004 for (row_type r = 0; r < nrows(); ++r)
2005 if (row_info[r].endfoot &&
2006 (withcaptions || !row_info[r].caption))
2012 bool Tabular::haveLTLastFoot(bool withcaptions) const
2014 if (!is_long_tabular || endlastfoot.empty)
2016 for (row_type r = 0; r < nrows(); ++r)
2017 if (row_info[r].endlastfoot &&
2018 (withcaptions || !row_info[r].caption))
2024 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2026 idx_type i = getFirstCellInRow(row);
2028 setMultiColumn(i, numberOfCellsInRow(row), false);
2029 setTopLine(i, false);
2030 setBottomLine(i, false);
2031 setLeftLine(i, false);
2032 setRightLine(i, false);
2033 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2034 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2035 setLTHead(row, true, endfirsthead, true);
2036 row_info[row].endfirsthead = true;
2039 unsetMultiColumn(i);
2040 // When unsetting a caption row, also all existing
2041 // captions in this row must be dissolved.
2043 row_info[row].caption = what;
2048 bool Tabular::ltCaption(row_type row) const
2050 return row_info[row].caption;
2054 bool Tabular::haveLTCaption(CaptionType captiontype) const
2056 if (!is_long_tabular)
2058 for (row_type r = 0; r < nrows(); ++r) {
2059 if (row_info[r].caption) {
2060 switch (captiontype) {
2061 case CAPTION_FIRSTHEAD:
2062 if (row_info[r].endfirsthead)
2066 if (row_info[r].endhead)
2070 if (row_info[r].endfoot)
2073 case CAPTION_LASTFOOT:
2074 if (row_info[r].endlastfoot)
2086 // end longtable support functions
2088 void Tabular::setRowAscent(row_type row, int height)
2090 if (row >= nrows() || row_info[row].ascent == height)
2092 row_info[row].ascent = height;
2096 void Tabular::setRowDescent(row_type row, int height)
2098 if (row >= nrows() || row_info[row].descent == height)
2100 row_info[row].descent = height;
2104 int Tabular::rowAscent(row_type row) const
2106 LASSERT(row < nrows(), row = 0);
2107 return row_info[row].ascent;
2111 int Tabular::rowDescent(row_type row) const
2113 LASSERT(row < nrows(), row = 0);
2114 return row_info[row].descent;
2118 int Tabular::height() const
2121 for (row_type row = 0; row < nrows(); ++row)
2122 height += rowAscent(row) + rowDescent(row) +
2128 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2130 LASSERT(row < nrows(), return false);
2131 LASSERT(column < ncols(), return false);
2132 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2136 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2138 LASSERT(row < nrows(), return false);
2139 LASSERT(column < ncols(), return false);
2140 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2144 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2146 // we only output complete row lines and the 1st row here, the rest
2147 // is done in Tabular::TeXBottomHLine(...)
2149 // get for each column the topline (if any)
2150 vector<bool> topline;
2152 for (col_type c = 0; c < ncols(); ++c) {
2153 topline.push_back(topLine(cellIndex(row, c)));
2154 // If cell is part of a multirow and not the first cell of the
2155 // multirow, no line must be drawn.
2157 if (isMultiRow(cellIndex(row, c))
2158 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2164 // do nothing if empty first row, or incomplete row line after
2165 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2168 // only output complete row lines and the 1st row's clines
2169 if (nset == ncols()) {
2171 os << (row == 0 ? "\\toprule " : "\\midrule ");
2175 } else if (row == 0) {
2176 for (col_type c = 0; c < ncols(); ++c) {
2178 col_type offset = 0;
2179 for (col_type j = 0 ; j < c; ++j)
2180 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2183 //babel makes the "-" character an active one, so we have to suppress this here
2184 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2185 if (lang == "slovak" || lang == "czech")
2186 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2187 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2189 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2191 col_type cstart = c;
2192 for ( ; c < ncols() && topline[c]; ++c) {}
2194 for (col_type j = cstart ; j < c ; ++j)
2195 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2198 os << c + offset << "} ";
2206 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2208 // we output bottomlines of row r and the toplines of row r+1
2209 // if the latter do not span the whole tabular
2211 // get the bottomlines of row r, and toplines in next row
2212 bool lastrow = row == nrows() - 1;
2213 vector<bool> bottomline, topline;
2214 bool nextrowset = true;
2215 for (col_type c = 0; c < ncols(); ++c) {
2216 bottomline.push_back(bottomLine(cellIndex(row, c)));
2217 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2218 // If cell is part of a multirow and not the last cell of the
2219 // multirow, no line must be drawn.
2221 if (isMultiRow(cellIndex(row, c))
2222 && isMultiRow(cellIndex(row + 1, c))
2223 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2224 bottomline[c] = false;
2227 nextrowset &= topline[c];
2230 // combine this row's bottom lines and next row's toplines if necessary
2232 for (col_type c = 0; c < ncols(); ++c) {
2234 bottomline[c] = bottomline[c] || topline[c];
2239 // do nothing if empty, OR incomplete row line with a topline in next row
2240 if (nset == 0 || (nextrowset && nset != ncols()))
2243 if (nset == ncols()) {
2245 os << (lastrow ? "\\bottomrule" : "\\midrule");
2249 for (col_type c = 0; c < ncols(); ++c) {
2250 if (bottomline[c]) {
2251 col_type offset = 0;
2252 for (col_type j = 0 ; j < c; ++j)
2253 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2256 //babel makes the "-" character an active one, so we have to suppress this here
2257 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2258 if (lang == "slovak" || lang == "czech")
2259 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2260 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2262 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2264 col_type cstart = c;
2265 for ( ; c < ncols() && bottomline[c]; ++c) {}
2267 for (col_type j = cstart ; j < c ; ++j)
2268 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2271 os << c + offset << "} ";
2279 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2280 bool & ismulticol, bool & ismultirow) const
2282 row_type const r = cellRow(cell);
2283 if (is_long_tabular && row_info[r].caption)
2286 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2287 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2288 // figure out how to set the lines
2289 // we always set double lines to the right of the cell
2290 col_type const c = cellColumn(cell);
2291 col_type const nextcol = c + columnSpan(cell);
2292 bool colright = columnRightLine(c);
2293 bool colleft = columnLeftLine(c);
2294 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2295 bool nextcellleft = nextcol < ncols()
2296 && leftLine(cellIndex(r, nextcol));
2297 bool coldouble = colright && nextcolleft;
2298 bool celldouble = rightLine(cell) && nextcellleft;
2300 ismulticol = isMultiColumn(cell)
2301 || (c == 0 && colleft != leftLine(cell))
2302 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2303 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2304 || (coldouble != celldouble);
2306 // we center in multicol when no decimal point
2307 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2308 docstring const align_d = column_info[c].decimal_point;
2309 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2313 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2314 int latexcolspan = columnSpan(cell);
2315 for(col_type col = c; col < c + columnSpan(cell); ++col)
2316 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2320 os << "\\multicolumn{" << latexcolspan << "}{";
2321 if (c ==0 && leftLine(cell))
2323 if (!cellInfo(cell).align_special.empty()) {
2324 os << cellInfo(cell).align_special;
2326 if (!getPWidth(cell).zero()) {
2328 case LYX_ALIGN_LEFT:
2329 os << ">{\\raggedright}";
2331 case LYX_ALIGN_RIGHT:
2332 os << ">{\\raggedleft}";
2334 case LYX_ALIGN_CENTER:
2335 os << ">{\\centering}";
2341 case LYX_VALIGN_TOP:
2344 case LYX_VALIGN_MIDDLE:
2347 case LYX_VALIGN_BOTTOM:
2352 << from_ascii(getPWidth(cell).asLatexString())
2356 case LYX_ALIGN_LEFT:
2359 case LYX_ALIGN_RIGHT:
2366 } // end if else !getPWidth
2367 } // end if else !cellinfo_of_cell
2368 if (rightLine(cell) || nextcellleft)
2371 // add extra vertical line if we want a double one
2374 } // end if ismulticol
2376 // we only need code for the first multirow cell
2377 ismultirow = isMultiRow(cell);
2379 os << "\\multirow{" << rowSpan(cell) << "}{";
2380 if (!getPWidth(cell).zero())
2381 os << from_ascii(getPWidth(cell).asLatexString());
2383 // we need to set a default value
2386 if (!getMROffset(cell).zero())
2387 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2389 } // end if ismultirow
2391 if (getRotateCell(cell) != 0)
2392 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2394 if (getUsebox(cell) == BOX_PARBOX) {
2397 case LYX_VALIGN_TOP:
2400 case LYX_VALIGN_MIDDLE:
2403 case LYX_VALIGN_BOTTOM:
2407 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2409 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2410 os << "\\begin{minipage}[";
2412 case LYX_VALIGN_TOP:
2415 case LYX_VALIGN_MIDDLE:
2418 case LYX_VALIGN_BOTTOM:
2422 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2428 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2429 bool ismulticol, bool ismultirow) const
2431 row_type const r = cellRow(cell);
2432 if (is_long_tabular && row_info[r].caption)
2436 if (getUsebox(cell) == BOX_PARBOX)
2438 else if (getUsebox(cell) == BOX_MINIPAGE)
2439 os << breakln << "\\end{minipage}";
2440 if (getRotateCell(cell) != 0)
2441 os << breakln << "\\end{turn}";
2449 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2450 OutputParams const & runparams) const
2452 if (!is_long_tabular)
2456 // output caption which is in no header or footer
2457 if (haveLTCaption()) {
2458 for (row_type r = 0; r < nrows(); ++r) {
2459 if (row_info[r].caption &&
2460 !row_info[r].endfirsthead && !row_info[r].endhead &&
2461 !row_info[r].endfoot && !row_info[r].endlastfoot)
2462 TeXRow(os, r, runparams);
2465 // output first header info
2466 if (haveLTFirstHead()) {
2467 if (endfirsthead.topDL)
2469 for (row_type r = 0; r < nrows(); ++r) {
2470 if (row_info[r].endfirsthead)
2471 TeXRow(os, r, runparams);
2473 if (endfirsthead.bottomDL)
2475 os << "\\endfirsthead\n";
2477 // output header info
2479 if (endfirsthead.empty && !haveLTFirstHead())
2480 os << "\\endfirsthead\n";
2483 for (row_type r = 0; r < nrows(); ++r) {
2484 if (row_info[r].endhead)
2485 TeXRow(os, r, runparams);
2487 if (endhead.bottomDL)
2489 os << "\\endhead\n";
2491 // output footer info
2495 for (row_type r = 0; r < nrows(); ++r) {
2496 if (row_info[r].endfoot)
2497 TeXRow(os, r, runparams);
2499 if (endfoot.bottomDL)
2501 os << "\\endfoot\n";
2502 if (endlastfoot.empty && !haveLTLastFoot())
2503 os << "\\endlastfoot\n";
2505 // output lastfooter info
2506 if (haveLTLastFoot()) {
2507 if (endlastfoot.topDL)
2509 for (row_type r = 0; r < nrows(); ++r) {
2510 if (row_info[r].endlastfoot)
2511 TeXRow(os, r, runparams);
2513 if (endlastfoot.bottomDL)
2515 os << "\\endlastfoot\n";
2520 bool Tabular::isValidRow(row_type row) const
2522 if (!is_long_tabular)
2524 return !row_info[row].endhead && !row_info[row].endfirsthead
2525 && !row_info[row].endfoot && !row_info[row].endlastfoot
2526 && !row_info[row].caption;
2530 void Tabular::TeXRow(otexstream & os, row_type row,
2531 OutputParams const & runparams) const
2533 idx_type cell = cellIndex(row, 0);
2534 shared_ptr<InsetTableCell> inset = cellInset(cell);
2535 Paragraph const & par = inset->paragraphs().front();
2536 string const lang = par.getParLanguage(buffer().params())->lang();
2538 //output the top line
2539 TeXTopHLine(os, row, lang);
2541 if (row_info[row].top_space_default) {
2543 os << "\\addlinespace\n";
2545 os << "\\noalign{\\vskip\\doublerulesep}\n";
2546 } else if(!row_info[row].top_space.zero()) {
2548 os << "\\addlinespace["
2549 << from_ascii(row_info[row].top_space.asLatexString())
2552 os << "\\noalign{\\vskip"
2553 << from_ascii(row_info[row].top_space.asLatexString())
2557 bool ismulticol = false;
2558 bool ismultirow = false;
2559 for (col_type c = 0; c < ncols(); ++c) {
2560 if (isPartOfMultiColumn(row, c))
2563 cell = cellIndex(row, c);
2565 if (isPartOfMultiRow(row, c)
2566 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2567 if (cell != getLastCellInRow(row))
2572 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2573 shared_ptr<InsetTableCell> inset = cellInset(cell);
2575 Paragraph const & par = inset->paragraphs().front();
2576 bool rtl = par.isRTL(buffer().params())
2578 && getPWidth(cell).zero()
2579 && !runparams.use_polyglossia;
2583 par.getParLanguage(buffer().params())->lang();
2584 if (lang == "farsi")
2586 else if (lang == "arabic_arabi")
2588 // currently, remaining RTL languages are
2589 // arabic_arabtex and hebrew
2593 // pass to the OutputParams that we are in a cell and
2594 // which alignment we have set.
2595 // InsetNewline needs this context information.
2596 OutputParams newrp(runparams);
2597 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2598 ? OutputParams::PLAIN
2599 : OutputParams::ALIGNED;
2601 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2602 // copy cell and split in 2
2603 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2604 head.setBuffer(buffer());
2605 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2607 dit.push_back(CursorSlice(head));
2608 head.setMacrocontextPositionRecursive(dit);
2609 bool hassep = false;
2610 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2611 head.latex(os, newrp);
2614 tail.setBuffer(head.buffer());
2616 dit.push_back(CursorSlice(tail));
2617 tail.setMacrocontextPositionRecursive(dit);
2618 tail.latex(os, newrp);
2620 } else if (!isPartOfMultiRow(row, c)) {
2621 if (!runparams.nice)
2622 os.texrow().start(par.id(), 0);
2623 inset->latex(os, newrp);
2626 runparams.encoding = newrp.encoding;
2630 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2631 if (cell != getLastCellInRow(row)) { // not last cell in row
2638 os << "\\tabularnewline";
2639 if (row_info[row].bottom_space_default) {
2641 os << "\\addlinespace";
2643 os << "[\\doublerulesep]";
2644 } else if (!row_info[row].bottom_space.zero()) {
2646 os << "\\addlinespace";
2648 << from_ascii(row_info[row].bottom_space.asLatexString())
2653 //output the bottom line
2654 TeXBottomHLine(os, row, lang);
2656 if (row_info[row].interline_space_default) {
2658 os << "\\addlinespace\n";
2660 os << "\\noalign{\\vskip\\doublerulesep}\n";
2661 } else if (!row_info[row].interline_space.zero()) {
2663 os << "\\addlinespace["
2664 << from_ascii(row_info[row].interline_space.asLatexString())
2667 os << "\\noalign{\\vskip"
2668 << from_ascii(row_info[row].interline_space.asLatexString())
2674 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2676 bool const is_tabular_star = !tabular_width.zero();
2678 //+---------------------------------------------------------------------
2679 //+ first the opening preamble +
2680 //+---------------------------------------------------------------------
2683 if (runparams.lastid != -1)
2684 os.texrow().start(runparams.lastid, runparams.lastpos);
2687 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2689 if (is_long_tabular) {
2690 os << "\\begin{longtable}";
2691 switch (longtabular_alignment) {
2692 case LYX_LONGTABULAR_ALIGN_LEFT:
2695 case LYX_LONGTABULAR_ALIGN_CENTER:
2697 case LYX_LONGTABULAR_ALIGN_RIGHT:
2702 if (is_tabular_star)
2703 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2705 os << "\\begin{tabular}";
2706 switch (tabular_valignment) {
2707 case LYX_VALIGN_TOP:
2710 case LYX_VALIGN_MIDDLE:
2712 case LYX_VALIGN_BOTTOM:
2720 if (is_tabular_star)
2721 os << "@{\\extracolsep{\\fill}}";
2723 for (col_type c = 0; c < ncols(); ++c) {
2724 if (columnLeftLine(c))
2726 if (!column_info[c].align_special.empty()) {
2727 os << column_info[c].align_special;
2729 if (!column_info[c].p_width.zero()) {
2730 switch (column_info[c].alignment) {
2731 case LYX_ALIGN_LEFT:
2732 os << ">{\\raggedright}";
2734 case LYX_ALIGN_RIGHT:
2735 os << ">{\\raggedleft}";
2737 case LYX_ALIGN_CENTER:
2738 os << ">{\\centering}";
2740 case LYX_ALIGN_NONE:
2741 case LYX_ALIGN_BLOCK:
2742 case LYX_ALIGN_LAYOUT:
2743 case LYX_ALIGN_SPECIAL:
2744 case LYX_ALIGN_DECIMAL:
2748 switch (column_info[c].valignment) {
2749 case LYX_VALIGN_TOP:
2752 case LYX_VALIGN_MIDDLE:
2755 case LYX_VALIGN_BOTTOM:
2760 << from_ascii(column_info[c].p_width.asLatexString())
2763 switch (column_info[c].alignment) {
2764 case LYX_ALIGN_LEFT:
2767 case LYX_ALIGN_RIGHT:
2770 case LYX_ALIGN_DECIMAL:
2771 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2777 } // end if else !column_info[i].p_width
2778 } // end if else !column_info[i].align_special
2779 if (columnRightLine(c))
2784 TeXLongtableHeaderFooter(os, runparams);
2786 //+---------------------------------------------------------------------
2787 //+ the single row and columns (cells) +
2788 //+---------------------------------------------------------------------
2790 for (row_type r = 0; r < nrows(); ++r) {
2791 if (isValidRow(r)) {
2792 TeXRow(os, r, runparams);
2793 if (is_long_tabular && row_info[r].newpage)
2794 os << "\\newpage\n";
2798 //+---------------------------------------------------------------------
2799 //+ the closing of the tabular +
2800 //+---------------------------------------------------------------------
2802 if (is_long_tabular)
2803 os << "\\end{longtable}";
2805 if (is_tabular_star)
2806 os << "\\end{tabular*}";
2808 os << "\\end{tabular}";
2812 os << breakln << "\\end{turn}";
2816 int Tabular::docbookRow(odocstream & os, row_type row,
2817 OutputParams const & runparams) const
2820 idx_type cell = getFirstCellInRow(row);
2823 for (col_type c = 0; c < ncols(); ++c) {
2824 if (isPartOfMultiColumn(row, c))
2827 os << "<entry align=\"";
2828 switch (getAlignment(cell)) {
2829 case LYX_ALIGN_LEFT:
2832 case LYX_ALIGN_RIGHT:
2840 os << "\" valign=\"";
2841 switch (getVAlignment(cell)) {
2842 case LYX_VALIGN_TOP:
2845 case LYX_VALIGN_BOTTOM:
2848 case LYX_VALIGN_MIDDLE:
2853 if (isMultiColumn(cell)) {
2854 os << " namest=\"col" << c << "\" ";
2855 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2859 ret += cellInset(cell)->docbook(os, runparams);
2868 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2872 //+---------------------------------------------------------------------
2873 //+ first the opening preamble +
2874 //+---------------------------------------------------------------------
2876 os << "<tgroup cols=\"" << ncols()
2877 << "\" colsep=\"1\" rowsep=\"1\">\n";
2879 for (col_type c = 0; c < ncols(); ++c) {
2880 os << "<colspec colname=\"col" << c << "\" align=\"";
2881 switch (column_info[c].alignment) {
2882 case LYX_ALIGN_LEFT:
2885 case LYX_ALIGN_RIGHT:
2893 if (runparams.flavor == OutputParams::XML)
2899 //+---------------------------------------------------------------------
2900 //+ Long Tabular case +
2901 //+---------------------------------------------------------------------
2903 // output caption info
2904 // The caption flag wins over head/foot
2905 if (haveLTCaption()) {
2906 os << "<caption>\n";
2908 for (row_type r = 0; r < nrows(); ++r) {
2909 if (row_info[r].caption) {
2910 ret += docbookRow(os, r, runparams);
2913 os << "</caption>\n";
2916 // output header info
2917 if (haveLTHead(false) || haveLTFirstHead(false)) {
2920 for (row_type r = 0; r < nrows(); ++r) {
2921 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2922 !row_info[r].caption) {
2923 ret += docbookRow(os, r, runparams);
2929 // output footer info
2930 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2933 for (row_type r = 0; r < nrows(); ++r) {
2934 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2935 !row_info[r].caption) {
2936 ret += docbookRow(os, r, runparams);
2943 //+---------------------------------------------------------------------
2944 //+ the single row and columns (cells) +
2945 //+---------------------------------------------------------------------
2949 for (row_type r = 0; r < nrows(); ++r) {
2950 if (isValidRow(r)) {
2951 ret += docbookRow(os, r, runparams);
2956 //+---------------------------------------------------------------------
2957 //+ the closing of the tabular +
2958 //+---------------------------------------------------------------------
2967 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2968 OutputParams const & runparams, bool header) const
2971 string const celltag = header ? "th" : "td";
2972 idx_type cell = getFirstCellInRow(row);
2974 xs << html::StartTag("tr");
2975 for (col_type c = 0; c < ncols(); ++c) {
2976 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2981 switch (getAlignment(cell)) {
2982 case LYX_ALIGN_LEFT:
2985 case LYX_ALIGN_RIGHT:
2993 attr << " valign='";
2994 switch (getVAlignment(cell)) {
2995 case LYX_VALIGN_TOP:
2998 case LYX_VALIGN_BOTTOM:
3001 case LYX_VALIGN_MIDDLE:
3006 if (isMultiColumn(cell))
3007 attr << " colspan='" << columnSpan(cell) << "'";
3008 else if (isMultiRow(cell))
3009 attr << " rowspan='" << rowSpan(cell) << "'";
3011 xs << html::StartTag(celltag, attr.str()) << html::CR();
3012 ret += cellInset(cell)->xhtml(xs, runparams);
3013 xs << html::EndTag(celltag) << html::CR();
3016 xs << html::EndTag("tr");
3021 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3025 if (is_long_tabular) {
3026 // we'll wrap it in a div, so as to deal with alignment
3028 switch (longtabular_alignment) {
3029 case LYX_LONGTABULAR_ALIGN_LEFT:
3032 case LYX_LONGTABULAR_ALIGN_CENTER:
3035 case LYX_LONGTABULAR_ALIGN_RIGHT:
3039 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3041 // The caption flag wins over head/foot
3042 if (haveLTCaption()) {
3043 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3045 for (row_type r = 0; r < nrows(); ++r)
3046 if (row_info[r].caption)
3047 ret += xhtmlRow(xs, r, runparams);
3048 xs << html::EndTag("div") << html::CR();
3052 xs << html::StartTag("table") << html::CR();
3054 // output header info
3055 bool const havefirsthead = haveLTFirstHead(false);
3056 // if we have a first head, then we are going to ignore the
3057 // headers for the additional pages, since there aren't any
3058 // in XHTML. this test accomplishes that.
3059 bool const havehead = !havefirsthead && haveLTHead(false);
3060 if (havehead || havefirsthead) {
3061 xs << html::StartTag("thead") << html::CR();
3062 for (row_type r = 0; r < nrows(); ++r) {
3063 if (((havefirsthead && row_info[r].endfirsthead) ||
3064 (havehead && row_info[r].endhead)) &&
3065 !row_info[r].caption) {
3066 ret += xhtmlRow(xs, r, runparams, true);
3069 xs << html::EndTag("thead") << html::CR();
3071 // output footer info
3072 bool const havelastfoot = haveLTLastFoot(false);
3074 bool const havefoot = !havelastfoot && haveLTFoot(false);
3075 if (havefoot || havelastfoot) {
3076 xs << html::StartTag("tfoot") << html::CR();
3077 for (row_type r = 0; r < nrows(); ++r) {
3078 if (((havelastfoot && row_info[r].endlastfoot) ||
3079 (havefoot && row_info[r].endfoot)) &&
3080 !row_info[r].caption) {
3081 ret += xhtmlRow(xs, r, runparams);
3084 xs << html::EndTag("tfoot") << html::CR();
3087 xs << html::StartTag("tbody") << html::CR();
3088 for (row_type r = 0; r < nrows(); ++r) {
3089 if (isValidRow(r)) {
3090 ret += xhtmlRow(xs, r, runparams);
3093 xs << html::EndTag("tbody")
3095 << html::EndTag("table")
3097 if (is_long_tabular)
3098 xs << html::EndTag("div") << html::CR();
3103 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3104 vector<unsigned int> const & clen) const
3106 idx_type const fcell = getFirstCellInRow(row);
3107 idx_type const n = numberOfCellsInRow(row) + fcell;
3110 for (idx_type i = fcell; i < n; ++i) {
3120 for (idx_type i = fcell; i < n; ++i) {
3131 col_type column = cellColumn(i);
3132 int len = clen[column];
3133 while (column < ncols() - 1
3134 && isPartOfMultiColumn(row, ++column))
3135 len += clen[column] + 4;
3136 os << docstring(len, ch);
3151 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3152 vector<unsigned int> const & clen) const
3154 idx_type const fcell = getFirstCellInRow(row);
3155 idx_type const n = numberOfCellsInRow(row) + fcell;
3158 for (idx_type i = fcell; i < n; ++i) {
3159 if (bottomLine(i)) {
3168 for (idx_type i = fcell; i < n; ++i) {
3169 if (bottomLine(i)) {
3179 col_type column = cellColumn(i);
3180 int len = clen[column];
3181 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3182 len += clen[column] + 4;
3183 os << docstring(len, ch);
3184 if (bottomLine(i)) {
3198 void Tabular::plaintextPrintCell(odocstringstream & os,
3199 OutputParams const & runparams,
3200 idx_type cell, row_type row, col_type column,
3201 vector<unsigned int> const & clen,
3202 bool onlydata, size_t max_length) const
3204 odocstringstream sstr;
3205 cellInset(cell)->plaintext(sstr, runparams, max_length);
3217 unsigned int len1 = sstr.str().length();
3218 unsigned int len2 = clen[column];
3219 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3220 len2 += clen[column] + 4;
3223 switch (getAlignment(cell)) {
3225 case LYX_ALIGN_LEFT:
3228 case LYX_ALIGN_RIGHT:
3232 case LYX_ALIGN_CENTER:
3238 os << docstring(len1, ' ') << sstr.str()
3239 << docstring(len2, ' ');
3241 if (rightLine(cell))
3248 void Tabular::plaintext(odocstringstream & os,
3249 OutputParams const & runparams, int const depth,
3250 bool onlydata, char_type delim, size_t max_length) const
3252 // first calculate the width of the single columns
3253 vector<unsigned int> clen(ncols());
3256 // first all non multicolumn cells!
3257 for (col_type c = 0; c < ncols(); ++c) {
3259 for (row_type r = 0; r < nrows(); ++r) {
3260 idx_type cell = cellIndex(r, c);
3261 if (isMultiColumn(cell))
3263 odocstringstream sstr;
3264 cellInset(cell)->plaintext(sstr, runparams, max_length);
3265 if (clen[c] < sstr.str().length())
3266 clen[c] = sstr.str().length();
3269 // then all multicolumn cells!
3270 for (col_type c = 0; c < ncols(); ++c) {
3271 for (row_type r = 0; r < nrows(); ++r) {
3272 idx_type cell = cellIndex(r, c);
3273 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3275 odocstringstream sstr;
3276 cellInset(cell)->plaintext(sstr, runparams, max_length);
3277 int len = int(sstr.str().length());
3278 idx_type const n = columnSpan(cell);
3279 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3281 if (len > int(clen[c + n - 1]))
3282 clen[c + n - 1] = len;
3287 for (row_type r = 0; r < nrows(); ++r) {
3288 if (!onlydata && plaintextTopHLine(os, r, clen))
3289 os << docstring(depth * 2, ' ');
3290 for (col_type c = 0; c < ncols(); ++c) {
3291 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3293 if (onlydata && c > 0)
3294 // we don't use operator<< for single UCS4 character.
3295 // see explanation in docstream.h
3297 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3299 if (os.str().size() > max_length)
3304 os << docstring(depth * 2, ' ');
3305 if (plaintextBottomHLine(os, r, clen))
3306 os << docstring(depth * 2, ' ');
3308 if (os.str().size() > max_length)
3314 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3316 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3320 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3321 col_type column) const
3323 return cell_info[row][column].inset;
3327 void Tabular::setCellInset(row_type row, col_type column,
3328 shared_ptr<InsetTableCell> ins) const
3330 CellData & cd = cell_info[row][column];
3335 void Tabular::validate(LaTeXFeatures & features) const
3337 features.require("NeedTabularnewline");
3339 features.require("booktabs");
3340 if (is_long_tabular)
3341 features.require("longtable");
3343 features.require("rotating");
3344 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3345 if (isMultiRow(cell))
3346 features.require("multirow");
3347 if (getVAlignment(cell) != LYX_VALIGN_TOP
3348 || !getPWidth(cell).zero())
3349 features.require("array");
3350 cellInset(cell)->validate(features);
3355 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3357 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3358 ParagraphList::const_iterator cit = parlist.begin();
3359 ParagraphList::const_iterator end = parlist.end();
3361 for (; cit != end; ++cit)
3362 for (int i = 0; i < cit->size(); ++i)
3363 if (cit->isNewline(i))
3370 /////////////////////////////////////////////////////////////////////
3374 /////////////////////////////////////////////////////////////////////
3376 InsetTableCell::InsetTableCell(Buffer * buf)
3377 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3378 contentAlign(LYX_ALIGN_CENTER)
3382 bool InsetTableCell::forcePlainLayout(idx_type) const
3384 return !isFixedWidth;
3388 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3390 return isFixedWidth;
3394 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3395 FuncStatus & status) const
3397 bool enabled = true;
3398 switch (cmd.action()) {
3400 enabled = !forcePlainLayout();
3402 case LFUN_LAYOUT_PARAGRAPH:
3403 enabled = allowParagraphCustomization();
3406 case LFUN_MATH_DISPLAY:
3407 if (!hasFixedWidth()) {
3412 return InsetText::getStatus(cur, cmd, status);
3414 status.setEnabled(enabled);
3418 docstring InsetTableCell::asString(bool intoInsets)
3421 if (paragraphs().empty())
3423 ParagraphList::const_iterator it = paragraphs().begin();
3424 ParagraphList::const_iterator en = paragraphs().end();
3426 for (; it != en; ++it) {
3431 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3437 void InsetTableCell::addToToc(DocIterator const & di, bool output_active) const
3439 InsetText::iterateForToc(di, output_active);
3443 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3446 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3447 return InsetText::xhtml(xs, rp);
3452 /////////////////////////////////////////////////////////////////////
3456 /////////////////////////////////////////////////////////////////////
3458 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3460 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3461 rowselect_(false), colselect_(false)
3466 InsetTabular::InsetTabular(InsetTabular const & tab)
3467 : Inset(tab), tabular(tab.tabular), scx_(0)
3472 InsetTabular::~InsetTabular()
3474 hideDialogs("tabular", this);
3478 void InsetTabular::setBuffer(Buffer & buf)
3480 tabular.setBuffer(buf);
3481 Inset::setBuffer(buf);
3485 bool InsetTabular::insetAllowed(InsetCode code) const
3490 case MATHMACRO_CODE:
3495 return tabular.is_long_tabular;
3503 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3505 return tabular.is_long_tabular &&
3506 (newtype == "Standard" || newtype == "LongTableNoNumber");
3510 void InsetTabular::write(ostream & os) const
3512 os << "Tabular" << endl;
3517 string InsetTabular::contextMenu(BufferView const &, int, int) const
3519 // FIXME: depending on the selection state,
3520 // we could offer a different menu.
3521 return cell(0)->contextMenuName() + ";" + contextMenuName();
3525 string InsetTabular::contextMenuName() const
3527 return "context-tabular";
3531 void InsetTabular::read(Lexer & lex)
3533 //bool const old_format = (lex.getString() == "\\LyXTable");
3541 string token = lex.getString();
3542 while (lex && token != "\\end_inset") {
3544 token = lex.getString();
3547 lex.printError("Missing \\end_inset at this point. ");
3551 int InsetTabular::rowFromY(Cursor & cur, int y) const
3553 // top y coordinate of tabular
3554 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3556 for (; r < tabular.nrows() && y > h; ++r)
3557 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3558 + tabular.interRowSpace(r);
3564 int InsetTabular::columnFromX(Cursor & cur, int x) const
3566 // left x coordinate of tabular
3567 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3569 for (; c < tabular.ncols() && x > w; ++c)
3570 w += tabular.cellWidth(c);
3575 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3577 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3578 // mi.base.textwidth << "\n";
3579 LBUFERR(mi.base.bv);
3581 for (row_type r = 0; r < tabular.nrows(); ++r) {
3584 for (col_type c = 0; c < tabular.ncols(); ++c) {
3585 if (tabular.isPartOfMultiColumn(r, c)
3586 || tabular.isPartOfMultiRow(r, c))
3587 // multicolumn or multirow cell, but not first one
3589 idx_type const cell = tabular.cellIndex(r, c);
3592 Length const p_width = tabular.getPWidth(cell);
3593 if (!p_width.zero())
3594 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3595 tabular.cellInset(cell)->metrics(m, dim);
3596 if (!p_width.zero())
3597 dim.wid = m.base.textwidth;
3598 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3599 + tabular.interColumnSpace(cell);
3601 // FIXME(?): do we need a second metrics call?
3602 TextMetrics const & tm =
3603 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3605 // determine horizontal offset because of decimal align (if necessary)
3606 int decimal_width = 0;
3607 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3608 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3609 tail.setBuffer(tabular.buffer());
3610 // we need to set macrocontext position everywhere
3611 // otherwise we crash with nested insets (e.g. footnotes)
3612 // after decimal point
3613 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3615 dit.push_back(CursorSlice(tail));
3616 tail.setMacrocontextPositionRecursive(dit);
3618 // remove text leading decimal point
3619 docstring const align_d = tabular.column_info[c].decimal_point;
3620 dit = separatorPos(&tail, align_d);
3622 pit_type const psize = tail.paragraphs().front().size();
3624 tail.paragraphs().front().eraseChars(0,
3625 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3627 tail.metrics(m, dim1);
3628 decimal_width = dim1.width();
3631 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3632 tabular.cell_info[r][c].decimal_width = decimal_width;
3634 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3635 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3636 int const lastpardes = tm.last().second->descent()
3637 + TEXT_TO_INSET_OFFSET;
3639 switch (tabular.getVAlignment(cell)) {
3640 case Tabular::LYX_VALIGN_TOP:
3642 case Tabular::LYX_VALIGN_MIDDLE:
3643 offset = -(dim.des - lastpardes)/2;
3645 case Tabular::LYX_VALIGN_BOTTOM:
3646 offset = -(dim.des - lastpardes);
3649 tabular.cell_info[r][c].voffset = offset;
3650 maxasc = max(maxasc, dim.asc - offset);
3651 maxdes = max(maxdes, dim.des + offset);
3653 int const top_space = tabular.row_info[r].top_space_default ?
3654 default_line_space :
3655 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3656 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3657 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3658 default_line_space :
3659 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3660 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3663 // for top-alignment the first horizontal table line must be exactly at
3664 // the position of the base line of the surrounding text line
3665 // for bottom alignment, the same is for the last table line
3666 switch (tabular.tabular_valignment) {
3667 case Tabular::LYX_VALIGN_BOTTOM:
3668 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3670 case Tabular::LYX_VALIGN_MIDDLE:
3671 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3673 case Tabular::LYX_VALIGN_TOP:
3674 offset_valign_ = tabular.rowAscent(0);
3678 tabular.updateColumnWidths();
3679 dim.asc = tabular.rowAscent(0) - offset_valign_;
3680 dim.des = tabular.height() - dim.asc;
3681 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3685 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3688 if (&cur.inset() == this && cur.selection()) {
3689 if (cur.selIsMultiCell()) {
3692 getSelection(cur, rs, re, cs, ce);
3694 idx_type const cell = tabular.cellIndex(row, col);
3695 col_type const cspan = tabular.columnSpan(cell);
3696 row_type const rspan = tabular.rowSpan(cell);
3697 if (col + cspan - 1 >= cs && col <= ce
3698 && row + rspan - 1 >= rs && row <= re)
3701 if (col == tabular.cellColumn(cur.idx())
3702 && row == tabular.cellRow(cur.idx())) {
3703 CursorSlice const & beg = cur.selBegin();
3704 CursorSlice const & end = cur.selEnd();
3706 if ((end.lastpos() > 0 || end.lastpit() > 0)
3707 && end.pos() == end.lastpos() && beg.pos() == 0
3708 && end.pit() == end.lastpit() && beg.pit() == 0)
3716 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3718 x += scx_ + ADD_TO_TABULAR_WIDTH;
3720 BufferView * bv = pi.base.bv;
3721 Cursor & cur = pi.base.bv->cursor();
3724 // FIXME: As the full background is painted in drawBackground(),
3725 // we have no choice but to do a full repaint for the Text cells.
3726 pi.full_repaint = true;
3728 bool const original_selection_state = pi.selected;
3731 first_visible_cell = Tabular::npos;
3733 int yy = y + offset_valign_;
3734 for (row_type r = 0; r < tabular.nrows(); ++r) {
3736 for (col_type c = 0; c < tabular.ncols(); ++c) {
3737 if (tabular.isPartOfMultiColumn(r, c))
3740 idx = tabular.cellIndex(r, c);
3742 if (tabular.isPartOfMultiRow(r, c)) {
3743 nx += tabular.cellWidth(idx);
3747 if (first_visible_cell == Tabular::npos)
3748 first_visible_cell = idx;
3750 pi.selected |= isCellSelected(cur, r, c);
3751 int const cx = nx + tabular.textHOffset(idx);
3752 int const cy = yy + tabular.textVOffset(idx);
3753 // Cache the Inset position.
3754 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3755 cell(idx)->draw(pi, cx, cy);
3756 drawCellLines(pi, nx, yy, r, idx);
3757 nx += tabular.cellWidth(idx);
3758 pi.selected = original_selection_state;
3761 if (r + 1 < tabular.nrows())
3762 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3763 + tabular.interRowSpace(r + 1);
3768 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3770 x += scx_ + ADD_TO_TABULAR_WIDTH;
3771 y += offset_valign_ - tabular.rowAscent(0);
3772 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3773 pi.backgroundColor(this));
3777 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3779 Cursor & cur = pi.base.bv->cursor();
3782 x += scx_ + ADD_TO_TABULAR_WIDTH;
3784 if (!cur.selection())
3786 if (&cur.inset() != this)
3791 bool const full_cell_selected = isCellSelected(cur,
3792 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3794 if (cur.selIsMultiCell() || full_cell_selected) {
3795 for (row_type r = 0; r < tabular.nrows(); ++r) {
3797 for (col_type c = 0; c < tabular.ncols(); ++c) {
3798 if (tabular.isPartOfMultiColumn(r, c))
3801 idx_type const cell = tabular.cellIndex(r, c);
3803 if (tabular.isPartOfMultiRow(r, c)) {
3804 xx += tabular.cellWidth(cell);
3807 int const w = tabular.cellWidth(cell);
3808 int const h = tabular.cellHeight(cell);
3809 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3810 if (isCellSelected(cur, r, c))
3811 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3814 if (r + 1 < tabular.nrows())
3815 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3816 + tabular.interRowSpace(r + 1);
3820 // FIXME: This code has no effect because InsetTableCell does not handle
3821 // drawSelection other than the trivial implementation in Inset.
3823 // x += cellXPos(cur.idx());
3824 // x += tabular.textHOffset(cur.idx());
3825 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3830 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3831 row_type row, idx_type cell) const
3833 y -= tabular.rowAscent(row);
3834 int const w = tabular.cellWidth(cell);
3835 int const h = tabular.cellHeight(cell);
3836 Color const linecolor = pi.textColor(Color_tabularline);
3837 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3840 bool drawline = tabular.topLine(cell)
3841 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3842 pi.pain.line(x, y, x + w, y,
3843 drawline ? linecolor : gridcolor,
3844 drawline ? Painter::line_solid : Painter::line_onoffdash);
3847 drawline = tabular.bottomLine(cell);
3848 pi.pain.line(x, y + h, x + w, y + h,
3849 drawline ? linecolor : gridcolor,
3850 drawline ? Painter::line_solid : Painter::line_onoffdash);
3853 col_type const col = tabular.cellColumn(cell);
3854 drawline = tabular.leftLine(cell)
3855 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3856 pi.pain.line(x, y, x, y + h,
3857 drawline ? linecolor : gridcolor,
3858 drawline ? Painter::line_solid : Painter::line_onoffdash);
3861 x -= tabular.interColumnSpace(cell);
3862 col_type next_cell_col = col + 1;
3863 while (next_cell_col < tabular.ncols()
3864 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3866 drawline = tabular.rightLine(cell)
3867 || (next_cell_col < tabular.ncols()
3868 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3869 pi.pain.line(x + w, y, x + w, y + h,
3870 drawline ? linecolor : gridcolor,
3871 drawline ? Painter::line_solid : Painter::line_onoffdash);
3875 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3877 //lyxerr << "InsetTabular::edit: " << this << endl;
3879 cur.setSelection(false);
3882 if (isRightToLeft(cur))
3883 cur.idx() = tabular.getLastCellInRow(0);
3889 if (isRightToLeft(cur))
3890 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3892 cur.idx() = tabular.numberofcells - 1;
3894 cur.pos() = cur.lastpos(); // FIXME crude guess
3896 cur.setCurrentFont();
3897 // FIXME: this accesses the position cache before it is initialized
3899 //cur.bv().fitCursor();
3903 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3905 // In a longtable, tell captions what the current float is
3906 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3907 string const saveflt = cnts.current_float();
3908 if (tabular.is_long_tabular) {
3909 cnts.current_float("table");
3910 // in longtables, we only step the counter once
3911 cnts.step(from_ascii("table"), utype);
3912 cnts.isLongtable(true);
3915 ParIterator it2 = it;
3917 size_t const end = it2.nargs();
3918 for ( ; it2.idx() < end; it2.top().forwardIdx())
3919 buffer().updateBuffer(it2, utype);
3922 if (tabular.is_long_tabular) {
3923 cnts.current_float(saveflt);
3924 cnts.isLongtable(false);
3929 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active) const
3931 DocIterator dit = cpit;
3933 size_t const end = dit.nargs();
3934 for ( ; dit.idx() < end; dit.top().forwardIdx())
3935 cell(dit.idx())->addToToc(dit, output_active);
3939 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3941 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3942 << "\n cur:" << cur);
3943 CursorSlice sl = cur.top();
3944 Cursor & bvcur = cur.bv().cursor();
3946 FuncCode const act = cmd.action();
3950 case LFUN_MOUSE_PRESS: {
3951 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3953 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3954 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3955 row_type r = rowFromY(cur, cmd.y());
3956 cur.idx() = tabular.getFirstCellInRow(r);
3959 cur.idx() = tabular.getLastCellInRow(r);
3960 cur.pos() = cur.lastpos();
3961 cur.setSelection(true);
3967 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3968 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3969 || cmd.y() > y0 + tabular.height()) {
3970 col_type c = columnFromX(cur, cmd.x());
3971 cur.idx() = tabular.cellIndex(0, c);
3974 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3975 cur.pos() = cur.lastpos();
3976 cur.setSelection(true);
3981 // do not reset cursor/selection if we have selected
3982 // some cells (bug 2715).
3983 if (cmd.button() == mouse_button::button3
3984 && &bvcur.selBegin().inset() == this
3985 && bvcur.selIsMultiCell())
3988 // Let InsetTableCell do it
3989 cell(cur.idx())->dispatch(cur, cmd);
3992 case LFUN_MOUSE_MOTION:
3993 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3994 if (cmd.button() == mouse_button::button1) {
3995 // only accept motions to places not deeper nested than the real anchor
3996 if (!bvcur.realAnchor().hasPart(cur)) {
4000 // select (additional) row
4002 row_type r = rowFromY(cur, cmd.y());
4003 cur.idx() = tabular.getLastCellInRow(r);
4004 // we need to reset the cursor's pit and pos now, as the old ones
4005 // may no longer be valid.
4008 bvcur.setCursor(cur);
4009 bvcur.setSelection(true);
4012 // select (additional) column
4014 col_type c = columnFromX(cur, cmd.x());
4015 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4016 // we need to reset the cursor's pit and pos now, as the old ones
4017 // may no longer be valid.
4020 bvcur.setCursor(cur);
4021 bvcur.setSelection(true);
4024 // only update if selection changes
4025 if (bvcur.idx() == cur.idx() &&
4026 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4027 cur.noScreenUpdate();
4028 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4029 bvcur.setCursor(cur);
4030 bvcur.setSelection(true);
4031 // if this is a multicell selection, we just set the cursor to
4032 // the beginning of the cell's text.
4033 if (bvcur.selIsMultiCell()) {
4034 bvcur.pit() = bvcur.lastpit();
4035 bvcur.pos() = bvcur.lastpos();
4040 case LFUN_MOUSE_RELEASE:
4045 case LFUN_CELL_BACKWARD:
4047 cur.setSelection(false);
4050 case LFUN_CELL_FORWARD:
4052 cur.setSelection(false);
4055 case LFUN_CHAR_FORWARD_SELECT:
4056 case LFUN_CHAR_FORWARD:
4057 case LFUN_CHAR_BACKWARD_SELECT:
4058 case LFUN_CHAR_BACKWARD:
4059 case LFUN_CHAR_RIGHT_SELECT:
4060 case LFUN_CHAR_RIGHT:
4061 case LFUN_CHAR_LEFT_SELECT:
4062 case LFUN_CHAR_LEFT:
4063 case LFUN_WORD_FORWARD:
4064 case LFUN_WORD_FORWARD_SELECT:
4065 case LFUN_WORD_BACKWARD:
4066 case LFUN_WORD_BACKWARD_SELECT:
4067 case LFUN_WORD_RIGHT:
4068 case LFUN_WORD_RIGHT_SELECT:
4069 case LFUN_WORD_LEFT:
4070 case LFUN_WORD_LEFT_SELECT: {
4071 // determine whether we move to next or previous cell, where to enter
4072 // the new cell from, and which command to "finish" (i.e., exit the
4075 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4076 FuncCode finish_lfun;
4078 if (act == LFUN_CHAR_FORWARD
4079 || act == LFUN_CHAR_FORWARD_SELECT
4080 || act == LFUN_WORD_FORWARD
4081 || act == LFUN_WORD_FORWARD_SELECT) {
4083 finish_lfun = LFUN_FINISHED_FORWARD;
4085 else if (act == LFUN_CHAR_BACKWARD
4086 || act == LFUN_CHAR_BACKWARD_SELECT
4087 || act == LFUN_WORD_BACKWARD
4088 || act == LFUN_WORD_BACKWARD_SELECT) {
4090 finish_lfun = LFUN_FINISHED_BACKWARD;
4092 // LEFT or RIGHT commands --- the interpretation will depend on the
4093 // table's direction.
4095 bool const right = act == LFUN_CHAR_RIGHT
4096 || act == LFUN_CHAR_RIGHT_SELECT
4097 || act == LFUN_WORD_RIGHT
4098 || act == LFUN_WORD_RIGHT_SELECT;
4099 next_cell = isRightToLeft(cur) != right;
4101 if (lyxrc.visual_cursor)
4102 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4104 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4107 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4108 || act == LFUN_CHAR_BACKWARD_SELECT
4109 || act == LFUN_CHAR_RIGHT_SELECT
4110 || act == LFUN_CHAR_LEFT_SELECT
4111 || act == LFUN_WORD_FORWARD_SELECT
4112 || act == LFUN_WORD_RIGHT_SELECT
4113 || act == LFUN_WORD_BACKWARD_SELECT
4114 || act == LFUN_WORD_LEFT_SELECT;
4116 // If we have a multicell selection or we're
4117 // not doing some LFUN_*_SELECT thing anyway...
4118 if (!cur.selIsMultiCell() || !select) {
4119 col_type const c = tabular.cellColumn(cur.idx());
4120 row_type const r = tabular.cellRow(cur.idx());
4121 // Are we trying to select the whole cell and is the whole cell
4122 // not yet selected?
4123 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4124 ((next_cell && cur.pit() == cur.lastpit()
4125 && cur.pos() == cur.lastpos())
4126 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4128 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4130 // ...try to dispatch to the cell's inset.
4131 cell(cur.idx())->dispatch(cur, cmd);
4133 // When we already have a selection we want to select the whole cell
4134 // before going to the next cell.
4135 if (select_whole && !empty_cell){
4136 getText(cur.idx())->selectAll(cur);
4138 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4142 // FIXME: When we support the selection of an empty cell, remove
4143 // the !empty_cell from this condition. For now we jump to the next
4144 // cell if the current cell is empty.
4145 if (cur.result().dispatched() && !empty_cell)
4149 // move to next/prev cell, as appropriate
4150 // note that we will always do this if we're selecting and we have
4151 // a multicell selection
4152 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4153 << " cell from: " << int(entry_from));
4155 moveNextCell(cur, entry_from);
4157 movePrevCell(cur, entry_from);
4158 // if we're exiting the table, call the appropriate FINISHED lfun
4159 if (sl == cur.top())
4160 cmd = FuncRequest(finish_lfun);
4164 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4169 case LFUN_DOWN_SELECT:
4171 if (!(cur.selection() && cur.selIsMultiCell()))
4172 cell(cur.idx())->dispatch(cur, cmd);
4174 cur.dispatched(); // override the cell's decision
4175 if (sl == cur.top()) {
4176 // if our Text didn't do anything to the cursor
4177 // then we try to put the cursor into the cell below
4178 // setting also the right targetX.
4179 cur.selHandle(act == LFUN_DOWN_SELECT);
4180 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4181 int const xtarget = cur.targetX();
4182 // WARNING: Once cur.idx() has been reset, the cursor is in
4183 // an inconsistent state until pos() has been set. Be careful
4184 // what you do with it!
4185 cur.idx() = tabular.cellBelow(cur.idx());
4187 TextMetrics const & tm =
4188 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4189 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4190 cur.setCurrentFont();
4193 if (sl == cur.top()) {
4194 // we trick it to go to forward after leaving the
4196 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4199 if (cur.selIsMultiCell()) {
4200 cur.pit() = cur.lastpit();
4201 cur.pos() = cur.lastpos();
4202 cur.setCurrentFont();
4203 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4206 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4209 case LFUN_UP_SELECT:
4211 if (!(cur.selection() && cur.selIsMultiCell()))
4212 cell(cur.idx())->dispatch(cur, cmd);
4213 cur.dispatched(); // override the cell's decision
4214 if (sl == cur.top()) {
4215 // if our Text didn't do anything to the cursor
4216 // then we try to put the cursor into the cell above
4217 // setting also the right targetX.
4218 cur.selHandle(act == LFUN_UP_SELECT);
4219 if (tabular.cellRow(cur.idx()) != 0) {
4220 int const xtarget = cur.targetX();
4221 // WARNING: Once cur.idx() has been reset, the cursor is in
4222 // an inconsistent state until pos() has been set. Be careful
4223 // what you do with it!
4224 cur.idx() = tabular.cellAbove(cur.idx());
4225 cur.pit() = cur.lastpit();
4226 Text const * text = cell(cur.idx())->getText(0);
4227 TextMetrics const & tm = cur.bv().textMetrics(text);
4228 ParagraphMetrics const & pm =
4229 tm.parMetrics(cur.lastpit());
4230 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4231 cur.setCurrentFont();
4234 if (sl == cur.top()) {
4235 cmd = FuncRequest(LFUN_UP);
4238 if (cur.selIsMultiCell()) {
4240 cur.pos() = cur.lastpos();
4241 cur.setCurrentFont();
4242 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4245 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4248 // case LFUN_SCREEN_DOWN: {
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();
4253 // if (t < yo() + tabular.getHeightOfTabular()) {
4254 // cur.bv().scrollDocView(t, true);
4255 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4257 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4264 // case LFUN_SCREEN_UP: {
4265 // //if (hasSelection())
4266 // // cur.selection() = false;
4267 // col_type const col = tabular.cellColumn(cur.idx());
4268 // int const t = cur.bv().top_y() + cur.bv().height();
4270 // cur.bv().scrollDocView(t, true);
4274 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4278 // cur.par() = cur.lastpar();
4279 // cur.pos() = cur.lastpos();
4283 case LFUN_LAYOUT_TABULAR:
4284 cur.bv().showDialog("tabular");
4287 case LFUN_INSET_MODIFY: {
4289 if (cmd.getArg(1) == "from-dialog")
4290 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4292 arg = to_utf8(cmd.argument());
4293 if (!tabularFeatures(cur, arg))
4298 // insert file functions
4299 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4300 case LFUN_FILE_INSERT_PLAINTEXT:
4302 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4303 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4304 FileName(to_utf8(cmd.argument())));
4307 cur.recordUndoInset(INSERT_UNDO);
4308 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4309 // content has been replaced,
4310 // so cursor might be invalid
4311 cur.pos() = cur.lastpos();
4312 cur.pit() = cur.lastpit();
4313 bvcur.setCursor(cur);
4320 if (cur.selIsMultiCell()) {
4321 if (copySelection(cur)) {
4322 cur.recordUndoInset(DELETE_UNDO);
4326 cell(cur.idx())->dispatch(cur, cmd);
4329 case LFUN_SELF_INSERT:
4330 if (cur.selIsMultiCell()) {
4331 cur.recordUndoInset(DELETE_UNDO);
4333 BufferView * bv = &cur.bv();
4334 docstring::const_iterator cit = cmd.argument().begin();
4335 docstring::const_iterator const end = cmd.argument().end();
4336 for (; cit != end; ++cit)
4337 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4340 bv->bookmarkEditPosition();
4342 cell(cur.idx())->dispatch(cur, cmd);
4345 case LFUN_CHAR_DELETE_BACKWARD:
4346 case LFUN_CHAR_DELETE_FORWARD:
4347 if (cur.selIsMultiCell()) {
4348 cur.recordUndoInset(DELETE_UNDO);
4351 cell(cur.idx())->dispatch(cur, cmd);
4355 if (!cur.selection())
4357 if (cur.selIsMultiCell()) {
4361 cell(cur.idx())->dispatch(cur, cmd);
4364 case LFUN_CLIPBOARD_PASTE:
4365 case LFUN_PRIMARY_SELECTION_PASTE: {
4366 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4367 theClipboard().getAsText(Clipboard::PlainTextType) :
4368 theSelection().get();
4371 // pass to InsertPlaintextString, but
4372 // only if we have multi-cell content
4373 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4374 cur.recordUndoInset(INSERT_UNDO);
4375 if (insertPlaintextString(cur.bv(), clip, false)) {
4376 // content has been replaced,
4377 // so cursor might be invalid
4378 cur.pos() = cur.lastpos();
4379 cur.pit() = cur.lastpit();
4380 bvcur.setCursor(cur);
4384 // Let the cell handle normal text
4385 cell(cur.idx())->dispatch(cur, cmd);
4390 if (!tabularStackDirty()) {
4391 if (!cur.selIsMultiCell())
4392 cell(cur.idx())->dispatch(cur, cmd);
4395 if (theClipboard().isInternal()) {
4396 cur.recordUndoInset(INSERT_UNDO);
4397 pasteClipboard(cur);
4401 case LFUN_FONT_EMPH:
4402 case LFUN_FONT_BOLD:
4403 case LFUN_FONT_BOLDSYMBOL:
4404 case LFUN_FONT_ROMAN:
4405 case LFUN_FONT_NOUN:
4406 case LFUN_FONT_ITAL:
4407 case LFUN_FONT_FRAK:
4408 case LFUN_FONT_TYPEWRITER:
4409 case LFUN_FONT_SANS:
4410 case LFUN_TEXTSTYLE_APPLY:
4411 case LFUN_TEXTSTYLE_UPDATE:
4412 case LFUN_FONT_SIZE:
4413 case LFUN_FONT_UNDERLINE:
4414 case LFUN_FONT_STRIKEOUT:
4415 case LFUN_FONT_UNDERUNDERLINE:
4416 case LFUN_FONT_UNDERWAVE:
4418 case LFUN_PARAGRAPH_PARAMS_APPLY:
4419 case LFUN_PARAGRAPH_PARAMS:
4420 case LFUN_WORD_CAPITALIZE:
4421 case LFUN_WORD_UPCASE:
4422 case LFUN_WORD_LOWCASE:
4423 case LFUN_CHARS_TRANSPOSE:
4424 if (cur.selIsMultiCell()) {
4427 getSelection(cur, rs, re, cs, ce);
4428 Cursor tmpcur = cur;
4429 for (row_type r = rs; r <= re; ++r) {
4430 for (col_type c = cs; c <= ce; ++c) {
4431 // cursor follows cell:
4432 tmpcur.idx() = tabular.cellIndex(r, c);
4433 // select this cell only:
4436 tmpcur.resetAnchor();
4437 tmpcur.pit() = tmpcur.lastpit();
4438 tmpcur.pos() = tmpcur.top().lastpos();
4439 tmpcur.setCursor(tmpcur);
4440 tmpcur.setSelection();
4441 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4446 cell(cur.idx())->dispatch(cur, cmd);
4450 case LFUN_INSET_SETTINGS:
4451 // relay this lfun to Inset, not to the cell.
4452 Inset::doDispatch(cur, cmd);
4456 // we try to handle this event in the insets dispatch function.
4457 cell(cur.idx())->dispatch(cur, cmd);
4463 // function sets an object as defined in func_status.h:
4464 // states OK, Unknown, Disabled, On, Off.
4465 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4466 FuncStatus & status) const
4468 switch (cmd.action()) {
4469 case LFUN_INSET_MODIFY: {
4470 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4473 // FIXME: We only check for the very first argument...
4474 string const s = cmd.getArg(1);
4475 // We always enable the lfun if it is coming from the dialog
4476 // because the dialog makes sure all the settings are valid,
4477 // even though the first argument might not be valid now.
4478 if (s == "from-dialog") {
4479 status.setEnabled(true);
4483 int action = Tabular::LAST_ACTION;
4485 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4486 if (tabularFeature[i].feature == s) {
4487 action = tabularFeature[i].action;
4491 if (action == Tabular::LAST_ACTION) {
4493 status.setUnknown(true);
4497 string const argument = cmd.getLongArg(2);
4499 row_type sel_row_start = 0;
4500 row_type sel_row_end = 0;
4501 col_type sel_col_start = 0;
4502 col_type sel_col_end = 0;
4503 Tabular::ltType dummyltt;
4506 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4509 case Tabular::SET_PWIDTH:
4510 case Tabular::SET_MPWIDTH:
4511 case Tabular::SET_SPECIAL_COLUMN:
4512 case Tabular::SET_SPECIAL_MULTICOLUMN:
4513 case Tabular::APPEND_ROW:
4514 case Tabular::APPEND_COLUMN:
4515 case Tabular::DELETE_ROW:
4516 case Tabular::DELETE_COLUMN:
4517 case Tabular::COPY_ROW:
4518 case Tabular::COPY_COLUMN:
4519 case Tabular::SET_TOP_SPACE:
4520 case Tabular::SET_BOTTOM_SPACE:
4521 case Tabular::SET_INTERLINE_SPACE:
4525 case Tabular::SET_TABULAR_WIDTH:
4526 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4527 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4530 case Tabular::MOVE_COLUMN_RIGHT:
4531 case Tabular::MOVE_COLUMN_LEFT:
4532 case Tabular::MOVE_ROW_DOWN:
4533 case Tabular::MOVE_ROW_UP: {
4534 if (cur.selection()) {
4535 status.message(_("Selections not supported."));
4536 status.setEnabled(false);
4540 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4541 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4542 (action == Tabular::MOVE_COLUMN_LEFT &&
4543 tabular.cellColumn(cur.idx()) == 0) ||
4544 (action == Tabular::MOVE_ROW_DOWN &&
4545 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4546 (action == Tabular::MOVE_ROW_UP &&
4547 tabular.cellRow(cur.idx()) == 0)) {
4548 status.setEnabled(false);
4552 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4553 action == Tabular::MOVE_COLUMN_LEFT) {
4554 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4555 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4556 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4557 status.message(_("Multi-column in current or"
4558 " destination column."));
4559 status.setEnabled(false);
4564 if (action == Tabular::MOVE_ROW_DOWN ||
4565 action == Tabular::MOVE_ROW_UP) {
4566 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4567 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4568 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4569 status.message(_("Multi-row in current or"
4570 " destination row."));
4571 status.setEnabled(false);
4576 status.setEnabled(true);
4580 case Tabular::SET_DECIMAL_POINT:
4582 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4585 case Tabular::SET_MULTICOLUMN:
4586 case Tabular::UNSET_MULTICOLUMN:
4587 case Tabular::MULTICOLUMN:
4588 // If a row is set as longtable caption, it must not be allowed
4589 // to unset that this row is a multicolumn.
4590 status.setEnabled(sel_row_start == sel_row_end
4591 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4592 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4595 case Tabular::SET_MULTIROW:
4596 case Tabular::UNSET_MULTIROW:
4597 case Tabular::MULTIROW:
4598 // If a row is set as longtable caption, it must not be allowed
4599 // to unset that this row is a multirow.
4600 status.setEnabled(sel_col_start == sel_col_end
4601 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4602 status.setOnOff(tabular.isMultiRow(cur.idx()));
4605 case Tabular::SET_ALL_LINES:
4606 case Tabular::UNSET_ALL_LINES:
4607 case Tabular::SET_BORDER_LINES:
4608 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4611 case Tabular::SET_LINE_TOP:
4612 case Tabular::SET_LINE_BOTTOM:
4613 case Tabular::SET_LINE_LEFT:
4614 case Tabular::SET_LINE_RIGHT:
4615 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4618 case Tabular::TOGGLE_LINE_TOP:
4619 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4620 status.setOnOff(tabular.topLine(cur.idx()));
4623 case Tabular::TOGGLE_LINE_BOTTOM:
4624 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4625 status.setOnOff(tabular.bottomLine(cur.idx()));
4628 case Tabular::TOGGLE_LINE_LEFT:
4629 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4630 status.setOnOff(tabular.leftLine(cur.idx()));
4633 case Tabular::TOGGLE_LINE_RIGHT:
4634 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4635 status.setOnOff(tabular.rightLine(cur.idx()));
4638 // multirow cells only inherit the alignment of the column if the column has
4639 // no width, otherwise they are left-aligned
4640 // therefore allow always left but right and center only if there is no width
4641 case Tabular::M_ALIGN_LEFT:
4643 case Tabular::ALIGN_LEFT:
4644 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4647 case Tabular::M_ALIGN_RIGHT:
4649 case Tabular::ALIGN_RIGHT:
4650 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4651 && !tabular.getPWidth(cur.idx()).zero()));
4652 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4655 case Tabular::M_ALIGN_CENTER:
4657 case Tabular::ALIGN_CENTER:
4658 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4659 && !tabular.getPWidth(cur.idx()).zero()));
4660 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4663 case Tabular::ALIGN_BLOCK:
4664 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4665 && !tabular.isMultiRow(cur.idx()));
4666 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4669 case Tabular::ALIGN_DECIMAL:
4670 status.setEnabled(!tabular.isMultiRow(cur.idx())
4671 && !tabular.isMultiColumn(cur.idx()));
4672 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4675 case Tabular::M_VALIGN_TOP:
4677 case Tabular::VALIGN_TOP:
4678 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4679 && !tabular.isMultiRow(cur.idx()));
4681 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4684 case Tabular::M_VALIGN_BOTTOM:
4686 case Tabular::VALIGN_BOTTOM:
4687 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4688 && !tabular.isMultiRow(cur.idx()));
4690 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4693 case Tabular::M_VALIGN_MIDDLE:
4695 case Tabular::VALIGN_MIDDLE:
4696 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4697 && !tabular.isMultiRow(cur.idx()));
4699 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4702 case Tabular::SET_LONGTABULAR:
4703 // setting as longtable is not allowed when table is inside a float
4704 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4705 || cur.innerInsetOfType(WRAP_CODE) != 0)
4706 status.setEnabled(false);
4708 status.setEnabled(true);
4709 status.setOnOff(tabular.is_long_tabular);
4712 case Tabular::UNSET_LONGTABULAR:
4713 status.setOnOff(!tabular.is_long_tabular);
4716 case Tabular::TOGGLE_ROTATE_TABULAR:
4717 case Tabular::SET_ROTATE_TABULAR:
4718 status.setOnOff(tabular.rotate != 0);
4721 case Tabular::TABULAR_VALIGN_TOP:
4722 status.setEnabled(tabular.tabular_width.zero());
4723 status.setOnOff(tabular.tabular_valignment
4724 == Tabular::LYX_VALIGN_TOP);
4726 case Tabular::TABULAR_VALIGN_MIDDLE:
4727 status.setEnabled(tabular.tabular_width.zero());
4728 status.setOnOff(tabular.tabular_valignment
4729 == Tabular::LYX_VALIGN_MIDDLE);
4731 case Tabular::TABULAR_VALIGN_BOTTOM:
4732 status.setEnabled(tabular.tabular_width.zero());
4733 status.setOnOff(tabular.tabular_valignment
4734 == Tabular::LYX_VALIGN_BOTTOM);
4737 case Tabular::LONGTABULAR_ALIGN_LEFT:
4738 status.setOnOff(tabular.longtabular_alignment
4739 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4741 case Tabular::LONGTABULAR_ALIGN_CENTER:
4742 status.setOnOff(tabular.longtabular_alignment
4743 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4745 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4746 status.setOnOff(tabular.longtabular_alignment
4747 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4750 case Tabular::UNSET_ROTATE_TABULAR:
4751 status.setOnOff(tabular.rotate == 0);
4754 case Tabular::TOGGLE_ROTATE_CELL:
4755 case Tabular::SET_ROTATE_CELL:
4756 status.setOnOff(!oneCellHasRotationState(false,
4757 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4760 case Tabular::UNSET_ROTATE_CELL:
4761 status.setOnOff(!oneCellHasRotationState(true,
4762 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4765 case Tabular::SET_USEBOX:
4766 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4769 // every row can only be one thing:
4770 // either a footer or header
4771 case Tabular::SET_LTFIRSTHEAD:
4772 status.setEnabled(sel_row_start == sel_row_end);
4773 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4776 case Tabular::UNSET_LTFIRSTHEAD:
4777 status.setEnabled(sel_row_start == sel_row_end);
4778 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4781 case Tabular::SET_LTHEAD:
4782 status.setEnabled(sel_row_start == sel_row_end);
4783 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4786 case Tabular::UNSET_LTHEAD:
4787 status.setEnabled(sel_row_start == sel_row_end);
4788 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4791 case Tabular::SET_LTFOOT:
4792 status.setEnabled(sel_row_start == sel_row_end);
4793 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4796 case Tabular::UNSET_LTFOOT:
4797 status.setEnabled(sel_row_start == sel_row_end);
4798 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4801 case Tabular::SET_LTLASTFOOT:
4802 status.setEnabled(sel_row_start == sel_row_end);
4803 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4806 case Tabular::UNSET_LTLASTFOOT:
4807 status.setEnabled(sel_row_start == sel_row_end);
4808 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4811 case Tabular::SET_LTNEWPAGE:
4812 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4814 case Tabular::UNSET_LTNEWPAGE:
4815 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4818 // only one row in head/firsthead/foot/lasthead can be the caption
4819 // and a multirow cannot be set as caption
4820 case Tabular::SET_LTCAPTION:
4821 status.setEnabled(sel_row_start == sel_row_end
4822 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4823 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4824 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4825 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4826 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4827 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4828 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4829 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4830 && !tabular.isMultiRow(sel_row_start));
4831 status.setOnOff(tabular.ltCaption(sel_row_start));
4834 case Tabular::UNSET_LTCAPTION:
4835 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4838 case Tabular::TOGGLE_LTCAPTION:
4839 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4840 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4841 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4842 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4843 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4844 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4845 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4846 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4847 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4848 status.setOnOff(tabular.ltCaption(sel_row_start));
4851 case Tabular::SET_BOOKTABS:
4852 status.setOnOff(tabular.use_booktabs);
4855 case Tabular::UNSET_BOOKTABS:
4856 status.setOnOff(!tabular.use_booktabs);
4861 status.setEnabled(false);
4867 case LFUN_CAPTION_INSERT: {
4868 // caption is only allowed in caption cell of longtable
4869 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4870 status.setEnabled(false);
4873 // only standard caption is allowed
4874 string arg = cmd.getArg(0);
4875 if (!arg.empty() && arg != "Standard") {
4876 status.setEnabled(false);
4879 // check if there is already a caption
4880 bool have_caption = false;
4881 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4882 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4883 ParagraphList::const_iterator pend = itc.paragraphs().end();
4884 for (; pit != pend; ++pit) {
4885 InsetList::const_iterator it = pit->insetList().begin();
4886 InsetList::const_iterator end = pit->insetList().end();
4887 for (; it != end; ++it) {
4888 if (it->inset->lyxCode() == CAPTION_CODE) {
4889 have_caption = true;
4894 status.setEnabled(!have_caption);
4898 // These are only enabled inside tabular
4899 case LFUN_CELL_BACKWARD:
4900 case LFUN_CELL_FORWARD:
4901 status.setEnabled(true);
4904 // disable these with multiple cells selected
4905 case LFUN_INSET_INSERT:
4906 case LFUN_TABULAR_INSERT:
4907 case LFUN_FLEX_INSERT:
4908 case LFUN_FLOAT_INSERT:
4909 case LFUN_FLOAT_WIDE_INSERT:
4910 case LFUN_FOOTNOTE_INSERT:
4911 case LFUN_MARGINALNOTE_INSERT:
4912 case LFUN_MATH_INSERT:
4913 case LFUN_MATH_MODE:
4914 case LFUN_MATH_MUTATE:
4915 case LFUN_MATH_DISPLAY:
4916 case LFUN_NOTE_INSERT:
4917 case LFUN_ARGUMENT_INSERT:
4918 case LFUN_BOX_INSERT:
4919 case LFUN_BRANCH_INSERT:
4920 case LFUN_PHANTOM_INSERT:
4921 case LFUN_WRAP_INSERT:
4922 case LFUN_PREVIEW_INSERT:
4923 case LFUN_ERT_INSERT: {
4924 if (cur.selIsMultiCell()) {
4925 status.setEnabled(false);
4928 return cell(cur.idx())->getStatus(cur, cmd, status);
4931 // disable in non-fixed-width cells
4932 case LFUN_PARAGRAPH_BREAK:
4933 // multirow does not allow paragraph breaks
4934 if (tabular.isMultiRow(cur.idx())) {
4935 status.setEnabled(false);
4938 case LFUN_NEWLINE_INSERT: {
4939 if (tabular.getPWidth(cur.idx()).zero()) {
4940 status.setEnabled(false);
4943 return cell(cur.idx())->getStatus(cur, cmd, status);
4946 case LFUN_NEWPAGE_INSERT:
4947 status.setEnabled(false);
4951 if (tabularStackDirty() && theClipboard().isInternal()) {
4952 if (cur.selIsMultiCell()) {
4955 getSelection(cur, rs, re, cs, ce);
4956 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4957 && paste_tabular->nrows() == re - rs + 1)
4958 status.setEnabled(true);
4960 status.setEnabled(false);
4961 status.message(_("Selection size should match clipboard content."));
4964 status.setEnabled(true);
4967 return cell(cur.idx())->getStatus(cur, cmd, status);
4969 case LFUN_INSET_SETTINGS:
4970 // relay this lfun to Inset, not to the cell.
4971 return Inset::getStatus(cur, cmd, status);
4974 // we try to handle this event in the insets dispatch function.
4975 return cell(cur.idx())->getStatus(cur, cmd, status);
4981 Inset::DisplayType InsetTabular::display() const
4983 if (tabular.is_long_tabular) {
4984 switch (tabular.longtabular_alignment) {
4985 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4987 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4989 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4999 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5001 tabular.latex(os, runparams);
5005 int InsetTabular::plaintext(odocstringstream & os,
5006 OutputParams const & runparams, size_t max_length) const
5008 os << '\n'; // output table on a new line
5009 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5010 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5011 return PLAINTEXT_NEWLINE;
5015 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5020 // FIXME: Why not pass a proper DocIterator here?
5022 // if the table is inside a float it doesn't need the informaltable
5023 // wrapper. Search for it.
5024 for (master = owner(); master; master = master->owner())
5025 if (master->lyxCode() == FLOAT_CODE)
5030 os << "<informaltable>";
5033 ret += tabular.docbook(os, runparams);
5035 os << "</informaltable>";
5042 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5044 return tabular.xhtml(xs, rp);
5048 void InsetTabular::validate(LaTeXFeatures & features) const
5050 tabular.validate(features);
5052 // It'd be better to be able to get this from an InsetLayout, but at present
5053 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5054 if (features.runparams().flavor == OutputParams::HTML)
5055 features.addCSSSnippet(
5056 "table { border: 1px solid black; display: inline-block; }\n"
5057 "td { border: 1px solid black; padding: 0.5ex; }");
5061 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5063 return tabular.cellInset(idx);
5067 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5069 return tabular.cellInset(idx);
5073 void InsetTabular::cursorPos(BufferView const & bv,
5074 CursorSlice const & sl, bool boundary, int & x, int & y) const
5076 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5078 // y offset correction
5079 y += cellYPos(sl.idx());
5080 y += tabular.textVOffset(sl.idx());
5081 y += offset_valign_;
5083 // x offset correction
5084 x += cellXPos(sl.idx());
5085 x += tabular.textHOffset(sl.idx());
5086 x += ADD_TO_TABULAR_WIDTH;
5091 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5095 Inset const & inset = *tabular.cellInset(cell);
5096 Point o = bv.coordCache().getInsets().xy(&inset);
5097 int const xbeg = o.x_ - tabular.textHOffset(cell);
5098 int const xend = xbeg + tabular.cellWidth(cell);
5099 row_type const row = tabular.cellRow(cell);
5100 int const ybeg = o.y_ - tabular.rowAscent(row)
5101 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5102 int const yend = ybeg + tabular.cellHeight(cell);
5114 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5115 // << " ybeg=" << ybeg << " yend=" << yend
5116 // << " xx=" << xx << " yy=" << yy
5117 // << " dist=" << xx + yy << endl;
5122 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5124 //lyxerr << "InsetTabular::editXY: " << this << endl;
5125 cur.setSelection(false);
5127 cur.idx() = getNearestCell(cur.bv(), x, y);
5129 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5133 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5135 cur.idx() = getNearestCell(cur.bv(), x, y);
5136 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5140 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5142 idx_type idx_min = 0;
5143 int dist_min = numeric_limits<int>::max();
5144 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5145 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5146 int const d = dist(bv, i, x, y);
5157 int InsetTabular::cellYPos(idx_type const cell) const
5159 row_type row = tabular.cellRow(cell);
5161 for (row_type r = 0; r < row; ++r)
5162 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5163 + tabular.interRowSpace(r + 1);
5168 int InsetTabular::cellXPos(idx_type const cell) const
5170 col_type col = tabular.cellColumn(cell);
5172 for (col_type c = 0; c < col; ++c)
5173 lx += tabular.column_info[c].width;
5178 void InsetTabular::resetPos(Cursor & cur) const
5180 BufferView & bv = cur.bv();
5181 int const maxwidth = bv.workWidth();
5183 int const scx_old = scx_;
5184 int const i = cur.find(this);
5189 int const X2 = maxwidth;
5190 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5191 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5192 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5195 scx_ = X1 + 20 - x1;
5197 scx_ = X2 - 20 - x2;
5202 // only update if offset changed
5203 if (scx_ != scx_old)
5204 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5208 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5210 row_type const row = tabular.cellRow(cur.idx());
5211 col_type const col = tabular.cellColumn(cur.idx());
5213 if (isRightToLeft(cur)) {
5214 if (tabular.cellColumn(cur.idx()) == 0) {
5215 if (row == tabular.nrows() - 1)
5217 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5222 cur.idx() = tabular.getLastCellInRow(row - 1);
5224 cur.idx() = tabular.cellIndex(row, col - 1);
5227 if (tabular.isLastCell(cur.idx()))
5229 if (cur.idx() == tabular.getLastCellInRow(row))
5230 cur.idx() = tabular.cellIndex(row + 1, 0);
5232 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5233 cur.idx() = tabular.cellIndex(row, colnextcell);
5237 cur.boundary(false);
5239 if (cur.selIsMultiCell()) {
5240 cur.pit() = cur.lastpit();
5241 cur.pos() = cur.lastpos();
5249 // in visual mode, place cursor at extreme left or right
5251 switch(entry_from) {
5253 case ENTRY_DIRECTION_RIGHT:
5254 cur.posVisToRowExtremity(false /* !left */);
5256 case ENTRY_DIRECTION_LEFT:
5257 cur.posVisToRowExtremity(true /* left */);
5259 case ENTRY_DIRECTION_IGNORE:
5260 // nothing to do in this case
5264 cur.setCurrentFont();
5269 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5271 row_type const row = tabular.cellRow(cur.idx());
5272 col_type const col = tabular.cellColumn(cur.idx());
5274 if (isRightToLeft(cur)) {
5275 if (cur.idx() == tabular.getLastCellInRow(row)) {
5278 cur.idx() = tabular.getFirstCellInRow(row);
5279 cur.idx() = tabular.cellAbove(cur.idx());
5281 if (tabular.isLastCell(cur.idx()))
5283 if (cur.idx() == tabular.getLastCellInRow(row))
5284 cur.idx() = tabular.cellIndex(row + 1, 0);
5286 cur.idx() = tabular.cellIndex(row, col + 1);
5289 if (cur.idx() == 0) // first cell
5292 cur.idx() = tabular.getLastCellInRow(row - 1);
5294 cur.idx() = tabular.cellIndex(row, col - 1);
5297 if (cur.selIsMultiCell()) {
5298 cur.pit() = cur.lastpit();
5299 cur.pos() = cur.lastpos();
5304 cur.pit() = cur.lastpit();
5305 cur.pos() = cur.lastpos();
5307 // in visual mode, place cursor at extreme left or right
5309 switch(entry_from) {
5311 case ENTRY_DIRECTION_RIGHT:
5312 cur.posVisToRowExtremity(false /* !left */);
5314 case ENTRY_DIRECTION_LEFT:
5315 cur.posVisToRowExtremity(true /* left */);
5317 case ENTRY_DIRECTION_IGNORE:
5318 // nothing to do in this case
5322 cur.setCurrentFont();
5327 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5329 istringstream is(argument);
5332 if (insetCode(s) != TABULAR_CODE)
5336 size_t safe_guard = 0;
5341 if (safe_guard > 1000) {
5342 LYXERR0("parameter max count reached!");
5346 Tabular::Feature action = Tabular::LAST_ACTION;
5349 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5350 if (s != tabularFeature[i].feature)
5352 action = tabularFeature[i].action;
5355 if (action == Tabular::LAST_ACTION) {
5356 LYXERR0("Feature not found " << s);
5360 if (tabularFeature[i].need_value)
5362 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5363 tabularFeatures(cur, action, val);
5369 static void checkLongtableSpecial(Tabular::ltType & ltt,
5370 string const & special, bool & flag)
5372 if (special == "dl_above") {
5375 } else if (special == "dl_below") {
5376 ltt.bottomDL = flag;
5378 } else if (special == "empty") {
5388 bool InsetTabular::oneCellHasRotationState(bool rotated,
5389 row_type row_start, row_type row_end,
5390 col_type col_start, col_type col_end) const
5392 for (row_type r = row_start; r <= row_end; ++r)
5393 for (col_type c = col_start; c <= col_end; ++c)
5395 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5398 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5405 void InsetTabular::tabularFeatures(Cursor & cur,
5406 Tabular::Feature feature, string const & value)
5408 col_type sel_col_start;
5409 col_type sel_col_end;
5410 row_type sel_row_start;
5411 row_type sel_row_end;
5412 bool setLines = false;
5413 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5414 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5418 case Tabular::M_ALIGN_LEFT:
5419 case Tabular::ALIGN_LEFT:
5420 setAlign = LYX_ALIGN_LEFT;
5423 case Tabular::M_ALIGN_RIGHT:
5424 case Tabular::ALIGN_RIGHT:
5425 setAlign = LYX_ALIGN_RIGHT;
5428 case Tabular::M_ALIGN_CENTER:
5429 case Tabular::ALIGN_CENTER:
5430 setAlign = LYX_ALIGN_CENTER;
5433 case Tabular::ALIGN_BLOCK:
5434 setAlign = LYX_ALIGN_BLOCK;
5437 case Tabular::ALIGN_DECIMAL:
5438 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5439 setAlign = LYX_ALIGN_CENTER;
5441 setAlign = LYX_ALIGN_DECIMAL;
5444 case Tabular::M_VALIGN_TOP:
5445 case Tabular::VALIGN_TOP:
5446 setVAlign = Tabular::LYX_VALIGN_TOP;
5449 case Tabular::M_VALIGN_BOTTOM:
5450 case Tabular::VALIGN_BOTTOM:
5451 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5454 case Tabular::M_VALIGN_MIDDLE:
5455 case Tabular::VALIGN_MIDDLE:
5456 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5463 cur.recordUndoInset(ATOMIC_UNDO, this);
5465 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5466 row_type const row = tabular.cellRow(cur.idx());
5467 col_type const column = tabular.cellColumn(cur.idx());
5469 Tabular::ltType ltt;
5473 case Tabular::SET_TABULAR_WIDTH:
5474 tabular.setTabularWidth(Length(value));
5477 case Tabular::SET_PWIDTH: {
5478 Length const len(value);
5479 tabular.setColumnPWidth(cur, cur.idx(), len);
5481 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5482 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5486 case Tabular::SET_MPWIDTH:
5487 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5490 case Tabular::SET_MROFFSET:
5491 tabular.setMROffset(cur, cur.idx(), Length(value));
5494 case Tabular::SET_SPECIAL_COLUMN:
5495 case Tabular::SET_SPECIAL_MULTICOLUMN:
5496 if (value == "none")
5497 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5499 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5502 case Tabular::APPEND_ROW:
5503 // append the row into the tabular
5504 tabular.appendRow(row);
5507 case Tabular::APPEND_COLUMN:
5508 // append the column into the tabular
5509 tabular.appendColumn(column);
5510 cur.idx() = tabular.cellIndex(row, column);
5513 case Tabular::DELETE_ROW:
5514 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5515 for (col_type c = 0; c < tabular.ncols(); c++)
5516 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5517 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5520 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5521 tabular.deleteRow(sel_row_start);
5522 if (sel_row_start >= tabular.nrows())
5524 cur.idx() = tabular.cellIndex(sel_row_start, column);
5527 cur.setSelection(false);
5530 case Tabular::DELETE_COLUMN:
5531 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5532 for (row_type r = 0; r < tabular.nrows(); r++)
5533 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5534 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5537 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5538 for (row_type r = 0; r < tabular.nrows(); r++)
5539 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5540 tabular.leftLine(tabular.cellIndex(r, 0)));
5543 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5544 tabular.deleteColumn(sel_col_start);
5545 if (sel_col_start >= tabular.ncols())
5547 cur.idx() = tabular.cellIndex(row, sel_col_start);
5550 cur.setSelection(false);
5553 case Tabular::COPY_ROW:
5554 tabular.copyRow(row);
5557 case Tabular::COPY_COLUMN:
5558 tabular.copyColumn(column);
5559 cur.idx() = tabular.cellIndex(row, column);
5562 case Tabular::MOVE_COLUMN_RIGHT:
5563 tabular.moveColumn(column, Tabular::RIGHT);
5564 cur.idx() = tabular.cellIndex(row, column + 1);
5567 case Tabular::MOVE_COLUMN_LEFT:
5568 tabular.moveColumn(column, Tabular::LEFT);
5569 cur.idx() = tabular.cellIndex(row, column - 1);
5572 case Tabular::MOVE_ROW_DOWN:
5573 tabular.moveRow(row, Tabular::DOWN);
5574 cur.idx() = tabular.cellIndex(row + 1, column);
5577 case Tabular::MOVE_ROW_UP:
5578 tabular.moveRow(row, Tabular::UP);
5579 cur.idx() = tabular.cellIndex(row - 1, column);
5582 case Tabular::SET_LINE_TOP:
5583 case Tabular::TOGGLE_LINE_TOP: {
5584 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5585 ? (value == "true") : !tabular.topLine(cur.idx());
5586 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5587 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5588 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5592 case Tabular::SET_LINE_BOTTOM:
5593 case Tabular::TOGGLE_LINE_BOTTOM: {
5594 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5595 ? (value == "true") : !tabular.bottomLine(cur.idx());
5596 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5597 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5598 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5602 case Tabular::SET_LINE_LEFT:
5603 case Tabular::TOGGLE_LINE_LEFT: {
5604 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5605 ? (value == "true") : !tabular.leftLine(cur.idx());
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.setLeftLine(tabular.cellIndex(r, c), lineSet);
5612 case Tabular::SET_LINE_RIGHT:
5613 case Tabular::TOGGLE_LINE_RIGHT: {
5614 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5615 ? (value == "true") : !tabular.rightLine(cur.idx());
5616 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5617 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5618 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5622 case Tabular::M_ALIGN_LEFT:
5623 case Tabular::M_ALIGN_RIGHT:
5624 case Tabular::M_ALIGN_CENTER:
5625 case Tabular::ALIGN_LEFT:
5626 case Tabular::ALIGN_RIGHT:
5627 case Tabular::ALIGN_CENTER:
5628 case Tabular::ALIGN_BLOCK:
5629 case Tabular::ALIGN_DECIMAL:
5630 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5631 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5632 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5633 !tabular.getPWidth(c).zero());
5636 case Tabular::M_VALIGN_TOP:
5637 case Tabular::M_VALIGN_BOTTOM:
5638 case Tabular::M_VALIGN_MIDDLE:
5640 case Tabular::VALIGN_TOP:
5641 case Tabular::VALIGN_BOTTOM:
5642 case Tabular::VALIGN_MIDDLE:
5643 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5644 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5645 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5648 case Tabular::SET_MULTICOLUMN: {
5649 if (!cur.selection()) {
5650 // just multicol for one single cell
5651 // check whether we are completely in a multicol
5652 if (!tabular.isMultiColumn(cur.idx()))
5653 tabular.setMultiColumn(cur.idx(), 1,
5654 tabular.rightLine(cur.idx()));
5657 // we have a selection so this means we just add all these
5658 // cells to form a multicolumn cell
5659 idx_type const s_start = cur.selBegin().idx();
5660 row_type const col_start = tabular.cellColumn(s_start);
5661 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5662 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5663 tabular.rightLine(cur.selEnd().idx()));
5666 cur.setSelection(false);
5670 case Tabular::UNSET_MULTICOLUMN: {
5671 if (!cur.selection()) {
5672 if (tabular.isMultiColumn(cur.idx()))
5673 tabular.unsetMultiColumn(cur.idx());
5678 case Tabular::MULTICOLUMN: {
5679 if (!cur.selection()) {
5680 if (tabular.isMultiColumn(cur.idx()))
5681 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5683 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5687 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5688 row_type const r = sel_row_start;
5689 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5690 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5693 // If the selection contains at least one singlecol cell
5694 // or multiple multicol cells,
5695 // we assume the user will merge is to a single multicol
5697 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5699 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5703 case Tabular::SET_MULTIROW: {
5704 if (!cur.selection()) {
5705 // just multirow for one single cell
5706 // check whether we are completely in a multirow
5707 if (!tabular.isMultiRow(cur.idx()))
5708 tabular.setMultiRow(cur.idx(), 1,
5709 tabular.bottomLine(cur.idx()),
5710 tabular.getAlignment(cur.idx()));
5713 // we have a selection so this means we just add all this
5714 // cells to form a multirow cell
5715 idx_type const s_start = cur.selBegin().idx();
5716 row_type const row_start = tabular.cellRow(s_start);
5717 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5718 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5719 tabular.bottomLine(cur.selEnd().idx()),
5720 tabular.getAlignment(cur.selEnd().idx()));
5723 cur.setSelection(false);
5727 case Tabular::UNSET_MULTIROW: {
5728 if (!cur.selection()) {
5729 if (tabular.isMultiRow(cur.idx()))
5730 tabular.unsetMultiRow(cur.idx());
5735 case Tabular::MULTIROW: {
5736 if (!cur.selection()) {
5737 if (tabular.isMultiRow(cur.idx()))
5738 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5740 tabularFeatures(cur, Tabular::SET_MULTIROW);
5744 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5745 col_type const c = sel_col_start;
5746 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5747 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5750 // If the selection contains at least one singlerow cell
5751 // or multiple multirow cells,
5752 // we assume the user will merge is to a single multirow
5754 tabularFeatures(cur, Tabular::SET_MULTIROW);
5756 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5760 case Tabular::SET_ALL_LINES:
5762 case Tabular::UNSET_ALL_LINES:
5763 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5764 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5765 idx_type const cell = tabular.cellIndex(r, c);
5766 tabular.setTopLine(cell, setLines);
5767 tabular.setBottomLine(cell, setLines);
5768 tabular.setRightLine(cell, setLines);
5769 tabular.setLeftLine(cell, setLines);
5773 case Tabular::SET_BORDER_LINES:
5774 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5775 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5776 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5778 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5779 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5780 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5784 case Tabular::SET_LONGTABULAR:
5785 tabular.is_long_tabular = true;
5788 case Tabular::UNSET_LONGTABULAR:
5789 for (row_type r = 0; r < tabular.nrows(); ++r) {
5790 if (tabular.ltCaption(r)) {
5791 cur.idx() = tabular.cellIndex(r, 0);
5794 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5797 tabular.is_long_tabular = false;
5800 case Tabular::SET_ROTATE_TABULAR:
5801 tabular.rotate = convert<int>(value);
5804 case Tabular::UNSET_ROTATE_TABULAR:
5808 case Tabular::TOGGLE_ROTATE_TABULAR:
5809 // when pressing the rotate button we default to 90° rotation
5810 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5813 case Tabular::TABULAR_VALIGN_TOP:
5814 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5817 case Tabular::TABULAR_VALIGN_MIDDLE:
5818 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5821 case Tabular::TABULAR_VALIGN_BOTTOM:
5822 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5825 case Tabular::LONGTABULAR_ALIGN_LEFT:
5826 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5829 case Tabular::LONGTABULAR_ALIGN_CENTER:
5830 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5833 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5834 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5839 case Tabular::SET_ROTATE_CELL:
5840 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5841 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5842 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5845 case Tabular::UNSET_ROTATE_CELL:
5846 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5847 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5848 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5851 case Tabular::TOGGLE_ROTATE_CELL:
5853 bool oneNotRotated = oneCellHasRotationState(false,
5854 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5856 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5857 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5858 // when pressing the rotate cell button we default to 90° rotation
5860 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5862 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5867 case Tabular::SET_USEBOX: {
5868 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5869 if (val == tabular.getUsebox(cur.idx()))
5870 val = Tabular::BOX_NONE;
5871 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5872 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5873 tabular.setUsebox(tabular.cellIndex(r, c), val);
5877 case Tabular::UNSET_LTFIRSTHEAD:
5879 case Tabular::SET_LTFIRSTHEAD:
5880 tabular.getRowOfLTFirstHead(row, ltt);
5881 checkLongtableSpecial(ltt, value, flag);
5882 tabular.setLTHead(row, flag, ltt, true);
5885 case Tabular::UNSET_LTHEAD:
5887 case Tabular::SET_LTHEAD:
5888 tabular.getRowOfLTHead(row, ltt);
5889 checkLongtableSpecial(ltt, value, flag);
5890 tabular.setLTHead(row, flag, ltt, false);
5893 case Tabular::UNSET_LTFOOT:
5895 case Tabular::SET_LTFOOT:
5896 tabular.getRowOfLTFoot(row, ltt);
5897 checkLongtableSpecial(ltt, value, flag);
5898 tabular.setLTFoot(row, flag, ltt, false);
5901 case Tabular::UNSET_LTLASTFOOT:
5903 case Tabular::SET_LTLASTFOOT:
5904 tabular.getRowOfLTLastFoot(row, ltt);
5905 checkLongtableSpecial(ltt, value, flag);
5906 tabular.setLTFoot(row, flag, ltt, true);
5909 case Tabular::UNSET_LTNEWPAGE:
5911 case Tabular::SET_LTNEWPAGE:
5912 tabular.setLTNewPage(row, flag);
5915 case Tabular::SET_LTCAPTION: {
5916 if (tabular.ltCaption(row))
5918 cur.idx() = tabular.setLTCaption(row, true);
5921 cur.setSelection(false);
5922 // If a row is set as caption, then also insert
5923 // a caption. Otherwise the LaTeX output is broken.
5924 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5925 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5929 case Tabular::UNSET_LTCAPTION: {
5930 if (!tabular.ltCaption(row))
5932 cur.idx() = tabular.setLTCaption(row, false);
5935 cur.setSelection(false);
5936 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5937 if (lyx::getStatus(fr).enabled())
5942 case Tabular::TOGGLE_LTCAPTION: {
5943 if (tabular.ltCaption(row))
5944 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5946 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5950 case Tabular::SET_BOOKTABS:
5951 tabular.use_booktabs = true;
5954 case Tabular::UNSET_BOOKTABS:
5955 tabular.use_booktabs = false;
5958 case Tabular::SET_TOP_SPACE: {
5960 if (value == "default")
5961 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5962 tabular.row_info[r].top_space_default = true;
5963 else if (value == "none")
5964 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5965 tabular.row_info[r].top_space_default = false;
5966 tabular.row_info[r].top_space = len;
5968 else if (isValidLength(value, &len))
5969 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5970 tabular.row_info[r].top_space_default = false;
5971 tabular.row_info[r].top_space = len;
5976 case Tabular::SET_BOTTOM_SPACE: {
5978 if (value == "default")
5979 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5980 tabular.row_info[r].bottom_space_default = true;
5981 else if (value == "none")
5982 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5983 tabular.row_info[r].bottom_space_default = false;
5984 tabular.row_info[r].bottom_space = len;
5986 else if (isValidLength(value, &len))
5987 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5988 tabular.row_info[r].bottom_space_default = false;
5989 tabular.row_info[r].bottom_space = len;
5994 case Tabular::SET_INTERLINE_SPACE: {
5996 if (value == "default")
5997 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5998 tabular.row_info[r].interline_space_default = true;
5999 else if (value == "none")
6000 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6001 tabular.row_info[r].interline_space_default = false;
6002 tabular.row_info[r].interline_space = len;
6004 else if (isValidLength(value, &len))
6005 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6006 tabular.row_info[r].interline_space_default = false;
6007 tabular.row_info[r].interline_space = len;
6012 case Tabular::SET_DECIMAL_POINT:
6013 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6014 tabular.column_info[c].decimal_point = from_utf8(value);
6017 // dummy stuff just to avoid warnings
6018 case Tabular::LAST_ACTION:
6024 bool InsetTabular::copySelection(Cursor & cur)
6026 if (!cur.selection())
6031 getSelection(cur, rs, re, cs, ce);
6033 paste_tabular.reset(new Tabular(tabular));
6035 for (row_type r = 0; r < rs; ++r)
6036 paste_tabular->deleteRow(0);
6038 row_type const rows = re - rs + 1;
6039 while (paste_tabular->nrows() > rows)
6040 paste_tabular->deleteRow(rows);
6042 for (col_type c = 0; c < cs; ++c)
6043 paste_tabular->deleteColumn(0);
6045 col_type const columns = ce - cs + 1;
6046 while (paste_tabular->ncols() > columns)
6047 paste_tabular->deleteColumn(columns);
6049 paste_tabular->setBuffer(tabular.buffer());
6051 odocstringstream os;
6052 OutputParams const runparams(0);
6053 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6054 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6055 cap::copySelection(cur, os.str());
6057 // mark tabular stack dirty
6058 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6059 // when we (hopefully) have a one-for-all paste mechanism.
6060 // This must be called after cap::copySelection.
6061 dirtyTabularStack(true);
6067 bool InsetTabular::pasteClipboard(Cursor & cur)
6071 col_type actcol = tabular.cellColumn(cur.idx());
6072 row_type actrow = tabular.cellRow(cur.idx());
6074 if (cur.selIsMultiCell()) {
6077 getSelection(cur, actrow, re, actcol, ce);
6080 for (row_type r1 = 0, r2 = actrow;
6081 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6083 for (col_type c1 = 0, c2 = actcol;
6084 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6086 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6087 tabular.isPartOfMultiColumn(r2, c2))
6089 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6093 if (tabular.isPartOfMultiColumn(r2, c2)) {
6097 shared_ptr<InsetTableCell> inset(
6098 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6099 tabular.setCellInset(r2, c2, inset);
6100 // FIXME?: why do we need to do this explicitly? (EL)
6101 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6103 // FIXME: change tracking (MG)
6104 inset->setChange(Change(buffer().params().track_changes ?
6105 Change::INSERTED : Change::UNCHANGED));
6113 void InsetTabular::cutSelection(Cursor & cur)
6115 if (!cur.selection())
6120 getSelection(cur, rs, re, cs, ce);
6121 for (row_type r = rs; r <= re; ++r) {
6122 for (col_type c = cs; c <= ce; ++c) {
6123 shared_ptr<InsetTableCell> t
6124 = cell(tabular.cellIndex(r, c));
6125 if (buffer().params().track_changes)
6126 // FIXME: Change tracking (MG)
6127 t->setChange(Change(Change::DELETED));
6133 // cursor position might be invalid now
6134 if (cur.pit() > cur.lastpit())
6135 cur.pit() = cur.lastpit();
6136 if (cur.pos() > cur.lastpos())
6137 cur.pos() = cur.lastpos();
6138 cur.clearSelection();
6142 bool InsetTabular::isRightToLeft(Cursor & cur) const
6144 // LASSERT: It might be better to abandon this Buffer.
6145 LASSERT(cur.depth() > 1, return false);
6146 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6147 pos_type const parentpos = cur[cur.depth() - 2].pos();
6148 return parentpar.getFontSettings(buffer().params(),
6149 parentpos).language()->rightToLeft();
6153 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6156 LASSERT(stidx <= enidx, return docstring());
6158 col_type const col1 = tabular.cellColumn(stidx);
6159 col_type const col2 = tabular.cellColumn(enidx);
6160 row_type const row1 = tabular.cellRow(stidx);
6161 row_type const row2 = tabular.cellRow(enidx);
6163 for (col_type col = col1; col <= col2; col++)
6164 for (row_type row = row1; row <= row2; row++) {
6169 retval += tabular.cellInset(row, col)->asString(intoInsets);
6175 void InsetTabular::getSelection(Cursor & cur,
6176 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6178 CursorSlice const & beg = cur.selBegin();
6179 CursorSlice const & end = cur.selEnd();
6180 cs = tabular.cellColumn(beg.idx());
6181 ce = tabular.cellColumn(end.idx());
6185 rs = tabular.cellRow(beg.idx());
6186 re = tabular.cellRow(end.idx());
6192 Text * InsetTabular::getText(int idx) const
6194 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6198 void InsetTabular::setChange(Change const & change)
6200 for (idx_type idx = 0; idx < nargs(); ++idx)
6201 cell(idx)->setChange(change);
6205 void InsetTabular::acceptChanges()
6207 for (idx_type idx = 0; idx < nargs(); ++idx)
6208 cell(idx)->acceptChanges();
6212 void InsetTabular::rejectChanges()
6214 for (idx_type idx = 0; idx < nargs(); ++idx)
6215 cell(idx)->rejectChanges();
6219 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6221 return tabular.getPWidth(cell).zero();
6225 bool InsetTabular::forcePlainLayout(idx_type cell) const
6227 return !tabular.getPWidth(cell).zero();
6231 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6234 if (buf.length() <= 0)
6239 col_type maxCols = 1;
6240 size_t const len = buf.length();
6244 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6252 maxCols = max(cols, maxCols);
6258 maxCols = max(cols, maxCols);
6264 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6265 loctab = paste_tabular.get();
6267 dirtyTabularStack(true);
6270 cell = bv.cursor().idx();
6271 ocol = tabular.cellColumn(cell);
6272 row = tabular.cellRow(cell);
6276 idx_type const cells = loctab->numberofcells;
6279 rows = loctab->nrows();
6280 col_type const columns = loctab->ncols();
6282 while (cell < cells && p < len && row < rows &&
6283 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6289 // we can only set this if we are not too far right
6290 if (cols < columns) {
6291 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6292 Font const font = bv.textMetrics(&inset->text()).
6294 inset->setText(buf.substr(op, p - op), font,
6295 buffer().params().track_changes);
6301 // we can only set this if we are not too far right
6302 if (cols < columns) {
6303 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6304 Font const font = bv.textMetrics(&inset->text()).
6306 inset->setText(buf.substr(op, p - op), font,
6307 buffer().params().track_changes);
6312 cell = loctab->cellIndex(row, cols);
6318 // check for the last cell if there is no trailing '\n'
6319 if (cell < cells && op < len) {
6320 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6321 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6322 inset->setText(buf.substr(op, len - op), font,
6323 buffer().params().track_changes);
6329 void InsetTabular::addPreview(DocIterator const & inset_pos,
6330 PreviewLoader & loader) const
6332 DocIterator cell_pos = inset_pos;
6334 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6335 for (row_type r = 0; r < tabular.nrows(); ++r) {
6336 for (col_type c = 0; c < tabular.ncols(); ++c) {
6337 cell_pos.top().idx() = tabular.cellIndex(r, c);
6338 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6344 bool InsetTabular::completionSupported(Cursor const & cur) const
6346 Cursor const & bvCur = cur.bv().cursor();
6347 if (&bvCur.inset() != this)
6349 return cur.text()->completionSupported(cur);
6353 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6355 return completionSupported(cur);
6359 bool InsetTabular::automaticInlineCompletion() const
6361 return lyxrc.completion_inline_text;
6365 bool InsetTabular::automaticPopupCompletion() const
6367 return lyxrc.completion_popup_text;
6371 bool InsetTabular::showCompletionCursor() const
6373 return lyxrc.completion_cursor_text;
6377 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6379 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6383 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6385 if (!completionSupported(cur))
6387 return cur.text()->completionPrefix(cur);
6391 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6393 if (!completionSupported(cur))
6396 return cur.text()->insertCompletion(cur, s, finished);
6400 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6401 Dimension & dim) const
6403 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6404 tm.completionPosAndDim(cur, x, y, dim);
6408 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6410 istringstream data(in);
6412 lex.setStream(data);
6419 if (!lex || token != "tabular") {
6420 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6424 // This is part of the inset proper that is usually swallowed
6425 // by Buffer::readInset
6427 if (!lex || token != "Tabular") {
6428 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6436 string InsetTabular::params2string(InsetTabular const & inset)
6439 data << "tabular" << ' ';
6441 data << "\\end_inset\n";