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"
64 #include "support/unique_ptr.h"
72 using namespace lyx::support;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 unique_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
108 TabularFeature tabularFeature[] =
110 // the SET/UNSET actions are used by the table dialog,
111 // the TOGGLE actions by the table toolbar buttons
112 // FIXME: these values have been hardcoded in InsetMathGrid and other
114 { Tabular::APPEND_ROW, "append-row", false },
115 { Tabular::APPEND_COLUMN, "append-column", false },
116 { Tabular::DELETE_ROW, "delete-row", false },
117 { Tabular::DELETE_COLUMN, "delete-column", false },
118 { Tabular::COPY_ROW, "copy-row", false },
119 { Tabular::COPY_COLUMN, "copy-column", false },
120 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
121 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
122 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
123 { Tabular::MOVE_ROW_UP, "move-row-up", false },
124 { Tabular::SET_LINE_TOP, "set-line-top", true },
125 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
126 { Tabular::SET_LINE_LEFT, "set-line-left", true },
127 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
128 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
129 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
130 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
131 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
132 { Tabular::ALIGN_LEFT, "align-left", false },
133 { Tabular::ALIGN_RIGHT, "align-right", false },
134 { Tabular::ALIGN_CENTER, "align-center", false },
135 { Tabular::ALIGN_BLOCK, "align-block", false },
136 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
137 { Tabular::VALIGN_TOP, "valign-top", false },
138 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
139 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
140 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
141 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
142 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
143 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
144 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
145 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
146 { Tabular::MULTICOLUMN, "multicolumn", false },
147 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
148 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
149 { Tabular::MULTIROW, "multirow", false },
150 { Tabular::SET_MULTIROW, "set-multirow", false },
151 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
152 { Tabular::SET_MROFFSET, "set-mroffset", true },
153 { Tabular::SET_ALL_LINES, "set-all-lines", false },
154 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
155 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
156 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
157 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
158 { Tabular::SET_PWIDTH, "set-pwidth", true },
159 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
160 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
161 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
162 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
163 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
164 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
165 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
166 { Tabular::SET_USEBOX, "set-usebox", true },
167 { Tabular::SET_LTHEAD, "set-lthead", true },
168 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
169 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
170 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
171 { Tabular::SET_LTFOOT, "set-ltfoot", true },
172 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
173 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
174 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
175 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
176 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
177 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
178 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
179 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
180 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
181 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
182 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
183 { Tabular::SET_BOOKTABS, "set-booktabs", false },
184 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
185 { Tabular::SET_TOP_SPACE, "set-top-space", true },
186 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
187 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
188 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
189 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
190 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
191 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
192 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
193 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
194 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
195 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
196 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
197 { Tabular::LAST_ACTION, "", false }
201 string const tostr(LyXAlignment const & num)
206 case LYX_ALIGN_BLOCK:
210 case LYX_ALIGN_CENTER:
212 case LYX_ALIGN_RIGHT:
214 case LYX_ALIGN_LAYOUT:
216 case LYX_ALIGN_SPECIAL:
218 case LYX_ALIGN_DECIMAL:
225 string const tostr(Tabular::HAlignment const & num)
228 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
230 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
232 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
239 string const tostr(Tabular::VAlignment const & num)
242 case Tabular::LYX_VALIGN_TOP:
244 case Tabular::LYX_VALIGN_MIDDLE:
246 case Tabular::LYX_VALIGN_BOTTOM:
253 string const tostr(Tabular::BoxType const & num)
256 case Tabular::BOX_NONE:
258 case Tabular::BOX_PARBOX:
260 case Tabular::BOX_MINIPAGE:
267 // I would have liked a fromstr template a lot better. (Lgb)
268 bool string2type(string const & str, LyXAlignment & num)
271 num = LYX_ALIGN_NONE;
272 else if (str == "block")
273 num = LYX_ALIGN_BLOCK;
274 else if (str == "left")
275 num = LYX_ALIGN_LEFT;
276 else if (str == "center")
277 num = LYX_ALIGN_CENTER;
278 else if (str == "right")
279 num = LYX_ALIGN_RIGHT;
280 else if (str == "decimal")
281 num = LYX_ALIGN_DECIMAL;
288 bool string2type(string const & str, Tabular::HAlignment & num)
291 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
292 else if (str == "center" )
293 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
294 else if (str == "right")
295 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
302 bool string2type(string const & str, Tabular::VAlignment & num)
305 num = Tabular::LYX_VALIGN_TOP;
306 else if (str == "middle" )
307 num = Tabular::LYX_VALIGN_MIDDLE;
308 else if (str == "bottom")
309 num = Tabular::LYX_VALIGN_BOTTOM;
316 bool string2type(string const & str, Tabular::BoxType & num)
319 num = Tabular::BOX_NONE;
320 else if (str == "parbox")
321 num = Tabular::BOX_PARBOX;
322 else if (str == "minipage")
323 num = Tabular::BOX_MINIPAGE;
330 bool string2type(string const & str, bool & num)
334 else if (str == "false")
342 bool getTokenValue(string const & str, char const * token, string & ret)
345 size_t token_length = strlen(token);
346 size_t pos = str.find(token);
348 if (pos == string::npos || pos + token_length + 1 >= str.length()
349 || str[pos + token_length] != '=')
351 pos += token_length + 1;
353 if (ch != '"' && ch != '\'') { // only read till next space
357 while (pos < str.length() - 1 && str[++pos] != ch)
364 bool getTokenValue(string const & str, char const * token, docstring & ret)
367 bool const success = getTokenValue(str, token, tmp);
368 ret = from_utf8(tmp);
373 bool getTokenValue(string const & str, char const * token, int & num)
377 if (!getTokenValue(str, token, tmp))
379 num = convert<int>(tmp);
384 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
387 return getTokenValue(str, token, tmp) && string2type(tmp, num);
391 bool getTokenValue(string const & str, char const * token,
392 Tabular::HAlignment & num)
395 return getTokenValue(str, token, tmp) && string2type(tmp, num);
399 bool getTokenValue(string const & str, char const * token,
400 Tabular::VAlignment & num)
403 return getTokenValue(str, token, tmp) && string2type(tmp, num);
407 bool getTokenValue(string const & str, char const * token,
408 Tabular::BoxType & num)
411 return getTokenValue(str, token, tmp) && string2type(tmp, num);
415 bool getTokenValue(string const & str, char const * token, bool & flag)
417 // set the flag always to false as this should be the default for bools
418 // not in the file-format.
421 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
425 bool getTokenValue(string const & str, char const * token, Length & len)
427 // set the length to be zero() as default as this it should be if not
428 // in the file format.
431 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
435 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
440 if (!getTokenValue(str, token, tmp))
442 if (tmp == "default") {
446 return isValidLength(tmp, &len);
450 void l_getline(istream & is, string & str)
453 while (str.empty()) {
455 if (!str.empty() && str[str.length() - 1] == '\r')
456 str.erase(str.length() - 1);
461 string const write_attribute(string const & name, T const & t)
463 string const s = tostr(t);
464 return s.empty() ? s : " " + name + "=\"" + s + "\"";
468 string const write_attribute(string const & name, string const & t)
470 return t.empty() ? t : " " + name + "=\"" + t + "\"";
475 string const write_attribute(string const & name, docstring const & t)
477 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
482 string const write_attribute(string const & name, bool const & b)
484 // we write only true attribute values so we remove a bit of the
485 // file format bloat for tabulars.
486 return b ? write_attribute(name, convert<string>(b)) : string();
491 string const write_attribute(string const & name, int const & i)
493 // we write only true attribute values so we remove a bit of the
494 // file format bloat for tabulars.
495 return i ? write_attribute(name, convert<string>(i)) : string();
500 string const write_attribute(string const & name, Tabular::idx_type const & i)
502 // we write only true attribute values so we remove a bit of the
503 // file format bloat for tabulars.
504 return i ? write_attribute(name, convert<string>(i)) : string();
509 string const write_attribute(string const & name, Length const & value)
511 // we write only the value if we really have one same reson as above.
512 return value.zero() ? string() : write_attribute(name, value.asString());
518 string const featureAsString(Tabular::Feature action)
520 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
521 if (tabularFeature[i].action == action)
522 return tabularFeature[i].feature;
528 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
530 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
531 for (; dit; dit.forwardChar())
532 if (dit.inTexted() && dit.depth() == 1
533 && dit.paragraph().find(align_d, false, false, dit.pos()))
540 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
542 InsetTableCell tail = InsetTableCell(head);
543 DocIterator const dit = separatorPos(&head, align_d);
546 pit_type const psize = head.paragraphs().front().size();
547 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
548 tail.paragraphs().front().eraseChars(0,
549 dit.pos() < psize ? dit.pos() + 1 : psize, false);
556 /////////////////////////////////////////////////////////////////////
560 /////////////////////////////////////////////////////////////////////
563 Tabular::CellData::CellData(Buffer * buf)
566 multicolumn(Tabular::CELL_NORMAL),
567 multirow(Tabular::CELL_NORMAL),
568 alignment(LYX_ALIGN_CENTER),
569 valignment(LYX_VALIGN_TOP),
579 inset(new InsetTableCell(buf))
581 inset->setBuffer(*buf);
585 Tabular::CellData::CellData(CellData const & cs)
588 multicolumn(cs.multicolumn),
589 multirow(cs.multirow),
590 mroffset(cs.mroffset),
591 alignment(cs.alignment),
592 valignment(cs.valignment),
593 decimal_hoffset(cs.decimal_hoffset),
594 decimal_width(cs.decimal_width),
596 top_line(cs.top_line),
597 bottom_line(cs.bottom_line),
598 left_line(cs.left_line),
599 right_line(cs.right_line),
602 align_special(cs.align_special),
604 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
608 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
614 multicolumn = cs.multicolumn;
615 multirow = cs.multirow;
616 mroffset = cs.mroffset;
617 alignment = cs.alignment;
618 valignment = cs.valignment;
619 decimal_hoffset = cs.decimal_hoffset;
620 decimal_width = cs.decimal_width;
621 voffset = cs.voffset;
622 top_line = cs.top_line;
623 bottom_line = cs.bottom_line;
624 left_line = cs.left_line;
625 right_line = cs.right_line;
628 align_special = cs.align_special;
629 p_width = cs.p_width;
630 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
634 Tabular::RowData::RowData()
637 top_space_default(false),
638 bottom_space_default(false),
639 interline_space_default(false),
649 Tabular::ColumnData::ColumnData()
650 : alignment(LYX_ALIGN_CENTER),
651 valignment(LYX_VALIGN_TOP),
657 Tabular::ltType::ltType()
658 : set(false), topDL(false),
664 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
666 init(buffer, rows_arg, columns_arg);
670 void Tabular::setBuffer(Buffer & buffer)
673 for (row_type i = 0; i < nrows(); ++i)
674 for (col_type j = 0; j < ncols(); ++j)
675 cell_info[i][j].inset->setBuffer(*buffer_);
679 // activates all lines and sets all widths to 0
680 void Tabular::init(Buffer * buf, row_type rows_arg,
681 col_type columns_arg)
684 row_info = row_vector(rows_arg);
685 column_info = column_vector(columns_arg);
686 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
687 row_info.reserve(10);
688 column_info.reserve(10);
689 cell_info.reserve(100);
691 is_long_tabular = false;
692 tabular_valignment = LYX_VALIGN_MIDDLE;
693 tabular_width = Length();
694 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
696 use_booktabs = false;
697 // set silly default lines
698 for (row_type r = 0; r < nrows(); ++r)
699 for (col_type c = 0; c < ncols(); ++c) {
700 cell_info[r][c].inset->setBuffer(*buffer_);
701 cell_info[r][c].top_line = true;
702 cell_info[r][c].left_line = true;
703 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
704 cell_info[r][c].right_line = c == ncols() - 1;
709 void Tabular::deleteRow(row_type const row)
711 // Not allowed to delete last row
715 for (col_type c = 0; c < ncols(); ++c) {
716 // Care about multirow cells
717 if (row + 1 < nrows() &&
718 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
719 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
720 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
723 row_info.erase(row_info.begin() + row);
724 cell_info.erase(cell_info.begin() + row);
729 void Tabular::copyRow(row_type const row)
731 insertRow(row, true);
735 void Tabular::appendRow(row_type row)
737 insertRow(row, false);
741 void Tabular::insertRow(row_type const row, bool copy)
743 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
744 cell_info.insert(cell_info.begin() + row + 1,
745 cell_vector(0, CellData(buffer_)));
747 for (col_type c = 0; c < ncols(); ++c) {
748 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
749 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
750 if (buffer().params().track_changes)
751 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
752 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
753 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
757 for (col_type c = 0; c < ncols(); ++c) {
758 if (isPartOfMultiRow(row, c))
760 // inherit line settings
761 idx_type const i = cellIndex(row + 1, c);
762 idx_type const j = cellIndex(row, c);
763 setLeftLine(i, leftLine(j));
764 setRightLine(i, rightLine(j));
765 setTopLine(i, topLine(j));
766 if (topLine(j) && bottomLine(j)) {
767 setBottomLine(i, true);
768 setBottomLine(j, false);
770 // mark track changes
771 if (buffer().params().track_changes)
772 cellInfo(i).inset->setChange(Change(Change::INSERTED));
777 void Tabular::moveColumn(col_type col, ColDirection direction)
779 if (direction == Tabular::LEFT)
782 std::swap(column_info[col], column_info[col + 1]);
784 for (row_type r = 0; r < nrows(); ++r) {
785 std::swap(cell_info[r][col], cell_info[r][col + 1]);
786 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
787 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
789 // FIXME track changes is broken for tabular features (#8469)
790 idx_type const i = cellIndex(r, col);
791 idx_type const j = cellIndex(r, col + 1);
792 if (buffer().params().track_changes) {
793 cellInfo(i).inset->setChange(Change(Change::INSERTED));
794 cellInfo(j).inset->setChange(Change(Change::INSERTED));
801 void Tabular::moveRow(row_type row, RowDirection direction)
803 if (direction == Tabular::UP)
806 std::swap(row_info[row], row_info[row + 1]);
808 for (col_type c = 0; c < ncols(); ++c) {
809 std::swap(cell_info[row][c], cell_info[row + 1][c]);
810 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
811 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
813 // FIXME track changes is broken for tabular features (#8469)
814 idx_type const i = cellIndex(row, c);
815 idx_type const j = cellIndex(row + 1, c);
816 if (buffer().params().track_changes) {
817 cellInfo(i).inset->setChange(Change(Change::INSERTED));
818 cellInfo(j).inset->setChange(Change(Change::INSERTED));
825 void Tabular::deleteColumn(col_type const col)
827 // Not allowed to delete last column
831 for (row_type r = 0; r < nrows(); ++r) {
832 // Care about multicolumn cells
833 if (col + 1 < ncols() &&
834 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
835 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
836 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
838 cell_info[r].erase(cell_info[r].begin() + col);
840 column_info.erase(column_info.begin() + col);
845 void Tabular::copyColumn(col_type const col)
847 insertColumn(col, true);
851 void Tabular::appendColumn(col_type col)
853 insertColumn(col, false);
857 void Tabular::insertColumn(col_type const col, bool copy)
859 BufferParams const & bp = buffer().params();
860 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
862 for (row_type r = 0; r < nrows(); ++r) {
863 cell_info[r].insert(cell_info[r].begin() + col + 1,
864 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
865 if (bp.track_changes)
866 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
867 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
868 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
871 for (row_type r = 0; r < nrows(); ++r) {
872 // inherit line settings
873 idx_type const i = cellIndex(r, col + 1);
874 idx_type const j = cellIndex(r, col);
875 setBottomLine(i, bottomLine(j));
876 setTopLine(i, topLine(j));
877 setLeftLine(i, leftLine(j));
878 if (rightLine(i) && rightLine(j)) {
879 setRightLine(i, true);
880 setRightLine(j, false);
882 if (buffer().params().track_changes)
883 cellInfo(i).inset->setChange(Change(Change::INSERTED));
888 void Tabular::updateIndexes()
893 for (row_type row = 0; row < nrows(); ++row)
894 for (col_type column = 0; column < ncols(); ++column) {
895 if (!isPartOfMultiColumn(row, column)
896 && !isPartOfMultiRow(row, column))
898 if (isPartOfMultiRow(row, column))
899 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
901 cell_info[row][column].cellno = numberofcells - 1;
904 rowofcell.resize(numberofcells);
905 columnofcell.resize(numberofcells);
907 // reset column and row of cells and update their width and alignment
908 for (row_type row = 0; row < nrows(); ++row)
909 for (col_type column = 0; column < ncols(); ++column) {
910 if (isPartOfMultiColumn(row, column))
912 // columnofcell needs to be called before setting width and aligment
913 // multirow cells inherit the width from the column width
914 if (!isPartOfMultiRow(row, column)) {
915 columnofcell[i] = column;
918 setFixedWidth(row, column);
919 if (isPartOfMultiRow(row, column))
921 cell_info[row][column].inset->setContentAlignment(
922 getAlignment(cellIndex(row, column)));
928 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
931 for (col_type c = 0; c < ncols(); ++c)
932 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
938 bool Tabular::topLine(idx_type const cell) const
940 return cellInfo(cell).top_line;
944 bool Tabular::bottomLine(idx_type const cell) const
946 return cellInfo(cell).bottom_line;
950 bool Tabular::leftLine(idx_type cell) const
954 return cellInfo(cell).left_line;
958 bool Tabular::rightLine(idx_type cell) const
962 return cellInfo(cell).right_line;
966 int Tabular::interRowSpace(row_type row) const
968 if (!row || row >= nrows())
971 int const interline_space = row_info[row - 1].interline_space_default ?
973 row_info[row - 1].interline_space.inPixels(width());
974 if (rowTopLine(row) && rowBottomLine(row - 1))
975 return interline_space + WIDTH_OF_LINE;
976 return interline_space;
980 int Tabular::interColumnSpace(idx_type cell) const
982 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
983 if (rightLine(cell) && nextcol < ncols()
984 && leftLine(cellIndex(cellRow(cell), nextcol)))
985 return WIDTH_OF_LINE;
990 int Tabular::cellWidth(idx_type cell) const
993 col_type const span = columnSpan(cell);
994 col_type const col = cellColumn(cell);
995 for(col_type c = col; c < col + span ; ++c)
996 w += column_info[c].width;
1001 int Tabular::cellHeight(idx_type cell) const
1003 row_type const span = rowSpan(cell);
1004 row_type const row = cellRow(cell);
1006 for(row_type r = row; r < row + span ; ++r) {
1007 h += rowAscent(r) + rowDescent(r);
1008 if (r != row + span - 1)
1009 h += interRowSpace(r + 1);
1016 bool Tabular::updateColumnWidths()
1018 vector<int> max_dwidth(ncols(), 0);
1019 for(col_type c = 0; c < ncols(); ++c)
1020 for(row_type r = 0; r < nrows(); ++r) {
1021 idx_type const i = cellIndex(r, c);
1022 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1023 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1026 bool update = false;
1027 // for each col get max of single col cells
1028 for(col_type c = 0; c < ncols(); ++c) {
1030 for(row_type r = 0; r < nrows(); ++r) {
1031 idx_type const i = cellIndex(r, c);
1032 if (columnSpan(i) == 1) {
1033 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1034 && cell_info[r][c].decimal_width!=0)
1035 new_width = max(new_width, cellInfo(i).width
1036 + max_dwidth[c] - cellInfo(i).decimal_width);
1038 new_width = max(new_width, cellInfo(i).width);
1042 if (column_info[c].width != new_width) {
1043 column_info[c].width = new_width;
1047 // update col widths to fit merged cells
1048 for(col_type c = 0; c < ncols(); ++c)
1049 for(row_type r = 0; r < nrows(); ++r) {
1050 idx_type const i = cellIndex(r, c);
1051 int const span = columnSpan(i);
1052 if (span == 1 || c > cellColumn(i))
1056 for(col_type j = c; j < c + span ; ++j)
1057 old_width += column_info[j].width;
1059 if (cellInfo(i).width > old_width) {
1060 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1069 int Tabular::width() const
1072 for (col_type c = 0; c < ncols(); ++c)
1073 width += column_info[c].width;
1078 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1081 col_type const col = cellColumn(cell);
1082 // set alignment for the whole row if we are not in a multicolumn cell,
1083 // exclude possible multicolumn cells in the row
1084 if (!isMultiColumn(cell)) {
1085 for (row_type r = 0; r < nrows(); ++r) {
1086 // only if the column has no width the multirow inherits the
1087 // alignment of the column, otherwise it is left aligned
1088 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1089 && !isMultiColumn(cellIndex(r, col))) {
1090 cell_info[r][col].alignment = align;
1091 cell_info[r][col].inset->setContentAlignment(align);
1093 if ((isMultiRow(cellIndex(r, col)) && has_width)
1094 && !isMultiColumn(cellIndex(r, col))) {
1095 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1096 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1099 column_info[col].alignment = align;
1100 docstring & dpoint = column_info[col].decimal_point;
1101 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1102 dpoint = from_utf8(lyxrc.default_decimal_point);
1104 cellInfo(cell).alignment = align;
1105 cellInset(cell)->setContentAlignment(align);
1110 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1113 if (!isMultiColumn(cell) || onlycolumn)
1114 column_info[cellColumn(cell)].valignment = align;
1116 cellInfo(cell).valignment = align;
1123 * Allow line and paragraph breaks for fixed width cells or disallow them,
1124 * merge cell paragraphs and reset layout to standard for variable width
1127 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1129 inset->toggleFixedWidth(fixedWidth);
1133 // merge all paragraphs to one
1134 BufferParams const & bp = cur.bv().buffer().params();
1135 while (inset->paragraphs().size() > 1)
1136 mergeParagraph(bp, inset->paragraphs(), 0);
1140 // undo information has already been recorded
1141 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1142 bp.documentClass().plainLayoutName());
1149 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1150 Length const & width)
1152 col_type const c = cellColumn(cell);
1154 column_info[c].p_width = width;
1155 // reset the vertical alignment to top if the fixed width
1156 // is removed or zero because only fixed width columns can
1157 // have a vertical alignment
1158 if (column_info[c].p_width.zero())
1159 column_info[c].valignment = LYX_VALIGN_TOP;
1160 for (row_type r = 0; r < nrows(); ++r) {
1161 idx_type const cell = cellIndex(r, c);
1162 // because of multicolumns
1163 toggleFixedWidth(cur, cellInset(cell).get(),
1164 !getPWidth(cell).zero());
1165 if (isMultiRow(cell))
1166 setAlignment(cell, LYX_ALIGN_LEFT, false);
1168 // cur paragraph can become invalid after paragraphs were merged
1169 if (cur.pit() > cur.lastpit())
1170 cur.pit() = cur.lastpit();
1171 // cur position can become invalid after newlines were removed
1172 if (cur.pos() > cur.lastpos())
1173 cur.pos() = cur.lastpos();
1177 bool Tabular::setFixedWidth(row_type r, col_type c)
1179 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1180 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1181 || (multicol && !cell_info[r][c].p_width.zero());
1182 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1187 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1188 Length const & width)
1190 if (!isMultiColumn(cell))
1193 cellInfo(cell).p_width = width;
1194 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1195 // cur paragraph can become invalid after paragraphs were merged
1196 if (cur.pit() > cur.lastpit())
1197 cur.pit() = cur.lastpit();
1198 // cur position can become invalid after newlines were removed
1199 if (cur.pos() > cur.lastpos())
1200 cur.pos() = cur.lastpos();
1205 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1207 cellInfo(cell).mroffset = mroffset;
1212 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1213 Tabular::Feature what)
1215 if (what == SET_SPECIAL_MULTICOLUMN)
1216 cellInfo(cell).align_special = special;
1218 column_info[cellColumn(cell)].align_special = special;
1222 void Tabular::setTopLine(idx_type i, bool line)
1224 cellInfo(i).top_line = line;
1228 void Tabular::setBottomLine(idx_type i, bool line)
1230 cellInfo(i).bottom_line = line;
1234 void Tabular::setLeftLine(idx_type cell, bool line)
1236 cellInfo(cell).left_line = line;
1240 void Tabular::setRightLine(idx_type cell, bool line)
1242 cellInfo(cell).right_line = line;
1245 bool Tabular::rowTopLine(row_type r) const
1247 bool all_rows_set = true;
1248 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1249 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1250 return all_rows_set;
1254 bool Tabular::rowBottomLine(row_type r) const
1256 bool all_rows_set = true;
1257 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1258 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1259 return all_rows_set;
1263 bool Tabular::columnLeftLine(col_type c) const
1270 for (row_type r = 0; r < nrows(); ++r) {
1271 idx_type const i = cellIndex(r, c);
1272 if (c == cellColumn(i)) {
1274 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1275 if (cellInfo(i).left_line || right)
1279 return 2 * nrows_left >= total;
1283 bool Tabular::columnRightLine(col_type c) const
1288 int nrows_right = 0;
1290 for (row_type r = 0; r < nrows(); ++r) {
1291 idx_type i = cellIndex(r, c);
1292 if (c == cellColumn(i) + columnSpan(i) - 1) {
1294 bool left = (c + 1 < ncols()
1295 && cellInfo(cellIndex(r, c + 1)).left_line)
1296 || c + 1 == ncols();
1297 if (cellInfo(i).right_line && left)
1301 return 2 * nrows_right >= total;
1305 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1307 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1308 return cellInfo(cell).alignment;
1310 return column_info[cellColumn(cell)].alignment;
1315 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1317 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1318 return cellInfo(cell).valignment;
1319 return column_info[cellColumn(cell)].valignment;
1323 Length const Tabular::getPWidth(idx_type cell) const
1325 if (isMultiColumn(cell))
1326 return cellInfo(cell).p_width;
1327 return column_info[cellColumn(cell)].p_width;
1331 Length const Tabular::getMROffset(idx_type cell) const
1333 return cellInfo(cell).mroffset;
1337 int Tabular::textHOffset(idx_type cell) const
1339 // the LaTeX Way :-(
1340 int x = WIDTH_OF_LINE;
1342 int const w = cellWidth(cell) - cellInfo(cell).width;
1344 switch (getAlignment(cell)) {
1345 case LYX_ALIGN_CENTER:
1348 case LYX_ALIGN_RIGHT:
1351 case LYX_ALIGN_DECIMAL: {
1352 // we center when no decimal point
1353 if (cellInfo(cell).decimal_width == 0) {
1357 col_type const c = cellColumn(cell);
1358 int max_dhoffset = 0;
1359 for(row_type r = 0; r < row_info.size() ; ++r) {
1360 idx_type const i = cellIndex(r, c);
1361 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1362 && cellInfo(i).decimal_width != 0)
1363 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1365 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1368 // LYX_ALIGN_LEFT: nothing :-)
1376 int Tabular::textVOffset(idx_type cell) const
1378 int voffset = cellInfo(cell).voffset;
1379 if (isMultiRow(cell)) {
1380 row_type const row = cellRow(cell);
1381 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1387 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1390 idx_type const numcells = numberOfCellsInRow(row);
1391 // we check against numcells to make sure we do not crash if all the
1392 // cells are multirow (bug #7535), but in that case our return value
1393 // is really invalid, i.e., it is NOT the first cell in the row. but
1394 // i do not know what to do here. (rgh)
1395 while (c < numcells - 1
1396 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1398 return cell_info[row][c].cellno;
1402 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1404 col_type c = ncols() - 1;
1405 // of course we check against 0 so we don't crash. but we have the same
1406 // problem as in the previous routine: if all the cells are part of a
1407 // multirow or part of a multi column, then our return value is invalid.
1409 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1410 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1412 return cell_info[row][c].cellno;
1416 Tabular::row_type Tabular::cellRow(idx_type cell) const
1418 if (cell >= numberofcells)
1422 return rowofcell[cell];
1426 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1428 if (cell >= numberofcells)
1432 return columnofcell[cell];
1436 void Tabular::write(ostream & os) const
1440 << write_attribute("version", 3)
1441 << write_attribute("rows", nrows())
1442 << write_attribute("columns", ncols())
1444 // global longtable options
1446 << write_attribute("rotate", rotate)
1447 << write_attribute("booktabs", use_booktabs)
1448 << write_attribute("islongtable", is_long_tabular)
1449 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1450 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1451 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1452 << write_attribute("headTopDL", endhead.topDL)
1453 << write_attribute("headBottomDL", endhead.bottomDL)
1454 << write_attribute("footTopDL", endfoot.topDL)
1455 << write_attribute("footBottomDL", endfoot.bottomDL)
1456 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1457 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1458 << write_attribute("lastFootEmpty", endlastfoot.empty);
1459 // longtables cannot be aligned vertically
1460 if (!is_long_tabular) {
1461 os << write_attribute("tabularvalignment", tabular_valignment);
1462 os << write_attribute("tabularwidth", tabular_width);
1464 if (is_long_tabular)
1465 os << write_attribute("longtabularalignment", longtabular_alignment);
1467 for (col_type c = 0; c < ncols(); ++c) {
1469 << write_attribute("alignment", column_info[c].alignment);
1470 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1471 os << write_attribute("decimal_point", column_info[c].decimal_point);
1472 os << write_attribute("valignment", column_info[c].valignment)
1473 << write_attribute("width", column_info[c].p_width.asString())
1474 << write_attribute("special", column_info[c].align_special)
1477 for (row_type r = 0; r < nrows(); ++r) {
1478 static const string def("default");
1480 if (row_info[r].top_space_default)
1481 os << write_attribute("topspace", def);
1483 os << write_attribute("topspace", row_info[r].top_space);
1484 if (row_info[r].bottom_space_default)
1485 os << write_attribute("bottomspace", def);
1487 os << write_attribute("bottomspace", row_info[r].bottom_space);
1488 if (row_info[r].interline_space_default)
1489 os << write_attribute("interlinespace", def);
1491 os << write_attribute("interlinespace", row_info[r].interline_space);
1492 os << write_attribute("endhead", row_info[r].endhead)
1493 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1494 << write_attribute("endfoot", row_info[r].endfoot)
1495 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1496 << write_attribute("newpage", row_info[r].newpage)
1497 << write_attribute("caption", row_info[r].caption)
1499 for (col_type c = 0; c < ncols(); ++c) {
1501 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1502 << write_attribute("multirow", cell_info[r][c].multirow)
1503 << write_attribute("mroffset", cell_info[r][c].mroffset)
1504 << write_attribute("alignment", cell_info[r][c].alignment)
1505 << write_attribute("valignment", cell_info[r][c].valignment)
1506 << write_attribute("topline", cell_info[r][c].top_line)
1507 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1508 << write_attribute("leftline", cell_info[r][c].left_line)
1509 << write_attribute("rightline", cell_info[r][c].right_line)
1510 << write_attribute("rotate", cell_info[r][c].rotate)
1511 << write_attribute("usebox", cell_info[r][c].usebox)
1512 << write_attribute("width", cell_info[r][c].p_width)
1513 << write_attribute("special", cell_info[r][c].align_special)
1515 os << "\\begin_inset ";
1516 cell_info[r][c].inset->write(os);
1517 os << "\n\\end_inset\n"
1519 // FIXME This can be removed again once the mystery
1520 // crash has been resolved.
1525 os << "</lyxtabular>\n";
1529 void Tabular::read(Lexer & lex)
1532 istream & is = lex.getStream();
1534 l_getline(is, line);
1535 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1536 LASSERT(false, return);
1540 if (!getTokenValue(line, "version", version))
1542 LATTEST(version >= 2);
1545 if (!getTokenValue(line, "rows", rows_arg))
1548 if (!getTokenValue(line, "columns", columns_arg))
1550 init(buffer_, rows_arg, columns_arg);
1551 l_getline(is, line);
1552 if (!prefixIs(line, "<features")) {
1553 lyxerr << "Wrong tabular format (expected <features ...> got"
1554 << line << ')' << endl;
1557 getTokenValue(line, "rotate", rotate);
1558 getTokenValue(line, "booktabs", use_booktabs);
1559 getTokenValue(line, "islongtable", is_long_tabular);
1560 getTokenValue(line, "tabularvalignment", tabular_valignment);
1561 getTokenValue(line, "tabularwidth", tabular_width);
1562 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1563 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1564 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1565 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1566 getTokenValue(line, "headTopDL", endhead.topDL);
1567 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1568 getTokenValue(line, "footTopDL", endfoot.topDL);
1569 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1570 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1571 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1572 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1574 for (col_type c = 0; c < ncols(); ++c) {
1576 if (!prefixIs(line,"<column")) {
1577 lyxerr << "Wrong tabular format (expected <column ...> got"
1578 << line << ')' << endl;
1581 getTokenValue(line, "alignment", column_info[c].alignment);
1582 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1583 getTokenValue(line, "valignment", column_info[c].valignment);
1584 getTokenValue(line, "width", column_info[c].p_width);
1585 getTokenValue(line, "special", column_info[c].align_special);
1588 for (row_type i = 0; i < nrows(); ++i) {
1589 l_getline(is, line);
1590 if (!prefixIs(line, "<row")) {
1591 lyxerr << "Wrong tabular format (expected <row ...> got"
1592 << line << ')' << endl;
1595 getTokenValue(line, "topspace", row_info[i].top_space,
1596 row_info[i].top_space_default);
1597 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1598 row_info[i].bottom_space_default);
1599 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1600 row_info[i].interline_space_default);
1601 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1602 getTokenValue(line, "endhead", row_info[i].endhead);
1603 getTokenValue(line, "endfoot", row_info[i].endfoot);
1604 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1605 getTokenValue(line, "newpage", row_info[i].newpage);
1606 getTokenValue(line, "caption", row_info[i].caption);
1607 for (col_type j = 0; j < ncols(); ++j) {
1608 l_getline(is, line);
1609 if (!prefixIs(line, "<cell")) {
1610 lyxerr << "Wrong tabular format (expected <cell ...> got"
1611 << line << ')' << endl;
1614 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1615 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1616 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1617 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1618 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1619 getTokenValue(line, "topline", cell_info[i][j].top_line);
1620 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1621 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1622 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1623 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1624 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1625 getTokenValue(line, "width", cell_info[i][j].p_width);
1627 getTokenValue(line, "special", cell_info[i][j].align_special);
1628 l_getline(is, line);
1629 if (prefixIs(line, "\\begin_inset")) {
1630 cell_info[i][j].inset->setBuffer(*buffer_);
1631 cell_info[i][j].inset->read(lex);
1632 l_getline(is, line);
1634 if (!prefixIs(line, "</cell>")) {
1635 lyxerr << "Wrong tabular format (expected </cell> got"
1636 << line << ')' << endl;
1640 l_getline(is, line);
1641 if (!prefixIs(line, "</row>")) {
1642 lyxerr << "Wrong tabular format (expected </row> got"
1643 << line << ')' << endl;
1647 while (!prefixIs(line, "</lyxtabular>")) {
1648 l_getline(is, line);
1654 bool Tabular::isMultiColumn(idx_type cell) const
1656 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1657 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1661 bool Tabular::hasMultiColumn(col_type c) const
1663 for (row_type r = 0; r < nrows(); ++r) {
1664 if (isMultiColumn(cellIndex(r, c)))
1671 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1673 return cell_info[cellRow(cell)][cellColumn(cell)];
1677 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1679 return cell_info[cellRow(cell)][cellColumn(cell)];
1683 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1684 bool const right_border)
1686 idx_type const col = cellColumn(cell);
1687 idx_type const row = cellRow(cell);
1688 for (idx_type i = 0; i < number; ++i)
1689 unsetMultiRow(cellIndex(row, col + i));
1691 // unsetting of multirow may have invalidated cell index
1692 cell = cellIndex(row, col);
1693 CellData & cs = cellInfo(cell);
1694 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1695 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1696 cs.alignment = column_info[col].alignment;
1697 setRightLine(cell, right_border);
1699 idx_type lastcell = cellIndex(row, col + number - 1);
1700 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1701 CellData & cs1 = cellInfo(cell + i);
1702 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1703 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1711 bool Tabular::isMultiRow(idx_type cell) const
1713 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1714 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1717 bool Tabular::hasMultiRow(row_type r) const
1719 for (col_type c = 0; c < ncols(); ++c) {
1720 if (isMultiRow(cellIndex(r, c)))
1726 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1727 bool const bottom_border,
1728 LyXAlignment const halign)
1730 idx_type const col = cellColumn(cell);
1731 idx_type const row = cellRow(cell);
1732 for (idx_type i = 0; i < number; ++i)
1733 unsetMultiColumn(cellIndex(row + i, col));
1735 // unsetting of multirow may have invalidated cell index
1736 cell = cellIndex(row, col);
1737 CellData & cs = cellInfo(cell);
1738 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1739 cs.valignment = LYX_VALIGN_MIDDLE;
1740 // the horizontal alignment of multirow cells can only
1741 // be changed for the whole table row,
1742 // support changing this only for the multirow cell can be done via
1744 if (getPWidth(cell).zero())
1745 cs.alignment = halign;
1747 cs.alignment = LYX_ALIGN_LEFT;
1749 // set the bottom line of the last selected cell
1750 setBottomLine(cell, bottom_border);
1752 for (idx_type i = 1; i < number; ++i) {
1753 CellData & cs1 = cell_info[row + i][col];
1754 cs1.multirow = CELL_PART_OF_MULTIROW;
1755 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1763 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1765 row_type const row = cellRow(cell);
1766 col_type const col = cellColumn(cell);
1768 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1775 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1777 col_type const column = cellColumn(cell);
1778 col_type row = cellRow(cell) + 1;
1779 while (row < nrows() && isPartOfMultiRow(row, column))
1782 return row - cellRow(cell);
1786 void Tabular::unsetMultiColumn(idx_type cell)
1788 if (!isMultiColumn(cell))
1791 row_type const row = cellRow(cell);
1792 col_type const col = cellColumn(cell);
1793 row_type const span = columnSpan(cell);
1794 for (col_type c = 0; c < span; ++c) {
1795 // in the table dialog the lines are set in every case
1796 // when unsetting a multicolumn this leads to an additional right
1797 // line for every cell that was part of the former multicolumn cell,
1798 // except if the cell is in the last column
1799 // therefore remove this line
1800 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1801 && (col + c) < (col + span - 1))
1802 cell_info[row][col + c].right_line = false;
1803 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1809 void Tabular::unsetMultiRow(idx_type cell)
1811 if (!isMultiRow(cell))
1814 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1815 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1816 row_type const row = cellRow(cell);
1817 col_type const col = cellColumn(cell);
1818 row_type const span = rowSpan(cell);
1819 for (row_type r = 0; r < span; ++r)
1820 cell_info[row + r][col].multirow = CELL_NORMAL;
1825 void Tabular::setRotateCell(idx_type cell, int value)
1827 cellInfo(cell).rotate = value;
1831 int Tabular::getRotateCell(idx_type cell) const
1833 return cellInfo(cell).rotate;
1837 bool Tabular::needRotating() const
1841 for (row_type r = 0; r < nrows(); ++r)
1842 for (col_type c = 0; c < ncols(); ++c)
1843 if (cell_info[r][c].rotate != 0)
1849 bool Tabular::isLastCell(idx_type cell) const
1851 if (cell + 1 < numberofcells)
1857 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1859 if (cellRow(cell) == 0)
1862 col_type const col = cellColumn(cell);
1863 row_type r = cellRow(cell) - 1;
1864 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1867 return cell_info[r][col].cellno;
1871 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1873 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1874 if (nextrow < nrows())
1875 return cell_info[nextrow][cellColumn(cell)].cellno;
1880 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1882 LASSERT(column != npos && column < ncols(), column = 0);
1883 LASSERT(row != npos && row < nrows(), row = 0);
1884 return cell_info[row][column].cellno;
1888 void Tabular::setUsebox(idx_type cell, BoxType type)
1890 cellInfo(cell).usebox = type;
1894 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1895 // adds line breaks, see bug 4886.
1896 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1898 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1899 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1901 if (cellInfo(cell).usebox > 1)
1902 return cellInfo(cell).usebox;
1903 return useParbox(cell);
1908 // This are functions used for the longtable support
1910 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1916 row_info[row].endfirsthead = flag;
1920 row_info[row].endhead = flag;
1925 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1928 hd.set = haveLTHead();
1929 return row_info[row].endhead;
1933 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1936 hd.set = haveLTFirstHead();
1937 return row_info[row].endfirsthead;
1941 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1947 row_info[row].endlastfoot = flag;
1951 row_info[row].endfoot = flag;
1956 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1959 fd.set = haveLTFoot();
1960 return row_info[row].endfoot;
1964 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1967 fd.set = haveLTLastFoot();
1968 return row_info[row].endlastfoot;
1972 void Tabular::setLTNewPage(row_type row, bool what)
1974 row_info[row].newpage = what;
1978 bool Tabular::getLTNewPage(row_type row) const
1980 return row_info[row].newpage;
1984 bool Tabular::haveLTHead(bool withcaptions) const
1986 if (!is_long_tabular)
1988 for (row_type i = 0; i < nrows(); ++i)
1989 if (row_info[i].endhead &&
1990 (withcaptions || !row_info[i].caption))
1996 bool Tabular::haveLTFirstHead(bool withcaptions) const
1998 if (!is_long_tabular || endfirsthead.empty)
2000 for (row_type r = 0; r < nrows(); ++r)
2001 if (row_info[r].endfirsthead &&
2002 (withcaptions || !row_info[r].caption))
2008 bool Tabular::haveLTFoot(bool withcaptions) const
2010 if (!is_long_tabular)
2012 for (row_type r = 0; r < nrows(); ++r)
2013 if (row_info[r].endfoot &&
2014 (withcaptions || !row_info[r].caption))
2020 bool Tabular::haveLTLastFoot(bool withcaptions) const
2022 if (!is_long_tabular || endlastfoot.empty)
2024 for (row_type r = 0; r < nrows(); ++r)
2025 if (row_info[r].endlastfoot &&
2026 (withcaptions || !row_info[r].caption))
2032 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2034 idx_type i = getFirstCellInRow(row);
2036 setMultiColumn(i, numberOfCellsInRow(row), false);
2037 setTopLine(i, false);
2038 setBottomLine(i, false);
2039 setLeftLine(i, false);
2040 setRightLine(i, false);
2041 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2042 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2043 setLTHead(row, true, endfirsthead, true);
2044 row_info[row].endfirsthead = true;
2047 unsetMultiColumn(i);
2048 // When unsetting a caption row, also all existing
2049 // captions in this row must be dissolved.
2051 row_info[row].caption = what;
2056 bool Tabular::ltCaption(row_type row) const
2058 return row_info[row].caption;
2062 bool Tabular::haveLTCaption(CaptionType captiontype) const
2064 if (!is_long_tabular)
2066 for (row_type r = 0; r < nrows(); ++r) {
2067 if (row_info[r].caption) {
2068 switch (captiontype) {
2069 case CAPTION_FIRSTHEAD:
2070 if (row_info[r].endfirsthead)
2074 if (row_info[r].endhead)
2078 if (row_info[r].endfoot)
2081 case CAPTION_LASTFOOT:
2082 if (row_info[r].endlastfoot)
2094 // end longtable support functions
2096 void Tabular::setRowAscent(row_type row, int height)
2098 if (row >= nrows() || row_info[row].ascent == height)
2100 row_info[row].ascent = height;
2104 void Tabular::setRowDescent(row_type row, int height)
2106 if (row >= nrows() || row_info[row].descent == height)
2108 row_info[row].descent = height;
2112 int Tabular::rowAscent(row_type row) const
2114 LASSERT(row < nrows(), row = 0);
2115 return row_info[row].ascent;
2119 int Tabular::rowDescent(row_type row) const
2121 LASSERT(row < nrows(), row = 0);
2122 return row_info[row].descent;
2126 int Tabular::height() const
2129 for (row_type row = 0; row < nrows(); ++row)
2130 height += rowAscent(row) + rowDescent(row) +
2136 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2138 LASSERT(row < nrows(), return false);
2139 LASSERT(column < ncols(), return false);
2140 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2144 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2146 LASSERT(row < nrows(), return false);
2147 LASSERT(column < ncols(), return false);
2148 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2152 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2154 // we only output complete row lines and the 1st row here, the rest
2155 // is done in Tabular::TeXBottomHLine(...)
2157 // get for each column the topline (if any)
2158 vector<bool> topline;
2160 for (col_type c = 0; c < ncols(); ++c) {
2161 topline.push_back(topLine(cellIndex(row, c)));
2162 // If cell is part of a multirow and not the first cell of the
2163 // multirow, no line must be drawn.
2165 if (isMultiRow(cellIndex(row, c))
2166 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2172 // do nothing if empty first row, or incomplete row line after
2173 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2176 // only output complete row lines and the 1st row's clines
2177 if (nset == ncols()) {
2179 os << (row == 0 ? "\\toprule " : "\\midrule ");
2183 } else if (row == 0) {
2184 for (col_type c = 0; c < ncols(); ++c) {
2186 col_type offset = 0;
2187 for (col_type j = 0 ; j < c; ++j)
2188 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2191 //babel makes the "-" character an active one, so we have to suppress this here
2192 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2193 if (lang == "slovak" || lang == "czech")
2194 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2195 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2197 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2199 col_type cstart = c;
2200 for ( ; c < ncols() && topline[c]; ++c) {}
2202 for (col_type j = cstart ; j < c ; ++j)
2203 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2206 os << c + offset << "} ";
2214 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2216 // we output bottomlines of row r and the toplines of row r+1
2217 // if the latter do not span the whole tabular
2219 // get the bottomlines of row r, and toplines in next row
2220 bool lastrow = row == nrows() - 1;
2221 vector<bool> bottomline, topline;
2222 bool nextrowset = true;
2223 for (col_type c = 0; c < ncols(); ++c) {
2224 bottomline.push_back(bottomLine(cellIndex(row, c)));
2225 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2226 // If cell is part of a multirow and not the last cell of the
2227 // multirow, no line must be drawn.
2229 if (isMultiRow(cellIndex(row, c))
2230 && isMultiRow(cellIndex(row + 1, c))
2231 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2232 bottomline[c] = false;
2235 nextrowset &= topline[c];
2238 // combine this row's bottom lines and next row's toplines if necessary
2240 for (col_type c = 0; c < ncols(); ++c) {
2242 bottomline[c] = bottomline[c] || topline[c];
2247 // do nothing if empty, OR incomplete row line with a topline in next row
2248 if (nset == 0 || (nextrowset && nset != ncols()))
2251 if (nset == ncols()) {
2253 os << (lastrow ? "\\bottomrule" : "\\midrule");
2257 for (col_type c = 0; c < ncols(); ++c) {
2258 if (bottomline[c]) {
2259 col_type offset = 0;
2260 for (col_type j = 0 ; j < c; ++j)
2261 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2264 //babel makes the "-" character an active one, so we have to suppress this here
2265 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2266 if (lang == "slovak" || lang == "czech")
2267 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2268 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2270 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2272 col_type cstart = c;
2273 for ( ; c < ncols() && bottomline[c]; ++c) {}
2275 for (col_type j = cstart ; j < c ; ++j)
2276 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2279 os << c + offset << "} ";
2287 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2288 bool & ismulticol, bool & ismultirow) const
2290 row_type const r = cellRow(cell);
2291 if (is_long_tabular && row_info[r].caption)
2294 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2295 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2296 // figure out how to set the lines
2297 // we always set double lines to the right of the cell
2298 col_type const c = cellColumn(cell);
2299 col_type const nextcol = c + columnSpan(cell);
2300 bool colright = columnRightLine(c);
2301 bool colleft = columnLeftLine(c);
2302 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2303 bool nextcellleft = nextcol < ncols()
2304 && leftLine(cellIndex(r, nextcol));
2305 bool coldouble = colright && nextcolleft;
2306 bool celldouble = rightLine(cell) && nextcellleft;
2308 ismulticol = isMultiColumn(cell)
2309 || (c == 0 && colleft != leftLine(cell))
2310 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2311 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2312 || (coldouble != celldouble);
2314 // we center in multicol when no decimal point
2315 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2316 docstring const align_d = column_info[c].decimal_point;
2317 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2321 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2322 int latexcolspan = columnSpan(cell);
2323 for(col_type col = c; col < c + columnSpan(cell); ++col)
2324 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2328 os << "\\multicolumn{" << latexcolspan << "}{";
2329 if (c ==0 && leftLine(cell))
2331 if (!cellInfo(cell).align_special.empty()) {
2332 os << cellInfo(cell).align_special;
2334 if (!getPWidth(cell).zero()) {
2336 case LYX_ALIGN_LEFT:
2337 os << ">{\\raggedright}";
2339 case LYX_ALIGN_RIGHT:
2340 os << ">{\\raggedleft}";
2342 case LYX_ALIGN_CENTER:
2343 os << ">{\\centering}";
2349 case LYX_VALIGN_TOP:
2352 case LYX_VALIGN_MIDDLE:
2355 case LYX_VALIGN_BOTTOM:
2360 << from_ascii(getPWidth(cell).asLatexString())
2364 case LYX_ALIGN_LEFT:
2367 case LYX_ALIGN_RIGHT:
2374 } // end if else !getPWidth
2375 } // end if else !cellinfo_of_cell
2376 if (rightLine(cell) || nextcellleft)
2379 // add extra vertical line if we want a double one
2382 } // end if ismulticol
2384 // we only need code for the first multirow cell
2385 ismultirow = isMultiRow(cell);
2387 os << "\\multirow{" << rowSpan(cell) << "}{";
2388 if (!getPWidth(cell).zero())
2389 os << from_ascii(getPWidth(cell).asLatexString());
2391 // we need to set a default value
2394 if (!getMROffset(cell).zero())
2395 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2397 } // end if ismultirow
2399 if (getRotateCell(cell) != 0)
2400 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2402 if (getUsebox(cell) == BOX_PARBOX) {
2405 case LYX_VALIGN_TOP:
2408 case LYX_VALIGN_MIDDLE:
2411 case LYX_VALIGN_BOTTOM:
2415 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2417 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2418 os << "\\begin{minipage}[";
2420 case LYX_VALIGN_TOP:
2423 case LYX_VALIGN_MIDDLE:
2426 case LYX_VALIGN_BOTTOM:
2430 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2436 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2437 bool ismulticol, bool ismultirow) const
2439 row_type const r = cellRow(cell);
2440 if (is_long_tabular && row_info[r].caption)
2444 if (getUsebox(cell) == BOX_PARBOX)
2446 else if (getUsebox(cell) == BOX_MINIPAGE)
2447 os << breakln << "\\end{minipage}";
2448 if (getRotateCell(cell) != 0)
2449 os << breakln << "\\end{turn}";
2457 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2458 OutputParams const & runparams) const
2460 if (!is_long_tabular)
2464 // output caption which is in no header or footer
2465 if (haveLTCaption()) {
2466 for (row_type r = 0; r < nrows(); ++r) {
2467 if (row_info[r].caption &&
2468 !row_info[r].endfirsthead && !row_info[r].endhead &&
2469 !row_info[r].endfoot && !row_info[r].endlastfoot)
2470 TeXRow(os, r, runparams);
2473 // output first header info
2474 if (haveLTFirstHead()) {
2475 if (endfirsthead.topDL)
2477 for (row_type r = 0; r < nrows(); ++r) {
2478 if (row_info[r].endfirsthead)
2479 TeXRow(os, r, runparams);
2481 if (endfirsthead.bottomDL)
2483 os << "\\endfirsthead\n";
2485 // output header info
2487 if (endfirsthead.empty && !haveLTFirstHead())
2488 os << "\\endfirsthead\n";
2491 for (row_type r = 0; r < nrows(); ++r) {
2492 if (row_info[r].endhead)
2493 TeXRow(os, r, runparams);
2495 if (endhead.bottomDL)
2497 os << "\\endhead\n";
2499 // output footer info
2503 for (row_type r = 0; r < nrows(); ++r) {
2504 if (row_info[r].endfoot)
2505 TeXRow(os, r, runparams);
2507 if (endfoot.bottomDL)
2509 os << "\\endfoot\n";
2510 if (endlastfoot.empty && !haveLTLastFoot())
2511 os << "\\endlastfoot\n";
2513 // output lastfooter info
2514 if (haveLTLastFoot()) {
2515 if (endlastfoot.topDL)
2517 for (row_type r = 0; r < nrows(); ++r) {
2518 if (row_info[r].endlastfoot)
2519 TeXRow(os, r, runparams);
2521 if (endlastfoot.bottomDL)
2523 os << "\\endlastfoot\n";
2528 bool Tabular::isValidRow(row_type row) const
2530 if (!is_long_tabular)
2532 return !row_info[row].endhead && !row_info[row].endfirsthead
2533 && !row_info[row].endfoot && !row_info[row].endlastfoot
2534 && !row_info[row].caption;
2538 void Tabular::TeXRow(otexstream & os, row_type row,
2539 OutputParams const & runparams) const
2541 idx_type cell = cellIndex(row, 0);
2542 InsetTableCell const * inset = cellInset(cell);
2543 Paragraph const & par = inset->paragraphs().front();
2544 string const lang = par.getParLanguage(buffer().params())->lang();
2546 //output the top line
2547 TeXTopHLine(os, row, lang);
2549 if (row_info[row].top_space_default) {
2551 os << "\\addlinespace\n";
2553 os << "\\noalign{\\vskip\\doublerulesep}\n";
2554 } else if(!row_info[row].top_space.zero()) {
2556 os << "\\addlinespace["
2557 << from_ascii(row_info[row].top_space.asLatexString())
2560 os << "\\noalign{\\vskip"
2561 << from_ascii(row_info[row].top_space.asLatexString())
2565 bool ismulticol = false;
2566 bool ismultirow = false;
2567 for (col_type c = 0; c < ncols(); ++c) {
2568 if (isPartOfMultiColumn(row, c))
2571 cell = cellIndex(row, c);
2573 if (isPartOfMultiRow(row, c)
2574 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2575 if (cell != getLastCellInRow(row))
2580 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2581 InsetTableCell const * inset = cellInset(cell);
2583 Paragraph const & par = inset->paragraphs().front();
2585 os.texrow().forceStart(par.id(), 0);
2587 bool rtl = par.isRTL(buffer().params())
2589 && getPWidth(cell).zero()
2590 && !runparams.use_polyglossia;
2594 par.getParLanguage(buffer().params())->lang();
2595 if (lang == "farsi")
2597 else if (lang == "arabic_arabi")
2599 // currently, remaining RTL languages are
2600 // arabic_arabtex and hebrew
2604 // pass to the OutputParams that we are in a cell and
2605 // which alignment we have set.
2606 // InsetNewline needs this context information.
2607 OutputParams newrp(runparams);
2608 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2609 ? OutputParams::PLAIN
2610 : OutputParams::ALIGNED;
2612 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2613 // copy cell and split in 2
2614 InsetTableCell head = InsetTableCell(*cellInset(cell));
2615 head.setBuffer(const_cast<Buffer &>(buffer()));
2616 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2618 dit.push_back(CursorSlice(head));
2619 head.setMacrocontextPositionRecursive(dit);
2620 bool hassep = false;
2621 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2622 head.latex(os, newrp);
2625 tail.setBuffer(head.buffer());
2627 dit.push_back(CursorSlice(tail));
2628 tail.setMacrocontextPositionRecursive(dit);
2629 tail.latex(os, newrp);
2631 } else if (!isPartOfMultiRow(row, c)) {
2632 if (!runparams.nice)
2633 os.texrow().start(par.id(), 0);
2634 inset->latex(os, newrp);
2637 runparams.encoding = newrp.encoding;
2641 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2642 if (cell != getLastCellInRow(row)) { // not last cell in row
2649 os << "\\tabularnewline";
2650 if (row_info[row].bottom_space_default) {
2652 os << "\\addlinespace";
2654 os << "[\\doublerulesep]";
2655 } else if (!row_info[row].bottom_space.zero()) {
2657 os << "\\addlinespace";
2659 << from_ascii(row_info[row].bottom_space.asLatexString())
2664 //output the bottom line
2665 TeXBottomHLine(os, row, lang);
2667 if (row_info[row].interline_space_default) {
2669 os << "\\addlinespace\n";
2671 os << "\\noalign{\\vskip\\doublerulesep}\n";
2672 } else if (!row_info[row].interline_space.zero()) {
2674 os << "\\addlinespace["
2675 << from_ascii(row_info[row].interline_space.asLatexString())
2678 os << "\\noalign{\\vskip"
2679 << from_ascii(row_info[row].interline_space.asLatexString())
2685 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2687 bool const is_tabular_star = !tabular_width.zero();
2688 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid,
2691 //+---------------------------------------------------------------------
2692 //+ first the opening preamble +
2693 //+---------------------------------------------------------------------
2696 if (!TexRow::isNone(pos))
2697 os.texrow().start(pos);
2700 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2702 if (is_long_tabular) {
2703 os << "\\begin{longtable}";
2704 switch (longtabular_alignment) {
2705 case LYX_LONGTABULAR_ALIGN_LEFT:
2708 case LYX_LONGTABULAR_ALIGN_CENTER:
2710 case LYX_LONGTABULAR_ALIGN_RIGHT:
2715 if (is_tabular_star)
2716 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2718 os << "\\begin{tabular}";
2719 switch (tabular_valignment) {
2720 case LYX_VALIGN_TOP:
2723 case LYX_VALIGN_MIDDLE:
2725 case LYX_VALIGN_BOTTOM:
2733 if (is_tabular_star)
2734 os << "@{\\extracolsep{\\fill}}";
2736 for (col_type c = 0; c < ncols(); ++c) {
2737 if (columnLeftLine(c))
2739 if (!column_info[c].align_special.empty()) {
2740 os << column_info[c].align_special;
2742 if (!column_info[c].p_width.zero()) {
2743 switch (column_info[c].alignment) {
2744 case LYX_ALIGN_LEFT:
2745 os << ">{\\raggedright}";
2747 case LYX_ALIGN_RIGHT:
2748 os << ">{\\raggedleft}";
2750 case LYX_ALIGN_CENTER:
2751 os << ">{\\centering}";
2753 case LYX_ALIGN_NONE:
2754 case LYX_ALIGN_BLOCK:
2755 case LYX_ALIGN_LAYOUT:
2756 case LYX_ALIGN_SPECIAL:
2757 case LYX_ALIGN_DECIMAL:
2761 switch (column_info[c].valignment) {
2762 case LYX_VALIGN_TOP:
2765 case LYX_VALIGN_MIDDLE:
2768 case LYX_VALIGN_BOTTOM:
2773 << from_ascii(column_info[c].p_width.asLatexString())
2776 switch (column_info[c].alignment) {
2777 case LYX_ALIGN_LEFT:
2780 case LYX_ALIGN_RIGHT:
2783 case LYX_ALIGN_DECIMAL:
2784 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2790 } // end if else !column_info[i].p_width
2791 } // end if else !column_info[i].align_special
2792 if (columnRightLine(c))
2797 TeXLongtableHeaderFooter(os, runparams);
2799 //+---------------------------------------------------------------------
2800 //+ the single row and columns (cells) +
2801 //+---------------------------------------------------------------------
2803 for (row_type r = 0; r < nrows(); ++r) {
2804 if (isValidRow(r)) {
2805 TeXRow(os, r, runparams);
2806 if (is_long_tabular && row_info[r].newpage)
2807 os << "\\newpage\n";
2811 //+---------------------------------------------------------------------
2812 //+ the closing of the tabular +
2813 //+---------------------------------------------------------------------
2815 if (is_long_tabular)
2816 os << "\\end{longtable}";
2818 if (is_tabular_star)
2819 os << "\\end{tabular*}";
2821 os << "\\end{tabular}";
2825 os << breakln << "\\end{turn}";
2827 if (!TexRow::isNone(pos))
2828 os.texrow().start(pos);
2832 int Tabular::docbookRow(odocstream & os, row_type row,
2833 OutputParams const & runparams) const
2836 idx_type cell = getFirstCellInRow(row);
2839 for (col_type c = 0; c < ncols(); ++c) {
2840 if (isPartOfMultiColumn(row, c))
2843 os << "<entry align=\"";
2844 switch (getAlignment(cell)) {
2845 case LYX_ALIGN_LEFT:
2848 case LYX_ALIGN_RIGHT:
2856 os << "\" valign=\"";
2857 switch (getVAlignment(cell)) {
2858 case LYX_VALIGN_TOP:
2861 case LYX_VALIGN_BOTTOM:
2864 case LYX_VALIGN_MIDDLE:
2869 if (isMultiColumn(cell)) {
2870 os << " namest=\"col" << c << "\" ";
2871 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2875 ret += cellInset(cell)->docbook(os, runparams);
2884 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2888 //+---------------------------------------------------------------------
2889 //+ first the opening preamble +
2890 //+---------------------------------------------------------------------
2892 os << "<tgroup cols=\"" << ncols()
2893 << "\" colsep=\"1\" rowsep=\"1\">\n";
2895 for (col_type c = 0; c < ncols(); ++c) {
2896 os << "<colspec colname=\"col" << c << "\" align=\"";
2897 switch (column_info[c].alignment) {
2898 case LYX_ALIGN_LEFT:
2901 case LYX_ALIGN_RIGHT:
2909 if (runparams.flavor == OutputParams::XML)
2915 //+---------------------------------------------------------------------
2916 //+ Long Tabular case +
2917 //+---------------------------------------------------------------------
2919 // output caption info
2920 // The caption flag wins over head/foot
2921 if (haveLTCaption()) {
2922 os << "<caption>\n";
2924 for (row_type r = 0; r < nrows(); ++r) {
2925 if (row_info[r].caption) {
2926 ret += docbookRow(os, r, runparams);
2929 os << "</caption>\n";
2932 // output header info
2933 if (haveLTHead(false) || haveLTFirstHead(false)) {
2936 for (row_type r = 0; r < nrows(); ++r) {
2937 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2938 !row_info[r].caption) {
2939 ret += docbookRow(os, r, runparams);
2945 // output footer info
2946 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2949 for (row_type r = 0; r < nrows(); ++r) {
2950 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2951 !row_info[r].caption) {
2952 ret += docbookRow(os, r, runparams);
2959 //+---------------------------------------------------------------------
2960 //+ the single row and columns (cells) +
2961 //+---------------------------------------------------------------------
2965 for (row_type r = 0; r < nrows(); ++r) {
2966 if (isValidRow(r)) {
2967 ret += docbookRow(os, r, runparams);
2972 //+---------------------------------------------------------------------
2973 //+ the closing of the tabular +
2974 //+---------------------------------------------------------------------
2983 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2984 OutputParams const & runparams, bool header) const
2987 string const celltag = header ? "th" : "td";
2988 idx_type cell = getFirstCellInRow(row);
2990 xs << html::StartTag("tr");
2991 for (col_type c = 0; c < ncols(); ++c) {
2992 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2997 switch (getAlignment(cell)) {
2998 case LYX_ALIGN_LEFT:
3001 case LYX_ALIGN_RIGHT:
3009 attr << " valign='";
3010 switch (getVAlignment(cell)) {
3011 case LYX_VALIGN_TOP:
3014 case LYX_VALIGN_BOTTOM:
3017 case LYX_VALIGN_MIDDLE:
3022 if (isMultiColumn(cell))
3023 attr << " colspan='" << columnSpan(cell) << "'";
3024 else if (isMultiRow(cell))
3025 attr << " rowspan='" << rowSpan(cell) << "'";
3027 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3028 ret += cellInset(cell)->xhtml(xs, runparams);
3029 xs << html::EndTag(celltag) << html::CR();
3032 xs << html::EndTag("tr");
3037 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3041 if (is_long_tabular) {
3042 // we'll wrap it in a div, so as to deal with alignment
3044 switch (longtabular_alignment) {
3045 case LYX_LONGTABULAR_ALIGN_LEFT:
3048 case LYX_LONGTABULAR_ALIGN_CENTER:
3051 case LYX_LONGTABULAR_ALIGN_RIGHT:
3055 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3057 // The caption flag wins over head/foot
3058 if (haveLTCaption()) {
3059 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3061 for (row_type r = 0; r < nrows(); ++r)
3062 if (row_info[r].caption)
3063 ret += xhtmlRow(xs, r, runparams);
3064 xs << html::EndTag("div") << html::CR();
3068 xs << html::StartTag("table") << html::CR();
3070 // output header info
3071 bool const havefirsthead = haveLTFirstHead(false);
3072 // if we have a first head, then we are going to ignore the
3073 // headers for the additional pages, since there aren't any
3074 // in XHTML. this test accomplishes that.
3075 bool const havehead = !havefirsthead && haveLTHead(false);
3076 if (havehead || havefirsthead) {
3077 xs << html::StartTag("thead") << html::CR();
3078 for (row_type r = 0; r < nrows(); ++r) {
3079 if (((havefirsthead && row_info[r].endfirsthead) ||
3080 (havehead && row_info[r].endhead)) &&
3081 !row_info[r].caption) {
3082 ret += xhtmlRow(xs, r, runparams, true);
3085 xs << html::EndTag("thead") << html::CR();
3087 // output footer info
3088 bool const havelastfoot = haveLTLastFoot(false);
3090 bool const havefoot = !havelastfoot && haveLTFoot(false);
3091 if (havefoot || havelastfoot) {
3092 xs << html::StartTag("tfoot") << html::CR();
3093 for (row_type r = 0; r < nrows(); ++r) {
3094 if (((havelastfoot && row_info[r].endlastfoot) ||
3095 (havefoot && row_info[r].endfoot)) &&
3096 !row_info[r].caption) {
3097 ret += xhtmlRow(xs, r, runparams);
3100 xs << html::EndTag("tfoot") << html::CR();
3103 xs << html::StartTag("tbody") << html::CR();
3104 for (row_type r = 0; r < nrows(); ++r) {
3105 if (isValidRow(r)) {
3106 ret += xhtmlRow(xs, r, runparams);
3109 xs << html::EndTag("tbody")
3111 << html::EndTag("table")
3113 if (is_long_tabular)
3114 xs << html::EndTag("div") << html::CR();
3119 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3120 vector<unsigned int> const & clen) const
3122 idx_type const fcell = getFirstCellInRow(row);
3123 idx_type const n = numberOfCellsInRow(row) + fcell;
3126 for (idx_type i = fcell; i < n; ++i) {
3136 for (idx_type i = fcell; i < n; ++i) {
3147 col_type column = cellColumn(i);
3148 int len = clen[column];
3149 while (column < ncols() - 1
3150 && isPartOfMultiColumn(row, ++column))
3151 len += clen[column] + 4;
3152 os << docstring(len, ch);
3167 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3168 vector<unsigned int> const & clen) const
3170 idx_type const fcell = getFirstCellInRow(row);
3171 idx_type const n = numberOfCellsInRow(row) + fcell;
3174 for (idx_type i = fcell; i < n; ++i) {
3175 if (bottomLine(i)) {
3184 for (idx_type i = fcell; i < n; ++i) {
3185 if (bottomLine(i)) {
3195 col_type column = cellColumn(i);
3196 int len = clen[column];
3197 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3198 len += clen[column] + 4;
3199 os << docstring(len, ch);
3200 if (bottomLine(i)) {
3214 void Tabular::plaintextPrintCell(odocstringstream & os,
3215 OutputParams const & runparams,
3216 idx_type cell, row_type row, col_type column,
3217 vector<unsigned int> const & clen,
3218 bool onlydata, size_t max_length) const
3220 odocstringstream sstr;
3221 cellInset(cell)->plaintext(sstr, runparams, max_length);
3233 unsigned int len1 = sstr.str().length();
3234 unsigned int len2 = clen[column];
3235 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3236 len2 += clen[column] + 4;
3239 switch (getAlignment(cell)) {
3241 case LYX_ALIGN_LEFT:
3244 case LYX_ALIGN_RIGHT:
3248 case LYX_ALIGN_CENTER:
3254 os << docstring(len1, ' ') << sstr.str()
3255 << docstring(len2, ' ');
3257 if (rightLine(cell))
3264 void Tabular::plaintext(odocstringstream & os,
3265 OutputParams const & runparams, int const depth,
3266 bool onlydata, char_type delim, size_t max_length) const
3268 // first calculate the width of the single columns
3269 vector<unsigned int> clen(ncols());
3272 // first all non multicolumn cells!
3273 for (col_type c = 0; c < ncols(); ++c) {
3275 for (row_type r = 0; r < nrows(); ++r) {
3276 idx_type cell = cellIndex(r, c);
3277 if (isMultiColumn(cell))
3279 odocstringstream sstr;
3280 cellInset(cell)->plaintext(sstr, runparams, max_length);
3281 if (clen[c] < sstr.str().length())
3282 clen[c] = sstr.str().length();
3285 // then all multicolumn cells!
3286 for (col_type c = 0; c < ncols(); ++c) {
3287 for (row_type r = 0; r < nrows(); ++r) {
3288 idx_type cell = cellIndex(r, c);
3289 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3291 odocstringstream sstr;
3292 cellInset(cell)->plaintext(sstr, runparams, max_length);
3293 int len = int(sstr.str().length());
3294 idx_type const n = columnSpan(cell);
3295 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3297 if (len > int(clen[c + n - 1]))
3298 clen[c + n - 1] = len;
3303 for (row_type r = 0; r < nrows(); ++r) {
3304 if (!onlydata && plaintextTopHLine(os, r, clen))
3305 os << docstring(depth * 2, ' ');
3306 for (col_type c = 0; c < ncols(); ++c) {
3307 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3309 if (onlydata && c > 0)
3310 // we don't use operator<< for single UCS4 character.
3311 // see explanation in docstream.h
3313 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3315 if (os.str().size() > max_length)
3320 os << docstring(depth * 2, ' ');
3321 if (plaintextBottomHLine(os, r, clen))
3322 os << docstring(depth * 2, ' ');
3324 if (os.str().size() > max_length)
3330 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3332 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3336 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3338 return cell_info[row][column].inset;
3342 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3344 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3348 void Tabular::setCellInset(row_type row, col_type column,
3349 shared_ptr<InsetTableCell> ins)
3351 CellData & cd = cell_info[row][column];
3356 void Tabular::validate(LaTeXFeatures & features) const
3358 features.require("NeedTabularnewline");
3360 features.require("booktabs");
3361 if (is_long_tabular)
3362 features.require("longtable");
3364 features.require("rotating");
3365 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3366 if (isMultiRow(cell))
3367 features.require("multirow");
3368 if (getVAlignment(cell) != LYX_VALIGN_TOP
3369 || !getPWidth(cell).zero())
3370 features.require("array");
3371 cellInset(cell)->validate(features);
3376 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3378 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3379 ParagraphList::const_iterator cit = parlist.begin();
3380 ParagraphList::const_iterator end = parlist.end();
3382 for (; cit != end; ++cit)
3383 for (int i = 0; i < cit->size(); ++i)
3384 if (cit->isNewline(i))
3391 /////////////////////////////////////////////////////////////////////
3395 /////////////////////////////////////////////////////////////////////
3397 InsetTableCell::InsetTableCell(Buffer * buf)
3398 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3399 contentAlign(LYX_ALIGN_CENTER)
3403 bool InsetTableCell::forcePlainLayout(idx_type) const
3405 return !isFixedWidth;
3409 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3411 return isFixedWidth;
3415 bool InsetTableCell::forceLocalFontSwitch() const
3417 return isFixedWidth;
3421 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3422 FuncStatus & status) const
3424 bool enabled = true;
3425 switch (cmd.action()) {
3426 case LFUN_MATH_DISPLAY:
3427 if (!hasFixedWidth()) {
3432 return InsetText::getStatus(cur, cmd, status);
3434 status.setEnabled(enabled);
3438 docstring InsetTableCell::asString(bool intoInsets)
3441 if (paragraphs().empty())
3443 ParagraphList::const_iterator it = paragraphs().begin();
3444 ParagraphList::const_iterator en = paragraphs().end();
3446 for (; it != en; ++it) {
3451 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3457 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3458 UpdateType utype) const
3460 InsetText::iterateForToc(di, output_active, utype);
3464 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3467 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3468 return InsetText::xhtml(xs, rp);
3473 /////////////////////////////////////////////////////////////////////
3477 /////////////////////////////////////////////////////////////////////
3479 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3481 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3482 first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
3487 InsetTabular::InsetTabular(InsetTabular const & tab)
3488 : Inset(tab), tabular(tab.tabular),
3489 first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
3494 InsetTabular::~InsetTabular()
3496 hideDialogs("tabular", this);
3500 void InsetTabular::setBuffer(Buffer & buf)
3502 tabular.setBuffer(buf);
3503 Inset::setBuffer(buf);
3507 bool InsetTabular::insetAllowed(InsetCode code) const
3512 case MATHMACRO_CODE:
3517 return tabular.is_long_tabular;
3525 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3527 return tabular.is_long_tabular &&
3528 (newtype == "Standard" || newtype == "Unnumbered");
3532 void InsetTabular::write(ostream & os) const
3534 os << "Tabular" << endl;
3539 string InsetTabular::contextMenu(BufferView const &, int, int) const
3541 // FIXME: depending on the selection state,
3542 // we could offer a different menu.
3543 return cell(0)->contextMenuName() + ";" + contextMenuName();
3547 string InsetTabular::contextMenuName() const
3549 return "context-tabular";
3553 void InsetTabular::read(Lexer & lex)
3555 //bool const old_format = (lex.getString() == "\\LyXTable");
3563 string token = lex.getString();
3564 while (lex && token != "\\end_inset") {
3566 token = lex.getString();
3569 lex.printError("Missing \\end_inset at this point. ");
3573 int InsetTabular::rowFromY(Cursor & cur, int y) const
3575 // top y coordinate of tabular
3576 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3578 for (; r < tabular.nrows() && y > h; ++r)
3579 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3580 + tabular.interRowSpace(r);
3586 int InsetTabular::columnFromX(Cursor & cur, int x) const
3588 // left x coordinate of tabular
3589 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3591 for (; c < tabular.ncols() && x > w; ++c)
3592 w += tabular.cellWidth(c);
3597 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3599 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3600 // mi.base.textwidth << "\n";
3601 LBUFERR(mi.base.bv);
3603 for (row_type r = 0; r < tabular.nrows(); ++r) {
3606 for (col_type c = 0; c < tabular.ncols(); ++c) {
3607 if (tabular.isPartOfMultiColumn(r, c)
3608 || tabular.isPartOfMultiRow(r, c))
3609 // multicolumn or multirow cell, but not first one
3611 idx_type const cell = tabular.cellIndex(r, c);
3614 Length const p_width = tabular.getPWidth(cell);
3615 if (!p_width.zero())
3616 m.base.textwidth = p_width.inPixels(mi.base);
3617 tabular.cellInset(cell)->metrics(m, dim);
3618 if (!p_width.zero())
3619 dim.wid = m.base.textwidth;
3620 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3621 + tabular.interColumnSpace(cell);
3623 // FIXME(?): do we need a second metrics call?
3624 TextMetrics const & tm =
3625 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3627 // determine horizontal offset because of decimal align (if necessary)
3628 int decimal_width = 0;
3629 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3630 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3631 tail.setBuffer(tabular.buffer());
3632 // we need to set macrocontext position everywhere
3633 // otherwise we crash with nested insets (e.g. footnotes)
3634 // after decimal point
3635 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3637 dit.push_back(CursorSlice(tail));
3638 tail.setMacrocontextPositionRecursive(dit);
3640 // remove text leading decimal point
3641 docstring const align_d = tabular.column_info[c].decimal_point;
3642 dit = separatorPos(&tail, align_d);
3644 pit_type const psize = tail.paragraphs().front().size();
3646 tail.paragraphs().front().eraseChars(0,
3647 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3649 tail.metrics(m, dim1);
3650 decimal_width = dim1.width();
3653 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3654 tabular.cell_info[r][c].decimal_width = decimal_width;
3656 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3657 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3658 int const lastpardes = tm.last().second->descent()
3659 + TEXT_TO_INSET_OFFSET;
3661 switch (tabular.getVAlignment(cell)) {
3662 case Tabular::LYX_VALIGN_TOP:
3664 case Tabular::LYX_VALIGN_MIDDLE:
3665 offset = -(dim.des - lastpardes)/2;
3667 case Tabular::LYX_VALIGN_BOTTOM:
3668 offset = -(dim.des - lastpardes);
3671 tabular.cell_info[r][c].voffset = offset;
3672 maxasc = max(maxasc, dim.asc - offset);
3673 maxdes = max(maxdes, dim.des + offset);
3675 int const top_space = tabular.row_info[r].top_space_default ?
3676 default_line_space :
3677 tabular.row_info[r].top_space.inPixels(mi.base);
3678 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3679 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3680 default_line_space :
3681 tabular.row_info[r].bottom_space.inPixels(mi.base);
3682 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3685 // for top-alignment the first horizontal table line must be exactly at
3686 // the position of the base line of the surrounding text line
3687 // for bottom alignment, the same is for the last table line
3688 switch (tabular.tabular_valignment) {
3689 case Tabular::LYX_VALIGN_BOTTOM:
3690 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3692 case Tabular::LYX_VALIGN_MIDDLE:
3693 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3695 case Tabular::LYX_VALIGN_TOP:
3696 offset_valign_ = tabular.rowAscent(0);
3700 tabular.updateColumnWidths();
3701 dim.asc = tabular.rowAscent(0) - offset_valign_;
3702 dim.des = tabular.height() - dim.asc;
3703 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3707 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3710 if (&cur.inset() == this && cur.selection()) {
3711 if (cur.selIsMultiCell()) {
3714 getSelection(cur, rs, re, cs, ce);
3716 idx_type const cell = tabular.cellIndex(row, col);
3717 col_type const cspan = tabular.columnSpan(cell);
3718 row_type const rspan = tabular.rowSpan(cell);
3719 if (col + cspan - 1 >= cs && col <= ce
3720 && row + rspan - 1 >= rs && row <= re)
3723 if (col == tabular.cellColumn(cur.idx())
3724 && row == tabular.cellRow(cur.idx())) {
3725 CursorSlice const & beg = cur.selBegin();
3726 CursorSlice const & end = cur.selEnd();
3728 if ((end.lastpos() > 0 || end.lastpit() > 0)
3729 && end.pos() == end.lastpos() && beg.pos() == 0
3730 && end.pit() == end.lastpit() && beg.pit() == 0)
3738 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3740 x += ADD_TO_TABULAR_WIDTH;
3742 BufferView * bv = pi.base.bv;
3743 Cursor & cur = pi.base.bv->cursor();
3745 // FIXME: As the full background is painted in drawBackground(),
3746 // we have no choice but to do a full repaint for the Text cells.
3747 pi.full_repaint = true;
3749 bool const original_selection_state = pi.selected;
3752 first_visible_cell_ = Tabular::npos;
3754 int yy = y + offset_valign_;
3755 for (row_type r = 0; r < tabular.nrows(); ++r) {
3757 for (col_type c = 0; c < tabular.ncols(); ++c) {
3758 if (tabular.isPartOfMultiColumn(r, c))
3761 idx = tabular.cellIndex(r, c);
3763 if (tabular.isPartOfMultiRow(r, c)) {
3764 nx += tabular.cellWidth(idx);
3768 if (first_visible_cell_ == Tabular::npos)
3769 first_visible_cell_ = idx;
3771 pi.selected |= isCellSelected(cur, r, c);
3772 int const cx = nx + tabular.textHOffset(idx);
3773 int const cy = yy + tabular.textVOffset(idx);
3774 // Cache the Inset position.
3775 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3776 cell(idx)->draw(pi, cx, cy);
3777 drawCellLines(pi, nx, yy, r, idx);
3778 nx += tabular.cellWidth(idx);
3779 pi.selected = original_selection_state;
3782 if (r + 1 < tabular.nrows())
3783 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3784 + tabular.interRowSpace(r + 1);
3789 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3791 x += ADD_TO_TABULAR_WIDTH;
3792 y += offset_valign_ - tabular.rowAscent(0);
3793 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3794 pi.backgroundColor(this));
3798 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3800 Cursor & cur = pi.base.bv->cursor();
3802 x += ADD_TO_TABULAR_WIDTH;
3804 if (!cur.selection())
3806 if (&cur.inset() != this)
3811 bool const full_cell_selected = isCellSelected(cur,
3812 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3814 if (cur.selIsMultiCell() || full_cell_selected) {
3815 for (row_type r = 0; r < tabular.nrows(); ++r) {
3817 for (col_type c = 0; c < tabular.ncols(); ++c) {
3818 if (tabular.isPartOfMultiColumn(r, c))
3821 idx_type const cell = tabular.cellIndex(r, c);
3823 if (tabular.isPartOfMultiRow(r, c)) {
3824 xx += tabular.cellWidth(cell);
3827 int const w = tabular.cellWidth(cell);
3828 int const h = tabular.cellHeight(cell);
3829 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3830 if (isCellSelected(cur, r, c))
3831 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3834 if (r + 1 < tabular.nrows())
3835 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3836 + tabular.interRowSpace(r + 1);
3840 // FIXME: This code has no effect because InsetTableCell does not handle
3841 // drawSelection other than the trivial implementation in Inset.
3843 // x += cellXPos(cur.idx());
3844 // x += tabular.textHOffset(cur.idx());
3845 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3850 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3851 row_type row, idx_type cell) const
3853 y -= tabular.rowAscent(row);
3854 int const w = tabular.cellWidth(cell);
3855 int const h = tabular.cellHeight(cell);
3856 Color const linecolor = pi.textColor(Color_tabularline);
3857 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3860 bool drawline = tabular.topLine(cell)
3861 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3862 pi.pain.line(x, y, x + w, y,
3863 drawline ? linecolor : gridcolor,
3864 drawline ? Painter::line_solid : Painter::line_onoffdash);
3867 drawline = tabular.bottomLine(cell);
3868 pi.pain.line(x, y + h, x + w, y + h,
3869 drawline ? linecolor : gridcolor,
3870 drawline ? Painter::line_solid : Painter::line_onoffdash);
3873 col_type const col = tabular.cellColumn(cell);
3874 drawline = tabular.leftLine(cell)
3875 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3876 pi.pain.line(x, y, x, y + h,
3877 drawline ? linecolor : gridcolor,
3878 drawline ? Painter::line_solid : Painter::line_onoffdash);
3881 x -= tabular.interColumnSpace(cell);
3882 col_type next_cell_col = col + 1;
3883 while (next_cell_col < tabular.ncols()
3884 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3886 drawline = tabular.rightLine(cell)
3887 || (next_cell_col < tabular.ncols()
3888 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3889 pi.pain.line(x + w, y, x + w, y + h,
3890 drawline ? linecolor : gridcolor,
3891 drawline ? Painter::line_solid : Painter::line_onoffdash);
3895 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3897 //lyxerr << "InsetTabular::edit: " << this << endl;
3901 if (isRightToLeft(cur))
3902 cur.idx() = tabular.getLastCellInRow(0);
3908 if (isRightToLeft(cur))
3909 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3911 cur.idx() = tabular.numberofcells - 1;
3913 cur.pos() = cur.lastpos(); // FIXME crude guess
3915 cur.setCurrentFont();
3916 // FIXME: this accesses the position cache before it is initialized
3917 //cur.bv().fitCursor();
3921 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3923 // In a longtable, tell captions what the current float is
3924 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3925 string const saveflt = cnts.current_float();
3926 if (tabular.is_long_tabular) {
3927 cnts.current_float("table");
3928 // in longtables, we only step the counter once
3929 cnts.step(from_ascii("table"), utype);
3930 cnts.isLongtable(true);
3933 ParIterator it2 = it;
3935 size_t const end = it2.nargs();
3936 for ( ; it2.idx() < end; it2.top().forwardIdx())
3937 buffer().updateBuffer(it2, utype);
3940 if (tabular.is_long_tabular) {
3941 cnts.current_float(saveflt);
3942 cnts.isLongtable(false);
3947 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3948 UpdateType utype) const
3950 DocIterator dit = cpit;
3952 size_t const end = dit.nargs();
3953 for ( ; dit.idx() < end; dit.top().forwardIdx())
3954 cell(dit.idx())->addToToc(dit, output_active, utype);
3958 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
3960 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
3961 return x < x0 || x > x0 + tabular.width();
3965 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
3967 int const y0 = yo(bv) - tabular.rowAscent(0) + offset_valign_;
3968 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
3969 // there is no margin added vertically to tabular insets.
3970 // However, it works for now.
3971 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
3975 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
3977 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
3981 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3983 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3984 << "\n cur:" << cur);
3985 CursorSlice sl = cur.top();
3986 Cursor & bvcur = cur.bv().cursor();
3988 FuncCode const act = cmd.action();
3992 case LFUN_MOUSE_PRESS: {
3993 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3995 if (hitSelectRow(cur.bv(), cmd.x())) {
3996 row_type r = rowFromY(cur, cmd.y());
3997 cur.idx() = tabular.getFirstCellInRow(r);
4001 cur.idx() = tabular.getLastCellInRow(r);
4002 cur.pit() = cur.lastpit();
4003 cur.pos() = cur.lastpos();
4004 cur.selection(true);
4010 if (hitSelectColumn(cur.bv(), cmd.y())) {
4011 col_type c = columnFromX(cur, cmd.x());
4012 cur.idx() = tabular.cellIndex(0, c);
4016 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4017 cur.pit() = cur.lastpit();
4018 cur.pos() = cur.lastpos();
4019 cur.selection(true);
4024 // do not reset cursor/selection if we have selected
4025 // some cells (bug 2715).
4026 if (cmd.button() == mouse_button::button3
4027 && &bvcur.selBegin().inset() == this
4028 && bvcur.selIsMultiCell())
4031 // Let InsetTableCell do it
4032 cell(cur.idx())->dispatch(cur, cmd);
4035 case LFUN_MOUSE_MOTION:
4036 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4037 if (cmd.button() == mouse_button::button1) {
4038 // only accept motions to places not deeper nested than the real anchor
4039 if (!bvcur.realAnchor().hasPart(cur)) {
4043 // select (additional) row
4045 row_type r = rowFromY(cur, cmd.y());
4046 cur.idx() = tabular.getLastCellInRow(r);
4047 // we need to reset the cursor's pit and pos now, as the old ones
4048 // may no longer be valid.
4051 bvcur.setCursor(cur);
4052 bvcur.selection(true);
4055 // select (additional) column
4057 col_type c = columnFromX(cur, cmd.x());
4058 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4059 // we need to reset the cursor's pit and pos now, as the old ones
4060 // may no longer be valid.
4063 bvcur.setCursor(cur);
4064 bvcur.selection(true);
4067 // only update if selection changes
4068 if (bvcur.idx() == cur.idx() &&
4069 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4070 cur.noScreenUpdate();
4071 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4072 bvcur.setCursor(cur);
4073 bvcur.selection(true);
4074 // if this is a multicell selection, we just set the cursor to
4075 // the beginning of the cell's text.
4076 if (bvcur.selIsMultiCell()) {
4077 bvcur.pit() = bvcur.lastpit();
4078 bvcur.pos() = bvcur.lastpos();
4083 case LFUN_MOUSE_RELEASE:
4088 case LFUN_CELL_BACKWARD:
4090 cur.selection(false);
4093 case LFUN_CELL_FORWARD:
4095 cur.selection(false);
4098 case LFUN_CHAR_FORWARD_SELECT:
4099 case LFUN_CHAR_FORWARD:
4100 case LFUN_CHAR_BACKWARD_SELECT:
4101 case LFUN_CHAR_BACKWARD:
4102 case LFUN_CHAR_RIGHT_SELECT:
4103 case LFUN_CHAR_RIGHT:
4104 case LFUN_CHAR_LEFT_SELECT:
4105 case LFUN_CHAR_LEFT:
4106 case LFUN_WORD_FORWARD:
4107 case LFUN_WORD_FORWARD_SELECT:
4108 case LFUN_WORD_BACKWARD:
4109 case LFUN_WORD_BACKWARD_SELECT:
4110 case LFUN_WORD_RIGHT:
4111 case LFUN_WORD_RIGHT_SELECT:
4112 case LFUN_WORD_LEFT:
4113 case LFUN_WORD_LEFT_SELECT: {
4114 // determine whether we move to next or previous cell, where to enter
4115 // the new cell from, and which command to "finish" (i.e., exit the
4118 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4119 FuncCode finish_lfun;
4121 if (act == LFUN_CHAR_FORWARD
4122 || act == LFUN_CHAR_FORWARD_SELECT
4123 || act == LFUN_WORD_FORWARD
4124 || act == LFUN_WORD_FORWARD_SELECT) {
4126 finish_lfun = LFUN_FINISHED_FORWARD;
4128 else if (act == LFUN_CHAR_BACKWARD
4129 || act == LFUN_CHAR_BACKWARD_SELECT
4130 || act == LFUN_WORD_BACKWARD
4131 || act == LFUN_WORD_BACKWARD_SELECT) {
4133 finish_lfun = LFUN_FINISHED_BACKWARD;
4135 // LEFT or RIGHT commands --- the interpretation will depend on the
4136 // table's direction.
4138 bool const right = act == LFUN_CHAR_RIGHT
4139 || act == LFUN_CHAR_RIGHT_SELECT
4140 || act == LFUN_WORD_RIGHT
4141 || act == LFUN_WORD_RIGHT_SELECT;
4142 next_cell = isRightToLeft(cur) != right;
4144 if (lyxrc.visual_cursor)
4145 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4147 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4150 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4151 || act == LFUN_CHAR_BACKWARD_SELECT
4152 || act == LFUN_CHAR_RIGHT_SELECT
4153 || act == LFUN_CHAR_LEFT_SELECT
4154 || act == LFUN_WORD_FORWARD_SELECT
4155 || act == LFUN_WORD_RIGHT_SELECT
4156 || act == LFUN_WORD_BACKWARD_SELECT
4157 || act == LFUN_WORD_LEFT_SELECT;
4159 // If we have a multicell selection or we're
4160 // not doing some LFUN_*_SELECT thing anyway...
4161 if (!cur.selIsMultiCell() || !select) {
4162 col_type const c = tabular.cellColumn(cur.idx());
4163 row_type const r = tabular.cellRow(cur.idx());
4164 // Are we trying to select the whole cell and is the whole cell
4165 // not yet selected?
4166 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4167 ((next_cell && cur.pit() == cur.lastpit()
4168 && cur.pos() == cur.lastpos())
4169 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4171 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4173 // ...try to dispatch to the cell's inset.
4174 cell(cur.idx())->dispatch(cur, cmd);
4176 // When we already have a selection we want to select the whole cell
4177 // before going to the next cell.
4178 if (select_whole && !empty_cell){
4179 getText(cur.idx())->selectAll(cur);
4181 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4185 // FIXME: When we support the selection of an empty cell, remove
4186 // the !empty_cell from this condition. For now we jump to the next
4187 // cell if the current cell is empty.
4188 if (cur.result().dispatched() && !empty_cell)
4192 // move to next/prev cell, as appropriate
4193 // note that we will always do this if we're selecting and we have
4194 // a multicell selection
4195 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4196 << " cell from: " << int(entry_from));
4198 moveNextCell(cur, entry_from);
4200 movePrevCell(cur, entry_from);
4201 // if we're exiting the table, call the appropriate FINISHED lfun
4202 if (sl == cur.top()) {
4203 cmd = FuncRequest(finish_lfun);
4208 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4213 case LFUN_DOWN_SELECT:
4215 if (!(cur.selection() && cur.selIsMultiCell()))
4216 cell(cur.idx())->dispatch(cur, cmd);
4218 cur.dispatched(); // override the cell's decision
4219 if (sl == cur.top()) {
4220 // if our Text didn't do anything to the cursor
4221 // then we try to put the cursor into the cell below
4222 // setting also the right targetX.
4223 cur.selHandle(act == LFUN_DOWN_SELECT);
4224 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4225 int const xtarget = cur.targetX();
4226 // WARNING: Once cur.idx() has been reset, the cursor is in
4227 // an inconsistent state until pos() has been set. Be careful
4228 // what you do with it!
4229 cur.idx() = tabular.cellBelow(cur.idx());
4231 TextMetrics const & tm =
4232 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4233 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4234 cur.setCurrentFont();
4237 if (sl == cur.top()) {
4238 // we trick it to go to forward after leaving the
4240 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4243 if (cur.selIsMultiCell()) {
4244 cur.pit() = cur.lastpit();
4245 cur.pos() = cur.lastpos();
4246 cur.setCurrentFont();
4247 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4250 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4253 case LFUN_UP_SELECT:
4255 if (!(cur.selection() && cur.selIsMultiCell()))
4256 cell(cur.idx())->dispatch(cur, cmd);
4257 cur.dispatched(); // override the cell's decision
4258 if (sl == cur.top()) {
4259 // if our Text didn't do anything to the cursor
4260 // then we try to put the cursor into the cell above
4261 // setting also the right targetX.
4262 cur.selHandle(act == LFUN_UP_SELECT);
4263 if (tabular.cellRow(cur.idx()) != 0) {
4264 int const xtarget = cur.targetX();
4265 // WARNING: Once cur.idx() has been reset, the cursor is in
4266 // an inconsistent state until pos() has been set. Be careful
4267 // what you do with it!
4268 cur.idx() = tabular.cellAbove(cur.idx());
4269 cur.pit() = cur.lastpit();
4270 Text const * text = cell(cur.idx())->getText(0);
4271 TextMetrics const & tm = cur.bv().textMetrics(text);
4272 ParagraphMetrics const & pm =
4273 tm.parMetrics(cur.lastpit());
4274 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4275 cur.setCurrentFont();
4278 if (sl == cur.top()) {
4279 cmd = FuncRequest(LFUN_UP);
4282 if (cur.selIsMultiCell()) {
4284 cur.pos() = cur.lastpos();
4285 cur.setCurrentFont();
4286 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4289 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4292 // case LFUN_SCREEN_DOWN: {
4293 // //if (hasSelection())
4294 // // cur.selection() = false;
4295 // col_type const col = tabular.cellColumn(cur.idx());
4296 // int const t = cur.bv().top_y() + cur.bv().height();
4297 // if (t < yo() + tabular.getHeightOfTabular()) {
4298 // cur.bv().scrollDocView(t, true);
4299 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4301 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4308 // case LFUN_SCREEN_UP: {
4309 // //if (hasSelection())
4310 // // cur.selection() = false;
4311 // col_type const col = tabular.cellColumn(cur.idx());
4312 // int const t = cur.bv().top_y() + cur.bv().height();
4314 // cur.bv().scrollDocView(t, true);
4318 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4322 // cur.par() = cur.lastpar();
4323 // cur.pos() = cur.lastpos();
4327 case LFUN_LAYOUT_TABULAR:
4328 cur.bv().showDialog("tabular");
4331 case LFUN_INSET_MODIFY:
4332 // we come from the dialog
4333 if (cmd.getArg(0) == "tabular")
4334 tabularFeatures(cur, cmd.getLongArg(1));
4339 case LFUN_TABULAR_FEATURE:
4340 tabularFeatures(cur, to_utf8(cmd.argument()));
4343 // insert file functions
4344 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4345 case LFUN_FILE_INSERT_PLAINTEXT:
4347 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4348 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4349 FileName(to_utf8(cmd.argument())));
4352 cur.recordUndoInset();
4353 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4354 // content has been replaced,
4355 // so cursor might be invalid
4356 cur.pos() = cur.lastpos();
4357 cur.pit() = cur.lastpit();
4358 bvcur.setCursor(cur);
4365 if (cur.selIsMultiCell()) {
4366 if (copySelection(cur)) {
4367 cur.recordUndoInset();
4371 cell(cur.idx())->dispatch(cur, cmd);
4374 case LFUN_SELF_INSERT:
4375 if (cur.selIsMultiCell()) {
4376 cur.recordUndoInset();
4378 BufferView * bv = &cur.bv();
4379 docstring::const_iterator cit = cmd.argument().begin();
4380 docstring::const_iterator const end = cmd.argument().end();
4381 for (; cit != end; ++cit)
4382 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4385 bv->bookmarkEditPosition();
4387 cell(cur.idx())->dispatch(cur, cmd);
4390 case LFUN_CHAR_DELETE_BACKWARD:
4391 case LFUN_CHAR_DELETE_FORWARD:
4392 if (cur.selIsMultiCell()) {
4393 cur.recordUndoInset();
4396 cell(cur.idx())->dispatch(cur, cmd);
4400 if (!cur.selection())
4402 if (cur.selIsMultiCell()) {
4406 cell(cur.idx())->dispatch(cur, cmd);
4409 case LFUN_CLIPBOARD_PASTE:
4410 case LFUN_PRIMARY_SELECTION_PASTE: {
4411 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4412 theClipboard().getAsText(Clipboard::PlainTextType) :
4413 theSelection().get();
4416 // pass to InsertPlaintextString, but
4417 // only if we have multi-cell content
4418 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4419 cur.recordUndoInset();
4420 if (insertPlaintextString(cur.bv(), clip, false)) {
4421 // content has been replaced,
4422 // so cursor might be invalid
4423 cur.pos() = cur.lastpos();
4424 cur.pit() = cur.lastpit();
4425 bvcur.setCursor(cur);
4429 // Let the cell handle normal text
4430 cell(cur.idx())->dispatch(cur, cmd);
4435 if (!tabularStackDirty()) {
4436 if (!cur.selIsMultiCell())
4437 cell(cur.idx())->dispatch(cur, cmd);
4440 if (theClipboard().isInternal()) {
4441 cur.recordUndoInset();
4442 pasteClipboard(cur);
4446 case LFUN_FONT_EMPH:
4447 case LFUN_FONT_BOLD:
4448 case LFUN_FONT_BOLDSYMBOL:
4449 case LFUN_FONT_ROMAN:
4450 case LFUN_FONT_NOUN:
4451 case LFUN_FONT_ITAL:
4452 case LFUN_FONT_FRAK:
4453 case LFUN_FONT_TYPEWRITER:
4454 case LFUN_FONT_SANS:
4455 case LFUN_TEXTSTYLE_APPLY:
4456 case LFUN_TEXTSTYLE_UPDATE:
4457 case LFUN_FONT_SIZE:
4458 case LFUN_FONT_UNDERLINE:
4459 case LFUN_FONT_STRIKEOUT:
4460 case LFUN_FONT_UNDERUNDERLINE:
4461 case LFUN_FONT_UNDERWAVE:
4463 case LFUN_PARAGRAPH_PARAMS_APPLY:
4464 case LFUN_PARAGRAPH_PARAMS:
4465 case LFUN_WORD_CAPITALIZE:
4466 case LFUN_WORD_UPCASE:
4467 case LFUN_WORD_LOWCASE:
4468 case LFUN_CHARS_TRANSPOSE:
4469 if (cur.selIsMultiCell()) {
4472 getSelection(cur, rs, re, cs, ce);
4473 Cursor tmpcur = cur;
4474 for (row_type r = rs; r <= re; ++r) {
4475 for (col_type c = cs; c <= ce; ++c) {
4476 // cursor follows cell:
4477 tmpcur.idx() = tabular.cellIndex(r, c);
4478 // select this cell only:
4481 tmpcur.resetAnchor();
4482 tmpcur.pit() = tmpcur.lastpit();
4483 tmpcur.pos() = tmpcur.top().lastpos();
4484 tmpcur.setCursor(tmpcur);
4485 tmpcur.setSelection();
4486 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4491 cell(cur.idx())->dispatch(cur, cmd);
4495 case LFUN_INSET_SETTINGS:
4496 // relay this lfun to Inset, not to the cell.
4497 Inset::doDispatch(cur, cmd);
4501 // we try to handle this event in the insets dispatch function.
4502 cell(cur.idx())->dispatch(cur, cmd);
4508 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4509 string const & argument, FuncStatus & status) const
4512 int action = Tabular::LAST_ACTION;
4514 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4515 if (tabularFeature[i].feature == s) {
4516 action = tabularFeature[i].action;
4520 if (action == Tabular::LAST_ACTION) {
4522 status.setUnknown(true);
4526 row_type sel_row_start = 0;
4527 row_type sel_row_end = 0;
4528 col_type sel_col_start = 0;
4529 col_type sel_col_end = 0;
4530 Tabular::ltType dummyltt;
4533 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4536 case Tabular::SET_PWIDTH:
4537 case Tabular::SET_MPWIDTH:
4538 case Tabular::SET_SPECIAL_COLUMN:
4539 case Tabular::SET_SPECIAL_MULTICOLUMN:
4540 case Tabular::APPEND_ROW:
4541 case Tabular::APPEND_COLUMN:
4542 case Tabular::DELETE_ROW:
4543 case Tabular::DELETE_COLUMN:
4544 case Tabular::COPY_ROW:
4545 case Tabular::COPY_COLUMN:
4546 case Tabular::SET_TOP_SPACE:
4547 case Tabular::SET_BOTTOM_SPACE:
4548 case Tabular::SET_INTERLINE_SPACE:
4552 case Tabular::SET_TABULAR_WIDTH:
4553 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4554 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4557 case Tabular::MOVE_COLUMN_RIGHT:
4558 case Tabular::MOVE_COLUMN_LEFT:
4559 case Tabular::MOVE_ROW_DOWN:
4560 case Tabular::MOVE_ROW_UP: {
4561 if (cur.selection()) {
4562 status.message(_("Selections not supported."));
4563 status.setEnabled(false);
4567 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4568 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4569 (action == Tabular::MOVE_COLUMN_LEFT &&
4570 tabular.cellColumn(cur.idx()) == 0) ||
4571 (action == Tabular::MOVE_ROW_DOWN &&
4572 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4573 (action == Tabular::MOVE_ROW_UP &&
4574 tabular.cellRow(cur.idx()) == 0)) {
4575 status.setEnabled(false);
4579 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4580 action == Tabular::MOVE_COLUMN_LEFT) {
4581 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4582 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4583 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4584 status.message(_("Multi-column in current or"
4585 " destination column."));
4586 status.setEnabled(false);
4591 if (action == Tabular::MOVE_ROW_DOWN ||
4592 action == Tabular::MOVE_ROW_UP) {
4593 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4594 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4595 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4596 status.message(_("Multi-row in current or"
4597 " destination row."));
4598 status.setEnabled(false);
4603 status.setEnabled(true);
4607 case Tabular::SET_DECIMAL_POINT:
4609 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4612 case Tabular::SET_MULTICOLUMN:
4613 case Tabular::UNSET_MULTICOLUMN:
4614 case Tabular::MULTICOLUMN:
4615 // If a row is set as longtable caption, it must not be allowed
4616 // to unset that this row is a multicolumn.
4617 status.setEnabled(sel_row_start == sel_row_end
4618 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4619 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4622 case Tabular::SET_MULTIROW:
4623 case Tabular::UNSET_MULTIROW:
4624 case Tabular::MULTIROW:
4625 // If a row is set as longtable caption, it must not be allowed
4626 // to unset that this row is a multirow.
4627 status.setEnabled(sel_col_start == sel_col_end
4628 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4629 status.setOnOff(tabular.isMultiRow(cur.idx()));
4632 case Tabular::SET_ALL_LINES:
4633 case Tabular::UNSET_ALL_LINES:
4634 case Tabular::SET_BORDER_LINES:
4635 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4638 case Tabular::SET_LINE_TOP:
4639 case Tabular::SET_LINE_BOTTOM:
4640 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4643 case Tabular::SET_LINE_LEFT:
4644 case Tabular::SET_LINE_RIGHT:
4645 status.setEnabled(!tabular.use_booktabs
4646 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4649 case Tabular::TOGGLE_LINE_TOP:
4650 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4651 status.setOnOff(tabular.topLine(cur.idx()));
4654 case Tabular::TOGGLE_LINE_BOTTOM:
4655 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4656 status.setOnOff(tabular.bottomLine(cur.idx()));
4659 case Tabular::TOGGLE_LINE_LEFT:
4660 status.setEnabled(!tabular.use_booktabs
4661 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4662 status.setOnOff(tabular.leftLine(cur.idx()));
4665 case Tabular::TOGGLE_LINE_RIGHT:
4666 status.setEnabled(!tabular.use_booktabs
4667 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4668 status.setOnOff(tabular.rightLine(cur.idx()));
4671 // multirow cells only inherit the alignment of the column if the column has
4672 // no width, otherwise they are left-aligned
4673 // therefore allow always left but right and center only if there is no width
4674 case Tabular::M_ALIGN_LEFT:
4676 case Tabular::ALIGN_LEFT:
4677 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4680 case Tabular::M_ALIGN_RIGHT:
4682 case Tabular::ALIGN_RIGHT:
4683 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4684 && !tabular.getPWidth(cur.idx()).zero()));
4685 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4688 case Tabular::M_ALIGN_CENTER:
4690 case Tabular::ALIGN_CENTER:
4691 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4692 && !tabular.getPWidth(cur.idx()).zero()));
4693 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4696 case Tabular::ALIGN_BLOCK:
4697 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4698 && !tabular.isMultiRow(cur.idx()));
4699 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4702 case Tabular::ALIGN_DECIMAL:
4703 status.setEnabled(!tabular.isMultiRow(cur.idx())
4704 && !tabular.isMultiColumn(cur.idx()));
4705 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4708 case Tabular::M_VALIGN_TOP:
4710 case Tabular::VALIGN_TOP:
4711 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4712 && !tabular.isMultiRow(cur.idx()));
4714 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4717 case Tabular::M_VALIGN_BOTTOM:
4719 case Tabular::VALIGN_BOTTOM:
4720 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4721 && !tabular.isMultiRow(cur.idx()));
4723 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4726 case Tabular::M_VALIGN_MIDDLE:
4728 case Tabular::VALIGN_MIDDLE:
4729 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4730 && !tabular.isMultiRow(cur.idx()));
4732 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4735 case Tabular::SET_LONGTABULAR:
4736 case Tabular::TOGGLE_LONGTABULAR:
4737 // setting as longtable is not allowed when table is inside a float
4738 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4739 || cur.innerInsetOfType(WRAP_CODE) != 0)
4740 status.setEnabled(false);
4742 status.setEnabled(true);
4743 status.setOnOff(tabular.is_long_tabular);
4746 case Tabular::UNSET_LONGTABULAR:
4747 status.setOnOff(!tabular.is_long_tabular);
4750 case Tabular::TOGGLE_ROTATE_TABULAR:
4751 case Tabular::SET_ROTATE_TABULAR:
4752 status.setOnOff(tabular.rotate != 0);
4755 case Tabular::TABULAR_VALIGN_TOP:
4756 status.setEnabled(tabular.tabular_width.zero());
4757 status.setOnOff(tabular.tabular_valignment
4758 == Tabular::LYX_VALIGN_TOP);
4760 case Tabular::TABULAR_VALIGN_MIDDLE:
4761 status.setEnabled(tabular.tabular_width.zero());
4762 status.setOnOff(tabular.tabular_valignment
4763 == Tabular::LYX_VALIGN_MIDDLE);
4765 case Tabular::TABULAR_VALIGN_BOTTOM:
4766 status.setEnabled(tabular.tabular_width.zero());
4767 status.setOnOff(tabular.tabular_valignment
4768 == Tabular::LYX_VALIGN_BOTTOM);
4771 case Tabular::LONGTABULAR_ALIGN_LEFT:
4772 status.setOnOff(tabular.longtabular_alignment
4773 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4775 case Tabular::LONGTABULAR_ALIGN_CENTER:
4776 status.setOnOff(tabular.longtabular_alignment
4777 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4779 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4780 status.setOnOff(tabular.longtabular_alignment
4781 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4784 case Tabular::UNSET_ROTATE_TABULAR:
4785 status.setOnOff(tabular.rotate == 0);
4788 case Tabular::TOGGLE_ROTATE_CELL:
4789 case Tabular::SET_ROTATE_CELL:
4790 status.setOnOff(!oneCellHasRotationState(false,
4791 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4794 case Tabular::UNSET_ROTATE_CELL:
4795 status.setOnOff(!oneCellHasRotationState(true,
4796 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4799 case Tabular::SET_USEBOX:
4800 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4803 // every row can only be one thing:
4804 // either a footer or header
4805 case Tabular::SET_LTFIRSTHEAD:
4806 status.setEnabled(sel_row_start == sel_row_end);
4807 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4810 case Tabular::UNSET_LTFIRSTHEAD:
4811 status.setEnabled(sel_row_start == sel_row_end);
4812 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4815 case Tabular::SET_LTHEAD:
4816 status.setEnabled(sel_row_start == sel_row_end);
4817 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4820 case Tabular::UNSET_LTHEAD:
4821 status.setEnabled(sel_row_start == sel_row_end);
4822 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4825 case Tabular::SET_LTFOOT:
4826 status.setEnabled(sel_row_start == sel_row_end);
4827 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4830 case Tabular::UNSET_LTFOOT:
4831 status.setEnabled(sel_row_start == sel_row_end);
4832 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4835 case Tabular::SET_LTLASTFOOT:
4836 status.setEnabled(sel_row_start == sel_row_end);
4837 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4840 case Tabular::UNSET_LTLASTFOOT:
4841 status.setEnabled(sel_row_start == sel_row_end);
4842 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4845 case Tabular::SET_LTNEWPAGE:
4846 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4848 case Tabular::UNSET_LTNEWPAGE:
4849 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4852 // only one row in head/firsthead/foot/lasthead can be the caption
4853 // and a multirow cannot be set as caption
4854 case Tabular::SET_LTCAPTION:
4855 status.setEnabled(sel_row_start == sel_row_end
4856 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4857 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4858 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4859 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4860 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4861 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4862 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4863 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4864 && !tabular.isMultiRow(sel_row_start));
4865 status.setOnOff(tabular.ltCaption(sel_row_start));
4868 case Tabular::UNSET_LTCAPTION:
4869 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4872 case Tabular::TOGGLE_LTCAPTION:
4873 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4874 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4875 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4876 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4877 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4878 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4879 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4880 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4881 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4882 status.setOnOff(tabular.ltCaption(sel_row_start));
4885 case Tabular::TOGGLE_BOOKTABS:
4886 case Tabular::SET_BOOKTABS:
4887 status.setOnOff(tabular.use_booktabs);
4890 case Tabular::UNSET_BOOKTABS:
4891 status.setOnOff(!tabular.use_booktabs);
4896 status.setEnabled(false);
4903 // function sets an object as defined in FuncStatus.h:
4904 // states OK, Unknown, Disabled, On, Off.
4905 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4906 FuncStatus & status) const
4908 switch (cmd.action()) {
4909 case LFUN_INSET_MODIFY:
4910 if (cmd.getArg(0) != "tabular")
4912 if (cmd.getArg(1) == "for-dialog") {
4913 // The dialog is asking the status of a command
4914 if (&cur.inset() != this)
4916 string action = cmd.getArg(2);
4917 string arg = cmd.getLongArg(3);
4918 return getFeatureStatus(cur, action, arg, status);
4920 // We always enable the lfun if it is coming from the dialog
4921 // because the dialog makes sure all the settings are valid,
4922 // even though the first argument might not be valid now.
4923 status.setEnabled(true);
4927 case LFUN_TABULAR_FEATURE: {
4928 if (&cur.inset() != this)
4930 string action = cmd.getArg(0);
4931 string arg = cmd.getLongArg(1);
4932 return getFeatureStatus(cur, action, arg, status);
4935 case LFUN_CAPTION_INSERT: {
4936 // caption is only allowed in caption cell of longtable
4937 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4938 status.setEnabled(false);
4941 // only standard caption is allowed
4942 string arg = cmd.getArg(0);
4943 if (!arg.empty() && arg != "Standard") {
4944 status.setEnabled(false);
4947 // check if there is already a caption
4948 bool have_caption = false;
4949 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
4950 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4951 ParagraphList::const_iterator pend = itc.paragraphs().end();
4952 for (; pit != pend; ++pit) {
4953 InsetList::const_iterator it = pit->insetList().begin();
4954 InsetList::const_iterator end = pit->insetList().end();
4955 for (; it != end; ++it) {
4956 if (it->inset->lyxCode() == CAPTION_CODE) {
4957 have_caption = true;
4962 status.setEnabled(!have_caption);
4966 // These are only enabled inside tabular
4967 case LFUN_CELL_BACKWARD:
4968 case LFUN_CELL_FORWARD:
4969 status.setEnabled(true);
4972 // disable these with multiple cells selected
4973 case LFUN_INSET_INSERT:
4974 case LFUN_TABULAR_INSERT:
4975 case LFUN_FLEX_INSERT:
4976 case LFUN_FLOAT_INSERT:
4977 case LFUN_FLOAT_WIDE_INSERT:
4978 case LFUN_FOOTNOTE_INSERT:
4979 case LFUN_MARGINALNOTE_INSERT:
4980 case LFUN_MATH_INSERT:
4981 case LFUN_MATH_MODE:
4982 case LFUN_MATH_MUTATE:
4983 case LFUN_MATH_DISPLAY:
4984 case LFUN_NOTE_INSERT:
4985 case LFUN_ARGUMENT_INSERT:
4986 case LFUN_BOX_INSERT:
4987 case LFUN_BRANCH_INSERT:
4988 case LFUN_PHANTOM_INSERT:
4989 case LFUN_WRAP_INSERT:
4990 case LFUN_PREVIEW_INSERT:
4991 case LFUN_ERT_INSERT: {
4992 if (cur.selIsMultiCell()) {
4993 status.setEnabled(false);
4996 return cell(cur.idx())->getStatus(cur, cmd, status);
4999 // disable in non-fixed-width cells
5000 case LFUN_PARAGRAPH_BREAK:
5001 // multirow does not allow paragraph breaks
5002 if (tabular.isMultiRow(cur.idx())) {
5003 status.setEnabled(false);
5007 case LFUN_NEWLINE_INSERT: {
5008 if (tabular.getPWidth(cur.idx()).zero()) {
5009 status.setEnabled(false);
5012 return cell(cur.idx())->getStatus(cur, cmd, status);
5015 case LFUN_NEWPAGE_INSERT:
5016 status.setEnabled(false);
5020 if (tabularStackDirty() && theClipboard().isInternal()) {
5021 if (cur.selIsMultiCell()) {
5024 getSelection(cur, rs, re, cs, ce);
5025 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5026 && paste_tabular->nrows() == re - rs + 1)
5027 status.setEnabled(true);
5029 status.setEnabled(false);
5030 status.message(_("Selection size should match clipboard content."));
5033 status.setEnabled(true);
5036 return cell(cur.idx())->getStatus(cur, cmd, status);
5038 case LFUN_INSET_SETTINGS:
5039 // relay this lfun to Inset, not to the cell.
5040 return Inset::getStatus(cur, cmd, status);
5043 // we try to handle this event in the insets dispatch function.
5044 return cell(cur.idx())->getStatus(cur, cmd, status);
5050 Inset::DisplayType InsetTabular::display() const
5052 if (tabular.is_long_tabular) {
5053 switch (tabular.longtabular_alignment) {
5054 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5056 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5058 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5068 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5070 tabular.latex(os, runparams);
5074 int InsetTabular::plaintext(odocstringstream & os,
5075 OutputParams const & runparams, size_t max_length) const
5077 os << '\n'; // output table on a new line
5078 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5079 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5080 return PLAINTEXT_NEWLINE;
5084 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5089 // FIXME: Why not pass a proper DocIterator here?
5091 // if the table is inside a float it doesn't need the informaltable
5092 // wrapper. Search for it.
5093 for (master = owner(); master; master = master->owner())
5094 if (master->lyxCode() == FLOAT_CODE)
5099 os << "<informaltable>";
5102 ret += tabular.docbook(os, runparams);
5104 os << "</informaltable>";
5111 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5113 return tabular.xhtml(xs, rp);
5117 void InsetTabular::validate(LaTeXFeatures & features) const
5119 tabular.validate(features);
5121 // It'd be better to be able to get this from an InsetLayout, but at present
5122 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5123 if (features.runparams().flavor == OutputParams::HTML)
5124 features.addCSSSnippet(
5125 "table { border: 1px solid black; display: inline-block; }\n"
5126 "td { border: 1px solid black; padding: 0.5ex; }");
5130 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5132 return tabular.cellInset(idx);
5136 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5138 return tabular.cellInset(idx);
5142 void InsetTabular::cursorPos(BufferView const & bv,
5143 CursorSlice const & sl, bool boundary, int & x, int & y) const
5145 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5147 // y offset correction
5148 y += cellYPos(sl.idx());
5149 y += tabular.textVOffset(sl.idx());
5150 y += offset_valign_;
5152 // x offset correction
5153 x += cellXPos(sl.idx());
5154 x += tabular.textHOffset(sl.idx());
5155 x += ADD_TO_TABULAR_WIDTH;
5159 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5163 Inset const & inset = *tabular.cellInset(cell);
5164 Point o = bv.coordCache().getInsets().xy(&inset);
5165 int const xbeg = o.x_ - tabular.textHOffset(cell);
5166 int const xend = xbeg + tabular.cellWidth(cell);
5167 row_type const row = tabular.cellRow(cell);
5168 int const ybeg = o.y_ - tabular.rowAscent(row)
5169 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5170 int const yend = ybeg + tabular.cellHeight(cell);
5182 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5183 // << " ybeg=" << ybeg << " yend=" << yend
5184 // << " xx=" << xx << " yy=" << yy
5185 // << " dist=" << xx + yy << endl;
5190 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5192 //lyxerr << "InsetTabular::editXY: " << this << endl;
5193 cur.selection(false);
5195 cur.idx() = getNearestCell(cur.bv(), x, y);
5196 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5200 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5202 cur.idx() = getNearestCell(cur.bv(), x, y);
5203 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5207 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5209 idx_type idx_min = 0;
5210 int dist_min = numeric_limits<int>::max();
5211 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5212 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5213 int const d = dist(bv, i, x, y);
5224 int InsetTabular::cellYPos(idx_type const cell) const
5226 row_type row = tabular.cellRow(cell);
5228 for (row_type r = 0; r < row; ++r)
5229 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5230 + tabular.interRowSpace(r + 1);
5235 int InsetTabular::cellXPos(idx_type const cell) const
5237 col_type col = tabular.cellColumn(cell);
5239 for (col_type c = 0; c < col; ++c)
5240 lx += tabular.column_info[c].width;
5245 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5247 row_type const row = tabular.cellRow(cur.idx());
5248 col_type const col = tabular.cellColumn(cur.idx());
5250 if (isRightToLeft(cur)) {
5251 if (tabular.cellColumn(cur.idx()) == 0) {
5252 if (row == tabular.nrows() - 1)
5254 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5259 cur.idx() = tabular.getLastCellInRow(row - 1);
5261 cur.idx() = tabular.cellIndex(row, col - 1);
5264 if (tabular.isLastCell(cur.idx()))
5266 if (cur.idx() == tabular.getLastCellInRow(row))
5267 cur.idx() = tabular.cellIndex(row + 1, 0);
5269 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5270 cur.idx() = tabular.cellIndex(row, colnextcell);
5274 cur.boundary(false);
5276 if (cur.selIsMultiCell()) {
5277 cur.pit() = cur.lastpit();
5278 cur.pos() = cur.lastpos();
5285 // in visual mode, place cursor at extreme left or right
5287 switch(entry_from) {
5289 case ENTRY_DIRECTION_RIGHT:
5290 cur.posVisToRowExtremity(false /* !left */);
5292 case ENTRY_DIRECTION_LEFT:
5293 cur.posVisToRowExtremity(true /* left */);
5295 case ENTRY_DIRECTION_IGNORE:
5296 // nothing to do in this case
5300 cur.setCurrentFont();
5304 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5306 row_type const row = tabular.cellRow(cur.idx());
5307 col_type const col = tabular.cellColumn(cur.idx());
5309 if (isRightToLeft(cur)) {
5310 if (cur.idx() == tabular.getLastCellInRow(row)) {
5313 cur.idx() = tabular.getFirstCellInRow(row);
5314 cur.idx() = tabular.cellAbove(cur.idx());
5316 if (tabular.isLastCell(cur.idx()))
5318 if (cur.idx() == tabular.getLastCellInRow(row))
5319 cur.idx() = tabular.cellIndex(row + 1, 0);
5321 cur.idx() = tabular.cellIndex(row, col + 1);
5324 if (cur.idx() == 0) // first cell
5327 cur.idx() = tabular.getLastCellInRow(row - 1);
5329 cur.idx() = tabular.cellIndex(row, col - 1);
5332 if (cur.selIsMultiCell()) {
5333 cur.pit() = cur.lastpit();
5334 cur.pos() = cur.lastpos();
5338 cur.pit() = cur.lastpit();
5339 cur.pos() = cur.lastpos();
5341 // in visual mode, place cursor at extreme left or right
5343 switch(entry_from) {
5345 case ENTRY_DIRECTION_RIGHT:
5346 cur.posVisToRowExtremity(false /* !left */);
5348 case ENTRY_DIRECTION_LEFT:
5349 cur.posVisToRowExtremity(true /* left */);
5351 case ENTRY_DIRECTION_IGNORE:
5352 // nothing to do in this case
5356 cur.setCurrentFont();
5360 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5362 cur.recordUndoInset(this);
5364 istringstream is(argument);
5367 size_t safe_guard = 0;
5372 if (safe_guard > 1000) {
5373 LYXERR0("parameter max count reached!");
5377 Tabular::Feature action = Tabular::LAST_ACTION;
5380 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5381 if (s != tabularFeature[i].feature)
5383 action = tabularFeature[i].action;
5386 if (action == Tabular::LAST_ACTION) {
5387 LYXERR0("Feature not found " << s);
5391 if (tabularFeature[i].need_value)
5393 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5394 tabularFeatures(cur, action, val);
5399 static void checkLongtableSpecial(Tabular::ltType & ltt,
5400 string const & special, bool & flag)
5402 if (special == "dl_above") {
5405 } else if (special == "dl_below") {
5406 ltt.bottomDL = flag;
5408 } else if (special == "empty") {
5418 bool InsetTabular::oneCellHasRotationState(bool rotated,
5419 row_type row_start, row_type row_end,
5420 col_type col_start, col_type col_end) const
5422 for (row_type r = row_start; r <= row_end; ++r)
5423 for (col_type c = col_start; c <= col_end; ++c)
5425 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5428 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5435 void InsetTabular::tabularFeatures(Cursor & cur,
5436 Tabular::Feature feature, string const & value)
5438 col_type sel_col_start;
5439 col_type sel_col_end;
5440 row_type sel_row_start;
5441 row_type sel_row_end;
5442 bool setLines = false;
5443 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5444 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5448 case Tabular::M_ALIGN_LEFT:
5449 case Tabular::ALIGN_LEFT:
5450 setAlign = LYX_ALIGN_LEFT;
5453 case Tabular::M_ALIGN_RIGHT:
5454 case Tabular::ALIGN_RIGHT:
5455 setAlign = LYX_ALIGN_RIGHT;
5458 case Tabular::M_ALIGN_CENTER:
5459 case Tabular::ALIGN_CENTER:
5460 setAlign = LYX_ALIGN_CENTER;
5463 case Tabular::ALIGN_BLOCK:
5464 setAlign = LYX_ALIGN_BLOCK;
5467 case Tabular::ALIGN_DECIMAL:
5468 setAlign = LYX_ALIGN_DECIMAL;
5471 case Tabular::M_VALIGN_TOP:
5472 case Tabular::VALIGN_TOP:
5473 setVAlign = Tabular::LYX_VALIGN_TOP;
5476 case Tabular::M_VALIGN_BOTTOM:
5477 case Tabular::VALIGN_BOTTOM:
5478 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5481 case Tabular::M_VALIGN_MIDDLE:
5482 case Tabular::VALIGN_MIDDLE:
5483 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5490 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5491 row_type const row = tabular.cellRow(cur.idx());
5492 col_type const column = tabular.cellColumn(cur.idx());
5494 Tabular::ltType ltt;
5498 case Tabular::SET_TABULAR_WIDTH:
5499 tabular.setTabularWidth(Length(value));
5502 case Tabular::SET_PWIDTH: {
5503 Length const len(value);
5504 tabular.setColumnPWidth(cur, cur.idx(), len);
5506 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5507 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5511 case Tabular::SET_MPWIDTH:
5512 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5515 case Tabular::SET_MROFFSET:
5516 tabular.setMROffset(cur, cur.idx(), Length(value));
5519 case Tabular::SET_SPECIAL_COLUMN:
5520 case Tabular::SET_SPECIAL_MULTICOLUMN:
5521 if (value == "none")
5522 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5524 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5527 case Tabular::APPEND_ROW:
5528 // append the row into the tabular
5529 tabular.appendRow(row);
5532 case Tabular::APPEND_COLUMN:
5533 // append the column into the tabular
5534 tabular.appendColumn(column);
5535 cur.idx() = tabular.cellIndex(row, column);
5538 case Tabular::DELETE_ROW:
5539 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5540 for (col_type c = 0; c < tabular.ncols(); c++)
5541 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5542 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5545 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5546 tabular.deleteRow(sel_row_start);
5547 if (sel_row_start >= tabular.nrows())
5549 cur.idx() = tabular.cellIndex(sel_row_start, column);
5552 cur.selection(false);
5555 case Tabular::DELETE_COLUMN:
5556 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5557 for (row_type r = 0; r < tabular.nrows(); r++)
5558 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5559 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5562 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5563 for (row_type r = 0; r < tabular.nrows(); r++)
5564 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5565 tabular.leftLine(tabular.cellIndex(r, 0)));
5568 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5569 tabular.deleteColumn(sel_col_start);
5570 if (sel_col_start >= tabular.ncols())
5572 cur.idx() = tabular.cellIndex(row, sel_col_start);
5575 cur.selection(false);
5578 case Tabular::COPY_ROW:
5579 tabular.copyRow(row);
5582 case Tabular::COPY_COLUMN:
5583 tabular.copyColumn(column);
5584 cur.idx() = tabular.cellIndex(row, column);
5587 case Tabular::MOVE_COLUMN_RIGHT:
5588 tabular.moveColumn(column, Tabular::RIGHT);
5589 cur.idx() = tabular.cellIndex(row, column + 1);
5592 case Tabular::MOVE_COLUMN_LEFT:
5593 tabular.moveColumn(column, Tabular::LEFT);
5594 cur.idx() = tabular.cellIndex(row, column - 1);
5597 case Tabular::MOVE_ROW_DOWN:
5598 tabular.moveRow(row, Tabular::DOWN);
5599 cur.idx() = tabular.cellIndex(row + 1, column);
5602 case Tabular::MOVE_ROW_UP:
5603 tabular.moveRow(row, Tabular::UP);
5604 cur.idx() = tabular.cellIndex(row - 1, column);
5607 case Tabular::SET_LINE_TOP:
5608 case Tabular::TOGGLE_LINE_TOP: {
5609 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5610 ? (value == "true") : !tabular.topLine(cur.idx());
5611 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5612 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5613 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5617 case Tabular::SET_LINE_BOTTOM:
5618 case Tabular::TOGGLE_LINE_BOTTOM: {
5619 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5620 ? (value == "true") : !tabular.bottomLine(cur.idx());
5621 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5622 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5623 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5627 case Tabular::SET_LINE_LEFT:
5628 case Tabular::TOGGLE_LINE_LEFT: {
5629 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5630 ? (value == "true") : !tabular.leftLine(cur.idx());
5631 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5632 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5633 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5637 case Tabular::SET_LINE_RIGHT:
5638 case Tabular::TOGGLE_LINE_RIGHT: {
5639 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5640 ? (value == "true") : !tabular.rightLine(cur.idx());
5641 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5642 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5643 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5647 case Tabular::M_ALIGN_LEFT:
5648 case Tabular::M_ALIGN_RIGHT:
5649 case Tabular::M_ALIGN_CENTER:
5650 case Tabular::ALIGN_LEFT:
5651 case Tabular::ALIGN_RIGHT:
5652 case Tabular::ALIGN_CENTER:
5653 case Tabular::ALIGN_BLOCK:
5654 case Tabular::ALIGN_DECIMAL:
5655 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5656 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5657 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5658 !tabular.getPWidth(c).zero());
5661 case Tabular::M_VALIGN_TOP:
5662 case Tabular::M_VALIGN_BOTTOM:
5663 case Tabular::M_VALIGN_MIDDLE:
5665 case Tabular::VALIGN_TOP:
5666 case Tabular::VALIGN_BOTTOM:
5667 case Tabular::VALIGN_MIDDLE:
5668 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5669 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5670 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5673 case Tabular::SET_MULTICOLUMN: {
5674 if (!cur.selection()) {
5675 // just multicol for one single cell
5676 // check whether we are completely in a multicol
5677 if (!tabular.isMultiColumn(cur.idx()))
5678 tabular.setMultiColumn(cur.idx(), 1,
5679 tabular.rightLine(cur.idx()));
5682 // we have a selection so this means we just add all these
5683 // cells to form a multicolumn cell
5684 idx_type const s_start = cur.selBegin().idx();
5685 row_type const col_start = tabular.cellColumn(s_start);
5686 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5687 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5688 tabular.rightLine(cur.selEnd().idx()));
5691 cur.selection(false);
5695 case Tabular::UNSET_MULTICOLUMN: {
5696 if (!cur.selection()) {
5697 if (tabular.isMultiColumn(cur.idx()))
5698 tabular.unsetMultiColumn(cur.idx());
5703 case Tabular::MULTICOLUMN: {
5704 if (!cur.selection()) {
5705 if (tabular.isMultiColumn(cur.idx()))
5706 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5708 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5712 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5713 row_type const r = sel_row_start;
5714 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5715 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5718 // If the selection contains at least one singlecol cell
5719 // or multiple multicol cells,
5720 // we assume the user will merge is to a single multicol
5722 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5724 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5728 case Tabular::SET_MULTIROW: {
5729 if (!cur.selection()) {
5730 // just multirow for one single cell
5731 // check whether we are completely in a multirow
5732 if (!tabular.isMultiRow(cur.idx()))
5733 tabular.setMultiRow(cur.idx(), 1,
5734 tabular.bottomLine(cur.idx()),
5735 tabular.getAlignment(cur.idx()));
5738 // we have a selection so this means we just add all this
5739 // cells to form a multirow cell
5740 idx_type const s_start = cur.selBegin().idx();
5741 row_type const row_start = tabular.cellRow(s_start);
5742 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5743 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5744 tabular.bottomLine(cur.selEnd().idx()),
5745 tabular.getAlignment(cur.selEnd().idx()));
5748 cur.selection(false);
5752 case Tabular::UNSET_MULTIROW: {
5753 if (!cur.selection()) {
5754 if (tabular.isMultiRow(cur.idx()))
5755 tabular.unsetMultiRow(cur.idx());
5760 case Tabular::MULTIROW: {
5761 if (!cur.selection()) {
5762 if (tabular.isMultiRow(cur.idx()))
5763 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5765 tabularFeatures(cur, Tabular::SET_MULTIROW);
5769 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5770 col_type const c = sel_col_start;
5771 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5772 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5775 // If the selection contains at least one singlerow cell
5776 // or multiple multirow cells,
5777 // we assume the user will merge is to a single multirow
5779 tabularFeatures(cur, Tabular::SET_MULTIROW);
5781 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5785 case Tabular::SET_ALL_LINES:
5787 case Tabular::UNSET_ALL_LINES:
5788 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5789 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5790 idx_type const cell = tabular.cellIndex(r, c);
5791 tabular.setTopLine(cell, setLines);
5792 tabular.setBottomLine(cell, setLines);
5793 tabular.setRightLine(cell, setLines);
5794 tabular.setLeftLine(cell, setLines);
5798 case Tabular::SET_BORDER_LINES:
5799 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5800 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5801 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5803 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5804 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5805 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5809 case Tabular::TOGGLE_LONGTABULAR:
5810 if (tabular.is_long_tabular)
5811 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5813 tabular.is_long_tabular = true;
5816 case Tabular::SET_LONGTABULAR:
5817 tabular.is_long_tabular = true;
5820 case Tabular::UNSET_LONGTABULAR:
5821 for (row_type r = 0; r < tabular.nrows(); ++r) {
5822 if (tabular.ltCaption(r)) {
5823 cur.idx() = tabular.cellIndex(r, 0);
5826 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5829 tabular.is_long_tabular = false;
5832 case Tabular::SET_ROTATE_TABULAR:
5833 tabular.rotate = convert<int>(value);
5836 case Tabular::UNSET_ROTATE_TABULAR:
5840 case Tabular::TOGGLE_ROTATE_TABULAR:
5841 // when pressing the rotate button we default to 90° rotation
5842 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5845 case Tabular::TABULAR_VALIGN_TOP:
5846 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5849 case Tabular::TABULAR_VALIGN_MIDDLE:
5850 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5853 case Tabular::TABULAR_VALIGN_BOTTOM:
5854 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5857 case Tabular::LONGTABULAR_ALIGN_LEFT:
5858 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5861 case Tabular::LONGTABULAR_ALIGN_CENTER:
5862 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5865 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5866 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5871 case Tabular::SET_ROTATE_CELL:
5872 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5873 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5874 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5877 case Tabular::UNSET_ROTATE_CELL:
5878 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5879 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5880 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5883 case Tabular::TOGGLE_ROTATE_CELL:
5885 bool oneNotRotated = oneCellHasRotationState(false,
5886 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5888 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5889 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5890 // when pressing the rotate cell button we default to 90° rotation
5892 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5894 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5899 case Tabular::SET_USEBOX: {
5900 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5901 if (val == tabular.getUsebox(cur.idx()))
5902 val = Tabular::BOX_NONE;
5903 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5904 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5905 tabular.setUsebox(tabular.cellIndex(r, c), val);
5909 case Tabular::UNSET_LTFIRSTHEAD:
5911 case Tabular::SET_LTFIRSTHEAD:
5912 tabular.getRowOfLTFirstHead(row, ltt);
5913 checkLongtableSpecial(ltt, value, flag);
5914 tabular.setLTHead(row, flag, ltt, true);
5917 case Tabular::UNSET_LTHEAD:
5919 case Tabular::SET_LTHEAD:
5920 tabular.getRowOfLTHead(row, ltt);
5921 checkLongtableSpecial(ltt, value, flag);
5922 tabular.setLTHead(row, flag, ltt, false);
5925 case Tabular::UNSET_LTFOOT:
5927 case Tabular::SET_LTFOOT:
5928 tabular.getRowOfLTFoot(row, ltt);
5929 checkLongtableSpecial(ltt, value, flag);
5930 tabular.setLTFoot(row, flag, ltt, false);
5933 case Tabular::UNSET_LTLASTFOOT:
5935 case Tabular::SET_LTLASTFOOT:
5936 tabular.getRowOfLTLastFoot(row, ltt);
5937 checkLongtableSpecial(ltt, value, flag);
5938 tabular.setLTFoot(row, flag, ltt, true);
5941 case Tabular::UNSET_LTNEWPAGE:
5943 case Tabular::SET_LTNEWPAGE:
5944 tabular.setLTNewPage(row, flag);
5947 case Tabular::SET_LTCAPTION: {
5948 if (tabular.ltCaption(row))
5950 cur.idx() = tabular.setLTCaption(row, true);
5953 cur.selection(false);
5954 // If a row is set as caption, then also insert
5955 // a caption. Otherwise the LaTeX output is broken.
5956 // Select cell if it is non-empty
5957 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5958 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5959 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5963 case Tabular::UNSET_LTCAPTION: {
5964 if (!tabular.ltCaption(row))
5966 cur.idx() = tabular.setLTCaption(row, false);
5969 cur.selection(false);
5970 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5971 if (lyx::getStatus(fr).enabled())
5976 case Tabular::TOGGLE_LTCAPTION: {
5977 if (tabular.ltCaption(row))
5978 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5980 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5984 case Tabular::TOGGLE_BOOKTABS:
5985 tabular.use_booktabs = !tabular.use_booktabs;
5988 case Tabular::SET_BOOKTABS:
5989 tabular.use_booktabs = true;
5992 case Tabular::UNSET_BOOKTABS:
5993 tabular.use_booktabs = false;
5996 case Tabular::SET_TOP_SPACE: {
5998 if (value == "default")
5999 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6000 tabular.row_info[r].top_space_default = true;
6001 else if (value == "none")
6002 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6003 tabular.row_info[r].top_space_default = false;
6004 tabular.row_info[r].top_space = len;
6006 else if (isValidLength(value, &len))
6007 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6008 tabular.row_info[r].top_space_default = false;
6009 tabular.row_info[r].top_space = len;
6014 case Tabular::SET_BOTTOM_SPACE: {
6016 if (value == "default")
6017 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6018 tabular.row_info[r].bottom_space_default = true;
6019 else if (value == "none")
6020 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6021 tabular.row_info[r].bottom_space_default = false;
6022 tabular.row_info[r].bottom_space = len;
6024 else if (isValidLength(value, &len))
6025 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6026 tabular.row_info[r].bottom_space_default = false;
6027 tabular.row_info[r].bottom_space = len;
6032 case Tabular::SET_INTERLINE_SPACE: {
6034 if (value == "default")
6035 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6036 tabular.row_info[r].interline_space_default = true;
6037 else if (value == "none")
6038 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6039 tabular.row_info[r].interline_space_default = false;
6040 tabular.row_info[r].interline_space = len;
6042 else if (isValidLength(value, &len))
6043 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6044 tabular.row_info[r].interline_space_default = false;
6045 tabular.row_info[r].interline_space = len;
6050 case Tabular::SET_DECIMAL_POINT:
6051 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6052 tabular.column_info[c].decimal_point = from_utf8(value);
6055 // dummy stuff just to avoid warnings
6056 case Tabular::LAST_ACTION:
6062 bool InsetTabular::copySelection(Cursor & cur)
6064 if (!cur.selection())
6069 getSelection(cur, rs, re, cs, ce);
6071 paste_tabular.reset(new Tabular(tabular));
6073 for (row_type r = 0; r < rs; ++r)
6074 paste_tabular->deleteRow(0);
6076 row_type const rows = re - rs + 1;
6077 while (paste_tabular->nrows() > rows)
6078 paste_tabular->deleteRow(rows);
6080 for (col_type c = 0; c < cs; ++c)
6081 paste_tabular->deleteColumn(0);
6083 col_type const columns = ce - cs + 1;
6084 while (paste_tabular->ncols() > columns)
6085 paste_tabular->deleteColumn(columns);
6087 paste_tabular->setBuffer(tabular.buffer());
6089 odocstringstream os;
6090 OutputParams const runparams(0);
6091 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6092 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6093 cap::copySelection(cur, os.str());
6095 // mark tabular stack dirty
6096 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6097 // when we (hopefully) have a one-for-all paste mechanism.
6098 // This must be called after cap::copySelection.
6099 dirtyTabularStack(true);
6105 bool InsetTabular::pasteClipboard(Cursor & cur)
6109 col_type actcol = tabular.cellColumn(cur.idx());
6110 row_type actrow = tabular.cellRow(cur.idx());
6112 if (cur.selIsMultiCell()) {
6115 getSelection(cur, actrow, re, actcol, ce);
6118 for (row_type r1 = 0, r2 = actrow;
6119 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6121 for (col_type c1 = 0, c2 = actcol;
6122 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6124 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6125 tabular.isPartOfMultiColumn(r2, c2))
6127 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6131 if (tabular.isPartOfMultiColumn(r2, c2)) {
6135 shared_ptr<InsetTableCell> inset(
6136 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6137 tabular.setCellInset(r2, c2, inset);
6138 // FIXME?: why do we need to do this explicitly? (EL)
6139 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6141 // FIXME: change tracking (MG)
6142 inset->setChange(Change(buffer().params().track_changes ?
6143 Change::INSERTED : Change::UNCHANGED));
6151 void InsetTabular::cutSelection(Cursor & cur)
6153 if (!cur.selection())
6158 getSelection(cur, rs, re, cs, ce);
6159 for (row_type r = rs; r <= re; ++r) {
6160 for (col_type c = cs; c <= ce; ++c) {
6161 shared_ptr<InsetTableCell> t
6162 = cell(tabular.cellIndex(r, c));
6163 if (buffer().params().track_changes)
6164 // FIXME: Change tracking (MG)
6165 t->setChange(Change(Change::DELETED));
6171 // cursor position might be invalid now
6172 if (cur.pit() > cur.lastpit())
6173 cur.pit() = cur.lastpit();
6174 if (cur.pos() > cur.lastpos())
6175 cur.pos() = cur.lastpos();
6176 cur.clearSelection();
6180 bool InsetTabular::isRightToLeft(Cursor & cur) const
6182 // LASSERT: It might be better to abandon this Buffer.
6183 LASSERT(cur.depth() > 1, return false);
6184 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6185 pos_type const parentpos = cur[cur.depth() - 2].pos();
6186 return parentpar.getFontSettings(buffer().params(),
6187 parentpos).language()->rightToLeft();
6191 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6194 LASSERT(stidx <= enidx, return docstring());
6196 col_type const col1 = tabular.cellColumn(stidx);
6197 col_type const col2 = tabular.cellColumn(enidx);
6198 row_type const row1 = tabular.cellRow(stidx);
6199 row_type const row2 = tabular.cellRow(enidx);
6201 for (col_type col = col1; col <= col2; col++)
6202 for (row_type row = row1; row <= row2; row++) {
6207 retval += tabular.cellInset(row, col)->asString(intoInsets);
6213 void InsetTabular::getSelection(Cursor & cur,
6214 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6216 CursorSlice const & beg = cur.selBegin();
6217 CursorSlice const & end = cur.selEnd();
6218 cs = tabular.cellColumn(beg.idx());
6219 ce = tabular.cellColumn(end.idx());
6223 rs = tabular.cellRow(beg.idx());
6224 re = tabular.cellRow(end.idx());
6230 Text * InsetTabular::getText(int idx) const
6232 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6236 void InsetTabular::setChange(Change const & change)
6238 for (idx_type idx = 0; idx < nargs(); ++idx)
6239 cell(idx)->setChange(change);
6243 void InsetTabular::acceptChanges()
6245 for (idx_type idx = 0; idx < nargs(); ++idx)
6246 cell(idx)->acceptChanges();
6250 void InsetTabular::rejectChanges()
6252 for (idx_type idx = 0; idx < nargs(); ++idx)
6253 cell(idx)->rejectChanges();
6257 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6259 return tabular.getPWidth(cell).zero();
6263 bool InsetTabular::forcePlainLayout(idx_type cell) const
6265 return !tabular.getPWidth(cell).zero();
6269 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6272 if (buf.length() <= 0)
6277 col_type maxCols = 1;
6278 size_t const len = buf.length();
6282 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6290 maxCols = max(cols, maxCols);
6296 maxCols = max(cols, maxCols);
6302 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6303 loctab = paste_tabular.get();
6304 dirtyTabularStack(true);
6307 cell = bv.cursor().idx();
6308 ocol = tabular.cellColumn(cell);
6309 row = tabular.cellRow(cell);
6313 idx_type const cells = loctab->numberofcells;
6316 rows = loctab->nrows();
6317 col_type const columns = loctab->ncols();
6319 while (cell < cells && p < len && row < rows &&
6320 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6326 // we can only set this if we are not too far right
6327 if (cols < columns) {
6328 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6329 Font const font = bv.textMetrics(&inset->text()).
6331 inset->setText(buf.substr(op, p - op), font,
6332 buffer().params().track_changes);
6338 // we can only set this if we are not too far right
6339 if (cols < columns) {
6340 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6341 Font const font = bv.textMetrics(&inset->text()).
6343 inset->setText(buf.substr(op, p - op), font,
6344 buffer().params().track_changes);
6349 cell = loctab->cellIndex(row, cols);
6355 // check for the last cell if there is no trailing '\n'
6356 if (cell < cells && op < len) {
6357 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6358 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6359 inset->setText(buf.substr(op, len - op), font,
6360 buffer().params().track_changes);
6366 void InsetTabular::addPreview(DocIterator const & inset_pos,
6367 PreviewLoader & loader) const
6369 DocIterator cell_pos = inset_pos;
6371 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6372 for (row_type r = 0; r < tabular.nrows(); ++r) {
6373 for (col_type c = 0; c < tabular.ncols(); ++c) {
6374 cell_pos.top().idx() = tabular.cellIndex(r, c);
6375 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6381 bool InsetTabular::completionSupported(Cursor const & cur) const
6383 Cursor const & bvCur = cur.bv().cursor();
6384 if (&bvCur.inset() != this)
6386 return cur.text()->completionSupported(cur);
6390 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6392 return completionSupported(cur);
6396 bool InsetTabular::automaticInlineCompletion() const
6398 return lyxrc.completion_inline_text;
6402 bool InsetTabular::automaticPopupCompletion() const
6404 return lyxrc.completion_popup_text;
6408 bool InsetTabular::showCompletionCursor() const
6410 return lyxrc.completion_cursor_text;
6414 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6416 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6420 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6422 if (!completionSupported(cur))
6424 return cur.text()->completionPrefix(cur);
6428 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6430 if (!completionSupported(cur))
6433 return cur.text()->insertCompletion(cur, s, finished);
6437 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6438 Dimension & dim) const
6440 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6441 tm.completionPosAndDim(cur, x, y, dim);
6445 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6447 istringstream data(in);
6449 lex.setStream(data);
6456 if (!lex || token != "tabular") {
6457 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6461 // This is part of the inset proper that is usually swallowed
6462 // by Buffer::readInset
6464 if (!lex || token != "Tabular") {
6465 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6473 string InsetTabular::params2string(InsetTabular const & inset)
6476 data << "tabular" << ' ';
6478 data << "\\end_inset\n";
6483 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6485 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6486 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6487 if (!tabular.isPartOfMultiColumn(r,c) &&
6488 !tabular.isPartOfMultiRow(r,c))
6491 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6492 ParagraphList::iterator it = parlist.begin();
6493 ParagraphList::iterator const en = parlist.end();
6494 for (; it != en; ++it)
6495 it->setLayout(dc.plainLayout());