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 // FIXME: these values have been hardcoded in InsetMathGrid and other
115 { Tabular::APPEND_ROW, "append-row", false },
116 { Tabular::APPEND_COLUMN, "append-column", false },
117 { Tabular::DELETE_ROW, "delete-row", false },
118 { Tabular::DELETE_COLUMN, "delete-column", false },
119 { Tabular::COPY_ROW, "copy-row", false },
120 { Tabular::COPY_COLUMN, "copy-column", false },
121 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
122 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
123 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
124 { Tabular::MOVE_ROW_UP, "move-row-up", false },
125 { Tabular::SET_LINE_TOP, "set-line-top", true },
126 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
127 { Tabular::SET_LINE_LEFT, "set-line-left", true },
128 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
129 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
130 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
131 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
132 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
133 { Tabular::ALIGN_LEFT, "align-left", false },
134 { Tabular::ALIGN_RIGHT, "align-right", false },
135 { Tabular::ALIGN_CENTER, "align-center", false },
136 { Tabular::ALIGN_BLOCK, "align-block", false },
137 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
138 { Tabular::VALIGN_TOP, "valign-top", false },
139 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
140 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
141 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
142 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
143 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
144 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
145 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
146 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
147 { Tabular::MULTICOLUMN, "multicolumn", false },
148 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
149 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
150 { Tabular::MULTIROW, "multirow", false },
151 { Tabular::SET_MULTIROW, "set-multirow", false },
152 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
153 { Tabular::SET_MROFFSET, "set-mroffset", true },
154 { Tabular::SET_ALL_LINES, "set-all-lines", false },
155 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
156 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
157 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
158 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
159 { Tabular::SET_PWIDTH, "set-pwidth", true },
160 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
161 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
162 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
163 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
164 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
165 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
166 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
167 { Tabular::SET_USEBOX, "set-usebox", true },
168 { Tabular::SET_LTHEAD, "set-lthead", true },
169 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
170 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
171 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
172 { Tabular::SET_LTFOOT, "set-ltfoot", true },
173 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
174 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
175 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
176 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
177 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
178 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
179 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
180 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
181 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
182 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
183 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
184 { Tabular::SET_BOOKTABS, "set-booktabs", false },
185 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
186 { Tabular::SET_TOP_SPACE, "set-top-space", true },
187 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
188 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
189 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
190 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
191 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
192 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
193 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
194 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
195 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
196 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
197 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
198 { Tabular::LAST_ACTION, "", false }
202 string const tostr(LyXAlignment const & num)
207 case LYX_ALIGN_BLOCK:
211 case LYX_ALIGN_CENTER:
213 case LYX_ALIGN_RIGHT:
215 case LYX_ALIGN_LAYOUT:
217 case LYX_ALIGN_SPECIAL:
219 case LYX_ALIGN_DECIMAL:
226 string const tostr(Tabular::HAlignment const & num)
229 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
231 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
233 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
240 string const tostr(Tabular::VAlignment const & num)
243 case Tabular::LYX_VALIGN_TOP:
245 case Tabular::LYX_VALIGN_MIDDLE:
247 case Tabular::LYX_VALIGN_BOTTOM:
254 string const tostr(Tabular::BoxType const & num)
257 case Tabular::BOX_NONE:
259 case Tabular::BOX_PARBOX:
261 case Tabular::BOX_MINIPAGE:
268 // I would have liked a fromstr template a lot better. (Lgb)
269 bool string2type(string const & str, LyXAlignment & num)
272 num = LYX_ALIGN_NONE;
273 else if (str == "block")
274 num = LYX_ALIGN_BLOCK;
275 else if (str == "left")
276 num = LYX_ALIGN_LEFT;
277 else if (str == "center")
278 num = LYX_ALIGN_CENTER;
279 else if (str == "right")
280 num = LYX_ALIGN_RIGHT;
281 else if (str == "decimal")
282 num = LYX_ALIGN_DECIMAL;
289 bool string2type(string const & str, Tabular::HAlignment & num)
292 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
293 else if (str == "center" )
294 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
295 else if (str == "right")
296 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
303 bool string2type(string const & str, Tabular::VAlignment & num)
306 num = Tabular::LYX_VALIGN_TOP;
307 else if (str == "middle" )
308 num = Tabular::LYX_VALIGN_MIDDLE;
309 else if (str == "bottom")
310 num = Tabular::LYX_VALIGN_BOTTOM;
317 bool string2type(string const & str, Tabular::BoxType & num)
320 num = Tabular::BOX_NONE;
321 else if (str == "parbox")
322 num = Tabular::BOX_PARBOX;
323 else if (str == "minipage")
324 num = Tabular::BOX_MINIPAGE;
331 bool string2type(string const & str, bool & num)
335 else if (str == "false")
343 bool getTokenValue(string const & str, char const * token, string & ret)
346 size_t token_length = strlen(token);
347 size_t pos = str.find(token);
349 if (pos == string::npos || pos + token_length + 1 >= str.length()
350 || str[pos + token_length] != '=')
352 pos += token_length + 1;
354 if (ch != '"' && ch != '\'') { // only read till next space
358 while (pos < str.length() - 1 && str[++pos] != ch)
365 bool getTokenValue(string const & str, char const * token, docstring & ret)
368 bool const success = getTokenValue(str, token, tmp);
369 ret = from_utf8(tmp);
374 bool getTokenValue(string const & str, char const * token, int & num)
378 if (!getTokenValue(str, token, tmp))
380 num = convert<int>(tmp);
385 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
388 return getTokenValue(str, token, tmp) && string2type(tmp, num);
392 bool getTokenValue(string const & str, char const * token,
393 Tabular::HAlignment & num)
396 return getTokenValue(str, token, tmp) && string2type(tmp, num);
400 bool getTokenValue(string const & str, char const * token,
401 Tabular::VAlignment & num)
404 return getTokenValue(str, token, tmp) && string2type(tmp, num);
408 bool getTokenValue(string const & str, char const * token,
409 Tabular::BoxType & num)
412 return getTokenValue(str, token, tmp) && string2type(tmp, num);
416 bool getTokenValue(string const & str, char const * token, bool & flag)
418 // set the flag always to false as this should be the default for bools
419 // not in the file-format.
422 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
426 bool getTokenValue(string const & str, char const * token, Length & len)
428 // set the length to be zero() as default as this it should be if not
429 // in the file format.
432 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
436 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
441 if (!getTokenValue(str, token, tmp))
443 if (tmp == "default") {
447 return isValidLength(tmp, &len);
451 void l_getline(istream & is, string & str)
454 while (str.empty()) {
456 if (!str.empty() && str[str.length() - 1] == '\r')
457 str.erase(str.length() - 1);
462 string const write_attribute(string const & name, T const & t)
464 string const s = tostr(t);
465 return s.empty() ? s : " " + name + "=\"" + s + "\"";
469 string const write_attribute(string const & name, string const & t)
471 return t.empty() ? t : " " + name + "=\"" + t + "\"";
476 string const write_attribute(string const & name, docstring const & t)
478 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
483 string const write_attribute(string const & name, bool const & b)
485 // we write only true attribute values so we remove a bit of the
486 // file format bloat for tabulars.
487 return b ? write_attribute(name, convert<string>(b)) : string();
492 string const write_attribute(string const & name, int const & i)
494 // we write only true attribute values so we remove a bit of the
495 // file format bloat for tabulars.
496 return i ? write_attribute(name, convert<string>(i)) : string();
501 string const write_attribute(string const & name, Tabular::idx_type const & i)
503 // we write only true attribute values so we remove a bit of the
504 // file format bloat for tabulars.
505 return i ? write_attribute(name, convert<string>(i)) : string();
510 string const write_attribute(string const & name, Length const & value)
512 // we write only the value if we really have one same reson as above.
513 return value.zero() ? string() : write_attribute(name, value.asString());
519 string const featureAsString(Tabular::Feature action)
521 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
522 if (tabularFeature[i].action == action)
523 return tabularFeature[i].feature;
529 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
531 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
532 for (; dit; dit.forwardChar())
533 if (dit.inTexted() && dit.depth() == 1
534 && dit.paragraph().find(align_d, false, false, dit.pos()))
541 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
543 InsetTableCell tail = InsetTableCell(head);
544 DocIterator const dit = separatorPos(&head, align_d);
547 pit_type const psize = head.paragraphs().front().size();
548 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
549 tail.paragraphs().front().eraseChars(0,
550 dit.pos() < psize ? dit.pos() + 1 : psize, false);
557 /////////////////////////////////////////////////////////////////////
561 /////////////////////////////////////////////////////////////////////
564 Tabular::CellData::CellData(Buffer * buf)
567 multicolumn(Tabular::CELL_NORMAL),
568 multirow(Tabular::CELL_NORMAL),
569 alignment(LYX_ALIGN_CENTER),
570 valignment(LYX_VALIGN_TOP),
580 inset(new InsetTableCell(buf))
582 inset->setBuffer(*buf);
586 Tabular::CellData::CellData(CellData const & cs)
589 multicolumn(cs.multicolumn),
590 multirow(cs.multirow),
591 mroffset(cs.mroffset),
592 alignment(cs.alignment),
593 valignment(cs.valignment),
594 decimal_hoffset(cs.decimal_hoffset),
595 decimal_width(cs.decimal_width),
597 top_line(cs.top_line),
598 bottom_line(cs.bottom_line),
599 left_line(cs.left_line),
600 right_line(cs.right_line),
603 align_special(cs.align_special),
605 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
609 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
615 multicolumn = cs.multicolumn;
616 multirow = cs.multirow;
617 mroffset = cs.mroffset;
618 alignment = cs.alignment;
619 valignment = cs.valignment;
620 decimal_hoffset = cs.decimal_hoffset;
621 decimal_width = cs.decimal_width;
622 voffset = cs.voffset;
623 top_line = cs.top_line;
624 bottom_line = cs.bottom_line;
625 left_line = cs.left_line;
626 right_line = cs.right_line;
629 align_special = cs.align_special;
630 p_width = cs.p_width;
631 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
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()
659 : set(false), topDL(false),
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->toggleFixedWidth(fixedWidth);
1134 // merge all paragraphs to one
1135 BufferParams const & bp = cur.bv().buffer().params();
1136 while (inset->paragraphs().size() > 1)
1137 mergeParagraph(bp, inset->paragraphs(), 0);
1141 // undo information has already been recorded
1142 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1143 bp.documentClass().plainLayoutName());
1150 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1151 Length const & width)
1153 col_type const c = cellColumn(cell);
1155 column_info[c].p_width = width;
1156 // reset the vertical alignment to top if the fixed width
1157 // is removed or zero because only fixed width columns can
1158 // have a vertical alignment
1159 if (column_info[c].p_width.zero())
1160 column_info[c].valignment = LYX_VALIGN_TOP;
1161 for (row_type r = 0; r < nrows(); ++r) {
1162 idx_type const cell = cellIndex(r, c);
1163 // because of multicolumns
1164 toggleFixedWidth(cur, cellInset(cell).get(),
1165 !getPWidth(cell).zero());
1166 if (isMultiRow(cell))
1167 setAlignment(cell, LYX_ALIGN_LEFT, false);
1169 // cur paragraph can become invalid after paragraphs were merged
1170 if (cur.pit() > cur.lastpit())
1171 cur.pit() = cur.lastpit();
1172 // cur position can become invalid after newlines were removed
1173 if (cur.pos() > cur.lastpos())
1174 cur.pos() = cur.lastpos();
1178 bool Tabular::setFixedWidth(row_type r, col_type c)
1180 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1181 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1182 || (multicol && !cell_info[r][c].p_width.zero());
1183 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1188 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1189 Length const & width)
1191 if (!isMultiColumn(cell))
1194 cellInfo(cell).p_width = width;
1195 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1196 // cur paragraph can become invalid after paragraphs were merged
1197 if (cur.pit() > cur.lastpit())
1198 cur.pit() = cur.lastpit();
1199 // cur position can become invalid after newlines were removed
1200 if (cur.pos() > cur.lastpos())
1201 cur.pos() = cur.lastpos();
1206 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1208 cellInfo(cell).mroffset = mroffset;
1213 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1214 Tabular::Feature what)
1216 if (what == SET_SPECIAL_MULTICOLUMN)
1217 cellInfo(cell).align_special = special;
1219 column_info[cellColumn(cell)].align_special = special;
1223 void Tabular::setTopLine(idx_type i, bool line)
1225 cellInfo(i).top_line = line;
1229 void Tabular::setBottomLine(idx_type i, bool line)
1231 cellInfo(i).bottom_line = line;
1235 void Tabular::setLeftLine(idx_type cell, bool line)
1237 cellInfo(cell).left_line = line;
1241 void Tabular::setRightLine(idx_type cell, bool line)
1243 cellInfo(cell).right_line = line;
1246 bool Tabular::rowTopLine(row_type r) const
1248 bool all_rows_set = true;
1249 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1250 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1251 return all_rows_set;
1255 bool Tabular::rowBottomLine(row_type r) const
1257 bool all_rows_set = true;
1258 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1259 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1260 return all_rows_set;
1264 bool Tabular::columnLeftLine(col_type c) const
1271 for (row_type r = 0; r < nrows(); ++r) {
1272 idx_type const i = cellIndex(r, c);
1273 if (c == cellColumn(i)) {
1275 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1276 if (cellInfo(i).left_line || right)
1280 return 2 * nrows_left >= total;
1284 bool Tabular::columnRightLine(col_type c) const
1289 int nrows_right = 0;
1291 for (row_type r = 0; r < nrows(); ++r) {
1292 idx_type i = cellIndex(r, c);
1293 if (c == cellColumn(i) + columnSpan(i) - 1) {
1295 bool left = (c + 1 < ncols()
1296 && cellInfo(cellIndex(r, c + 1)).left_line)
1297 || c + 1 == ncols();
1298 if (cellInfo(i).right_line && left)
1302 return 2 * nrows_right >= total;
1306 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1308 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1309 return cellInfo(cell).alignment;
1311 return column_info[cellColumn(cell)].alignment;
1316 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1318 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1319 return cellInfo(cell).valignment;
1320 return column_info[cellColumn(cell)].valignment;
1324 Length const Tabular::getPWidth(idx_type cell) const
1326 if (isMultiColumn(cell))
1327 return cellInfo(cell).p_width;
1328 return column_info[cellColumn(cell)].p_width;
1332 Length const Tabular::getMROffset(idx_type cell) const
1334 return cellInfo(cell).mroffset;
1338 int Tabular::textHOffset(idx_type cell) const
1340 // the LaTeX Way :-(
1341 int x = WIDTH_OF_LINE;
1343 int const w = cellWidth(cell) - cellInfo(cell).width;
1345 switch (getAlignment(cell)) {
1346 case LYX_ALIGN_CENTER:
1349 case LYX_ALIGN_RIGHT:
1352 case LYX_ALIGN_DECIMAL: {
1353 // we center when no decimal point
1354 if (cellInfo(cell).decimal_width == 0) {
1358 col_type const c = cellColumn(cell);
1359 int max_dhoffset = 0;
1360 for(row_type r = 0; r < row_info.size() ; ++r) {
1361 idx_type const i = cellIndex(r, c);
1362 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1363 && cellInfo(i).decimal_width != 0)
1364 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1366 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1369 // LYX_ALIGN_LEFT: nothing :-)
1377 int Tabular::textVOffset(idx_type cell) const
1379 int voffset = cellInfo(cell).voffset;
1380 if (isMultiRow(cell)) {
1381 row_type const row = cellRow(cell);
1382 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1388 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1391 idx_type const numcells = numberOfCellsInRow(row);
1392 // we check against numcells to make sure we do not crash if all the
1393 // cells are multirow (bug #7535), but in that case our return value
1394 // is really invalid, i.e., it is NOT the first cell in the row. but
1395 // i do not know what to do here. (rgh)
1396 while (c < numcells - 1
1397 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1399 return cell_info[row][c].cellno;
1403 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1405 col_type c = ncols() - 1;
1406 // of course we check against 0 so we don't crash. but we have the same
1407 // problem as in the previous routine: if all the cells are part of a
1408 // multirow or part of a multi column, then our return value is invalid.
1410 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1411 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1413 return cell_info[row][c].cellno;
1417 Tabular::row_type Tabular::cellRow(idx_type cell) const
1419 if (cell >= numberofcells)
1423 return rowofcell[cell];
1427 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1429 if (cell >= numberofcells)
1433 return columnofcell[cell];
1437 void Tabular::write(ostream & os) const
1441 << write_attribute("version", 3)
1442 << write_attribute("rows", nrows())
1443 << write_attribute("columns", ncols())
1445 // global longtable options
1447 << write_attribute("rotate", rotate)
1448 << write_attribute("booktabs", use_booktabs)
1449 << write_attribute("islongtable", is_long_tabular)
1450 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1451 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1452 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1453 << write_attribute("headTopDL", endhead.topDL)
1454 << write_attribute("headBottomDL", endhead.bottomDL)
1455 << write_attribute("footTopDL", endfoot.topDL)
1456 << write_attribute("footBottomDL", endfoot.bottomDL)
1457 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1458 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1459 << write_attribute("lastFootEmpty", endlastfoot.empty);
1460 // longtables cannot be aligned vertically
1461 if (!is_long_tabular) {
1462 os << write_attribute("tabularvalignment", tabular_valignment);
1463 os << write_attribute("tabularwidth", tabular_width);
1465 if (is_long_tabular)
1466 os << write_attribute("longtabularalignment", longtabular_alignment);
1468 for (col_type c = 0; c < ncols(); ++c) {
1470 << write_attribute("alignment", column_info[c].alignment);
1471 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1472 os << write_attribute("decimal_point", column_info[c].decimal_point);
1473 os << write_attribute("valignment", column_info[c].valignment)
1474 << write_attribute("width", column_info[c].p_width.asString())
1475 << write_attribute("special", column_info[c].align_special)
1478 for (row_type r = 0; r < nrows(); ++r) {
1479 static const string def("default");
1481 if (row_info[r].top_space_default)
1482 os << write_attribute("topspace", def);
1484 os << write_attribute("topspace", row_info[r].top_space);
1485 if (row_info[r].bottom_space_default)
1486 os << write_attribute("bottomspace", def);
1488 os << write_attribute("bottomspace", row_info[r].bottom_space);
1489 if (row_info[r].interline_space_default)
1490 os << write_attribute("interlinespace", def);
1492 os << write_attribute("interlinespace", row_info[r].interline_space);
1493 os << write_attribute("endhead", row_info[r].endhead)
1494 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1495 << write_attribute("endfoot", row_info[r].endfoot)
1496 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1497 << write_attribute("newpage", row_info[r].newpage)
1498 << write_attribute("caption", row_info[r].caption)
1500 for (col_type c = 0; c < ncols(); ++c) {
1502 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1503 << write_attribute("multirow", cell_info[r][c].multirow)
1504 << write_attribute("mroffset", cell_info[r][c].mroffset)
1505 << write_attribute("alignment", cell_info[r][c].alignment)
1506 << write_attribute("valignment", cell_info[r][c].valignment)
1507 << write_attribute("topline", cell_info[r][c].top_line)
1508 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1509 << write_attribute("leftline", cell_info[r][c].left_line)
1510 << write_attribute("rightline", cell_info[r][c].right_line)
1511 << write_attribute("rotate", cell_info[r][c].rotate)
1512 << write_attribute("usebox", cell_info[r][c].usebox)
1513 << write_attribute("width", cell_info[r][c].p_width)
1514 << write_attribute("special", cell_info[r][c].align_special)
1516 os << "\\begin_inset ";
1517 cell_info[r][c].inset->write(os);
1518 os << "\n\\end_inset\n"
1520 // FIXME This can be removed again once the mystery
1521 // crash has been resolved.
1526 os << "</lyxtabular>\n";
1530 void Tabular::read(Lexer & lex)
1533 istream & is = lex.getStream();
1535 l_getline(is, line);
1536 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1537 LASSERT(false, return);
1541 if (!getTokenValue(line, "version", version))
1543 LATTEST(version >= 2);
1546 if (!getTokenValue(line, "rows", rows_arg))
1549 if (!getTokenValue(line, "columns", columns_arg))
1551 init(buffer_, rows_arg, columns_arg);
1552 l_getline(is, line);
1553 if (!prefixIs(line, "<features")) {
1554 lyxerr << "Wrong tabular format (expected <features ...> got"
1555 << line << ')' << endl;
1558 getTokenValue(line, "rotate", rotate);
1559 getTokenValue(line, "booktabs", use_booktabs);
1560 getTokenValue(line, "islongtable", is_long_tabular);
1561 getTokenValue(line, "tabularvalignment", tabular_valignment);
1562 getTokenValue(line, "tabularwidth", tabular_width);
1563 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1564 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1565 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1566 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1567 getTokenValue(line, "headTopDL", endhead.topDL);
1568 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1569 getTokenValue(line, "footTopDL", endfoot.topDL);
1570 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1571 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1572 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1573 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1575 for (col_type c = 0; c < ncols(); ++c) {
1577 if (!prefixIs(line,"<column")) {
1578 lyxerr << "Wrong tabular format (expected <column ...> got"
1579 << line << ')' << endl;
1582 getTokenValue(line, "alignment", column_info[c].alignment);
1583 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1584 getTokenValue(line, "valignment", column_info[c].valignment);
1585 getTokenValue(line, "width", column_info[c].p_width);
1586 getTokenValue(line, "special", column_info[c].align_special);
1589 for (row_type i = 0; i < nrows(); ++i) {
1590 l_getline(is, line);
1591 if (!prefixIs(line, "<row")) {
1592 lyxerr << "Wrong tabular format (expected <row ...> got"
1593 << line << ')' << endl;
1596 getTokenValue(line, "topspace", row_info[i].top_space,
1597 row_info[i].top_space_default);
1598 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1599 row_info[i].bottom_space_default);
1600 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1601 row_info[i].interline_space_default);
1602 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1603 getTokenValue(line, "endhead", row_info[i].endhead);
1604 getTokenValue(line, "endfoot", row_info[i].endfoot);
1605 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1606 getTokenValue(line, "newpage", row_info[i].newpage);
1607 getTokenValue(line, "caption", row_info[i].caption);
1608 for (col_type j = 0; j < ncols(); ++j) {
1609 l_getline(is, line);
1610 if (!prefixIs(line, "<cell")) {
1611 lyxerr << "Wrong tabular format (expected <cell ...> got"
1612 << line << ')' << endl;
1615 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1616 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1617 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1618 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1619 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1620 getTokenValue(line, "topline", cell_info[i][j].top_line);
1621 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1622 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1623 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1624 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1625 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1626 getTokenValue(line, "width", cell_info[i][j].p_width);
1628 getTokenValue(line, "special", cell_info[i][j].align_special);
1629 l_getline(is, line);
1630 if (prefixIs(line, "\\begin_inset")) {
1631 cell_info[i][j].inset->setBuffer(*buffer_);
1632 cell_info[i][j].inset->read(lex);
1633 l_getline(is, line);
1635 if (!prefixIs(line, "</cell>")) {
1636 lyxerr << "Wrong tabular format (expected </cell> got"
1637 << line << ')' << endl;
1641 l_getline(is, line);
1642 if (!prefixIs(line, "</row>")) {
1643 lyxerr << "Wrong tabular format (expected </row> got"
1644 << line << ')' << endl;
1648 while (!prefixIs(line, "</lyxtabular>")) {
1649 l_getline(is, line);
1655 bool Tabular::isMultiColumn(idx_type cell) const
1657 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1658 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1662 bool Tabular::hasMultiColumn(col_type c) const
1664 for (row_type r = 0; r < nrows(); ++r) {
1665 if (isMultiColumn(cellIndex(r, c)))
1672 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1674 return cell_info[cellRow(cell)][cellColumn(cell)];
1678 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1679 bool const right_border)
1681 idx_type const col = cellColumn(cell);
1682 idx_type const row = cellRow(cell);
1683 for (idx_type i = 0; i < number; ++i)
1684 unsetMultiRow(cellIndex(row, col + i));
1686 // unsetting of multirow may have invalidated cell index
1687 cell = cellIndex(row, col);
1688 CellData & cs = cellInfo(cell);
1689 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1690 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1691 cs.alignment = column_info[col].alignment;
1692 setRightLine(cell, right_border);
1694 idx_type lastcell = cellIndex(row, col + number - 1);
1695 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1696 CellData & cs1 = cellInfo(cell + i);
1697 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1698 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1706 bool Tabular::isMultiRow(idx_type cell) const
1708 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1709 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1712 bool Tabular::hasMultiRow(row_type r) const
1714 for (col_type c = 0; c < ncols(); ++c) {
1715 if (isMultiRow(cellIndex(r, c)))
1721 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1722 bool const bottom_border,
1723 LyXAlignment const halign)
1725 idx_type const col = cellColumn(cell);
1726 idx_type const row = cellRow(cell);
1727 for (idx_type i = 0; i < number; ++i)
1728 unsetMultiColumn(cellIndex(row + i, col));
1730 // unsetting of multirow may have invalidated cell index
1731 cell = cellIndex(row, col);
1732 CellData & cs = cellInfo(cell);
1733 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1734 cs.valignment = LYX_VALIGN_MIDDLE;
1735 // the horizontal alignment of multirow cells can only
1736 // be changed for the whole table row,
1737 // support changing this only for the multirow cell can be done via
1739 if (getPWidth(cell).zero())
1740 cs.alignment = halign;
1742 cs.alignment = LYX_ALIGN_LEFT;
1744 // set the bottom line of the last selected cell
1745 setBottomLine(cell, bottom_border);
1747 for (idx_type i = 1; i < number; ++i) {
1748 CellData & cs1 = cell_info[row + i][col];
1749 cs1.multirow = CELL_PART_OF_MULTIROW;
1750 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1758 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1760 row_type const row = cellRow(cell);
1761 col_type const col = cellColumn(cell);
1763 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1770 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1772 col_type const column = cellColumn(cell);
1773 col_type row = cellRow(cell) + 1;
1774 while (row < nrows() && isPartOfMultiRow(row, column))
1777 return row - cellRow(cell);
1781 void Tabular::unsetMultiColumn(idx_type cell)
1783 if (!isMultiColumn(cell))
1786 row_type const row = cellRow(cell);
1787 col_type const col = cellColumn(cell);
1788 row_type const span = columnSpan(cell);
1789 for (col_type c = 0; c < span; ++c) {
1790 // in the table dialog the lines are set in every case
1791 // when unsetting a multicolumn this leads to an additional right
1792 // line for every cell that was part of the former multicolumn cell,
1793 // except if the cell is in the last column
1794 // therefore remove this line
1795 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1796 && (col + c) < (col + span - 1))
1797 cell_info[row][col + c].right_line = false;
1798 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1804 void Tabular::unsetMultiRow(idx_type cell)
1806 if (!isMultiRow(cell))
1809 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1810 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1811 row_type const row = cellRow(cell);
1812 col_type const col = cellColumn(cell);
1813 row_type const span = rowSpan(cell);
1814 for (row_type r = 0; r < span; ++r)
1815 cell_info[row + r][col].multirow = CELL_NORMAL;
1820 void Tabular::setRotateCell(idx_type cell, int value)
1822 cellInfo(cell).rotate = value;
1826 int Tabular::getRotateCell(idx_type cell) const
1828 return cellInfo(cell).rotate;
1832 bool Tabular::needRotating() const
1836 for (row_type r = 0; r < nrows(); ++r)
1837 for (col_type c = 0; c < ncols(); ++c)
1838 if (cell_info[r][c].rotate != 0)
1844 bool Tabular::isLastCell(idx_type cell) const
1846 if (cell + 1 < numberofcells)
1852 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1854 if (cellRow(cell) == 0)
1857 col_type const col = cellColumn(cell);
1858 row_type r = cellRow(cell) - 1;
1859 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1862 return cell_info[r][col].cellno;
1866 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1868 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1869 if (nextrow < nrows())
1870 return cell_info[nextrow][cellColumn(cell)].cellno;
1875 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1877 LASSERT(column != npos && column < ncols(), column = 0);
1878 LASSERT(row != npos && row < nrows(), row = 0);
1879 return cell_info[row][column].cellno;
1883 void Tabular::setUsebox(idx_type cell, BoxType type)
1885 cellInfo(cell).usebox = type;
1889 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1890 // adds line breaks, see bug 4886.
1891 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1893 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1894 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1896 if (cellInfo(cell).usebox > 1)
1897 return cellInfo(cell).usebox;
1898 return useParbox(cell);
1903 // This are functions used for the longtable support
1905 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1911 row_info[row].endfirsthead = flag;
1915 row_info[row].endhead = flag;
1920 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1923 hd.set = haveLTHead();
1924 return row_info[row].endhead;
1928 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1931 hd.set = haveLTFirstHead();
1932 return row_info[row].endfirsthead;
1936 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1942 row_info[row].endlastfoot = flag;
1946 row_info[row].endfoot = flag;
1951 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1954 fd.set = haveLTFoot();
1955 return row_info[row].endfoot;
1959 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1962 fd.set = haveLTLastFoot();
1963 return row_info[row].endlastfoot;
1967 void Tabular::setLTNewPage(row_type row, bool what)
1969 row_info[row].newpage = what;
1973 bool Tabular::getLTNewPage(row_type row) const
1975 return row_info[row].newpage;
1979 bool Tabular::haveLTHead(bool withcaptions) const
1981 if (!is_long_tabular)
1983 for (row_type i = 0; i < nrows(); ++i)
1984 if (row_info[i].endhead &&
1985 (withcaptions || !row_info[i].caption))
1991 bool Tabular::haveLTFirstHead(bool withcaptions) const
1993 if (!is_long_tabular || endfirsthead.empty)
1995 for (row_type r = 0; r < nrows(); ++r)
1996 if (row_info[r].endfirsthead &&
1997 (withcaptions || !row_info[r].caption))
2003 bool Tabular::haveLTFoot(bool withcaptions) const
2005 if (!is_long_tabular)
2007 for (row_type r = 0; r < nrows(); ++r)
2008 if (row_info[r].endfoot &&
2009 (withcaptions || !row_info[r].caption))
2015 bool Tabular::haveLTLastFoot(bool withcaptions) const
2017 if (!is_long_tabular || endlastfoot.empty)
2019 for (row_type r = 0; r < nrows(); ++r)
2020 if (row_info[r].endlastfoot &&
2021 (withcaptions || !row_info[r].caption))
2027 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2029 idx_type i = getFirstCellInRow(row);
2031 setMultiColumn(i, numberOfCellsInRow(row), false);
2032 setTopLine(i, false);
2033 setBottomLine(i, false);
2034 setLeftLine(i, false);
2035 setRightLine(i, false);
2036 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2037 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2038 setLTHead(row, true, endfirsthead, true);
2039 row_info[row].endfirsthead = true;
2042 unsetMultiColumn(i);
2043 // When unsetting a caption row, also all existing
2044 // captions in this row must be dissolved.
2046 row_info[row].caption = what;
2051 bool Tabular::ltCaption(row_type row) const
2053 return row_info[row].caption;
2057 bool Tabular::haveLTCaption(CaptionType captiontype) const
2059 if (!is_long_tabular)
2061 for (row_type r = 0; r < nrows(); ++r) {
2062 if (row_info[r].caption) {
2063 switch (captiontype) {
2064 case CAPTION_FIRSTHEAD:
2065 if (row_info[r].endfirsthead)
2069 if (row_info[r].endhead)
2073 if (row_info[r].endfoot)
2076 case CAPTION_LASTFOOT:
2077 if (row_info[r].endlastfoot)
2089 // end longtable support functions
2091 void Tabular::setRowAscent(row_type row, int height)
2093 if (row >= nrows() || row_info[row].ascent == height)
2095 row_info[row].ascent = height;
2099 void Tabular::setRowDescent(row_type row, int height)
2101 if (row >= nrows() || row_info[row].descent == height)
2103 row_info[row].descent = height;
2107 int Tabular::rowAscent(row_type row) const
2109 LASSERT(row < nrows(), row = 0);
2110 return row_info[row].ascent;
2114 int Tabular::rowDescent(row_type row) const
2116 LASSERT(row < nrows(), row = 0);
2117 return row_info[row].descent;
2121 int Tabular::height() const
2124 for (row_type row = 0; row < nrows(); ++row)
2125 height += rowAscent(row) + rowDescent(row) +
2131 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2133 LASSERT(row < nrows(), return false);
2134 LASSERT(column < ncols(), return false);
2135 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2139 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2141 LASSERT(row < nrows(), return false);
2142 LASSERT(column < ncols(), return false);
2143 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2147 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2149 // we only output complete row lines and the 1st row here, the rest
2150 // is done in Tabular::TeXBottomHLine(...)
2152 // get for each column the topline (if any)
2153 vector<bool> topline;
2155 for (col_type c = 0; c < ncols(); ++c) {
2156 topline.push_back(topLine(cellIndex(row, c)));
2157 // If cell is part of a multirow and not the first cell of the
2158 // multirow, no line must be drawn.
2160 if (isMultiRow(cellIndex(row, c))
2161 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2167 // do nothing if empty first row, or incomplete row line after
2168 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2171 // only output complete row lines and the 1st row's clines
2172 if (nset == ncols()) {
2174 os << (row == 0 ? "\\toprule " : "\\midrule ");
2178 } else if (row == 0) {
2179 for (col_type c = 0; c < ncols(); ++c) {
2181 col_type offset = 0;
2182 for (col_type j = 0 ; j < c; ++j)
2183 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2186 //babel makes the "-" character an active one, so we have to suppress this here
2187 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2188 if (lang == "slovak" || lang == "czech")
2189 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2190 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2192 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2194 col_type cstart = c;
2195 for ( ; c < ncols() && topline[c]; ++c) {}
2197 for (col_type j = cstart ; j < c ; ++j)
2198 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2201 os << c + offset << "} ";
2209 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2211 // we output bottomlines of row r and the toplines of row r+1
2212 // if the latter do not span the whole tabular
2214 // get the bottomlines of row r, and toplines in next row
2215 bool lastrow = row == nrows() - 1;
2216 vector<bool> bottomline, topline;
2217 bool nextrowset = true;
2218 for (col_type c = 0; c < ncols(); ++c) {
2219 bottomline.push_back(bottomLine(cellIndex(row, c)));
2220 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2221 // If cell is part of a multirow and not the last cell of the
2222 // multirow, no line must be drawn.
2224 if (isMultiRow(cellIndex(row, c))
2225 && isMultiRow(cellIndex(row + 1, c))
2226 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2227 bottomline[c] = false;
2230 nextrowset &= topline[c];
2233 // combine this row's bottom lines and next row's toplines if necessary
2235 for (col_type c = 0; c < ncols(); ++c) {
2237 bottomline[c] = bottomline[c] || topline[c];
2242 // do nothing if empty, OR incomplete row line with a topline in next row
2243 if (nset == 0 || (nextrowset && nset != ncols()))
2246 if (nset == ncols()) {
2248 os << (lastrow ? "\\bottomrule" : "\\midrule");
2252 for (col_type c = 0; c < ncols(); ++c) {
2253 if (bottomline[c]) {
2254 col_type offset = 0;
2255 for (col_type j = 0 ; j < c; ++j)
2256 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2259 //babel makes the "-" character an active one, so we have to suppress this here
2260 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2261 if (lang == "slovak" || lang == "czech")
2262 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2263 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2265 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2267 col_type cstart = c;
2268 for ( ; c < ncols() && bottomline[c]; ++c) {}
2270 for (col_type j = cstart ; j < c ; ++j)
2271 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2274 os << c + offset << "} ";
2282 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2283 bool & ismulticol, bool & ismultirow) const
2285 row_type const r = cellRow(cell);
2286 if (is_long_tabular && row_info[r].caption)
2289 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2290 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2291 // figure out how to set the lines
2292 // we always set double lines to the right of the cell
2293 col_type const c = cellColumn(cell);
2294 col_type const nextcol = c + columnSpan(cell);
2295 bool colright = columnRightLine(c);
2296 bool colleft = columnLeftLine(c);
2297 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2298 bool nextcellleft = nextcol < ncols()
2299 && leftLine(cellIndex(r, nextcol));
2300 bool coldouble = colright && nextcolleft;
2301 bool celldouble = rightLine(cell) && nextcellleft;
2303 ismulticol = isMultiColumn(cell)
2304 || (c == 0 && colleft != leftLine(cell))
2305 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2306 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2307 || (coldouble != celldouble);
2309 // we center in multicol when no decimal point
2310 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2311 docstring const align_d = column_info[c].decimal_point;
2312 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2316 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2317 int latexcolspan = columnSpan(cell);
2318 for(col_type col = c; col < c + columnSpan(cell); ++col)
2319 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2323 os << "\\multicolumn{" << latexcolspan << "}{";
2324 if (c ==0 && leftLine(cell))
2326 if (!cellInfo(cell).align_special.empty()) {
2327 os << cellInfo(cell).align_special;
2329 if (!getPWidth(cell).zero()) {
2331 case LYX_ALIGN_LEFT:
2332 os << ">{\\raggedright}";
2334 case LYX_ALIGN_RIGHT:
2335 os << ">{\\raggedleft}";
2337 case LYX_ALIGN_CENTER:
2338 os << ">{\\centering}";
2344 case LYX_VALIGN_TOP:
2347 case LYX_VALIGN_MIDDLE:
2350 case LYX_VALIGN_BOTTOM:
2355 << from_ascii(getPWidth(cell).asLatexString())
2359 case LYX_ALIGN_LEFT:
2362 case LYX_ALIGN_RIGHT:
2369 } // end if else !getPWidth
2370 } // end if else !cellinfo_of_cell
2371 if (rightLine(cell) || nextcellleft)
2374 // add extra vertical line if we want a double one
2377 } // end if ismulticol
2379 // we only need code for the first multirow cell
2380 ismultirow = isMultiRow(cell);
2382 os << "\\multirow{" << rowSpan(cell) << "}{";
2383 if (!getPWidth(cell).zero())
2384 os << from_ascii(getPWidth(cell).asLatexString());
2386 // we need to set a default value
2389 if (!getMROffset(cell).zero())
2390 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2392 } // end if ismultirow
2394 if (getRotateCell(cell) != 0)
2395 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2397 if (getUsebox(cell) == BOX_PARBOX) {
2400 case LYX_VALIGN_TOP:
2403 case LYX_VALIGN_MIDDLE:
2406 case LYX_VALIGN_BOTTOM:
2410 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2412 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2413 os << "\\begin{minipage}[";
2415 case LYX_VALIGN_TOP:
2418 case LYX_VALIGN_MIDDLE:
2421 case LYX_VALIGN_BOTTOM:
2425 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2431 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2432 bool ismulticol, bool ismultirow) const
2434 row_type const r = cellRow(cell);
2435 if (is_long_tabular && row_info[r].caption)
2439 if (getUsebox(cell) == BOX_PARBOX)
2441 else if (getUsebox(cell) == BOX_MINIPAGE)
2442 os << breakln << "\\end{minipage}";
2443 if (getRotateCell(cell) != 0)
2444 os << breakln << "\\end{turn}";
2452 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2453 OutputParams const & runparams) const
2455 if (!is_long_tabular)
2459 // output caption which is in no header or footer
2460 if (haveLTCaption()) {
2461 for (row_type r = 0; r < nrows(); ++r) {
2462 if (row_info[r].caption &&
2463 !row_info[r].endfirsthead && !row_info[r].endhead &&
2464 !row_info[r].endfoot && !row_info[r].endlastfoot)
2465 TeXRow(os, r, runparams);
2468 // output first header info
2469 if (haveLTFirstHead()) {
2470 if (endfirsthead.topDL)
2472 for (row_type r = 0; r < nrows(); ++r) {
2473 if (row_info[r].endfirsthead)
2474 TeXRow(os, r, runparams);
2476 if (endfirsthead.bottomDL)
2478 os << "\\endfirsthead\n";
2480 // output header info
2482 if (endfirsthead.empty && !haveLTFirstHead())
2483 os << "\\endfirsthead\n";
2486 for (row_type r = 0; r < nrows(); ++r) {
2487 if (row_info[r].endhead)
2488 TeXRow(os, r, runparams);
2490 if (endhead.bottomDL)
2492 os << "\\endhead\n";
2494 // output footer info
2498 for (row_type r = 0; r < nrows(); ++r) {
2499 if (row_info[r].endfoot)
2500 TeXRow(os, r, runparams);
2502 if (endfoot.bottomDL)
2504 os << "\\endfoot\n";
2505 if (endlastfoot.empty && !haveLTLastFoot())
2506 os << "\\endlastfoot\n";
2508 // output lastfooter info
2509 if (haveLTLastFoot()) {
2510 if (endlastfoot.topDL)
2512 for (row_type r = 0; r < nrows(); ++r) {
2513 if (row_info[r].endlastfoot)
2514 TeXRow(os, r, runparams);
2516 if (endlastfoot.bottomDL)
2518 os << "\\endlastfoot\n";
2523 bool Tabular::isValidRow(row_type row) const
2525 if (!is_long_tabular)
2527 return !row_info[row].endhead && !row_info[row].endfirsthead
2528 && !row_info[row].endfoot && !row_info[row].endlastfoot
2529 && !row_info[row].caption;
2533 void Tabular::TeXRow(otexstream & os, row_type row,
2534 OutputParams const & runparams) const
2536 idx_type cell = cellIndex(row, 0);
2537 shared_ptr<InsetTableCell> inset = cellInset(cell);
2538 Paragraph const & par = inset->paragraphs().front();
2539 string const lang = par.getParLanguage(buffer().params())->lang();
2541 //output the top line
2542 TeXTopHLine(os, row, lang);
2544 if (row_info[row].top_space_default) {
2546 os << "\\addlinespace\n";
2548 os << "\\noalign{\\vskip\\doublerulesep}\n";
2549 } else if(!row_info[row].top_space.zero()) {
2551 os << "\\addlinespace["
2552 << from_ascii(row_info[row].top_space.asLatexString())
2555 os << "\\noalign{\\vskip"
2556 << from_ascii(row_info[row].top_space.asLatexString())
2560 bool ismulticol = false;
2561 bool ismultirow = false;
2562 for (col_type c = 0; c < ncols(); ++c) {
2563 if (isPartOfMultiColumn(row, c))
2566 cell = cellIndex(row, c);
2568 if (isPartOfMultiRow(row, c)
2569 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2570 if (cell != getLastCellInRow(row))
2575 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2576 shared_ptr<InsetTableCell> inset = cellInset(cell);
2578 Paragraph const & par = inset->paragraphs().front();
2580 os.texrow().forceStart(par.id(), 0);
2582 bool rtl = par.isRTL(buffer().params())
2584 && getPWidth(cell).zero()
2585 && !runparams.use_polyglossia;
2589 par.getParLanguage(buffer().params())->lang();
2590 if (lang == "farsi")
2592 else if (lang == "arabic_arabi")
2594 // currently, remaining RTL languages are
2595 // arabic_arabtex and hebrew
2599 // pass to the OutputParams that we are in a cell and
2600 // which alignment we have set.
2601 // InsetNewline needs this context information.
2602 OutputParams newrp(runparams);
2603 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2604 ? OutputParams::PLAIN
2605 : OutputParams::ALIGNED;
2607 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2608 // copy cell and split in 2
2609 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2610 head.setBuffer(buffer());
2611 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2613 dit.push_back(CursorSlice(head));
2614 head.setMacrocontextPositionRecursive(dit);
2615 bool hassep = false;
2616 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2617 head.latex(os, newrp);
2620 tail.setBuffer(head.buffer());
2622 dit.push_back(CursorSlice(tail));
2623 tail.setMacrocontextPositionRecursive(dit);
2624 tail.latex(os, newrp);
2626 } else if (!isPartOfMultiRow(row, c)) {
2627 if (!runparams.nice)
2628 os.texrow().start(par.id(), 0);
2629 inset->latex(os, newrp);
2632 runparams.encoding = newrp.encoding;
2636 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2637 if (cell != getLastCellInRow(row)) { // not last cell in row
2644 os << "\\tabularnewline";
2645 if (row_info[row].bottom_space_default) {
2647 os << "\\addlinespace";
2649 os << "[\\doublerulesep]";
2650 } else if (!row_info[row].bottom_space.zero()) {
2652 os << "\\addlinespace";
2654 << from_ascii(row_info[row].bottom_space.asLatexString())
2659 //output the bottom line
2660 TeXBottomHLine(os, row, lang);
2662 if (row_info[row].interline_space_default) {
2664 os << "\\addlinespace\n";
2666 os << "\\noalign{\\vskip\\doublerulesep}\n";
2667 } else if (!row_info[row].interline_space.zero()) {
2669 os << "\\addlinespace["
2670 << from_ascii(row_info[row].interline_space.asLatexString())
2673 os << "\\noalign{\\vskip"
2674 << from_ascii(row_info[row].interline_space.asLatexString())
2680 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2682 bool const is_tabular_star = !tabular_width.zero();
2683 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid,
2686 //+---------------------------------------------------------------------
2687 //+ first the opening preamble +
2688 //+---------------------------------------------------------------------
2691 if (!TexRow::isNone(pos))
2692 os.texrow().start(pos);
2695 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2697 if (is_long_tabular) {
2698 os << "\\begin{longtable}";
2699 switch (longtabular_alignment) {
2700 case LYX_LONGTABULAR_ALIGN_LEFT:
2703 case LYX_LONGTABULAR_ALIGN_CENTER:
2705 case LYX_LONGTABULAR_ALIGN_RIGHT:
2710 if (is_tabular_star)
2711 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2713 os << "\\begin{tabular}";
2714 switch (tabular_valignment) {
2715 case LYX_VALIGN_TOP:
2718 case LYX_VALIGN_MIDDLE:
2720 case LYX_VALIGN_BOTTOM:
2728 if (is_tabular_star)
2729 os << "@{\\extracolsep{\\fill}}";
2731 for (col_type c = 0; c < ncols(); ++c) {
2732 if (columnLeftLine(c))
2734 if (!column_info[c].align_special.empty()) {
2735 os << column_info[c].align_special;
2737 if (!column_info[c].p_width.zero()) {
2738 switch (column_info[c].alignment) {
2739 case LYX_ALIGN_LEFT:
2740 os << ">{\\raggedright}";
2742 case LYX_ALIGN_RIGHT:
2743 os << ">{\\raggedleft}";
2745 case LYX_ALIGN_CENTER:
2746 os << ">{\\centering}";
2748 case LYX_ALIGN_NONE:
2749 case LYX_ALIGN_BLOCK:
2750 case LYX_ALIGN_LAYOUT:
2751 case LYX_ALIGN_SPECIAL:
2752 case LYX_ALIGN_DECIMAL:
2756 switch (column_info[c].valignment) {
2757 case LYX_VALIGN_TOP:
2760 case LYX_VALIGN_MIDDLE:
2763 case LYX_VALIGN_BOTTOM:
2768 << from_ascii(column_info[c].p_width.asLatexString())
2771 switch (column_info[c].alignment) {
2772 case LYX_ALIGN_LEFT:
2775 case LYX_ALIGN_RIGHT:
2778 case LYX_ALIGN_DECIMAL:
2779 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2785 } // end if else !column_info[i].p_width
2786 } // end if else !column_info[i].align_special
2787 if (columnRightLine(c))
2792 TeXLongtableHeaderFooter(os, runparams);
2794 //+---------------------------------------------------------------------
2795 //+ the single row and columns (cells) +
2796 //+---------------------------------------------------------------------
2798 for (row_type r = 0; r < nrows(); ++r) {
2799 if (isValidRow(r)) {
2800 TeXRow(os, r, runparams);
2801 if (is_long_tabular && row_info[r].newpage)
2802 os << "\\newpage\n";
2806 //+---------------------------------------------------------------------
2807 //+ the closing of the tabular +
2808 //+---------------------------------------------------------------------
2810 if (is_long_tabular)
2811 os << "\\end{longtable}";
2813 if (is_tabular_star)
2814 os << "\\end{tabular*}";
2816 os << "\\end{tabular}";
2820 os << breakln << "\\end{turn}";
2822 if (!TexRow::isNone(pos))
2823 os.texrow().start(pos);
2827 int Tabular::docbookRow(odocstream & os, row_type row,
2828 OutputParams const & runparams) const
2831 idx_type cell = getFirstCellInRow(row);
2834 for (col_type c = 0; c < ncols(); ++c) {
2835 if (isPartOfMultiColumn(row, c))
2838 os << "<entry align=\"";
2839 switch (getAlignment(cell)) {
2840 case LYX_ALIGN_LEFT:
2843 case LYX_ALIGN_RIGHT:
2851 os << "\" valign=\"";
2852 switch (getVAlignment(cell)) {
2853 case LYX_VALIGN_TOP:
2856 case LYX_VALIGN_BOTTOM:
2859 case LYX_VALIGN_MIDDLE:
2864 if (isMultiColumn(cell)) {
2865 os << " namest=\"col" << c << "\" ";
2866 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2870 ret += cellInset(cell)->docbook(os, runparams);
2879 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2883 //+---------------------------------------------------------------------
2884 //+ first the opening preamble +
2885 //+---------------------------------------------------------------------
2887 os << "<tgroup cols=\"" << ncols()
2888 << "\" colsep=\"1\" rowsep=\"1\">\n";
2890 for (col_type c = 0; c < ncols(); ++c) {
2891 os << "<colspec colname=\"col" << c << "\" align=\"";
2892 switch (column_info[c].alignment) {
2893 case LYX_ALIGN_LEFT:
2896 case LYX_ALIGN_RIGHT:
2904 if (runparams.flavor == OutputParams::XML)
2910 //+---------------------------------------------------------------------
2911 //+ Long Tabular case +
2912 //+---------------------------------------------------------------------
2914 // output caption info
2915 // The caption flag wins over head/foot
2916 if (haveLTCaption()) {
2917 os << "<caption>\n";
2919 for (row_type r = 0; r < nrows(); ++r) {
2920 if (row_info[r].caption) {
2921 ret += docbookRow(os, r, runparams);
2924 os << "</caption>\n";
2927 // output header info
2928 if (haveLTHead(false) || haveLTFirstHead(false)) {
2931 for (row_type r = 0; r < nrows(); ++r) {
2932 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2933 !row_info[r].caption) {
2934 ret += docbookRow(os, r, runparams);
2940 // output footer info
2941 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2944 for (row_type r = 0; r < nrows(); ++r) {
2945 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2946 !row_info[r].caption) {
2947 ret += docbookRow(os, r, runparams);
2954 //+---------------------------------------------------------------------
2955 //+ the single row and columns (cells) +
2956 //+---------------------------------------------------------------------
2960 for (row_type r = 0; r < nrows(); ++r) {
2961 if (isValidRow(r)) {
2962 ret += docbookRow(os, r, runparams);
2967 //+---------------------------------------------------------------------
2968 //+ the closing of the tabular +
2969 //+---------------------------------------------------------------------
2978 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2979 OutputParams const & runparams, bool header) const
2982 string const celltag = header ? "th" : "td";
2983 idx_type cell = getFirstCellInRow(row);
2985 xs << html::StartTag("tr");
2986 for (col_type c = 0; c < ncols(); ++c) {
2987 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2992 switch (getAlignment(cell)) {
2993 case LYX_ALIGN_LEFT:
2996 case LYX_ALIGN_RIGHT:
3004 attr << " valign='";
3005 switch (getVAlignment(cell)) {
3006 case LYX_VALIGN_TOP:
3009 case LYX_VALIGN_BOTTOM:
3012 case LYX_VALIGN_MIDDLE:
3017 if (isMultiColumn(cell))
3018 attr << " colspan='" << columnSpan(cell) << "'";
3019 else if (isMultiRow(cell))
3020 attr << " rowspan='" << rowSpan(cell) << "'";
3022 xs << html::StartTag(celltag, attr.str()) << html::CR();
3023 ret += cellInset(cell)->xhtml(xs, runparams);
3024 xs << html::EndTag(celltag) << html::CR();
3027 xs << html::EndTag("tr");
3032 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3036 if (is_long_tabular) {
3037 // we'll wrap it in a div, so as to deal with alignment
3039 switch (longtabular_alignment) {
3040 case LYX_LONGTABULAR_ALIGN_LEFT:
3043 case LYX_LONGTABULAR_ALIGN_CENTER:
3046 case LYX_LONGTABULAR_ALIGN_RIGHT:
3050 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3052 // The caption flag wins over head/foot
3053 if (haveLTCaption()) {
3054 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3056 for (row_type r = 0; r < nrows(); ++r)
3057 if (row_info[r].caption)
3058 ret += xhtmlRow(xs, r, runparams);
3059 xs << html::EndTag("div") << html::CR();
3063 xs << html::StartTag("table") << html::CR();
3065 // output header info
3066 bool const havefirsthead = haveLTFirstHead(false);
3067 // if we have a first head, then we are going to ignore the
3068 // headers for the additional pages, since there aren't any
3069 // in XHTML. this test accomplishes that.
3070 bool const havehead = !havefirsthead && haveLTHead(false);
3071 if (havehead || havefirsthead) {
3072 xs << html::StartTag("thead") << html::CR();
3073 for (row_type r = 0; r < nrows(); ++r) {
3074 if (((havefirsthead && row_info[r].endfirsthead) ||
3075 (havehead && row_info[r].endhead)) &&
3076 !row_info[r].caption) {
3077 ret += xhtmlRow(xs, r, runparams, true);
3080 xs << html::EndTag("thead") << html::CR();
3082 // output footer info
3083 bool const havelastfoot = haveLTLastFoot(false);
3085 bool const havefoot = !havelastfoot && haveLTFoot(false);
3086 if (havefoot || havelastfoot) {
3087 xs << html::StartTag("tfoot") << html::CR();
3088 for (row_type r = 0; r < nrows(); ++r) {
3089 if (((havelastfoot && row_info[r].endlastfoot) ||
3090 (havefoot && row_info[r].endfoot)) &&
3091 !row_info[r].caption) {
3092 ret += xhtmlRow(xs, r, runparams);
3095 xs << html::EndTag("tfoot") << html::CR();
3098 xs << html::StartTag("tbody") << html::CR();
3099 for (row_type r = 0; r < nrows(); ++r) {
3100 if (isValidRow(r)) {
3101 ret += xhtmlRow(xs, r, runparams);
3104 xs << html::EndTag("tbody")
3106 << html::EndTag("table")
3108 if (is_long_tabular)
3109 xs << html::EndTag("div") << html::CR();
3114 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3115 vector<unsigned int> const & clen) const
3117 idx_type const fcell = getFirstCellInRow(row);
3118 idx_type const n = numberOfCellsInRow(row) + fcell;
3121 for (idx_type i = fcell; i < n; ++i) {
3131 for (idx_type i = fcell; i < n; ++i) {
3142 col_type column = cellColumn(i);
3143 int len = clen[column];
3144 while (column < ncols() - 1
3145 && isPartOfMultiColumn(row, ++column))
3146 len += clen[column] + 4;
3147 os << docstring(len, ch);
3162 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3163 vector<unsigned int> const & clen) const
3165 idx_type const fcell = getFirstCellInRow(row);
3166 idx_type const n = numberOfCellsInRow(row) + fcell;
3169 for (idx_type i = fcell; i < n; ++i) {
3170 if (bottomLine(i)) {
3179 for (idx_type i = fcell; i < n; ++i) {
3180 if (bottomLine(i)) {
3190 col_type column = cellColumn(i);
3191 int len = clen[column];
3192 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3193 len += clen[column] + 4;
3194 os << docstring(len, ch);
3195 if (bottomLine(i)) {
3209 void Tabular::plaintextPrintCell(odocstringstream & os,
3210 OutputParams const & runparams,
3211 idx_type cell, row_type row, col_type column,
3212 vector<unsigned int> const & clen,
3213 bool onlydata, size_t max_length) const
3215 odocstringstream sstr;
3216 cellInset(cell)->plaintext(sstr, runparams, max_length);
3228 unsigned int len1 = sstr.str().length();
3229 unsigned int len2 = clen[column];
3230 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3231 len2 += clen[column] + 4;
3234 switch (getAlignment(cell)) {
3236 case LYX_ALIGN_LEFT:
3239 case LYX_ALIGN_RIGHT:
3243 case LYX_ALIGN_CENTER:
3249 os << docstring(len1, ' ') << sstr.str()
3250 << docstring(len2, ' ');
3252 if (rightLine(cell))
3259 void Tabular::plaintext(odocstringstream & os,
3260 OutputParams const & runparams, int const depth,
3261 bool onlydata, char_type delim, size_t max_length) const
3263 // first calculate the width of the single columns
3264 vector<unsigned int> clen(ncols());
3267 // first all non multicolumn cells!
3268 for (col_type c = 0; c < ncols(); ++c) {
3270 for (row_type r = 0; r < nrows(); ++r) {
3271 idx_type cell = cellIndex(r, c);
3272 if (isMultiColumn(cell))
3274 odocstringstream sstr;
3275 cellInset(cell)->plaintext(sstr, runparams, max_length);
3276 if (clen[c] < sstr.str().length())
3277 clen[c] = sstr.str().length();
3280 // then all multicolumn cells!
3281 for (col_type c = 0; c < ncols(); ++c) {
3282 for (row_type r = 0; r < nrows(); ++r) {
3283 idx_type cell = cellIndex(r, c);
3284 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3286 odocstringstream sstr;
3287 cellInset(cell)->plaintext(sstr, runparams, max_length);
3288 int len = int(sstr.str().length());
3289 idx_type const n = columnSpan(cell);
3290 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3292 if (len > int(clen[c + n - 1]))
3293 clen[c + n - 1] = len;
3298 for (row_type r = 0; r < nrows(); ++r) {
3299 if (!onlydata && plaintextTopHLine(os, r, clen))
3300 os << docstring(depth * 2, ' ');
3301 for (col_type c = 0; c < ncols(); ++c) {
3302 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3304 if (onlydata && c > 0)
3305 // we don't use operator<< for single UCS4 character.
3306 // see explanation in docstream.h
3308 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3310 if (os.str().size() > max_length)
3315 os << docstring(depth * 2, ' ');
3316 if (plaintextBottomHLine(os, r, clen))
3317 os << docstring(depth * 2, ' ');
3319 if (os.str().size() > max_length)
3325 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3327 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3331 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3332 col_type column) const
3334 return cell_info[row][column].inset;
3338 void Tabular::setCellInset(row_type row, col_type column,
3339 shared_ptr<InsetTableCell> ins) const
3341 CellData & cd = cell_info[row][column];
3346 void Tabular::validate(LaTeXFeatures & features) const
3348 features.require("NeedTabularnewline");
3350 features.require("booktabs");
3351 if (is_long_tabular)
3352 features.require("longtable");
3354 features.require("rotating");
3355 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3356 if (isMultiRow(cell))
3357 features.require("multirow");
3358 if (getVAlignment(cell) != LYX_VALIGN_TOP
3359 || !getPWidth(cell).zero())
3360 features.require("array");
3361 cellInset(cell)->validate(features);
3366 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3368 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3369 ParagraphList::const_iterator cit = parlist.begin();
3370 ParagraphList::const_iterator end = parlist.end();
3372 for (; cit != end; ++cit)
3373 for (int i = 0; i < cit->size(); ++i)
3374 if (cit->isNewline(i))
3381 /////////////////////////////////////////////////////////////////////
3385 /////////////////////////////////////////////////////////////////////
3387 InsetTableCell::InsetTableCell(Buffer * buf)
3388 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3389 contentAlign(LYX_ALIGN_CENTER)
3393 bool InsetTableCell::forcePlainLayout(idx_type) const
3395 return !isFixedWidth;
3399 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3401 return isFixedWidth;
3405 bool InsetTableCell::forceLocalFontSwitch() const
3407 return isFixedWidth;
3411 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3412 FuncStatus & status) const
3414 bool enabled = true;
3415 switch (cmd.action()) {
3416 case LFUN_LAYOUT_PARAGRAPH:
3417 enabled = allowParagraphCustomization();
3420 case LFUN_MATH_DISPLAY:
3421 if (!hasFixedWidth()) {
3426 return InsetText::getStatus(cur, cmd, status);
3428 status.setEnabled(enabled);
3432 docstring InsetTableCell::asString(bool intoInsets)
3435 if (paragraphs().empty())
3437 ParagraphList::const_iterator it = paragraphs().begin();
3438 ParagraphList::const_iterator en = paragraphs().end();
3440 for (; it != en; ++it) {
3445 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3451 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3452 UpdateType utype) const
3454 InsetText::iterateForToc(di, output_active, utype);
3458 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3461 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3462 return InsetText::xhtml(xs, rp);
3467 /////////////////////////////////////////////////////////////////////
3471 /////////////////////////////////////////////////////////////////////
3473 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3475 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3476 first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
3481 InsetTabular::InsetTabular(InsetTabular const & tab)
3482 : Inset(tab), tabular(tab.tabular)
3487 InsetTabular::~InsetTabular()
3489 hideDialogs("tabular", this);
3493 void InsetTabular::setBuffer(Buffer & buf)
3495 tabular.setBuffer(buf);
3496 Inset::setBuffer(buf);
3500 bool InsetTabular::insetAllowed(InsetCode code) const
3505 case MATHMACRO_CODE:
3510 return tabular.is_long_tabular;
3518 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3520 return tabular.is_long_tabular &&
3521 (newtype == "Standard" || newtype == "Unnumbered");
3525 void InsetTabular::write(ostream & os) const
3527 os << "Tabular" << endl;
3532 string InsetTabular::contextMenu(BufferView const &, int, int) const
3534 // FIXME: depending on the selection state,
3535 // we could offer a different menu.
3536 return cell(0)->contextMenuName() + ";" + contextMenuName();
3540 string InsetTabular::contextMenuName() const
3542 return "context-tabular";
3546 void InsetTabular::read(Lexer & lex)
3548 //bool const old_format = (lex.getString() == "\\LyXTable");
3556 string token = lex.getString();
3557 while (lex && token != "\\end_inset") {
3559 token = lex.getString();
3562 lex.printError("Missing \\end_inset at this point. ");
3566 int InsetTabular::rowFromY(Cursor & cur, int y) const
3568 // top y coordinate of tabular
3569 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3571 for (; r < tabular.nrows() && y > h; ++r)
3572 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3573 + tabular.interRowSpace(r);
3579 int InsetTabular::columnFromX(Cursor & cur, int x) const
3581 // left x coordinate of tabular
3582 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3584 for (; c < tabular.ncols() && x > w; ++c)
3585 w += tabular.cellWidth(c);
3590 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3592 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3593 // mi.base.textwidth << "\n";
3594 LBUFERR(mi.base.bv);
3596 for (row_type r = 0; r < tabular.nrows(); ++r) {
3599 for (col_type c = 0; c < tabular.ncols(); ++c) {
3600 if (tabular.isPartOfMultiColumn(r, c)
3601 || tabular.isPartOfMultiRow(r, c))
3602 // multicolumn or multirow cell, but not first one
3604 idx_type const cell = tabular.cellIndex(r, c);
3607 Length const p_width = tabular.getPWidth(cell);
3608 if (!p_width.zero())
3609 m.base.textwidth = p_width.inPixels(mi.base);
3610 tabular.cellInset(cell)->metrics(m, dim);
3611 if (!p_width.zero())
3612 dim.wid = m.base.textwidth;
3613 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3614 + tabular.interColumnSpace(cell);
3616 // FIXME(?): do we need a second metrics call?
3617 TextMetrics const & tm =
3618 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3620 // determine horizontal offset because of decimal align (if necessary)
3621 int decimal_width = 0;
3622 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3623 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3624 tail.setBuffer(tabular.buffer());
3625 // we need to set macrocontext position everywhere
3626 // otherwise we crash with nested insets (e.g. footnotes)
3627 // after decimal point
3628 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3630 dit.push_back(CursorSlice(tail));
3631 tail.setMacrocontextPositionRecursive(dit);
3633 // remove text leading decimal point
3634 docstring const align_d = tabular.column_info[c].decimal_point;
3635 dit = separatorPos(&tail, align_d);
3637 pit_type const psize = tail.paragraphs().front().size();
3639 tail.paragraphs().front().eraseChars(0,
3640 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3642 tail.metrics(m, dim1);
3643 decimal_width = dim1.width();
3646 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3647 tabular.cell_info[r][c].decimal_width = decimal_width;
3649 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3650 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3651 int const lastpardes = tm.last().second->descent()
3652 + TEXT_TO_INSET_OFFSET;
3654 switch (tabular.getVAlignment(cell)) {
3655 case Tabular::LYX_VALIGN_TOP:
3657 case Tabular::LYX_VALIGN_MIDDLE:
3658 offset = -(dim.des - lastpardes)/2;
3660 case Tabular::LYX_VALIGN_BOTTOM:
3661 offset = -(dim.des - lastpardes);
3664 tabular.cell_info[r][c].voffset = offset;
3665 maxasc = max(maxasc, dim.asc - offset);
3666 maxdes = max(maxdes, dim.des + offset);
3668 int const top_space = tabular.row_info[r].top_space_default ?
3669 default_line_space :
3670 tabular.row_info[r].top_space.inPixels(mi.base);
3671 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3672 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3673 default_line_space :
3674 tabular.row_info[r].bottom_space.inPixels(mi.base);
3675 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3678 // for top-alignment the first horizontal table line must be exactly at
3679 // the position of the base line of the surrounding text line
3680 // for bottom alignment, the same is for the last table line
3681 switch (tabular.tabular_valignment) {
3682 case Tabular::LYX_VALIGN_BOTTOM:
3683 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3685 case Tabular::LYX_VALIGN_MIDDLE:
3686 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3688 case Tabular::LYX_VALIGN_TOP:
3689 offset_valign_ = tabular.rowAscent(0);
3693 tabular.updateColumnWidths();
3694 dim.asc = tabular.rowAscent(0) - offset_valign_;
3695 dim.des = tabular.height() - dim.asc;
3696 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3700 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3703 if (&cur.inset() == this && cur.selection()) {
3704 if (cur.selIsMultiCell()) {
3707 getSelection(cur, rs, re, cs, ce);
3709 idx_type const cell = tabular.cellIndex(row, col);
3710 col_type const cspan = tabular.columnSpan(cell);
3711 row_type const rspan = tabular.rowSpan(cell);
3712 if (col + cspan - 1 >= cs && col <= ce
3713 && row + rspan - 1 >= rs && row <= re)
3716 if (col == tabular.cellColumn(cur.idx())
3717 && row == tabular.cellRow(cur.idx())) {
3718 CursorSlice const & beg = cur.selBegin();
3719 CursorSlice const & end = cur.selEnd();
3721 if ((end.lastpos() > 0 || end.lastpit() > 0)
3722 && end.pos() == end.lastpos() && beg.pos() == 0
3723 && end.pit() == end.lastpit() && beg.pit() == 0)
3731 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3733 x += ADD_TO_TABULAR_WIDTH;
3735 BufferView * bv = pi.base.bv;
3736 Cursor & cur = pi.base.bv->cursor();
3738 // FIXME: As the full background is painted in drawBackground(),
3739 // we have no choice but to do a full repaint for the Text cells.
3740 pi.full_repaint = true;
3742 bool const original_selection_state = pi.selected;
3745 first_visible_cell_ = Tabular::npos;
3747 int yy = y + offset_valign_;
3748 for (row_type r = 0; r < tabular.nrows(); ++r) {
3750 for (col_type c = 0; c < tabular.ncols(); ++c) {
3751 if (tabular.isPartOfMultiColumn(r, c))
3754 idx = tabular.cellIndex(r, c);
3756 if (tabular.isPartOfMultiRow(r, c)) {
3757 nx += tabular.cellWidth(idx);
3761 if (first_visible_cell_ == Tabular::npos)
3762 first_visible_cell_ = idx;
3764 pi.selected |= isCellSelected(cur, r, c);
3765 int const cx = nx + tabular.textHOffset(idx);
3766 int const cy = yy + tabular.textVOffset(idx);
3767 // Cache the Inset position.
3768 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3769 cell(idx)->draw(pi, cx, cy);
3770 drawCellLines(pi, nx, yy, r, idx);
3771 nx += tabular.cellWidth(idx);
3772 pi.selected = original_selection_state;
3775 if (r + 1 < tabular.nrows())
3776 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3777 + tabular.interRowSpace(r + 1);
3782 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3784 x += ADD_TO_TABULAR_WIDTH;
3785 y += offset_valign_ - tabular.rowAscent(0);
3786 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3787 pi.backgroundColor(this));
3791 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3793 Cursor & cur = pi.base.bv->cursor();
3795 x += ADD_TO_TABULAR_WIDTH;
3797 if (!cur.selection())
3799 if (&cur.inset() != this)
3804 bool const full_cell_selected = isCellSelected(cur,
3805 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3807 if (cur.selIsMultiCell() || full_cell_selected) {
3808 for (row_type r = 0; r < tabular.nrows(); ++r) {
3810 for (col_type c = 0; c < tabular.ncols(); ++c) {
3811 if (tabular.isPartOfMultiColumn(r, c))
3814 idx_type const cell = tabular.cellIndex(r, c);
3816 if (tabular.isPartOfMultiRow(r, c)) {
3817 xx += tabular.cellWidth(cell);
3820 int const w = tabular.cellWidth(cell);
3821 int const h = tabular.cellHeight(cell);
3822 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3823 if (isCellSelected(cur, r, c))
3824 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3827 if (r + 1 < tabular.nrows())
3828 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3829 + tabular.interRowSpace(r + 1);
3833 // FIXME: This code has no effect because InsetTableCell does not handle
3834 // drawSelection other than the trivial implementation in Inset.
3836 // x += cellXPos(cur.idx());
3837 // x += tabular.textHOffset(cur.idx());
3838 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3843 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3844 row_type row, idx_type cell) const
3846 y -= tabular.rowAscent(row);
3847 int const w = tabular.cellWidth(cell);
3848 int const h = tabular.cellHeight(cell);
3849 Color const linecolor = pi.textColor(Color_tabularline);
3850 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3853 bool drawline = tabular.topLine(cell)
3854 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3855 pi.pain.line(x, y, x + w, y,
3856 drawline ? linecolor : gridcolor,
3857 drawline ? Painter::line_solid : Painter::line_onoffdash);
3860 drawline = tabular.bottomLine(cell);
3861 pi.pain.line(x, y + h, x + w, y + h,
3862 drawline ? linecolor : gridcolor,
3863 drawline ? Painter::line_solid : Painter::line_onoffdash);
3866 col_type const col = tabular.cellColumn(cell);
3867 drawline = tabular.leftLine(cell)
3868 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3869 pi.pain.line(x, y, x, y + h,
3870 drawline ? linecolor : gridcolor,
3871 drawline ? Painter::line_solid : Painter::line_onoffdash);
3874 x -= tabular.interColumnSpace(cell);
3875 col_type next_cell_col = col + 1;
3876 while (next_cell_col < tabular.ncols()
3877 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3879 drawline = tabular.rightLine(cell)
3880 || (next_cell_col < tabular.ncols()
3881 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3882 pi.pain.line(x + w, y, x + w, y + h,
3883 drawline ? linecolor : gridcolor,
3884 drawline ? Painter::line_solid : Painter::line_onoffdash);
3888 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3890 //lyxerr << "InsetTabular::edit: " << this << endl;
3894 if (isRightToLeft(cur))
3895 cur.idx() = tabular.getLastCellInRow(0);
3901 if (isRightToLeft(cur))
3902 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3904 cur.idx() = tabular.numberofcells - 1;
3906 cur.pos() = cur.lastpos(); // FIXME crude guess
3908 cur.setCurrentFont();
3909 // FIXME: this accesses the position cache before it is initialized
3910 //cur.bv().fitCursor();
3914 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3916 // In a longtable, tell captions what the current float is
3917 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3918 string const saveflt = cnts.current_float();
3919 if (tabular.is_long_tabular) {
3920 cnts.current_float("table");
3921 // in longtables, we only step the counter once
3922 cnts.step(from_ascii("table"), utype);
3923 cnts.isLongtable(true);
3926 ParIterator it2 = it;
3928 size_t const end = it2.nargs();
3929 for ( ; it2.idx() < end; it2.top().forwardIdx())
3930 buffer().updateBuffer(it2, utype);
3933 if (tabular.is_long_tabular) {
3934 cnts.current_float(saveflt);
3935 cnts.isLongtable(false);
3940 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3941 UpdateType utype) const
3943 DocIterator dit = cpit;
3945 size_t const end = dit.nargs();
3946 for ( ; dit.idx() < end; dit.top().forwardIdx())
3947 cell(dit.idx())->addToToc(dit, output_active, utype);
3951 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
3953 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
3954 return x < x0 || x > x0 + tabular.width();
3958 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
3960 int const y0 = yo(bv) - tabular.rowAscent(0) + offset_valign_;
3961 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
3962 // there is no margin added vertically to tabular insets.
3963 // However, it works for now.
3964 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
3968 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
3970 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
3974 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3976 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3977 << "\n cur:" << cur);
3978 CursorSlice sl = cur.top();
3979 Cursor & bvcur = cur.bv().cursor();
3981 FuncCode const act = cmd.action();
3985 case LFUN_MOUSE_PRESS: {
3986 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3988 if (hitSelectRow(cur.bv(), cmd.x())) {
3989 row_type r = rowFromY(cur, cmd.y());
3990 cur.idx() = tabular.getFirstCellInRow(r);
3994 cur.idx() = tabular.getLastCellInRow(r);
3995 cur.pit() = cur.lastpit();
3996 cur.pos() = cur.lastpos();
3997 cur.selection(true);
4003 if (hitSelectColumn(cur.bv(), cmd.y())) {
4004 col_type c = columnFromX(cur, cmd.x());
4005 cur.idx() = tabular.cellIndex(0, c);
4009 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4010 cur.pit() = cur.lastpit();
4011 cur.pos() = cur.lastpos();
4012 cur.selection(true);
4017 // do not reset cursor/selection if we have selected
4018 // some cells (bug 2715).
4019 if (cmd.button() == mouse_button::button3
4020 && &bvcur.selBegin().inset() == this
4021 && bvcur.selIsMultiCell())
4024 // Let InsetTableCell do it
4025 cell(cur.idx())->dispatch(cur, cmd);
4028 case LFUN_MOUSE_MOTION:
4029 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4030 if (cmd.button() == mouse_button::button1) {
4031 // only accept motions to places not deeper nested than the real anchor
4032 if (!bvcur.realAnchor().hasPart(cur)) {
4036 // select (additional) row
4038 row_type r = rowFromY(cur, cmd.y());
4039 cur.idx() = tabular.getLastCellInRow(r);
4040 // we need to reset the cursor's pit and pos now, as the old ones
4041 // may no longer be valid.
4044 bvcur.setCursor(cur);
4045 bvcur.selection(true);
4048 // select (additional) column
4050 col_type c = columnFromX(cur, cmd.x());
4051 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4052 // we need to reset the cursor's pit and pos now, as the old ones
4053 // may no longer be valid.
4056 bvcur.setCursor(cur);
4057 bvcur.selection(true);
4060 // only update if selection changes
4061 if (bvcur.idx() == cur.idx() &&
4062 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4063 cur.noScreenUpdate();
4064 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4065 bvcur.setCursor(cur);
4066 bvcur.selection(true);
4067 // if this is a multicell selection, we just set the cursor to
4068 // the beginning of the cell's text.
4069 if (bvcur.selIsMultiCell()) {
4070 bvcur.pit() = bvcur.lastpit();
4071 bvcur.pos() = bvcur.lastpos();
4076 case LFUN_MOUSE_RELEASE:
4081 case LFUN_CELL_BACKWARD:
4083 cur.selection(false);
4086 case LFUN_CELL_FORWARD:
4088 cur.selection(false);
4091 case LFUN_CHAR_FORWARD_SELECT:
4092 case LFUN_CHAR_FORWARD:
4093 case LFUN_CHAR_BACKWARD_SELECT:
4094 case LFUN_CHAR_BACKWARD:
4095 case LFUN_CHAR_RIGHT_SELECT:
4096 case LFUN_CHAR_RIGHT:
4097 case LFUN_CHAR_LEFT_SELECT:
4098 case LFUN_CHAR_LEFT:
4099 case LFUN_WORD_FORWARD:
4100 case LFUN_WORD_FORWARD_SELECT:
4101 case LFUN_WORD_BACKWARD:
4102 case LFUN_WORD_BACKWARD_SELECT:
4103 case LFUN_WORD_RIGHT:
4104 case LFUN_WORD_RIGHT_SELECT:
4105 case LFUN_WORD_LEFT:
4106 case LFUN_WORD_LEFT_SELECT: {
4107 // determine whether we move to next or previous cell, where to enter
4108 // the new cell from, and which command to "finish" (i.e., exit the
4111 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4112 FuncCode finish_lfun;
4114 if (act == LFUN_CHAR_FORWARD
4115 || act == LFUN_CHAR_FORWARD_SELECT
4116 || act == LFUN_WORD_FORWARD
4117 || act == LFUN_WORD_FORWARD_SELECT) {
4119 finish_lfun = LFUN_FINISHED_FORWARD;
4121 else if (act == LFUN_CHAR_BACKWARD
4122 || act == LFUN_CHAR_BACKWARD_SELECT
4123 || act == LFUN_WORD_BACKWARD
4124 || act == LFUN_WORD_BACKWARD_SELECT) {
4126 finish_lfun = LFUN_FINISHED_BACKWARD;
4128 // LEFT or RIGHT commands --- the interpretation will depend on the
4129 // table's direction.
4131 bool const right = act == LFUN_CHAR_RIGHT
4132 || act == LFUN_CHAR_RIGHT_SELECT
4133 || act == LFUN_WORD_RIGHT
4134 || act == LFUN_WORD_RIGHT_SELECT;
4135 next_cell = isRightToLeft(cur) != right;
4137 if (lyxrc.visual_cursor)
4138 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4140 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4143 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4144 || act == LFUN_CHAR_BACKWARD_SELECT
4145 || act == LFUN_CHAR_RIGHT_SELECT
4146 || act == LFUN_CHAR_LEFT_SELECT
4147 || act == LFUN_WORD_FORWARD_SELECT
4148 || act == LFUN_WORD_RIGHT_SELECT
4149 || act == LFUN_WORD_BACKWARD_SELECT
4150 || act == LFUN_WORD_LEFT_SELECT;
4152 // If we have a multicell selection or we're
4153 // not doing some LFUN_*_SELECT thing anyway...
4154 if (!cur.selIsMultiCell() || !select) {
4155 col_type const c = tabular.cellColumn(cur.idx());
4156 row_type const r = tabular.cellRow(cur.idx());
4157 // Are we trying to select the whole cell and is the whole cell
4158 // not yet selected?
4159 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4160 ((next_cell && cur.pit() == cur.lastpit()
4161 && cur.pos() == cur.lastpos())
4162 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4164 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4166 // ...try to dispatch to the cell's inset.
4167 cell(cur.idx())->dispatch(cur, cmd);
4169 // When we already have a selection we want to select the whole cell
4170 // before going to the next cell.
4171 if (select_whole && !empty_cell){
4172 getText(cur.idx())->selectAll(cur);
4174 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4178 // FIXME: When we support the selection of an empty cell, remove
4179 // the !empty_cell from this condition. For now we jump to the next
4180 // cell if the current cell is empty.
4181 if (cur.result().dispatched() && !empty_cell)
4185 // move to next/prev cell, as appropriate
4186 // note that we will always do this if we're selecting and we have
4187 // a multicell selection
4188 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4189 << " cell from: " << int(entry_from));
4191 moveNextCell(cur, entry_from);
4193 movePrevCell(cur, entry_from);
4194 // if we're exiting the table, call the appropriate FINISHED lfun
4195 if (sl == cur.top()) {
4196 cmd = FuncRequest(finish_lfun);
4201 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4206 case LFUN_DOWN_SELECT:
4208 if (!(cur.selection() && cur.selIsMultiCell()))
4209 cell(cur.idx())->dispatch(cur, cmd);
4211 cur.dispatched(); // override the cell's decision
4212 if (sl == cur.top()) {
4213 // if our Text didn't do anything to the cursor
4214 // then we try to put the cursor into the cell below
4215 // setting also the right targetX.
4216 cur.selHandle(act == LFUN_DOWN_SELECT);
4217 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4218 int const xtarget = cur.targetX();
4219 // WARNING: Once cur.idx() has been reset, the cursor is in
4220 // an inconsistent state until pos() has been set. Be careful
4221 // what you do with it!
4222 cur.idx() = tabular.cellBelow(cur.idx());
4224 TextMetrics const & tm =
4225 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4226 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4227 cur.setCurrentFont();
4230 if (sl == cur.top()) {
4231 // we trick it to go to forward after leaving the
4233 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4236 if (cur.selIsMultiCell()) {
4237 cur.pit() = cur.lastpit();
4238 cur.pos() = cur.lastpos();
4239 cur.setCurrentFont();
4240 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4243 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4246 case LFUN_UP_SELECT:
4248 if (!(cur.selection() && cur.selIsMultiCell()))
4249 cell(cur.idx())->dispatch(cur, cmd);
4250 cur.dispatched(); // override the cell's decision
4251 if (sl == cur.top()) {
4252 // if our Text didn't do anything to the cursor
4253 // then we try to put the cursor into the cell above
4254 // setting also the right targetX.
4255 cur.selHandle(act == LFUN_UP_SELECT);
4256 if (tabular.cellRow(cur.idx()) != 0) {
4257 int const xtarget = cur.targetX();
4258 // WARNING: Once cur.idx() has been reset, the cursor is in
4259 // an inconsistent state until pos() has been set. Be careful
4260 // what you do with it!
4261 cur.idx() = tabular.cellAbove(cur.idx());
4262 cur.pit() = cur.lastpit();
4263 Text const * text = cell(cur.idx())->getText(0);
4264 TextMetrics const & tm = cur.bv().textMetrics(text);
4265 ParagraphMetrics const & pm =
4266 tm.parMetrics(cur.lastpit());
4267 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4268 cur.setCurrentFont();
4271 if (sl == cur.top()) {
4272 cmd = FuncRequest(LFUN_UP);
4275 if (cur.selIsMultiCell()) {
4277 cur.pos() = cur.lastpos();
4278 cur.setCurrentFont();
4279 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4282 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4285 // case LFUN_SCREEN_DOWN: {
4286 // //if (hasSelection())
4287 // // cur.selection() = false;
4288 // col_type const col = tabular.cellColumn(cur.idx());
4289 // int const t = cur.bv().top_y() + cur.bv().height();
4290 // if (t < yo() + tabular.getHeightOfTabular()) {
4291 // cur.bv().scrollDocView(t, true);
4292 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4294 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4301 // case LFUN_SCREEN_UP: {
4302 // //if (hasSelection())
4303 // // cur.selection() = false;
4304 // col_type const col = tabular.cellColumn(cur.idx());
4305 // int const t = cur.bv().top_y() + cur.bv().height();
4307 // cur.bv().scrollDocView(t, true);
4311 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4315 // cur.par() = cur.lastpar();
4316 // cur.pos() = cur.lastpos();
4320 case LFUN_LAYOUT_TABULAR:
4321 cur.bv().showDialog("tabular");
4324 case LFUN_INSET_MODIFY:
4325 // we come from the dialog
4326 if (cmd.getArg(0) == "tabular")
4327 tabularFeatures(cur, cmd.getLongArg(1));
4332 case LFUN_TABULAR_FEATURE:
4333 tabularFeatures(cur, to_utf8(cmd.argument()));
4336 // insert file functions
4337 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4338 case LFUN_FILE_INSERT_PLAINTEXT:
4340 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4341 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4342 FileName(to_utf8(cmd.argument())));
4345 cur.recordUndoInset();
4346 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4347 // content has been replaced,
4348 // so cursor might be invalid
4349 cur.pos() = cur.lastpos();
4350 cur.pit() = cur.lastpit();
4351 bvcur.setCursor(cur);
4358 if (cur.selIsMultiCell()) {
4359 if (copySelection(cur)) {
4360 cur.recordUndoInset();
4364 cell(cur.idx())->dispatch(cur, cmd);
4367 case LFUN_SELF_INSERT:
4368 if (cur.selIsMultiCell()) {
4369 cur.recordUndoInset();
4371 BufferView * bv = &cur.bv();
4372 docstring::const_iterator cit = cmd.argument().begin();
4373 docstring::const_iterator const end = cmd.argument().end();
4374 for (; cit != end; ++cit)
4375 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4378 bv->bookmarkEditPosition();
4380 cell(cur.idx())->dispatch(cur, cmd);
4383 case LFUN_CHAR_DELETE_BACKWARD:
4384 case LFUN_CHAR_DELETE_FORWARD:
4385 if (cur.selIsMultiCell()) {
4386 cur.recordUndoInset();
4389 cell(cur.idx())->dispatch(cur, cmd);
4393 if (!cur.selection())
4395 if (cur.selIsMultiCell()) {
4399 cell(cur.idx())->dispatch(cur, cmd);
4402 case LFUN_CLIPBOARD_PASTE:
4403 case LFUN_PRIMARY_SELECTION_PASTE: {
4404 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4405 theClipboard().getAsText(Clipboard::PlainTextType) :
4406 theSelection().get();
4409 // pass to InsertPlaintextString, but
4410 // only if we have multi-cell content
4411 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4412 cur.recordUndoInset();
4413 if (insertPlaintextString(cur.bv(), clip, false)) {
4414 // content has been replaced,
4415 // so cursor might be invalid
4416 cur.pos() = cur.lastpos();
4417 cur.pit() = cur.lastpit();
4418 bvcur.setCursor(cur);
4422 // Let the cell handle normal text
4423 cell(cur.idx())->dispatch(cur, cmd);
4428 if (!tabularStackDirty()) {
4429 if (!cur.selIsMultiCell())
4430 cell(cur.idx())->dispatch(cur, cmd);
4433 if (theClipboard().isInternal()) {
4434 cur.recordUndoInset();
4435 pasteClipboard(cur);
4439 case LFUN_FONT_EMPH:
4440 case LFUN_FONT_BOLD:
4441 case LFUN_FONT_BOLDSYMBOL:
4442 case LFUN_FONT_ROMAN:
4443 case LFUN_FONT_NOUN:
4444 case LFUN_FONT_ITAL:
4445 case LFUN_FONT_FRAK:
4446 case LFUN_FONT_TYPEWRITER:
4447 case LFUN_FONT_SANS:
4448 case LFUN_TEXTSTYLE_APPLY:
4449 case LFUN_TEXTSTYLE_UPDATE:
4450 case LFUN_FONT_SIZE:
4451 case LFUN_FONT_UNDERLINE:
4452 case LFUN_FONT_STRIKEOUT:
4453 case LFUN_FONT_UNDERUNDERLINE:
4454 case LFUN_FONT_UNDERWAVE:
4456 case LFUN_PARAGRAPH_PARAMS_APPLY:
4457 case LFUN_PARAGRAPH_PARAMS:
4458 case LFUN_WORD_CAPITALIZE:
4459 case LFUN_WORD_UPCASE:
4460 case LFUN_WORD_LOWCASE:
4461 case LFUN_CHARS_TRANSPOSE:
4462 if (cur.selIsMultiCell()) {
4465 getSelection(cur, rs, re, cs, ce);
4466 Cursor tmpcur = cur;
4467 for (row_type r = rs; r <= re; ++r) {
4468 for (col_type c = cs; c <= ce; ++c) {
4469 // cursor follows cell:
4470 tmpcur.idx() = tabular.cellIndex(r, c);
4471 // select this cell only:
4474 tmpcur.resetAnchor();
4475 tmpcur.pit() = tmpcur.lastpit();
4476 tmpcur.pos() = tmpcur.top().lastpos();
4477 tmpcur.setCursor(tmpcur);
4478 tmpcur.setSelection();
4479 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4484 cell(cur.idx())->dispatch(cur, cmd);
4488 case LFUN_INSET_SETTINGS:
4489 // relay this lfun to Inset, not to the cell.
4490 Inset::doDispatch(cur, cmd);
4494 // we try to handle this event in the insets dispatch function.
4495 cell(cur.idx())->dispatch(cur, cmd);
4501 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4502 string const & argument, FuncStatus & status) const
4505 int action = Tabular::LAST_ACTION;
4507 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4508 if (tabularFeature[i].feature == s) {
4509 action = tabularFeature[i].action;
4513 if (action == Tabular::LAST_ACTION) {
4515 status.setUnknown(true);
4519 row_type sel_row_start = 0;
4520 row_type sel_row_end = 0;
4521 col_type sel_col_start = 0;
4522 col_type sel_col_end = 0;
4523 Tabular::ltType dummyltt;
4526 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4529 case Tabular::SET_PWIDTH:
4530 case Tabular::SET_MPWIDTH:
4531 case Tabular::SET_SPECIAL_COLUMN:
4532 case Tabular::SET_SPECIAL_MULTICOLUMN:
4533 case Tabular::APPEND_ROW:
4534 case Tabular::APPEND_COLUMN:
4535 case Tabular::DELETE_ROW:
4536 case Tabular::DELETE_COLUMN:
4537 case Tabular::COPY_ROW:
4538 case Tabular::COPY_COLUMN:
4539 case Tabular::SET_TOP_SPACE:
4540 case Tabular::SET_BOTTOM_SPACE:
4541 case Tabular::SET_INTERLINE_SPACE:
4545 case Tabular::SET_TABULAR_WIDTH:
4546 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4547 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4550 case Tabular::MOVE_COLUMN_RIGHT:
4551 case Tabular::MOVE_COLUMN_LEFT:
4552 case Tabular::MOVE_ROW_DOWN:
4553 case Tabular::MOVE_ROW_UP: {
4554 if (cur.selection()) {
4555 status.message(_("Selections not supported."));
4556 status.setEnabled(false);
4560 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4561 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4562 (action == Tabular::MOVE_COLUMN_LEFT &&
4563 tabular.cellColumn(cur.idx()) == 0) ||
4564 (action == Tabular::MOVE_ROW_DOWN &&
4565 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4566 (action == Tabular::MOVE_ROW_UP &&
4567 tabular.cellRow(cur.idx()) == 0)) {
4568 status.setEnabled(false);
4572 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4573 action == Tabular::MOVE_COLUMN_LEFT) {
4574 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4575 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4576 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4577 status.message(_("Multi-column in current or"
4578 " destination column."));
4579 status.setEnabled(false);
4584 if (action == Tabular::MOVE_ROW_DOWN ||
4585 action == Tabular::MOVE_ROW_UP) {
4586 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4587 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4588 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4589 status.message(_("Multi-row in current or"
4590 " destination row."));
4591 status.setEnabled(false);
4596 status.setEnabled(true);
4600 case Tabular::SET_DECIMAL_POINT:
4602 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4605 case Tabular::SET_MULTICOLUMN:
4606 case Tabular::UNSET_MULTICOLUMN:
4607 case Tabular::MULTICOLUMN:
4608 // If a row is set as longtable caption, it must not be allowed
4609 // to unset that this row is a multicolumn.
4610 status.setEnabled(sel_row_start == sel_row_end
4611 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4612 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4615 case Tabular::SET_MULTIROW:
4616 case Tabular::UNSET_MULTIROW:
4617 case Tabular::MULTIROW:
4618 // If a row is set as longtable caption, it must not be allowed
4619 // to unset that this row is a multirow.
4620 status.setEnabled(sel_col_start == sel_col_end
4621 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4622 status.setOnOff(tabular.isMultiRow(cur.idx()));
4625 case Tabular::SET_ALL_LINES:
4626 case Tabular::UNSET_ALL_LINES:
4627 case Tabular::SET_BORDER_LINES:
4628 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4631 case Tabular::SET_LINE_TOP:
4632 case Tabular::SET_LINE_BOTTOM:
4633 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4636 case Tabular::SET_LINE_LEFT:
4637 case Tabular::SET_LINE_RIGHT:
4638 status.setEnabled(!tabular.use_booktabs
4639 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4642 case Tabular::TOGGLE_LINE_TOP:
4643 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4644 status.setOnOff(tabular.topLine(cur.idx()));
4647 case Tabular::TOGGLE_LINE_BOTTOM:
4648 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4649 status.setOnOff(tabular.bottomLine(cur.idx()));
4652 case Tabular::TOGGLE_LINE_LEFT:
4653 status.setEnabled(!tabular.use_booktabs
4654 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4655 status.setOnOff(tabular.leftLine(cur.idx()));
4658 case Tabular::TOGGLE_LINE_RIGHT:
4659 status.setEnabled(!tabular.use_booktabs
4660 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4661 status.setOnOff(tabular.rightLine(cur.idx()));
4664 // multirow cells only inherit the alignment of the column if the column has
4665 // no width, otherwise they are left-aligned
4666 // therefore allow always left but right and center only if there is no width
4667 case Tabular::M_ALIGN_LEFT:
4669 case Tabular::ALIGN_LEFT:
4670 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4673 case Tabular::M_ALIGN_RIGHT:
4675 case Tabular::ALIGN_RIGHT:
4676 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4677 && !tabular.getPWidth(cur.idx()).zero()));
4678 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4681 case Tabular::M_ALIGN_CENTER:
4683 case Tabular::ALIGN_CENTER:
4684 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4685 && !tabular.getPWidth(cur.idx()).zero()));
4686 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4689 case Tabular::ALIGN_BLOCK:
4690 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4691 && !tabular.isMultiRow(cur.idx()));
4692 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4695 case Tabular::ALIGN_DECIMAL:
4696 status.setEnabled(!tabular.isMultiRow(cur.idx())
4697 && !tabular.isMultiColumn(cur.idx()));
4698 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4701 case Tabular::M_VALIGN_TOP:
4703 case Tabular::VALIGN_TOP:
4704 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4705 && !tabular.isMultiRow(cur.idx()));
4707 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4710 case Tabular::M_VALIGN_BOTTOM:
4712 case Tabular::VALIGN_BOTTOM:
4713 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4714 && !tabular.isMultiRow(cur.idx()));
4716 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4719 case Tabular::M_VALIGN_MIDDLE:
4721 case Tabular::VALIGN_MIDDLE:
4722 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4723 && !tabular.isMultiRow(cur.idx()));
4725 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4728 case Tabular::SET_LONGTABULAR:
4729 case Tabular::TOGGLE_LONGTABULAR:
4730 // setting as longtable is not allowed when table is inside a float
4731 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4732 || cur.innerInsetOfType(WRAP_CODE) != 0)
4733 status.setEnabled(false);
4735 status.setEnabled(true);
4736 status.setOnOff(tabular.is_long_tabular);
4739 case Tabular::UNSET_LONGTABULAR:
4740 status.setOnOff(!tabular.is_long_tabular);
4743 case Tabular::TOGGLE_ROTATE_TABULAR:
4744 case Tabular::SET_ROTATE_TABULAR:
4745 status.setOnOff(tabular.rotate != 0);
4748 case Tabular::TABULAR_VALIGN_TOP:
4749 status.setEnabled(tabular.tabular_width.zero());
4750 status.setOnOff(tabular.tabular_valignment
4751 == Tabular::LYX_VALIGN_TOP);
4753 case Tabular::TABULAR_VALIGN_MIDDLE:
4754 status.setEnabled(tabular.tabular_width.zero());
4755 status.setOnOff(tabular.tabular_valignment
4756 == Tabular::LYX_VALIGN_MIDDLE);
4758 case Tabular::TABULAR_VALIGN_BOTTOM:
4759 status.setEnabled(tabular.tabular_width.zero());
4760 status.setOnOff(tabular.tabular_valignment
4761 == Tabular::LYX_VALIGN_BOTTOM);
4764 case Tabular::LONGTABULAR_ALIGN_LEFT:
4765 status.setOnOff(tabular.longtabular_alignment
4766 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4768 case Tabular::LONGTABULAR_ALIGN_CENTER:
4769 status.setOnOff(tabular.longtabular_alignment
4770 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4772 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4773 status.setOnOff(tabular.longtabular_alignment
4774 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4777 case Tabular::UNSET_ROTATE_TABULAR:
4778 status.setOnOff(tabular.rotate == 0);
4781 case Tabular::TOGGLE_ROTATE_CELL:
4782 case Tabular::SET_ROTATE_CELL:
4783 status.setOnOff(!oneCellHasRotationState(false,
4784 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4787 case Tabular::UNSET_ROTATE_CELL:
4788 status.setOnOff(!oneCellHasRotationState(true,
4789 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4792 case Tabular::SET_USEBOX:
4793 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4796 // every row can only be one thing:
4797 // either a footer or header
4798 case Tabular::SET_LTFIRSTHEAD:
4799 status.setEnabled(sel_row_start == sel_row_end);
4800 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4803 case Tabular::UNSET_LTFIRSTHEAD:
4804 status.setEnabled(sel_row_start == sel_row_end);
4805 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4808 case Tabular::SET_LTHEAD:
4809 status.setEnabled(sel_row_start == sel_row_end);
4810 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4813 case Tabular::UNSET_LTHEAD:
4814 status.setEnabled(sel_row_start == sel_row_end);
4815 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4818 case Tabular::SET_LTFOOT:
4819 status.setEnabled(sel_row_start == sel_row_end);
4820 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4823 case Tabular::UNSET_LTFOOT:
4824 status.setEnabled(sel_row_start == sel_row_end);
4825 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4828 case Tabular::SET_LTLASTFOOT:
4829 status.setEnabled(sel_row_start == sel_row_end);
4830 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4833 case Tabular::UNSET_LTLASTFOOT:
4834 status.setEnabled(sel_row_start == sel_row_end);
4835 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4838 case Tabular::SET_LTNEWPAGE:
4839 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4841 case Tabular::UNSET_LTNEWPAGE:
4842 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4845 // only one row in head/firsthead/foot/lasthead can be the caption
4846 // and a multirow cannot be set as caption
4847 case Tabular::SET_LTCAPTION:
4848 status.setEnabled(sel_row_start == sel_row_end
4849 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4850 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4851 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4852 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4853 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4854 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4855 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4856 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4857 && !tabular.isMultiRow(sel_row_start));
4858 status.setOnOff(tabular.ltCaption(sel_row_start));
4861 case Tabular::UNSET_LTCAPTION:
4862 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4865 case Tabular::TOGGLE_LTCAPTION:
4866 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4867 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4868 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4869 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4870 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4871 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4872 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4873 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4874 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4875 status.setOnOff(tabular.ltCaption(sel_row_start));
4878 case Tabular::TOGGLE_BOOKTABS:
4879 case Tabular::SET_BOOKTABS:
4880 status.setOnOff(tabular.use_booktabs);
4883 case Tabular::UNSET_BOOKTABS:
4884 status.setOnOff(!tabular.use_booktabs);
4889 status.setEnabled(false);
4896 // function sets an object as defined in FuncStatus.h:
4897 // states OK, Unknown, Disabled, On, Off.
4898 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4899 FuncStatus & status) const
4901 switch (cmd.action()) {
4902 case LFUN_INSET_MODIFY:
4903 if (cmd.getArg(0) != "tabular")
4905 if (cmd.getArg(1) == "for-dialog") {
4906 // The dialog is asking the status of a command
4907 if (&cur.inset() != this)
4909 string action = cmd.getArg(2);
4910 string arg = cmd.getLongArg(3);
4911 return getFeatureStatus(cur, action, arg, status);
4913 // We always enable the lfun if it is coming from the dialog
4914 // because the dialog makes sure all the settings are valid,
4915 // even though the first argument might not be valid now.
4916 status.setEnabled(true);
4920 case LFUN_TABULAR_FEATURE: {
4921 if (&cur.inset() != this)
4923 string action = cmd.getArg(0);
4924 string arg = cmd.getLongArg(1);
4925 return getFeatureStatus(cur, action, arg, status);
4928 case LFUN_CAPTION_INSERT: {
4929 // caption is only allowed in caption cell of longtable
4930 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4931 status.setEnabled(false);
4934 // only standard caption is allowed
4935 string arg = cmd.getArg(0);
4936 if (!arg.empty() && arg != "Standard") {
4937 status.setEnabled(false);
4940 // check if there is already a caption
4941 bool have_caption = false;
4942 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4943 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4944 ParagraphList::const_iterator pend = itc.paragraphs().end();
4945 for (; pit != pend; ++pit) {
4946 InsetList::const_iterator it = pit->insetList().begin();
4947 InsetList::const_iterator end = pit->insetList().end();
4948 for (; it != end; ++it) {
4949 if (it->inset->lyxCode() == CAPTION_CODE) {
4950 have_caption = true;
4955 status.setEnabled(!have_caption);
4959 // These are only enabled inside tabular
4960 case LFUN_CELL_BACKWARD:
4961 case LFUN_CELL_FORWARD:
4962 status.setEnabled(true);
4965 // disable these with multiple cells selected
4966 case LFUN_INSET_INSERT:
4967 case LFUN_TABULAR_INSERT:
4968 case LFUN_FLEX_INSERT:
4969 case LFUN_FLOAT_INSERT:
4970 case LFUN_FLOAT_WIDE_INSERT:
4971 case LFUN_FOOTNOTE_INSERT:
4972 case LFUN_MARGINALNOTE_INSERT:
4973 case LFUN_MATH_INSERT:
4974 case LFUN_MATH_MODE:
4975 case LFUN_MATH_MUTATE:
4976 case LFUN_MATH_DISPLAY:
4977 case LFUN_NOTE_INSERT:
4978 case LFUN_ARGUMENT_INSERT:
4979 case LFUN_BOX_INSERT:
4980 case LFUN_BRANCH_INSERT:
4981 case LFUN_PHANTOM_INSERT:
4982 case LFUN_WRAP_INSERT:
4983 case LFUN_PREVIEW_INSERT:
4984 case LFUN_ERT_INSERT: {
4985 if (cur.selIsMultiCell()) {
4986 status.setEnabled(false);
4989 return cell(cur.idx())->getStatus(cur, cmd, status);
4992 // disable in non-fixed-width cells
4993 case LFUN_PARAGRAPH_BREAK:
4994 // multirow does not allow paragraph breaks
4995 if (tabular.isMultiRow(cur.idx())) {
4996 status.setEnabled(false);
5000 case LFUN_NEWLINE_INSERT: {
5001 if (tabular.getPWidth(cur.idx()).zero()) {
5002 status.setEnabled(false);
5005 return cell(cur.idx())->getStatus(cur, cmd, status);
5008 case LFUN_NEWPAGE_INSERT:
5009 status.setEnabled(false);
5013 if (tabularStackDirty() && theClipboard().isInternal()) {
5014 if (cur.selIsMultiCell()) {
5017 getSelection(cur, rs, re, cs, ce);
5018 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5019 && paste_tabular->nrows() == re - rs + 1)
5020 status.setEnabled(true);
5022 status.setEnabled(false);
5023 status.message(_("Selection size should match clipboard content."));
5026 status.setEnabled(true);
5029 return cell(cur.idx())->getStatus(cur, cmd, status);
5031 case LFUN_INSET_SETTINGS:
5032 // relay this lfun to Inset, not to the cell.
5033 return Inset::getStatus(cur, cmd, status);
5036 // we try to handle this event in the insets dispatch function.
5037 return cell(cur.idx())->getStatus(cur, cmd, status);
5043 Inset::DisplayType InsetTabular::display() const
5045 if (tabular.is_long_tabular) {
5046 switch (tabular.longtabular_alignment) {
5047 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5049 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5051 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5061 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5063 tabular.latex(os, runparams);
5067 int InsetTabular::plaintext(odocstringstream & os,
5068 OutputParams const & runparams, size_t max_length) const
5070 os << '\n'; // output table on a new line
5071 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5072 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5073 return PLAINTEXT_NEWLINE;
5077 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5082 // FIXME: Why not pass a proper DocIterator here?
5084 // if the table is inside a float it doesn't need the informaltable
5085 // wrapper. Search for it.
5086 for (master = owner(); master; master = master->owner())
5087 if (master->lyxCode() == FLOAT_CODE)
5092 os << "<informaltable>";
5095 ret += tabular.docbook(os, runparams);
5097 os << "</informaltable>";
5104 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5106 return tabular.xhtml(xs, rp);
5110 void InsetTabular::validate(LaTeXFeatures & features) const
5112 tabular.validate(features);
5114 // It'd be better to be able to get this from an InsetLayout, but at present
5115 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5116 if (features.runparams().flavor == OutputParams::HTML)
5117 features.addCSSSnippet(
5118 "table { border: 1px solid black; display: inline-block; }\n"
5119 "td { border: 1px solid black; padding: 0.5ex; }");
5123 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5125 return tabular.cellInset(idx);
5129 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5131 return tabular.cellInset(idx);
5135 void InsetTabular::cursorPos(BufferView const & bv,
5136 CursorSlice const & sl, bool boundary, int & x, int & y) const
5138 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5140 // y offset correction
5141 y += cellYPos(sl.idx());
5142 y += tabular.textVOffset(sl.idx());
5143 y += offset_valign_;
5145 // x offset correction
5146 x += cellXPos(sl.idx());
5147 x += tabular.textHOffset(sl.idx());
5148 x += ADD_TO_TABULAR_WIDTH;
5152 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5156 Inset const & inset = *tabular.cellInset(cell);
5157 Point o = bv.coordCache().getInsets().xy(&inset);
5158 int const xbeg = o.x_ - tabular.textHOffset(cell);
5159 int const xend = xbeg + tabular.cellWidth(cell);
5160 row_type const row = tabular.cellRow(cell);
5161 int const ybeg = o.y_ - tabular.rowAscent(row)
5162 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5163 int const yend = ybeg + tabular.cellHeight(cell);
5175 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5176 // << " ybeg=" << ybeg << " yend=" << yend
5177 // << " xx=" << xx << " yy=" << yy
5178 // << " dist=" << xx + yy << endl;
5183 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5185 //lyxerr << "InsetTabular::editXY: " << this << endl;
5186 cur.selection(false);
5188 cur.idx() = getNearestCell(cur.bv(), x, y);
5189 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5193 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5195 cur.idx() = getNearestCell(cur.bv(), x, y);
5196 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5200 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5202 idx_type idx_min = 0;
5203 int dist_min = numeric_limits<int>::max();
5204 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5205 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5206 int const d = dist(bv, i, x, y);
5217 int InsetTabular::cellYPos(idx_type const cell) const
5219 row_type row = tabular.cellRow(cell);
5221 for (row_type r = 0; r < row; ++r)
5222 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5223 + tabular.interRowSpace(r + 1);
5228 int InsetTabular::cellXPos(idx_type const cell) const
5230 col_type col = tabular.cellColumn(cell);
5232 for (col_type c = 0; c < col; ++c)
5233 lx += tabular.column_info[c].width;
5238 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5240 row_type const row = tabular.cellRow(cur.idx());
5241 col_type const col = tabular.cellColumn(cur.idx());
5243 if (isRightToLeft(cur)) {
5244 if (tabular.cellColumn(cur.idx()) == 0) {
5245 if (row == tabular.nrows() - 1)
5247 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5252 cur.idx() = tabular.getLastCellInRow(row - 1);
5254 cur.idx() = tabular.cellIndex(row, col - 1);
5257 if (tabular.isLastCell(cur.idx()))
5259 if (cur.idx() == tabular.getLastCellInRow(row))
5260 cur.idx() = tabular.cellIndex(row + 1, 0);
5262 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5263 cur.idx() = tabular.cellIndex(row, colnextcell);
5267 cur.boundary(false);
5269 if (cur.selIsMultiCell()) {
5270 cur.pit() = cur.lastpit();
5271 cur.pos() = cur.lastpos();
5278 // in visual mode, place cursor at extreme left or right
5280 switch(entry_from) {
5282 case ENTRY_DIRECTION_RIGHT:
5283 cur.posVisToRowExtremity(false /* !left */);
5285 case ENTRY_DIRECTION_LEFT:
5286 cur.posVisToRowExtremity(true /* left */);
5288 case ENTRY_DIRECTION_IGNORE:
5289 // nothing to do in this case
5293 cur.setCurrentFont();
5297 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5299 row_type const row = tabular.cellRow(cur.idx());
5300 col_type const col = tabular.cellColumn(cur.idx());
5302 if (isRightToLeft(cur)) {
5303 if (cur.idx() == tabular.getLastCellInRow(row)) {
5306 cur.idx() = tabular.getFirstCellInRow(row);
5307 cur.idx() = tabular.cellAbove(cur.idx());
5309 if (tabular.isLastCell(cur.idx()))
5311 if (cur.idx() == tabular.getLastCellInRow(row))
5312 cur.idx() = tabular.cellIndex(row + 1, 0);
5314 cur.idx() = tabular.cellIndex(row, col + 1);
5317 if (cur.idx() == 0) // first cell
5320 cur.idx() = tabular.getLastCellInRow(row - 1);
5322 cur.idx() = tabular.cellIndex(row, col - 1);
5325 if (cur.selIsMultiCell()) {
5326 cur.pit() = cur.lastpit();
5327 cur.pos() = cur.lastpos();
5331 cur.pit() = cur.lastpit();
5332 cur.pos() = cur.lastpos();
5334 // in visual mode, place cursor at extreme left or right
5336 switch(entry_from) {
5338 case ENTRY_DIRECTION_RIGHT:
5339 cur.posVisToRowExtremity(false /* !left */);
5341 case ENTRY_DIRECTION_LEFT:
5342 cur.posVisToRowExtremity(true /* left */);
5344 case ENTRY_DIRECTION_IGNORE:
5345 // nothing to do in this case
5349 cur.setCurrentFont();
5353 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5355 cur.recordUndoInset(this);
5357 istringstream is(argument);
5360 size_t safe_guard = 0;
5365 if (safe_guard > 1000) {
5366 LYXERR0("parameter max count reached!");
5370 Tabular::Feature action = Tabular::LAST_ACTION;
5373 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5374 if (s != tabularFeature[i].feature)
5376 action = tabularFeature[i].action;
5379 if (action == Tabular::LAST_ACTION) {
5380 LYXERR0("Feature not found " << s);
5384 if (tabularFeature[i].need_value)
5386 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5387 tabularFeatures(cur, action, val);
5392 static void checkLongtableSpecial(Tabular::ltType & ltt,
5393 string const & special, bool & flag)
5395 if (special == "dl_above") {
5398 } else if (special == "dl_below") {
5399 ltt.bottomDL = flag;
5401 } else if (special == "empty") {
5411 bool InsetTabular::oneCellHasRotationState(bool rotated,
5412 row_type row_start, row_type row_end,
5413 col_type col_start, col_type col_end) const
5415 for (row_type r = row_start; r <= row_end; ++r)
5416 for (col_type c = col_start; c <= col_end; ++c)
5418 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5421 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5428 void InsetTabular::tabularFeatures(Cursor & cur,
5429 Tabular::Feature feature, string const & value)
5431 col_type sel_col_start;
5432 col_type sel_col_end;
5433 row_type sel_row_start;
5434 row_type sel_row_end;
5435 bool setLines = false;
5436 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5437 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5441 case Tabular::M_ALIGN_LEFT:
5442 case Tabular::ALIGN_LEFT:
5443 setAlign = LYX_ALIGN_LEFT;
5446 case Tabular::M_ALIGN_RIGHT:
5447 case Tabular::ALIGN_RIGHT:
5448 setAlign = LYX_ALIGN_RIGHT;
5451 case Tabular::M_ALIGN_CENTER:
5452 case Tabular::ALIGN_CENTER:
5453 setAlign = LYX_ALIGN_CENTER;
5456 case Tabular::ALIGN_BLOCK:
5457 setAlign = LYX_ALIGN_BLOCK;
5460 case Tabular::ALIGN_DECIMAL:
5461 setAlign = LYX_ALIGN_DECIMAL;
5464 case Tabular::M_VALIGN_TOP:
5465 case Tabular::VALIGN_TOP:
5466 setVAlign = Tabular::LYX_VALIGN_TOP;
5469 case Tabular::M_VALIGN_BOTTOM:
5470 case Tabular::VALIGN_BOTTOM:
5471 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5474 case Tabular::M_VALIGN_MIDDLE:
5475 case Tabular::VALIGN_MIDDLE:
5476 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5483 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5484 row_type const row = tabular.cellRow(cur.idx());
5485 col_type const column = tabular.cellColumn(cur.idx());
5487 Tabular::ltType ltt;
5491 case Tabular::SET_TABULAR_WIDTH:
5492 tabular.setTabularWidth(Length(value));
5495 case Tabular::SET_PWIDTH: {
5496 Length const len(value);
5497 tabular.setColumnPWidth(cur, cur.idx(), len);
5499 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5500 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5504 case Tabular::SET_MPWIDTH:
5505 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5508 case Tabular::SET_MROFFSET:
5509 tabular.setMROffset(cur, cur.idx(), Length(value));
5512 case Tabular::SET_SPECIAL_COLUMN:
5513 case Tabular::SET_SPECIAL_MULTICOLUMN:
5514 if (value == "none")
5515 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5517 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5520 case Tabular::APPEND_ROW:
5521 // append the row into the tabular
5522 tabular.appendRow(row);
5525 case Tabular::APPEND_COLUMN:
5526 // append the column into the tabular
5527 tabular.appendColumn(column);
5528 cur.idx() = tabular.cellIndex(row, column);
5531 case Tabular::DELETE_ROW:
5532 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5533 for (col_type c = 0; c < tabular.ncols(); c++)
5534 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5535 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5538 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5539 tabular.deleteRow(sel_row_start);
5540 if (sel_row_start >= tabular.nrows())
5542 cur.idx() = tabular.cellIndex(sel_row_start, column);
5545 cur.selection(false);
5548 case Tabular::DELETE_COLUMN:
5549 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5550 for (row_type r = 0; r < tabular.nrows(); r++)
5551 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5552 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5555 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5556 for (row_type r = 0; r < tabular.nrows(); r++)
5557 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5558 tabular.leftLine(tabular.cellIndex(r, 0)));
5561 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5562 tabular.deleteColumn(sel_col_start);
5563 if (sel_col_start >= tabular.ncols())
5565 cur.idx() = tabular.cellIndex(row, sel_col_start);
5568 cur.selection(false);
5571 case Tabular::COPY_ROW:
5572 tabular.copyRow(row);
5575 case Tabular::COPY_COLUMN:
5576 tabular.copyColumn(column);
5577 cur.idx() = tabular.cellIndex(row, column);
5580 case Tabular::MOVE_COLUMN_RIGHT:
5581 tabular.moveColumn(column, Tabular::RIGHT);
5582 cur.idx() = tabular.cellIndex(row, column + 1);
5585 case Tabular::MOVE_COLUMN_LEFT:
5586 tabular.moveColumn(column, Tabular::LEFT);
5587 cur.idx() = tabular.cellIndex(row, column - 1);
5590 case Tabular::MOVE_ROW_DOWN:
5591 tabular.moveRow(row, Tabular::DOWN);
5592 cur.idx() = tabular.cellIndex(row + 1, column);
5595 case Tabular::MOVE_ROW_UP:
5596 tabular.moveRow(row, Tabular::UP);
5597 cur.idx() = tabular.cellIndex(row - 1, column);
5600 case Tabular::SET_LINE_TOP:
5601 case Tabular::TOGGLE_LINE_TOP: {
5602 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5603 ? (value == "true") : !tabular.topLine(cur.idx());
5604 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5605 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5606 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5610 case Tabular::SET_LINE_BOTTOM:
5611 case Tabular::TOGGLE_LINE_BOTTOM: {
5612 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5613 ? (value == "true") : !tabular.bottomLine(cur.idx());
5614 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5615 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5616 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5620 case Tabular::SET_LINE_LEFT:
5621 case Tabular::TOGGLE_LINE_LEFT: {
5622 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5623 ? (value == "true") : !tabular.leftLine(cur.idx());
5624 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5625 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5626 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5630 case Tabular::SET_LINE_RIGHT:
5631 case Tabular::TOGGLE_LINE_RIGHT: {
5632 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5633 ? (value == "true") : !tabular.rightLine(cur.idx());
5634 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5635 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5636 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5640 case Tabular::M_ALIGN_LEFT:
5641 case Tabular::M_ALIGN_RIGHT:
5642 case Tabular::M_ALIGN_CENTER:
5643 case Tabular::ALIGN_LEFT:
5644 case Tabular::ALIGN_RIGHT:
5645 case Tabular::ALIGN_CENTER:
5646 case Tabular::ALIGN_BLOCK:
5647 case Tabular::ALIGN_DECIMAL:
5648 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5649 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5650 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5651 !tabular.getPWidth(c).zero());
5654 case Tabular::M_VALIGN_TOP:
5655 case Tabular::M_VALIGN_BOTTOM:
5656 case Tabular::M_VALIGN_MIDDLE:
5658 case Tabular::VALIGN_TOP:
5659 case Tabular::VALIGN_BOTTOM:
5660 case Tabular::VALIGN_MIDDLE:
5661 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5662 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5663 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5666 case Tabular::SET_MULTICOLUMN: {
5667 if (!cur.selection()) {
5668 // just multicol for one single cell
5669 // check whether we are completely in a multicol
5670 if (!tabular.isMultiColumn(cur.idx()))
5671 tabular.setMultiColumn(cur.idx(), 1,
5672 tabular.rightLine(cur.idx()));
5675 // we have a selection so this means we just add all these
5676 // cells to form a multicolumn cell
5677 idx_type const s_start = cur.selBegin().idx();
5678 row_type const col_start = tabular.cellColumn(s_start);
5679 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5680 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5681 tabular.rightLine(cur.selEnd().idx()));
5684 cur.selection(false);
5688 case Tabular::UNSET_MULTICOLUMN: {
5689 if (!cur.selection()) {
5690 if (tabular.isMultiColumn(cur.idx()))
5691 tabular.unsetMultiColumn(cur.idx());
5696 case Tabular::MULTICOLUMN: {
5697 if (!cur.selection()) {
5698 if (tabular.isMultiColumn(cur.idx()))
5699 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5701 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5705 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5706 row_type const r = sel_row_start;
5707 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5708 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5711 // If the selection contains at least one singlecol cell
5712 // or multiple multicol cells,
5713 // we assume the user will merge is to a single multicol
5715 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5717 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5721 case Tabular::SET_MULTIROW: {
5722 if (!cur.selection()) {
5723 // just multirow for one single cell
5724 // check whether we are completely in a multirow
5725 if (!tabular.isMultiRow(cur.idx()))
5726 tabular.setMultiRow(cur.idx(), 1,
5727 tabular.bottomLine(cur.idx()),
5728 tabular.getAlignment(cur.idx()));
5731 // we have a selection so this means we just add all this
5732 // cells to form a multirow cell
5733 idx_type const s_start = cur.selBegin().idx();
5734 row_type const row_start = tabular.cellRow(s_start);
5735 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5736 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5737 tabular.bottomLine(cur.selEnd().idx()),
5738 tabular.getAlignment(cur.selEnd().idx()));
5741 cur.selection(false);
5745 case Tabular::UNSET_MULTIROW: {
5746 if (!cur.selection()) {
5747 if (tabular.isMultiRow(cur.idx()))
5748 tabular.unsetMultiRow(cur.idx());
5753 case Tabular::MULTIROW: {
5754 if (!cur.selection()) {
5755 if (tabular.isMultiRow(cur.idx()))
5756 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5758 tabularFeatures(cur, Tabular::SET_MULTIROW);
5762 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5763 col_type const c = sel_col_start;
5764 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5765 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5768 // If the selection contains at least one singlerow cell
5769 // or multiple multirow cells,
5770 // we assume the user will merge is to a single multirow
5772 tabularFeatures(cur, Tabular::SET_MULTIROW);
5774 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5778 case Tabular::SET_ALL_LINES:
5780 case Tabular::UNSET_ALL_LINES:
5781 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5782 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5783 idx_type const cell = tabular.cellIndex(r, c);
5784 tabular.setTopLine(cell, setLines);
5785 tabular.setBottomLine(cell, setLines);
5786 tabular.setRightLine(cell, setLines);
5787 tabular.setLeftLine(cell, setLines);
5791 case Tabular::SET_BORDER_LINES:
5792 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5793 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5794 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5796 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5797 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5798 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5802 case Tabular::TOGGLE_LONGTABULAR:
5803 if (tabular.is_long_tabular)
5804 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5806 tabular.is_long_tabular = true;
5809 case Tabular::SET_LONGTABULAR:
5810 tabular.is_long_tabular = true;
5813 case Tabular::UNSET_LONGTABULAR:
5814 for (row_type r = 0; r < tabular.nrows(); ++r) {
5815 if (tabular.ltCaption(r)) {
5816 cur.idx() = tabular.cellIndex(r, 0);
5819 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5822 tabular.is_long_tabular = false;
5825 case Tabular::SET_ROTATE_TABULAR:
5826 tabular.rotate = convert<int>(value);
5829 case Tabular::UNSET_ROTATE_TABULAR:
5833 case Tabular::TOGGLE_ROTATE_TABULAR:
5834 // when pressing the rotate button we default to 90° rotation
5835 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5838 case Tabular::TABULAR_VALIGN_TOP:
5839 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5842 case Tabular::TABULAR_VALIGN_MIDDLE:
5843 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5846 case Tabular::TABULAR_VALIGN_BOTTOM:
5847 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5850 case Tabular::LONGTABULAR_ALIGN_LEFT:
5851 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5854 case Tabular::LONGTABULAR_ALIGN_CENTER:
5855 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5858 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5859 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5864 case Tabular::SET_ROTATE_CELL:
5865 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5866 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5867 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5870 case Tabular::UNSET_ROTATE_CELL:
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.setRotateCell(tabular.cellIndex(r, c), 0);
5876 case Tabular::TOGGLE_ROTATE_CELL:
5878 bool oneNotRotated = oneCellHasRotationState(false,
5879 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5881 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5882 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5883 // when pressing the rotate cell button we default to 90° rotation
5885 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5887 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5892 case Tabular::SET_USEBOX: {
5893 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5894 if (val == tabular.getUsebox(cur.idx()))
5895 val = Tabular::BOX_NONE;
5896 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5897 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5898 tabular.setUsebox(tabular.cellIndex(r, c), val);
5902 case Tabular::UNSET_LTFIRSTHEAD:
5904 case Tabular::SET_LTFIRSTHEAD:
5905 tabular.getRowOfLTFirstHead(row, ltt);
5906 checkLongtableSpecial(ltt, value, flag);
5907 tabular.setLTHead(row, flag, ltt, true);
5910 case Tabular::UNSET_LTHEAD:
5912 case Tabular::SET_LTHEAD:
5913 tabular.getRowOfLTHead(row, ltt);
5914 checkLongtableSpecial(ltt, value, flag);
5915 tabular.setLTHead(row, flag, ltt, false);
5918 case Tabular::UNSET_LTFOOT:
5920 case Tabular::SET_LTFOOT:
5921 tabular.getRowOfLTFoot(row, ltt);
5922 checkLongtableSpecial(ltt, value, flag);
5923 tabular.setLTFoot(row, flag, ltt, false);
5926 case Tabular::UNSET_LTLASTFOOT:
5928 case Tabular::SET_LTLASTFOOT:
5929 tabular.getRowOfLTLastFoot(row, ltt);
5930 checkLongtableSpecial(ltt, value, flag);
5931 tabular.setLTFoot(row, flag, ltt, true);
5934 case Tabular::UNSET_LTNEWPAGE:
5936 case Tabular::SET_LTNEWPAGE:
5937 tabular.setLTNewPage(row, flag);
5940 case Tabular::SET_LTCAPTION: {
5941 if (tabular.ltCaption(row))
5943 cur.idx() = tabular.setLTCaption(row, true);
5946 cur.selection(false);
5947 // If a row is set as caption, then also insert
5948 // a caption. Otherwise the LaTeX output is broken.
5949 // Select cell if it is non-empty
5950 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5951 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5952 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5956 case Tabular::UNSET_LTCAPTION: {
5957 if (!tabular.ltCaption(row))
5959 cur.idx() = tabular.setLTCaption(row, false);
5962 cur.selection(false);
5963 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5964 if (lyx::getStatus(fr).enabled())
5969 case Tabular::TOGGLE_LTCAPTION: {
5970 if (tabular.ltCaption(row))
5971 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5973 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5977 case Tabular::TOGGLE_BOOKTABS:
5978 tabular.use_booktabs = !tabular.use_booktabs;
5981 case Tabular::SET_BOOKTABS:
5982 tabular.use_booktabs = true;
5985 case Tabular::UNSET_BOOKTABS:
5986 tabular.use_booktabs = false;
5989 case Tabular::SET_TOP_SPACE: {
5991 if (value == "default")
5992 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5993 tabular.row_info[r].top_space_default = true;
5994 else if (value == "none")
5995 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5996 tabular.row_info[r].top_space_default = false;
5997 tabular.row_info[r].top_space = len;
5999 else if (isValidLength(value, &len))
6000 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6001 tabular.row_info[r].top_space_default = false;
6002 tabular.row_info[r].top_space = len;
6007 case Tabular::SET_BOTTOM_SPACE: {
6009 if (value == "default")
6010 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6011 tabular.row_info[r].bottom_space_default = true;
6012 else if (value == "none")
6013 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6014 tabular.row_info[r].bottom_space_default = false;
6015 tabular.row_info[r].bottom_space = len;
6017 else if (isValidLength(value, &len))
6018 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6019 tabular.row_info[r].bottom_space_default = false;
6020 tabular.row_info[r].bottom_space = len;
6025 case Tabular::SET_INTERLINE_SPACE: {
6027 if (value == "default")
6028 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6029 tabular.row_info[r].interline_space_default = true;
6030 else if (value == "none")
6031 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6032 tabular.row_info[r].interline_space_default = false;
6033 tabular.row_info[r].interline_space = len;
6035 else if (isValidLength(value, &len))
6036 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6037 tabular.row_info[r].interline_space_default = false;
6038 tabular.row_info[r].interline_space = len;
6043 case Tabular::SET_DECIMAL_POINT:
6044 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6045 tabular.column_info[c].decimal_point = from_utf8(value);
6048 // dummy stuff just to avoid warnings
6049 case Tabular::LAST_ACTION:
6055 bool InsetTabular::copySelection(Cursor & cur)
6057 if (!cur.selection())
6062 getSelection(cur, rs, re, cs, ce);
6064 paste_tabular.reset(new Tabular(tabular));
6066 for (row_type r = 0; r < rs; ++r)
6067 paste_tabular->deleteRow(0);
6069 row_type const rows = re - rs + 1;
6070 while (paste_tabular->nrows() > rows)
6071 paste_tabular->deleteRow(rows);
6073 for (col_type c = 0; c < cs; ++c)
6074 paste_tabular->deleteColumn(0);
6076 col_type const columns = ce - cs + 1;
6077 while (paste_tabular->ncols() > columns)
6078 paste_tabular->deleteColumn(columns);
6080 paste_tabular->setBuffer(tabular.buffer());
6082 odocstringstream os;
6083 OutputParams const runparams(0);
6084 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6085 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6086 cap::copySelection(cur, os.str());
6088 // mark tabular stack dirty
6089 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6090 // when we (hopefully) have a one-for-all paste mechanism.
6091 // This must be called after cap::copySelection.
6092 dirtyTabularStack(true);
6098 bool InsetTabular::pasteClipboard(Cursor & cur)
6102 col_type actcol = tabular.cellColumn(cur.idx());
6103 row_type actrow = tabular.cellRow(cur.idx());
6105 if (cur.selIsMultiCell()) {
6108 getSelection(cur, actrow, re, actcol, ce);
6111 for (row_type r1 = 0, r2 = actrow;
6112 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6114 for (col_type c1 = 0, c2 = actcol;
6115 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6117 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6118 tabular.isPartOfMultiColumn(r2, c2))
6120 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6124 if (tabular.isPartOfMultiColumn(r2, c2)) {
6128 shared_ptr<InsetTableCell> inset(
6129 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6130 tabular.setCellInset(r2, c2, inset);
6131 // FIXME?: why do we need to do this explicitly? (EL)
6132 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6134 // FIXME: change tracking (MG)
6135 inset->setChange(Change(buffer().params().track_changes ?
6136 Change::INSERTED : Change::UNCHANGED));
6144 void InsetTabular::cutSelection(Cursor & cur)
6146 if (!cur.selection())
6151 getSelection(cur, rs, re, cs, ce);
6152 for (row_type r = rs; r <= re; ++r) {
6153 for (col_type c = cs; c <= ce; ++c) {
6154 shared_ptr<InsetTableCell> t
6155 = cell(tabular.cellIndex(r, c));
6156 if (buffer().params().track_changes)
6157 // FIXME: Change tracking (MG)
6158 t->setChange(Change(Change::DELETED));
6164 // cursor position might be invalid now
6165 if (cur.pit() > cur.lastpit())
6166 cur.pit() = cur.lastpit();
6167 if (cur.pos() > cur.lastpos())
6168 cur.pos() = cur.lastpos();
6169 cur.clearSelection();
6173 bool InsetTabular::isRightToLeft(Cursor & cur) const
6175 // LASSERT: It might be better to abandon this Buffer.
6176 LASSERT(cur.depth() > 1, return false);
6177 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6178 pos_type const parentpos = cur[cur.depth() - 2].pos();
6179 return parentpar.getFontSettings(buffer().params(),
6180 parentpos).language()->rightToLeft();
6184 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6187 LASSERT(stidx <= enidx, return docstring());
6189 col_type const col1 = tabular.cellColumn(stidx);
6190 col_type const col2 = tabular.cellColumn(enidx);
6191 row_type const row1 = tabular.cellRow(stidx);
6192 row_type const row2 = tabular.cellRow(enidx);
6194 for (col_type col = col1; col <= col2; col++)
6195 for (row_type row = row1; row <= row2; row++) {
6200 retval += tabular.cellInset(row, col)->asString(intoInsets);
6206 void InsetTabular::getSelection(Cursor & cur,
6207 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6209 CursorSlice const & beg = cur.selBegin();
6210 CursorSlice const & end = cur.selEnd();
6211 cs = tabular.cellColumn(beg.idx());
6212 ce = tabular.cellColumn(end.idx());
6216 rs = tabular.cellRow(beg.idx());
6217 re = tabular.cellRow(end.idx());
6223 Text * InsetTabular::getText(int idx) const
6225 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6229 void InsetTabular::setChange(Change const & change)
6231 for (idx_type idx = 0; idx < nargs(); ++idx)
6232 cell(idx)->setChange(change);
6236 void InsetTabular::acceptChanges()
6238 for (idx_type idx = 0; idx < nargs(); ++idx)
6239 cell(idx)->acceptChanges();
6243 void InsetTabular::rejectChanges()
6245 for (idx_type idx = 0; idx < nargs(); ++idx)
6246 cell(idx)->rejectChanges();
6250 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6252 return tabular.getPWidth(cell).zero();
6256 bool InsetTabular::forcePlainLayout(idx_type cell) const
6258 return !tabular.getPWidth(cell).zero();
6262 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6265 if (buf.length() <= 0)
6270 col_type maxCols = 1;
6271 size_t const len = buf.length();
6275 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6283 maxCols = max(cols, maxCols);
6289 maxCols = max(cols, maxCols);
6295 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6296 loctab = paste_tabular.get();
6297 dirtyTabularStack(true);
6300 cell = bv.cursor().idx();
6301 ocol = tabular.cellColumn(cell);
6302 row = tabular.cellRow(cell);
6306 idx_type const cells = loctab->numberofcells;
6309 rows = loctab->nrows();
6310 col_type const columns = loctab->ncols();
6312 while (cell < cells && p < len && row < rows &&
6313 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6319 // we can only set this if we are not too far right
6320 if (cols < columns) {
6321 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6322 Font const font = bv.textMetrics(&inset->text()).
6324 inset->setText(buf.substr(op, p - op), font,
6325 buffer().params().track_changes);
6331 // we can only set this if we are not too far right
6332 if (cols < columns) {
6333 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6334 Font const font = bv.textMetrics(&inset->text()).
6336 inset->setText(buf.substr(op, p - op), font,
6337 buffer().params().track_changes);
6342 cell = loctab->cellIndex(row, cols);
6348 // check for the last cell if there is no trailing '\n'
6349 if (cell < cells && op < len) {
6350 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6351 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6352 inset->setText(buf.substr(op, len - op), font,
6353 buffer().params().track_changes);
6359 void InsetTabular::addPreview(DocIterator const & inset_pos,
6360 PreviewLoader & loader) const
6362 DocIterator cell_pos = inset_pos;
6364 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6365 for (row_type r = 0; r < tabular.nrows(); ++r) {
6366 for (col_type c = 0; c < tabular.ncols(); ++c) {
6367 cell_pos.top().idx() = tabular.cellIndex(r, c);
6368 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6374 bool InsetTabular::completionSupported(Cursor const & cur) const
6376 Cursor const & bvCur = cur.bv().cursor();
6377 if (&bvCur.inset() != this)
6379 return cur.text()->completionSupported(cur);
6383 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6385 return completionSupported(cur);
6389 bool InsetTabular::automaticInlineCompletion() const
6391 return lyxrc.completion_inline_text;
6395 bool InsetTabular::automaticPopupCompletion() const
6397 return lyxrc.completion_popup_text;
6401 bool InsetTabular::showCompletionCursor() const
6403 return lyxrc.completion_cursor_text;
6407 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6409 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6413 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6415 if (!completionSupported(cur))
6417 return cur.text()->completionPrefix(cur);
6421 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6423 if (!completionSupported(cur))
6426 return cur.text()->insertCompletion(cur, s, finished);
6430 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6431 Dimension & dim) const
6433 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6434 tm.completionPosAndDim(cur, x, y, dim);
6438 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6440 istringstream data(in);
6442 lex.setStream(data);
6449 if (!lex || token != "tabular") {
6450 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6454 // This is part of the inset proper that is usually swallowed
6455 // by Buffer::readInset
6457 if (!lex || token != "Tabular") {
6458 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6466 string InsetTabular::params2string(InsetTabular const & inset)
6469 data << "tabular" << ' ';
6471 data << "\\end_inset\n";
6476 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6478 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6479 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6480 if (!tabular.isPartOfMultiColumn(r,c) &&
6481 !tabular.isPartOfMultiRow(r,c))
6484 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6485 ParagraphList::iterator it = parlist.begin();
6486 ParagraphList::iterator const en = parlist.end();
6487 for (; it != en; ++it)
6488 it->setLayout(dc.plainLayout());