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 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 switch (column_info[c].alignment) {
2766 case LYX_ALIGN_LEFT:
2767 os << ">{\\raggedright}";
2769 case LYX_ALIGN_RIGHT:
2770 os << ">{\\raggedleft}";
2772 case LYX_ALIGN_CENTER:
2773 os << ">{\\centering}";
2775 case LYX_ALIGN_NONE:
2776 case LYX_ALIGN_BLOCK:
2777 case LYX_ALIGN_LAYOUT:
2778 case LYX_ALIGN_SPECIAL:
2779 case LYX_ALIGN_DECIMAL:
2783 switch (column_info[c].valignment) {
2784 case LYX_VALIGN_TOP:
2787 case LYX_VALIGN_MIDDLE:
2790 case LYX_VALIGN_BOTTOM:
2795 << from_ascii(column_info[c].p_width.asLatexString())
2798 switch (column_info[c].alignment) {
2799 case LYX_ALIGN_LEFT:
2802 case LYX_ALIGN_RIGHT:
2805 case LYX_ALIGN_DECIMAL:
2806 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2812 } // end if else !column_info[i].p_width
2813 } // end if else !column_info[i].align_special
2814 if (columnRightLine(c))
2819 TeXLongtableHeaderFooter(os, runparams);
2821 //+---------------------------------------------------------------------
2822 //+ the single row and columns (cells) +
2823 //+---------------------------------------------------------------------
2825 for (row_type r = 0; r < nrows(); ++r) {
2826 if (isValidRow(r)) {
2827 TeXRow(os, r, runparams);
2828 if (is_long_tabular && row_info[r].newpage)
2829 os << "\\newpage\n";
2833 //+---------------------------------------------------------------------
2834 //+ the closing of the tabular +
2835 //+---------------------------------------------------------------------
2837 if (is_long_tabular)
2838 os << "\\end{longtable}";
2840 if (is_tabular_star)
2841 os << "\\end{tabular*}";
2843 os << "\\end{tabular}";
2847 os << breakln << "\\end{turn}";
2849 if (!TexRow::isNone(pos))
2850 os.texrow().start(pos);
2854 int Tabular::docbookRow(odocstream & os, row_type row,
2855 OutputParams const & runparams) const
2858 idx_type cell = getFirstCellInRow(row);
2861 for (col_type c = 0; c < ncols(); ++c) {
2862 if (isPartOfMultiColumn(row, c))
2865 os << "<entry align=\"";
2866 switch (getAlignment(cell)) {
2867 case LYX_ALIGN_LEFT:
2870 case LYX_ALIGN_RIGHT:
2878 os << "\" valign=\"";
2879 switch (getVAlignment(cell)) {
2880 case LYX_VALIGN_TOP:
2883 case LYX_VALIGN_BOTTOM:
2886 case LYX_VALIGN_MIDDLE:
2891 if (isMultiColumn(cell)) {
2892 os << " namest=\"col" << c << "\" ";
2893 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2897 ret += cellInset(cell)->docbook(os, runparams);
2906 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2910 //+---------------------------------------------------------------------
2911 //+ first the opening preamble +
2912 //+---------------------------------------------------------------------
2914 os << "<tgroup cols=\"" << ncols()
2915 << "\" colsep=\"1\" rowsep=\"1\">\n";
2917 for (col_type c = 0; c < ncols(); ++c) {
2918 os << "<colspec colname=\"col" << c << "\" align=\"";
2919 switch (column_info[c].alignment) {
2920 case LYX_ALIGN_LEFT:
2923 case LYX_ALIGN_RIGHT:
2931 if (runparams.flavor == OutputParams::XML)
2937 //+---------------------------------------------------------------------
2938 //+ Long Tabular case +
2939 //+---------------------------------------------------------------------
2941 // output caption info
2942 // The caption flag wins over head/foot
2943 if (haveLTCaption()) {
2944 os << "<caption>\n";
2946 for (row_type r = 0; r < nrows(); ++r) {
2947 if (row_info[r].caption) {
2948 ret += docbookRow(os, r, runparams);
2951 os << "</caption>\n";
2954 // output header info
2955 if (haveLTHead(false) || haveLTFirstHead(false)) {
2958 for (row_type r = 0; r < nrows(); ++r) {
2959 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2960 !row_info[r].caption) {
2961 ret += docbookRow(os, r, runparams);
2967 // output footer info
2968 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2971 for (row_type r = 0; r < nrows(); ++r) {
2972 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2973 !row_info[r].caption) {
2974 ret += docbookRow(os, r, runparams);
2981 //+---------------------------------------------------------------------
2982 //+ the single row and columns (cells) +
2983 //+---------------------------------------------------------------------
2987 for (row_type r = 0; r < nrows(); ++r) {
2988 if (isValidRow(r)) {
2989 ret += docbookRow(os, r, runparams);
2994 //+---------------------------------------------------------------------
2995 //+ the closing of the tabular +
2996 //+---------------------------------------------------------------------
3005 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
3006 OutputParams const & runparams, bool header) const
3009 string const celltag = header ? "th" : "td";
3010 idx_type cell = getFirstCellInRow(row);
3012 xs << html::StartTag("tr");
3013 for (col_type c = 0; c < ncols(); ++c) {
3014 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
3019 Length const cwidth = column_info[c].p_width;
3020 if (!cwidth.zero()) {
3021 string const hwidth = cwidth.asHTMLString();
3022 attr << "style =\"width: " << hwidth << ";\" ";
3026 switch (getAlignment(cell)) {
3027 case LYX_ALIGN_LEFT:
3030 case LYX_ALIGN_RIGHT:
3038 attr << " valign='";
3039 switch (getVAlignment(cell)) {
3040 case LYX_VALIGN_TOP:
3043 case LYX_VALIGN_BOTTOM:
3046 case LYX_VALIGN_MIDDLE:
3051 if (isMultiColumn(cell))
3052 attr << " colspan='" << columnSpan(cell) << "'";
3053 else if (isMultiRow(cell))
3054 attr << " rowspan='" << rowSpan(cell) << "'";
3056 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3057 ret += cellInset(cell)->xhtml(xs, runparams);
3058 xs << html::EndTag(celltag) << html::CR();
3061 xs << html::EndTag("tr");
3066 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3070 if (is_long_tabular) {
3071 // we'll wrap it in a div, so as to deal with alignment
3073 switch (longtabular_alignment) {
3074 case LYX_LONGTABULAR_ALIGN_LEFT:
3077 case LYX_LONGTABULAR_ALIGN_CENTER:
3080 case LYX_LONGTABULAR_ALIGN_RIGHT:
3084 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3086 // The caption flag wins over head/foot
3087 if (haveLTCaption()) {
3088 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3090 for (row_type r = 0; r < nrows(); ++r)
3091 if (row_info[r].caption)
3092 ret += xhtmlRow(xs, r, runparams);
3093 xs << html::EndTag("div") << html::CR();
3097 xs << html::StartTag("table") << html::CR();
3099 // output header info
3100 bool const havefirsthead = haveLTFirstHead(false);
3101 // if we have a first head, then we are going to ignore the
3102 // headers for the additional pages, since there aren't any
3103 // in XHTML. this test accomplishes that.
3104 bool const havehead = !havefirsthead && haveLTHead(false);
3105 if (havehead || havefirsthead) {
3106 xs << html::StartTag("thead") << html::CR();
3107 for (row_type r = 0; r < nrows(); ++r) {
3108 if (((havefirsthead && row_info[r].endfirsthead) ||
3109 (havehead && row_info[r].endhead)) &&
3110 !row_info[r].caption) {
3111 ret += xhtmlRow(xs, r, runparams, true);
3114 xs << html::EndTag("thead") << html::CR();
3116 // output footer info
3117 bool const havelastfoot = haveLTLastFoot(false);
3119 bool const havefoot = !havelastfoot && haveLTFoot(false);
3120 if (havefoot || havelastfoot) {
3121 xs << html::StartTag("tfoot") << html::CR();
3122 for (row_type r = 0; r < nrows(); ++r) {
3123 if (((havelastfoot && row_info[r].endlastfoot) ||
3124 (havefoot && row_info[r].endfoot)) &&
3125 !row_info[r].caption) {
3126 ret += xhtmlRow(xs, r, runparams);
3129 xs << html::EndTag("tfoot") << html::CR();
3132 xs << html::StartTag("tbody") << html::CR();
3133 for (row_type r = 0; r < nrows(); ++r) {
3134 if (isValidRow(r)) {
3135 ret += xhtmlRow(xs, r, runparams);
3138 xs << html::EndTag("tbody")
3140 << html::EndTag("table")
3142 if (is_long_tabular)
3143 xs << html::EndTag("div") << html::CR();
3148 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3149 vector<unsigned int> const & clen) const
3151 idx_type const fcell = getFirstCellInRow(row);
3152 idx_type const n = numberOfCellsInRow(row) + fcell;
3155 for (idx_type i = fcell; i < n; ++i) {
3165 for (idx_type i = fcell; i < n; ++i) {
3176 col_type column = cellColumn(i);
3177 int len = clen[column];
3178 while (column < ncols() - 1
3179 && isPartOfMultiColumn(row, ++column))
3180 len += clen[column] + 4;
3181 os << docstring(len, ch);
3196 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3197 vector<unsigned int> const & clen) const
3199 idx_type const fcell = getFirstCellInRow(row);
3200 idx_type const n = numberOfCellsInRow(row) + fcell;
3203 for (idx_type i = fcell; i < n; ++i) {
3204 if (bottomLine(i)) {
3213 for (idx_type i = fcell; i < n; ++i) {
3214 if (bottomLine(i)) {
3224 col_type column = cellColumn(i);
3225 int len = clen[column];
3226 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3227 len += clen[column] + 4;
3228 os << docstring(len, ch);
3229 if (bottomLine(i)) {
3243 void Tabular::plaintextPrintCell(odocstringstream & os,
3244 OutputParams const & runparams,
3245 idx_type cell, row_type row, col_type column,
3246 vector<unsigned int> const & clen,
3247 bool onlydata, size_t max_length) const
3249 odocstringstream sstr;
3250 cellInset(cell)->plaintext(sstr, runparams, max_length);
3262 unsigned int len1 = sstr.str().length();
3263 unsigned int len2 = clen[column];
3264 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3265 len2 += clen[column] + 4;
3268 switch (getAlignment(cell)) {
3270 case LYX_ALIGN_LEFT:
3273 case LYX_ALIGN_RIGHT:
3277 case LYX_ALIGN_CENTER:
3283 os << docstring(len1, ' ') << sstr.str()
3284 << docstring(len2, ' ');
3286 if (rightLine(cell))
3293 void Tabular::plaintext(odocstringstream & os,
3294 OutputParams const & runparams, int const depth,
3295 bool onlydata, char_type delim, size_t max_length) const
3297 // first calculate the width of the single columns
3298 vector<unsigned int> clen(ncols());
3301 // first all non multicolumn cells!
3302 for (col_type c = 0; c < ncols(); ++c) {
3304 for (row_type r = 0; r < nrows(); ++r) {
3305 idx_type cell = cellIndex(r, c);
3306 if (isMultiColumn(cell))
3308 odocstringstream sstr;
3309 cellInset(cell)->plaintext(sstr, runparams, max_length);
3310 if (clen[c] < sstr.str().length())
3311 clen[c] = sstr.str().length();
3314 // then all multicolumn cells!
3315 for (col_type c = 0; c < ncols(); ++c) {
3316 for (row_type r = 0; r < nrows(); ++r) {
3317 idx_type cell = cellIndex(r, c);
3318 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3320 odocstringstream sstr;
3321 cellInset(cell)->plaintext(sstr, runparams, max_length);
3322 int len = int(sstr.str().length());
3323 idx_type const n = columnSpan(cell);
3324 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3326 if (len > int(clen[c + n - 1]))
3327 clen[c + n - 1] = len;
3332 for (row_type r = 0; r < nrows(); ++r) {
3333 if (!onlydata && plaintextTopHLine(os, r, clen))
3334 os << docstring(depth * 2, ' ');
3335 for (col_type c = 0; c < ncols(); ++c) {
3336 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3338 if (onlydata && c > 0)
3339 // we don't use operator<< for single UCS4 character.
3340 // see explanation in docstream.h
3342 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3344 if (os.str().size() > max_length)
3349 os << docstring(depth * 2, ' ');
3350 if (plaintextBottomHLine(os, r, clen))
3351 os << docstring(depth * 2, ' ');
3353 if (os.str().size() > max_length)
3359 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3361 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3365 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3367 return cell_info[row][column].inset;
3371 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3373 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3377 void Tabular::setCellInset(row_type row, col_type column,
3378 shared_ptr<InsetTableCell> ins)
3380 CellData & cd = cell_info[row][column];
3385 void Tabular::validate(LaTeXFeatures & features) const
3387 features.require("NeedTabularnewline");
3389 features.require("booktabs");
3390 if (is_long_tabular)
3391 features.require("longtable");
3393 features.require("rotating");
3394 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3395 if (isMultiRow(cell))
3396 features.require("multirow");
3397 if (getVAlignment(cell) != LYX_VALIGN_TOP
3398 || !getPWidth(cell).zero())
3399 features.require("array");
3400 cellInset(cell)->validate(features);
3405 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3407 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3408 ParagraphList::const_iterator cit = parlist.begin();
3409 ParagraphList::const_iterator end = parlist.end();
3411 for (; cit != end; ++cit)
3412 for (int i = 0; i < cit->size(); ++i)
3413 if (cit->isNewline(i))
3420 /////////////////////////////////////////////////////////////////////
3424 /////////////////////////////////////////////////////////////////////
3426 InsetTableCell::InsetTableCell(Buffer * buf)
3427 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3428 contentAlign(LYX_ALIGN_CENTER)
3432 bool InsetTableCell::forcePlainLayout(idx_type) const
3434 return !isFixedWidth;
3438 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3440 return isFixedWidth;
3444 bool InsetTableCell::forceLocalFontSwitch() const
3446 return isFixedWidth;
3450 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3451 FuncStatus & status) const
3453 bool enabled = true;
3454 switch (cmd.action()) {
3455 case LFUN_MATH_DISPLAY:
3456 if (!hasFixedWidth()) {
3461 return InsetText::getStatus(cur, cmd, status);
3463 status.setEnabled(enabled);
3467 docstring InsetTableCell::asString(bool intoInsets)
3470 if (paragraphs().empty())
3472 ParagraphList::const_iterator it = paragraphs().begin();
3473 ParagraphList::const_iterator en = paragraphs().end();
3475 for (; it != en; ++it) {
3480 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3486 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3487 UpdateType utype) const
3489 InsetText::iterateForToc(di, output_active, utype);
3493 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3496 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3497 return InsetText::xhtml(xs, rp);
3502 /////////////////////////////////////////////////////////////////////
3506 /////////////////////////////////////////////////////////////////////
3508 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3510 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3511 rowselect_(false), colselect_(false)
3516 InsetTabular::InsetTabular(InsetTabular const & tab)
3517 : Inset(tab), tabular(tab.tabular),
3518 rowselect_(false), colselect_(false)
3523 InsetTabular::~InsetTabular()
3525 hideDialogs("tabular", this);
3529 void InsetTabular::setBuffer(Buffer & buf)
3531 tabular.setBuffer(buf);
3532 Inset::setBuffer(buf);
3536 bool InsetTabular::insetAllowed(InsetCode code) const
3541 case MATHMACRO_CODE:
3546 return tabular.is_long_tabular;
3554 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3556 return tabular.is_long_tabular &&
3557 (newtype == "Standard" || newtype == "Unnumbered");
3561 void InsetTabular::write(ostream & os) const
3563 os << "Tabular" << endl;
3568 string InsetTabular::contextMenu(BufferView const &, int, int) const
3570 // FIXME: depending on the selection state,
3571 // we could offer a different menu.
3572 return cell(0)->contextMenuName() + ";" + contextMenuName();
3576 string InsetTabular::contextMenuName() const
3578 return "context-tabular";
3582 void InsetTabular::read(Lexer & lex)
3584 //bool const old_format = (lex.getString() == "\\LyXTable");
3592 string token = lex.getString();
3593 while (lex && token != "\\end_inset") {
3595 token = lex.getString();
3598 lex.printError("Missing \\end_inset at this point. ");
3602 int InsetTabular::rowFromY(Cursor & cur, int y) const
3604 // top y coordinate of tabular
3605 int h = yo(cur.bv()) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3607 for (; r < tabular.nrows() && y > h; ++r)
3608 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3609 + tabular.interRowSpace(r);
3615 int InsetTabular::columnFromX(Cursor & cur, int x) const
3617 // left x coordinate of tabular
3618 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3620 for (; c < tabular.ncols() && x > w; ++c)
3621 w += tabular.cellWidth(c);
3626 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3628 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3629 // mi.base.textwidth << "\n";
3630 LBUFERR(mi.base.bv);
3632 for (row_type r = 0; r < tabular.nrows(); ++r) {
3635 for (col_type c = 0; c < tabular.ncols(); ++c) {
3636 if (tabular.isPartOfMultiColumn(r, c)
3637 || tabular.isPartOfMultiRow(r, c))
3638 // multicolumn or multirow cell, but not first one
3640 idx_type const cell = tabular.cellIndex(r, c);
3643 Length const p_width = tabular.getPWidth(cell);
3644 if (!p_width.zero())
3645 m.base.textwidth = p_width.inPixels(mi.base);
3646 tabular.cellInset(cell)->metrics(m, dim);
3647 if (!p_width.zero())
3648 dim.wid = m.base.textwidth;
3649 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3650 + tabular.interColumnSpace(cell);
3652 // FIXME(?): do we need a second metrics call?
3653 TextMetrics const & tm =
3654 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3656 // determine horizontal offset because of decimal align (if necessary)
3657 int decimal_width = 0;
3658 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3659 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3660 tail.setBuffer(tabular.buffer());
3661 // we need to set macrocontext position everywhere
3662 // otherwise we crash with nested insets (e.g. footnotes)
3663 // after decimal point
3664 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3666 dit.push_back(CursorSlice(tail));
3667 tail.setMacrocontextPositionRecursive(dit);
3669 // remove text leading decimal point
3670 docstring const align_d = tabular.column_info[c].decimal_point;
3671 dit = separatorPos(&tail, align_d);
3673 pit_type const psize = tail.paragraphs().front().size();
3675 tail.paragraphs().front().eraseChars(0,
3676 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3678 tail.metrics(m, dim1);
3679 decimal_width = dim1.width();
3682 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3683 tabular.cell_info[r][c].decimal_width = decimal_width;
3685 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3686 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3687 int const lastpardes = tm.last().second->descent()
3688 + TEXT_TO_INSET_OFFSET;
3690 switch (tabular.getVAlignment(cell)) {
3691 case Tabular::LYX_VALIGN_TOP:
3693 case Tabular::LYX_VALIGN_MIDDLE:
3694 offset = -(dim.des - lastpardes)/2;
3696 case Tabular::LYX_VALIGN_BOTTOM:
3697 offset = -(dim.des - lastpardes);
3700 tabular.cell_info[r][c].voffset = offset;
3701 maxasc = max(maxasc, dim.asc - offset);
3702 maxdes = max(maxdes, dim.des + offset);
3704 int const top_space = tabular.row_info[r].top_space_default ?
3705 default_line_space :
3706 tabular.row_info[r].top_space.inPixels(mi.base);
3707 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3708 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3709 default_line_space :
3710 tabular.row_info[r].bottom_space.inPixels(mi.base);
3711 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3714 tabular.updateColumnWidths();
3715 dim.asc = tabular.rowAscent(0) - tabular.offsetVAlignment();
3716 dim.des = tabular.height() - dim.asc;
3717 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3721 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3724 if (&cur.inset() == this && cur.selection()) {
3725 if (cur.selIsMultiCell()) {
3728 getSelection(cur, rs, re, cs, ce);
3730 idx_type const cell = tabular.cellIndex(row, col);
3731 col_type const cspan = tabular.columnSpan(cell);
3732 row_type const rspan = tabular.rowSpan(cell);
3733 if (col + cspan - 1 >= cs && col <= ce
3734 && row + rspan - 1 >= rs && row <= re)
3737 if (col == tabular.cellColumn(cur.idx())
3738 && row == tabular.cellRow(cur.idx())) {
3739 CursorSlice const & beg = cur.selBegin();
3740 CursorSlice const & end = cur.selEnd();
3742 if ((end.lastpos() > 0 || end.lastpit() > 0)
3743 && end.pos() == end.lastpos() && beg.pos() == 0
3744 && end.pit() == end.lastpit() && beg.pit() == 0)
3752 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3754 x += ADD_TO_TABULAR_WIDTH;
3756 BufferView * bv = pi.base.bv;
3757 Cursor & cur = pi.base.bv->cursor();
3759 // FIXME: As the full background is painted in drawBackground(),
3760 // we have no choice but to do a full repaint for the Text cells.
3761 pi.full_repaint = true;
3763 bool const original_selection_state = pi.selected;
3767 int yy = y + tabular.offsetVAlignment();
3768 for (row_type r = 0; r < tabular.nrows(); ++r) {
3770 for (col_type c = 0; c < tabular.ncols(); ++c) {
3771 if (tabular.isPartOfMultiColumn(r, c))
3774 idx = tabular.cellIndex(r, c);
3776 if (tabular.isPartOfMultiRow(r, c)) {
3777 nx += tabular.cellWidth(idx);
3781 pi.selected |= isCellSelected(cur, r, c);
3782 int const cx = nx + tabular.textHOffset(idx);
3783 int const cy = yy + tabular.textVOffset(idx);
3784 // Cache the Inset position.
3785 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3786 cell(idx)->draw(pi, cx, cy);
3787 drawCellLines(pi, nx, yy, r, idx);
3788 nx += tabular.cellWidth(idx);
3789 pi.selected = original_selection_state;
3792 if (r + 1 < tabular.nrows())
3793 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3794 + tabular.interRowSpace(r + 1);
3799 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3801 x += ADD_TO_TABULAR_WIDTH;
3802 y += tabular.offsetVAlignment() - tabular.rowAscent(0);
3803 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3804 pi.backgroundColor(this));
3808 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3810 Cursor & cur = pi.base.bv->cursor();
3812 x += ADD_TO_TABULAR_WIDTH;
3814 if (!cur.selection())
3816 if (&cur.inset() != this)
3821 bool const full_cell_selected = isCellSelected(cur,
3822 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3824 if (cur.selIsMultiCell() || full_cell_selected) {
3825 for (row_type r = 0; r < tabular.nrows(); ++r) {
3827 for (col_type c = 0; c < tabular.ncols(); ++c) {
3828 if (tabular.isPartOfMultiColumn(r, c))
3831 idx_type const cell = tabular.cellIndex(r, c);
3833 if (tabular.isPartOfMultiRow(r, c)) {
3834 xx += tabular.cellWidth(cell);
3837 int const w = tabular.cellWidth(cell);
3838 int const h = tabular.cellHeight(cell);
3839 int const yy = y - tabular.rowAscent(r) + tabular.offsetVAlignment();
3840 if (isCellSelected(cur, r, c))
3841 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3844 if (r + 1 < tabular.nrows())
3845 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3846 + tabular.interRowSpace(r + 1);
3850 // FIXME: This code has no effect because InsetTableCell does not handle
3851 // drawSelection other than the trivial implementation in Inset.
3853 // x += cellXPos(cur.idx());
3854 // x += tabular.textHOffset(cur.idx());
3855 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3860 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3861 row_type row, idx_type cell) const
3863 y -= tabular.rowAscent(row);
3864 int const w = tabular.cellWidth(cell);
3865 int const h = tabular.cellHeight(cell);
3866 Color const linecolor = pi.textColor(Color_tabularline);
3867 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3870 bool drawline = tabular.topLine(cell)
3871 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3872 pi.pain.line(x, y, x + w, y,
3873 drawline ? linecolor : gridcolor,
3874 drawline ? Painter::line_solid : Painter::line_onoffdash);
3877 drawline = tabular.bottomLine(cell);
3878 pi.pain.line(x, y + h, x + w, y + h,
3879 drawline ? linecolor : gridcolor,
3880 drawline ? Painter::line_solid : Painter::line_onoffdash);
3883 col_type const col = tabular.cellColumn(cell);
3884 drawline = tabular.leftLine(cell)
3885 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3886 pi.pain.line(x, y, x, y + h,
3887 drawline ? linecolor : gridcolor,
3888 drawline ? Painter::line_solid : Painter::line_onoffdash);
3891 x -= tabular.interColumnSpace(cell);
3892 col_type next_cell_col = col + 1;
3893 while (next_cell_col < tabular.ncols()
3894 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3896 drawline = tabular.rightLine(cell)
3897 || (next_cell_col < tabular.ncols()
3898 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3899 pi.pain.line(x + w, y, x + w, y + h,
3900 drawline ? linecolor : gridcolor,
3901 drawline ? Painter::line_solid : Painter::line_onoffdash);
3905 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3907 //lyxerr << "InsetTabular::edit: " << this << endl;
3911 if (isRightToLeft(cur))
3912 cur.idx() = tabular.getLastCellInRow(0);
3918 if (isRightToLeft(cur))
3919 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3921 cur.idx() = tabular.numberofcells - 1;
3923 cur.pos() = cur.lastpos(); // FIXME crude guess
3925 cur.setCurrentFont();
3926 // FIXME: this accesses the position cache before it is initialized
3927 //cur.bv().fitCursor();
3931 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3933 // In a longtable, tell captions what the current float is
3934 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3935 string const saveflt = cnts.current_float();
3936 if (tabular.is_long_tabular) {
3937 cnts.current_float("table");
3938 // in longtables, we only step the counter once
3939 cnts.step(from_ascii("table"), utype);
3940 cnts.isLongtable(true);
3943 ParIterator it2 = it;
3945 size_t const end = it2.nargs();
3946 for ( ; it2.idx() < end; it2.top().forwardIdx())
3947 buffer().updateBuffer(it2, utype);
3950 if (tabular.is_long_tabular) {
3951 cnts.current_float(saveflt);
3952 cnts.isLongtable(false);
3957 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3958 UpdateType utype) const
3960 DocIterator dit = cpit;
3962 size_t const end = dit.nargs();
3963 for ( ; dit.idx() < end; dit.top().forwardIdx())
3964 cell(dit.idx())->addToToc(dit, output_active, utype);
3968 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
3970 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
3971 return x < x0 || x > x0 + tabular.width();
3975 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
3977 int const y0 = yo(bv) - tabular.rowAscent(0) + tabular.offsetVAlignment();
3978 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
3979 // there is no margin added vertically to tabular insets.
3980 // However, it works for now.
3981 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
3985 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
3987 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
3991 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3993 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3994 << "\n cur:" << cur);
3995 CursorSlice sl = cur.top();
3996 Cursor & bvcur = cur.bv().cursor();
3998 FuncCode const act = cmd.action();
4002 case LFUN_MOUSE_PRESS: {
4003 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4005 if (hitSelectRow(cur.bv(), cmd.x())) {
4006 row_type r = rowFromY(cur, cmd.y());
4007 cur.idx() = tabular.getFirstCellInRow(r);
4011 cur.idx() = tabular.getLastCellInRow(r);
4012 cur.pit() = cur.lastpit();
4013 cur.pos() = cur.lastpos();
4014 cur.selection(true);
4020 if (hitSelectColumn(cur.bv(), cmd.y())) {
4021 col_type c = columnFromX(cur, cmd.x());
4022 cur.idx() = tabular.cellIndex(0, c);
4026 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4027 cur.pit() = cur.lastpit();
4028 cur.pos() = cur.lastpos();
4029 cur.selection(true);
4034 // do not reset cursor/selection if we have selected
4035 // some cells (bug 2715).
4036 if (cmd.button() == mouse_button::button3
4037 && &bvcur.selBegin().inset() == this
4038 && bvcur.selIsMultiCell())
4041 // Let InsetTableCell do it
4042 cell(cur.idx())->dispatch(cur, cmd);
4045 case LFUN_MOUSE_MOTION:
4046 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4047 if (cmd.button() == mouse_button::button1) {
4048 // only accept motions to places not deeper nested than the real anchor
4049 if (!bvcur.realAnchor().hasPart(cur)) {
4053 // select (additional) row
4055 row_type r = rowFromY(cur, cmd.y());
4056 cur.idx() = tabular.getLastCellInRow(r);
4057 // we need to reset the cursor's pit and pos now, as the old ones
4058 // may no longer be valid.
4061 bvcur.setCursor(cur);
4062 bvcur.selection(true);
4065 // select (additional) column
4067 col_type c = columnFromX(cur, cmd.x());
4068 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4069 // we need to reset the cursor's pit and pos now, as the old ones
4070 // may no longer be valid.
4073 bvcur.setCursor(cur);
4074 bvcur.selection(true);
4077 // only update if selection changes
4078 if (bvcur.idx() == cur.idx() &&
4079 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4080 cur.noScreenUpdate();
4081 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4082 bvcur.setCursor(cur);
4083 bvcur.selection(true);
4084 // if this is a multicell selection, we just set the cursor to
4085 // the beginning of the cell's text.
4086 if (bvcur.selIsMultiCell()) {
4087 bvcur.pit() = bvcur.lastpit();
4088 bvcur.pos() = bvcur.lastpos();
4093 case LFUN_MOUSE_RELEASE:
4098 case LFUN_CELL_BACKWARD:
4100 cur.selection(false);
4103 case LFUN_CELL_FORWARD:
4105 cur.selection(false);
4108 case LFUN_CHAR_FORWARD_SELECT:
4109 case LFUN_CHAR_FORWARD:
4110 case LFUN_CHAR_BACKWARD_SELECT:
4111 case LFUN_CHAR_BACKWARD:
4112 case LFUN_CHAR_RIGHT_SELECT:
4113 case LFUN_CHAR_RIGHT:
4114 case LFUN_CHAR_LEFT_SELECT:
4115 case LFUN_CHAR_LEFT:
4116 case LFUN_WORD_FORWARD:
4117 case LFUN_WORD_FORWARD_SELECT:
4118 case LFUN_WORD_BACKWARD:
4119 case LFUN_WORD_BACKWARD_SELECT:
4120 case LFUN_WORD_RIGHT:
4121 case LFUN_WORD_RIGHT_SELECT:
4122 case LFUN_WORD_LEFT:
4123 case LFUN_WORD_LEFT_SELECT: {
4124 // determine whether we move to next or previous cell, where to enter
4125 // the new cell from, and which command to "finish" (i.e., exit the
4128 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4129 FuncCode finish_lfun;
4131 if (act == LFUN_CHAR_FORWARD
4132 || act == LFUN_CHAR_FORWARD_SELECT
4133 || act == LFUN_WORD_FORWARD
4134 || act == LFUN_WORD_FORWARD_SELECT) {
4136 finish_lfun = LFUN_FINISHED_FORWARD;
4138 else if (act == LFUN_CHAR_BACKWARD
4139 || act == LFUN_CHAR_BACKWARD_SELECT
4140 || act == LFUN_WORD_BACKWARD
4141 || act == LFUN_WORD_BACKWARD_SELECT) {
4143 finish_lfun = LFUN_FINISHED_BACKWARD;
4145 // LEFT or RIGHT commands --- the interpretation will depend on the
4146 // table's direction.
4148 bool const right = act == LFUN_CHAR_RIGHT
4149 || act == LFUN_CHAR_RIGHT_SELECT
4150 || act == LFUN_WORD_RIGHT
4151 || act == LFUN_WORD_RIGHT_SELECT;
4152 next_cell = isRightToLeft(cur) != right;
4154 if (lyxrc.visual_cursor)
4155 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4157 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4160 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4161 || act == LFUN_CHAR_BACKWARD_SELECT
4162 || act == LFUN_CHAR_RIGHT_SELECT
4163 || act == LFUN_CHAR_LEFT_SELECT
4164 || act == LFUN_WORD_FORWARD_SELECT
4165 || act == LFUN_WORD_RIGHT_SELECT
4166 || act == LFUN_WORD_BACKWARD_SELECT
4167 || act == LFUN_WORD_LEFT_SELECT;
4169 // If we have a multicell selection or we're
4170 // not doing some LFUN_*_SELECT thing anyway...
4171 if (!cur.selIsMultiCell() || !select) {
4172 col_type const c = tabular.cellColumn(cur.idx());
4173 row_type const r = tabular.cellRow(cur.idx());
4174 // Are we trying to select the whole cell and is the whole cell
4175 // not yet selected?
4176 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4177 ((next_cell && cur.pit() == cur.lastpit()
4178 && cur.pos() == cur.lastpos())
4179 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4181 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4183 // ...try to dispatch to the cell's inset.
4184 cell(cur.idx())->dispatch(cur, cmd);
4186 // When we already have a selection we want to select the whole cell
4187 // before going to the next cell.
4188 if (select_whole && !empty_cell){
4189 getText(cur.idx())->selectAll(cur);
4191 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4195 // FIXME: When we support the selection of an empty cell, remove
4196 // the !empty_cell from this condition. For now we jump to the next
4197 // cell if the current cell is empty.
4198 if (cur.result().dispatched() && !empty_cell)
4202 // move to next/prev cell, as appropriate
4203 // note that we will always do this if we're selecting and we have
4204 // a multicell selection
4205 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4206 << " cell from: " << int(entry_from));
4208 moveNextCell(cur, entry_from);
4210 movePrevCell(cur, entry_from);
4211 // if we're exiting the table, call the appropriate FINISHED lfun
4212 if (sl == cur.top()) {
4213 cmd = FuncRequest(finish_lfun);
4218 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4223 case LFUN_DOWN_SELECT:
4225 if (!(cur.selection() && cur.selIsMultiCell()))
4226 cell(cur.idx())->dispatch(cur, cmd);
4228 cur.dispatched(); // override the cell's decision
4229 if (sl == cur.top()) {
4230 // if our Text didn't do anything to the cursor
4231 // then we try to put the cursor into the cell below
4232 // setting also the right targetX.
4233 cur.selHandle(act == LFUN_DOWN_SELECT);
4234 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4235 int const xtarget = cur.targetX();
4236 // WARNING: Once cur.idx() has been reset, the cursor is in
4237 // an inconsistent state until pos() has been set. Be careful
4238 // what you do with it!
4239 cur.idx() = tabular.cellBelow(cur.idx());
4241 TextMetrics const & tm =
4242 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4243 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4244 cur.setCurrentFont();
4247 if (sl == cur.top()) {
4248 // we trick it to go to forward after leaving the
4250 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4253 if (cur.selIsMultiCell()) {
4254 cur.pit() = cur.lastpit();
4255 cur.pos() = cur.lastpos();
4256 cur.setCurrentFont();
4257 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4260 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4263 case LFUN_UP_SELECT:
4265 if (!(cur.selection() && cur.selIsMultiCell()))
4266 cell(cur.idx())->dispatch(cur, cmd);
4267 cur.dispatched(); // override the cell's decision
4268 if (sl == cur.top()) {
4269 // if our Text didn't do anything to the cursor
4270 // then we try to put the cursor into the cell above
4271 // setting also the right targetX.
4272 cur.selHandle(act == LFUN_UP_SELECT);
4273 if (tabular.cellRow(cur.idx()) != 0) {
4274 int const xtarget = cur.targetX();
4275 // WARNING: Once cur.idx() has been reset, the cursor is in
4276 // an inconsistent state until pos() has been set. Be careful
4277 // what you do with it!
4278 cur.idx() = tabular.cellAbove(cur.idx());
4279 cur.pit() = cur.lastpit();
4280 Text const * text = cell(cur.idx())->getText(0);
4281 TextMetrics const & tm = cur.bv().textMetrics(text);
4282 ParagraphMetrics const & pm =
4283 tm.parMetrics(cur.lastpit());
4284 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4285 cur.setCurrentFont();
4288 if (sl == cur.top()) {
4289 cmd = FuncRequest(LFUN_UP);
4292 if (cur.selIsMultiCell()) {
4294 cur.pos() = cur.lastpos();
4295 cur.setCurrentFont();
4296 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4299 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4302 case LFUN_LAYOUT_TABULAR:
4303 cur.bv().showDialog("tabular");
4306 case LFUN_INSET_MODIFY:
4307 // we come from the dialog
4308 if (cmd.getArg(0) == "tabular")
4309 tabularFeatures(cur, cmd.getLongArg(1));
4314 case LFUN_TABULAR_FEATURE:
4315 tabularFeatures(cur, to_utf8(cmd.argument()));
4318 // insert file functions
4319 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4320 case LFUN_FILE_INSERT_PLAINTEXT:
4322 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4323 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4324 FileName(to_utf8(cmd.argument())));
4327 cur.recordUndoInset();
4328 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4329 // content has been replaced,
4330 // so cursor might be invalid
4331 cur.pos() = cur.lastpos();
4332 cur.pit() = cur.lastpit();
4333 bvcur.setCursor(cur);
4340 if (cur.selIsMultiCell()) {
4341 if (copySelection(cur)) {
4342 cur.recordUndoInset();
4346 cell(cur.idx())->dispatch(cur, cmd);
4349 case LFUN_SELF_INSERT:
4350 if (cur.selIsMultiCell()) {
4351 cur.recordUndoInset();
4353 BufferView * bv = &cur.bv();
4354 docstring::const_iterator cit = cmd.argument().begin();
4355 docstring::const_iterator const end = cmd.argument().end();
4356 for (; cit != end; ++cit)
4357 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4360 bv->bookmarkEditPosition();
4362 cell(cur.idx())->dispatch(cur, cmd);
4365 case LFUN_CHAR_DELETE_BACKWARD:
4366 case LFUN_CHAR_DELETE_FORWARD:
4367 if (cur.selIsMultiCell()) {
4368 cur.recordUndoInset();
4371 cell(cur.idx())->dispatch(cur, cmd);
4375 if (!cur.selection())
4377 if (cur.selIsMultiCell()) {
4381 cell(cur.idx())->dispatch(cur, cmd);
4384 case LFUN_CLIPBOARD_PASTE:
4385 case LFUN_PRIMARY_SELECTION_PASTE: {
4386 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4387 theClipboard().getAsText(Clipboard::PlainTextType) :
4388 theSelection().get();
4391 // pass to InsertPlaintextString, but
4392 // only if we have multi-cell content
4393 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4394 cur.recordUndoInset();
4395 if (insertPlaintextString(cur.bv(), clip, false)) {
4396 // content has been replaced,
4397 // so cursor might be invalid
4398 cur.pos() = cur.lastpos();
4399 cur.pit() = cur.lastpit();
4400 bvcur.setCursor(cur);
4404 // Let the cell handle normal text
4405 cell(cur.idx())->dispatch(cur, cmd);
4410 if (!tabularStackDirty()) {
4411 if (!cur.selIsMultiCell())
4412 cell(cur.idx())->dispatch(cur, cmd);
4415 if (theClipboard().isInternal()) {
4416 cur.recordUndoInset();
4417 pasteClipboard(cur);
4421 case LFUN_FONT_EMPH:
4422 case LFUN_FONT_BOLD:
4423 case LFUN_FONT_BOLDSYMBOL:
4424 case LFUN_FONT_ROMAN:
4425 case LFUN_FONT_NOUN:
4426 case LFUN_FONT_ITAL:
4427 case LFUN_FONT_FRAK:
4428 case LFUN_FONT_TYPEWRITER:
4429 case LFUN_FONT_SANS:
4430 case LFUN_TEXTSTYLE_APPLY:
4431 case LFUN_TEXTSTYLE_UPDATE:
4432 case LFUN_FONT_SIZE:
4433 case LFUN_FONT_UNDERLINE:
4434 case LFUN_FONT_STRIKEOUT:
4435 case LFUN_FONT_UNDERUNDERLINE:
4436 case LFUN_FONT_UNDERWAVE:
4438 case LFUN_PARAGRAPH_PARAMS_APPLY:
4439 case LFUN_PARAGRAPH_PARAMS:
4440 case LFUN_WORD_CAPITALIZE:
4441 case LFUN_WORD_UPCASE:
4442 case LFUN_WORD_LOWCASE:
4443 case LFUN_CHARS_TRANSPOSE:
4444 if (cur.selIsMultiCell()) {
4447 getSelection(cur, rs, re, cs, ce);
4448 Cursor tmpcur = cur;
4449 for (row_type r = rs; r <= re; ++r) {
4450 for (col_type c = cs; c <= ce; ++c) {
4451 // cursor follows cell:
4452 tmpcur.idx() = tabular.cellIndex(r, c);
4453 // select this cell only:
4456 tmpcur.resetAnchor();
4457 tmpcur.pit() = tmpcur.lastpit();
4458 tmpcur.pos() = tmpcur.top().lastpos();
4459 tmpcur.setCursor(tmpcur);
4460 tmpcur.setSelection();
4461 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4466 cell(cur.idx())->dispatch(cur, cmd);
4470 case LFUN_INSET_SETTINGS:
4471 // relay this lfun to Inset, not to the cell.
4472 Inset::doDispatch(cur, cmd);
4476 // we try to handle this event in the insets dispatch function.
4477 cell(cur.idx())->dispatch(cur, cmd);
4483 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4484 string const & argument, FuncStatus & status) const
4487 int action = Tabular::LAST_ACTION;
4489 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4490 if (tabularFeature[i].feature == s) {
4491 action = tabularFeature[i].action;
4495 if (action == Tabular::LAST_ACTION) {
4497 status.setUnknown(true);
4501 row_type sel_row_start = 0;
4502 row_type sel_row_end = 0;
4503 col_type sel_col_start = 0;
4504 col_type sel_col_end = 0;
4505 Tabular::ltType dummyltt;
4508 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4511 case Tabular::SET_PWIDTH:
4512 case Tabular::SET_MPWIDTH:
4513 case Tabular::SET_SPECIAL_COLUMN:
4514 case Tabular::SET_SPECIAL_MULTICOLUMN:
4515 case Tabular::APPEND_ROW:
4516 case Tabular::APPEND_COLUMN:
4517 case Tabular::DELETE_ROW:
4518 case Tabular::DELETE_COLUMN:
4519 case Tabular::COPY_ROW:
4520 case Tabular::COPY_COLUMN:
4521 case Tabular::SET_TOP_SPACE:
4522 case Tabular::SET_BOTTOM_SPACE:
4523 case Tabular::SET_INTERLINE_SPACE:
4527 case Tabular::SET_TABULAR_WIDTH:
4528 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4529 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4532 case Tabular::MOVE_COLUMN_RIGHT:
4533 case Tabular::MOVE_COLUMN_LEFT:
4534 case Tabular::MOVE_ROW_DOWN:
4535 case Tabular::MOVE_ROW_UP: {
4536 if (cur.selection()) {
4537 status.message(_("Selections not supported."));
4538 status.setEnabled(false);
4542 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4543 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4544 (action == Tabular::MOVE_COLUMN_LEFT &&
4545 tabular.cellColumn(cur.idx()) == 0) ||
4546 (action == Tabular::MOVE_ROW_DOWN &&
4547 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4548 (action == Tabular::MOVE_ROW_UP &&
4549 tabular.cellRow(cur.idx()) == 0)) {
4550 status.setEnabled(false);
4554 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4555 action == Tabular::MOVE_COLUMN_LEFT) {
4556 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4557 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4558 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4559 status.message(_("Multi-column in current or"
4560 " destination column."));
4561 status.setEnabled(false);
4566 if (action == Tabular::MOVE_ROW_DOWN ||
4567 action == Tabular::MOVE_ROW_UP) {
4568 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4569 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4570 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4571 status.message(_("Multi-row in current or"
4572 " destination row."));
4573 status.setEnabled(false);
4578 status.setEnabled(true);
4582 case Tabular::SET_DECIMAL_POINT:
4584 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4587 case Tabular::SET_MULTICOLUMN:
4588 case Tabular::UNSET_MULTICOLUMN:
4589 case Tabular::MULTICOLUMN:
4590 // If a row is set as longtable caption, it must not be allowed
4591 // to unset that this row is a multicolumn.
4592 status.setEnabled(sel_row_start == sel_row_end
4593 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4594 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4597 case Tabular::SET_MULTIROW:
4598 case Tabular::UNSET_MULTIROW:
4599 case Tabular::MULTIROW:
4600 // If a row is set as longtable caption, it must not be allowed
4601 // to unset that this row is a multirow.
4602 status.setEnabled(sel_col_start == sel_col_end
4603 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4604 status.setOnOff(tabular.isMultiRow(cur.idx()));
4607 case Tabular::SET_ALL_LINES:
4608 case Tabular::UNSET_ALL_LINES:
4609 case Tabular::SET_BORDER_LINES:
4610 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4613 case Tabular::SET_LINE_TOP:
4614 case Tabular::SET_LINE_BOTTOM:
4615 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4618 case Tabular::SET_LINE_LEFT:
4619 case Tabular::SET_LINE_RIGHT:
4620 status.setEnabled(!tabular.use_booktabs
4621 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4624 case Tabular::TOGGLE_LINE_TOP:
4625 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4626 status.setOnOff(tabular.topLine(cur.idx()));
4629 case Tabular::TOGGLE_LINE_BOTTOM:
4630 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4631 status.setOnOff(tabular.bottomLine(cur.idx()));
4634 case Tabular::TOGGLE_LINE_LEFT:
4635 status.setEnabled(!tabular.use_booktabs
4636 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4637 status.setOnOff(tabular.leftLine(cur.idx()));
4640 case Tabular::TOGGLE_LINE_RIGHT:
4641 status.setEnabled(!tabular.use_booktabs
4642 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4643 status.setOnOff(tabular.rightLine(cur.idx()));
4646 // multirow cells only inherit the alignment of the column if the column has
4647 // no width, otherwise they are left-aligned
4648 // therefore allow always left but right and center only if there is no width
4649 case Tabular::M_ALIGN_LEFT:
4651 case Tabular::ALIGN_LEFT:
4652 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4655 case Tabular::M_ALIGN_RIGHT:
4657 case Tabular::ALIGN_RIGHT:
4658 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4659 && !tabular.getPWidth(cur.idx()).zero()));
4660 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4663 case Tabular::M_ALIGN_CENTER:
4665 case Tabular::ALIGN_CENTER:
4666 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4667 && !tabular.getPWidth(cur.idx()).zero()));
4668 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4671 case Tabular::ALIGN_BLOCK:
4672 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4673 && !tabular.isMultiRow(cur.idx()));
4674 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4677 case Tabular::ALIGN_DECIMAL:
4678 status.setEnabled(!tabular.isMultiRow(cur.idx())
4679 && !tabular.isMultiColumn(cur.idx()));
4680 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4683 case Tabular::M_VALIGN_TOP:
4685 case Tabular::VALIGN_TOP:
4686 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4687 && !tabular.isMultiRow(cur.idx()));
4689 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4692 case Tabular::M_VALIGN_BOTTOM:
4694 case Tabular::VALIGN_BOTTOM:
4695 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4696 && !tabular.isMultiRow(cur.idx()));
4698 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4701 case Tabular::M_VALIGN_MIDDLE:
4703 case Tabular::VALIGN_MIDDLE:
4704 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4705 && !tabular.isMultiRow(cur.idx()));
4707 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4710 case Tabular::SET_LONGTABULAR:
4711 case Tabular::TOGGLE_LONGTABULAR:
4712 // setting as longtable is not allowed when table is inside a float
4713 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4714 || cur.innerInsetOfType(WRAP_CODE) != 0)
4715 status.setEnabled(false);
4717 status.setEnabled(true);
4718 status.setOnOff(tabular.is_long_tabular);
4721 case Tabular::UNSET_LONGTABULAR:
4722 status.setOnOff(!tabular.is_long_tabular);
4725 case Tabular::TOGGLE_ROTATE_TABULAR:
4726 case Tabular::SET_ROTATE_TABULAR:
4727 status.setOnOff(tabular.rotate != 0);
4730 case Tabular::TABULAR_VALIGN_TOP:
4731 status.setEnabled(tabular.tabular_width.zero());
4732 status.setOnOff(tabular.tabular_valignment
4733 == Tabular::LYX_VALIGN_TOP);
4735 case Tabular::TABULAR_VALIGN_MIDDLE:
4736 status.setEnabled(tabular.tabular_width.zero());
4737 status.setOnOff(tabular.tabular_valignment
4738 == Tabular::LYX_VALIGN_MIDDLE);
4740 case Tabular::TABULAR_VALIGN_BOTTOM:
4741 status.setEnabled(tabular.tabular_width.zero());
4742 status.setOnOff(tabular.tabular_valignment
4743 == Tabular::LYX_VALIGN_BOTTOM);
4746 case Tabular::LONGTABULAR_ALIGN_LEFT:
4747 status.setOnOff(tabular.longtabular_alignment
4748 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4750 case Tabular::LONGTABULAR_ALIGN_CENTER:
4751 status.setOnOff(tabular.longtabular_alignment
4752 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4754 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4755 status.setOnOff(tabular.longtabular_alignment
4756 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4759 case Tabular::UNSET_ROTATE_TABULAR:
4760 status.setOnOff(tabular.rotate == 0);
4763 case Tabular::TOGGLE_ROTATE_CELL:
4764 case Tabular::SET_ROTATE_CELL:
4765 status.setOnOff(!oneCellHasRotationState(false,
4766 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4769 case Tabular::UNSET_ROTATE_CELL:
4770 status.setOnOff(!oneCellHasRotationState(true,
4771 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4774 case Tabular::SET_USEBOX:
4775 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4778 // every row can only be one thing:
4779 // either a footer or header
4780 case Tabular::SET_LTFIRSTHEAD:
4781 status.setEnabled(sel_row_start == sel_row_end);
4782 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4785 case Tabular::UNSET_LTFIRSTHEAD:
4786 status.setEnabled(sel_row_start == sel_row_end);
4787 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4790 case Tabular::SET_LTHEAD:
4791 status.setEnabled(sel_row_start == sel_row_end);
4792 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4795 case Tabular::UNSET_LTHEAD:
4796 status.setEnabled(sel_row_start == sel_row_end);
4797 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4800 case Tabular::SET_LTFOOT:
4801 status.setEnabled(sel_row_start == sel_row_end);
4802 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4805 case Tabular::UNSET_LTFOOT:
4806 status.setEnabled(sel_row_start == sel_row_end);
4807 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4810 case Tabular::SET_LTLASTFOOT:
4811 status.setEnabled(sel_row_start == sel_row_end);
4812 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4815 case Tabular::UNSET_LTLASTFOOT:
4816 status.setEnabled(sel_row_start == sel_row_end);
4817 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4820 case Tabular::SET_LTNEWPAGE:
4821 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4823 case Tabular::UNSET_LTNEWPAGE:
4824 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4827 // only one row in head/firsthead/foot/lasthead can be the caption
4828 // and a multirow cannot be set as caption
4829 case Tabular::SET_LTCAPTION:
4830 status.setEnabled(sel_row_start == sel_row_end
4831 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4832 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4833 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4834 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4835 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4836 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4837 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4838 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4839 && !tabular.isMultiRow(sel_row_start));
4840 status.setOnOff(tabular.ltCaption(sel_row_start));
4843 case Tabular::UNSET_LTCAPTION:
4844 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4847 case Tabular::TOGGLE_LTCAPTION:
4848 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4849 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4850 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4851 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4852 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4853 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4854 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4855 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4856 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4857 status.setOnOff(tabular.ltCaption(sel_row_start));
4860 case Tabular::TOGGLE_BOOKTABS:
4861 case Tabular::SET_BOOKTABS:
4862 status.setOnOff(tabular.use_booktabs);
4865 case Tabular::UNSET_BOOKTABS:
4866 status.setOnOff(!tabular.use_booktabs);
4871 status.setEnabled(false);
4878 // function sets an object as defined in FuncStatus.h:
4879 // states OK, Unknown, Disabled, On, Off.
4880 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4881 FuncStatus & status) const
4883 switch (cmd.action()) {
4884 case LFUN_INSET_MODIFY:
4885 if (cmd.getArg(0) != "tabular")
4887 if (cmd.getArg(1) == "for-dialog") {
4888 // The dialog is asking the status of a command
4889 if (&cur.inset() != this)
4891 string action = cmd.getArg(2);
4892 string arg = cmd.getLongArg(3);
4893 return getFeatureStatus(cur, action, arg, status);
4895 // We always enable the lfun if it is coming from the dialog
4896 // because the dialog makes sure all the settings are valid,
4897 // even though the first argument might not be valid now.
4898 status.setEnabled(true);
4902 case LFUN_TABULAR_FEATURE: {
4903 if (&cur.inset() != this)
4905 string action = cmd.getArg(0);
4906 string arg = cmd.getLongArg(1);
4907 return getFeatureStatus(cur, action, arg, status);
4910 case LFUN_CAPTION_INSERT: {
4911 // caption is only allowed in caption cell of longtable
4912 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4913 status.setEnabled(false);
4916 // only standard caption is allowed
4917 string arg = cmd.getArg(0);
4918 if (!arg.empty() && arg != "Standard") {
4919 status.setEnabled(false);
4922 // check if there is already a caption
4923 bool have_caption = false;
4924 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
4925 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4926 ParagraphList::const_iterator pend = itc.paragraphs().end();
4927 for (; pit != pend; ++pit) {
4928 InsetList::const_iterator it = pit->insetList().begin();
4929 InsetList::const_iterator end = pit->insetList().end();
4930 for (; it != end; ++it) {
4931 if (it->inset->lyxCode() == CAPTION_CODE) {
4932 have_caption = true;
4937 status.setEnabled(!have_caption);
4941 // These are only enabled inside tabular
4942 case LFUN_CELL_BACKWARD:
4943 case LFUN_CELL_FORWARD:
4944 status.setEnabled(true);
4947 // disable these with multiple cells selected
4948 case LFUN_INSET_INSERT:
4949 case LFUN_TABULAR_INSERT:
4950 case LFUN_FLEX_INSERT:
4951 case LFUN_FLOAT_INSERT:
4952 case LFUN_FLOAT_WIDE_INSERT:
4953 case LFUN_FOOTNOTE_INSERT:
4954 case LFUN_MARGINALNOTE_INSERT:
4955 case LFUN_MATH_INSERT:
4956 case LFUN_MATH_MODE:
4957 case LFUN_MATH_MUTATE:
4958 case LFUN_MATH_DISPLAY:
4959 case LFUN_NOTE_INSERT:
4960 case LFUN_ARGUMENT_INSERT:
4961 case LFUN_BOX_INSERT:
4962 case LFUN_BRANCH_INSERT:
4963 case LFUN_PHANTOM_INSERT:
4964 case LFUN_WRAP_INSERT:
4965 case LFUN_PREVIEW_INSERT:
4966 case LFUN_ERT_INSERT: {
4967 if (cur.selIsMultiCell()) {
4968 status.setEnabled(false);
4971 return cell(cur.idx())->getStatus(cur, cmd, status);
4974 // disable in non-fixed-width cells
4975 case LFUN_PARAGRAPH_BREAK:
4976 // multirow does not allow paragraph breaks
4977 if (tabular.isMultiRow(cur.idx())) {
4978 status.setEnabled(false);
4982 case LFUN_NEWLINE_INSERT: {
4983 if (tabular.getPWidth(cur.idx()).zero()) {
4984 status.setEnabled(false);
4987 return cell(cur.idx())->getStatus(cur, cmd, status);
4990 case LFUN_NEWPAGE_INSERT:
4991 status.setEnabled(false);
4995 if (tabularStackDirty() && theClipboard().isInternal()) {
4996 if (cur.selIsMultiCell()) {
4999 getSelection(cur, rs, re, cs, ce);
5000 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5001 && paste_tabular->nrows() == re - rs + 1)
5002 status.setEnabled(true);
5004 status.setEnabled(false);
5005 status.message(_("Selection size should match clipboard content."));
5008 status.setEnabled(true);
5011 return cell(cur.idx())->getStatus(cur, cmd, status);
5013 case LFUN_INSET_SETTINGS:
5014 // relay this lfun to Inset, not to the cell.
5015 return Inset::getStatus(cur, cmd, status);
5018 // we try to handle this event in the insets dispatch function.
5019 return cell(cur.idx())->getStatus(cur, cmd, status);
5025 Inset::DisplayType InsetTabular::display() const
5027 if (tabular.is_long_tabular) {
5028 switch (tabular.longtabular_alignment) {
5029 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5031 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5033 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5043 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5045 tabular.latex(os, runparams);
5049 int InsetTabular::plaintext(odocstringstream & os,
5050 OutputParams const & runparams, size_t max_length) const
5052 os << '\n'; // output table on a new line
5053 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5054 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5055 return PLAINTEXT_NEWLINE;
5059 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5064 // FIXME: Why not pass a proper DocIterator here?
5066 // if the table is inside a float it doesn't need the informaltable
5067 // wrapper. Search for it.
5068 for (master = owner(); master; master = master->owner())
5069 if (master->lyxCode() == FLOAT_CODE)
5074 os << "<informaltable>";
5077 ret += tabular.docbook(os, runparams);
5079 os << "</informaltable>";
5086 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5088 return tabular.xhtml(xs, rp);
5092 void InsetTabular::validate(LaTeXFeatures & features) const
5094 tabular.validate(features);
5095 features.useInsetLayout(getLayout());
5099 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5101 return tabular.cellInset(idx);
5105 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5107 return tabular.cellInset(idx);
5111 void InsetTabular::cursorPos(BufferView const & bv,
5112 CursorSlice const & sl, bool boundary, int & x, int & y) const
5114 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5116 // y offset correction
5117 y += cellYPos(sl.idx());
5118 y += tabular.textVOffset(sl.idx());
5119 y += tabular.offsetVAlignment();
5121 // x offset correction
5122 x += cellXPos(sl.idx());
5123 x += tabular.textHOffset(sl.idx());
5124 x += ADD_TO_TABULAR_WIDTH;
5128 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5132 Inset const & inset = *tabular.cellInset(cell);
5133 Point o = bv.coordCache().getInsets().xy(&inset);
5134 int const xbeg = o.x_ - tabular.textHOffset(cell);
5135 int const xend = xbeg + tabular.cellWidth(cell);
5136 row_type const row = tabular.cellRow(cell);
5137 int const ybeg = o.y_ - tabular.rowAscent(row)
5138 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5139 int const yend = ybeg + tabular.cellHeight(cell);
5151 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5152 // << " ybeg=" << ybeg << " yend=" << yend
5153 // << " xx=" << xx << " yy=" << yy
5154 // << " dist=" << xx + yy << endl;
5159 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5161 //lyxerr << "InsetTabular::editXY: " << this << endl;
5162 cur.selection(false);
5164 cur.idx() = getNearestCell(cur.bv(), x, y);
5165 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5169 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5171 cur.idx() = getNearestCell(cur.bv(), x, y);
5172 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5176 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5178 idx_type idx_min = 0;
5179 int dist_min = numeric_limits<int>::max();
5180 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5181 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5182 int const d = dist(bv, i, x, y);
5193 int InsetTabular::cellYPos(idx_type const cell) const
5195 row_type row = tabular.cellRow(cell);
5197 for (row_type r = 0; r < row; ++r)
5198 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5199 + tabular.interRowSpace(r + 1);
5204 int InsetTabular::cellXPos(idx_type const cell) const
5206 col_type col = tabular.cellColumn(cell);
5208 for (col_type c = 0; c < col; ++c)
5209 lx += tabular.column_info[c].width;
5214 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5216 row_type const row = tabular.cellRow(cur.idx());
5217 col_type const col = tabular.cellColumn(cur.idx());
5219 if (isRightToLeft(cur)) {
5220 if (tabular.cellColumn(cur.idx()) == 0) {
5221 if (row == tabular.nrows() - 1)
5223 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5228 cur.idx() = tabular.getLastCellInRow(row - 1);
5230 cur.idx() = tabular.cellIndex(row, col - 1);
5233 if (tabular.isLastCell(cur.idx()))
5235 if (cur.idx() == tabular.getLastCellInRow(row))
5236 cur.idx() = tabular.cellIndex(row + 1, 0);
5238 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5239 cur.idx() = tabular.cellIndex(row, colnextcell);
5243 cur.boundary(false);
5245 if (cur.selIsMultiCell()) {
5246 cur.pit() = cur.lastpit();
5247 cur.pos() = cur.lastpos();
5254 // in visual mode, place cursor at extreme left or right
5256 switch(entry_from) {
5258 case ENTRY_DIRECTION_RIGHT:
5259 cur.posVisToRowExtremity(false /* !left */);
5261 case ENTRY_DIRECTION_LEFT:
5262 cur.posVisToRowExtremity(true /* left */);
5264 case ENTRY_DIRECTION_IGNORE:
5265 // nothing to do in this case
5269 cur.setCurrentFont();
5273 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5275 row_type const row = tabular.cellRow(cur.idx());
5276 col_type const col = tabular.cellColumn(cur.idx());
5278 if (isRightToLeft(cur)) {
5279 if (cur.idx() == tabular.getLastCellInRow(row)) {
5282 cur.idx() = tabular.getFirstCellInRow(row);
5283 cur.idx() = tabular.cellAbove(cur.idx());
5285 if (tabular.isLastCell(cur.idx()))
5287 if (cur.idx() == tabular.getLastCellInRow(row))
5288 cur.idx() = tabular.cellIndex(row + 1, 0);
5290 cur.idx() = tabular.cellIndex(row, col + 1);
5293 if (cur.idx() == 0) // first cell
5296 cur.idx() = tabular.getLastCellInRow(row - 1);
5298 cur.idx() = tabular.cellIndex(row, col - 1);
5301 if (cur.selIsMultiCell()) {
5302 cur.pit() = cur.lastpit();
5303 cur.pos() = cur.lastpos();
5307 cur.pit() = cur.lastpit();
5308 cur.pos() = cur.lastpos();
5310 // in visual mode, place cursor at extreme left or right
5312 switch(entry_from) {
5314 case ENTRY_DIRECTION_RIGHT:
5315 cur.posVisToRowExtremity(false /* !left */);
5317 case ENTRY_DIRECTION_LEFT:
5318 cur.posVisToRowExtremity(true /* left */);
5320 case ENTRY_DIRECTION_IGNORE:
5321 // nothing to do in this case
5325 cur.setCurrentFont();
5329 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5331 cur.recordUndoInset(this);
5333 istringstream is(argument);
5336 size_t safe_guard = 0;
5341 if (safe_guard > 1000) {
5342 LYXERR0("parameter max count reached!");
5346 Tabular::Feature action = Tabular::LAST_ACTION;
5349 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5350 if (s != tabularFeature[i].feature)
5352 action = tabularFeature[i].action;
5355 if (action == Tabular::LAST_ACTION) {
5356 LYXERR0("Feature not found " << s);
5360 if (tabularFeature[i].need_value)
5362 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5363 tabularFeatures(cur, action, val);
5368 static void checkLongtableSpecial(Tabular::ltType & ltt,
5369 string const & special, bool & flag)
5371 if (special == "dl_above") {
5374 } else if (special == "dl_below") {
5375 ltt.bottomDL = flag;
5377 } else if (special == "empty") {
5387 bool InsetTabular::oneCellHasRotationState(bool rotated,
5388 row_type row_start, row_type row_end,
5389 col_type col_start, col_type col_end) const
5391 for (row_type r = row_start; r <= row_end; ++r)
5392 for (col_type c = col_start; c <= col_end; ++c)
5394 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5397 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5404 void InsetTabular::tabularFeatures(Cursor & cur,
5405 Tabular::Feature feature, string const & value)
5407 col_type sel_col_start;
5408 col_type sel_col_end;
5409 row_type sel_row_start;
5410 row_type sel_row_end;
5411 bool setLines = false;
5412 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5413 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5417 case Tabular::M_ALIGN_LEFT:
5418 case Tabular::ALIGN_LEFT:
5419 setAlign = LYX_ALIGN_LEFT;
5422 case Tabular::M_ALIGN_RIGHT:
5423 case Tabular::ALIGN_RIGHT:
5424 setAlign = LYX_ALIGN_RIGHT;
5427 case Tabular::M_ALIGN_CENTER:
5428 case Tabular::ALIGN_CENTER:
5429 setAlign = LYX_ALIGN_CENTER;
5432 case Tabular::ALIGN_BLOCK:
5433 setAlign = LYX_ALIGN_BLOCK;
5436 case Tabular::ALIGN_DECIMAL:
5437 setAlign = LYX_ALIGN_DECIMAL;
5440 case Tabular::M_VALIGN_TOP:
5441 case Tabular::VALIGN_TOP:
5442 setVAlign = Tabular::LYX_VALIGN_TOP;
5445 case Tabular::M_VALIGN_BOTTOM:
5446 case Tabular::VALIGN_BOTTOM:
5447 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5450 case Tabular::M_VALIGN_MIDDLE:
5451 case Tabular::VALIGN_MIDDLE:
5452 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5459 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5460 row_type const row = tabular.cellRow(cur.idx());
5461 col_type const column = tabular.cellColumn(cur.idx());
5463 Tabular::ltType ltt;
5467 case Tabular::SET_TABULAR_WIDTH:
5468 tabular.setTabularWidth(Length(value));
5471 case Tabular::SET_PWIDTH: {
5472 Length const len(value);
5473 tabular.setColumnPWidth(cur, cur.idx(), len);
5475 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5476 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5480 case Tabular::SET_MPWIDTH:
5481 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5484 case Tabular::SET_MROFFSET:
5485 tabular.setMROffset(cur, cur.idx(), Length(value));
5488 case Tabular::SET_SPECIAL_COLUMN:
5489 case Tabular::SET_SPECIAL_MULTICOLUMN:
5490 if (value == "none")
5491 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5493 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5496 case Tabular::APPEND_ROW:
5497 // append the row into the tabular
5498 tabular.appendRow(row);
5501 case Tabular::APPEND_COLUMN:
5502 // append the column into the tabular
5503 tabular.appendColumn(column);
5504 cur.idx() = tabular.cellIndex(row, column);
5507 case Tabular::DELETE_ROW:
5508 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5509 for (col_type c = 0; c < tabular.ncols(); c++)
5510 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5511 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5514 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5515 tabular.deleteRow(sel_row_start);
5516 if (sel_row_start >= tabular.nrows())
5518 cur.idx() = tabular.cellIndex(sel_row_start, column);
5521 cur.selection(false);
5524 case Tabular::DELETE_COLUMN:
5525 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5526 for (row_type r = 0; r < tabular.nrows(); r++)
5527 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5528 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5531 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5532 for (row_type r = 0; r < tabular.nrows(); r++)
5533 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5534 tabular.leftLine(tabular.cellIndex(r, 0)));
5537 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5538 tabular.deleteColumn(sel_col_start);
5539 if (sel_col_start >= tabular.ncols())
5541 cur.idx() = tabular.cellIndex(row, sel_col_start);
5544 cur.selection(false);
5547 case Tabular::COPY_ROW:
5548 tabular.copyRow(row);
5551 case Tabular::COPY_COLUMN:
5552 tabular.copyColumn(column);
5553 cur.idx() = tabular.cellIndex(row, column);
5556 case Tabular::MOVE_COLUMN_RIGHT:
5557 tabular.moveColumn(column, Tabular::RIGHT);
5558 cur.idx() = tabular.cellIndex(row, column + 1);
5561 case Tabular::MOVE_COLUMN_LEFT:
5562 tabular.moveColumn(column, Tabular::LEFT);
5563 cur.idx() = tabular.cellIndex(row, column - 1);
5566 case Tabular::MOVE_ROW_DOWN:
5567 tabular.moveRow(row, Tabular::DOWN);
5568 cur.idx() = tabular.cellIndex(row + 1, column);
5571 case Tabular::MOVE_ROW_UP:
5572 tabular.moveRow(row, Tabular::UP);
5573 cur.idx() = tabular.cellIndex(row - 1, column);
5576 case Tabular::SET_LINE_TOP:
5577 case Tabular::TOGGLE_LINE_TOP: {
5578 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5579 ? (value == "true") : !tabular.topLine(cur.idx());
5580 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5581 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5582 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5586 case Tabular::SET_LINE_BOTTOM:
5587 case Tabular::TOGGLE_LINE_BOTTOM: {
5588 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5589 ? (value == "true") : !tabular.bottomLine(cur.idx());
5590 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5591 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5592 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5596 case Tabular::SET_LINE_LEFT:
5597 case Tabular::TOGGLE_LINE_LEFT: {
5598 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5599 ? (value == "true") : !tabular.leftLine(cur.idx());
5600 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5601 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5602 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5606 case Tabular::SET_LINE_RIGHT:
5607 case Tabular::TOGGLE_LINE_RIGHT: {
5608 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5609 ? (value == "true") : !tabular.rightLine(cur.idx());
5610 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5611 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5612 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5616 case Tabular::M_ALIGN_LEFT:
5617 case Tabular::M_ALIGN_RIGHT:
5618 case Tabular::M_ALIGN_CENTER:
5619 case Tabular::ALIGN_LEFT:
5620 case Tabular::ALIGN_RIGHT:
5621 case Tabular::ALIGN_CENTER:
5622 case Tabular::ALIGN_BLOCK:
5623 case Tabular::ALIGN_DECIMAL:
5624 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5625 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5626 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5627 !tabular.getPWidth(c).zero());
5630 case Tabular::M_VALIGN_TOP:
5631 case Tabular::M_VALIGN_BOTTOM:
5632 case Tabular::M_VALIGN_MIDDLE:
5634 case Tabular::VALIGN_TOP:
5635 case Tabular::VALIGN_BOTTOM:
5636 case Tabular::VALIGN_MIDDLE:
5637 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5638 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5639 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5642 case Tabular::SET_MULTICOLUMN: {
5643 if (!cur.selection()) {
5644 // just multicol for one single cell
5645 // check whether we are completely in a multicol
5646 if (!tabular.isMultiColumn(cur.idx()))
5647 tabular.setMultiColumn(cur.idx(), 1,
5648 tabular.rightLine(cur.idx()));
5651 // we have a selection so this means we just add all these
5652 // cells to form a multicolumn cell
5653 idx_type const s_start = cur.selBegin().idx();
5654 row_type const col_start = tabular.cellColumn(s_start);
5655 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5656 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5657 tabular.rightLine(cur.selEnd().idx()));
5660 cur.selection(false);
5664 case Tabular::UNSET_MULTICOLUMN: {
5665 if (!cur.selection()) {
5666 if (tabular.isMultiColumn(cur.idx()))
5667 tabular.unsetMultiColumn(cur.idx());
5672 case Tabular::MULTICOLUMN: {
5673 if (!cur.selection()) {
5674 if (tabular.isMultiColumn(cur.idx()))
5675 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5677 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5681 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5682 row_type const r = sel_row_start;
5683 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5684 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5687 // If the selection contains at least one singlecol cell
5688 // or multiple multicol cells,
5689 // we assume the user will merge is to a single multicol
5691 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5693 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5697 case Tabular::SET_MULTIROW: {
5698 if (!cur.selection()) {
5699 // just multirow for one single cell
5700 // check whether we are completely in a multirow
5701 if (!tabular.isMultiRow(cur.idx()))
5702 tabular.setMultiRow(cur.idx(), 1,
5703 tabular.bottomLine(cur.idx()),
5704 tabular.getAlignment(cur.idx()));
5707 // we have a selection so this means we just add all this
5708 // cells to form a multirow cell
5709 idx_type const s_start = cur.selBegin().idx();
5710 row_type const row_start = tabular.cellRow(s_start);
5711 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5712 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5713 tabular.bottomLine(cur.selEnd().idx()),
5714 tabular.getAlignment(cur.selEnd().idx()));
5717 cur.selection(false);
5721 case Tabular::UNSET_MULTIROW: {
5722 if (!cur.selection()) {
5723 if (tabular.isMultiRow(cur.idx()))
5724 tabular.unsetMultiRow(cur.idx());
5729 case Tabular::MULTIROW: {
5730 if (!cur.selection()) {
5731 if (tabular.isMultiRow(cur.idx()))
5732 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5734 tabularFeatures(cur, Tabular::SET_MULTIROW);
5738 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5739 col_type const c = sel_col_start;
5740 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5741 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5744 // If the selection contains at least one singlerow cell
5745 // or multiple multirow cells,
5746 // we assume the user will merge is to a single multirow
5748 tabularFeatures(cur, Tabular::SET_MULTIROW);
5750 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5754 case Tabular::SET_ALL_LINES:
5756 case Tabular::UNSET_ALL_LINES:
5757 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5758 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5759 idx_type const cell = tabular.cellIndex(r, c);
5760 tabular.setTopLine(cell, setLines);
5761 tabular.setBottomLine(cell, setLines);
5762 tabular.setRightLine(cell, setLines);
5763 tabular.setLeftLine(cell, setLines);
5767 case Tabular::SET_BORDER_LINES:
5768 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5769 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5770 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5772 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5773 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5774 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5778 case Tabular::TOGGLE_LONGTABULAR:
5779 if (tabular.is_long_tabular)
5780 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5782 tabular.is_long_tabular = true;
5785 case Tabular::SET_LONGTABULAR:
5786 tabular.is_long_tabular = true;
5789 case Tabular::UNSET_LONGTABULAR:
5790 for (row_type r = 0; r < tabular.nrows(); ++r) {
5791 if (tabular.ltCaption(r)) {
5792 cur.idx() = tabular.cellIndex(r, 0);
5795 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5798 tabular.is_long_tabular = false;
5801 case Tabular::SET_ROTATE_TABULAR:
5802 tabular.rotate = convert<int>(value);
5805 case Tabular::UNSET_ROTATE_TABULAR:
5809 case Tabular::TOGGLE_ROTATE_TABULAR:
5810 // when pressing the rotate button we default to 90° rotation
5811 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5814 case Tabular::TABULAR_VALIGN_TOP:
5815 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5818 case Tabular::TABULAR_VALIGN_MIDDLE:
5819 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5822 case Tabular::TABULAR_VALIGN_BOTTOM:
5823 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5826 case Tabular::LONGTABULAR_ALIGN_LEFT:
5827 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5830 case Tabular::LONGTABULAR_ALIGN_CENTER:
5831 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5834 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5835 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5840 case Tabular::SET_ROTATE_CELL:
5841 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5842 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5843 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5846 case Tabular::UNSET_ROTATE_CELL:
5847 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5848 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5849 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5852 case Tabular::TOGGLE_ROTATE_CELL:
5854 bool oneNotRotated = oneCellHasRotationState(false,
5855 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5857 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5858 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5859 // when pressing the rotate cell button we default to 90° rotation
5861 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5863 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5868 case Tabular::SET_USEBOX: {
5869 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5870 if (val == tabular.getUsebox(cur.idx()))
5871 val = Tabular::BOX_NONE;
5872 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5873 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5874 tabular.setUsebox(tabular.cellIndex(r, c), val);
5878 case Tabular::UNSET_LTFIRSTHEAD:
5880 case Tabular::SET_LTFIRSTHEAD:
5881 tabular.getRowOfLTFirstHead(row, ltt);
5882 checkLongtableSpecial(ltt, value, flag);
5883 tabular.setLTHead(row, flag, ltt, true);
5886 case Tabular::UNSET_LTHEAD:
5888 case Tabular::SET_LTHEAD:
5889 tabular.getRowOfLTHead(row, ltt);
5890 checkLongtableSpecial(ltt, value, flag);
5891 tabular.setLTHead(row, flag, ltt, false);
5894 case Tabular::UNSET_LTFOOT:
5896 case Tabular::SET_LTFOOT:
5897 tabular.getRowOfLTFoot(row, ltt);
5898 checkLongtableSpecial(ltt, value, flag);
5899 tabular.setLTFoot(row, flag, ltt, false);
5902 case Tabular::UNSET_LTLASTFOOT:
5904 case Tabular::SET_LTLASTFOOT:
5905 tabular.getRowOfLTLastFoot(row, ltt);
5906 checkLongtableSpecial(ltt, value, flag);
5907 tabular.setLTFoot(row, flag, ltt, true);
5910 case Tabular::UNSET_LTNEWPAGE:
5912 case Tabular::SET_LTNEWPAGE:
5913 tabular.setLTNewPage(row, flag);
5916 case Tabular::SET_LTCAPTION: {
5917 if (tabular.ltCaption(row))
5919 cur.idx() = tabular.setLTCaption(row, true);
5922 cur.selection(false);
5923 // If a row is set as caption, then also insert
5924 // a caption. Otherwise the LaTeX output is broken.
5925 // Select cell if it is non-empty
5926 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5927 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5928 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5932 case Tabular::UNSET_LTCAPTION: {
5933 if (!tabular.ltCaption(row))
5935 cur.idx() = tabular.setLTCaption(row, false);
5938 cur.selection(false);
5939 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5940 if (lyx::getStatus(fr).enabled())
5945 case Tabular::TOGGLE_LTCAPTION: {
5946 if (tabular.ltCaption(row))
5947 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5949 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5953 case Tabular::TOGGLE_BOOKTABS:
5954 tabular.use_booktabs = !tabular.use_booktabs;
5957 case Tabular::SET_BOOKTABS:
5958 tabular.use_booktabs = true;
5961 case Tabular::UNSET_BOOKTABS:
5962 tabular.use_booktabs = false;
5965 case Tabular::SET_TOP_SPACE: {
5967 if (value == "default")
5968 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5969 tabular.row_info[r].top_space_default = true;
5970 else if (value == "none")
5971 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5972 tabular.row_info[r].top_space_default = false;
5973 tabular.row_info[r].top_space = len;
5975 else if (isValidLength(value, &len))
5976 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5977 tabular.row_info[r].top_space_default = false;
5978 tabular.row_info[r].top_space = len;
5983 case Tabular::SET_BOTTOM_SPACE: {
5985 if (value == "default")
5986 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5987 tabular.row_info[r].bottom_space_default = true;
5988 else if (value == "none")
5989 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5990 tabular.row_info[r].bottom_space_default = false;
5991 tabular.row_info[r].bottom_space = len;
5993 else if (isValidLength(value, &len))
5994 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5995 tabular.row_info[r].bottom_space_default = false;
5996 tabular.row_info[r].bottom_space = len;
6001 case Tabular::SET_INTERLINE_SPACE: {
6003 if (value == "default")
6004 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6005 tabular.row_info[r].interline_space_default = true;
6006 else if (value == "none")
6007 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6008 tabular.row_info[r].interline_space_default = false;
6009 tabular.row_info[r].interline_space = len;
6011 else if (isValidLength(value, &len))
6012 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6013 tabular.row_info[r].interline_space_default = false;
6014 tabular.row_info[r].interline_space = len;
6019 case Tabular::SET_DECIMAL_POINT:
6020 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6021 tabular.column_info[c].decimal_point = from_utf8(value);
6024 // dummy stuff just to avoid warnings
6025 case Tabular::LAST_ACTION:
6031 bool InsetTabular::copySelection(Cursor & cur)
6033 if (!cur.selection())
6038 getSelection(cur, rs, re, cs, ce);
6040 paste_tabular.reset(new Tabular(tabular));
6042 for (row_type r = 0; r < rs; ++r)
6043 paste_tabular->deleteRow(0);
6045 row_type const rows = re - rs + 1;
6046 while (paste_tabular->nrows() > rows)
6047 paste_tabular->deleteRow(rows);
6049 for (col_type c = 0; c < cs; ++c)
6050 paste_tabular->deleteColumn(0);
6052 col_type const columns = ce - cs + 1;
6053 while (paste_tabular->ncols() > columns)
6054 paste_tabular->deleteColumn(columns);
6056 paste_tabular->setBuffer(tabular.buffer());
6058 odocstringstream os;
6059 OutputParams const runparams(0);
6060 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6061 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6062 cap::copySelection(cur, os.str());
6064 // mark tabular stack dirty
6065 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6066 // when we (hopefully) have a one-for-all paste mechanism.
6067 // This must be called after cap::copySelection.
6068 dirtyTabularStack(true);
6074 bool InsetTabular::pasteClipboard(Cursor & cur)
6078 col_type actcol = tabular.cellColumn(cur.idx());
6079 row_type actrow = tabular.cellRow(cur.idx());
6081 if (cur.selIsMultiCell()) {
6084 getSelection(cur, actrow, re, actcol, ce);
6087 for (row_type r1 = 0, r2 = actrow;
6088 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6090 for (col_type c1 = 0, c2 = actcol;
6091 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6093 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6094 tabular.isPartOfMultiColumn(r2, c2))
6096 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6100 if (tabular.isPartOfMultiColumn(r2, c2)) {
6104 shared_ptr<InsetTableCell> inset(
6105 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6106 tabular.setCellInset(r2, c2, inset);
6107 // FIXME?: why do we need to do this explicitly? (EL)
6108 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6110 // FIXME: change tracking (MG)
6111 inset->setChange(Change(buffer().params().track_changes ?
6112 Change::INSERTED : Change::UNCHANGED));
6120 void InsetTabular::cutSelection(Cursor & cur)
6122 if (!cur.selection())
6127 getSelection(cur, rs, re, cs, ce);
6128 for (row_type r = rs; r <= re; ++r) {
6129 for (col_type c = cs; c <= ce; ++c) {
6130 shared_ptr<InsetTableCell> t
6131 = cell(tabular.cellIndex(r, c));
6132 if (buffer().params().track_changes)
6133 // FIXME: Change tracking (MG)
6134 t->setChange(Change(Change::DELETED));
6140 // cursor position might be invalid now
6141 if (cur.pit() > cur.lastpit())
6142 cur.pit() = cur.lastpit();
6143 if (cur.pos() > cur.lastpos())
6144 cur.pos() = cur.lastpos();
6145 cur.clearSelection();
6149 bool InsetTabular::isRightToLeft(Cursor & cur) const
6151 // LASSERT: It might be better to abandon this Buffer.
6152 LASSERT(cur.depth() > 1, return false);
6153 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6154 pos_type const parentpos = cur[cur.depth() - 2].pos();
6155 return parentpar.getFontSettings(buffer().params(),
6156 parentpos).language()->rightToLeft();
6160 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6163 LASSERT(stidx <= enidx, return docstring());
6165 col_type const col1 = tabular.cellColumn(stidx);
6166 col_type const col2 = tabular.cellColumn(enidx);
6167 row_type const row1 = tabular.cellRow(stidx);
6168 row_type const row2 = tabular.cellRow(enidx);
6170 for (col_type col = col1; col <= col2; col++)
6171 for (row_type row = row1; row <= row2; row++) {
6176 retval += tabular.cellInset(row, col)->asString(intoInsets);
6182 void InsetTabular::getSelection(Cursor & cur,
6183 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6185 CursorSlice const & beg = cur.selBegin();
6186 CursorSlice const & end = cur.selEnd();
6187 cs = tabular.cellColumn(beg.idx());
6188 ce = tabular.cellColumn(end.idx());
6192 rs = tabular.cellRow(beg.idx());
6193 re = tabular.cellRow(end.idx());
6199 Text * InsetTabular::getText(int idx) const
6201 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6205 void InsetTabular::setChange(Change const & change)
6207 for (idx_type idx = 0; idx < nargs(); ++idx)
6208 cell(idx)->setChange(change);
6212 void InsetTabular::acceptChanges()
6214 for (idx_type idx = 0; idx < nargs(); ++idx)
6215 cell(idx)->acceptChanges();
6219 void InsetTabular::rejectChanges()
6221 for (idx_type idx = 0; idx < nargs(); ++idx)
6222 cell(idx)->rejectChanges();
6226 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6228 return tabular.getPWidth(cell).zero();
6232 bool InsetTabular::forcePlainLayout(idx_type cell) const
6234 return !tabular.getPWidth(cell).zero();
6238 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6241 if (buf.length() <= 0)
6246 col_type maxCols = 1;
6247 size_t const len = buf.length();
6251 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6259 maxCols = max(cols, maxCols);
6265 maxCols = max(cols, maxCols);
6271 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6272 loctab = paste_tabular.get();
6273 dirtyTabularStack(true);
6276 cell = bv.cursor().idx();
6277 ocol = tabular.cellColumn(cell);
6278 row = tabular.cellRow(cell);
6282 idx_type const cells = loctab->numberofcells;
6285 rows = loctab->nrows();
6286 col_type const columns = loctab->ncols();
6288 while (cell < cells && p < len && row < rows &&
6289 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6295 // we can only set this if we are not too far right
6296 if (cols < columns) {
6297 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6298 Font const font = bv.textMetrics(&inset->text()).
6300 inset->setText(buf.substr(op, p - op), font,
6301 buffer().params().track_changes);
6307 // we can only set this if we are not too far right
6308 if (cols < columns) {
6309 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6310 Font const font = bv.textMetrics(&inset->text()).
6312 inset->setText(buf.substr(op, p - op), font,
6313 buffer().params().track_changes);
6318 cell = loctab->cellIndex(row, cols);
6324 // check for the last cell if there is no trailing '\n'
6325 if (cell < cells && op < len) {
6326 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6327 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6328 inset->setText(buf.substr(op, len - op), font,
6329 buffer().params().track_changes);
6335 void InsetTabular::addPreview(DocIterator const & inset_pos,
6336 PreviewLoader & loader) const
6338 DocIterator cell_pos = inset_pos;
6340 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6341 for (row_type r = 0; r < tabular.nrows(); ++r) {
6342 for (col_type c = 0; c < tabular.ncols(); ++c) {
6343 cell_pos.top().idx() = tabular.cellIndex(r, c);
6344 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6350 bool InsetTabular::completionSupported(Cursor const & cur) const
6352 Cursor const & bvCur = cur.bv().cursor();
6353 if (&bvCur.inset() != this)
6355 return cur.text()->completionSupported(cur);
6359 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6361 return completionSupported(cur);
6365 bool InsetTabular::automaticInlineCompletion() const
6367 return lyxrc.completion_inline_text;
6371 bool InsetTabular::automaticPopupCompletion() const
6373 return lyxrc.completion_popup_text;
6377 bool InsetTabular::showCompletionCursor() const
6379 return lyxrc.completion_cursor_text;
6383 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6385 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6389 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6391 if (!completionSupported(cur))
6393 return cur.text()->completionPrefix(cur);
6397 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6399 if (!completionSupported(cur))
6402 return cur.text()->insertCompletion(cur, s, finished);
6406 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6407 Dimension & dim) const
6409 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6410 tm.completionPosAndDim(cur, x, y, dim);
6414 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6416 istringstream data(in);
6418 lex.setStream(data);
6425 if (!lex || token != "tabular") {
6426 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6430 // This is part of the inset proper that is usually swallowed
6431 // by Buffer::readInset
6433 if (!lex || token != "Tabular") {
6434 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6442 string InsetTabular::params2string(InsetTabular const & inset)
6445 data << "tabular" << ' ';
6447 data << "\\end_inset\n";
6452 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6454 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6455 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6456 if (!tabular.isPartOfMultiColumn(r,c) &&
6457 !tabular.isPartOfMultiRow(r,c))
6460 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6461 ParagraphList::iterator it = parlist.begin();
6462 ParagraphList::iterator const en = parlist.end();
6463 for (; it != en; ++it)
6464 it->setLayout(dc.plainLayout());