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 Length const Tabular::getPWidth(idx_type cell) const
1327 if (isMultiColumn(cell))
1328 return cellInfo(cell).p_width;
1329 return column_info[cellColumn(cell)].p_width;
1333 Length const Tabular::getMROffset(idx_type cell) const
1335 return cellInfo(cell).mroffset;
1339 int Tabular::textHOffset(idx_type cell) const
1341 // the LaTeX Way :-(
1342 int x = WIDTH_OF_LINE;
1344 int const w = cellWidth(cell) - cellInfo(cell).width;
1346 switch (getAlignment(cell)) {
1347 case LYX_ALIGN_CENTER:
1350 case LYX_ALIGN_RIGHT:
1353 case LYX_ALIGN_DECIMAL: {
1354 // we center when no decimal point
1355 if (cellInfo(cell).decimal_width == 0) {
1359 col_type const c = cellColumn(cell);
1360 int max_dhoffset = 0;
1361 for(row_type r = 0; r < row_info.size() ; ++r) {
1362 idx_type const i = cellIndex(r, c);
1363 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1364 && cellInfo(i).decimal_width != 0)
1365 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1367 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1370 // LYX_ALIGN_LEFT: nothing :-)
1378 int Tabular::textVOffset(idx_type cell) const
1380 int voffset = cellInfo(cell).voffset;
1381 if (isMultiRow(cell)) {
1382 row_type const row = cellRow(cell);
1383 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1389 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1392 idx_type const numcells = numberOfCellsInRow(row);
1393 // we check against numcells to make sure we do not crash if all the
1394 // cells are multirow (bug #7535), but in that case our return value
1395 // is really invalid, i.e., it is NOT the first cell in the row. but
1396 // i do not know what to do here. (rgh)
1397 while (c < numcells - 1
1398 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1400 return cell_info[row][c].cellno;
1404 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1406 col_type c = ncols() - 1;
1407 // of course we check against 0 so we don't crash. but we have the same
1408 // problem as in the previous routine: if all the cells are part of a
1409 // multirow or part of a multi column, then our return value is invalid.
1411 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1412 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1414 return cell_info[row][c].cellno;
1418 Tabular::row_type Tabular::cellRow(idx_type cell) const
1420 if (cell >= numberofcells)
1424 return rowofcell[cell];
1428 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1430 if (cell >= numberofcells)
1434 return columnofcell[cell];
1438 void Tabular::write(ostream & os) const
1442 << write_attribute("version", 3)
1443 << write_attribute("rows", nrows())
1444 << write_attribute("columns", ncols())
1446 // global longtable options
1448 << write_attribute("rotate", rotate)
1449 << write_attribute("booktabs", use_booktabs)
1450 << write_attribute("islongtable", is_long_tabular)
1451 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1452 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1453 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1454 << write_attribute("headTopDL", endhead.topDL)
1455 << write_attribute("headBottomDL", endhead.bottomDL)
1456 << write_attribute("footTopDL", endfoot.topDL)
1457 << write_attribute("footBottomDL", endfoot.bottomDL)
1458 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1459 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1460 << write_attribute("lastFootEmpty", endlastfoot.empty);
1461 // longtables cannot be aligned vertically
1462 if (!is_long_tabular) {
1463 os << write_attribute("tabularvalignment", tabular_valignment);
1464 os << write_attribute("tabularwidth", tabular_width);
1466 if (is_long_tabular)
1467 os << write_attribute("longtabularalignment", longtabular_alignment);
1469 for (col_type c = 0; c < ncols(); ++c) {
1471 << write_attribute("alignment", column_info[c].alignment);
1472 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1473 os << write_attribute("decimal_point", column_info[c].decimal_point);
1474 os << write_attribute("valignment", column_info[c].valignment)
1475 << write_attribute("width", column_info[c].p_width.asString())
1476 << write_attribute("special", column_info[c].align_special)
1479 for (row_type r = 0; r < nrows(); ++r) {
1480 static const string def("default");
1482 if (row_info[r].top_space_default)
1483 os << write_attribute("topspace", def);
1485 os << write_attribute("topspace", row_info[r].top_space);
1486 if (row_info[r].bottom_space_default)
1487 os << write_attribute("bottomspace", def);
1489 os << write_attribute("bottomspace", row_info[r].bottom_space);
1490 if (row_info[r].interline_space_default)
1491 os << write_attribute("interlinespace", def);
1493 os << write_attribute("interlinespace", row_info[r].interline_space);
1494 os << write_attribute("endhead", row_info[r].endhead)
1495 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1496 << write_attribute("endfoot", row_info[r].endfoot)
1497 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1498 << write_attribute("newpage", row_info[r].newpage)
1499 << write_attribute("caption", row_info[r].caption)
1501 for (col_type c = 0; c < ncols(); ++c) {
1503 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1504 << write_attribute("multirow", cell_info[r][c].multirow)
1505 << write_attribute("mroffset", cell_info[r][c].mroffset)
1506 << write_attribute("alignment", cell_info[r][c].alignment)
1507 << write_attribute("valignment", cell_info[r][c].valignment)
1508 << write_attribute("topline", cell_info[r][c].top_line)
1509 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1510 << write_attribute("leftline", cell_info[r][c].left_line)
1511 << write_attribute("rightline", cell_info[r][c].right_line)
1512 << write_attribute("rotate", cell_info[r][c].rotate)
1513 << write_attribute("usebox", cell_info[r][c].usebox)
1514 << write_attribute("width", cell_info[r][c].p_width)
1515 << write_attribute("special", cell_info[r][c].align_special)
1517 os << "\\begin_inset ";
1518 cell_info[r][c].inset->write(os);
1519 os << "\n\\end_inset\n"
1521 // FIXME This can be removed again once the mystery
1522 // crash has been resolved.
1527 os << "</lyxtabular>\n";
1531 void Tabular::read(Lexer & lex)
1534 istream & is = lex.getStream();
1536 l_getline(is, line);
1537 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1538 LASSERT(false, return);
1542 if (!getTokenValue(line, "version", version))
1544 LATTEST(version >= 2);
1547 if (!getTokenValue(line, "rows", rows_arg))
1550 if (!getTokenValue(line, "columns", columns_arg))
1552 init(buffer_, rows_arg, columns_arg);
1553 l_getline(is, line);
1554 if (!prefixIs(line, "<features")) {
1555 lyxerr << "Wrong tabular format (expected <features ...> got"
1556 << line << ')' << endl;
1559 getTokenValue(line, "rotate", rotate);
1560 getTokenValue(line, "booktabs", use_booktabs);
1561 getTokenValue(line, "islongtable", is_long_tabular);
1562 getTokenValue(line, "tabularvalignment", tabular_valignment);
1563 getTokenValue(line, "tabularwidth", tabular_width);
1564 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1565 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1566 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1567 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1568 getTokenValue(line, "headTopDL", endhead.topDL);
1569 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1570 getTokenValue(line, "footTopDL", endfoot.topDL);
1571 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1572 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1573 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1574 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1576 for (col_type c = 0; c < ncols(); ++c) {
1578 if (!prefixIs(line,"<column")) {
1579 lyxerr << "Wrong tabular format (expected <column ...> got"
1580 << line << ')' << endl;
1583 getTokenValue(line, "alignment", column_info[c].alignment);
1584 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1585 getTokenValue(line, "valignment", column_info[c].valignment);
1586 getTokenValue(line, "width", column_info[c].p_width);
1587 getTokenValue(line, "special", column_info[c].align_special);
1590 for (row_type i = 0; i < nrows(); ++i) {
1591 l_getline(is, line);
1592 if (!prefixIs(line, "<row")) {
1593 lyxerr << "Wrong tabular format (expected <row ...> got"
1594 << line << ')' << endl;
1597 getTokenValue(line, "topspace", row_info[i].top_space,
1598 row_info[i].top_space_default);
1599 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1600 row_info[i].bottom_space_default);
1601 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1602 row_info[i].interline_space_default);
1603 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1604 getTokenValue(line, "endhead", row_info[i].endhead);
1605 getTokenValue(line, "endfoot", row_info[i].endfoot);
1606 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1607 getTokenValue(line, "newpage", row_info[i].newpage);
1608 getTokenValue(line, "caption", row_info[i].caption);
1609 for (col_type j = 0; j < ncols(); ++j) {
1610 l_getline(is, line);
1611 if (!prefixIs(line, "<cell")) {
1612 lyxerr << "Wrong tabular format (expected <cell ...> got"
1613 << line << ')' << endl;
1616 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1617 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1618 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1619 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1620 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1621 getTokenValue(line, "topline", cell_info[i][j].top_line);
1622 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1623 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1624 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1625 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1626 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1627 getTokenValue(line, "width", cell_info[i][j].p_width);
1629 getTokenValue(line, "special", cell_info[i][j].align_special);
1630 l_getline(is, line);
1631 if (prefixIs(line, "\\begin_inset")) {
1632 cell_info[i][j].inset->setBuffer(*buffer_);
1633 cell_info[i][j].inset->read(lex);
1634 l_getline(is, line);
1636 if (!prefixIs(line, "</cell>")) {
1637 lyxerr << "Wrong tabular format (expected </cell> got"
1638 << line << ')' << endl;
1642 l_getline(is, line);
1643 if (!prefixIs(line, "</row>")) {
1644 lyxerr << "Wrong tabular format (expected </row> got"
1645 << line << ')' << endl;
1649 while (!prefixIs(line, "</lyxtabular>")) {
1650 l_getline(is, line);
1656 bool Tabular::isMultiColumn(idx_type cell) const
1658 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1659 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1663 bool Tabular::hasMultiColumn(col_type c) const
1665 for (row_type r = 0; r < nrows(); ++r) {
1666 if (isMultiColumn(cellIndex(r, c)))
1673 Tabular::CellData const & Tabular::cellInfo(idx_type cell) const
1675 return cell_info[cellRow(cell)][cellColumn(cell)];
1679 Tabular::CellData & Tabular::cellInfo(idx_type cell)
1681 return cell_info[cellRow(cell)][cellColumn(cell)];
1685 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1686 bool const right_border)
1688 idx_type const col = cellColumn(cell);
1689 idx_type const row = cellRow(cell);
1690 for (idx_type i = 0; i < number; ++i)
1691 unsetMultiRow(cellIndex(row, col + i));
1693 // unsetting of multirow may have invalidated cell index
1694 cell = cellIndex(row, col);
1695 CellData & cs = cellInfo(cell);
1696 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1697 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1698 cs.alignment = column_info[col].alignment;
1699 setRightLine(cell, right_border);
1701 idx_type lastcell = cellIndex(row, col + number - 1);
1702 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1703 CellData & cs1 = cellInfo(cell + i);
1704 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1705 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1713 bool Tabular::isMultiRow(idx_type cell) const
1715 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1716 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1719 bool Tabular::hasMultiRow(row_type r) const
1721 for (col_type c = 0; c < ncols(); ++c) {
1722 if (isMultiRow(cellIndex(r, c)))
1728 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1729 bool const bottom_border,
1730 LyXAlignment const halign)
1732 idx_type const col = cellColumn(cell);
1733 idx_type const row = cellRow(cell);
1734 for (idx_type i = 0; i < number; ++i)
1735 unsetMultiColumn(cellIndex(row + i, col));
1737 // unsetting of multirow may have invalidated cell index
1738 cell = cellIndex(row, col);
1739 CellData & cs = cellInfo(cell);
1740 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1741 cs.valignment = LYX_VALIGN_MIDDLE;
1742 // the horizontal alignment of multirow cells can only
1743 // be changed for the whole table row,
1744 // support changing this only for the multirow cell can be done via
1746 if (getPWidth(cell).zero())
1747 cs.alignment = halign;
1749 cs.alignment = LYX_ALIGN_LEFT;
1751 // set the bottom line of the last selected cell
1752 setBottomLine(cell, bottom_border);
1754 for (idx_type i = 1; i < number; ++i) {
1755 CellData & cs1 = cell_info[row + i][col];
1756 cs1.multirow = CELL_PART_OF_MULTIROW;
1757 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1765 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1767 row_type const row = cellRow(cell);
1768 col_type const col = cellColumn(cell);
1770 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1777 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1779 col_type const column = cellColumn(cell);
1780 col_type row = cellRow(cell) + 1;
1781 while (row < nrows() && isPartOfMultiRow(row, column))
1784 return row - cellRow(cell);
1788 void Tabular::unsetMultiColumn(idx_type cell)
1790 if (!isMultiColumn(cell))
1793 row_type const row = cellRow(cell);
1794 col_type const col = cellColumn(cell);
1795 row_type const span = columnSpan(cell);
1796 for (col_type c = 0; c < span; ++c) {
1797 // in the table dialog the lines are set in every case
1798 // when unsetting a multicolumn this leads to an additional right
1799 // line for every cell that was part of the former multicolumn cell,
1800 // except if the cell is in the last column
1801 // therefore remove this line
1802 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1803 && (col + c) < (col + span - 1))
1804 cell_info[row][col + c].right_line = false;
1805 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1811 void Tabular::unsetMultiRow(idx_type cell)
1813 if (!isMultiRow(cell))
1816 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1817 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1818 row_type const row = cellRow(cell);
1819 col_type const col = cellColumn(cell);
1820 row_type const span = rowSpan(cell);
1821 for (row_type r = 0; r < span; ++r)
1822 cell_info[row + r][col].multirow = CELL_NORMAL;
1827 void Tabular::setRotateCell(idx_type cell, int value)
1829 cellInfo(cell).rotate = value;
1833 int Tabular::getRotateCell(idx_type cell) const
1835 return cellInfo(cell).rotate;
1839 bool Tabular::needRotating() const
1843 for (row_type r = 0; r < nrows(); ++r)
1844 for (col_type c = 0; c < ncols(); ++c)
1845 if (cell_info[r][c].rotate != 0)
1851 bool Tabular::isLastCell(idx_type cell) const
1853 if (cell + 1 < numberofcells)
1859 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1861 if (cellRow(cell) == 0)
1864 col_type const col = cellColumn(cell);
1865 row_type r = cellRow(cell) - 1;
1866 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1869 return cell_info[r][col].cellno;
1873 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1875 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1876 if (nextrow < nrows())
1877 return cell_info[nextrow][cellColumn(cell)].cellno;
1882 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1884 LASSERT(column != npos && column < ncols(), column = 0);
1885 LASSERT(row != npos && row < nrows(), row = 0);
1886 return cell_info[row][column].cellno;
1890 void Tabular::setUsebox(idx_type cell, BoxType type)
1892 cellInfo(cell).usebox = type;
1896 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1897 // adds line breaks, see bug 4886.
1898 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1900 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1901 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1903 if (cellInfo(cell).usebox > 1)
1904 return cellInfo(cell).usebox;
1905 return useParbox(cell);
1910 // This are functions used for the longtable support
1912 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1918 row_info[row].endfirsthead = flag;
1922 row_info[row].endhead = flag;
1927 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1930 hd.set = haveLTHead();
1931 return row_info[row].endhead;
1935 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1938 hd.set = haveLTFirstHead();
1939 return row_info[row].endfirsthead;
1943 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1949 row_info[row].endlastfoot = flag;
1953 row_info[row].endfoot = flag;
1958 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1961 fd.set = haveLTFoot();
1962 return row_info[row].endfoot;
1966 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1969 fd.set = haveLTLastFoot();
1970 return row_info[row].endlastfoot;
1974 void Tabular::setLTNewPage(row_type row, bool what)
1976 row_info[row].newpage = what;
1980 bool Tabular::getLTNewPage(row_type row) const
1982 return row_info[row].newpage;
1986 bool Tabular::haveLTHead(bool withcaptions) const
1988 if (!is_long_tabular)
1990 for (row_type i = 0; i < nrows(); ++i)
1991 if (row_info[i].endhead &&
1992 (withcaptions || !row_info[i].caption))
1998 bool Tabular::haveLTFirstHead(bool withcaptions) const
2000 if (!is_long_tabular || endfirsthead.empty)
2002 for (row_type r = 0; r < nrows(); ++r)
2003 if (row_info[r].endfirsthead &&
2004 (withcaptions || !row_info[r].caption))
2010 bool Tabular::haveLTFoot(bool withcaptions) const
2012 if (!is_long_tabular)
2014 for (row_type r = 0; r < nrows(); ++r)
2015 if (row_info[r].endfoot &&
2016 (withcaptions || !row_info[r].caption))
2022 bool Tabular::haveLTLastFoot(bool withcaptions) const
2024 if (!is_long_tabular || endlastfoot.empty)
2026 for (row_type r = 0; r < nrows(); ++r)
2027 if (row_info[r].endlastfoot &&
2028 (withcaptions || !row_info[r].caption))
2034 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2036 idx_type i = getFirstCellInRow(row);
2038 setMultiColumn(i, numberOfCellsInRow(row), false);
2039 setTopLine(i, false);
2040 setBottomLine(i, false);
2041 setLeftLine(i, false);
2042 setRightLine(i, false);
2043 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2044 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2045 setLTHead(row, true, endfirsthead, true);
2046 row_info[row].endfirsthead = true;
2049 unsetMultiColumn(i);
2050 // When unsetting a caption row, also all existing
2051 // captions in this row must be dissolved.
2053 row_info[row].caption = what;
2058 bool Tabular::ltCaption(row_type row) const
2060 return row_info[row].caption;
2064 bool Tabular::haveLTCaption(CaptionType captiontype) const
2066 if (!is_long_tabular)
2068 for (row_type r = 0; r < nrows(); ++r) {
2069 if (row_info[r].caption) {
2070 switch (captiontype) {
2071 case CAPTION_FIRSTHEAD:
2072 if (row_info[r].endfirsthead)
2076 if (row_info[r].endhead)
2080 if (row_info[r].endfoot)
2083 case CAPTION_LASTFOOT:
2084 if (row_info[r].endlastfoot)
2096 // end longtable support functions
2098 void Tabular::setRowAscent(row_type row, int height)
2100 if (row >= nrows() || row_info[row].ascent == height)
2102 row_info[row].ascent = height;
2106 void Tabular::setRowDescent(row_type row, int height)
2108 if (row >= nrows() || row_info[row].descent == height)
2110 row_info[row].descent = height;
2114 int Tabular::rowAscent(row_type row) const
2116 LASSERT(row < nrows(), row = 0);
2117 return row_info[row].ascent;
2121 int Tabular::rowDescent(row_type row) const
2123 LASSERT(row < nrows(), row = 0);
2124 return row_info[row].descent;
2128 int Tabular::height() const
2131 for (row_type row = 0; row < nrows(); ++row)
2132 height += rowAscent(row) + rowDescent(row) +
2138 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2140 LASSERT(row < nrows(), return false);
2141 LASSERT(column < ncols(), return false);
2142 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2146 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2148 LASSERT(row < nrows(), return false);
2149 LASSERT(column < ncols(), return false);
2150 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2154 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2156 // we only output complete row lines and the 1st row here, the rest
2157 // is done in Tabular::TeXBottomHLine(...)
2159 // get for each column the topline (if any)
2160 vector<bool> topline;
2162 for (col_type c = 0; c < ncols(); ++c) {
2163 topline.push_back(topLine(cellIndex(row, c)));
2164 // If cell is part of a multirow and not the first cell of the
2165 // multirow, no line must be drawn.
2167 if (isMultiRow(cellIndex(row, c))
2168 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2174 // do nothing if empty first row, or incomplete row line after
2175 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2178 // only output complete row lines and the 1st row's clines
2179 if (nset == ncols()) {
2181 os << (row == 0 ? "\\toprule " : "\\midrule ");
2185 } else if (row == 0) {
2186 for (col_type c = 0; c < ncols(); ++c) {
2188 col_type offset = 0;
2189 for (col_type j = 0 ; j < c; ++j)
2190 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2193 //babel makes the "-" character an active one, so we have to suppress this here
2194 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2195 if (lang == "slovak" || lang == "czech")
2196 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2197 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2199 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2201 col_type cstart = c;
2202 for ( ; c < ncols() && topline[c]; ++c) {}
2204 for (col_type j = cstart ; j < c ; ++j)
2205 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2208 os << c + offset << "} ";
2216 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2218 // we output bottomlines of row r and the toplines of row r+1
2219 // if the latter do not span the whole tabular
2221 // get the bottomlines of row r, and toplines in next row
2222 bool lastrow = row == nrows() - 1;
2223 vector<bool> bottomline, topline;
2224 bool nextrowset = true;
2225 for (col_type c = 0; c < ncols(); ++c) {
2226 bottomline.push_back(bottomLine(cellIndex(row, c)));
2227 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2228 // If cell is part of a multirow and not the last cell of the
2229 // multirow, no line must be drawn.
2231 if (isMultiRow(cellIndex(row, c))
2232 && isMultiRow(cellIndex(row + 1, c))
2233 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2234 bottomline[c] = false;
2237 nextrowset &= topline[c];
2240 // combine this row's bottom lines and next row's toplines if necessary
2242 for (col_type c = 0; c < ncols(); ++c) {
2244 bottomline[c] = bottomline[c] || topline[c];
2249 // do nothing if empty, OR incomplete row line with a topline in next row
2250 if (nset == 0 || (nextrowset && nset != ncols()))
2253 if (nset == ncols()) {
2255 os << (lastrow ? "\\bottomrule" : "\\midrule");
2259 for (col_type c = 0; c < ncols(); ++c) {
2260 if (bottomline[c]) {
2261 col_type offset = 0;
2262 for (col_type j = 0 ; j < c; ++j)
2263 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2266 //babel makes the "-" character an active one, so we have to suppress this here
2267 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2268 if (lang == "slovak" || lang == "czech")
2269 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2270 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2272 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2274 col_type cstart = c;
2275 for ( ; c < ncols() && bottomline[c]; ++c) {}
2277 for (col_type j = cstart ; j < c ; ++j)
2278 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2281 os << c + offset << "} ";
2289 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2290 bool & ismulticol, bool & ismultirow) const
2292 row_type const r = cellRow(cell);
2293 if (is_long_tabular && row_info[r].caption)
2296 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2297 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2298 // figure out how to set the lines
2299 // we always set double lines to the right of the cell
2300 col_type const c = cellColumn(cell);
2301 col_type const nextcol = c + columnSpan(cell);
2302 bool colright = columnRightLine(c);
2303 bool colleft = columnLeftLine(c);
2304 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2305 bool nextcellleft = nextcol < ncols()
2306 && leftLine(cellIndex(r, nextcol));
2307 bool coldouble = colright && nextcolleft;
2308 bool celldouble = rightLine(cell) && nextcellleft;
2310 ismulticol = isMultiColumn(cell)
2311 || (c == 0 && colleft != leftLine(cell))
2312 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2313 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2314 || (coldouble != celldouble);
2316 // we center in multicol when no decimal point
2317 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2318 docstring const align_d = column_info[c].decimal_point;
2319 DocIterator const dit = separatorPos(cellInset(cell), align_d);
2323 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2324 int latexcolspan = columnSpan(cell);
2325 for(col_type col = c; col < c + columnSpan(cell); ++col)
2326 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2330 os << "\\multicolumn{" << latexcolspan << "}{";
2331 if (c ==0 && leftLine(cell))
2333 if (!cellInfo(cell).align_special.empty()) {
2334 os << cellInfo(cell).align_special;
2336 if (!getPWidth(cell).zero()) {
2338 case LYX_ALIGN_LEFT:
2339 os << ">{\\raggedright}";
2341 case LYX_ALIGN_RIGHT:
2342 os << ">{\\raggedleft}";
2344 case LYX_ALIGN_CENTER:
2345 os << ">{\\centering}";
2351 case LYX_VALIGN_TOP:
2354 case LYX_VALIGN_MIDDLE:
2357 case LYX_VALIGN_BOTTOM:
2362 << from_ascii(getPWidth(cell).asLatexString())
2366 case LYX_ALIGN_LEFT:
2369 case LYX_ALIGN_RIGHT:
2376 } // end if else !getPWidth
2377 } // end if else !cellinfo_of_cell
2378 if (rightLine(cell) || nextcellleft)
2381 // add extra vertical line if we want a double one
2384 } // end if ismulticol
2386 // we only need code for the first multirow cell
2387 ismultirow = isMultiRow(cell);
2389 os << "\\multirow{" << rowSpan(cell) << "}{";
2390 if (!getPWidth(cell).zero())
2391 os << from_ascii(getPWidth(cell).asLatexString());
2393 // we need to set a default value
2396 if (!getMROffset(cell).zero())
2397 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2399 } // end if ismultirow
2401 if (getRotateCell(cell) != 0)
2402 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2404 if (getUsebox(cell) == BOX_PARBOX) {
2407 case LYX_VALIGN_TOP:
2410 case LYX_VALIGN_MIDDLE:
2413 case LYX_VALIGN_BOTTOM:
2417 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2419 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2420 os << "\\begin{minipage}[";
2422 case LYX_VALIGN_TOP:
2425 case LYX_VALIGN_MIDDLE:
2428 case LYX_VALIGN_BOTTOM:
2432 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2438 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2439 bool ismulticol, bool ismultirow) const
2441 row_type const r = cellRow(cell);
2442 if (is_long_tabular && row_info[r].caption)
2446 if (getUsebox(cell) == BOX_PARBOX)
2448 else if (getUsebox(cell) == BOX_MINIPAGE)
2449 os << breakln << "\\end{minipage}";
2450 if (getRotateCell(cell) != 0)
2451 os << breakln << "\\end{turn}";
2459 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2460 OutputParams const & runparams) const
2462 if (!is_long_tabular)
2466 // output caption which is in no header or footer
2467 if (haveLTCaption()) {
2468 for (row_type r = 0; r < nrows(); ++r) {
2469 if (row_info[r].caption &&
2470 !row_info[r].endfirsthead && !row_info[r].endhead &&
2471 !row_info[r].endfoot && !row_info[r].endlastfoot)
2472 TeXRow(os, r, runparams);
2475 // output first header info
2476 if (haveLTFirstHead()) {
2477 if (endfirsthead.topDL)
2479 for (row_type r = 0; r < nrows(); ++r) {
2480 if (row_info[r].endfirsthead)
2481 TeXRow(os, r, runparams);
2483 if (endfirsthead.bottomDL)
2485 os << "\\endfirsthead\n";
2487 // output header info
2489 if (endfirsthead.empty && !haveLTFirstHead())
2490 os << "\\endfirsthead\n";
2493 for (row_type r = 0; r < nrows(); ++r) {
2494 if (row_info[r].endhead)
2495 TeXRow(os, r, runparams);
2497 if (endhead.bottomDL)
2499 os << "\\endhead\n";
2501 // output footer info
2505 for (row_type r = 0; r < nrows(); ++r) {
2506 if (row_info[r].endfoot)
2507 TeXRow(os, r, runparams);
2509 if (endfoot.bottomDL)
2511 os << "\\endfoot\n";
2512 if (endlastfoot.empty && !haveLTLastFoot())
2513 os << "\\endlastfoot\n";
2515 // output lastfooter info
2516 if (haveLTLastFoot()) {
2517 if (endlastfoot.topDL)
2519 for (row_type r = 0; r < nrows(); ++r) {
2520 if (row_info[r].endlastfoot)
2521 TeXRow(os, r, runparams);
2523 if (endlastfoot.bottomDL)
2525 os << "\\endlastfoot\n";
2530 bool Tabular::isValidRow(row_type row) const
2532 if (!is_long_tabular)
2534 return !row_info[row].endhead && !row_info[row].endfirsthead
2535 && !row_info[row].endfoot && !row_info[row].endlastfoot
2536 && !row_info[row].caption;
2540 void Tabular::TeXRow(otexstream & os, row_type row,
2541 OutputParams const & runparams) const
2543 idx_type cell = cellIndex(row, 0);
2544 InsetTableCell const * inset = cellInset(cell);
2545 Paragraph const & par = inset->paragraphs().front();
2546 string const lang = par.getParLanguage(buffer().params())->lang();
2548 //output the top line
2549 TeXTopHLine(os, row, lang);
2551 if (row_info[row].top_space_default) {
2553 os << "\\addlinespace\n";
2555 os << "\\noalign{\\vskip\\doublerulesep}\n";
2556 } else if(!row_info[row].top_space.zero()) {
2558 os << "\\addlinespace["
2559 << from_ascii(row_info[row].top_space.asLatexString())
2562 os << "\\noalign{\\vskip"
2563 << from_ascii(row_info[row].top_space.asLatexString())
2567 bool ismulticol = false;
2568 bool ismultirow = false;
2569 for (col_type c = 0; c < ncols(); ++c) {
2570 if (isPartOfMultiColumn(row, c))
2573 cell = cellIndex(row, c);
2575 if (isPartOfMultiRow(row, c)
2576 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2577 if (cell != getLastCellInRow(row))
2582 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2583 InsetTableCell const * inset = cellInset(cell);
2585 Paragraph const & par = inset->paragraphs().front();
2587 os.texrow().forceStart(par.id(), 0);
2589 bool rtl = par.isRTL(buffer().params())
2591 && getPWidth(cell).zero()
2592 && !runparams.use_polyglossia;
2596 par.getParLanguage(buffer().params())->lang();
2597 if (lang == "farsi")
2599 else if (lang == "arabic_arabi")
2601 // currently, remaining RTL languages are
2602 // arabic_arabtex and hebrew
2606 // pass to the OutputParams that we are in a cell and
2607 // which alignment we have set.
2608 // InsetNewline needs this context information.
2609 OutputParams newrp(runparams);
2610 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2611 ? OutputParams::PLAIN
2612 : OutputParams::ALIGNED;
2614 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2615 // copy cell and split in 2
2616 InsetTableCell head = InsetTableCell(*cellInset(cell));
2617 head.setBuffer(const_cast<Buffer &>(buffer()));
2618 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2620 dit.push_back(CursorSlice(head));
2621 head.setMacrocontextPositionRecursive(dit);
2622 bool hassep = false;
2623 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2624 head.latex(os, newrp);
2627 tail.setBuffer(head.buffer());
2629 dit.push_back(CursorSlice(tail));
2630 tail.setMacrocontextPositionRecursive(dit);
2631 tail.latex(os, newrp);
2633 } else if (!isPartOfMultiRow(row, c)) {
2634 if (!runparams.nice)
2635 os.texrow().start(par.id(), 0);
2636 inset->latex(os, newrp);
2639 runparams.encoding = newrp.encoding;
2643 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2644 if (cell != getLastCellInRow(row)) { // not last cell in row
2651 os << "\\tabularnewline";
2652 if (row_info[row].bottom_space_default) {
2654 os << "\\addlinespace";
2656 os << "[\\doublerulesep]";
2657 } else if (!row_info[row].bottom_space.zero()) {
2659 os << "\\addlinespace";
2661 << from_ascii(row_info[row].bottom_space.asLatexString())
2666 //output the bottom line
2667 TeXBottomHLine(os, row, lang);
2669 if (row_info[row].interline_space_default) {
2671 os << "\\addlinespace\n";
2673 os << "\\noalign{\\vskip\\doublerulesep}\n";
2674 } else if (!row_info[row].interline_space.zero()) {
2676 os << "\\addlinespace["
2677 << from_ascii(row_info[row].interline_space.asLatexString())
2680 os << "\\noalign{\\vskip"
2681 << from_ascii(row_info[row].interline_space.asLatexString())
2687 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2689 bool const is_tabular_star = !tabular_width.zero();
2690 RowEntry pos = TexRow::textEntry(runparams.lastid, runparams.lastpos);
2692 //+---------------------------------------------------------------------
2693 //+ first the opening preamble +
2694 //+---------------------------------------------------------------------
2697 if (!TexRow::isNone(pos))
2698 os.texrow().start(pos);
2701 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2703 if (is_long_tabular) {
2704 os << "\\begin{longtable}";
2705 switch (longtabular_alignment) {
2706 case LYX_LONGTABULAR_ALIGN_LEFT:
2709 case LYX_LONGTABULAR_ALIGN_CENTER:
2711 case LYX_LONGTABULAR_ALIGN_RIGHT:
2716 if (is_tabular_star)
2717 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2719 os << "\\begin{tabular}";
2720 switch (tabular_valignment) {
2721 case LYX_VALIGN_TOP:
2724 case LYX_VALIGN_MIDDLE:
2726 case LYX_VALIGN_BOTTOM:
2734 if (is_tabular_star)
2735 os << "@{\\extracolsep{\\fill}}";
2737 for (col_type c = 0; c < ncols(); ++c) {
2738 if (columnLeftLine(c))
2740 if (!column_info[c].align_special.empty()) {
2741 os << column_info[c].align_special;
2743 if (!column_info[c].p_width.zero()) {
2744 switch (column_info[c].alignment) {
2745 case LYX_ALIGN_LEFT:
2746 os << ">{\\raggedright}";
2748 case LYX_ALIGN_RIGHT:
2749 os << ">{\\raggedleft}";
2751 case LYX_ALIGN_CENTER:
2752 os << ">{\\centering}";
2754 case LYX_ALIGN_NONE:
2755 case LYX_ALIGN_BLOCK:
2756 case LYX_ALIGN_LAYOUT:
2757 case LYX_ALIGN_SPECIAL:
2758 case LYX_ALIGN_DECIMAL:
2762 switch (column_info[c].valignment) {
2763 case LYX_VALIGN_TOP:
2766 case LYX_VALIGN_MIDDLE:
2769 case LYX_VALIGN_BOTTOM:
2774 << from_ascii(column_info[c].p_width.asLatexString())
2777 switch (column_info[c].alignment) {
2778 case LYX_ALIGN_LEFT:
2781 case LYX_ALIGN_RIGHT:
2784 case LYX_ALIGN_DECIMAL:
2785 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2791 } // end if else !column_info[i].p_width
2792 } // end if else !column_info[i].align_special
2793 if (columnRightLine(c))
2798 TeXLongtableHeaderFooter(os, runparams);
2800 //+---------------------------------------------------------------------
2801 //+ the single row and columns (cells) +
2802 //+---------------------------------------------------------------------
2804 for (row_type r = 0; r < nrows(); ++r) {
2805 if (isValidRow(r)) {
2806 TeXRow(os, r, runparams);
2807 if (is_long_tabular && row_info[r].newpage)
2808 os << "\\newpage\n";
2812 //+---------------------------------------------------------------------
2813 //+ the closing of the tabular +
2814 //+---------------------------------------------------------------------
2816 if (is_long_tabular)
2817 os << "\\end{longtable}";
2819 if (is_tabular_star)
2820 os << "\\end{tabular*}";
2822 os << "\\end{tabular}";
2826 os << breakln << "\\end{turn}";
2828 if (!TexRow::isNone(pos))
2829 os.texrow().start(pos);
2833 int Tabular::docbookRow(odocstream & os, row_type row,
2834 OutputParams const & runparams) const
2837 idx_type cell = getFirstCellInRow(row);
2840 for (col_type c = 0; c < ncols(); ++c) {
2841 if (isPartOfMultiColumn(row, c))
2844 os << "<entry align=\"";
2845 switch (getAlignment(cell)) {
2846 case LYX_ALIGN_LEFT:
2849 case LYX_ALIGN_RIGHT:
2857 os << "\" valign=\"";
2858 switch (getVAlignment(cell)) {
2859 case LYX_VALIGN_TOP:
2862 case LYX_VALIGN_BOTTOM:
2865 case LYX_VALIGN_MIDDLE:
2870 if (isMultiColumn(cell)) {
2871 os << " namest=\"col" << c << "\" ";
2872 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2876 ret += cellInset(cell)->docbook(os, runparams);
2885 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2889 //+---------------------------------------------------------------------
2890 //+ first the opening preamble +
2891 //+---------------------------------------------------------------------
2893 os << "<tgroup cols=\"" << ncols()
2894 << "\" colsep=\"1\" rowsep=\"1\">\n";
2896 for (col_type c = 0; c < ncols(); ++c) {
2897 os << "<colspec colname=\"col" << c << "\" align=\"";
2898 switch (column_info[c].alignment) {
2899 case LYX_ALIGN_LEFT:
2902 case LYX_ALIGN_RIGHT:
2910 if (runparams.flavor == OutputParams::XML)
2916 //+---------------------------------------------------------------------
2917 //+ Long Tabular case +
2918 //+---------------------------------------------------------------------
2920 // output caption info
2921 // The caption flag wins over head/foot
2922 if (haveLTCaption()) {
2923 os << "<caption>\n";
2925 for (row_type r = 0; r < nrows(); ++r) {
2926 if (row_info[r].caption) {
2927 ret += docbookRow(os, r, runparams);
2930 os << "</caption>\n";
2933 // output header info
2934 if (haveLTHead(false) || haveLTFirstHead(false)) {
2937 for (row_type r = 0; r < nrows(); ++r) {
2938 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2939 !row_info[r].caption) {
2940 ret += docbookRow(os, r, runparams);
2946 // output footer info
2947 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2950 for (row_type r = 0; r < nrows(); ++r) {
2951 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2952 !row_info[r].caption) {
2953 ret += docbookRow(os, r, runparams);
2960 //+---------------------------------------------------------------------
2961 //+ the single row and columns (cells) +
2962 //+---------------------------------------------------------------------
2966 for (row_type r = 0; r < nrows(); ++r) {
2967 if (isValidRow(r)) {
2968 ret += docbookRow(os, r, runparams);
2973 //+---------------------------------------------------------------------
2974 //+ the closing of the tabular +
2975 //+---------------------------------------------------------------------
2984 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2985 OutputParams const & runparams, bool header) const
2988 string const celltag = header ? "th" : "td";
2989 idx_type cell = getFirstCellInRow(row);
2991 xs << html::StartTag("tr");
2992 for (col_type c = 0; c < ncols(); ++c) {
2993 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2998 Length const cwidth = column_info[c].p_width;
2999 if (!cwidth.zero()) {
3000 string const hwidth = cwidth.asHTMLString();
3001 attr << "style =\"width: " << hwidth << ";\" ";
3005 switch (getAlignment(cell)) {
3006 case LYX_ALIGN_LEFT:
3009 case LYX_ALIGN_RIGHT:
3017 attr << " valign='";
3018 switch (getVAlignment(cell)) {
3019 case LYX_VALIGN_TOP:
3022 case LYX_VALIGN_BOTTOM:
3025 case LYX_VALIGN_MIDDLE:
3030 if (isMultiColumn(cell))
3031 attr << " colspan='" << columnSpan(cell) << "'";
3032 else if (isMultiRow(cell))
3033 attr << " rowspan='" << rowSpan(cell) << "'";
3035 xs << html::StartTag(celltag, attr.str(), true) << html::CR();
3036 ret += cellInset(cell)->xhtml(xs, runparams);
3037 xs << html::EndTag(celltag) << html::CR();
3040 xs << html::EndTag("tr");
3045 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3049 if (is_long_tabular) {
3050 // we'll wrap it in a div, so as to deal with alignment
3052 switch (longtabular_alignment) {
3053 case LYX_LONGTABULAR_ALIGN_LEFT:
3056 case LYX_LONGTABULAR_ALIGN_CENTER:
3059 case LYX_LONGTABULAR_ALIGN_RIGHT:
3063 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3065 // The caption flag wins over head/foot
3066 if (haveLTCaption()) {
3067 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3069 for (row_type r = 0; r < nrows(); ++r)
3070 if (row_info[r].caption)
3071 ret += xhtmlRow(xs, r, runparams);
3072 xs << html::EndTag("div") << html::CR();
3076 xs << html::StartTag("table") << html::CR();
3078 // output header info
3079 bool const havefirsthead = haveLTFirstHead(false);
3080 // if we have a first head, then we are going to ignore the
3081 // headers for the additional pages, since there aren't any
3082 // in XHTML. this test accomplishes that.
3083 bool const havehead = !havefirsthead && haveLTHead(false);
3084 if (havehead || havefirsthead) {
3085 xs << html::StartTag("thead") << html::CR();
3086 for (row_type r = 0; r < nrows(); ++r) {
3087 if (((havefirsthead && row_info[r].endfirsthead) ||
3088 (havehead && row_info[r].endhead)) &&
3089 !row_info[r].caption) {
3090 ret += xhtmlRow(xs, r, runparams, true);
3093 xs << html::EndTag("thead") << html::CR();
3095 // output footer info
3096 bool const havelastfoot = haveLTLastFoot(false);
3098 bool const havefoot = !havelastfoot && haveLTFoot(false);
3099 if (havefoot || havelastfoot) {
3100 xs << html::StartTag("tfoot") << html::CR();
3101 for (row_type r = 0; r < nrows(); ++r) {
3102 if (((havelastfoot && row_info[r].endlastfoot) ||
3103 (havefoot && row_info[r].endfoot)) &&
3104 !row_info[r].caption) {
3105 ret += xhtmlRow(xs, r, runparams);
3108 xs << html::EndTag("tfoot") << html::CR();
3111 xs << html::StartTag("tbody") << html::CR();
3112 for (row_type r = 0; r < nrows(); ++r) {
3113 if (isValidRow(r)) {
3114 ret += xhtmlRow(xs, r, runparams);
3117 xs << html::EndTag("tbody")
3119 << html::EndTag("table")
3121 if (is_long_tabular)
3122 xs << html::EndTag("div") << html::CR();
3127 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3128 vector<unsigned int> const & clen) const
3130 idx_type const fcell = getFirstCellInRow(row);
3131 idx_type const n = numberOfCellsInRow(row) + fcell;
3134 for (idx_type i = fcell; i < n; ++i) {
3144 for (idx_type i = fcell; i < n; ++i) {
3155 col_type column = cellColumn(i);
3156 int len = clen[column];
3157 while (column < ncols() - 1
3158 && isPartOfMultiColumn(row, ++column))
3159 len += clen[column] + 4;
3160 os << docstring(len, ch);
3175 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3176 vector<unsigned int> const & clen) const
3178 idx_type const fcell = getFirstCellInRow(row);
3179 idx_type const n = numberOfCellsInRow(row) + fcell;
3182 for (idx_type i = fcell; i < n; ++i) {
3183 if (bottomLine(i)) {
3192 for (idx_type i = fcell; i < n; ++i) {
3193 if (bottomLine(i)) {
3203 col_type column = cellColumn(i);
3204 int len = clen[column];
3205 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3206 len += clen[column] + 4;
3207 os << docstring(len, ch);
3208 if (bottomLine(i)) {
3222 void Tabular::plaintextPrintCell(odocstringstream & os,
3223 OutputParams const & runparams,
3224 idx_type cell, row_type row, col_type column,
3225 vector<unsigned int> const & clen,
3226 bool onlydata, size_t max_length) const
3228 odocstringstream sstr;
3229 cellInset(cell)->plaintext(sstr, runparams, max_length);
3241 unsigned int len1 = sstr.str().length();
3242 unsigned int len2 = clen[column];
3243 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3244 len2 += clen[column] + 4;
3247 switch (getAlignment(cell)) {
3249 case LYX_ALIGN_LEFT:
3252 case LYX_ALIGN_RIGHT:
3256 case LYX_ALIGN_CENTER:
3262 os << docstring(len1, ' ') << sstr.str()
3263 << docstring(len2, ' ');
3265 if (rightLine(cell))
3272 void Tabular::plaintext(odocstringstream & os,
3273 OutputParams const & runparams, int const depth,
3274 bool onlydata, char_type delim, size_t max_length) const
3276 // first calculate the width of the single columns
3277 vector<unsigned int> clen(ncols());
3280 // first all non multicolumn cells!
3281 for (col_type c = 0; c < ncols(); ++c) {
3283 for (row_type r = 0; r < nrows(); ++r) {
3284 idx_type cell = cellIndex(r, c);
3285 if (isMultiColumn(cell))
3287 odocstringstream sstr;
3288 cellInset(cell)->plaintext(sstr, runparams, max_length);
3289 if (clen[c] < sstr.str().length())
3290 clen[c] = sstr.str().length();
3293 // then all multicolumn cells!
3294 for (col_type c = 0; c < ncols(); ++c) {
3295 for (row_type r = 0; r < nrows(); ++r) {
3296 idx_type cell = cellIndex(r, c);
3297 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3299 odocstringstream sstr;
3300 cellInset(cell)->plaintext(sstr, runparams, max_length);
3301 int len = int(sstr.str().length());
3302 idx_type const n = columnSpan(cell);
3303 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3305 if (len > int(clen[c + n - 1]))
3306 clen[c + n - 1] = len;
3311 for (row_type r = 0; r < nrows(); ++r) {
3312 if (!onlydata && plaintextTopHLine(os, r, clen))
3313 os << docstring(depth * 2, ' ');
3314 for (col_type c = 0; c < ncols(); ++c) {
3315 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3317 if (onlydata && c > 0)
3318 // we don't use operator<< for single UCS4 character.
3319 // see explanation in docstream.h
3321 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3323 if (os.str().size() > max_length)
3328 os << docstring(depth * 2, ' ');
3329 if (plaintextBottomHLine(os, r, clen))
3330 os << docstring(depth * 2, ' ');
3332 if (os.str().size() > max_length)
3338 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell)
3340 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3344 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row, col_type column)
3346 return cell_info[row][column].inset;
3350 InsetTableCell const * Tabular::cellInset(idx_type cell) const
3352 return cell_info[cellRow(cell)][cellColumn(cell)].inset.get();
3356 void Tabular::setCellInset(row_type row, col_type column,
3357 shared_ptr<InsetTableCell> ins)
3359 CellData & cd = cell_info[row][column];
3364 void Tabular::validate(LaTeXFeatures & features) const
3366 features.require("NeedTabularnewline");
3368 features.require("booktabs");
3369 if (is_long_tabular)
3370 features.require("longtable");
3372 features.require("rotating");
3373 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3374 if (isMultiRow(cell))
3375 features.require("multirow");
3376 if (getVAlignment(cell) != LYX_VALIGN_TOP
3377 || !getPWidth(cell).zero())
3378 features.require("array");
3379 cellInset(cell)->validate(features);
3384 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3386 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3387 ParagraphList::const_iterator cit = parlist.begin();
3388 ParagraphList::const_iterator end = parlist.end();
3390 for (; cit != end; ++cit)
3391 for (int i = 0; i < cit->size(); ++i)
3392 if (cit->isNewline(i))
3399 /////////////////////////////////////////////////////////////////////
3403 /////////////////////////////////////////////////////////////////////
3405 InsetTableCell::InsetTableCell(Buffer * buf)
3406 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3407 contentAlign(LYX_ALIGN_CENTER)
3411 bool InsetTableCell::forcePlainLayout(idx_type) const
3413 return !isFixedWidth;
3417 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3419 return isFixedWidth;
3423 bool InsetTableCell::forceLocalFontSwitch() const
3425 return isFixedWidth;
3429 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3430 FuncStatus & status) const
3432 bool enabled = true;
3433 switch (cmd.action()) {
3434 case LFUN_MATH_DISPLAY:
3435 if (!hasFixedWidth()) {
3440 return InsetText::getStatus(cur, cmd, status);
3442 status.setEnabled(enabled);
3446 docstring InsetTableCell::asString(bool intoInsets)
3449 if (paragraphs().empty())
3451 ParagraphList::const_iterator it = paragraphs().begin();
3452 ParagraphList::const_iterator en = paragraphs().end();
3454 for (; it != en; ++it) {
3459 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3465 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3466 UpdateType utype) const
3468 InsetText::iterateForToc(di, output_active, utype);
3472 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3475 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3476 return InsetText::xhtml(xs, rp);
3481 /////////////////////////////////////////////////////////////////////
3485 /////////////////////////////////////////////////////////////////////
3487 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3489 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3490 first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
3495 InsetTabular::InsetTabular(InsetTabular const & tab)
3496 : Inset(tab), tabular(tab.tabular),
3497 first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
3502 InsetTabular::~InsetTabular()
3504 hideDialogs("tabular", this);
3508 void InsetTabular::setBuffer(Buffer & buf)
3510 tabular.setBuffer(buf);
3511 Inset::setBuffer(buf);
3515 bool InsetTabular::insetAllowed(InsetCode code) const
3520 case MATHMACRO_CODE:
3525 return tabular.is_long_tabular;
3533 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3535 return tabular.is_long_tabular &&
3536 (newtype == "Standard" || newtype == "Unnumbered");
3540 void InsetTabular::write(ostream & os) const
3542 os << "Tabular" << endl;
3547 string InsetTabular::contextMenu(BufferView const &, int, int) const
3549 // FIXME: depending on the selection state,
3550 // we could offer a different menu.
3551 return cell(0)->contextMenuName() + ";" + contextMenuName();
3555 string InsetTabular::contextMenuName() const
3557 return "context-tabular";
3561 void InsetTabular::read(Lexer & lex)
3563 //bool const old_format = (lex.getString() == "\\LyXTable");
3571 string token = lex.getString();
3572 while (lex && token != "\\end_inset") {
3574 token = lex.getString();
3577 lex.printError("Missing \\end_inset at this point. ");
3581 int InsetTabular::rowFromY(Cursor & cur, int y) const
3583 // top y coordinate of tabular
3584 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3586 for (; r < tabular.nrows() && y > h; ++r)
3587 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3588 + tabular.interRowSpace(r);
3594 int InsetTabular::columnFromX(Cursor & cur, int x) const
3596 // left x coordinate of tabular
3597 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3599 for (; c < tabular.ncols() && x > w; ++c)
3600 w += tabular.cellWidth(c);
3605 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3607 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3608 // mi.base.textwidth << "\n";
3609 LBUFERR(mi.base.bv);
3611 for (row_type r = 0; r < tabular.nrows(); ++r) {
3614 for (col_type c = 0; c < tabular.ncols(); ++c) {
3615 if (tabular.isPartOfMultiColumn(r, c)
3616 || tabular.isPartOfMultiRow(r, c))
3617 // multicolumn or multirow cell, but not first one
3619 idx_type const cell = tabular.cellIndex(r, c);
3622 Length const p_width = tabular.getPWidth(cell);
3623 if (!p_width.zero())
3624 m.base.textwidth = p_width.inPixels(mi.base);
3625 tabular.cellInset(cell)->metrics(m, dim);
3626 if (!p_width.zero())
3627 dim.wid = m.base.textwidth;
3628 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3629 + tabular.interColumnSpace(cell);
3631 // FIXME(?): do we need a second metrics call?
3632 TextMetrics const & tm =
3633 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3635 // determine horizontal offset because of decimal align (if necessary)
3636 int decimal_width = 0;
3637 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3638 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell));
3639 tail.setBuffer(tabular.buffer());
3640 // we need to set macrocontext position everywhere
3641 // otherwise we crash with nested insets (e.g. footnotes)
3642 // after decimal point
3643 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3645 dit.push_back(CursorSlice(tail));
3646 tail.setMacrocontextPositionRecursive(dit);
3648 // remove text leading decimal point
3649 docstring const align_d = tabular.column_info[c].decimal_point;
3650 dit = separatorPos(&tail, align_d);
3652 pit_type const psize = tail.paragraphs().front().size();
3654 tail.paragraphs().front().eraseChars(0,
3655 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3657 tail.metrics(m, dim1);
3658 decimal_width = dim1.width();
3661 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3662 tabular.cell_info[r][c].decimal_width = decimal_width;
3664 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3665 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3666 int const lastpardes = tm.last().second->descent()
3667 + TEXT_TO_INSET_OFFSET;
3669 switch (tabular.getVAlignment(cell)) {
3670 case Tabular::LYX_VALIGN_TOP:
3672 case Tabular::LYX_VALIGN_MIDDLE:
3673 offset = -(dim.des - lastpardes)/2;
3675 case Tabular::LYX_VALIGN_BOTTOM:
3676 offset = -(dim.des - lastpardes);
3679 tabular.cell_info[r][c].voffset = offset;
3680 maxasc = max(maxasc, dim.asc - offset);
3681 maxdes = max(maxdes, dim.des + offset);
3683 int const top_space = tabular.row_info[r].top_space_default ?
3684 default_line_space :
3685 tabular.row_info[r].top_space.inPixels(mi.base);
3686 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3687 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3688 default_line_space :
3689 tabular.row_info[r].bottom_space.inPixels(mi.base);
3690 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3693 // for top-alignment the first horizontal table line must be exactly at
3694 // the position of the base line of the surrounding text line
3695 // for bottom alignment, the same is for the last table line
3696 switch (tabular.tabular_valignment) {
3697 case Tabular::LYX_VALIGN_BOTTOM:
3698 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3700 case Tabular::LYX_VALIGN_MIDDLE:
3701 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3703 case Tabular::LYX_VALIGN_TOP:
3704 offset_valign_ = tabular.rowAscent(0);
3708 tabular.updateColumnWidths();
3709 dim.asc = tabular.rowAscent(0) - offset_valign_;
3710 dim.des = tabular.height() - dim.asc;
3711 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3715 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3718 if (&cur.inset() == this && cur.selection()) {
3719 if (cur.selIsMultiCell()) {
3722 getSelection(cur, rs, re, cs, ce);
3724 idx_type const cell = tabular.cellIndex(row, col);
3725 col_type const cspan = tabular.columnSpan(cell);
3726 row_type const rspan = tabular.rowSpan(cell);
3727 if (col + cspan - 1 >= cs && col <= ce
3728 && row + rspan - 1 >= rs && row <= re)
3731 if (col == tabular.cellColumn(cur.idx())
3732 && row == tabular.cellRow(cur.idx())) {
3733 CursorSlice const & beg = cur.selBegin();
3734 CursorSlice const & end = cur.selEnd();
3736 if ((end.lastpos() > 0 || end.lastpit() > 0)
3737 && end.pos() == end.lastpos() && beg.pos() == 0
3738 && end.pit() == end.lastpit() && beg.pit() == 0)
3746 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3748 x += ADD_TO_TABULAR_WIDTH;
3750 BufferView * bv = pi.base.bv;
3751 Cursor & cur = pi.base.bv->cursor();
3753 // FIXME: As the full background is painted in drawBackground(),
3754 // we have no choice but to do a full repaint for the Text cells.
3755 pi.full_repaint = true;
3757 bool const original_selection_state = pi.selected;
3760 first_visible_cell_ = Tabular::npos;
3762 int yy = y + offset_valign_;
3763 for (row_type r = 0; r < tabular.nrows(); ++r) {
3765 for (col_type c = 0; c < tabular.ncols(); ++c) {
3766 if (tabular.isPartOfMultiColumn(r, c))
3769 idx = tabular.cellIndex(r, c);
3771 if (tabular.isPartOfMultiRow(r, c)) {
3772 nx += tabular.cellWidth(idx);
3776 if (first_visible_cell_ == Tabular::npos)
3777 first_visible_cell_ = idx;
3779 pi.selected |= isCellSelected(cur, r, c);
3780 int const cx = nx + tabular.textHOffset(idx);
3781 int const cy = yy + tabular.textVOffset(idx);
3782 // Cache the Inset position.
3783 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3784 cell(idx)->draw(pi, cx, cy);
3785 drawCellLines(pi, nx, yy, r, idx);
3786 nx += tabular.cellWidth(idx);
3787 pi.selected = original_selection_state;
3790 if (r + 1 < tabular.nrows())
3791 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3792 + tabular.interRowSpace(r + 1);
3797 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3799 x += ADD_TO_TABULAR_WIDTH;
3800 y += offset_valign_ - tabular.rowAscent(0);
3801 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3802 pi.backgroundColor(this));
3806 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3808 Cursor & cur = pi.base.bv->cursor();
3810 x += ADD_TO_TABULAR_WIDTH;
3812 if (!cur.selection())
3814 if (&cur.inset() != this)
3819 bool const full_cell_selected = isCellSelected(cur,
3820 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3822 if (cur.selIsMultiCell() || full_cell_selected) {
3823 for (row_type r = 0; r < tabular.nrows(); ++r) {
3825 for (col_type c = 0; c < tabular.ncols(); ++c) {
3826 if (tabular.isPartOfMultiColumn(r, c))
3829 idx_type const cell = tabular.cellIndex(r, c);
3831 if (tabular.isPartOfMultiRow(r, c)) {
3832 xx += tabular.cellWidth(cell);
3835 int const w = tabular.cellWidth(cell);
3836 int const h = tabular.cellHeight(cell);
3837 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3838 if (isCellSelected(cur, r, c))
3839 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3842 if (r + 1 < tabular.nrows())
3843 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3844 + tabular.interRowSpace(r + 1);
3848 // FIXME: This code has no effect because InsetTableCell does not handle
3849 // drawSelection other than the trivial implementation in Inset.
3851 // x += cellXPos(cur.idx());
3852 // x += tabular.textHOffset(cur.idx());
3853 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3858 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3859 row_type row, idx_type cell) const
3861 y -= tabular.rowAscent(row);
3862 int const w = tabular.cellWidth(cell);
3863 int const h = tabular.cellHeight(cell);
3864 Color const linecolor = pi.textColor(Color_tabularline);
3865 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3868 bool drawline = tabular.topLine(cell)
3869 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3870 pi.pain.line(x, y, x + w, y,
3871 drawline ? linecolor : gridcolor,
3872 drawline ? Painter::line_solid : Painter::line_onoffdash);
3875 drawline = tabular.bottomLine(cell);
3876 pi.pain.line(x, y + h, x + w, y + h,
3877 drawline ? linecolor : gridcolor,
3878 drawline ? Painter::line_solid : Painter::line_onoffdash);
3881 col_type const col = tabular.cellColumn(cell);
3882 drawline = tabular.leftLine(cell)
3883 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3884 pi.pain.line(x, y, x, y + h,
3885 drawline ? linecolor : gridcolor,
3886 drawline ? Painter::line_solid : Painter::line_onoffdash);
3889 x -= tabular.interColumnSpace(cell);
3890 col_type next_cell_col = col + 1;
3891 while (next_cell_col < tabular.ncols()
3892 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3894 drawline = tabular.rightLine(cell)
3895 || (next_cell_col < tabular.ncols()
3896 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3897 pi.pain.line(x + w, y, x + w, y + h,
3898 drawline ? linecolor : gridcolor,
3899 drawline ? Painter::line_solid : Painter::line_onoffdash);
3903 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3905 //lyxerr << "InsetTabular::edit: " << this << endl;
3909 if (isRightToLeft(cur))
3910 cur.idx() = tabular.getLastCellInRow(0);
3916 if (isRightToLeft(cur))
3917 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3919 cur.idx() = tabular.numberofcells - 1;
3921 cur.pos() = cur.lastpos(); // FIXME crude guess
3923 cur.setCurrentFont();
3924 // FIXME: this accesses the position cache before it is initialized
3925 //cur.bv().fitCursor();
3929 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3931 // In a longtable, tell captions what the current float is
3932 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3933 string const saveflt = cnts.current_float();
3934 if (tabular.is_long_tabular) {
3935 cnts.current_float("table");
3936 // in longtables, we only step the counter once
3937 cnts.step(from_ascii("table"), utype);
3938 cnts.isLongtable(true);
3941 ParIterator it2 = it;
3943 size_t const end = it2.nargs();
3944 for ( ; it2.idx() < end; it2.top().forwardIdx())
3945 buffer().updateBuffer(it2, utype);
3948 if (tabular.is_long_tabular) {
3949 cnts.current_float(saveflt);
3950 cnts.isLongtable(false);
3955 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3956 UpdateType utype) const
3958 DocIterator dit = cpit;
3960 size_t const end = dit.nargs();
3961 for ( ; dit.idx() < end; dit.top().forwardIdx())
3962 cell(dit.idx())->addToToc(dit, output_active, utype);
3966 bool InsetTabular::hitSelectRow(BufferView const & bv, int x) const
3968 int const x0 = xo(bv) + ADD_TO_TABULAR_WIDTH;
3969 return x < x0 || x > x0 + tabular.width();
3973 bool InsetTabular::hitSelectColumn(BufferView const & bv, int y) const
3975 int const y0 = yo(bv) - tabular.rowAscent(0) + offset_valign_;
3976 // FIXME: using ADD_TO_TABULAR_WIDTH is not really correct since
3977 // there is no margin added vertically to tabular insets.
3978 // However, it works for now.
3979 return y < y0 + ADD_TO_TABULAR_WIDTH || y > y0 + tabular.height() - ADD_TO_TABULAR_WIDTH;
3983 bool InsetTabular::clickable(BufferView const & bv, int x, int y) const
3985 return hitSelectRow(bv, x) || hitSelectColumn(bv, y);
3989 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3991 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3992 << "\n cur:" << cur);
3993 CursorSlice sl = cur.top();
3994 Cursor & bvcur = cur.bv().cursor();
3996 FuncCode const act = cmd.action();
4000 case LFUN_MOUSE_PRESS: {
4001 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
4003 if (hitSelectRow(cur.bv(), cmd.x())) {
4004 row_type r = rowFromY(cur, cmd.y());
4005 cur.idx() = tabular.getFirstCellInRow(r);
4009 cur.idx() = tabular.getLastCellInRow(r);
4010 cur.pit() = cur.lastpit();
4011 cur.pos() = cur.lastpos();
4012 cur.selection(true);
4018 if (hitSelectColumn(cur.bv(), cmd.y())) {
4019 col_type c = columnFromX(cur, cmd.x());
4020 cur.idx() = tabular.cellIndex(0, c);
4024 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4025 cur.pit() = cur.lastpit();
4026 cur.pos() = cur.lastpos();
4027 cur.selection(true);
4032 // do not reset cursor/selection if we have selected
4033 // some cells (bug 2715).
4034 if (cmd.button() == mouse_button::button3
4035 && &bvcur.selBegin().inset() == this
4036 && bvcur.selIsMultiCell())
4039 // Let InsetTableCell do it
4040 cell(cur.idx())->dispatch(cur, cmd);
4043 case LFUN_MOUSE_MOTION:
4044 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
4045 if (cmd.button() == mouse_button::button1) {
4046 // only accept motions to places not deeper nested than the real anchor
4047 if (!bvcur.realAnchor().hasPart(cur)) {
4051 // select (additional) row
4053 row_type r = rowFromY(cur, cmd.y());
4054 cur.idx() = tabular.getLastCellInRow(r);
4055 // we need to reset the cursor's pit and pos now, as the old ones
4056 // may no longer be valid.
4059 bvcur.setCursor(cur);
4060 bvcur.selection(true);
4063 // select (additional) column
4065 col_type c = columnFromX(cur, cmd.x());
4066 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4067 // we need to reset the cursor's pit and pos now, as the old ones
4068 // may no longer be valid.
4071 bvcur.setCursor(cur);
4072 bvcur.selection(true);
4075 // only update if selection changes
4076 if (bvcur.idx() == cur.idx() &&
4077 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4078 cur.noScreenUpdate();
4079 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4080 bvcur.setCursor(cur);
4081 bvcur.selection(true);
4082 // if this is a multicell selection, we just set the cursor to
4083 // the beginning of the cell's text.
4084 if (bvcur.selIsMultiCell()) {
4085 bvcur.pit() = bvcur.lastpit();
4086 bvcur.pos() = bvcur.lastpos();
4091 case LFUN_MOUSE_RELEASE:
4096 case LFUN_CELL_BACKWARD:
4098 cur.selection(false);
4101 case LFUN_CELL_FORWARD:
4103 cur.selection(false);
4106 case LFUN_CHAR_FORWARD_SELECT:
4107 case LFUN_CHAR_FORWARD:
4108 case LFUN_CHAR_BACKWARD_SELECT:
4109 case LFUN_CHAR_BACKWARD:
4110 case LFUN_CHAR_RIGHT_SELECT:
4111 case LFUN_CHAR_RIGHT:
4112 case LFUN_CHAR_LEFT_SELECT:
4113 case LFUN_CHAR_LEFT:
4114 case LFUN_WORD_FORWARD:
4115 case LFUN_WORD_FORWARD_SELECT:
4116 case LFUN_WORD_BACKWARD:
4117 case LFUN_WORD_BACKWARD_SELECT:
4118 case LFUN_WORD_RIGHT:
4119 case LFUN_WORD_RIGHT_SELECT:
4120 case LFUN_WORD_LEFT:
4121 case LFUN_WORD_LEFT_SELECT: {
4122 // determine whether we move to next or previous cell, where to enter
4123 // the new cell from, and which command to "finish" (i.e., exit the
4126 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4127 FuncCode finish_lfun;
4129 if (act == LFUN_CHAR_FORWARD
4130 || act == LFUN_CHAR_FORWARD_SELECT
4131 || act == LFUN_WORD_FORWARD
4132 || act == LFUN_WORD_FORWARD_SELECT) {
4134 finish_lfun = LFUN_FINISHED_FORWARD;
4136 else if (act == LFUN_CHAR_BACKWARD
4137 || act == LFUN_CHAR_BACKWARD_SELECT
4138 || act == LFUN_WORD_BACKWARD
4139 || act == LFUN_WORD_BACKWARD_SELECT) {
4141 finish_lfun = LFUN_FINISHED_BACKWARD;
4143 // LEFT or RIGHT commands --- the interpretation will depend on the
4144 // table's direction.
4146 bool const right = act == LFUN_CHAR_RIGHT
4147 || act == LFUN_CHAR_RIGHT_SELECT
4148 || act == LFUN_WORD_RIGHT
4149 || act == LFUN_WORD_RIGHT_SELECT;
4150 next_cell = isRightToLeft(cur) != right;
4152 if (lyxrc.visual_cursor)
4153 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4155 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4158 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4159 || act == LFUN_CHAR_BACKWARD_SELECT
4160 || act == LFUN_CHAR_RIGHT_SELECT
4161 || act == LFUN_CHAR_LEFT_SELECT
4162 || act == LFUN_WORD_FORWARD_SELECT
4163 || act == LFUN_WORD_RIGHT_SELECT
4164 || act == LFUN_WORD_BACKWARD_SELECT
4165 || act == LFUN_WORD_LEFT_SELECT;
4167 // If we have a multicell selection or we're
4168 // not doing some LFUN_*_SELECT thing anyway...
4169 if (!cur.selIsMultiCell() || !select) {
4170 col_type const c = tabular.cellColumn(cur.idx());
4171 row_type const r = tabular.cellRow(cur.idx());
4172 // Are we trying to select the whole cell and is the whole cell
4173 // not yet selected?
4174 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4175 ((next_cell && cur.pit() == cur.lastpit()
4176 && cur.pos() == cur.lastpos())
4177 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4179 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4181 // ...try to dispatch to the cell's inset.
4182 cell(cur.idx())->dispatch(cur, cmd);
4184 // When we already have a selection we want to select the whole cell
4185 // before going to the next cell.
4186 if (select_whole && !empty_cell){
4187 getText(cur.idx())->selectAll(cur);
4189 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4193 // FIXME: When we support the selection of an empty cell, remove
4194 // the !empty_cell from this condition. For now we jump to the next
4195 // cell if the current cell is empty.
4196 if (cur.result().dispatched() && !empty_cell)
4200 // move to next/prev cell, as appropriate
4201 // note that we will always do this if we're selecting and we have
4202 // a multicell selection
4203 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4204 << " cell from: " << int(entry_from));
4206 moveNextCell(cur, entry_from);
4208 movePrevCell(cur, entry_from);
4209 // if we're exiting the table, call the appropriate FINISHED lfun
4210 if (sl == cur.top()) {
4211 cmd = FuncRequest(finish_lfun);
4216 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4221 case LFUN_DOWN_SELECT:
4223 if (!(cur.selection() && cur.selIsMultiCell()))
4224 cell(cur.idx())->dispatch(cur, cmd);
4226 cur.dispatched(); // override the cell's decision
4227 if (sl == cur.top()) {
4228 // if our Text didn't do anything to the cursor
4229 // then we try to put the cursor into the cell below
4230 // setting also the right targetX.
4231 cur.selHandle(act == LFUN_DOWN_SELECT);
4232 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4233 int const xtarget = cur.targetX();
4234 // WARNING: Once cur.idx() has been reset, the cursor is in
4235 // an inconsistent state until pos() has been set. Be careful
4236 // what you do with it!
4237 cur.idx() = tabular.cellBelow(cur.idx());
4239 TextMetrics const & tm =
4240 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4241 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4242 cur.setCurrentFont();
4245 if (sl == cur.top()) {
4246 // we trick it to go to forward after leaving the
4248 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4251 if (cur.selIsMultiCell()) {
4252 cur.pit() = cur.lastpit();
4253 cur.pos() = cur.lastpos();
4254 cur.setCurrentFont();
4255 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4258 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4261 case LFUN_UP_SELECT:
4263 if (!(cur.selection() && cur.selIsMultiCell()))
4264 cell(cur.idx())->dispatch(cur, cmd);
4265 cur.dispatched(); // override the cell's decision
4266 if (sl == cur.top()) {
4267 // if our Text didn't do anything to the cursor
4268 // then we try to put the cursor into the cell above
4269 // setting also the right targetX.
4270 cur.selHandle(act == LFUN_UP_SELECT);
4271 if (tabular.cellRow(cur.idx()) != 0) {
4272 int const xtarget = cur.targetX();
4273 // WARNING: Once cur.idx() has been reset, the cursor is in
4274 // an inconsistent state until pos() has been set. Be careful
4275 // what you do with it!
4276 cur.idx() = tabular.cellAbove(cur.idx());
4277 cur.pit() = cur.lastpit();
4278 Text const * text = cell(cur.idx())->getText(0);
4279 TextMetrics const & tm = cur.bv().textMetrics(text);
4280 ParagraphMetrics const & pm =
4281 tm.parMetrics(cur.lastpit());
4282 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4283 cur.setCurrentFont();
4286 if (sl == cur.top()) {
4287 cmd = FuncRequest(LFUN_UP);
4290 if (cur.selIsMultiCell()) {
4292 cur.pos() = cur.lastpos();
4293 cur.setCurrentFont();
4294 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4297 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4300 // case LFUN_SCREEN_DOWN: {
4301 // //if (hasSelection())
4302 // // cur.selection() = false;
4303 // col_type const col = tabular.cellColumn(cur.idx());
4304 // int const t = cur.bv().top_y() + cur.bv().height();
4305 // if (t < yo() + tabular.getHeightOfTabular()) {
4306 // cur.bv().scrollDocView(t, true);
4307 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4309 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4316 // case LFUN_SCREEN_UP: {
4317 // //if (hasSelection())
4318 // // cur.selection() = false;
4319 // col_type const col = tabular.cellColumn(cur.idx());
4320 // int const t = cur.bv().top_y() + cur.bv().height();
4322 // cur.bv().scrollDocView(t, true);
4326 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4330 // cur.par() = cur.lastpar();
4331 // cur.pos() = cur.lastpos();
4335 case LFUN_LAYOUT_TABULAR:
4336 cur.bv().showDialog("tabular");
4339 case LFUN_INSET_MODIFY:
4340 // we come from the dialog
4341 if (cmd.getArg(0) == "tabular")
4342 tabularFeatures(cur, cmd.getLongArg(1));
4347 case LFUN_TABULAR_FEATURE:
4348 tabularFeatures(cur, to_utf8(cmd.argument()));
4351 // insert file functions
4352 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4353 case LFUN_FILE_INSERT_PLAINTEXT:
4355 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4356 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4357 FileName(to_utf8(cmd.argument())));
4360 cur.recordUndoInset();
4361 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4362 // content has been replaced,
4363 // so cursor might be invalid
4364 cur.pos() = cur.lastpos();
4365 cur.pit() = cur.lastpit();
4366 bvcur.setCursor(cur);
4373 if (cur.selIsMultiCell()) {
4374 if (copySelection(cur)) {
4375 cur.recordUndoInset();
4379 cell(cur.idx())->dispatch(cur, cmd);
4382 case LFUN_SELF_INSERT:
4383 if (cur.selIsMultiCell()) {
4384 cur.recordUndoInset();
4386 BufferView * bv = &cur.bv();
4387 docstring::const_iterator cit = cmd.argument().begin();
4388 docstring::const_iterator const end = cmd.argument().end();
4389 for (; cit != end; ++cit)
4390 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4393 bv->bookmarkEditPosition();
4395 cell(cur.idx())->dispatch(cur, cmd);
4398 case LFUN_CHAR_DELETE_BACKWARD:
4399 case LFUN_CHAR_DELETE_FORWARD:
4400 if (cur.selIsMultiCell()) {
4401 cur.recordUndoInset();
4404 cell(cur.idx())->dispatch(cur, cmd);
4408 if (!cur.selection())
4410 if (cur.selIsMultiCell()) {
4414 cell(cur.idx())->dispatch(cur, cmd);
4417 case LFUN_CLIPBOARD_PASTE:
4418 case LFUN_PRIMARY_SELECTION_PASTE: {
4419 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4420 theClipboard().getAsText(Clipboard::PlainTextType) :
4421 theSelection().get();
4424 // pass to InsertPlaintextString, but
4425 // only if we have multi-cell content
4426 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4427 cur.recordUndoInset();
4428 if (insertPlaintextString(cur.bv(), clip, false)) {
4429 // content has been replaced,
4430 // so cursor might be invalid
4431 cur.pos() = cur.lastpos();
4432 cur.pit() = cur.lastpit();
4433 bvcur.setCursor(cur);
4437 // Let the cell handle normal text
4438 cell(cur.idx())->dispatch(cur, cmd);
4443 if (!tabularStackDirty()) {
4444 if (!cur.selIsMultiCell())
4445 cell(cur.idx())->dispatch(cur, cmd);
4448 if (theClipboard().isInternal()) {
4449 cur.recordUndoInset();
4450 pasteClipboard(cur);
4454 case LFUN_FONT_EMPH:
4455 case LFUN_FONT_BOLD:
4456 case LFUN_FONT_BOLDSYMBOL:
4457 case LFUN_FONT_ROMAN:
4458 case LFUN_FONT_NOUN:
4459 case LFUN_FONT_ITAL:
4460 case LFUN_FONT_FRAK:
4461 case LFUN_FONT_TYPEWRITER:
4462 case LFUN_FONT_SANS:
4463 case LFUN_TEXTSTYLE_APPLY:
4464 case LFUN_TEXTSTYLE_UPDATE:
4465 case LFUN_FONT_SIZE:
4466 case LFUN_FONT_UNDERLINE:
4467 case LFUN_FONT_STRIKEOUT:
4468 case LFUN_FONT_UNDERUNDERLINE:
4469 case LFUN_FONT_UNDERWAVE:
4471 case LFUN_PARAGRAPH_PARAMS_APPLY:
4472 case LFUN_PARAGRAPH_PARAMS:
4473 case LFUN_WORD_CAPITALIZE:
4474 case LFUN_WORD_UPCASE:
4475 case LFUN_WORD_LOWCASE:
4476 case LFUN_CHARS_TRANSPOSE:
4477 if (cur.selIsMultiCell()) {
4480 getSelection(cur, rs, re, cs, ce);
4481 Cursor tmpcur = cur;
4482 for (row_type r = rs; r <= re; ++r) {
4483 for (col_type c = cs; c <= ce; ++c) {
4484 // cursor follows cell:
4485 tmpcur.idx() = tabular.cellIndex(r, c);
4486 // select this cell only:
4489 tmpcur.resetAnchor();
4490 tmpcur.pit() = tmpcur.lastpit();
4491 tmpcur.pos() = tmpcur.top().lastpos();
4492 tmpcur.setCursor(tmpcur);
4493 tmpcur.setSelection();
4494 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4499 cell(cur.idx())->dispatch(cur, cmd);
4503 case LFUN_INSET_SETTINGS:
4504 // relay this lfun to Inset, not to the cell.
4505 Inset::doDispatch(cur, cmd);
4509 // we try to handle this event in the insets dispatch function.
4510 cell(cur.idx())->dispatch(cur, cmd);
4516 bool InsetTabular::getFeatureStatus(Cursor & cur, string const & s,
4517 string const & argument, FuncStatus & status) const
4520 int action = Tabular::LAST_ACTION;
4522 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4523 if (tabularFeature[i].feature == s) {
4524 action = tabularFeature[i].action;
4528 if (action == Tabular::LAST_ACTION) {
4530 status.setUnknown(true);
4534 row_type sel_row_start = 0;
4535 row_type sel_row_end = 0;
4536 col_type sel_col_start = 0;
4537 col_type sel_col_end = 0;
4538 Tabular::ltType dummyltt;
4541 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4544 case Tabular::SET_PWIDTH:
4545 case Tabular::SET_MPWIDTH:
4546 case Tabular::SET_SPECIAL_COLUMN:
4547 case Tabular::SET_SPECIAL_MULTICOLUMN:
4548 case Tabular::APPEND_ROW:
4549 case Tabular::APPEND_COLUMN:
4550 case Tabular::DELETE_ROW:
4551 case Tabular::DELETE_COLUMN:
4552 case Tabular::COPY_ROW:
4553 case Tabular::COPY_COLUMN:
4554 case Tabular::SET_TOP_SPACE:
4555 case Tabular::SET_BOTTOM_SPACE:
4556 case Tabular::SET_INTERLINE_SPACE:
4560 case Tabular::SET_TABULAR_WIDTH:
4561 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4562 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4565 case Tabular::MOVE_COLUMN_RIGHT:
4566 case Tabular::MOVE_COLUMN_LEFT:
4567 case Tabular::MOVE_ROW_DOWN:
4568 case Tabular::MOVE_ROW_UP: {
4569 if (cur.selection()) {
4570 status.message(_("Selections not supported."));
4571 status.setEnabled(false);
4575 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4576 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4577 (action == Tabular::MOVE_COLUMN_LEFT &&
4578 tabular.cellColumn(cur.idx()) == 0) ||
4579 (action == Tabular::MOVE_ROW_DOWN &&
4580 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4581 (action == Tabular::MOVE_ROW_UP &&
4582 tabular.cellRow(cur.idx()) == 0)) {
4583 status.setEnabled(false);
4587 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4588 action == Tabular::MOVE_COLUMN_LEFT) {
4589 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4590 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4591 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4592 status.message(_("Multi-column in current or"
4593 " destination column."));
4594 status.setEnabled(false);
4599 if (action == Tabular::MOVE_ROW_DOWN ||
4600 action == Tabular::MOVE_ROW_UP) {
4601 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4602 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4603 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4604 status.message(_("Multi-row in current or"
4605 " destination row."));
4606 status.setEnabled(false);
4611 status.setEnabled(true);
4615 case Tabular::SET_DECIMAL_POINT:
4617 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4620 case Tabular::SET_MULTICOLUMN:
4621 case Tabular::UNSET_MULTICOLUMN:
4622 case Tabular::MULTICOLUMN:
4623 // If a row is set as longtable caption, it must not be allowed
4624 // to unset that this row is a multicolumn.
4625 status.setEnabled(sel_row_start == sel_row_end
4626 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4627 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4630 case Tabular::SET_MULTIROW:
4631 case Tabular::UNSET_MULTIROW:
4632 case Tabular::MULTIROW:
4633 // If a row is set as longtable caption, it must not be allowed
4634 // to unset that this row is a multirow.
4635 status.setEnabled(sel_col_start == sel_col_end
4636 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4637 status.setOnOff(tabular.isMultiRow(cur.idx()));
4640 case Tabular::SET_ALL_LINES:
4641 case Tabular::UNSET_ALL_LINES:
4642 case Tabular::SET_BORDER_LINES:
4643 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4646 case Tabular::SET_LINE_TOP:
4647 case Tabular::SET_LINE_BOTTOM:
4648 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4651 case Tabular::SET_LINE_LEFT:
4652 case Tabular::SET_LINE_RIGHT:
4653 status.setEnabled(!tabular.use_booktabs
4654 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4657 case Tabular::TOGGLE_LINE_TOP:
4658 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4659 status.setOnOff(tabular.topLine(cur.idx()));
4662 case Tabular::TOGGLE_LINE_BOTTOM:
4663 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4664 status.setOnOff(tabular.bottomLine(cur.idx()));
4667 case Tabular::TOGGLE_LINE_LEFT:
4668 status.setEnabled(!tabular.use_booktabs
4669 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4670 status.setOnOff(tabular.leftLine(cur.idx()));
4673 case Tabular::TOGGLE_LINE_RIGHT:
4674 status.setEnabled(!tabular.use_booktabs
4675 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4676 status.setOnOff(tabular.rightLine(cur.idx()));
4679 // multirow cells only inherit the alignment of the column if the column has
4680 // no width, otherwise they are left-aligned
4681 // therefore allow always left but right and center only if there is no width
4682 case Tabular::M_ALIGN_LEFT:
4684 case Tabular::ALIGN_LEFT:
4685 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4688 case Tabular::M_ALIGN_RIGHT:
4690 case Tabular::ALIGN_RIGHT:
4691 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4692 && !tabular.getPWidth(cur.idx()).zero()));
4693 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4696 case Tabular::M_ALIGN_CENTER:
4698 case Tabular::ALIGN_CENTER:
4699 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4700 && !tabular.getPWidth(cur.idx()).zero()));
4701 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4704 case Tabular::ALIGN_BLOCK:
4705 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4706 && !tabular.isMultiRow(cur.idx()));
4707 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4710 case Tabular::ALIGN_DECIMAL:
4711 status.setEnabled(!tabular.isMultiRow(cur.idx())
4712 && !tabular.isMultiColumn(cur.idx()));
4713 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4716 case Tabular::M_VALIGN_TOP:
4718 case Tabular::VALIGN_TOP:
4719 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4720 && !tabular.isMultiRow(cur.idx()));
4722 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4725 case Tabular::M_VALIGN_BOTTOM:
4727 case Tabular::VALIGN_BOTTOM:
4728 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4729 && !tabular.isMultiRow(cur.idx()));
4731 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4734 case Tabular::M_VALIGN_MIDDLE:
4736 case Tabular::VALIGN_MIDDLE:
4737 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4738 && !tabular.isMultiRow(cur.idx()));
4740 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4743 case Tabular::SET_LONGTABULAR:
4744 case Tabular::TOGGLE_LONGTABULAR:
4745 // setting as longtable is not allowed when table is inside a float
4746 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4747 || cur.innerInsetOfType(WRAP_CODE) != 0)
4748 status.setEnabled(false);
4750 status.setEnabled(true);
4751 status.setOnOff(tabular.is_long_tabular);
4754 case Tabular::UNSET_LONGTABULAR:
4755 status.setOnOff(!tabular.is_long_tabular);
4758 case Tabular::TOGGLE_ROTATE_TABULAR:
4759 case Tabular::SET_ROTATE_TABULAR:
4760 status.setOnOff(tabular.rotate != 0);
4763 case Tabular::TABULAR_VALIGN_TOP:
4764 status.setEnabled(tabular.tabular_width.zero());
4765 status.setOnOff(tabular.tabular_valignment
4766 == Tabular::LYX_VALIGN_TOP);
4768 case Tabular::TABULAR_VALIGN_MIDDLE:
4769 status.setEnabled(tabular.tabular_width.zero());
4770 status.setOnOff(tabular.tabular_valignment
4771 == Tabular::LYX_VALIGN_MIDDLE);
4773 case Tabular::TABULAR_VALIGN_BOTTOM:
4774 status.setEnabled(tabular.tabular_width.zero());
4775 status.setOnOff(tabular.tabular_valignment
4776 == Tabular::LYX_VALIGN_BOTTOM);
4779 case Tabular::LONGTABULAR_ALIGN_LEFT:
4780 status.setOnOff(tabular.longtabular_alignment
4781 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4783 case Tabular::LONGTABULAR_ALIGN_CENTER:
4784 status.setOnOff(tabular.longtabular_alignment
4785 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4787 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4788 status.setOnOff(tabular.longtabular_alignment
4789 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4792 case Tabular::UNSET_ROTATE_TABULAR:
4793 status.setOnOff(tabular.rotate == 0);
4796 case Tabular::TOGGLE_ROTATE_CELL:
4797 case Tabular::SET_ROTATE_CELL:
4798 status.setOnOff(!oneCellHasRotationState(false,
4799 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4802 case Tabular::UNSET_ROTATE_CELL:
4803 status.setOnOff(!oneCellHasRotationState(true,
4804 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4807 case Tabular::SET_USEBOX:
4808 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4811 // every row can only be one thing:
4812 // either a footer or header
4813 case Tabular::SET_LTFIRSTHEAD:
4814 status.setEnabled(sel_row_start == sel_row_end);
4815 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4818 case Tabular::UNSET_LTFIRSTHEAD:
4819 status.setEnabled(sel_row_start == sel_row_end);
4820 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4823 case Tabular::SET_LTHEAD:
4824 status.setEnabled(sel_row_start == sel_row_end);
4825 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4828 case Tabular::UNSET_LTHEAD:
4829 status.setEnabled(sel_row_start == sel_row_end);
4830 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4833 case Tabular::SET_LTFOOT:
4834 status.setEnabled(sel_row_start == sel_row_end);
4835 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4838 case Tabular::UNSET_LTFOOT:
4839 status.setEnabled(sel_row_start == sel_row_end);
4840 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4843 case Tabular::SET_LTLASTFOOT:
4844 status.setEnabled(sel_row_start == sel_row_end);
4845 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4848 case Tabular::UNSET_LTLASTFOOT:
4849 status.setEnabled(sel_row_start == sel_row_end);
4850 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4853 case Tabular::SET_LTNEWPAGE:
4854 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4856 case Tabular::UNSET_LTNEWPAGE:
4857 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4860 // only one row in head/firsthead/foot/lasthead can be the caption
4861 // and a multirow cannot be set as caption
4862 case Tabular::SET_LTCAPTION:
4863 status.setEnabled(sel_row_start == sel_row_end
4864 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4865 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4866 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4867 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4868 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4869 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4870 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4871 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4872 && !tabular.isMultiRow(sel_row_start));
4873 status.setOnOff(tabular.ltCaption(sel_row_start));
4876 case Tabular::UNSET_LTCAPTION:
4877 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4880 case Tabular::TOGGLE_LTCAPTION:
4881 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4882 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4883 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4884 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4885 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4886 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4887 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4888 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4889 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4890 status.setOnOff(tabular.ltCaption(sel_row_start));
4893 case Tabular::TOGGLE_BOOKTABS:
4894 case Tabular::SET_BOOKTABS:
4895 status.setOnOff(tabular.use_booktabs);
4898 case Tabular::UNSET_BOOKTABS:
4899 status.setOnOff(!tabular.use_booktabs);
4904 status.setEnabled(false);
4911 // function sets an object as defined in FuncStatus.h:
4912 // states OK, Unknown, Disabled, On, Off.
4913 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4914 FuncStatus & status) const
4916 switch (cmd.action()) {
4917 case LFUN_INSET_MODIFY:
4918 if (cmd.getArg(0) != "tabular")
4920 if (cmd.getArg(1) == "for-dialog") {
4921 // The dialog is asking the status of a command
4922 if (&cur.inset() != this)
4924 string action = cmd.getArg(2);
4925 string arg = cmd.getLongArg(3);
4926 return getFeatureStatus(cur, action, arg, status);
4928 // We always enable the lfun if it is coming from the dialog
4929 // because the dialog makes sure all the settings are valid,
4930 // even though the first argument might not be valid now.
4931 status.setEnabled(true);
4935 case LFUN_TABULAR_FEATURE: {
4936 if (&cur.inset() != this)
4938 string action = cmd.getArg(0);
4939 string arg = cmd.getLongArg(1);
4940 return getFeatureStatus(cur, action, arg, status);
4943 case LFUN_CAPTION_INSERT: {
4944 // caption is only allowed in caption cell of longtable
4945 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4946 status.setEnabled(false);
4949 // only standard caption is allowed
4950 string arg = cmd.getArg(0);
4951 if (!arg.empty() && arg != "Standard") {
4952 status.setEnabled(false);
4955 // check if there is already a caption
4956 bool have_caption = false;
4957 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()));
4958 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4959 ParagraphList::const_iterator pend = itc.paragraphs().end();
4960 for (; pit != pend; ++pit) {
4961 InsetList::const_iterator it = pit->insetList().begin();
4962 InsetList::const_iterator end = pit->insetList().end();
4963 for (; it != end; ++it) {
4964 if (it->inset->lyxCode() == CAPTION_CODE) {
4965 have_caption = true;
4970 status.setEnabled(!have_caption);
4974 // These are only enabled inside tabular
4975 case LFUN_CELL_BACKWARD:
4976 case LFUN_CELL_FORWARD:
4977 status.setEnabled(true);
4980 // disable these with multiple cells selected
4981 case LFUN_INSET_INSERT:
4982 case LFUN_TABULAR_INSERT:
4983 case LFUN_FLEX_INSERT:
4984 case LFUN_FLOAT_INSERT:
4985 case LFUN_FLOAT_WIDE_INSERT:
4986 case LFUN_FOOTNOTE_INSERT:
4987 case LFUN_MARGINALNOTE_INSERT:
4988 case LFUN_MATH_INSERT:
4989 case LFUN_MATH_MODE:
4990 case LFUN_MATH_MUTATE:
4991 case LFUN_MATH_DISPLAY:
4992 case LFUN_NOTE_INSERT:
4993 case LFUN_ARGUMENT_INSERT:
4994 case LFUN_BOX_INSERT:
4995 case LFUN_BRANCH_INSERT:
4996 case LFUN_PHANTOM_INSERT:
4997 case LFUN_WRAP_INSERT:
4998 case LFUN_PREVIEW_INSERT:
4999 case LFUN_ERT_INSERT: {
5000 if (cur.selIsMultiCell()) {
5001 status.setEnabled(false);
5004 return cell(cur.idx())->getStatus(cur, cmd, status);
5007 // disable in non-fixed-width cells
5008 case LFUN_PARAGRAPH_BREAK:
5009 // multirow does not allow paragraph breaks
5010 if (tabular.isMultiRow(cur.idx())) {
5011 status.setEnabled(false);
5015 case LFUN_NEWLINE_INSERT: {
5016 if (tabular.getPWidth(cur.idx()).zero()) {
5017 status.setEnabled(false);
5020 return cell(cur.idx())->getStatus(cur, cmd, status);
5023 case LFUN_NEWPAGE_INSERT:
5024 status.setEnabled(false);
5028 if (tabularStackDirty() && theClipboard().isInternal()) {
5029 if (cur.selIsMultiCell()) {
5032 getSelection(cur, rs, re, cs, ce);
5033 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
5034 && paste_tabular->nrows() == re - rs + 1)
5035 status.setEnabled(true);
5037 status.setEnabled(false);
5038 status.message(_("Selection size should match clipboard content."));
5041 status.setEnabled(true);
5044 return cell(cur.idx())->getStatus(cur, cmd, status);
5046 case LFUN_INSET_SETTINGS:
5047 // relay this lfun to Inset, not to the cell.
5048 return Inset::getStatus(cur, cmd, status);
5051 // we try to handle this event in the insets dispatch function.
5052 return cell(cur.idx())->getStatus(cur, cmd, status);
5058 Inset::DisplayType InsetTabular::display() const
5060 if (tabular.is_long_tabular) {
5061 switch (tabular.longtabular_alignment) {
5062 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
5064 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
5066 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5076 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5078 tabular.latex(os, runparams);
5082 int InsetTabular::plaintext(odocstringstream & os,
5083 OutputParams const & runparams, size_t max_length) const
5085 os << '\n'; // output table on a new line
5086 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5087 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5088 return PLAINTEXT_NEWLINE;
5092 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5097 // FIXME: Why not pass a proper DocIterator here?
5099 // if the table is inside a float it doesn't need the informaltable
5100 // wrapper. Search for it.
5101 for (master = owner(); master; master = master->owner())
5102 if (master->lyxCode() == FLOAT_CODE)
5107 os << "<informaltable>";
5110 ret += tabular.docbook(os, runparams);
5112 os << "</informaltable>";
5119 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5121 return tabular.xhtml(xs, rp);
5125 void InsetTabular::validate(LaTeXFeatures & features) const
5127 tabular.validate(features);
5129 // It'd be better to be able to get this from an InsetLayout, but at present
5130 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5131 if (features.runparams().flavor == OutputParams::HTML)
5132 features.addCSSSnippet(
5133 "table { border: 1px solid black; display: inline-block; }\n"
5134 "td { border: 1px solid black; padding: 0.5ex; }");
5138 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5140 return tabular.cellInset(idx);
5144 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5146 return tabular.cellInset(idx);
5150 void InsetTabular::cursorPos(BufferView const & bv,
5151 CursorSlice const & sl, bool boundary, int & x, int & y) const
5153 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5155 // y offset correction
5156 y += cellYPos(sl.idx());
5157 y += tabular.textVOffset(sl.idx());
5158 y += offset_valign_;
5160 // x offset correction
5161 x += cellXPos(sl.idx());
5162 x += tabular.textHOffset(sl.idx());
5163 x += ADD_TO_TABULAR_WIDTH;
5167 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5171 Inset const & inset = *tabular.cellInset(cell);
5172 Point o = bv.coordCache().getInsets().xy(&inset);
5173 int const xbeg = o.x_ - tabular.textHOffset(cell);
5174 int const xend = xbeg + tabular.cellWidth(cell);
5175 row_type const row = tabular.cellRow(cell);
5176 int const ybeg = o.y_ - tabular.rowAscent(row)
5177 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5178 int const yend = ybeg + tabular.cellHeight(cell);
5190 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5191 // << " ybeg=" << ybeg << " yend=" << yend
5192 // << " xx=" << xx << " yy=" << yy
5193 // << " dist=" << xx + yy << endl;
5198 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5200 //lyxerr << "InsetTabular::editXY: " << this << endl;
5201 cur.selection(false);
5203 cur.idx() = getNearestCell(cur.bv(), x, y);
5204 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5208 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5210 cur.idx() = getNearestCell(cur.bv(), x, y);
5211 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5215 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5217 idx_type idx_min = 0;
5218 int dist_min = numeric_limits<int>::max();
5219 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5220 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5221 int const d = dist(bv, i, x, y);
5232 int InsetTabular::cellYPos(idx_type const cell) const
5234 row_type row = tabular.cellRow(cell);
5236 for (row_type r = 0; r < row; ++r)
5237 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5238 + tabular.interRowSpace(r + 1);
5243 int InsetTabular::cellXPos(idx_type const cell) const
5245 col_type col = tabular.cellColumn(cell);
5247 for (col_type c = 0; c < col; ++c)
5248 lx += tabular.column_info[c].width;
5253 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5255 row_type const row = tabular.cellRow(cur.idx());
5256 col_type const col = tabular.cellColumn(cur.idx());
5258 if (isRightToLeft(cur)) {
5259 if (tabular.cellColumn(cur.idx()) == 0) {
5260 if (row == tabular.nrows() - 1)
5262 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5267 cur.idx() = tabular.getLastCellInRow(row - 1);
5269 cur.idx() = tabular.cellIndex(row, col - 1);
5272 if (tabular.isLastCell(cur.idx()))
5274 if (cur.idx() == tabular.getLastCellInRow(row))
5275 cur.idx() = tabular.cellIndex(row + 1, 0);
5277 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5278 cur.idx() = tabular.cellIndex(row, colnextcell);
5282 cur.boundary(false);
5284 if (cur.selIsMultiCell()) {
5285 cur.pit() = cur.lastpit();
5286 cur.pos() = cur.lastpos();
5293 // in visual mode, place cursor at extreme left or right
5295 switch(entry_from) {
5297 case ENTRY_DIRECTION_RIGHT:
5298 cur.posVisToRowExtremity(false /* !left */);
5300 case ENTRY_DIRECTION_LEFT:
5301 cur.posVisToRowExtremity(true /* left */);
5303 case ENTRY_DIRECTION_IGNORE:
5304 // nothing to do in this case
5308 cur.setCurrentFont();
5312 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5314 row_type const row = tabular.cellRow(cur.idx());
5315 col_type const col = tabular.cellColumn(cur.idx());
5317 if (isRightToLeft(cur)) {
5318 if (cur.idx() == tabular.getLastCellInRow(row)) {
5321 cur.idx() = tabular.getFirstCellInRow(row);
5322 cur.idx() = tabular.cellAbove(cur.idx());
5324 if (tabular.isLastCell(cur.idx()))
5326 if (cur.idx() == tabular.getLastCellInRow(row))
5327 cur.idx() = tabular.cellIndex(row + 1, 0);
5329 cur.idx() = tabular.cellIndex(row, col + 1);
5332 if (cur.idx() == 0) // first cell
5335 cur.idx() = tabular.getLastCellInRow(row - 1);
5337 cur.idx() = tabular.cellIndex(row, col - 1);
5340 if (cur.selIsMultiCell()) {
5341 cur.pit() = cur.lastpit();
5342 cur.pos() = cur.lastpos();
5346 cur.pit() = cur.lastpit();
5347 cur.pos() = cur.lastpos();
5349 // in visual mode, place cursor at extreme left or right
5351 switch(entry_from) {
5353 case ENTRY_DIRECTION_RIGHT:
5354 cur.posVisToRowExtremity(false /* !left */);
5356 case ENTRY_DIRECTION_LEFT:
5357 cur.posVisToRowExtremity(true /* left */);
5359 case ENTRY_DIRECTION_IGNORE:
5360 // nothing to do in this case
5364 cur.setCurrentFont();
5368 void InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5370 cur.recordUndoInset(this);
5372 istringstream is(argument);
5375 size_t safe_guard = 0;
5380 if (safe_guard > 1000) {
5381 LYXERR0("parameter max count reached!");
5385 Tabular::Feature action = Tabular::LAST_ACTION;
5388 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5389 if (s != tabularFeature[i].feature)
5391 action = tabularFeature[i].action;
5394 if (action == Tabular::LAST_ACTION) {
5395 LYXERR0("Feature not found " << s);
5399 if (tabularFeature[i].need_value)
5401 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5402 tabularFeatures(cur, action, val);
5407 static void checkLongtableSpecial(Tabular::ltType & ltt,
5408 string const & special, bool & flag)
5410 if (special == "dl_above") {
5413 } else if (special == "dl_below") {
5414 ltt.bottomDL = flag;
5416 } else if (special == "empty") {
5426 bool InsetTabular::oneCellHasRotationState(bool rotated,
5427 row_type row_start, row_type row_end,
5428 col_type col_start, col_type col_end) const
5430 for (row_type r = row_start; r <= row_end; ++r)
5431 for (col_type c = col_start; c <= col_end; ++c)
5433 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5436 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5443 void InsetTabular::tabularFeatures(Cursor & cur,
5444 Tabular::Feature feature, string const & value)
5446 col_type sel_col_start;
5447 col_type sel_col_end;
5448 row_type sel_row_start;
5449 row_type sel_row_end;
5450 bool setLines = false;
5451 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5452 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5456 case Tabular::M_ALIGN_LEFT:
5457 case Tabular::ALIGN_LEFT:
5458 setAlign = LYX_ALIGN_LEFT;
5461 case Tabular::M_ALIGN_RIGHT:
5462 case Tabular::ALIGN_RIGHT:
5463 setAlign = LYX_ALIGN_RIGHT;
5466 case Tabular::M_ALIGN_CENTER:
5467 case Tabular::ALIGN_CENTER:
5468 setAlign = LYX_ALIGN_CENTER;
5471 case Tabular::ALIGN_BLOCK:
5472 setAlign = LYX_ALIGN_BLOCK;
5475 case Tabular::ALIGN_DECIMAL:
5476 setAlign = LYX_ALIGN_DECIMAL;
5479 case Tabular::M_VALIGN_TOP:
5480 case Tabular::VALIGN_TOP:
5481 setVAlign = Tabular::LYX_VALIGN_TOP;
5484 case Tabular::M_VALIGN_BOTTOM:
5485 case Tabular::VALIGN_BOTTOM:
5486 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5489 case Tabular::M_VALIGN_MIDDLE:
5490 case Tabular::VALIGN_MIDDLE:
5491 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5498 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5499 row_type const row = tabular.cellRow(cur.idx());
5500 col_type const column = tabular.cellColumn(cur.idx());
5502 Tabular::ltType ltt;
5506 case Tabular::SET_TABULAR_WIDTH:
5507 tabular.setTabularWidth(Length(value));
5510 case Tabular::SET_PWIDTH: {
5511 Length const len(value);
5512 tabular.setColumnPWidth(cur, cur.idx(), len);
5514 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5515 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5519 case Tabular::SET_MPWIDTH:
5520 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5523 case Tabular::SET_MROFFSET:
5524 tabular.setMROffset(cur, cur.idx(), Length(value));
5527 case Tabular::SET_SPECIAL_COLUMN:
5528 case Tabular::SET_SPECIAL_MULTICOLUMN:
5529 if (value == "none")
5530 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5532 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5535 case Tabular::APPEND_ROW:
5536 // append the row into the tabular
5537 tabular.appendRow(row);
5540 case Tabular::APPEND_COLUMN:
5541 // append the column into the tabular
5542 tabular.appendColumn(column);
5543 cur.idx() = tabular.cellIndex(row, column);
5546 case Tabular::DELETE_ROW:
5547 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5548 for (col_type c = 0; c < tabular.ncols(); c++)
5549 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5550 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5553 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5554 tabular.deleteRow(sel_row_start);
5555 if (sel_row_start >= tabular.nrows())
5557 cur.idx() = tabular.cellIndex(sel_row_start, column);
5560 cur.selection(false);
5563 case Tabular::DELETE_COLUMN:
5564 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5565 for (row_type r = 0; r < tabular.nrows(); r++)
5566 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5567 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5570 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5571 for (row_type r = 0; r < tabular.nrows(); r++)
5572 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5573 tabular.leftLine(tabular.cellIndex(r, 0)));
5576 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5577 tabular.deleteColumn(sel_col_start);
5578 if (sel_col_start >= tabular.ncols())
5580 cur.idx() = tabular.cellIndex(row, sel_col_start);
5583 cur.selection(false);
5586 case Tabular::COPY_ROW:
5587 tabular.copyRow(row);
5590 case Tabular::COPY_COLUMN:
5591 tabular.copyColumn(column);
5592 cur.idx() = tabular.cellIndex(row, column);
5595 case Tabular::MOVE_COLUMN_RIGHT:
5596 tabular.moveColumn(column, Tabular::RIGHT);
5597 cur.idx() = tabular.cellIndex(row, column + 1);
5600 case Tabular::MOVE_COLUMN_LEFT:
5601 tabular.moveColumn(column, Tabular::LEFT);
5602 cur.idx() = tabular.cellIndex(row, column - 1);
5605 case Tabular::MOVE_ROW_DOWN:
5606 tabular.moveRow(row, Tabular::DOWN);
5607 cur.idx() = tabular.cellIndex(row + 1, column);
5610 case Tabular::MOVE_ROW_UP:
5611 tabular.moveRow(row, Tabular::UP);
5612 cur.idx() = tabular.cellIndex(row - 1, column);
5615 case Tabular::SET_LINE_TOP:
5616 case Tabular::TOGGLE_LINE_TOP: {
5617 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5618 ? (value == "true") : !tabular.topLine(cur.idx());
5619 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5620 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5621 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5625 case Tabular::SET_LINE_BOTTOM:
5626 case Tabular::TOGGLE_LINE_BOTTOM: {
5627 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5628 ? (value == "true") : !tabular.bottomLine(cur.idx());
5629 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5630 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5631 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5635 case Tabular::SET_LINE_LEFT:
5636 case Tabular::TOGGLE_LINE_LEFT: {
5637 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5638 ? (value == "true") : !tabular.leftLine(cur.idx());
5639 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5640 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5641 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5645 case Tabular::SET_LINE_RIGHT:
5646 case Tabular::TOGGLE_LINE_RIGHT: {
5647 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5648 ? (value == "true") : !tabular.rightLine(cur.idx());
5649 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5650 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5651 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5655 case Tabular::M_ALIGN_LEFT:
5656 case Tabular::M_ALIGN_RIGHT:
5657 case Tabular::M_ALIGN_CENTER:
5658 case Tabular::ALIGN_LEFT:
5659 case Tabular::ALIGN_RIGHT:
5660 case Tabular::ALIGN_CENTER:
5661 case Tabular::ALIGN_BLOCK:
5662 case Tabular::ALIGN_DECIMAL:
5663 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5664 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5665 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5666 !tabular.getPWidth(c).zero());
5669 case Tabular::M_VALIGN_TOP:
5670 case Tabular::M_VALIGN_BOTTOM:
5671 case Tabular::M_VALIGN_MIDDLE:
5673 case Tabular::VALIGN_TOP:
5674 case Tabular::VALIGN_BOTTOM:
5675 case Tabular::VALIGN_MIDDLE:
5676 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5677 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5678 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5681 case Tabular::SET_MULTICOLUMN: {
5682 if (!cur.selection()) {
5683 // just multicol for one single cell
5684 // check whether we are completely in a multicol
5685 if (!tabular.isMultiColumn(cur.idx()))
5686 tabular.setMultiColumn(cur.idx(), 1,
5687 tabular.rightLine(cur.idx()));
5690 // we have a selection so this means we just add all these
5691 // cells to form a multicolumn cell
5692 idx_type const s_start = cur.selBegin().idx();
5693 row_type const col_start = tabular.cellColumn(s_start);
5694 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5695 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5696 tabular.rightLine(cur.selEnd().idx()));
5699 cur.selection(false);
5703 case Tabular::UNSET_MULTICOLUMN: {
5704 if (!cur.selection()) {
5705 if (tabular.isMultiColumn(cur.idx()))
5706 tabular.unsetMultiColumn(cur.idx());
5711 case Tabular::MULTICOLUMN: {
5712 if (!cur.selection()) {
5713 if (tabular.isMultiColumn(cur.idx()))
5714 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5716 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5720 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5721 row_type const r = sel_row_start;
5722 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5723 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5726 // If the selection contains at least one singlecol cell
5727 // or multiple multicol cells,
5728 // we assume the user will merge is to a single multicol
5730 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5732 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5736 case Tabular::SET_MULTIROW: {
5737 if (!cur.selection()) {
5738 // just multirow for one single cell
5739 // check whether we are completely in a multirow
5740 if (!tabular.isMultiRow(cur.idx()))
5741 tabular.setMultiRow(cur.idx(), 1,
5742 tabular.bottomLine(cur.idx()),
5743 tabular.getAlignment(cur.idx()));
5746 // we have a selection so this means we just add all this
5747 // cells to form a multirow cell
5748 idx_type const s_start = cur.selBegin().idx();
5749 row_type const row_start = tabular.cellRow(s_start);
5750 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5751 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5752 tabular.bottomLine(cur.selEnd().idx()),
5753 tabular.getAlignment(cur.selEnd().idx()));
5756 cur.selection(false);
5760 case Tabular::UNSET_MULTIROW: {
5761 if (!cur.selection()) {
5762 if (tabular.isMultiRow(cur.idx()))
5763 tabular.unsetMultiRow(cur.idx());
5768 case Tabular::MULTIROW: {
5769 if (!cur.selection()) {
5770 if (tabular.isMultiRow(cur.idx()))
5771 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5773 tabularFeatures(cur, Tabular::SET_MULTIROW);
5777 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5778 col_type const c = sel_col_start;
5779 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5780 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5783 // If the selection contains at least one singlerow cell
5784 // or multiple multirow cells,
5785 // we assume the user will merge is to a single multirow
5787 tabularFeatures(cur, Tabular::SET_MULTIROW);
5789 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5793 case Tabular::SET_ALL_LINES:
5795 case Tabular::UNSET_ALL_LINES:
5796 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5797 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5798 idx_type const cell = tabular.cellIndex(r, c);
5799 tabular.setTopLine(cell, setLines);
5800 tabular.setBottomLine(cell, setLines);
5801 tabular.setRightLine(cell, setLines);
5802 tabular.setLeftLine(cell, setLines);
5806 case Tabular::SET_BORDER_LINES:
5807 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5808 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5809 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5811 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5812 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5813 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5817 case Tabular::TOGGLE_LONGTABULAR:
5818 if (tabular.is_long_tabular)
5819 tabularFeatures(cur, Tabular::UNSET_LONGTABULAR);
5821 tabular.is_long_tabular = true;
5824 case Tabular::SET_LONGTABULAR:
5825 tabular.is_long_tabular = true;
5828 case Tabular::UNSET_LONGTABULAR:
5829 for (row_type r = 0; r < tabular.nrows(); ++r) {
5830 if (tabular.ltCaption(r)) {
5831 cur.idx() = tabular.cellIndex(r, 0);
5834 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5837 tabular.is_long_tabular = false;
5840 case Tabular::SET_ROTATE_TABULAR:
5841 tabular.rotate = convert<int>(value);
5844 case Tabular::UNSET_ROTATE_TABULAR:
5848 case Tabular::TOGGLE_ROTATE_TABULAR:
5849 // when pressing the rotate button we default to 90° rotation
5850 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5853 case Tabular::TABULAR_VALIGN_TOP:
5854 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5857 case Tabular::TABULAR_VALIGN_MIDDLE:
5858 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5861 case Tabular::TABULAR_VALIGN_BOTTOM:
5862 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5865 case Tabular::LONGTABULAR_ALIGN_LEFT:
5866 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5869 case Tabular::LONGTABULAR_ALIGN_CENTER:
5870 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5873 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5874 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5879 case Tabular::SET_ROTATE_CELL:
5880 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5881 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5882 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5885 case Tabular::UNSET_ROTATE_CELL:
5886 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5887 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5888 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5891 case Tabular::TOGGLE_ROTATE_CELL:
5893 bool oneNotRotated = oneCellHasRotationState(false,
5894 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5896 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5897 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5898 // when pressing the rotate cell button we default to 90° rotation
5900 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5902 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5907 case Tabular::SET_USEBOX: {
5908 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5909 if (val == tabular.getUsebox(cur.idx()))
5910 val = Tabular::BOX_NONE;
5911 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5912 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5913 tabular.setUsebox(tabular.cellIndex(r, c), val);
5917 case Tabular::UNSET_LTFIRSTHEAD:
5919 case Tabular::SET_LTFIRSTHEAD:
5920 tabular.getRowOfLTFirstHead(row, ltt);
5921 checkLongtableSpecial(ltt, value, flag);
5922 tabular.setLTHead(row, flag, ltt, true);
5925 case Tabular::UNSET_LTHEAD:
5927 case Tabular::SET_LTHEAD:
5928 tabular.getRowOfLTHead(row, ltt);
5929 checkLongtableSpecial(ltt, value, flag);
5930 tabular.setLTHead(row, flag, ltt, false);
5933 case Tabular::UNSET_LTFOOT:
5935 case Tabular::SET_LTFOOT:
5936 tabular.getRowOfLTFoot(row, ltt);
5937 checkLongtableSpecial(ltt, value, flag);
5938 tabular.setLTFoot(row, flag, ltt, false);
5941 case Tabular::UNSET_LTLASTFOOT:
5943 case Tabular::SET_LTLASTFOOT:
5944 tabular.getRowOfLTLastFoot(row, ltt);
5945 checkLongtableSpecial(ltt, value, flag);
5946 tabular.setLTFoot(row, flag, ltt, true);
5949 case Tabular::UNSET_LTNEWPAGE:
5951 case Tabular::SET_LTNEWPAGE:
5952 tabular.setLTNewPage(row, flag);
5955 case Tabular::SET_LTCAPTION: {
5956 if (tabular.ltCaption(row))
5958 cur.idx() = tabular.setLTCaption(row, true);
5961 cur.selection(false);
5962 // If a row is set as caption, then also insert
5963 // a caption. Otherwise the LaTeX output is broken.
5964 // Select cell if it is non-empty
5965 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5966 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5967 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5971 case Tabular::UNSET_LTCAPTION: {
5972 if (!tabular.ltCaption(row))
5974 cur.idx() = tabular.setLTCaption(row, false);
5977 cur.selection(false);
5978 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5979 if (lyx::getStatus(fr).enabled())
5984 case Tabular::TOGGLE_LTCAPTION: {
5985 if (tabular.ltCaption(row))
5986 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5988 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5992 case Tabular::TOGGLE_BOOKTABS:
5993 tabular.use_booktabs = !tabular.use_booktabs;
5996 case Tabular::SET_BOOKTABS:
5997 tabular.use_booktabs = true;
6000 case Tabular::UNSET_BOOKTABS:
6001 tabular.use_booktabs = false;
6004 case Tabular::SET_TOP_SPACE: {
6006 if (value == "default")
6007 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6008 tabular.row_info[r].top_space_default = true;
6009 else if (value == "none")
6010 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6011 tabular.row_info[r].top_space_default = false;
6012 tabular.row_info[r].top_space = len;
6014 else if (isValidLength(value, &len))
6015 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6016 tabular.row_info[r].top_space_default = false;
6017 tabular.row_info[r].top_space = len;
6022 case Tabular::SET_BOTTOM_SPACE: {
6024 if (value == "default")
6025 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6026 tabular.row_info[r].bottom_space_default = true;
6027 else if (value == "none")
6028 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6029 tabular.row_info[r].bottom_space_default = false;
6030 tabular.row_info[r].bottom_space = len;
6032 else if (isValidLength(value, &len))
6033 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6034 tabular.row_info[r].bottom_space_default = false;
6035 tabular.row_info[r].bottom_space = len;
6040 case Tabular::SET_INTERLINE_SPACE: {
6042 if (value == "default")
6043 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
6044 tabular.row_info[r].interline_space_default = true;
6045 else if (value == "none")
6046 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6047 tabular.row_info[r].interline_space_default = false;
6048 tabular.row_info[r].interline_space = len;
6050 else if (isValidLength(value, &len))
6051 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
6052 tabular.row_info[r].interline_space_default = false;
6053 tabular.row_info[r].interline_space = len;
6058 case Tabular::SET_DECIMAL_POINT:
6059 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
6060 tabular.column_info[c].decimal_point = from_utf8(value);
6063 // dummy stuff just to avoid warnings
6064 case Tabular::LAST_ACTION:
6070 bool InsetTabular::copySelection(Cursor & cur)
6072 if (!cur.selection())
6077 getSelection(cur, rs, re, cs, ce);
6079 paste_tabular.reset(new Tabular(tabular));
6081 for (row_type r = 0; r < rs; ++r)
6082 paste_tabular->deleteRow(0);
6084 row_type const rows = re - rs + 1;
6085 while (paste_tabular->nrows() > rows)
6086 paste_tabular->deleteRow(rows);
6088 for (col_type c = 0; c < cs; ++c)
6089 paste_tabular->deleteColumn(0);
6091 col_type const columns = ce - cs + 1;
6092 while (paste_tabular->ncols() > columns)
6093 paste_tabular->deleteColumn(columns);
6095 paste_tabular->setBuffer(tabular.buffer());
6097 odocstringstream os;
6098 OutputParams const runparams(0);
6099 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6100 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6101 cap::copySelection(cur, os.str());
6103 // mark tabular stack dirty
6104 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6105 // when we (hopefully) have a one-for-all paste mechanism.
6106 // This must be called after cap::copySelection.
6107 dirtyTabularStack(true);
6113 bool InsetTabular::pasteClipboard(Cursor & cur)
6117 col_type actcol = tabular.cellColumn(cur.idx());
6118 row_type actrow = tabular.cellRow(cur.idx());
6120 if (cur.selIsMultiCell()) {
6123 getSelection(cur, actrow, re, actcol, ce);
6126 for (row_type r1 = 0, r2 = actrow;
6127 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6129 for (col_type c1 = 0, c2 = actcol;
6130 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6132 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6133 tabular.isPartOfMultiColumn(r2, c2))
6135 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6139 if (tabular.isPartOfMultiColumn(r2, c2)) {
6143 shared_ptr<InsetTableCell> inset(
6144 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6145 tabular.setCellInset(r2, c2, inset);
6146 // FIXME?: why do we need to do this explicitly? (EL)
6147 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6149 // FIXME: change tracking (MG)
6150 inset->setChange(Change(buffer().params().track_changes ?
6151 Change::INSERTED : Change::UNCHANGED));
6159 void InsetTabular::cutSelection(Cursor & cur)
6161 if (!cur.selection())
6166 getSelection(cur, rs, re, cs, ce);
6167 for (row_type r = rs; r <= re; ++r) {
6168 for (col_type c = cs; c <= ce; ++c) {
6169 shared_ptr<InsetTableCell> t
6170 = cell(tabular.cellIndex(r, c));
6171 if (buffer().params().track_changes)
6172 // FIXME: Change tracking (MG)
6173 t->setChange(Change(Change::DELETED));
6179 // cursor position might be invalid now
6180 if (cur.pit() > cur.lastpit())
6181 cur.pit() = cur.lastpit();
6182 if (cur.pos() > cur.lastpos())
6183 cur.pos() = cur.lastpos();
6184 cur.clearSelection();
6188 bool InsetTabular::isRightToLeft(Cursor & cur) const
6190 // LASSERT: It might be better to abandon this Buffer.
6191 LASSERT(cur.depth() > 1, return false);
6192 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6193 pos_type const parentpos = cur[cur.depth() - 2].pos();
6194 return parentpar.getFontSettings(buffer().params(),
6195 parentpos).language()->rightToLeft();
6199 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6202 LASSERT(stidx <= enidx, return docstring());
6204 col_type const col1 = tabular.cellColumn(stidx);
6205 col_type const col2 = tabular.cellColumn(enidx);
6206 row_type const row1 = tabular.cellRow(stidx);
6207 row_type const row2 = tabular.cellRow(enidx);
6209 for (col_type col = col1; col <= col2; col++)
6210 for (row_type row = row1; row <= row2; row++) {
6215 retval += tabular.cellInset(row, col)->asString(intoInsets);
6221 void InsetTabular::getSelection(Cursor & cur,
6222 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6224 CursorSlice const & beg = cur.selBegin();
6225 CursorSlice const & end = cur.selEnd();
6226 cs = tabular.cellColumn(beg.idx());
6227 ce = tabular.cellColumn(end.idx());
6231 rs = tabular.cellRow(beg.idx());
6232 re = tabular.cellRow(end.idx());
6238 Text * InsetTabular::getText(int idx) const
6240 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6244 void InsetTabular::setChange(Change const & change)
6246 for (idx_type idx = 0; idx < nargs(); ++idx)
6247 cell(idx)->setChange(change);
6251 void InsetTabular::acceptChanges()
6253 for (idx_type idx = 0; idx < nargs(); ++idx)
6254 cell(idx)->acceptChanges();
6258 void InsetTabular::rejectChanges()
6260 for (idx_type idx = 0; idx < nargs(); ++idx)
6261 cell(idx)->rejectChanges();
6265 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6267 return tabular.getPWidth(cell).zero();
6271 bool InsetTabular::forcePlainLayout(idx_type cell) const
6273 return !tabular.getPWidth(cell).zero();
6277 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6280 if (buf.length() <= 0)
6285 col_type maxCols = 1;
6286 size_t const len = buf.length();
6290 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6298 maxCols = max(cols, maxCols);
6304 maxCols = max(cols, maxCols);
6310 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6311 loctab = paste_tabular.get();
6312 dirtyTabularStack(true);
6315 cell = bv.cursor().idx();
6316 ocol = tabular.cellColumn(cell);
6317 row = tabular.cellRow(cell);
6321 idx_type const cells = loctab->numberofcells;
6324 rows = loctab->nrows();
6325 col_type const columns = loctab->ncols();
6327 while (cell < cells && p < len && row < rows &&
6328 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6334 // we can only set this if we are not too far right
6335 if (cols < columns) {
6336 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6337 Font const font = bv.textMetrics(&inset->text()).
6339 inset->setText(buf.substr(op, p - op), font,
6340 buffer().params().track_changes);
6346 // we can only set this if we are not too far right
6347 if (cols < columns) {
6348 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6349 Font const font = bv.textMetrics(&inset->text()).
6351 inset->setText(buf.substr(op, p - op), font,
6352 buffer().params().track_changes);
6357 cell = loctab->cellIndex(row, cols);
6363 // check for the last cell if there is no trailing '\n'
6364 if (cell < cells && op < len) {
6365 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6366 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6367 inset->setText(buf.substr(op, len - op), font,
6368 buffer().params().track_changes);
6374 void InsetTabular::addPreview(DocIterator const & inset_pos,
6375 PreviewLoader & loader) const
6377 DocIterator cell_pos = inset_pos;
6379 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6380 for (row_type r = 0; r < tabular.nrows(); ++r) {
6381 for (col_type c = 0; c < tabular.ncols(); ++c) {
6382 cell_pos.top().idx() = tabular.cellIndex(r, c);
6383 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6389 bool InsetTabular::completionSupported(Cursor const & cur) const
6391 Cursor const & bvCur = cur.bv().cursor();
6392 if (&bvCur.inset() != this)
6394 return cur.text()->completionSupported(cur);
6398 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6400 return completionSupported(cur);
6404 bool InsetTabular::automaticInlineCompletion() const
6406 return lyxrc.completion_inline_text;
6410 bool InsetTabular::automaticPopupCompletion() const
6412 return lyxrc.completion_popup_text;
6416 bool InsetTabular::showCompletionCursor() const
6418 return lyxrc.completion_cursor_text;
6422 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6424 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6428 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6430 if (!completionSupported(cur))
6432 return cur.text()->completionPrefix(cur);
6436 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6438 if (!completionSupported(cur))
6441 return cur.text()->insertCompletion(cur, s, finished);
6445 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6446 Dimension & dim) const
6448 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6449 tm.completionPosAndDim(cur, x, y, dim);
6453 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6455 istringstream data(in);
6457 lex.setStream(data);
6464 if (!lex || token != "tabular") {
6465 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6469 // This is part of the inset proper that is usually swallowed
6470 // by Buffer::readInset
6472 if (!lex || token != "Tabular") {
6473 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6481 string InsetTabular::params2string(InsetTabular const & inset)
6484 data << "tabular" << ' ';
6486 data << "\\end_inset\n";
6491 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6493 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6494 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6495 if (!tabular.isPartOfMultiColumn(r,c) &&
6496 !tabular.isPartOfMultiRow(r,c))
6499 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6500 ParagraphList::iterator it = parlist.begin();
6501 ParagraphList::iterator const en = parlist.end();
6502 for (; it != en; ++it)
6503 it->setLayout(dc.plainLayout());