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"
49 #include "texstream.h"
50 #include "TextClass.h"
51 #include "TextMetrics.h"
53 #include "frontends/Application.h"
54 #include "frontends/alert.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include "support/convert.h"
60 #include "support/debug.h"
61 #include "support/docstream.h"
62 #include "support/FileName.h"
63 #include "support/gettext.h"
64 #include "support/lassert.h"
65 #include "support/lstrings.h"
66 #include "support/unique_ptr.h"
74 using namespace lyx::support;
80 using cap::dirtyTabularStack;
81 using cap::tabularStackDirty;
83 using graphics::PreviewLoader;
85 using frontend::Painter;
86 using frontend::Clipboard;
88 namespace Alert = frontend::Alert;
93 int const ADD_TO_HEIGHT = 2; // in cell
94 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
95 int const default_line_space = 10; // ?
96 int const WIDTH_OF_LINE = 5; // space between double lines
100 unique_ptr<Tabular> paste_tabular;
103 struct TabularFeature {
104 Tabular::Feature action;
110 TabularFeature tabularFeature[] =
112 // the SET/UNSET actions are used by the table dialog,
113 // the TOGGLE actions by the table toolbar buttons
114 // FIXME: these values have been hardcoded in InsetMathGrid and other
116 { Tabular::APPEND_ROW, "append-row", false },
117 { Tabular::APPEND_COLUMN, "append-column", false },
118 { Tabular::DELETE_ROW, "delete-row", false },
119 { Tabular::DELETE_COLUMN, "delete-column", false },
120 { Tabular::COPY_ROW, "copy-row", false },
121 { Tabular::COPY_COLUMN, "copy-column", false },
122 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
123 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
124 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
125 { Tabular::MOVE_ROW_UP, "move-row-up", false },
126 { Tabular::SET_LINE_TOP, "set-line-top", true },
127 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
128 { Tabular::SET_LINE_LEFT, "set-line-left", true },
129 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
130 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
131 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
132 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
133 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
134 { Tabular::ALIGN_LEFT, "align-left", false },
135 { Tabular::ALIGN_RIGHT, "align-right", false },
136 { Tabular::ALIGN_CENTER, "align-center", false },
137 { Tabular::ALIGN_BLOCK, "align-block", false },
138 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
139 { Tabular::VALIGN_TOP, "valign-top", false },
140 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
141 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
142 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
143 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
144 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
145 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
146 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
147 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
148 { Tabular::MULTICOLUMN, "multicolumn", false },
149 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
150 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
151 { Tabular::MULTIROW, "multirow", false },
152 { Tabular::SET_MULTIROW, "set-multirow", false },
153 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
154 { Tabular::SET_MROFFSET, "set-mroffset", true },
155 { Tabular::SET_ALL_LINES, "set-all-lines", false },
156 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
157 { Tabular::TOGGLE_LONGTABULAR, "toggle-longtabular", false },
158 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
159 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
160 { Tabular::SET_PWIDTH, "set-pwidth", true },
161 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
162 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
163 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
164 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
165 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
166 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
167 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
168 { Tabular::SET_USEBOX, "set-usebox", true },
169 { Tabular::SET_LTHEAD, "set-lthead", true },
170 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
171 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
172 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
173 { Tabular::SET_LTFOOT, "set-ltfoot", true },
174 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
175 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
176 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
177 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
178 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
179 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
180 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
181 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
182 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
183 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
184 { Tabular::TOGGLE_BOOKTABS, "toggle-booktabs", false },
185 { Tabular::SET_BOOKTABS, "set-booktabs", false },
186 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
187 { Tabular::SET_TOP_SPACE, "set-top-space", true },
188 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
189 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
190 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
191 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
192 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
193 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
194 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
195 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
196 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
197 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
198 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
199 { Tabular::LAST_ACTION, "", false }
203 string const tostr(LyXAlignment const & num)
208 case LYX_ALIGN_BLOCK:
212 case LYX_ALIGN_CENTER:
214 case LYX_ALIGN_RIGHT:
216 case LYX_ALIGN_LAYOUT:
218 case LYX_ALIGN_SPECIAL:
220 case LYX_ALIGN_DECIMAL:
227 string const tostr(Tabular::HAlignment const & num)
230 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
232 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
234 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
241 string const tostr(Tabular::VAlignment const & num)
244 case Tabular::LYX_VALIGN_TOP:
246 case Tabular::LYX_VALIGN_MIDDLE:
248 case Tabular::LYX_VALIGN_BOTTOM:
255 string const tostr(Tabular::BoxType const & num)
258 case Tabular::BOX_NONE:
260 case Tabular::BOX_PARBOX:
262 case Tabular::BOX_MINIPAGE:
269 // I would have liked a fromstr template a lot better. (Lgb)
270 bool string2type(string const & str, LyXAlignment & num)
273 num = LYX_ALIGN_NONE;
274 else if (str == "block")
275 num = LYX_ALIGN_BLOCK;
276 else if (str == "left")
277 num = LYX_ALIGN_LEFT;
278 else if (str == "center")
279 num = LYX_ALIGN_CENTER;
280 else if (str == "right")
281 num = LYX_ALIGN_RIGHT;
282 else if (str == "decimal")
283 num = LYX_ALIGN_DECIMAL;
290 bool string2type(string const & str, Tabular::HAlignment & num)
293 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
294 else if (str == "center" )
295 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
296 else if (str == "right")
297 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
304 bool string2type(string const & str, Tabular::VAlignment & num)
307 num = Tabular::LYX_VALIGN_TOP;
308 else if (str == "middle" )
309 num = Tabular::LYX_VALIGN_MIDDLE;
310 else if (str == "bottom")
311 num = Tabular::LYX_VALIGN_BOTTOM;
318 bool string2type(string const & str, Tabular::BoxType & num)
321 num = Tabular::BOX_NONE;
322 else if (str == "parbox")
323 num = Tabular::BOX_PARBOX;
324 else if (str == "minipage")
325 num = Tabular::BOX_MINIPAGE;
332 bool string2type(string const & str, bool & num)
336 else if (str == "false")
344 bool getTokenValue(string const & str, char const * token, string & ret)
347 size_t token_length = strlen(token);
348 size_t pos = str.find(token);
350 if (pos == string::npos || pos + token_length + 1 >= str.length()
351 || str[pos + token_length] != '=')
353 pos += token_length + 1;
355 if (ch != '"' && ch != '\'') { // only read till next space
359 while (pos < str.length() - 1 && str[++pos] != ch)
366 bool getTokenValue(string const & str, char const * token, docstring & ret)
369 bool const success = getTokenValue(str, token, tmp);
370 ret = from_utf8(tmp);
375 bool getTokenValue(string const & str, char const * token, int & num)
379 if (!getTokenValue(str, token, tmp))
381 num = convert<int>(tmp);
386 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
389 return getTokenValue(str, token, tmp) && string2type(tmp, num);
393 bool getTokenValue(string const & str, char const * token,
394 Tabular::HAlignment & num)
397 return getTokenValue(str, token, tmp) && string2type(tmp, num);
401 bool getTokenValue(string const & str, char const * token,
402 Tabular::VAlignment & num)
405 return getTokenValue(str, token, tmp) && string2type(tmp, num);
409 bool getTokenValue(string const & str, char const * token,
410 Tabular::BoxType & num)
413 return getTokenValue(str, token, tmp) && string2type(tmp, num);
417 bool getTokenValue(string const & str, char const * token, bool & flag)
419 // set the flag always to false as this should be the default for bools
420 // not in the file-format.
423 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
427 bool getTokenValue(string const & str, char const * token, Length & len)
429 // set the length to be zero() as default as this it should be if not
430 // in the file format.
433 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
437 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
442 if (!getTokenValue(str, token, tmp))
444 if (tmp == "default") {
448 return isValidLength(tmp, &len);
452 void l_getline(istream & is, string & str)
455 while (str.empty()) {
457 if (!str.empty() && str[str.length() - 1] == '\r')
458 str.erase(str.length() - 1);
463 string const write_attribute(string const & name, T const & t)
465 string const s = tostr(t);
466 return s.empty() ? s : " " + name + "=\"" + s + "\"";
470 string const write_attribute(string const & name, string const & t)
472 return t.empty() ? t : " " + name + "=\"" + t + "\"";
477 string const write_attribute(string const & name, docstring const & t)
479 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
484 string const write_attribute(string const & name, bool const & b)
486 // we write only true attribute values so we remove a bit of the
487 // file format bloat for tabulars.
488 return b ? write_attribute(name, convert<string>(b)) : string();
493 string const write_attribute(string const & name, int const & i)
495 // we write only true attribute values so we remove a bit of the
496 // file format bloat for tabulars.
497 return i ? write_attribute(name, convert<string>(i)) : string();
502 string const write_attribute(string const & name, Tabular::idx_type const & i)
504 // we write only true attribute values so we remove a bit of the
505 // file format bloat for tabulars.
506 return i ? write_attribute(name, convert<string>(i)) : string();
511 string const write_attribute(string const & name, Length const & value)
513 // we write only the value if we really have one same reson as above.
514 return value.zero() ? string() : write_attribute(name, value.asString());
520 string const featureAsString(Tabular::Feature action)
522 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
523 if (tabularFeature[i].action == action)
524 return tabularFeature[i].feature;
530 DocIterator separatorPos(InsetTableCell const * cell, docstring const & align_d)
532 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
533 for (; dit; dit.forwardChar())
534 if (dit.inTexted() && dit.depth() == 1
535 && dit.paragraph().find(align_d, false, false, dit.pos()))
542 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
544 InsetTableCell tail = InsetTableCell(head);
545 DocIterator const dit = separatorPos(&head, align_d);
548 pit_type const psize = head.paragraphs().front().size();
549 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
550 tail.paragraphs().front().eraseChars(0,
551 dit.pos() < psize ? dit.pos() + 1 : psize, false);
558 /////////////////////////////////////////////////////////////////////
562 /////////////////////////////////////////////////////////////////////
565 Tabular::CellData::CellData(Buffer * buf)
568 multicolumn(Tabular::CELL_NORMAL),
569 multirow(Tabular::CELL_NORMAL),
570 alignment(LYX_ALIGN_CENTER),
571 valignment(LYX_VALIGN_TOP),
581 inset(new InsetTableCell(buf))
583 inset->setBuffer(*buf);
587 Tabular::CellData::CellData(CellData const & cs)
590 multicolumn(cs.multicolumn),
591 multirow(cs.multirow),
592 mroffset(cs.mroffset),
593 alignment(cs.alignment),
594 valignment(cs.valignment),
595 decimal_hoffset(cs.decimal_hoffset),
596 decimal_width(cs.decimal_width),
598 top_line(cs.top_line),
599 bottom_line(cs.bottom_line),
600 left_line(cs.left_line),
601 right_line(cs.right_line),
604 align_special(cs.align_special),
606 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
610 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
616 multicolumn = cs.multicolumn;
617 multirow = cs.multirow;
618 mroffset = cs.mroffset;
619 alignment = cs.alignment;
620 valignment = cs.valignment;
621 decimal_hoffset = cs.decimal_hoffset;
622 decimal_width = cs.decimal_width;
623 voffset = cs.voffset;
624 top_line = cs.top_line;
625 bottom_line = cs.bottom_line;
626 left_line = cs.left_line;
627 right_line = cs.right_line;
630 align_special = cs.align_special;
631 p_width = cs.p_width;
632 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
636 Tabular::RowData::RowData()
639 top_space_default(false),
640 bottom_space_default(false),
641 interline_space_default(false),
651 Tabular::ColumnData::ColumnData()
652 : alignment(LYX_ALIGN_CENTER),
653 valignment(LYX_VALIGN_TOP),
659 Tabular::ltType::ltType()
660 : set(false), topDL(false),
666 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
668 init(buffer, rows_arg, columns_arg);
672 void Tabular::setBuffer(Buffer & buffer)
675 for (row_type i = 0; i < nrows(); ++i)
676 for (col_type j = 0; j < ncols(); ++j)
677 cell_info[i][j].inset->setBuffer(*buffer_);
681 // activates all lines and sets all widths to 0
682 void Tabular::init(Buffer * buf, row_type rows_arg,
683 col_type columns_arg)
686 row_info = row_vector(rows_arg);
687 column_info = column_vector(columns_arg);
688 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
689 row_info.reserve(10);
690 column_info.reserve(10);
691 cell_info.reserve(100);
693 is_long_tabular = false;
694 tabular_valignment = LYX_VALIGN_MIDDLE;
695 tabular_width = Length();
696 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
698 use_booktabs = false;
699 // set silly default lines
700 for (row_type r = 0; r < nrows(); ++r)
701 for (col_type c = 0; c < ncols(); ++c) {
702 cell_info[r][c].inset->setBuffer(*buffer_);
703 cell_info[r][c].top_line = true;
704 cell_info[r][c].left_line = true;
705 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
706 cell_info[r][c].right_line = c == ncols() - 1;
711 void Tabular::deleteRow(row_type const row)
713 // Not allowed to delete last row
717 for (col_type c = 0; c < ncols(); ++c) {
718 // Care about multirow cells
719 if (row + 1 < nrows() &&
720 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
721 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
722 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
725 row_info.erase(row_info.begin() + row);
726 cell_info.erase(cell_info.begin() + row);
731 void Tabular::copyRow(row_type const row)
733 insertRow(row, true);
737 void Tabular::appendRow(row_type row)
739 insertRow(row, false);
743 void Tabular::insertRow(row_type const row, bool copy)
745 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
746 cell_info.insert(cell_info.begin() + row + 1,
747 cell_vector(0, CellData(buffer_)));
749 for (col_type c = 0; c < ncols(); ++c) {
750 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
751 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
752 if (buffer().params().track_changes)
753 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
754 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
755 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
759 for (col_type c = 0; c < ncols(); ++c) {
760 if (isPartOfMultiRow(row, c))
762 // inherit line settings
763 idx_type const i = cellIndex(row + 1, c);
764 idx_type const j = cellIndex(row, c);
765 setLeftLine(i, leftLine(j));
766 setRightLine(i, rightLine(j));
767 setTopLine(i, topLine(j));
768 if (topLine(j) && bottomLine(j)) {
769 setBottomLine(i, true);
770 setBottomLine(j, false);
772 // mark track changes
773 if (buffer().params().track_changes)
774 cellInfo(i).inset->setChange(Change(Change::INSERTED));
779 void Tabular::moveColumn(col_type col, ColDirection direction)
781 if (direction == Tabular::LEFT)
784 std::swap(column_info[col], column_info[col + 1]);
786 for (row_type r = 0; r < nrows(); ++r) {
787 std::swap(cell_info[r][col], cell_info[r][col + 1]);
788 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
789 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
791 // FIXME track changes is broken for tabular features (#8469)
792 idx_type const i = cellIndex(r, col);
793 idx_type const j = cellIndex(r, col + 1);
794 if (buffer().params().track_changes) {
795 cellInfo(i).inset->setChange(Change(Change::INSERTED));
796 cellInfo(j).inset->setChange(Change(Change::INSERTED));
803 void Tabular::moveRow(row_type row, RowDirection direction)
805 if (direction == Tabular::UP)
808 std::swap(row_info[row], row_info[row + 1]);
810 for (col_type c = 0; c < ncols(); ++c) {
811 std::swap(cell_info[row][c], cell_info[row + 1][c]);
812 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
813 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
815 // FIXME track changes is broken for tabular features (#8469)
816 idx_type const i = cellIndex(row, c);
817 idx_type const j = cellIndex(row + 1, c);
818 if (buffer().params().track_changes) {
819 cellInfo(i).inset->setChange(Change(Change::INSERTED));
820 cellInfo(j).inset->setChange(Change(Change::INSERTED));
827 void Tabular::deleteColumn(col_type const col)
829 // Not allowed to delete last column
833 for (row_type r = 0; r < nrows(); ++r) {
834 // Care about multicolumn cells
835 if (col + 1 < ncols() &&
836 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
837 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
838 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
840 cell_info[r].erase(cell_info[r].begin() + col);
842 column_info.erase(column_info.begin() + col);
847 void Tabular::copyColumn(col_type const col)
849 insertColumn(col, true);
853 void Tabular::appendColumn(col_type col)
855 insertColumn(col, false);
859 void Tabular::insertColumn(col_type const col, bool copy)
861 BufferParams const & bp = buffer().params();
862 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
864 for (row_type r = 0; r < nrows(); ++r) {
865 cell_info[r].insert(cell_info[r].begin() + col + 1,
866 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
867 if (bp.track_changes)
868 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
869 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
870 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
873 for (row_type r = 0; r < nrows(); ++r) {
874 // inherit line settings
875 idx_type const i = cellIndex(r, col + 1);
876 idx_type const j = cellIndex(r, col);
877 setBottomLine(i, bottomLine(j));
878 setTopLine(i, topLine(j));
879 setLeftLine(i, leftLine(j));
880 if (rightLine(i) && rightLine(j)) {
881 setRightLine(i, true);
882 setRightLine(j, false);
884 if (buffer().params().track_changes)
885 cellInfo(i).inset->setChange(Change(Change::INSERTED));
890 void Tabular::updateIndexes()
895 for (row_type row = 0; row < nrows(); ++row)
896 for (col_type column = 0; column < ncols(); ++column) {
897 if (!isPartOfMultiColumn(row, column)
898 && !isPartOfMultiRow(row, column))
900 if (isPartOfMultiRow(row, column))
901 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
903 cell_info[row][column].cellno = numberofcells - 1;
906 rowofcell.resize(numberofcells);
907 columnofcell.resize(numberofcells);
909 // reset column and row of cells and update their width and alignment
910 for (row_type row = 0; row < nrows(); ++row)
911 for (col_type column = 0; column < ncols(); ++column) {
912 if (isPartOfMultiColumn(row, column))
914 // columnofcell needs to be called before setting width and aligment
915 // multirow cells inherit the width from the column width
916 if (!isPartOfMultiRow(row, column)) {
917 columnofcell[i] = column;
920 setFixedWidth(row, column);
921 if (isPartOfMultiRow(row, column))
923 cell_info[row][column].inset->setContentAlignment(
924 getAlignment(cellIndex(row, column)));
930 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
933 for (col_type c = 0; c < ncols(); ++c)
934 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
940 bool Tabular::topLine(idx_type const cell) const
942 return cellInfo(cell).top_line;
946 bool Tabular::bottomLine(idx_type const cell) const
948 return cellInfo(cell).bottom_line;
952 bool Tabular::leftLine(idx_type cell) const
956 return cellInfo(cell).left_line;
960 bool Tabular::rightLine(idx_type cell) const
964 return cellInfo(cell).right_line;
968 int Tabular::interRowSpace(row_type row) const
970 if (!row || row >= nrows())
973 int const interline_space = row_info[row - 1].interline_space_default ?
975 row_info[row - 1].interline_space.inPixels(width());
976 if (rowTopLine(row) && rowBottomLine(row - 1))
977 return interline_space + WIDTH_OF_LINE;
978 return interline_space;
982 int Tabular::interColumnSpace(idx_type cell) const
984 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
985 if (rightLine(cell) && nextcol < ncols()
986 && leftLine(cellIndex(cellRow(cell), nextcol)))
987 return WIDTH_OF_LINE;
992 int Tabular::cellWidth(idx_type cell) const
995 col_type const span = columnSpan(cell);
996 col_type const col = cellColumn(cell);
997 for(col_type c = col; c < col + span ; ++c)
998 w += column_info[c].width;
1003 int Tabular::cellHeight(idx_type cell) const
1005 row_type const span = rowSpan(cell);
1006 row_type const row = cellRow(cell);
1008 for(row_type r = row; r < row + span ; ++r) {
1009 h += rowAscent(r) + rowDescent(r);
1010 if (r != row + span - 1)
1011 h += interRowSpace(r + 1);
1018 bool Tabular::updateColumnWidths()
1020 vector<int> max_dwidth(ncols(), 0);
1021 for(col_type c = 0; c < ncols(); ++c)
1022 for(row_type r = 0; r < nrows(); ++r) {
1023 idx_type const i = cellIndex(r, c);
1024 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1025 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1028 bool update = false;
1029 // for each col get max of single col cells
1030 for(col_type c = 0; c < ncols(); ++c) {
1032 for(row_type r = 0; r < nrows(); ++r) {
1033 idx_type const i = cellIndex(r, c);
1034 if (columnSpan(i) == 1) {
1035 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1036 && cell_info[r][c].decimal_width != 0)
1037 new_width = max(new_width, cellInfo(i).width
1038 + max_dwidth[c] - cellInfo(i).decimal_width);
1040 new_width = max(new_width, cellInfo(i).width);
1044 if (column_info[c].width != new_width) {
1045 column_info[c].width = new_width;
1049 // update col widths to fit merged cells
1050 for(col_type c = 0; c < ncols(); ++c)
1051 for(row_type r = 0; r < nrows(); ++r) {
1052 idx_type const i = cellIndex(r, c);
1053 int const span = columnSpan(i);
1054 if (span == 1 || c > cellColumn(i))
1058 for(col_type j = c; j < c + span ; ++j)
1059 old_width += column_info[j].width;
1061 if (cellInfo(i).width > old_width) {
1062 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1071 int Tabular::width() const
1074 for (col_type c = 0; c < ncols(); ++c)
1075 width += column_info[c].width;
1080 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1083 col_type const col = cellColumn(cell);
1084 // set alignment for the whole row if we are not in a multicolumn cell,
1085 // exclude possible multicolumn cells in the row
1086 if (!isMultiColumn(cell)) {
1087 for (row_type r = 0; r < nrows(); ++r) {
1088 // only if the column has no width the multirow inherits the
1089 // alignment of the column, otherwise it is left aligned
1090 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1091 && !isMultiColumn(cellIndex(r, col))) {
1092 cell_info[r][col].alignment = align;
1093 cell_info[r][col].inset->setContentAlignment(align);
1095 if ((isMultiRow(cellIndex(r, col)) && has_width)
1096 && !isMultiColumn(cellIndex(r, col))) {
1097 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1098 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1101 column_info[col].alignment = align;
1102 docstring & dpoint = column_info[col].decimal_point;
1103 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1104 dpoint = from_utf8(lyxrc.default_decimal_point);
1106 cellInfo(cell).alignment = align;
1107 cellInset(cell)->setContentAlignment(align);
1112 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1115 if (!isMultiColumn(cell) || onlycolumn)
1116 column_info[cellColumn(cell)].valignment = align;
1118 cellInfo(cell).valignment = align;
1125 * Allow line and paragraph breaks for fixed width cells or disallow them,
1126 * merge cell paragraphs and reset layout to standard for variable width
1129 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1131 inset->toggleFixedWidth(fixedWidth);
1135 // merge all paragraphs to one
1136 BufferParams const & bp = cur.bv().buffer().params();
1137 while (inset->paragraphs().size() > 1)
1138 mergeParagraph(bp, inset->paragraphs(), 0);
1142 // undo information has already been recorded
1143 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1144 bp.documentClass().plainLayoutName());
1151 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1152 Length const & width)
1154 col_type const c = cellColumn(cell);
1156 column_info[c].p_width = width;
1157 // reset the vertical alignment to top if the fixed width
1158 // is removed or zero because only fixed width columns can
1159 // have a vertical alignment
1160 if (column_info[c].p_width.zero())
1161 column_info[c].valignment = LYX_VALIGN_TOP;
1162 for (row_type r = 0; r < nrows(); ++r) {
1163 idx_type const cell = cellIndex(r, c);
1164 // because of multicolumns
1165 toggleFixedWidth(cur, cellInset(cell).get(),
1166 !getPWidth(cell).zero());
1167 if (isMultiRow(cell))
1168 setAlignment(cell, LYX_ALIGN_LEFT, false);
1170 // cur paragraph can become invalid after paragraphs were merged
1171 if (cur.pit() > cur.lastpit())
1172 cur.pit() = cur.lastpit();
1173 // cur position can become invalid after newlines were removed
1174 if (cur.pos() > cur.lastpos())
1175 cur.pos() = cur.lastpos();
1179 bool Tabular::setFixedWidth(row_type r, col_type c)
1181 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1182 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1183 || (multicol && !cell_info[r][c].p_width.zero());
1184 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1189 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1190 Length const & width)
1192 if (!isMultiColumn(cell))
1195 cellInfo(cell).p_width = width;
1196 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1197 // cur paragraph can become invalid after paragraphs were merged
1198 if (cur.pit() > cur.lastpit())
1199 cur.pit() = cur.lastpit();
1200 // cur position can become invalid after newlines were removed
1201 if (cur.pos() > cur.lastpos())
1202 cur.pos() = cur.lastpos();
1207 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1209 cellInfo(cell).mroffset = mroffset;
1214 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1215 Tabular::Feature what)
1217 if (what == SET_SPECIAL_MULTICOLUMN)
1218 cellInfo(cell).align_special = special;
1220 column_info[cellColumn(cell)].align_special = special;
1224 void Tabular::setTopLine(idx_type i, bool line)
1226 cellInfo(i).top_line = line;
1230 void Tabular::setBottomLine(idx_type i, bool line)
1232 cellInfo(i).bottom_line = line;
1236 void Tabular::setLeftLine(idx_type cell, bool line)
1238 cellInfo(cell).left_line = line;
1242 void Tabular::setRightLine(idx_type cell, bool line)
1244 cellInfo(cell).right_line = line;
1247 bool Tabular::rowTopLine(row_type r) const
1249 bool all_rows_set = true;
1250 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1251 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1252 return all_rows_set;
1256 bool Tabular::rowBottomLine(row_type r) const
1258 bool all_rows_set = true;
1259 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1260 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1261 return all_rows_set;
1265 bool Tabular::columnLeftLine(col_type c) const
1272 for (row_type r = 0; r < nrows(); ++r) {
1273 idx_type const i = cellIndex(r, c);
1274 if (c == cellColumn(i)) {
1276 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1277 if (cellInfo(i).left_line || right)
1281 return 2 * nrows_left >= total;
1285 bool Tabular::columnRightLine(col_type c) const
1290 int nrows_right = 0;
1292 for (row_type r = 0; r < nrows(); ++r) {
1293 idx_type i = cellIndex(r, c);
1294 if (c == cellColumn(i) + columnSpan(i) - 1) {
1296 bool left = (c + 1 < ncols()
1297 && cellInfo(cellIndex(r, c + 1)).left_line)
1298 || c + 1 == ncols();
1299 if (cellInfo(i).right_line && left)
1303 return 2 * nrows_right >= total;
1307 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1309 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1310 return cellInfo(cell).alignment;
1312 return column_info[cellColumn(cell)].alignment;
1317 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1319 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1320 return cellInfo(cell).valignment;
1321 return column_info[cellColumn(cell)].valignment;
1325 int Tabular::offsetVAlignment() const
1327 // for top-alignment the first horizontal table line must be exactly at
1328 // the position of the base line of the surrounding text line
1329 // for bottom alignment, the same is for the last table line
1330 int offset_valign = 0;
1331 switch (tabular_valignment) {
1332 case Tabular::LYX_VALIGN_BOTTOM:
1333 offset_valign = rowAscent(0) - height();
1335 case Tabular::LYX_VALIGN_MIDDLE:
1336 offset_valign = (- height()) / 2 + rowAscent(0);
1338 case Tabular::LYX_VALIGN_TOP:
1339 offset_valign = rowAscent(0);
1342 return offset_valign;
1346 Length const Tabular::getPWidth(idx_type cell) const
1348 if (isMultiColumn(cell))
1349 return cellInfo(cell).p_width;
1350 return column_info[cellColumn(cell)].p_width;
1354 Length const Tabular::getMROffset(idx_type cell) const
1356 return cellInfo(cell).mroffset;
1360 int Tabular::textHOffset(idx_type cell) const
1362 // the LaTeX Way :-(
1363 int x = WIDTH_OF_LINE;
1365 int const w = cellWidth(cell) - cellInfo(cell).width;
1367 switch (getAlignment(cell)) {
1368 case LYX_ALIGN_CENTER:
1371 case LYX_ALIGN_RIGHT:
1374 case LYX_ALIGN_DECIMAL: {
1375 // we center when no decimal point
1376 if (cellInfo(cell).decimal_width == 0) {
1380 col_type const c = cellColumn(cell);
1381 int max_dhoffset = 0;
1382 for(row_type r = 0; r < row_info.size() ; ++r) {
1383 idx_type const i = cellIndex(r, c);
1384 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1385 && cellInfo(i).decimal_width != 0)
1386 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1388 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1391 // LYX_ALIGN_LEFT: nothing :-)
1399 int Tabular::textVOffset(idx_type cell) const
1401 int voffset = cellInfo(cell).voffset;
1402 if (isMultiRow(cell)) {
1403 row_type const row = cellRow(cell);
1404 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1410 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1413 idx_type const numcells = numberOfCellsInRow(row);
1414 // we check against numcells to make sure we do not crash if all the
1415 // cells are multirow (bug #7535), but in that case our return value
1416 // is really invalid, i.e., it is NOT the first cell in the row. but
1417 // i do not know what to do here. (rgh)
1418 while (c < numcells - 1
1419 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1421 return cell_info[row][c].cellno;
1425 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1427 col_type c = ncols() - 1;
1428 // of course we check against 0 so we don't crash. but we have the same
1429 // problem as in the previous routine: if all the cells are part of a
1430 // multirow or part of a multi column, then our return value is invalid.
1432 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1433 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1435 return cell_info[row][c].cellno;
1439 Tabular::row_type Tabular::cellRow(idx_type cell) const
1441 if (cell >= numberofcells)
1445 return rowofcell[cell];
1449 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1451 if (cell >= numberofcells)
1455 return columnofcell[cell];
1459 void Tabular::write(ostream & os) const
1463 << write_attribute("version", 3)
1464 << write_attribute("rows", nrows())
1465 << write_attribute("columns", ncols())
1467 // global longtable options
1469 << write_attribute("rotate", rotate)
1470 << write_attribute("booktabs", use_booktabs)
1471 << write_attribute("islongtable", is_long_tabular)
1472 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1473 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1474 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1475 << write_attribute("headTopDL", endhead.topDL)
1476 << write_attribute("headBottomDL", endhead.bottomDL)
1477 << write_attribute("footTopDL", endfoot.topDL)
1478 << write_attribute("footBottomDL", endfoot.bottomDL)
1479 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1480 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1481 << write_attribute("lastFootEmpty", endlastfoot.empty);
1482 // longtables cannot be aligned vertically
1483 if (!is_long_tabular) {
1484 os << write_attribute("tabularvalignment", tabular_valignment);
1485 os << write_attribute("tabularwidth", tabular_width);
1487 if (is_long_tabular)
1488 os << write_attribute("longtabularalignment", longtabular_alignment);
1490 for (col_type c = 0; c < ncols(); ++c) {
1492 << write_attribute("alignment", column_info[c].alignment);
1493 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1494 os << write_attribute("decimal_point", column_info[c].decimal_point);
1495 os << write_attribute("valignment", column_info[c].valignment)
1496 << write_attribute("width", column_info[c].p_width.asString())
1497 << write_attribute("special", column_info[c].align_special)
1500 for (row_type r = 0; r < nrows(); ++r) {
1501 static const string def("default");
1503 if (row_info[r].top_space_default)
1504 os << write_attribute("topspace", def);
1506 os << write_attribute("topspace", row_info[r].top_space);
1507 if (row_info[r].bottom_space_default)
1508 os << write_attribute("bottomspace", def);
1510 os << write_attribute("bottomspace", row_info[r].bottom_space);
1511 if (row_info[r].interline_space_default)
1512 os << write_attribute("interlinespace", def);
1514 os << write_attribute("interlinespace", row_info[r].interline_space);
1515 os << write_attribute("endhead", row_info[r].endhead)
1516 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1517 << write_attribute("endfoot", row_info[r].endfoot)
1518 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1519 << write_attribute("newpage", row_info[r].newpage)
1520 << write_attribute("caption", row_info[r].caption)
1522 for (col_type c = 0; c < ncols(); ++c) {
1524 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1525 << write_attribute("multirow", cell_info[r][c].multirow)
1526 << write_attribute("mroffset", cell_info[r][c].mroffset)
1527 << write_attribute("alignment", cell_info[r][c].alignment)
1528 << write_attribute("valignment", cell_info[r][c].valignment)
1529 << write_attribute("topline", cell_info[r][c].top_line)
1530 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1531 << write_attribute("leftline", cell_info[r][c].left_line)
1532 << write_attribute("rightline", cell_info[r][c].right_line)
1533 << write_attribute("rotate", cell_info[r][c].rotate)
1534 << write_attribute("usebox", cell_info[r][c].usebox)
1535 << write_attribute("width", cell_info[r][c].p_width)
1536 << write_attribute("special", cell_info[r][c].align_special)
1538 os << "\\begin_inset ";
1539 cell_info[r][c].inset->write(os);
1540 os << "\n\\end_inset\n"
1542 // FIXME This can be removed again once the mystery
1543 // crash has been resolved.
1548 os << "</lyxtabular>\n";
1552 void Tabular::read(Lexer & lex)
1555 istream & is = lex.getStream();
1557 l_getline(is, line);
1558 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1559 LASSERT(false, return);
1563 if (!getTokenValue(line, "version", version))
1565 LATTEST(version >= 2);
1568 if (!getTokenValue(line, "rows", rows_arg))
1571 if (!getTokenValue(line, "columns", columns_arg))
1573 init(buffer_, rows_arg, columns_arg);
1574 l_getline(is, line);
1575 if (!prefixIs(line, "<features")) {
1576 lyxerr << "Wrong tabular format (expected <features ...> got"
1577 << line << ')' << endl;
1580 getTokenValue(line, "rotate", rotate);
1581 getTokenValue(line, "booktabs", use_booktabs);
1582 getTokenValue(line, "islongtable", is_long_tabular);
1583 getTokenValue(line, "tabularvalignment", tabular_valignment);
1584 getTokenValue(line, "tabularwidth", tabular_width);
1585 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1586 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1587 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1588 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1589 getTokenValue(line, "headTopDL", endhead.topDL);
1590 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1591 getTokenValue(line, "footTopDL", endfoot.topDL);
1592 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1593 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1594 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1595 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1597 for (col_type c = 0; c < ncols(); ++c) {
1599 if (!prefixIs(line,"<column")) {
1600 lyxerr << "Wrong tabular format (expected <column ...> got"
1601 << line << ')' << endl;
1604 getTokenValue(line, "alignment", column_info[c].alignment);
1605 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1606 getTokenValue(line, "valignment", column_info[c].valignment);
1607 getTokenValue(line, "width", column_info[c].p_width);
1608 getTokenValue(line, "special", column_info[c].align_special);
1611 for (row_type i = 0; i < nrows(); ++i) {
1612 l_getline(is, line);
1613 if (!prefixIs(line, "<row")) {
1614 lyxerr << "Wrong tabular format (expected <row ...> got"
1615 << line << ')' << endl;
1618 getTokenValue(line, "topspace", row_info[i].top_space,
1619 row_info[i].top_space_default);
1620 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1621 row_info[i].bottom_space_default);
1622 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1623 row_info[i].interline_space_default);
1624 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1625 getTokenValue(line, "endhead", row_info[i].endhead);
1626 getTokenValue(line, "endfoot", row_info[i].endfoot);
1627 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1628 getTokenValue(line, "newpage", row_info[i].newpage);
1629 getTokenValue(line, "caption", row_info[i].caption);
1630 for (col_type j = 0; j < ncols(); ++j) {
1631 l_getline(is, line);
1632 if (!prefixIs(line, "<cell")) {
1633 lyxerr << "Wrong tabular format (expected <cell ...> got"
1634 << line << ')' << endl;
1637 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1638 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1639 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1640 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1641 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1642 getTokenValue(line, "topline", cell_info[i][j].top_line);
1643 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1644 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1645 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1646 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1647 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1648 getTokenValue(line, "width", cell_info[i][j].p_width);
1650 getTokenValue(line, "special", cell_info[i][j].align_special);
1651 l_getline(is, line);
1652 if (prefixIs(line, "\\begin_inset")) {
1653 cell_info[i][j].inset->setBuffer(*buffer_);
1654 cell_info[i][j].inset->read(lex);
1655 l_getline(is, line);
1657 if (!prefixIs(line, "</cell>")) {
1658 lyxerr << "Wrong tabular format (expected </cell> got"
1659 << line << ')' << endl;
1663 l_getline(is, line);
1664 if (!prefixIs(line, "</row>")) {
1665 lyxerr << "Wrong tabular format (expected </row> got"
1666 << line << ')' << endl;
1670 while (!prefixIs(line, "</lyxtabular>")) {
1671 l_getline(is, line);
1677 bool Tabular::isMultiColumn(idx_type cell) const
1679 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1680 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1684 bool Tabular::hasMultiColumn(col_type c) const
1686 for (row_type r = 0; r < nrows(); ++r) {
1687 if (isMultiColumn(cellIndex(r, c)))
1694 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1696 return cell_info[cellRow(cell)][cellColumn(cell)];
1700 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1702 return cell_info[cellRow(cell)][cellColumn(cell)];
1706 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1707 bool const right_border)
1709 idx_type const col = cellColumn(cell);
1710 idx_type const row = cellRow(cell);
1711 for (idx_type i = 0; i < number; ++i)
1712 unsetMultiRow(cellIndex(row, col + i));
1714 // unsetting of multirow may have invalidated cell index
1715 cell = cellIndex(row, col);
1716 CellData & cs = cellInfo(cell);
1717 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1718 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1719 cs.alignment = column_info[col].alignment;
1720 setRightLine(cell, right_border);
1722 idx_type lastcell = cellIndex(row, col + number - 1);
1723 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1724 CellData & cs1 = cellInfo(cell + i);
1725 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1726 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1734 bool Tabular::isMultiRow(idx_type cell) const
1736 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1737 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1740 bool Tabular::hasMultiRow(row_type r) const
1742 for (col_type c = 0; c < ncols(); ++c) {
1743 if (isMultiRow(cellIndex(r, c)))
1749 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1750 bool const bottom_border,
1751 LyXAlignment const halign)
1753 idx_type const col = cellColumn(cell);
1754 idx_type const row = cellRow(cell);
1755 for (idx_type i = 0; i < number; ++i)
1756 unsetMultiColumn(cellIndex(row + i, col));
1758 // unsetting of multirow may have invalidated cell index
1759 cell = cellIndex(row, col);
1760 CellData & cs = cellInfo(cell);
1761 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1762 cs.valignment = LYX_VALIGN_MIDDLE;
1763 // the horizontal alignment of multirow cells can only
1764 // be changed for the whole table row,
1765 // support changing this only for the multirow cell can be done via
1767 if (getPWidth(cell).zero())
1768 cs.alignment = halign;
1770 cs.alignment = LYX_ALIGN_LEFT;
1772 // set the bottom line of the last selected cell
1773 setBottomLine(cell, bottom_border);
1775 for (idx_type i = 1; i < number; ++i) {
1776 CellData & cs1 = cell_info[row + i][col];
1777 cs1.multirow = CELL_PART_OF_MULTIROW;
1778 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1786 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1788 row_type const row = cellRow(cell);
1789 col_type const col = cellColumn(cell);
1791 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1798 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1800 col_type const column = cellColumn(cell);
1801 col_type row = cellRow(cell) + 1;
1802 while (row < nrows() && isPartOfMultiRow(row, column))
1805 return row - cellRow(cell);
1809 void Tabular::unsetMultiColumn(idx_type cell)
1811 if (!isMultiColumn(cell))
1814 row_type const row = cellRow(cell);
1815 col_type const col = cellColumn(cell);
1816 row_type const span = columnSpan(cell);
1817 for (col_type c = 0; c < span; ++c) {
1818 // in the table dialog the lines are set in every case
1819 // when unsetting a multicolumn this leads to an additional right
1820 // line for every cell that was part of the former multicolumn cell,
1821 // except if the cell is in the last column
1822 // therefore remove this line
1823 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1824 && (col + c) < (col + span - 1))
1825 cell_info[row][col + c].right_line = false;
1826 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1832 void Tabular::unsetMultiRow(idx_type cell)
1834 if (!isMultiRow(cell))
1837 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1838 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1839 row_type const row = cellRow(cell);
1840 col_type const col = cellColumn(cell);
1841 row_type const span = rowSpan(cell);
1842 for (row_type r = 0; r < span; ++r)
1843 cell_info[row + r][col].multirow = CELL_NORMAL;
1848 void Tabular::setRotateCell(idx_type cell, int value)
1850 cellInfo(cell).rotate = value;
1854 int Tabular::getRotateCell(idx_type cell) const
1856 return cellInfo(cell).rotate;
1860 bool Tabular::needRotating() const
1864 for (row_type r = 0; r < nrows(); ++r)
1865 for (col_type c = 0; c < ncols(); ++c)
1866 if (cell_info[r][c].rotate != 0)
1872 bool Tabular::isLastCell(idx_type cell) const
1874 if (cell + 1 < numberofcells)
1880 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1882 if (cellRow(cell) == 0)
1885 col_type const col = cellColumn(cell);
1886 row_type r = cellRow(cell) - 1;
1887 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1890 return cell_info[r][col].cellno;
1894 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1896 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1897 if (nextrow < nrows())
1898 return cell_info[nextrow][cellColumn(cell)].cellno;
1903 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1905 LASSERT(column != npos && column < ncols(), column = 0);
1906 LASSERT(row != npos && row < nrows(), row = 0);
1907 return cell_info[row][column].cellno;
1911 void Tabular::setUsebox(idx_type cell, BoxType type)
1913 cellInfo(cell).usebox = type;
1917 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1918 // adds line breaks, see bug 4886.
1919 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1921 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1922 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1924 if (cellInfo(cell).usebox > 1)
1925 return cellInfo(cell).usebox;
1926 return useParbox(cell);
1931 // This are functions used for the longtable support
1933 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1939 row_info[row].endfirsthead = flag;
1943 row_info[row].endhead = flag;
1948 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1951 hd.set = haveLTHead();
1952 return row_info[row].endhead;
1956 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1959 hd.set = haveLTFirstHead();
1960 return row_info[row].endfirsthead;
1964 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1970 row_info[row].endlastfoot = flag;
1974 row_info[row].endfoot = flag;
1979 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1982 fd.set = haveLTFoot();
1983 return row_info[row].endfoot;
1987 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1990 fd.set = haveLTLastFoot();
1991 return row_info[row].endlastfoot;
1995 void Tabular::setLTNewPage(row_type row, bool what)
1997 row_info[row].newpage = what;
2001 bool Tabular::getLTNewPage(row_type row) const
2003 return row_info[row].newpage;
2007 bool Tabular::haveLTHead(bool withcaptions) const
2009 if (!is_long_tabular)
2011 for (row_type i = 0; i < nrows(); ++i)
2012 if (row_info[i].endhead &&
2013 (withcaptions || !row_info[i].caption))
2019 bool Tabular::haveLTFirstHead(bool withcaptions) const
2021 if (!is_long_tabular || endfirsthead.empty)
2023 for (row_type r = 0; r < nrows(); ++r)
2024 if (row_info[r].endfirsthead &&
2025 (withcaptions || !row_info[r].caption))
2031 bool Tabular::haveLTFoot(bool withcaptions) const
2033 if (!is_long_tabular)
2035 for (row_type r = 0; r < nrows(); ++r)
2036 if (row_info[r].endfoot &&
2037 (withcaptions || !row_info[r].caption))
2043 bool Tabular::haveLTLastFoot(bool withcaptions) const
2045 if (!is_long_tabular || endlastfoot.empty)
2047 for (row_type r = 0; r < nrows(); ++r)
2048 if (row_info[r].endlastfoot &&
2049 (withcaptions || !row_info[r].caption))
2055 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2057 idx_type i = getFirstCellInRow(row);
2059 setMultiColumn(i, numberOfCellsInRow(row), false);
2060 setTopLine(i, false);
2061 setBottomLine(i, false);
2062 setLeftLine(i, false);
2063 setRightLine(i, false);
2064 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2065 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2066 setLTHead(row, true, endfirsthead, true);
2067 row_info[row].endfirsthead = true;
2070 unsetMultiColumn(i);
2071 // When unsetting a caption row, also all existing
2072 // captions in this row must be dissolved.
2074 row_info[row].caption = what;
2079 bool Tabular::ltCaption(row_type row) const
2081 return row_info[row].caption;
2085 bool Tabular::haveLTCaption(CaptionType captiontype) const
2087 if (!is_long_tabular)
2089 for (row_type r = 0; r < nrows(); ++r) {
2090 if (row_info[r].caption) {
2091 switch (captiontype) {
2092 case CAPTION_FIRSTHEAD:
2093 if (row_info[r].endfirsthead)
2097 if (row_info[r].endhead)
2101 if (row_info[r].endfoot)
2104 case CAPTION_LASTFOOT:
2105 if (row_info[r].endlastfoot)
2117 // end longtable support functions
2119 void Tabular::setRowAscent(row_type row, int height)
2121 if (row >= nrows() || row_info[row].ascent == height)
2123 row_info[row].ascent = height;
2127 void Tabular::setRowDescent(row_type row, int height)
2129 if (row >= nrows() || row_info[row].descent == height)
2131 row_info[row].descent = height;
2135 int Tabular::rowAscent(row_type row) const
2137 LASSERT(row < nrows(), row = 0);
2138 return row_info[row].ascent;
2142 int Tabular::rowDescent(row_type row) const
2144 LASSERT(row < nrows(), row = 0);
2145 return row_info[row].descent;
2149 int Tabular::height() const
2152 for (row_type row = 0; row < nrows(); ++row)
2153 height += rowAscent(row) + rowDescent(row) +
2159 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2161 LASSERT(row < nrows(), return false);
2162 LASSERT(column < ncols(), return false);
2163 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2167 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2169 LASSERT(row < nrows(), return false);
2170 LASSERT(column < ncols(), return false);
2171 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2175 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2177 // we only output complete row lines and the 1st row here, the rest
2178 // is done in Tabular::TeXBottomHLine(...)
2180 // get for each column the topline (if any)
2181 vector<bool> topline;
2183 for (col_type c = 0; c < ncols(); ++c) {
2184 topline.push_back(topLine(cellIndex(row, c)));
2185 // If cell is part of a multirow and not the first cell of the
2186 // multirow, no line must be drawn.
2188 if (isMultiRow(cellIndex(row, c))
2189 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2195 // do nothing if empty first row, or incomplete row line after
2196 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2199 // only output complete row lines and the 1st row's clines
2200 if (nset == ncols()) {
2202 os << (row == 0 ? "\\toprule " : "\\midrule ");
2206 } else if (row == 0) {
2207 for (col_type c = 0; c < ncols(); ++c) {
2209 col_type offset = 0;
2210 for (col_type j = 0 ; j < c; ++j)
2211 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2214 //babel makes the "-" character an active one, so we have to suppress this here
2215 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2216 if (lang == "slovak" || lang == "czech")
2217 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2218 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2220 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2222 col_type cstart = c;
2223 for ( ; c < ncols() && topline[c]; ++c) {}
2225 for (col_type j = cstart ; j < c ; ++j)
2226 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2229 os << c + offset << "} ";
2237 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2239 // we output bottomlines of row r and the toplines of row r+1
2240 // if the latter do not span the whole tabular
2242 // get the bottomlines of row r, and toplines in next row
2243 bool lastrow = row == nrows() - 1;
2244 vector<bool> bottomline, topline;
2245 bool nextrowset = true;
2246 for (col_type c = 0; c < ncols(); ++c) {
2247 bottomline.push_back(bottomLine(cellIndex(row, c)));
2248 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2249 // If cell is part of a multirow and not the last cell of the
2250 // multirow, no line must be drawn.
2252 if (isMultiRow(cellIndex(row, c))
2253 && isMultiRow(cellIndex(row + 1, c))
2254 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2255 bottomline[c] = false;
2258 nextrowset &= topline[c];
2261 // combine this row's bottom lines and next row's toplines if necessary
2263 for (col_type c = 0; c < ncols(); ++c) {
2265 bottomline[c] = bottomline[c] || topline[c];
2270 // do nothing if empty, OR incomplete row line with a topline in next row
2271 if (nset == 0 || (nextrowset && nset != ncols()))
2274 if (nset == ncols()) {
2276 os << (lastrow ? "\\bottomrule" : "\\midrule");
2280 for (col_type c = 0; c < ncols(); ++c) {
2281 if (bottomline[c]) {
2282 col_type offset = 0;
2283 for (col_type j = 0 ; j < c; ++j)
2284 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2287 //babel makes the "-" character an active one, so we have to suppress this here
2288 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2289 if (lang == "slovak" || lang == "czech")
2290 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2291 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2293 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2295 col_type cstart = c;
2296 for ( ; c < ncols() && bottomline[c]; ++c) {}
2298 for (col_type j = cstart ; j < c ; ++j)
2299 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2302 os << c + offset << "} ";
2310 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2311 bool & ismulticol, bool & ismultirow) const
2313 row_type const r = cellRow(cell);
2314 if (is_long_tabular && row_info[r].caption)
2317 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2318 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2319 // figure out how to set the lines
2320 // we always set double lines to the right of the cell
2321 col_type const c = cellColumn(cell);
2322 col_type const nextcol = c + columnSpan(cell);
2323 bool colright = columnRightLine(c);
2324 bool colleft = columnLeftLine(c);
2325 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2326 bool nextcellleft = nextcol < ncols()
2327 && leftLine(cellIndex(r, nextcol));
2328 bool coldouble = colright && nextcolleft;
2329 bool celldouble = rightLine(cell) && nextcellleft;
2331 ismulticol = isMultiColumn(cell)
2332 || (c == 0 && colleft != leftLine(cell))
2333 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2334 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2335 || (coldouble != celldouble);
2337 // we center in multicol when no decimal point
2338 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2339 docstring const align_d = column_info[c].decimal_point;
2340 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2344 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2345 int latexcolspan = columnSpan(cell);
2346 for(col_type col = c; col < c + columnSpan(cell); ++col)
2347 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2351 os << "\\multicolumn{" << latexcolspan << "}{";
2352 if (c ==0 && leftLine(cell))
2354 if (!cellInfo(cell).align_special.empty()) {
2355 os << cellInfo(cell).align_special;
2357 if (!getPWidth(cell).zero()) {
2359 case LYX_ALIGN_LEFT:
2360 os << ">{\\raggedright}";
2362 case LYX_ALIGN_RIGHT:
2363 os << ">{\\raggedleft}";
2365 case LYX_ALIGN_CENTER:
2366 os << ">{\\centering}";
2372 case LYX_VALIGN_TOP:
2375 case LYX_VALIGN_MIDDLE:
2378 case LYX_VALIGN_BOTTOM:
2383 << from_ascii(getPWidth(cell).asLatexString())
2387 case LYX_ALIGN_LEFT:
2390 case LYX_ALIGN_RIGHT:
2397 } // end if else !getPWidth
2398 } // end if else !cellinfo_of_cell
2399 if (rightLine(cell) || nextcellleft)
2402 // add extra vertical line if we want a double one
2405 } // end if ismulticol
2407 // we only need code for the first multirow cell
2408 ismultirow = isMultiRow(cell);
2410 os << "\\multirow{" << rowSpan(cell) << "}{";
2411 if (!getPWidth(cell).zero())
2412 os << from_ascii(getPWidth(cell).asLatexString());
2414 // we need to set a default value
2417 if (!getMROffset(cell).zero())
2418 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2420 } // end if ismultirow
2422 if (getRotateCell(cell) != 0)
2423 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2425 if (getUsebox(cell) == BOX_PARBOX) {
2428 case LYX_VALIGN_TOP:
2431 case LYX_VALIGN_MIDDLE:
2434 case LYX_VALIGN_BOTTOM:
2438 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2440 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2441 os << "\\begin{minipage}[";
2443 case LYX_VALIGN_TOP:
2446 case LYX_VALIGN_MIDDLE:
2449 case LYX_VALIGN_BOTTOM:
2453 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2459 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2460 bool ismulticol, bool ismultirow) const
2462 row_type const r = cellRow(cell);
2463 if (is_long_tabular && row_info[r].caption)
2467 if (getUsebox(cell) == BOX_PARBOX)
2469 else if (getUsebox(cell) == BOX_MINIPAGE)
2470 os << breakln << "\\end{minipage}";
2471 if (getRotateCell(cell) != 0)
2472 os << breakln << "\\end{turn}";
2480 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2481 OutputParams const & runparams) const
2483 if (!is_long_tabular)
2487 // output caption which is in no header or footer
2488 if (haveLTCaption()) {
2489 for (row_type r = 0; r < nrows(); ++r) {
2490 if (row_info[r].caption &&
2491 !row_info[r].endfirsthead && !row_info[r].endhead &&
2492 !row_info[r].endfoot && !row_info[r].endlastfoot)
2493 TeXRow(os, r, runparams);
2496 // output first header info
2497 if (haveLTFirstHead()) {
2498 if (endfirsthead.topDL)
2500 for (row_type r = 0; r < nrows(); ++r) {
2501 if (row_info[r].endfirsthead)
2502 TeXRow(os, r, runparams);
2504 if (endfirsthead.bottomDL)
2506 os << "\\endfirsthead\n";
2508 // output header info
2510 if (endfirsthead.empty && !haveLTFirstHead())
2511 os << "\\endfirsthead\n";
2514 for (row_type r = 0; r < nrows(); ++r) {
2515 if (row_info[r].endhead)
2516 TeXRow(os, r, runparams);
2518 if (endhead.bottomDL)
2520 os << "\\endhead\n";
2522 // output footer info
2526 for (row_type r = 0; r < nrows(); ++r) {
2527 if (row_info[r].endfoot)
2528 TeXRow(os, r, runparams);
2530 if (endfoot.bottomDL)
2532 os << "\\endfoot\n";
2533 if (endlastfoot.empty && !haveLTLastFoot())
2534 os << "\\endlastfoot\n";
2536 // output lastfooter info
2537 if (haveLTLastFoot()) {
2538 if (endlastfoot.topDL)
2540 for (row_type r = 0; r < nrows(); ++r) {
2541 if (row_info[r].endlastfoot)
2542 TeXRow(os, r, runparams);
2544 if (endlastfoot.bottomDL)
2546 os << "\\endlastfoot\n";
2551 bool Tabular::isValidRow(row_type row) const
2553 if (!is_long_tabular)
2555 return !row_info[row].endhead && !row_info[row].endfirsthead
2556 && !row_info[row].endfoot && !row_info[row].endlastfoot
2557 && !row_info[row].caption;
2561 void Tabular::TeXRow(otexstream & os, row_type row,
2562 OutputParams const & runparams) const
2564 idx_type cell = cellIndex(row, 0);
2565 InsetTableCell const * inset = cellInset(cell);
2566 Paragraph const & par = inset->paragraphs().front();
2567 string const lang = par.getParLanguage(buffer().params())->lang();
2569 //output the top line
2570 TeXTopHLine(os, row, lang);
2572 if (row_info[row].top_space_default) {
2574 os << "\\addlinespace\n";
2576 os << "\\noalign{\\vskip\\doublerulesep}\n";
2577 } else if(!row_info[row].top_space.zero()) {
2579 os << "\\addlinespace["
2580 << from_ascii(row_info[row].top_space.asLatexString())
2583 os << "\\noalign{\\vskip"
2584 << from_ascii(row_info[row].top_space.asLatexString())
2588 bool ismulticol = false;
2589 bool ismultirow = false;
2590 for (col_type c = 0; c < ncols(); ++c) {
2591 if (isPartOfMultiColumn(row, c))
2594 cell = cellIndex(row, c);
2596 if (isPartOfMultiRow(row, c)
2597 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2598 if (cell != getLastCellInRow(row))
2603 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2604 InsetTableCell const * inset = cellInset(cell);
2606 Paragraph const & par = inset->paragraphs().front();
2608 os.texrow().forceStart(par.id(), 0);
2610 bool rtl = par.isRTL(buffer().params())
2612 && getPWidth(cell).zero()
2613 && !runparams.use_polyglossia;
2617 par.getParLanguage(buffer().params())->lang();
2618 if (lang == "farsi")
2620 else if (lang == "arabic_arabi")
2622 // currently, remaining RTL languages are
2623 // arabic_arabtex and hebrew
2627 // pass to the OutputParams that we are in a cell and
2628 // which alignment we have set.
2629 // InsetNewline needs this context information.
2630 OutputParams newrp(runparams);
2631 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2632 ? OutputParams::PLAIN
2633 : OutputParams::ALIGNED;
2635 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2636 // copy cell and split in 2
2637 InsetTableCell head = InsetTableCell(*cellInset(cell));
2638 head.setBuffer(const_cast<Buffer &>(buffer()));
2639 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2641 dit.push_back(CursorSlice(head));
2642 head.setMacrocontextPositionRecursive(dit);
2643 bool hassep = false;
2644 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2645 head.latex(os, newrp);
2648 tail.setBuffer(head.buffer());
2650 dit.push_back(CursorSlice(tail));
2651 tail.setMacrocontextPositionRecursive(dit);
2652 tail.latex(os, newrp);
2654 } else if (!isPartOfMultiRow(row, c)) {
2655 if (!runparams.nice)
2656 os.texrow().start(par.id(), 0);
2657 inset->latex(os, newrp);
2660 runparams.encoding = newrp.encoding;
2664 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2665 if (cell != getLastCellInRow(row)) { // not last cell in row
2672 os << "\\tabularnewline";
2673 if (row_info[row].bottom_space_default) {
2675 os << "\\addlinespace";
2677 os << "[\\doublerulesep]";
2678 } else if (!row_info[row].bottom_space.zero()) {
2680 os << "\\addlinespace";
2682 << from_ascii(row_info[row].bottom_space.asLatexString())
2687 //output the bottom line
2688 TeXBottomHLine(os, row, lang);
2690 if (row_info[row].interline_space_default) {
2692 os << "\\addlinespace\n";
2694 os << "\\noalign{\\vskip\\doublerulesep}\n";
2695 } else if (!row_info[row].interline_space.zero()) {
2697 os << "\\addlinespace["
2698 << from_ascii(row_info[row].interline_space.asLatexString())
2701 os << "\\noalign{\\vskip"
2702 << from_ascii(row_info[row].interline_space.asLatexString())
2708 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2710 bool const is_tabular_star = !tabular_width.zero();
2711 TexRow::RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2713 //+---------------------------------------------------------------------
2714 //+ first the opening preamble +
2715 //+---------------------------------------------------------------------
2718 if (!TexRow::isNone(pos))
2719 os.texrow().start(pos);
2722 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2724 if (is_long_tabular) {
2725 os << "\\begin{longtable}";
2726 switch (longtabular_alignment) {
2727 case LYX_LONGTABULAR_ALIGN_LEFT:
2730 case LYX_LONGTABULAR_ALIGN_CENTER:
2732 case LYX_LONGTABULAR_ALIGN_RIGHT:
2737 if (is_tabular_star)
2738 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2740 os << "\\begin{tabular}";
2741 switch (tabular_valignment) {
2742 case LYX_VALIGN_TOP:
2745 case LYX_VALIGN_MIDDLE:
2747 case LYX_VALIGN_BOTTOM:
2755 if (is_tabular_star)
2756 os << "@{\\extracolsep{\\fill}}";
2758 for (col_type c = 0; c < ncols(); ++c) {
2759 if (columnLeftLine(c))
2761 if (!column_info[c].align_special.empty()) {
2762 os << column_info[c].align_special;
2764 if (!column_info[c].p_width.zero()) {
2765 bool decimal = false;
2766 switch (column_info[c].alignment) {
2767 case LYX_ALIGN_LEFT:
2768 os << ">{\\raggedright}";
2770 case LYX_ALIGN_RIGHT:
2771 os << ">{\\raggedleft}";
2773 case LYX_ALIGN_CENTER:
2774 os << ">{\\centering}";
2776 case LYX_ALIGN_NONE:
2777 case LYX_ALIGN_BLOCK:
2778 case LYX_ALIGN_LAYOUT:
2779 case LYX_ALIGN_SPECIAL:
2781 case LYX_ALIGN_DECIMAL:
2782 os << ">{\\raggedleft}";
2788 switch (column_info[c].valignment) {
2789 case LYX_VALIGN_TOP:
2790 // this is the default
2792 case LYX_VALIGN_MIDDLE:
2795 case LYX_VALIGN_BOTTOM:
2801 // Fixed-width cells with alignment at decimal separator
2802 // are output as two cells of half the width with the decimal
2803 // separator as column sep. This effectively puts the content
2804 // centered, which differs from the normal decimal sep alignment
2805 // and is not ideal, but we cannot do better ATM (see #9568).
2806 // FIXME: Implement proper decimal sep alignment, e.g. via siunitx.
2808 docstring const halffixedwith =
2809 from_ascii(Length(column_info[c].p_width.value() / 2,
2810 column_info[c].p_width.unit()).asLatexString());
2814 << "@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}"
2821 << from_ascii(column_info[c].p_width.asLatexString())
2824 switch (column_info[c].alignment) {
2825 case LYX_ALIGN_LEFT:
2828 case LYX_ALIGN_RIGHT:
2831 case LYX_ALIGN_DECIMAL:
2832 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2838 } // end if else !column_info[i].p_width
2839 } // end if else !column_info[i].align_special
2840 if (columnRightLine(c))
2845 TeXLongtableHeaderFooter(os, runparams);
2847 //+---------------------------------------------------------------------
2848 //+ the single row and columns (cells) +
2849 //+---------------------------------------------------------------------
2851 for (row_type r = 0; r < nrows(); ++r) {
2852 if (isValidRow(r)) {
2853 TeXRow(os, r, runparams);
2854 if (is_long_tabular && row_info[r].newpage)
2855 os << "\\newpage\n";
2859 //+---------------------------------------------------------------------
2860 //+ the closing of the tabular +
2861 //+---------------------------------------------------------------------
2863 if (is_long_tabular)
2864 os << "\\end{longtable}";
2866 if (is_tabular_star)
2867 os << "\\end{tabular*}";
2869 os << "\\end{tabular}";
2873 os << breakln << "\\end{turn}";
2875 if (!TexRow::isNone(pos))
2876 os.texrow().start(pos);
2880 int Tabular::docbookRow(odocstream & os, row_type row,
2881 OutputParams const & runparams) const
2884 idx_type cell = getFirstCellInRow(row);
2887 for (col_type c = 0; c < ncols(); ++c) {
2888 if (isPartOfMultiColumn(row, c))
2891 os << "<entry align=\"";
2892 switch (getAlignment(cell)) {
2893 case LYX_ALIGN_LEFT:
2896 case LYX_ALIGN_RIGHT:
2904 os << "\" valign=\"";
2905 switch (getVAlignment(cell)) {
2906 case LYX_VALIGN_TOP:
2909 case LYX_VALIGN_BOTTOM:
2912 case LYX_VALIGN_MIDDLE:
2917 if (isMultiColumn(cell)) {
2918 os << " namest=\"col" << c << "\" ";
2919 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2923 ret += cellInset(cell)->docbook(os, runparams);
2932 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2936 //+---------------------------------------------------------------------
2937 //+ first the opening preamble +
2938 //+---------------------------------------------------------------------
2940 os << "<tgroup cols=\"" << ncols()
2941 << "\" colsep=\"1\" rowsep=\"1\">\n";
2943 for (col_type c = 0; c < ncols(); ++c) {
2944 os << "<colspec colname=\"col" << c << "\" align=\"";
2945 switch (column_info[c].alignment) {
2946 case LYX_ALIGN_LEFT:
2949 case LYX_ALIGN_RIGHT:
2957 if (runparams.flavor == OutputParams::XML)
2963 //+---------------------------------------------------------------------
2964 //+ Long Tabular case +
2965 //+---------------------------------------------------------------------
2967 // output caption info
2968 // The caption flag wins over head/foot
2969 if (haveLTCaption()) {
2970 os << "<caption>\n";
2972 for (row_type r = 0; r < nrows(); ++r) {
2973 if (row_info[r].caption) {
2974 ret += docbookRow(os, r, runparams);
2977 os << "</caption>\n";
2980 // output header info
2981 if (haveLTHead(false) || haveLTFirstHead(false)) {
2984 for (row_type r = 0; r < nrows(); ++r) {
2985 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2986 !row_info[r].caption) {
2987 ret += docbookRow(os, r, runparams);
2993 // output footer info
2994 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2997 for (row_type r = 0; r < nrows(); ++r) {
2998 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2999 !row_info[r].caption) {
3000 ret += docbookRow(os, r, runparams);
3007 //+---------------------------------------------------------------------
3008 //+ the single row and columns (cells) +
3009 //+---------------------------------------------------------------------
3013 for (row_type r = 0; r < nrows(); ++r) {
3014 if (isValidRow(r)) {
3015 ret += docbookRow(os, r, runparams);
3020 //+---------------------------------------------------------------------
3021 //+ the closing of the tabular +
3022 //+---------------------------------------------------------------------
3031 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3032 OutputParams const & runparams, bool header) const
3035 string const celltag = header ? "th" : "td";
3036 idx_type cell = getFirstCellInRow(row);
3038 xs << html::StartTag("tr");
3039 for (col_type c = 0; c < ncols(); ++c) {
3040 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3045 Length const cwidth = column_info[c].p_width;
3046 if (!cwidth.zero()) {
3047 string const hwidth = cwidth.asHTMLString();
3048 attr << "style =\"width: " << hwidth << ";\" ";
3052 switch (getAlignment(cell)) {
3053 case LYX_ALIGN_LEFT:
3056 case LYX_ALIGN_RIGHT:
3064 attr << " valign='";
3065 switch (getVAlignment(cell)) {
3066 case LYX_VALIGN_TOP:
3069 case LYX_VALIGN_BOTTOM:
3072 case LYX_VALIGN_MIDDLE:
3077 if (isMultiColumn(cell))
3078 attr << " colspan='" << columnSpan(cell) << "'";
3079 else if (isMultiRow(cell))
3080 attr << " rowspan='" << rowSpan(cell) << "'";
3082 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3083 ret += cellInset(cell)->xhtml(xs, runparams);
3084 xs << html::EndTag(celltag) << html::CR();
3087 xs << html::EndTag("tr");
3092 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3096 if (is_long_tabular) {
3097 // we'll wrap it in a div, so as to deal with alignment
3099 switch (longtabular_alignment) {
3100 case LYX_LONGTABULAR_ALIGN_LEFT:
3103 case LYX_LONGTABULAR_ALIGN_CENTER:
3106 case LYX_LONGTABULAR_ALIGN_RIGHT:
3110 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3112 // The caption flag wins over head/foot
3113 if (haveLTCaption()) {
3114 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3116 for (row_type r = 0; r < nrows(); ++r)
3117 if (row_info[r].caption)
3118 ret += xhtmlRow(xs, r, runparams);
3119 xs << html::EndTag("div") << html::CR();
3123 xs << html::StartTag("table") << html::CR();
3125 // output header info
3126 bool const havefirsthead = haveLTFirstHead(false);
3127 // if we have a first head, then we are going to ignore the
3128 // headers for the additional pages, since there aren't any
3129 // in XHTML. this test accomplishes that.
3130 bool const havehead = !havefirsthead && haveLTHead(false);
3131 if (havehead || havefirsthead) {
3132 xs << html::StartTag("thead") << html::CR();
3133 for (row_type r = 0; r < nrows(); ++r) {
3134 if (((havefirsthead && row_info[r].endfirsthead) ||
3135 (havehead && row_info[r].endhead)) &&
3136 !row_info[r].caption) {
3137 ret += xhtmlRow(xs, r, runparams, true);
3140 xs << html::EndTag("thead") << html::CR();
3142 // output footer info
3143 bool const havelastfoot = haveLTLastFoot(false);
3145 bool const havefoot = !havelastfoot && haveLTFoot(false);
3146 if (havefoot || havelastfoot) {
3147 xs << html::StartTag("tfoot") << html::CR();
3148 for (row_type r = 0; r < nrows(); ++r) {
3149 if (((havelastfoot && row_info[r].endlastfoot) ||
3150 (havefoot && row_info[r].endfoot)) &&
3151 !row_info[r].caption) {
3152 ret += xhtmlRow(xs, r, runparams);
3155 xs << html::EndTag("tfoot") << html::CR();
3158 xs << html::StartTag("tbody") << html::CR();
3159 for (row_type r = 0; r < nrows(); ++r) {
3160 if (isValidRow(r)) {
3161 ret += xhtmlRow(xs, r, runparams);
3164 xs << html::EndTag("tbody")
3166 << html::EndTag("table")
3168 if (is_long_tabular)
3169 xs << html::EndTag("div") << html::CR();
3174 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3175 vector<unsigned int> const & clen) const
3177 idx_type const fcell = getFirstCellInRow(row);
3178 idx_type const n = numberOfCellsInRow(row) + fcell;
3181 for (idx_type i = fcell; i < n; ++i) {
3191 for (idx_type i = fcell; i < n; ++i) {
3202 col_type column = cellColumn(i);
3203 int len = clen[column];
3204 while (column < ncols() - 1
3205 && isPartOfMultiColumn(row, ++column))
3206 len += clen[column] + 4;
3207 os << docstring(len, ch);
3222 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3223 vector<unsigned int> const & clen) const
3225 idx_type const fcell = getFirstCellInRow(row);
3226 idx_type const n = numberOfCellsInRow(row) + fcell;
3229 for (idx_type i = fcell; i < n; ++i) {
3230 if (bottomLine(i)) {
3239 for (idx_type i = fcell; i < n; ++i) {
3240 if (bottomLine(i)) {
3250 col_type column = cellColumn(i);
3251 int len = clen[column];
3252 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3253 len += clen[column] + 4;
3254 os << docstring(len, ch);
3255 if (bottomLine(i)) {
3269 void Tabular::plaintextPrintCell(odocstringstream & os,
3270 OutputParams const & runparams,
3271 idx_type cell, row_type row, col_type column,
3272 vector<unsigned int> const & clen,
3273 bool onlydata, size_t max_length) const
3275 odocstringstream sstr;
3276 cellInset(cell)->plaintext(sstr, runparams, max_length);
3288 unsigned int len1 = sstr.str().length();
3289 unsigned int len2 = clen[column];
3290 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3291 len2 += clen[column] + 4;
3294 switch (getAlignment(cell)) {
3296 case LYX_ALIGN_LEFT:
3299 case LYX_ALIGN_RIGHT:
3303 case LYX_ALIGN_CENTER:
3309 os << docstring(len1, ' ') << sstr.str()
3310 << docstring(len2, ' ');
3312 if (rightLine(cell))
3319 void Tabular::plaintext(odocstringstream & os,
3320 OutputParams const & runparams, int const depth,
3321 bool onlydata, char_type delim, size_t max_length) const
3323 // first calculate the width of the single columns
3324 vector<unsigned int> clen(ncols());
3327 // first all non multicolumn cells!
3328 for (col_type c = 0; c < ncols(); ++c) {
3330 for (row_type r = 0; r < nrows(); ++r) {
3331 idx_type cell = cellIndex(r, c);
3332 if (isMultiColumn(cell))
3334 odocstringstream sstr;
3335 cellInset(cell)->plaintext(sstr, runparams, max_length);
3336 if (clen[c] < sstr.str().length())
3337 clen[c] = sstr.str().length();
3340 // then all multicolumn cells!
3341 for (col_type c = 0; c < ncols(); ++c) {
3342 for (row_type r = 0; r < nrows(); ++r) {
3343 idx_type cell = cellIndex(r, c);
3344 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3346 odocstringstream sstr;
3347 cellInset(cell)->plaintext(sstr, runparams, max_length);
3348 int len = int(sstr.str().length());
3349 idx_type const n = columnSpan(cell);
3350 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3352 if (len > int(clen[c + n - 1]))
3353 clen[c + n - 1] = len;
3358 for (row_type r = 0; r < nrows(); ++r) {
3359 if (!onlydata && plaintextTopHLine(os, r, clen))
3360 os << docstring(depth * 2, ' ');
3361 for (col_type c = 0; c < ncols(); ++c) {
3362 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3364 if (onlydata && c > 0)
3365 // we don't use operator<< for single UCS4 character.
3366 // see explanation in docstream.h
3368 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3370 if (os.str().size() > max_length)
3375 os << docstring(depth * 2, ' ');
3376 if (plaintextBottomHLine(os, r, clen))
3377 os << docstring(depth * 2, ' ');
3379 if (os.str().size() > max_length)
3385 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3387 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3391 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3393 return cell_info[row][column].inset;
3397 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3399 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3403 void Tabular::setCellInset(row_type row, col_type column,
3404 shared_ptr<InsetTableCell> ins)
3406 CellData & cd = cell_info[row][column];
3411 void Tabular::validate(LaTeXFeatures & features) const
3413 features.require("NeedTabularnewline");
3415 features.require("booktabs");
3416 if (is_long_tabular)
3417 features.require("longtable");
3419 features.require("rotating");
3420 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3421 if (isMultiRow(cell))
3422 features.require("multirow");
3423 if (getVAlignment(cell) != LYX_VALIGN_TOP
3424 || !getPWidth(cell).zero())
3425 features.require("array");
3426 cellInset(cell)->validate(features);
3431 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3433 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3434 ParagraphList::const_iterator cit = parlist.begin();
3435 ParagraphList::const_iterator end = parlist.end();
3437 for (; cit != end; ++cit)
3438 for (int i = 0; i < cit->size(); ++i)
3439 if (cit->isNewline(i))
3446 /////////////////////////////////////////////////////////////////////
3450 /////////////////////////////////////////////////////////////////////
3452 InsetTableCell::InsetTableCell(Buffer * buf)
3453 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3454 contentAlign(LYX_ALIGN_CENTER)
3458 bool InsetTableCell::forcePlainLayout(idx_type) const
3460 return !isFixedWidth;
3464 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3466 return isFixedWidth;
3470 bool InsetTableCell::forceLocalFontSwitch() const
3472 return isFixedWidth;
3476 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3477 FuncStatus & status) const
3479 bool enabled = true;
3480 switch (cmd.action()) {
3481 case LFUN_MATH_DISPLAY:
3482 if (!hasFixedWidth()) {
3487 return InsetText::getStatus(cur, cmd, status);
3489 status.setEnabled(enabled);
3493 docstring InsetTableCell::asString(bool intoInsets)
3496 if (paragraphs().empty())
3498 ParagraphList::const_iterator it = paragraphs().begin();
3499 ParagraphList::const_iterator en = paragraphs().end();
3501 for (; it != en; ++it) {
3506 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3512 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3513 UpdateType utype) const
3515 InsetText::iterateForToc(di, output_active, utype);
3519 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3522 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3523 return InsetText::xhtml(xs, rp);
3528 /////////////////////////////////////////////////////////////////////
3532 /////////////////////////////////////////////////////////////////////
3534 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3536 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3537 rowselect_(false), colselect_(false)
3542 InsetTabular::InsetTabular(InsetTabular const & tab)
3543 : Inset(tab), tabular(tab.tabular),
3544 rowselect_(false), colselect_(false)
3549 InsetTabular::~InsetTabular()
3551 hideDialogs("tabular", this);
3555 void InsetTabular::setBuffer(Buffer & buf)
3557 tabular.setBuffer(buf);
3558 Inset::setBuffer(buf);
3562 bool InsetTabular::insetAllowed(InsetCode code) const
3567 case MATHMACRO_CODE:
3572 return tabular.is_long_tabular;
3580 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3582 return tabular.is_long_tabular &&
3583 (newtype == "Standard" || newtype == "Unnumbered");
3587 void InsetTabular::write(ostream & os) const
3589 os << "Tabular" << endl;
3594 string InsetTabular::contextMenu(BufferView const &, int, int) const
3596 // FIXME: depending on the selection state,
3597 // we could offer a different menu.
3598 return cell(0)->contextMenuName() + ";" + contextMenuName();
3602 string InsetTabular::contextMenuName() const
3604 return "context-tabular";
3608 void InsetTabular::read(Lexer & lex)
3610 //bool const old_format = (lex.getString() == "\\LyXTable");
3618 string token = lex.getString();
3619 while (lex && token != "\\end_inset") {
3621 token = lex.getString();
3624 lex.printError("Missing \\end_inset at this point. ");
3628 int InsetTabular::rowFromY(Cursor & cur, int y) const
3630 // top y coordinate of tabular
3631 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3633 for (; r < tabular.nrows() && y > h; ++r)
3634 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3635 + tabular.interRowSpace(r);
3641 int InsetTabular::columnFromX(Cursor & cur, int x) const
3643 // left x coordinate of tabular
3644 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3646 for (; c < tabular.ncols() && x > w; ++c)
3647 w += tabular.cellWidth(c);
3652 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3654 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3655 // mi.base.textwidth << "\n";
3656 LBUFERR(mi.base.bv);
3658 for (row_type r = 0; r < tabular.nrows(); ++r) {
3661 for (col_type c = 0; c < tabular.ncols(); ++c) {
3662 if (tabular.isPartOfMultiColumn(r, c)
3663 || tabular.isPartOfMultiRow(r, c))
3664 // multicolumn or multirow cell, but not first one
3666 idx_type const cell = tabular.cellIndex(r, c);
3669 Length const p_width = tabular.getPWidth(cell);
3670 if (!p_width.zero())
3671 m.base.textwidth = p_width.inPixels(mi.base);
3672 tabular.cellInset(cell)->metrics(m, dim);
3673 if (!p_width.zero())
3674 dim.wid = m.base.textwidth;
3675 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3676 + tabular.interColumnSpace(cell);
3678 // FIXME(?): do we need a second metrics call?
3679 TextMetrics const & tm =
3680 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3682 // determine horizontal offset because of decimal align (if necessary)
3683 int decimal_width = 0;
3684 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3685 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3686 tail.setBuffer(tabular.buffer());
3687 // we need to set macrocontext position everywhere
3688 // otherwise we crash with nested insets (e.g. footnotes)
3689 // after decimal point
3690 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3692 dit.push_back(CursorSlice(tail));
3693 tail.setMacrocontextPositionRecursive(dit);
3695 // remove text leading decimal point
3696 docstring const align_d = tabular.column_info[c].decimal_point;
3697 dit = separatorPos(&tail, align_d);
3699 pit_type const psize = tail.paragraphs().front().size();
3701 tail.paragraphs().front().eraseChars(0,
3702 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3704 tail.metrics(m, dim1);
3705 decimal_width = dim1.width();
3708 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3709 tabular.cell_info[r][c].decimal_width = decimal_width;
3711 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3712 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3713 int const lastpardes = tm.last().second->descent()
3714 + TEXT_TO_INSET_OFFSET;
3716 switch (tabular.getVAlignment(cell)) {
3717 case Tabular::LYX_VALIGN_TOP:
3719 case Tabular::LYX_VALIGN_MIDDLE:
3720 offset = -(dim.des - lastpardes)/2;
3722 case Tabular::LYX_VALIGN_BOTTOM:
3723 offset = -(dim.des - lastpardes);
3726 tabular.cell_info[r][c].voffset = offset;
3727 maxasc = max(maxasc, dim.asc - offset);
3728 maxdes = max(maxdes, dim.des + offset);
3730 int const top_space = tabular.row_info[r].top_space_default ?
3731 default_line_space :
3732 tabular.row_info[r].top_space.inPixels(mi.base);
3733 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3734 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3735 default_line_space :
3736 tabular.row_info[r].bottom_space.inPixels(mi.base);
3737 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3740 tabular.updateColumnWidths();
3741 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
3742 dim.des = tabular.height() - dim.asc;
3743 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3747 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3750 if (&cur.inset() == this && cur.selection()) {
3751 if (cur.selIsMultiCell()) {
3754 getSelection(cur, rs, re, cs, ce);
3756 idx_type const cell = tabular.cellIndex(row, col);
3757 col_type const cspan = tabular.columnSpan(cell);
3758 row_type const rspan = tabular.rowSpan(cell);
3759 if (col + cspan - 1 >= cs && col <= ce
3760 && row + rspan - 1 >= rs && row <= re)
3763 if (col == tabular.cellColumn(cur.idx())
3764 && row == tabular.cellRow(cur.idx())) {
3765 CursorSlice const & beg = cur.selBegin();
3766 CursorSlice const & end = cur.selEnd();
3768 if ((end.lastpos() > 0 || end.lastpit() > 0)
3769 && end.pos() == end.lastpos() && beg.pos() == 0
3770 && end.pit() == end.lastpit() && beg.pit() == 0)
3778 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3780 x += ADD_TO_TABULAR_WIDTH;
3782 BufferView * bv = pi.base.bv;
3783 Cursor & cur = pi.base.bv->cursor();
3785 // FIXME: As the full background is painted in drawBackground(),
3786 // we have no choice but to do a full repaint for the Text cells.
3787 pi.full_repaint = true;
3789 bool const original_selection_state = pi.selected;
3793 int yy = y + tabular.offsetVAlignment();
3794 for (row_type r = 0; r < tabular.nrows(); ++r) {
3796 for (col_type c = 0; c < tabular.ncols(); ++c) {
3797 if (tabular.isPartOfMultiColumn(r, c))
3800 idx = tabular.cellIndex(r, c);
3802 if (tabular.isPartOfMultiRow(r, c)) {
3803 nx += tabular.cellWidth(idx);
3807 pi.selected |= isCellSelected(cur, r, c);
3808 int const cx = nx + tabular.textHOffset(idx);
3809 int const cy = yy + tabular.textVOffset(idx);
3810 // Cache the Inset position.
3811 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3812 cell(idx)->draw(pi, cx, cy);
3813 drawCellLines(pi, nx, yy, r, idx);
3814 nx += tabular.cellWidth(idx);
3815 pi.selected = original_selection_state;
3818 if (r + 1 < tabular.nrows())
3819 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3820 + tabular.interRowSpace(r + 1);
3825 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3827 x += ADD_TO_TABULAR_WIDTH;
3828 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
3829 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3830 pi.backgroundColor(this));
3834 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3836 Cursor & cur = pi.base.bv->cursor();
3838 x += ADD_TO_TABULAR_WIDTH;
3840 if (!cur.selection())
3842 if (&cur.inset() != this)
3847 bool const full_cell_selected = isCellSelected(cur,
3848 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3850 if (cur.selIsMultiCell() || full_cell_selected) {
3851 for (row_type r = 0; r < tabular.nrows(); ++r) {
3853 for (col_type c = 0; c < tabular.ncols(); ++c) {
3854 if (tabular.isPartOfMultiColumn(r, c))
3857 idx_type const cell = tabular.cellIndex(r, c);
3859 if (tabular.isPartOfMultiRow(r, c)) {
3860 xx += tabular.cellWidth(cell);
3863 int const w = tabular.cellWidth(cell);
3864 int const h = tabular.cellHeight(cell);
3865 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
3866 if (isCellSelected(cur, r, c))
3867 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3870 if (r + 1 < tabular.nrows())
3871 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3872 + tabular.interRowSpace(r + 1);
3876 // FIXME: This code has no effect because InsetTableCell does not handle
3877 // drawSelection other than the trivial implementation in Inset.
3879 // x += cellXPos(cur.idx());
3880 // x += tabular.textHOffset(cur.idx());
3881 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3886 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3887 row_type row, idx_type cell) const
3889 y -= tabular.rowAscent(row);
3890 int const w = tabular.cellWidth(cell);
3891 int const h = tabular.cellHeight(cell);
3892 Color const linecolor = pi.textColor(Color_tabularline);
3893 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3896 bool drawline = tabular.topLine(cell)
3897 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3898 pi.pain.line(x, y, x + w, y,
3899 drawline ? linecolor : gridcolor,
3900 drawline ? Painter::line_solid : Painter::line_onoffdash);
3903 drawline = tabular.bottomLine(cell);
3904 pi.pain.line(x, y + h, x + w, y + h,
3905 drawline ? linecolor : gridcolor,
3906 drawline ? Painter::line_solid : Painter::line_onoffdash);
3909 col_type const col = tabular.cellColumn(cell);
3910 drawline = tabular.leftLine(cell)
3911 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3912 pi.pain.line(x, y, x, y + h,
3913 drawline ? linecolor : gridcolor,
3914 drawline ? Painter::line_solid : Painter::line_onoffdash);
3917 x -= tabular.interColumnSpace(cell);
3918 col_type next_cell_col = col + 1;
3919 while (next_cell_col < tabular.ncols()
3920 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3922 drawline = tabular.rightLine(cell)
3923 || (next_cell_col < tabular.ncols()
3924 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3925 pi.pain.line(x + w, y, x + w, y + h,
3926 drawline ? linecolor : gridcolor,
3927 drawline ? Painter::line_solid : Painter::line_onoffdash);
3931 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3933 //lyxerr << "InsetTabular::edit: " << this << endl;
3937 if (isRightToLeft(cur))
3938 cur.idx() = tabular.getLastCellInRow(0);
3944 if (isRightToLeft(cur))
3945 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3947 cur.idx() = tabular.numberofcells - 1;
3949 cur.pos() = cur.lastpos(); // FIXME crude guess
3951 cur.setCurrentFont();
3952 // FIXME: this accesses the position cache before it is initialized
3953 //cur.bv().fitCursor();
3957 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3959 // In a longtable, tell captions what the current float is
3960 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3961 string const saveflt = cnts.current_float();
3962 if (tabular.is_long_tabular) {
3963 cnts.current_float("table");
3964 // in longtables, we only step the counter once
3965 cnts.step(from_ascii("table"), utype);
3966 cnts.isLongtable(true);
3969 ParIterator it2 = it;
3971 size_t const end = it2.nargs();
3972 for ( ; it2.idx() < end; it2.top().forwardIdx())
3973 buffer().updateBuffer(it2, utype);
3976 if (tabular.is_long_tabular) {
3977 cnts.current_float(saveflt);
3978 cnts.isLongtable(false);
3983 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3984 UpdateType utype) const
3986 DocIterator dit = cpit;
3988 size_t const end = dit.nargs();
3989 for ( ; dit.idx() < end; dit.top().forwardIdx())
3990 cell(dit.idx())->addToToc(dit, output_active, utype);
3994 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
3996 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
3997 return x < x0 || x > x0 + tabular.width();
4001 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
4003 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
4004 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
4005 // there is no margin added vertically to tabular insets.
4006 // However, it works for now.
4007 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
4011 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
4013 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
4017 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
4019 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
4020 << "\n cur:" << cur);
4021 CursorSlice sl = cur.top();
4022 Cursor & bvcur = cur.bv().cursor();
4024 FuncCode const act = cmd.action();
4028 case LFUN_MOUSE_PRESS: {
4029 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4031 if (hitSelectRow(cur.bv(), cmd.x())) {
4032 row_type r = rowFromY(cur, cmd.y());
4033 cur.idx() = tabular.getFirstCellInRow(r);
4037 cur.idx() = tabular.getLastCellInRow(r);
4038 cur.pit() = cur.lastpit();
4039 cur.pos() = cur.lastpos();
4040 cur.selection(true);
4046 if (hitSelectColumn(cur.bv(), cmd.y())) {
4047 col_type c = columnFromX(cur, cmd.x());
4048 cur.idx() = tabular.cellIndex(0, c);
4052 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4053 cur.pit() = cur.lastpit();
4054 cur.pos() = cur.lastpos();
4055 cur.selection(true);
4060 // do not reset cursor/selection if we have selected
4061 // some cells (bug 2715).
4062 if (cmd.button() == mouse_button::button3
4063 && &bvcur.selBegin().inset() == this
4064 && bvcur.selIsMultiCell())
4067 // Let InsetTableCell do it
4068 cell(cur.idx())->dispatch(cur, cmd);
4071 case LFUN_MOUSE_MOTION:
4072 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4073 if (cmd.button() == mouse_button::button1) {
4074 // only accept motions to places not deeper nested than the real anchor
4075 if (!bvcur.realAnchor().hasPart(cur)) {
4079 // select (additional) row
4081 row_type r = rowFromY(cur, cmd.y());
4082 cur.idx() = tabular.getLastCellInRow(r);
4083 // we need to reset the cursor's pit and pos now, as the old ones
4084 // may no longer be valid.
4087 bvcur.setCursor(cur);
4088 bvcur.selection(true);
4091 // select (additional) column
4093 col_type c = columnFromX(cur, cmd.x());
4094 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4095 // we need to reset the cursor's pit and pos now, as the old ones
4096 // may no longer be valid.
4099 bvcur.setCursor(cur);
4100 bvcur.selection(true);
4103 // only update if selection changes
4104 if (bvcur.idx() == cur.idx() &&
4105 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4106 cur.noScreenUpdate();
4107 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4108 bvcur.setCursor(cur);
4109 bvcur.selection(true);
4110 // if this is a multicell selection, we just set the cursor to
4111 // the beginning of the cell's text.
4112 if (bvcur.selIsMultiCell()) {
4113 bvcur.pit() = bvcur.lastpit();
4114 bvcur.pos() = bvcur.lastpos();
4119 case LFUN_MOUSE_RELEASE:
4124 case LFUN_CELL_BACKWARD:
4126 cur.selection(false);
4129 case LFUN_CELL_FORWARD:
4131 cur.selection(false);
4134 case LFUN_CHAR_FORWARD_SELECT:
4135 case LFUN_CHAR_FORWARD:
4136 case LFUN_CHAR_BACKWARD_SELECT:
4137 case LFUN_CHAR_BACKWARD:
4138 case LFUN_CHAR_RIGHT_SELECT:
4139 case LFUN_CHAR_RIGHT:
4140 case LFUN_CHAR_LEFT_SELECT:
4141 case LFUN_CHAR_LEFT:
4142 case LFUN_WORD_FORWARD:
4143 case LFUN_WORD_FORWARD_SELECT:
4144 case LFUN_WORD_BACKWARD:
4145 case LFUN_WORD_BACKWARD_SELECT:
4146 case LFUN_WORD_RIGHT:
4147 case LFUN_WORD_RIGHT_SELECT:
4148 case LFUN_WORD_LEFT:
4149 case LFUN_WORD_LEFT_SELECT: {
4150 // determine whether we move to next or previous cell, where to enter
4151 // the new cell from, and which command to "finish" (i.e., exit the
4154 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4155 FuncCode finish_lfun;
4157 if (act == LFUN_CHAR_FORWARD
4158 || act == LFUN_CHAR_FORWARD_SELECT
4159 || act == LFUN_WORD_FORWARD
4160 || act == LFUN_WORD_FORWARD_SELECT) {
4162 finish_lfun = LFUN_FINISHED_FORWARD;
4164 else if (act == LFUN_CHAR_BACKWARD
4165 || act == LFUN_CHAR_BACKWARD_SELECT
4166 || act == LFUN_WORD_BACKWARD
4167 || act == LFUN_WORD_BACKWARD_SELECT) {
4169 finish_lfun = LFUN_FINISHED_BACKWARD;
4171 // LEFT or RIGHT commands --- the interpretation will depend on the
4172 // table's direction.
4174 bool const right = act == LFUN_CHAR_RIGHT
4175 || act == LFUN_CHAR_RIGHT_SELECT
4176 || act == LFUN_WORD_RIGHT
4177 || act == LFUN_WORD_RIGHT_SELECT;
4178 next_cell = isRightToLeft(cur) != right;
4180 if (lyxrc.visual_cursor)
4181 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4183 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4186 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4187 || act == LFUN_CHAR_BACKWARD_SELECT
4188 || act == LFUN_CHAR_RIGHT_SELECT
4189 || act == LFUN_CHAR_LEFT_SELECT
4190 || act == LFUN_WORD_FORWARD_SELECT
4191 || act == LFUN_WORD_RIGHT_SELECT
4192 || act == LFUN_WORD_BACKWARD_SELECT
4193 || act == LFUN_WORD_LEFT_SELECT;
4195 // If we have a multicell selection or we're
4196 // not doing some LFUN_*_SELECT thing anyway...
4197 if (!cur.selIsMultiCell() || !select) {
4198 col_type const c = tabular.cellColumn(cur.idx());
4199 row_type const r = tabular.cellRow(cur.idx());
4200 // Are we trying to select the whole cell and is the whole cell
4201 // not yet selected?
4202 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4203 ((next_cell && cur.pit() == cur.lastpit()
4204 && cur.pos() == cur.lastpos())
4205 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4207 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4209 // ...try to dispatch to the cell's inset.
4210 cell(cur.idx())->dispatch(cur, cmd);
4212 // When we already have a selection we want to select the whole cell
4213 // before going to the next cell.
4214 if (select_whole && !empty_cell){
4215 getText(cur.idx())->selectAll(cur);
4217 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4221 // FIXME: When we support the selection of an empty cell, remove
4222 // the !empty_cell from this condition. For now we jump to the next
4223 // cell if the current cell is empty.
4224 if (cur.result().dispatched() && !empty_cell)
4228 // move to next/prev cell, as appropriate
4229 // note that we will always do this if we're selecting and we have
4230 // a multicell selection
4231 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4232 << " cell from: " << int(entry_from));
4234 moveNextCell(cur, entry_from);
4236 movePrevCell(cur, entry_from);
4237 // if we're exiting the table, call the appropriate FINISHED lfun
4238 if (sl == cur.top()) {
4239 cmd = FuncRequest(finish_lfun);
4244 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4249 case LFUN_DOWN_SELECT:
4251 if (!(cur.selection() && cur.selIsMultiCell()))
4252 cell(cur.idx())->dispatch(cur, cmd);
4254 cur.dispatched(); // override the cell's decision
4255 if (sl == cur.top()) {
4256 // if our Text didn't do anything to the cursor
4257 // then we try to put the cursor into the cell below
4258 // setting also the right targetX.
4259 cur.selHandle(act == LFUN_DOWN_SELECT);
4260 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4261 int const xtarget = cur.targetX();
4262 // WARNING: Once cur.idx() has been reset, the cursor is in
4263 // an inconsistent state until pos() has been set. Be careful
4264 // what you do with it!
4265 cur.idx() = tabular.cellBelow(cur.idx());
4267 TextMetrics const & tm =
4268 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4269 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4270 cur.setCurrentFont();
4273 if (sl == cur.top()) {
4274 // we trick it to go to forward after leaving the
4276 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4279 if (cur.selIsMultiCell()) {
4280 cur.pit() = cur.lastpit();
4281 cur.pos() = cur.lastpos();
4282 cur.setCurrentFont();
4283 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4286 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4289 case LFUN_UP_SELECT:
4291 if (!(cur.selection() && cur.selIsMultiCell()))
4292 cell(cur.idx())->dispatch(cur, cmd);
4293 cur.dispatched(); // override the cell's decision
4294 if (sl == cur.top()) {
4295 // if our Text didn't do anything to the cursor
4296 // then we try to put the cursor into the cell above
4297 // setting also the right targetX.
4298 cur.selHandle(act == LFUN_UP_SELECT);
4299 if (tabular.cellRow(cur.idx()) != 0) {
4300 int const xtarget = cur.targetX();
4301 // WARNING: Once cur.idx() has been reset, the cursor is in
4302 // an inconsistent state until pos() has been set. Be careful
4303 // what you do with it!
4304 cur.idx() = tabular.cellAbove(cur.idx());
4305 cur.pit() = cur.lastpit();
4306 Text const * text = cell(cur.idx())->getText(0);
4307 TextMetrics const & tm = cur.bv().textMetrics(text);
4308 ParagraphMetrics const & pm =
4309 tm.parMetrics(cur.lastpit());
4310 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4311 cur.setCurrentFont();
4314 if (sl == cur.top()) {
4315 cmd = FuncRequest(LFUN_UP);
4318 if (cur.selIsMultiCell()) {
4320 cur.pos() = cur.lastpos();
4321 cur.setCurrentFont();
4322 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4325 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4328 case LFUN_LAYOUT_TABULAR:
4329 cur.bv().showDialog("tabular");
4332 case LFUN_INSET_MODIFY:
4333 // we come from the dialog
4334 if (cmd.getArg(0) == "tabular")
4335 tabularFeatures(cur, cmd.getLongArg(1));
4340 case LFUN_TABULAR_FEATURE:
4341 tabularFeatures(cur, to_utf8(cmd.argument()));
4344 // insert file functions
4345 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4346 case LFUN_FILE_INSERT_PLAINTEXT:
4348 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4349 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4350 FileName(to_utf8(cmd.argument())));
4353 cur.recordUndoInset();
4354 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4355 // content has been replaced,
4356 // so cursor might be invalid
4357 cur.pos() = cur.lastpos();
4358 cur.pit() = cur.lastpit();
4359 bvcur.setCursor(cur);
4366 if (cur.selIsMultiCell()) {
4367 if (copySelection(cur)) {
4368 cur.recordUndoInset();
4372 cell(cur.idx())->dispatch(cur, cmd);
4375 case LFUN_SELF_INSERT:
4376 if (cur.selIsMultiCell()) {
4377 cur.recordUndoInset();
4379 BufferView * bv = &cur.bv();
4380 docstring::const_iterator cit = cmd.argument().begin();
4381 docstring::const_iterator const end = cmd.argument().end();
4382 for (; cit != end; ++cit)
4383 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4386 bv->bookmarkEditPosition();
4388 cell(cur.idx())->dispatch(cur, cmd);
4391 case LFUN_CHAR_DELETE_BACKWARD:
4392 case LFUN_CHAR_DELETE_FORWARD:
4393 if (cur.selIsMultiCell()) {
4394 cur.recordUndoInset();
4397 cell(cur.idx())->dispatch(cur, cmd);
4401 if (!cur.selection())
4403 if (cur.selIsMultiCell()) {
4407 cell(cur.idx())->dispatch(cur, cmd);
4410 case LFUN_CLIPBOARD_PASTE:
4411 case LFUN_PRIMARY_SELECTION_PASTE: {
4412 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4413 theClipboard().getAsText(Clipboard::PlainTextType) :
4414 theSelection().get();
4417 // pass to InsertPlaintextString, but
4418 // only if we have multi-cell content
4419 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4420 cur.recordUndoInset();
4421 if (insertPlaintextString(cur.bv(), clip, false)) {
4422 // content has been replaced,
4423 // so cursor might be invalid
4424 cur.pos() = cur.lastpos();
4425 cur.pit() = cur.lastpit();
4426 bvcur.setCursor(cur);
4430 // Let the cell handle normal text
4431 cell(cur.idx())->dispatch(cur, cmd);
4436 if (!tabularStackDirty()) {
4437 if (!cur.selIsMultiCell())
4438 cell(cur.idx())->dispatch(cur, cmd);
4441 if (theClipboard().isInternal()) {
4442 cur.recordUndoInset();
4443 pasteClipboard(cur);
4447 case LFUN_FONT_EMPH:
4448 case LFUN_FONT_BOLD:
4449 case LFUN_FONT_BOLDSYMBOL:
4450 case LFUN_FONT_ROMAN:
4451 case LFUN_FONT_NOUN:
4452 case LFUN_FONT_ITAL:
4453 case LFUN_FONT_FRAK:
4454 case LFUN_FONT_TYPEWRITER:
4455 case LFUN_FONT_SANS:
4456 case LFUN_TEXTSTYLE_APPLY:
4457 case LFUN_TEXTSTYLE_UPDATE:
4458 case LFUN_FONT_SIZE:
4459 case LFUN_FONT_UNDERLINE:
4460 case LFUN_FONT_STRIKEOUT:
4461 case LFUN_FONT_UNDERUNDERLINE:
4462 case LFUN_FONT_UNDERWAVE:
4464 case LFUN_PARAGRAPH_PARAMS_APPLY:
4465 case LFUN_PARAGRAPH_PARAMS:
4466 case LFUN_WORD_CAPITALIZE:
4467 case LFUN_WORD_UPCASE:
4468 case LFUN_WORD_LOWCASE:
4469 case LFUN_CHARS_TRANSPOSE:
4470 if (cur.selIsMultiCell()) {
4473 getSelection(cur, rs, re, cs, ce);
4474 Cursor tmpcur = cur;
4475 for (row_type r = rs; r <= re; ++r) {
4476 for (col_type c = cs; c <= ce; ++c) {
4477 // cursor follows cell:
4478 tmpcur.idx() = tabular.cellIndex(r, c);
4479 // select this cell only:
4482 tmpcur.resetAnchor();
4483 tmpcur.pit() = tmpcur.lastpit();
4484 tmpcur.pos() = tmpcur.top().lastpos();
4485 tmpcur.setCursor(tmpcur);
4486 tmpcur.setSelection();
4487 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4492 cell(cur.idx())->dispatch(cur, cmd);
4496 case LFUN_INSET_SETTINGS:
4497 // relay this lfun to Inset, not to the cell.
4498 Inset::doDispatch(cur, cmd);
4502 // we try to handle this event in the insets dispatch function.
4503 cell(cur.idx())->dispatch(cur, cmd);
4509 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4510 string const & argument, FuncStatus & status) const
4513 int action = Tabular::LAST_ACTION;
4515 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4516 if (tabularFeature[i].feature == s) {
4517 action = tabularFeature[i].action;
4521 if (action == Tabular::LAST_ACTION) {
4523 status.setUnknown(true);
4527 row_type sel_row_start = 0;
4528 row_type sel_row_end = 0;
4529 col_type sel_col_start = 0;
4530 col_type sel_col_end = 0;
4531 Tabular::ltType dummyltt;
4534 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4537 case Tabular::SET_PWIDTH:
4538 case Tabular::SET_MPWIDTH:
4539 case Tabular::SET_SPECIAL_COLUMN:
4540 case Tabular::SET_SPECIAL_MULTICOLUMN:
4541 case Tabular::APPEND_ROW:
4542 case Tabular::APPEND_COLUMN:
4543 case Tabular::DELETE_ROW:
4544 case Tabular::DELETE_COLUMN:
4545 case Tabular::COPY_ROW:
4546 case Tabular::COPY_COLUMN:
4547 case Tabular::SET_TOP_SPACE:
4548 case Tabular::SET_BOTTOM_SPACE:
4549 case Tabular::SET_INTERLINE_SPACE:
4553 case Tabular::SET_TABULAR_WIDTH:
4554 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4555 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4558 case Tabular::MOVE_COLUMN_RIGHT:
4559 case Tabular::MOVE_COLUMN_LEFT:
4560 case Tabular::MOVE_ROW_DOWN:
4561 case Tabular::MOVE_ROW_UP: {
4562 if (cur.selection()) {
4563 status.message(_("Selections not supported."));
4564 status.setEnabled(false);
4568 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4569 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4570 (action == Tabular::MOVE_COLUMN_LEFT &&
4571 tabular.cellColumn(cur.idx()) == 0) ||
4572 (action == Tabular::MOVE_ROW_DOWN &&
4573 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4574 (action == Tabular::MOVE_ROW_UP &&
4575 tabular.cellRow(cur.idx()) == 0)) {
4576 status.setEnabled(false);
4580 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4581 action == Tabular::MOVE_COLUMN_LEFT) {
4582 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4583 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4584 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4585 status.message(_("Multi-column in current or"
4586 " destination column."));
4587 status.setEnabled(false);
4592 if (action == Tabular::MOVE_ROW_DOWN ||
4593 action == Tabular::MOVE_ROW_UP) {
4594 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4595 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4596 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4597 status.message(_("Multi-row in current or"
4598 " destination row."));
4599 status.setEnabled(false);
4604 status.setEnabled(true);
4608 case Tabular::SET_DECIMAL_POINT:
4610 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4613 case Tabular::SET_MULTICOLUMN:
4614 case Tabular::UNSET_MULTICOLUMN:
4615 case Tabular::MULTICOLUMN:
4616 // If a row is set as longtable caption, it must not be allowed
4617 // to unset that this row is a multicolumn.
4618 status.setEnabled(sel_row_start == sel_row_end
4619 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4620 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4623 case Tabular::SET_MULTIROW:
4624 case Tabular::UNSET_MULTIROW:
4625 case Tabular::MULTIROW:
4626 // If a row is set as longtable caption, it must not be allowed
4627 // to unset that this row is a multirow.
4628 status.setEnabled(sel_col_start == sel_col_end
4629 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4630 status.setOnOff(tabular.isMultiRow(cur.idx()));
4633 case Tabular::SET_ALL_LINES:
4634 case Tabular::UNSET_ALL_LINES:
4635 case Tabular::SET_BORDER_LINES:
4636 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4639 case Tabular::SET_LINE_TOP:
4640 case Tabular::SET_LINE_BOTTOM:
4641 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4644 case Tabular::SET_LINE_LEFT:
4645 case Tabular::SET_LINE_RIGHT:
4646 status.setEnabled(!tabular.use_booktabs
4647 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4650 case Tabular::TOGGLE_LINE_TOP:
4651 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4652 status.setOnOff(tabular.topLine(cur.idx()));
4655 case Tabular::TOGGLE_LINE_BOTTOM:
4656 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4657 status.setOnOff(tabular.bottomLine(cur.idx()));
4660 case Tabular::TOGGLE_LINE_LEFT:
4661 status.setEnabled(!tabular.use_booktabs
4662 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4663 status.setOnOff(tabular.leftLine(cur.idx()));
4666 case Tabular::TOGGLE_LINE_RIGHT:
4667 status.setEnabled(!tabular.use_booktabs
4668 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4669 status.setOnOff(tabular.rightLine(cur.idx()));
4672 // multirow cells only inherit the alignment of the column if the column has
4673 // no width, otherwise they are left-aligned
4674 // therefore allow always left but right and center only if there is no width
4675 case Tabular::M_ALIGN_LEFT:
4677 case Tabular::ALIGN_LEFT:
4678 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4681 case Tabular::M_ALIGN_RIGHT:
4683 case Tabular::ALIGN_RIGHT:
4684 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4685 && !tabular.getPWidth(cur.idx()).zero()));
4686 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4689 case Tabular::M_ALIGN_CENTER:
4691 case Tabular::ALIGN_CENTER:
4692 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4693 && !tabular.getPWidth(cur.idx()).zero()));
4694 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4697 case Tabular::ALIGN_BLOCK:
4698 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4699 && !tabular.isMultiRow(cur.idx()));
4700 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4703 case Tabular::ALIGN_DECIMAL:
4704 status.setEnabled(!tabular.isMultiRow(cur.idx())
4705 && !tabular.isMultiColumn(cur.idx()));
4706 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4709 case Tabular::M_VALIGN_TOP:
4711 case Tabular::VALIGN_TOP:
4712 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4713 && !tabular.isMultiRow(cur.idx()));
4715 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4718 case Tabular::M_VALIGN_BOTTOM:
4720 case Tabular::VALIGN_BOTTOM:
4721 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4722 && !tabular.isMultiRow(cur.idx()));
4724 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4727 case Tabular::M_VALIGN_MIDDLE:
4729 case Tabular::VALIGN_MIDDLE:
4730 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4731 && !tabular.isMultiRow(cur.idx()));
4733 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4736 case Tabular::SET_LONGTABULAR:
4737 case Tabular::TOGGLE_LONGTABULAR:
4738 // setting as longtable is not allowed when table is inside a float
4739 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4740 || cur.innerInsetOfType(WRAP_CODE) != 0)
4741 status.setEnabled(false);
4743 status.setEnabled(true);
4744 status.setOnOff(tabular.is_long_tabular);
4747 case Tabular::UNSET_LONGTABULAR:
4748 status.setOnOff(!tabular.is_long_tabular);
4751 case Tabular::TOGGLE_ROTATE_TABULAR:
4752 case Tabular::SET_ROTATE_TABULAR:
4753 status.setOnOff(tabular.rotate != 0);
4756 case Tabular::TABULAR_VALIGN_TOP:
4757 status.setEnabled(tabular.tabular_width.zero());
4758 status.setOnOff(tabular.tabular_valignment
4759 == Tabular::LYX_VALIGN_TOP);
4761 case Tabular::TABULAR_VALIGN_MIDDLE:
4762 status.setEnabled(tabular.tabular_width.zero());
4763 status.setOnOff(tabular.tabular_valignment
4764 == Tabular::LYX_VALIGN_MIDDLE);
4766 case Tabular::TABULAR_VALIGN_BOTTOM:
4767 status.setEnabled(tabular.tabular_width.zero());
4768 status.setOnOff(tabular.tabular_valignment
4769 == Tabular::LYX_VALIGN_BOTTOM);
4772 case Tabular::LONGTABULAR_ALIGN_LEFT:
4773 status.setOnOff(tabular.longtabular_alignment
4774 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4776 case Tabular::LONGTABULAR_ALIGN_CENTER:
4777 status.setOnOff(tabular.longtabular_alignment
4778 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4780 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4781 status.setOnOff(tabular.longtabular_alignment
4782 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4785 case Tabular::UNSET_ROTATE_TABULAR:
4786 status.setOnOff(tabular.rotate == 0);
4789 case Tabular::TOGGLE_ROTATE_CELL:
4790 case Tabular::SET_ROTATE_CELL:
4791 status.setOnOff(!oneCellHasRotationState(false,
4792 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4795 case Tabular::UNSET_ROTATE_CELL:
4796 status.setOnOff(!oneCellHasRotationState(true,
4797 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4800 case Tabular::SET_USEBOX:
4801 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4804 // every row can only be one thing:
4805 // either a footer or header
4806 case Tabular::SET_LTFIRSTHEAD:
4807 status.setEnabled(sel_row_start == sel_row_end);
4808 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4811 case Tabular::UNSET_LTFIRSTHEAD:
4812 status.setEnabled(sel_row_start == sel_row_end);
4813 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4816 case Tabular::SET_LTHEAD:
4817 status.setEnabled(sel_row_start == sel_row_end);
4818 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4821 case Tabular::UNSET_LTHEAD:
4822 status.setEnabled(sel_row_start == sel_row_end);
4823 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4826 case Tabular::SET_LTFOOT:
4827 status.setEnabled(sel_row_start == sel_row_end);
4828 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4831 case Tabular::UNSET_LTFOOT:
4832 status.setEnabled(sel_row_start == sel_row_end);
4833 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4836 case Tabular::SET_LTLASTFOOT:
4837 status.setEnabled(sel_row_start == sel_row_end);
4838 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4841 case Tabular::UNSET_LTLASTFOOT:
4842 status.setEnabled(sel_row_start == sel_row_end);
4843 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4846 case Tabular::SET_LTNEWPAGE:
4847 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4849 case Tabular::UNSET_LTNEWPAGE:
4850 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4853 // only one row in head/firsthead/foot/lasthead can be the caption
4854 // and a multirow cannot be set as caption
4855 case Tabular::SET_LTCAPTION:
4856 status.setEnabled(sel_row_start == sel_row_end
4857 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4858 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4859 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4860 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4861 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4862 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4863 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4864 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4865 && !tabular.isMultiRow(sel_row_start));
4866 status.setOnOff(tabular.ltCaption(sel_row_start));
4869 case Tabular::UNSET_LTCAPTION:
4870 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4873 case Tabular::TOGGLE_LTCAPTION:
4874 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4875 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4876 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4877 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4878 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4879 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4880 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4881 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4882 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4883 status.setOnOff(tabular.ltCaption(sel_row_start));
4886 case Tabular::TOGGLE_BOOKTABS:
4887 case Tabular::SET_BOOKTABS:
4888 status.setOnOff(tabular.use_booktabs);
4891 case Tabular::UNSET_BOOKTABS:
4892 status.setOnOff(!tabular.use_booktabs);
4897 status.setEnabled(false);
4904 // function sets an object as defined in FuncStatus.h:
4905 // states OK, Unknown, Disabled, On, Off.
4906 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4907 FuncStatus & status) const
4909 switch (cmd.action()) {
4910 case LFUN_INSET_MODIFY:
4911 if (cmd.getArg(0) != "tabular")
4913 if (cmd.getArg(1) == "for-dialog") {
4914 // The dialog is asking the status of a command
4915 if (&cur.inset() != this)
4917 string action = cmd.getArg(2);
4918 string arg = cmd.getLongArg(3);
4919 return getFeatureStatus(cur, action, arg, status);
4921 // We always enable the lfun if it is coming from the dialog
4922 // because the dialog makes sure all the settings are valid,
4923 // even though the first argument might not be valid now.
4924 status.setEnabled(true);
4928 case LFUN_TABULAR_FEATURE: {
4929 if (&cur.inset() != this)
4931 string action = cmd.getArg(0);
4932 string arg = cmd.getLongArg(1);
4933 return getFeatureStatus(cur, action, arg, status);
4936 case LFUN_CAPTION_INSERT: {
4937 // caption is only allowed in caption cell of longtable
4938 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4939 status.setEnabled(false);
4942 // only standard caption is allowed
4943 string arg = cmd.getArg(0);
4944 if (!arg.empty() && arg != "Standard") {
4945 status.setEnabled(false);
4948 // check if there is already a caption
4949 bool have_caption = false;
4950 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
4951 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4952 ParagraphList::const_iterator pend = itc.paragraphs().end();
4953 for (; pit != pend; ++pit) {
4954 InsetList::const_iterator it = pit->insetList().begin();
4955 InsetList::const_iterator end = pit->insetList().end();
4956 for (; it != end; ++it) {
4957 if (it->inset->lyxCode() == CAPTION_CODE) {
4958 have_caption = true;
4963 status.setEnabled(!have_caption);
4967 // These are only enabled inside tabular
4968 case LFUN_CELL_BACKWARD:
4969 case LFUN_CELL_FORWARD:
4970 status.setEnabled(true);
4973 // disable these with multiple cells selected
4974 case LFUN_INSET_INSERT:
4975 case LFUN_TABULAR_INSERT:
4976 case LFUN_FLEX_INSERT:
4977 case LFUN_FLOAT_INSERT:
4978 case LFUN_FLOAT_WIDE_INSERT:
4979 case LFUN_FOOTNOTE_INSERT:
4980 case LFUN_MARGINALNOTE_INSERT:
4981 case LFUN_MATH_INSERT:
4982 case LFUN_MATH_MODE:
4983 case LFUN_MATH_MUTATE:
4984 case LFUN_MATH_DISPLAY:
4985 case LFUN_NOTE_INSERT:
4986 case LFUN_ARGUMENT_INSERT:
4987 case LFUN_BOX_INSERT:
4988 case LFUN_BRANCH_INSERT:
4989 case LFUN_PHANTOM_INSERT:
4990 case LFUN_WRAP_INSERT:
4991 case LFUN_PREVIEW_INSERT:
4992 case LFUN_ERT_INSERT: {
4993 if (cur.selIsMultiCell()) {
4994 status.setEnabled(false);
4997 return cell(cur.idx())->getStatus(cur, cmd, status);
5000 // disable in non-fixed-width cells
5001 case LFUN_PARAGRAPH_BREAK:
5002 // multirow does not allow paragraph breaks
5003 if (tabular.isMultiRow(cur.idx())) {
5004 status.setEnabled(false);
5008 case LFUN_NEWLINE_INSERT: {
5009 if (tabular.getPWidth(cur.idx()).zero()) {
5010 status.setEnabled(false);
5013 return cell(cur.idx())->getStatus(cur, cmd, status);
5016 case LFUN_NEWPAGE_INSERT:
5017 status.setEnabled(false);
5021 if (tabularStackDirty() && theClipboard().isInternal()) {
5022 if (cur.selIsMultiCell()) {
5025 getSelection(cur, rs, re, cs, ce);
5026 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5027 && paste_tabular->nrows() == re - rs + 1)
5028 status.setEnabled(true);
5030 status.setEnabled(false);
5031 status.message(_("Selection size should match clipboard content."));
5034 status.setEnabled(true);
5037 return cell(cur.idx())->getStatus(cur, cmd, status);
5039 case LFUN_INSET_SETTINGS:
5040 // relay this lfun to Inset, not to the cell.
5041 return Inset::getStatus(cur, cmd, status);
5044 // we try to handle this event in the insets dispatch function.
5045 return cell(cur.idx())->getStatus(cur, cmd, status);
5051 Inset::DisplayType InsetTabular::display() const
5053 if (tabular.is_long_tabular) {
5054 switch (tabular.longtabular_alignment) {
5055 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5057 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5059 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5069 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5071 tabular.latex(os, runparams);
5075 int InsetTabular::plaintext(odocstringstream & os,
5076 OutputParams const & runparams, size_t max_length) const
5078 os << '\n'; // output table on a new line
5079 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5080 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5081 return PLAINTEXT_NEWLINE;
5085 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5090 // FIXME: Why not pass a proper DocIterator here?
5092 // if the table is inside a float it doesn't need the informaltable
5093 // wrapper. Search for it.
5094 for (master = owner(); master; master = master->owner())
5095 if (master->lyxCode() == FLOAT_CODE)
5100 os << "<informaltable>";
5103 ret += tabular.docbook(os, runparams);
5105 os << "</informaltable>";
5112 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5114 return tabular.xhtml(xs, rp);
5118 void InsetTabular::validate(LaTeXFeatures & features) const
5120 tabular.validate(features);
5121 features.useInsetLayout(getLayout());
5125 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5127 return tabular.cellInset(idx);
5131 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5133 return tabular.cellInset(idx);
5137 void InsetTabular::cursorPos(BufferView const & bv,
5138 CursorSlice const & sl, bool boundary, int & x, int & y) const
5140 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5142 // y offset correction
5143 y += cellYPos(sl.idx());
5144 y += tabular.textVOffset(sl.idx());
5145 y += tabular.offsetVAlignment();
5147 // x offset correction
5148 x += cellXPos(sl.idx());
5149 x += tabular.textHOffset(sl.idx());
5150 x += ADD_TO_TABULAR_WIDTH;
5154 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5158 Inset const & inset = *tabular.cellInset(cell);
5159 Point o = bv.coordCache().getInsets().xy(&inset);
5160 int const xbeg = o.x_ - tabular.textHOffset(cell);
5161 int const xend = xbeg + tabular.cellWidth(cell);
5162 row_type const row = tabular.cellRow(cell);
5163 int const ybeg = o.y_ - tabular.rowAscent(row)
5164 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5165 int const yend = ybeg + tabular.cellHeight(cell);
5177 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5178 // << " ybeg=" << ybeg << " yend=" << yend
5179 // << " xx=" << xx << " yy=" << yy
5180 // << " dist=" << xx + yy << endl;
5185 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5187 //lyxerr << "InsetTabular::editXY: " << this << endl;
5188 cur.selection(false);
5190 cur.idx() = getNearestCell(cur.bv(), x, y);
5191 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5195 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5197 cur.idx() = getNearestCell(cur.bv(), x, y);
5198 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5202 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5204 idx_type idx_min = 0;
5205 int dist_min = numeric_limits<int>::max();
5206 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5207 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5208 int const d = dist(bv, i, x, y);
5219 int InsetTabular::cellYPos(idx_type const cell) const
5221 row_type row = tabular.cellRow(cell);
5223 for (row_type r = 0; r < row; ++r)
5224 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5225 + tabular.interRowSpace(r + 1);
5230 int InsetTabular::cellXPos(idx_type const cell) const
5232 col_type col = tabular.cellColumn(cell);
5234 for (col_type c = 0; c < col; ++c)
5235 lx += tabular.column_info[c].width;
5240 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5242 row_type const row = tabular.cellRow(cur.idx());
5243 col_type const col = tabular.cellColumn(cur.idx());
5245 if (isRightToLeft(cur)) {
5246 if (tabular.cellColumn(cur.idx()) == 0) {
5247 if (row == tabular.nrows() - 1)
5249 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5254 cur.idx() = tabular.getLastCellInRow(row - 1);
5256 cur.idx() = tabular.cellIndex(row, col - 1);
5259 if (tabular.isLastCell(cur.idx()))
5261 if (cur.idx() == tabular.getLastCellInRow(row))
5262 cur.idx() = tabular.cellIndex(row + 1, 0);
5264 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5265 cur.idx() = tabular.cellIndex(row, colnextcell);
5269 cur.boundary(false);
5271 if (cur.selIsMultiCell()) {
5272 cur.pit() = cur.lastpit();
5273 cur.pos() = cur.lastpos();
5280 // in visual mode, place cursor at extreme left or right
5282 switch(entry_from) {
5284 case ENTRY_DIRECTION_RIGHT:
5285 cur.posVisToRowExtremity(false /* !left */);
5287 case ENTRY_DIRECTION_LEFT:
5288 cur.posVisToRowExtremity(true /* left */);
5290 case ENTRY_DIRECTION_IGNORE:
5291 // nothing to do in this case
5295 cur.setCurrentFont();
5299 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5301 row_type const row = tabular.cellRow(cur.idx());
5302 col_type const col = tabular.cellColumn(cur.idx());
5304 if (isRightToLeft(cur)) {
5305 if (cur.idx() == tabular.getLastCellInRow(row)) {
5308 cur.idx() = tabular.getFirstCellInRow(row);
5309 cur.idx() = tabular.cellAbove(cur.idx());
5311 if (tabular.isLastCell(cur.idx()))
5313 if (cur.idx() == tabular.getLastCellInRow(row))
5314 cur.idx() = tabular.cellIndex(row + 1, 0);
5316 cur.idx() = tabular.cellIndex(row, col + 1);
5319 if (cur.idx() == 0) // first cell
5322 cur.idx() = tabular.getLastCellInRow(row - 1);
5324 cur.idx() = tabular.cellIndex(row, col - 1);
5327 if (cur.selIsMultiCell()) {
5328 cur.pit() = cur.lastpit();
5329 cur.pos() = cur.lastpos();
5333 cur.pit() = cur.lastpit();
5334 cur.pos() = cur.lastpos();
5336 // in visual mode, place cursor at extreme left or right
5338 switch(entry_from) {
5340 case ENTRY_DIRECTION_RIGHT:
5341 cur.posVisToRowExtremity(false /* !left */);
5343 case ENTRY_DIRECTION_LEFT:
5344 cur.posVisToRowExtremity(true /* left */);
5346 case ENTRY_DIRECTION_IGNORE:
5347 // nothing to do in this case
5351 cur.setCurrentFont();
5355 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5357 cur.recordUndoInset(this);
5359 istringstream is(argument);
5362 size_t safe_guard = 0;
5367 if (safe_guard > 1000) {
5368 LYXERR0("parameter max count reached!");
5372 Tabular::Feature action = Tabular::LAST_ACTION;
5375 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5376 if (s != tabularFeature[i].feature)
5378 action = tabularFeature[i].action;
5381 if (action == Tabular::LAST_ACTION) {
5382 LYXERR0("Feature not found " << s);
5386 if (tabularFeature[i].need_value)
5388 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5389 tabularFeatures(cur, action, val);
5394 static void checkLongtableSpecial(Tabular::ltType & ltt,
5395 string const & special, bool & flag)
5397 if (special == "dl_above") {
5400 } else if (special == "dl_below") {
5401 ltt.bottomDL = flag;
5403 } else if (special == "empty") {
5413 bool InsetTabular::oneCellHasRotationState(bool rotated,
5414 row_type row_start, row_type row_end,
5415 col_type col_start, col_type col_end) const
5417 for (row_type r = row_start; r <= row_end; ++r)
5418 for (col_type c = col_start; c <= col_end; ++c)
5420 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5423 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5430 void InsetTabular::tabularFeatures(Cursor & cur,
5431 Tabular::Feature feature, string const & value)
5433 col_type sel_col_start;
5434 col_type sel_col_end;
5435 row_type sel_row_start;
5436 row_type sel_row_end;
5437 bool setLines = false;
5438 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5439 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5443 case Tabular::M_ALIGN_LEFT:
5444 case Tabular::ALIGN_LEFT:
5445 setAlign = LYX_ALIGN_LEFT;
5448 case Tabular::M_ALIGN_RIGHT:
5449 case Tabular::ALIGN_RIGHT:
5450 setAlign = LYX_ALIGN_RIGHT;
5453 case Tabular::M_ALIGN_CENTER:
5454 case Tabular::ALIGN_CENTER:
5455 setAlign = LYX_ALIGN_CENTER;
5458 case Tabular::ALIGN_BLOCK:
5459 setAlign = LYX_ALIGN_BLOCK;
5462 case Tabular::ALIGN_DECIMAL:
5463 setAlign = LYX_ALIGN_DECIMAL;
5466 case Tabular::M_VALIGN_TOP:
5467 case Tabular::VALIGN_TOP:
5468 setVAlign = Tabular::LYX_VALIGN_TOP;
5471 case Tabular::M_VALIGN_BOTTOM:
5472 case Tabular::VALIGN_BOTTOM:
5473 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5476 case Tabular::M_VALIGN_MIDDLE:
5477 case Tabular::VALIGN_MIDDLE:
5478 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5485 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5486 row_type const row = tabular.cellRow(cur.idx());
5487 col_type const column = tabular.cellColumn(cur.idx());
5489 Tabular::ltType ltt;
5493 case Tabular::SET_TABULAR_WIDTH:
5494 tabular.setTabularWidth(Length(value));
5497 case Tabular::SET_PWIDTH: {
5498 Length const len(value);
5499 tabular.setColumnPWidth(cur, cur.idx(), len);
5501 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5502 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5506 case Tabular::SET_MPWIDTH:
5507 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5510 case Tabular::SET_MROFFSET:
5511 tabular.setMROffset(cur, cur.idx(), Length(value));
5514 case Tabular::SET_SPECIAL_COLUMN:
5515 case Tabular::SET_SPECIAL_MULTICOLUMN:
5516 if (value == "none")
5517 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5519 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5522 case Tabular::APPEND_ROW:
5523 // append the row into the tabular
5524 tabular.appendRow(row);
5527 case Tabular::APPEND_COLUMN:
5528 // append the column into the tabular
5529 tabular.appendColumn(column);
5530 cur.idx() = tabular.cellIndex(row, column);
5533 case Tabular::DELETE_ROW:
5534 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5535 for (col_type c = 0; c < tabular.ncols(); c++)
5536 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5537 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5540 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5541 tabular.deleteRow(sel_row_start);
5542 if (sel_row_start >= tabular.nrows())
5544 cur.idx() = tabular.cellIndex(sel_row_start, column);
5547 cur.selection(false);
5550 case Tabular::DELETE_COLUMN:
5551 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5552 for (row_type r = 0; r < tabular.nrows(); r++)
5553 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5554 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5557 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5558 for (row_type r = 0; r < tabular.nrows(); r++)
5559 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5560 tabular.leftLine(tabular.cellIndex(r, 0)));
5563 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5564 tabular.deleteColumn(sel_col_start);
5565 if (sel_col_start >= tabular.ncols())
5567 cur.idx() = tabular.cellIndex(row, sel_col_start);
5570 cur.selection(false);
5573 case Tabular::COPY_ROW:
5574 tabular.copyRow(row);
5577 case Tabular::COPY_COLUMN:
5578 tabular.copyColumn(column);
5579 cur.idx() = tabular.cellIndex(row, column);
5582 case Tabular::MOVE_COLUMN_RIGHT:
5583 tabular.moveColumn(column, Tabular::RIGHT);
5584 cur.idx() = tabular.cellIndex(row, column + 1);
5587 case Tabular::MOVE_COLUMN_LEFT:
5588 tabular.moveColumn(column, Tabular::LEFT);
5589 cur.idx() = tabular.cellIndex(row, column - 1);
5592 case Tabular::MOVE_ROW_DOWN:
5593 tabular.moveRow(row, Tabular::DOWN);
5594 cur.idx() = tabular.cellIndex(row + 1, column);
5597 case Tabular::MOVE_ROW_UP:
5598 tabular.moveRow(row, Tabular::UP);
5599 cur.idx() = tabular.cellIndex(row - 1, column);
5602 case Tabular::SET_LINE_TOP:
5603 case Tabular::TOGGLE_LINE_TOP: {
5604 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5605 ? (value == "true") : !tabular.topLine(cur.idx());
5606 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5607 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5608 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5612 case Tabular::SET_LINE_BOTTOM:
5613 case Tabular::TOGGLE_LINE_BOTTOM: {
5614 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5615 ? (value == "true") : !tabular.bottomLine(cur.idx());
5616 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5617 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5618 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5622 case Tabular::SET_LINE_LEFT:
5623 case Tabular::TOGGLE_LINE_LEFT: {
5624 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5625 ? (value == "true") : !tabular.leftLine(cur.idx());
5626 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5627 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5628 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5632 case Tabular::SET_LINE_RIGHT:
5633 case Tabular::TOGGLE_LINE_RIGHT: {
5634 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5635 ? (value == "true") : !tabular.rightLine(cur.idx());
5636 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5637 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5638 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5642 case Tabular::M_ALIGN_LEFT:
5643 case Tabular::M_ALIGN_RIGHT:
5644 case Tabular::M_ALIGN_CENTER:
5645 case Tabular::ALIGN_LEFT:
5646 case Tabular::ALIGN_RIGHT:
5647 case Tabular::ALIGN_CENTER:
5648 case Tabular::ALIGN_BLOCK:
5649 case Tabular::ALIGN_DECIMAL:
5650 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5651 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5652 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5653 !tabular.getPWidth(c).zero());
5656 case Tabular::M_VALIGN_TOP:
5657 case Tabular::M_VALIGN_BOTTOM:
5658 case Tabular::M_VALIGN_MIDDLE:
5660 case Tabular::VALIGN_TOP:
5661 case Tabular::VALIGN_BOTTOM:
5662 case Tabular::VALIGN_MIDDLE:
5663 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5664 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5665 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5668 case Tabular::SET_MULTICOLUMN: {
5669 if (!cur.selection()) {
5670 // just multicol for one single cell
5671 // check whether we are completely in a multicol
5672 if (!tabular.isMultiColumn(cur.idx()))
5673 tabular.setMultiColumn(cur.idx(), 1,
5674 tabular.rightLine(cur.idx()));
5677 // we have a selection so this means we just add all these
5678 // cells to form a multicolumn cell
5679 idx_type const s_start = cur.selBegin().idx();
5680 row_type const col_start = tabular.cellColumn(s_start);
5681 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5682 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5683 tabular.rightLine(cur.selEnd().idx()));
5686 cur.selection(false);
5690 case Tabular::UNSET_MULTICOLUMN: {
5691 if (!cur.selection()) {
5692 if (tabular.isMultiColumn(cur.idx()))
5693 tabular.unsetMultiColumn(cur.idx());
5698 case Tabular::MULTICOLUMN: {
5699 if (!cur.selection()) {
5700 if (tabular.isMultiColumn(cur.idx()))
5701 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5703 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5707 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5708 row_type const r = sel_row_start;
5709 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5710 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5713 // If the selection contains at least one singlecol cell
5714 // or multiple multicol cells,
5715 // we assume the user will merge is to a single multicol
5717 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5719 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5723 case Tabular::SET_MULTIROW: {
5724 if (!cur.selection()) {
5725 // just multirow for one single cell
5726 // check whether we are completely in a multirow
5727 if (!tabular.isMultiRow(cur.idx()))
5728 tabular.setMultiRow(cur.idx(), 1,
5729 tabular.bottomLine(cur.idx()),
5730 tabular.getAlignment(cur.idx()));
5733 // we have a selection so this means we just add all this
5734 // cells to form a multirow cell
5735 idx_type const s_start = cur.selBegin().idx();
5736 row_type const row_start = tabular.cellRow(s_start);
5737 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5738 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5739 tabular.bottomLine(cur.selEnd().idx()),
5740 tabular.getAlignment(cur.selEnd().idx()));
5743 cur.selection(false);
5747 case Tabular::UNSET_MULTIROW: {
5748 if (!cur.selection()) {
5749 if (tabular.isMultiRow(cur.idx()))
5750 tabular.unsetMultiRow(cur.idx());
5755 case Tabular::MULTIROW: {
5756 if (!cur.selection()) {
5757 if (tabular.isMultiRow(cur.idx()))
5758 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5760 tabularFeatures(cur, Tabular::SET_MULTIROW);
5764 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5765 col_type const c = sel_col_start;
5766 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5767 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5770 // If the selection contains at least one singlerow cell
5771 // or multiple multirow cells,
5772 // we assume the user will merge is to a single multirow
5774 tabularFeatures(cur, Tabular::SET_MULTIROW);
5776 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5780 case Tabular::SET_ALL_LINES:
5782 case Tabular::UNSET_ALL_LINES:
5783 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5784 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5785 idx_type const cell = tabular.cellIndex(r, c);
5786 tabular.setTopLine(cell, setLines);
5787 tabular.setBottomLine(cell, setLines);
5788 tabular.setRightLine(cell, setLines);
5789 tabular.setLeftLine(cell, setLines);
5793 case Tabular::SET_BORDER_LINES:
5794 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5795 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5796 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5798 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5799 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5800 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5804 case Tabular::TOGGLE_LONGTABULAR:
5805 if (tabular.is_long_tabular)
5806 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5808 tabular.is_long_tabular = true;
5811 case Tabular::SET_LONGTABULAR:
5812 tabular.is_long_tabular = true;
5815 case Tabular::UNSET_LONGTABULAR:
5816 for (row_type r = 0; r < tabular.nrows(); ++r) {
5817 if (tabular.ltCaption(r)) {
5818 cur.idx() = tabular.cellIndex(r, 0);
5821 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5824 tabular.is_long_tabular = false;
5827 case Tabular::SET_ROTATE_TABULAR:
5828 tabular.rotate = convert<int>(value);
5831 case Tabular::UNSET_ROTATE_TABULAR:
5835 case Tabular::TOGGLE_ROTATE_TABULAR:
5836 // when pressing the rotate button we default to 90° rotation
5837 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5840 case Tabular::TABULAR_VALIGN_TOP:
5841 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5844 case Tabular::TABULAR_VALIGN_MIDDLE:
5845 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5848 case Tabular::TABULAR_VALIGN_BOTTOM:
5849 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5852 case Tabular::LONGTABULAR_ALIGN_LEFT:
5853 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5856 case Tabular::LONGTABULAR_ALIGN_CENTER:
5857 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5860 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5861 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5866 case Tabular::SET_ROTATE_CELL:
5867 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5868 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5869 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5872 case Tabular::UNSET_ROTATE_CELL:
5873 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5874 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5875 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5878 case Tabular::TOGGLE_ROTATE_CELL:
5880 bool oneNotRotated = oneCellHasRotationState(false,
5881 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5883 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5884 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5885 // when pressing the rotate cell button we default to 90° rotation
5887 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5889 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5894 case Tabular::SET_USEBOX: {
5895 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5896 if (val == tabular.getUsebox(cur.idx()))
5897 val = Tabular::BOX_NONE;
5898 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5899 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5900 tabular.setUsebox(tabular.cellIndex(r, c), val);
5904 case Tabular::UNSET_LTFIRSTHEAD:
5906 case Tabular::SET_LTFIRSTHEAD:
5907 tabular.getRowOfLTFirstHead(row, ltt);
5908 checkLongtableSpecial(ltt, value, flag);
5909 tabular.setLTHead(row, flag, ltt, true);
5912 case Tabular::UNSET_LTHEAD:
5914 case Tabular::SET_LTHEAD:
5915 tabular.getRowOfLTHead(row, ltt);
5916 checkLongtableSpecial(ltt, value, flag);
5917 tabular.setLTHead(row, flag, ltt, false);
5920 case Tabular::UNSET_LTFOOT:
5922 case Tabular::SET_LTFOOT:
5923 tabular.getRowOfLTFoot(row, ltt);
5924 checkLongtableSpecial(ltt, value, flag);
5925 tabular.setLTFoot(row, flag, ltt, false);
5928 case Tabular::UNSET_LTLASTFOOT:
5930 case Tabular::SET_LTLASTFOOT:
5931 tabular.getRowOfLTLastFoot(row, ltt);
5932 checkLongtableSpecial(ltt, value, flag);
5933 tabular.setLTFoot(row, flag, ltt, true);
5936 case Tabular::UNSET_LTNEWPAGE:
5938 case Tabular::SET_LTNEWPAGE:
5939 tabular.setLTNewPage(row, flag);
5942 case Tabular::SET_LTCAPTION: {
5943 if (tabular.ltCaption(row))
5945 cur.idx() = tabular.setLTCaption(row, true);
5948 cur.selection(false);
5949 // If a row is set as caption, then also insert
5950 // a caption. Otherwise the LaTeX output is broken.
5951 // Select cell if it is non-empty
5952 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5953 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5954 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5958 case Tabular::UNSET_LTCAPTION: {
5959 if (!tabular.ltCaption(row))
5961 cur.idx() = tabular.setLTCaption(row, false);
5964 cur.selection(false);
5965 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5966 if (lyx::getStatus(fr).enabled())
5971 case Tabular::TOGGLE_LTCAPTION: {
5972 if (tabular.ltCaption(row))
5973 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5975 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5979 case Tabular::TOGGLE_BOOKTABS:
5980 tabular.use_booktabs = !tabular.use_booktabs;
5983 case Tabular::SET_BOOKTABS:
5984 tabular.use_booktabs = true;
5987 case Tabular::UNSET_BOOKTABS:
5988 tabular.use_booktabs = false;
5991 case Tabular::SET_TOP_SPACE: {
5993 if (value == "default")
5994 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5995 tabular.row_info[r].top_space_default = true;
5996 else if (value == "none")
5997 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5998 tabular.row_info[r].top_space_default = false;
5999 tabular.row_info[r].top_space = len;
6001 else if (isValidLength(value, &len))
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;
6009 case Tabular::SET_BOTTOM_SPACE: {
6011 if (value == "default")
6012 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6013 tabular.row_info[r].bottom_space_default = true;
6014 else if (value == "none")
6015 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6016 tabular.row_info[r].bottom_space_default = false;
6017 tabular.row_info[r].bottom_space = len;
6019 else if (isValidLength(value, &len))
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;
6027 case Tabular::SET_INTERLINE_SPACE: {
6029 if (value == "default")
6030 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6031 tabular.row_info[r].interline_space_default = true;
6032 else if (value == "none")
6033 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6034 tabular.row_info[r].interline_space_default = false;
6035 tabular.row_info[r].interline_space = len;
6037 else if (isValidLength(value, &len))
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;
6045 case Tabular::SET_DECIMAL_POINT:
6046 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6047 tabular.column_info[c].decimal_point = from_utf8(value);
6050 // dummy stuff just to avoid warnings
6051 case Tabular::LAST_ACTION:
6057 bool InsetTabular::copySelection(Cursor & cur)
6059 if (!cur.selection())
6064 getSelection(cur, rs, re, cs, ce);
6066 paste_tabular.reset(new Tabular(tabular));
6068 for (row_type r = 0; r < rs; ++r)
6069 paste_tabular->deleteRow(0);
6071 row_type const rows = re - rs + 1;
6072 while (paste_tabular->nrows() > rows)
6073 paste_tabular->deleteRow(rows);
6075 for (col_type c = 0; c < cs; ++c)
6076 paste_tabular->deleteColumn(0);
6078 col_type const columns = ce - cs + 1;
6079 while (paste_tabular->ncols() > columns)
6080 paste_tabular->deleteColumn(columns);
6082 paste_tabular->setBuffer(tabular.buffer());
6084 odocstringstream os;
6085 OutputParams const runparams(0);
6086 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6087 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6088 cap::copySelection(cur, os.str());
6090 // mark tabular stack dirty
6091 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6092 // when we (hopefully) have a one-for-all paste mechanism.
6093 // This must be called after cap::copySelection.
6094 dirtyTabularStack(true);
6100 bool InsetTabular::pasteClipboard(Cursor & cur)
6104 col_type actcol = tabular.cellColumn(cur.idx());
6105 row_type actrow = tabular.cellRow(cur.idx());
6107 if (cur.selIsMultiCell()) {
6110 getSelection(cur, actrow, re, actcol, ce);
6113 for (row_type r1 = 0, r2 = actrow;
6114 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6116 for (col_type c1 = 0, c2 = actcol;
6117 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6119 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6120 tabular.isPartOfMultiColumn(r2, c2))
6122 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6126 if (tabular.isPartOfMultiColumn(r2, c2)) {
6130 shared_ptr<InsetTableCell> inset(
6131 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6132 tabular.setCellInset(r2, c2, inset);
6133 // FIXME?: why do we need to do this explicitly? (EL)
6134 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6136 // FIXME: change tracking (MG)
6137 inset->setChange(Change(buffer().params().track_changes ?
6138 Change::INSERTED : Change::UNCHANGED));
6146 void InsetTabular::cutSelection(Cursor & cur)
6148 if (!cur.selection())
6153 getSelection(cur, rs, re, cs, ce);
6154 for (row_type r = rs; r <= re; ++r) {
6155 for (col_type c = cs; c <= ce; ++c) {
6156 shared_ptr<InsetTableCell> t
6157 = cell(tabular.cellIndex(r, c));
6158 if (buffer().params().track_changes)
6159 // FIXME: Change tracking (MG)
6160 t->setChange(Change(Change::DELETED));
6166 // cursor position might be invalid now
6167 if (cur.pit() > cur.lastpit())
6168 cur.pit() = cur.lastpit();
6169 if (cur.pos() > cur.lastpos())
6170 cur.pos() = cur.lastpos();
6171 cur.clearSelection();
6175 bool InsetTabular::isRightToLeft(Cursor & cur) const
6177 // LASSERT: It might be better to abandon this Buffer.
6178 LASSERT(cur.depth() > 1, return false);
6179 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6180 pos_type const parentpos = cur[cur.depth() - 2].pos();
6181 return parentpar.getFontSettings(buffer().params(),
6182 parentpos).language()->rightToLeft();
6186 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6189 LASSERT(stidx <= enidx, return docstring());
6191 col_type const col1 = tabular.cellColumn(stidx);
6192 col_type const col2 = tabular.cellColumn(enidx);
6193 row_type const row1 = tabular.cellRow(stidx);
6194 row_type const row2 = tabular.cellRow(enidx);
6196 for (col_type col = col1; col <= col2; col++)
6197 for (row_type row = row1; row <= row2; row++) {
6202 retval += tabular.cellInset(row, col)->asString(intoInsets);
6208 void InsetTabular::getSelection(Cursor & cur,
6209 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6211 CursorSlice const & beg = cur.selBegin();
6212 CursorSlice const & end = cur.selEnd();
6213 cs = tabular.cellColumn(beg.idx());
6214 ce = tabular.cellColumn(end.idx());
6218 rs = tabular.cellRow(beg.idx());
6219 re = tabular.cellRow(end.idx());
6225 Text * InsetTabular::getText(int idx) const
6227 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6231 void InsetTabular::setChange(Change const & change)
6233 for (idx_type idx = 0; idx < nargs(); ++idx)
6234 cell(idx)->setChange(change);
6238 void InsetTabular::acceptChanges()
6240 for (idx_type idx = 0; idx < nargs(); ++idx)
6241 cell(idx)->acceptChanges();
6245 void InsetTabular::rejectChanges()
6247 for (idx_type idx = 0; idx < nargs(); ++idx)
6248 cell(idx)->rejectChanges();
6252 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6254 return tabular.getPWidth(cell).zero();
6258 bool InsetTabular::forcePlainLayout(idx_type cell) const
6260 return !tabular.getPWidth(cell).zero();
6264 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6267 if (buf.length() <= 0)
6272 col_type maxCols = 1;
6273 size_t const len = buf.length();
6277 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6285 maxCols = max(cols, maxCols);
6291 maxCols = max(cols, maxCols);
6297 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6298 loctab = paste_tabular.get();
6299 dirtyTabularStack(true);
6302 cell = bv.cursor().idx();
6303 ocol = tabular.cellColumn(cell);
6304 row = tabular.cellRow(cell);
6308 idx_type const cells = loctab->numberofcells;
6311 rows = loctab->nrows();
6312 col_type const columns = loctab->ncols();
6314 while (cell < cells && p < len && row < rows &&
6315 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6321 // we can only set this if we are not too far right
6322 if (cols < columns) {
6323 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6324 Font const font = bv.textMetrics(&inset->text()).
6326 inset->setText(buf.substr(op, p - op), font,
6327 buffer().params().track_changes);
6333 // we can only set this if we are not too far right
6334 if (cols < columns) {
6335 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6336 Font const font = bv.textMetrics(&inset->text()).
6338 inset->setText(buf.substr(op, p - op), font,
6339 buffer().params().track_changes);
6344 cell = loctab->cellIndex(row, cols);
6350 // check for the last cell if there is no trailing '\n'
6351 if (cell < cells && op < len) {
6352 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6353 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6354 inset->setText(buf.substr(op, len - op), font,
6355 buffer().params().track_changes);
6361 void InsetTabular::addPreview(DocIterator const & inset_pos,
6362 PreviewLoader & loader) const
6364 DocIterator cell_pos = inset_pos;
6366 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6367 for (row_type r = 0; r < tabular.nrows(); ++r) {
6368 for (col_type c = 0; c < tabular.ncols(); ++c) {
6369 cell_pos.top().idx() = tabular.cellIndex(r, c);
6370 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6376 bool InsetTabular::completionSupported(Cursor const & cur) const
6378 Cursor const & bvCur = cur.bv().cursor();
6379 if (&bvCur.inset() != this)
6381 return cur.text()->completionSupported(cur);
6385 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6387 return completionSupported(cur);
6391 bool InsetTabular::automaticInlineCompletion() const
6393 return lyxrc.completion_inline_text;
6397 bool InsetTabular::automaticPopupCompletion() const
6399 return lyxrc.completion_popup_text;
6403 bool InsetTabular::showCompletionCursor() const
6405 return lyxrc.completion_cursor_text;
6409 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6411 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6415 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6417 if (!completionSupported(cur))
6419 return cur.text()->completionPrefix(cur);
6423 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6425 if (!completionSupported(cur))
6428 return cur.text()->insertCompletion(cur, s, finished);
6432 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6433 Dimension & dim) const
6435 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6436 tm.completionPosAndDim(cur, x, y, dim);
6440 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6442 istringstream data(in);
6444 lex.setStream(data);
6451 if (!lex || token != "tabular") {
6452 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6456 // This is part of the inset proper that is usually swallowed
6457 // by Buffer::readInset
6459 if (!lex || token != "Tabular") {
6460 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6468 string InsetTabular::params2string(InsetTabular const & inset)
6471 data << "tabular" << ' ';
6473 data << "\\end_inset\n";
6478 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6480 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6481 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6482 if (!tabular.isPartOfMultiColumn(r,c) &&
6483 !tabular.isPartOfMultiRow(r,c))
6486 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6487 ParagraphList::iterator it = parlist.begin();
6488 ParagraphList::iterator const en = parlist.end();
6489 for (; it != en; ++it)
6490 it->setLayout(dc.plainLayout());