2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
16 * \author Scott Kostyshak
18 * Full author contact details are available in file CREDITS.
23 #include "InsetTabular.h"
25 #include "buffer_funcs.h"
27 #include "BufferParams.h"
28 #include "BufferView.h"
29 #include "CoordCache.h"
32 #include "CutAndPaste.h"
33 #include "DispatchResult.h"
34 #include "FuncRequest.h"
35 #include "FuncStatus.h"
36 #include "InsetList.h"
38 #include "LaTeXFeatures.h"
42 #include "MetricsInfo.h"
43 #include "OutputParams.h"
44 #include "output_xhtml.h"
45 #include "Paragraph.h"
46 #include "ParagraphParameters.h"
47 #include "ParIterator.h"
48 #include "TextClass.h"
49 #include "TextMetrics.h"
51 #include "frontends/Application.h"
52 #include "frontends/alert.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "support/convert.h"
58 #include "support/debug.h"
59 #include "support/docstream.h"
60 #include "support/FileName.h"
61 #include "support/gettext.h"
62 #include "support/lassert.h"
63 #include "support/lstrings.h"
65 #include <boost/scoped_ptr.hpp>
73 using namespace lyx::support;
79 using cap::dirtyTabularStack;
80 using cap::tabularStackDirty;
82 using graphics::PreviewLoader;
84 using frontend::Painter;
85 using frontend::Clipboard;
87 namespace Alert = frontend::Alert;
92 int const ADD_TO_HEIGHT = 2; // in cell
93 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
94 int const default_line_space = 10; // ?
95 int const WIDTH_OF_LINE = 5; // space between double lines
99 boost::scoped_ptr<Tabular> paste_tabular;
102 struct TabularFeature {
103 Tabular::Feature action;
109 TabularFeature tabularFeature[] =
111 // the SET/UNSET actions are used by the table dialog,
112 // the TOGGLE actions by the table toolbar buttons
113 { Tabular::APPEND_ROW, "append-row", false },
114 { Tabular::APPEND_COLUMN, "append-column", false },
115 { Tabular::DELETE_ROW, "delete-row", false },
116 { Tabular::DELETE_COLUMN, "delete-column", false },
117 { Tabular::COPY_ROW, "copy-row", false },
118 { Tabular::COPY_COLUMN, "copy-column", false },
119 { Tabular::MOVE_COLUMN_RIGHT, "move-column-right", false },
120 { Tabular::MOVE_COLUMN_LEFT, "move-column-left", false },
121 { Tabular::MOVE_ROW_DOWN, "move-row-down", false },
122 { Tabular::MOVE_ROW_UP, "move-row-up", false },
123 { Tabular::SET_LINE_TOP, "set-line-top", true },
124 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
125 { Tabular::SET_LINE_LEFT, "set-line-left", true },
126 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
127 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
128 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
129 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
130 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
131 { Tabular::ALIGN_LEFT, "align-left", false },
132 { Tabular::ALIGN_RIGHT, "align-right", false },
133 { Tabular::ALIGN_CENTER, "align-center", false },
134 { Tabular::ALIGN_BLOCK, "align-block", false },
135 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
136 { Tabular::VALIGN_TOP, "valign-top", false },
137 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
138 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
139 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
140 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
141 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
142 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
143 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
144 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
145 { Tabular::MULTICOLUMN, "multicolumn", false },
146 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
147 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
148 { Tabular::MULTIROW, "multirow", false },
149 { Tabular::SET_MULTIROW, "set-multirow", false },
150 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
151 { Tabular::SET_MROFFSET, "set-mroffset", true },
152 { Tabular::SET_ALL_LINES, "set-all-lines", false },
153 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
154 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
155 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
156 { Tabular::SET_PWIDTH, "set-pwidth", true },
157 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
158 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
159 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
160 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
161 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
162 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
163 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
164 { Tabular::SET_USEBOX, "set-usebox", true },
165 { Tabular::SET_LTHEAD, "set-lthead", true },
166 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
167 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
168 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
169 { Tabular::SET_LTFOOT, "set-ltfoot", true },
170 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
171 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
172 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
173 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
174 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
175 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
176 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
177 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
178 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
179 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
180 { Tabular::SET_BOOKTABS, "set-booktabs", false },
181 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
182 { Tabular::SET_TOP_SPACE, "set-top-space", true },
183 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
184 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
185 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
186 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
187 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
188 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
189 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
190 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
191 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
192 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
193 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
194 { Tabular::LAST_ACTION, "", false }
198 string const tostr(LyXAlignment const & num)
203 case LYX_ALIGN_BLOCK:
207 case LYX_ALIGN_CENTER:
209 case LYX_ALIGN_RIGHT:
211 case LYX_ALIGN_LAYOUT:
213 case LYX_ALIGN_SPECIAL:
215 case LYX_ALIGN_DECIMAL:
222 string const tostr(Tabular::HAlignment const & num)
225 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
227 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
229 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
236 string const tostr(Tabular::VAlignment const & num)
239 case Tabular::LYX_VALIGN_TOP:
241 case Tabular::LYX_VALIGN_MIDDLE:
243 case Tabular::LYX_VALIGN_BOTTOM:
250 string const tostr(Tabular::BoxType const & num)
253 case Tabular::BOX_NONE:
255 case Tabular::BOX_PARBOX:
257 case Tabular::BOX_MINIPAGE:
264 // I would have liked a fromstr template a lot better. (Lgb)
265 bool string2type(string const & str, LyXAlignment & num)
268 num = LYX_ALIGN_NONE;
269 else if (str == "block")
270 num = LYX_ALIGN_BLOCK;
271 else if (str == "left")
272 num = LYX_ALIGN_LEFT;
273 else if (str == "center")
274 num = LYX_ALIGN_CENTER;
275 else if (str == "right")
276 num = LYX_ALIGN_RIGHT;
277 else if (str == "decimal")
278 num = LYX_ALIGN_DECIMAL;
285 bool string2type(string const & str, Tabular::HAlignment & num)
288 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
289 else if (str == "center" )
290 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
291 else if (str == "right")
292 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
299 bool string2type(string const & str, Tabular::VAlignment & num)
302 num = Tabular::LYX_VALIGN_TOP;
303 else if (str == "middle" )
304 num = Tabular::LYX_VALIGN_MIDDLE;
305 else if (str == "bottom")
306 num = Tabular::LYX_VALIGN_BOTTOM;
313 bool string2type(string const & str, Tabular::BoxType & num)
316 num = Tabular::BOX_NONE;
317 else if (str == "parbox")
318 num = Tabular::BOX_PARBOX;
319 else if (str == "minipage")
320 num = Tabular::BOX_MINIPAGE;
327 bool string2type(string const & str, bool & num)
331 else if (str == "false")
339 bool getTokenValue(string const & str, char const * token, string & ret)
342 size_t token_length = strlen(token);
343 size_t pos = str.find(token);
345 if (pos == string::npos || pos + token_length + 1 >= str.length()
346 || str[pos + token_length] != '=')
348 pos += token_length + 1;
350 if (ch != '"' && ch != '\'') { // only read till next space
354 while (pos < str.length() - 1 && str[++pos] != ch)
361 bool getTokenValue(string const & str, char const * token, docstring & ret)
364 bool const success = getTokenValue(str, token, tmp);
365 ret = from_utf8(tmp);
370 bool getTokenValue(string const & str, char const * token, int & num)
374 if (!getTokenValue(str, token, tmp))
376 num = convert<int>(tmp);
381 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
384 return getTokenValue(str, token, tmp) && string2type(tmp, num);
388 bool getTokenValue(string const & str, char const * token,
389 Tabular::HAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::VAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::BoxType & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token, bool & flag)
414 // set the flag always to false as this should be the default for bools
415 // not in the file-format.
418 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
422 bool getTokenValue(string const & str, char const * token, Length & len)
424 // set the length to be zero() as default as this it should be if not
425 // in the file format.
428 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
432 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
437 if (!getTokenValue(str, token, tmp))
439 if (tmp == "default") {
443 return isValidLength(tmp, &len);
447 void l_getline(istream & is, string & str)
450 while (str.empty()) {
452 if (!str.empty() && str[str.length() - 1] == '\r')
453 str.erase(str.length() - 1);
458 string const write_attribute(string const & name, T const & t)
460 string const s = tostr(t);
461 return s.empty() ? s : " " + name + "=\"" + s + "\"";
465 string const write_attribute(string const & name, string const & t)
467 return t.empty() ? t : " " + name + "=\"" + t + "\"";
472 string const write_attribute(string const & name, docstring const & t)
474 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
479 string const write_attribute(string const & name, bool const & b)
481 // we write only true attribute values so we remove a bit of the
482 // file format bloat for tabulars.
483 return b ? write_attribute(name, convert<string>(b)) : string();
488 string const write_attribute(string const & name, int const & i)
490 // we write only true attribute values so we remove a bit of the
491 // file format bloat for tabulars.
492 return i ? write_attribute(name, convert<string>(i)) : string();
497 string const write_attribute(string const & name, Tabular::idx_type const & i)
499 // we write only true attribute values so we remove a bit of the
500 // file format bloat for tabulars.
501 return i ? write_attribute(name, convert<string>(i)) : string();
506 string const write_attribute(string const & name, Length const & value)
508 // we write only the value if we really have one same reson as above.
509 return value.zero() ? string() : write_attribute(name, value.asString());
515 string const featureAsString(Tabular::Feature action)
517 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
518 if (tabularFeature[i].action == action)
519 return tabularFeature[i].feature;
525 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
527 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
528 for (; dit; dit.forwardChar())
529 if (dit.inTexted() && dit.depth() == 1
530 && dit.paragraph().find(align_d, false, false, dit.pos()))
537 InsetTableCell splitCell(InsetTableCell & head, docstring const & align_d, bool & hassep)
539 InsetTableCell tail = InsetTableCell(head);
540 DocIterator const dit = separatorPos(&head, align_d);
543 pit_type const psize = head.paragraphs().front().size();
544 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
545 tail.paragraphs().front().eraseChars(0,
546 dit.pos() < psize ? dit.pos() + 1 : psize, false);
553 /////////////////////////////////////////////////////////////////////
557 /////////////////////////////////////////////////////////////////////
560 Tabular::CellData::CellData(Buffer * buf)
563 multicolumn(Tabular::CELL_NORMAL),
564 multirow(Tabular::CELL_NORMAL),
565 alignment(LYX_ALIGN_CENTER),
566 valignment(LYX_VALIGN_TOP),
576 inset(new InsetTableCell(buf))
578 inset->setBuffer(*buf);
582 Tabular::CellData::CellData(CellData const & cs)
585 multicolumn(cs.multicolumn),
586 multirow(cs.multirow),
587 mroffset(cs.mroffset),
588 alignment(cs.alignment),
589 valignment(cs.valignment),
590 decimal_hoffset(cs.decimal_hoffset),
591 decimal_width(cs.decimal_width),
593 top_line(cs.top_line),
594 bottom_line(cs.bottom_line),
595 left_line(cs.left_line),
596 right_line(cs.right_line),
599 align_special(cs.align_special),
601 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
605 Tabular::CellData & Tabular::CellData::operator=(CellData const & cs)
611 multicolumn = cs.multicolumn;
612 multirow = cs.multirow;
613 mroffset = cs.mroffset;
614 alignment = cs.alignment;
615 valignment = cs.valignment;
616 decimal_hoffset = cs.decimal_hoffset;
617 decimal_width = cs.decimal_width;
618 voffset = cs.voffset;
619 top_line = cs.top_line;
620 bottom_line = cs.bottom_line;
621 left_line = cs.left_line;
622 right_line = cs.right_line;
625 align_special = cs.align_special;
626 p_width = cs.p_width;
627 inset.reset(static_cast<InsetTableCell *>(cs.inset->clone()));
631 Tabular::RowData::RowData()
634 top_space_default(false),
635 bottom_space_default(false),
636 interline_space_default(false),
646 Tabular::ColumnData::ColumnData()
647 : alignment(LYX_ALIGN_CENTER),
648 valignment(LYX_VALIGN_TOP),
654 Tabular::ltType::ltType()
655 : set(false), topDL(false),
661 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
663 init(buffer, rows_arg, columns_arg);
667 void Tabular::setBuffer(Buffer & buffer)
670 for (row_type i = 0; i < nrows(); ++i)
671 for (col_type j = 0; j < ncols(); ++j)
672 cell_info[i][j].inset->setBuffer(*buffer_);
676 // activates all lines and sets all widths to 0
677 void Tabular::init(Buffer * buf, row_type rows_arg,
678 col_type columns_arg)
681 row_info = row_vector(rows_arg);
682 column_info = column_vector(columns_arg);
683 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
684 row_info.reserve(10);
685 column_info.reserve(10);
686 cell_info.reserve(100);
688 is_long_tabular = false;
689 tabular_valignment = LYX_VALIGN_MIDDLE;
690 tabular_width = Length();
691 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
693 use_booktabs = false;
694 // set silly default lines
695 for (row_type r = 0; r < nrows(); ++r)
696 for (col_type c = 0; c < ncols(); ++c) {
697 cell_info[r][c].inset->setBuffer(*buffer_);
698 cell_info[r][c].top_line = true;
699 cell_info[r][c].left_line = true;
700 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
701 cell_info[r][c].right_line = c == ncols() - 1;
706 void Tabular::deleteRow(row_type const row)
708 // Not allowed to delete last row
712 for (col_type c = 0; c < ncols(); ++c) {
713 // Care about multirow cells
714 if (row + 1 < nrows() &&
715 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
716 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
717 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
720 row_info.erase(row_info.begin() + row);
721 cell_info.erase(cell_info.begin() + row);
726 void Tabular::copyRow(row_type const row)
728 insertRow(row, true);
732 void Tabular::appendRow(row_type row)
734 insertRow(row, false);
738 void Tabular::insertRow(row_type const row, bool copy)
740 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
741 cell_info.insert(cell_info.begin() + row + 1,
742 cell_vector(0, CellData(buffer_)));
744 for (col_type c = 0; c < ncols(); ++c) {
745 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
746 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
747 if (buffer().params().track_changes)
748 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
749 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
750 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
754 for (col_type c = 0; c < ncols(); ++c) {
755 if (isPartOfMultiRow(row, c))
757 // inherit line settings
758 idx_type const i = cellIndex(row + 1, c);
759 idx_type const j = cellIndex(row, c);
760 setLeftLine(i, leftLine(j));
761 setRightLine(i, rightLine(j));
762 setTopLine(i, topLine(j));
763 if (topLine(j) && bottomLine(j)) {
764 setBottomLine(i, true);
765 setBottomLine(j, false);
767 // mark track changes
768 if (buffer().params().track_changes)
769 cellInfo(i).inset->setChange(Change(Change::INSERTED));
774 void Tabular::moveColumn(col_type col, ColDirection direction)
776 if (direction == Tabular::LEFT)
779 std::swap(column_info[col], column_info[col + 1]);
781 for (row_type r = 0; r < nrows(); ++r) {
782 std::swap(cell_info[r][col], cell_info[r][col + 1]);
783 std::swap(cell_info[r][col].left_line, cell_info[r][col + 1].left_line);
784 std::swap(cell_info[r][col].right_line, cell_info[r][col + 1].right_line);
786 // FIXME track changes is broken for tabular features (#8469)
787 idx_type const i = cellIndex(r, col);
788 idx_type const j = cellIndex(r, col + 1);
789 if (buffer().params().track_changes) {
790 cellInfo(i).inset->setChange(Change(Change::INSERTED));
791 cellInfo(j).inset->setChange(Change(Change::INSERTED));
798 void Tabular::moveRow(row_type row, RowDirection direction)
800 if (direction == Tabular::UP)
803 std::swap(row_info[row], row_info[row + 1]);
805 for (col_type c = 0; c < ncols(); ++c) {
806 std::swap(cell_info[row][c], cell_info[row + 1][c]);
807 std::swap(cell_info[row][c].top_line, cell_info[row + 1][c].top_line);
808 std::swap(cell_info[row][c].bottom_line, cell_info[row + 1][c].bottom_line);
810 // FIXME track changes is broken for tabular features (#8469)
811 idx_type const i = cellIndex(row, c);
812 idx_type const j = cellIndex(row + 1, c);
813 if (buffer().params().track_changes) {
814 cellInfo(i).inset->setChange(Change(Change::INSERTED));
815 cellInfo(j).inset->setChange(Change(Change::INSERTED));
822 void Tabular::deleteColumn(col_type const col)
824 // Not allowed to delete last column
828 for (row_type r = 0; r < nrows(); ++r) {
829 // Care about multicolumn cells
830 if (col + 1 < ncols() &&
831 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
832 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
833 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
835 cell_info[r].erase(cell_info[r].begin() + col);
837 column_info.erase(column_info.begin() + col);
842 void Tabular::copyColumn(col_type const col)
844 insertColumn(col, true);
848 void Tabular::appendColumn(col_type col)
850 insertColumn(col, false);
854 void Tabular::insertColumn(col_type const col, bool copy)
856 BufferParams const & bp = buffer().params();
857 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
859 for (row_type r = 0; r < nrows(); ++r) {
860 cell_info[r].insert(cell_info[r].begin() + col + 1,
861 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
862 if (bp.track_changes)
863 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
864 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
865 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
868 for (row_type r = 0; r < nrows(); ++r) {
869 // inherit line settings
870 idx_type const i = cellIndex(r, col + 1);
871 idx_type const j = cellIndex(r, col);
872 setBottomLine(i, bottomLine(j));
873 setTopLine(i, topLine(j));
874 setLeftLine(i, leftLine(j));
875 if (rightLine(i) && rightLine(j)) {
876 setRightLine(i, true);
877 setRightLine(j, false);
879 if (buffer().params().track_changes)
880 cellInfo(i).inset->setChange(Change(Change::INSERTED));
885 void Tabular::updateIndexes()
890 for (row_type row = 0; row < nrows(); ++row)
891 for (col_type column = 0; column < ncols(); ++column) {
892 if (!isPartOfMultiColumn(row, column)
893 && !isPartOfMultiRow(row, column))
895 if (isPartOfMultiRow(row, column))
896 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
898 cell_info[row][column].cellno = numberofcells - 1;
901 rowofcell.resize(numberofcells);
902 columnofcell.resize(numberofcells);
904 // reset column and row of cells and update their width and alignment
905 for (row_type row = 0; row < nrows(); ++row)
906 for (col_type column = 0; column < ncols(); ++column) {
907 if (isPartOfMultiColumn(row, column))
909 // columnofcell needs to be called before setting width and aligment
910 // multirow cells inherit the width from the column width
911 if (!isPartOfMultiRow(row, column)) {
912 columnofcell[i] = column;
915 setFixedWidth(row, column);
916 if (isPartOfMultiRow(row, column))
918 cell_info[row][column].inset->setContentAlignment(
919 getAlignment(cellIndex(row, column)));
925 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
928 for (col_type c = 0; c < ncols(); ++c)
929 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
935 bool Tabular::topLine(idx_type const cell) const
937 return cellInfo(cell).top_line;
941 bool Tabular::bottomLine(idx_type const cell) const
943 return cellInfo(cell).bottom_line;
947 bool Tabular::leftLine(idx_type cell) const
951 return cellInfo(cell).left_line;
955 bool Tabular::rightLine(idx_type cell) const
959 return cellInfo(cell).right_line;
963 int Tabular::interRowSpace(row_type row) const
965 if (!row || row >= nrows())
968 int const interline_space = row_info[row - 1].interline_space_default ?
970 row_info[row - 1].interline_space.inPixels(width());
971 if (rowTopLine(row) && rowBottomLine(row - 1))
972 return interline_space + WIDTH_OF_LINE;
973 return interline_space;
977 int Tabular::interColumnSpace(idx_type cell) const
979 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
980 if (rightLine(cell) && nextcol < ncols()
981 && leftLine(cellIndex(cellRow(cell), nextcol)))
982 return WIDTH_OF_LINE;
987 int Tabular::cellWidth(idx_type cell) const
990 col_type const span = columnSpan(cell);
991 col_type const col = cellColumn(cell);
992 for(col_type c = col; c < col + span ; ++c)
993 w += column_info[c].width;
998 int Tabular::cellHeight(idx_type cell) const
1000 row_type const span = rowSpan(cell);
1001 row_type const row = cellRow(cell);
1003 for(row_type r = row; r < row + span ; ++r) {
1004 h += rowAscent(r) + rowDescent(r);
1005 if (r != row + span - 1)
1006 h += interRowSpace(r + 1);
1013 bool Tabular::updateColumnWidths()
1015 vector<int> max_dwidth(ncols(), 0);
1016 for(col_type c = 0; c < ncols(); ++c)
1017 for(row_type r = 0; r < nrows(); ++r) {
1018 idx_type const i = cellIndex(r, c);
1019 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1020 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1023 bool update = false;
1024 // for each col get max of single col cells
1025 for(col_type c = 0; c < ncols(); ++c) {
1027 for(row_type r = 0; r < nrows(); ++r) {
1028 idx_type const i = cellIndex(r, c);
1029 if (columnSpan(i) == 1) {
1030 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1031 && cell_info[r][c].decimal_width!=0)
1032 new_width = max(new_width, cellInfo(i).width
1033 + max_dwidth[c] - cellInfo(i).decimal_width);
1035 new_width = max(new_width, cellInfo(i).width);
1039 if (column_info[c].width != new_width) {
1040 column_info[c].width = new_width;
1044 // update col widths to fit merged cells
1045 for(col_type c = 0; c < ncols(); ++c)
1046 for(row_type r = 0; r < nrows(); ++r) {
1047 idx_type const i = cellIndex(r, c);
1048 int const span = columnSpan(i);
1049 if (span == 1 || c > cellColumn(i))
1053 for(col_type j = c; j < c + span ; ++j)
1054 old_width += column_info[j].width;
1056 if (cellInfo(i).width > old_width) {
1057 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1066 int Tabular::width() const
1069 for (col_type c = 0; c < ncols(); ++c)
1070 width += column_info[c].width;
1075 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1078 col_type const col = cellColumn(cell);
1079 // set alignment for the whole row if we are not in a multicolumn cell,
1080 // exclude possible multicolumn cells in the row
1081 if (!isMultiColumn(cell)) {
1082 for (row_type r = 0; r < nrows(); ++r) {
1083 // only if the column has no width the multirow inherits the
1084 // alignment of the column, otherwise it is left aligned
1085 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1086 && !isMultiColumn(cellIndex(r, col))) {
1087 cell_info[r][col].alignment = align;
1088 cell_info[r][col].inset->setContentAlignment(align);
1090 if ((isMultiRow(cellIndex(r, col)) && has_width)
1091 && !isMultiColumn(cellIndex(r, col))) {
1092 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1093 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1096 column_info[col].alignment = align;
1097 docstring & dpoint = column_info[col].decimal_point;
1098 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1099 dpoint = from_utf8(lyxrc.default_decimal_point);
1101 cellInfo(cell).alignment = align;
1102 cellInset(cell).get()->setContentAlignment(align);
1107 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1110 if (!isMultiColumn(cell) || onlycolumn)
1111 column_info[cellColumn(cell)].valignment = align;
1113 cellInfo(cell).valignment = align;
1120 * Allow line and paragraph breaks for fixed width cells or disallow them,
1121 * merge cell paragraphs and reset layout to standard for variable width
1124 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1126 inset->toggleFixedWidth(fixedWidth);
1130 // merge all paragraphs to one
1131 BufferParams const & bp = cur.bv().buffer().params();
1132 while (inset->paragraphs().size() > 1)
1133 mergeParagraph(bp, inset->paragraphs(), 0);
1137 // undo information has already been recorded
1138 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1139 bp.documentClass().plainLayoutName());
1146 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1147 Length const & width)
1149 col_type const c = cellColumn(cell);
1151 column_info[c].p_width = width;
1152 // reset the vertical alignment to top if the fixed width
1153 // is removed or zero because only fixed width columns can
1154 // have a vertical alignment
1155 if (column_info[c].p_width.zero())
1156 column_info[c].valignment = LYX_VALIGN_TOP;
1157 for (row_type r = 0; r < nrows(); ++r) {
1158 idx_type const cell = cellIndex(r, c);
1159 // because of multicolumns
1160 toggleFixedWidth(cur, cellInset(cell).get(),
1161 !getPWidth(cell).zero());
1162 if (isMultiRow(cell))
1163 setAlignment(cell, LYX_ALIGN_LEFT, false);
1165 // cur paragraph can become invalid after paragraphs were merged
1166 if (cur.pit() > cur.lastpit())
1167 cur.pit() = cur.lastpit();
1168 // cur position can become invalid after newlines were removed
1169 if (cur.pos() > cur.lastpos())
1170 cur.pos() = cur.lastpos();
1174 bool Tabular::setFixedWidth(row_type r, col_type c)
1176 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1177 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1178 || (multicol && !cell_info[r][c].p_width.zero());
1179 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1184 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1185 Length const & width)
1187 if (!isMultiColumn(cell))
1190 cellInfo(cell).p_width = width;
1191 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1192 // cur paragraph can become invalid after paragraphs were merged
1193 if (cur.pit() > cur.lastpit())
1194 cur.pit() = cur.lastpit();
1195 // cur position can become invalid after newlines were removed
1196 if (cur.pos() > cur.lastpos())
1197 cur.pos() = cur.lastpos();
1202 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1204 cellInfo(cell).mroffset = mroffset;
1209 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1210 Tabular::Feature what)
1212 if (what == SET_SPECIAL_MULTICOLUMN)
1213 cellInfo(cell).align_special = special;
1215 column_info[cellColumn(cell)].align_special = special;
1219 void Tabular::setTopLine(idx_type i, bool line)
1221 cellInfo(i).top_line = line;
1225 void Tabular::setBottomLine(idx_type i, bool line)
1227 cellInfo(i).bottom_line = line;
1231 void Tabular::setLeftLine(idx_type cell, bool line)
1233 cellInfo(cell).left_line = line;
1237 void Tabular::setRightLine(idx_type cell, bool line)
1239 cellInfo(cell).right_line = line;
1242 bool Tabular::rowTopLine(row_type r) const
1244 bool all_rows_set = true;
1245 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1246 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1247 return all_rows_set;
1251 bool Tabular::rowBottomLine(row_type r) const
1253 bool all_rows_set = true;
1254 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1255 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1256 return all_rows_set;
1260 bool Tabular::columnLeftLine(col_type c) const
1267 for (row_type r = 0; r < nrows(); ++r) {
1268 idx_type const i = cellIndex(r, c);
1269 if (c == cellColumn(i)) {
1271 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1272 if (cellInfo(i).left_line || right)
1276 return 2 * nrows_left >= total;
1280 bool Tabular::columnRightLine(col_type c) const
1285 int nrows_right = 0;
1287 for (row_type r = 0; r < nrows(); ++r) {
1288 idx_type i = cellIndex(r, c);
1289 if (c == cellColumn(i) + columnSpan(i) - 1) {
1291 bool left = (c + 1 < ncols()
1292 && cellInfo(cellIndex(r, c + 1)).left_line)
1293 || c + 1 == ncols();
1294 if (cellInfo(i).right_line && left)
1298 return 2 * nrows_right >= total;
1302 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1304 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1305 return cellInfo(cell).alignment;
1307 return column_info[cellColumn(cell)].alignment;
1312 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1314 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1315 return cellInfo(cell).valignment;
1316 return column_info[cellColumn(cell)].valignment;
1320 Length const Tabular::getPWidth(idx_type cell) const
1322 if (isMultiColumn(cell))
1323 return cellInfo(cell).p_width;
1324 return column_info[cellColumn(cell)].p_width;
1328 Length const Tabular::getMROffset(idx_type cell) const
1330 return cellInfo(cell).mroffset;
1334 int Tabular::textHOffset(idx_type cell) const
1336 // the LaTeX Way :-(
1337 int x = WIDTH_OF_LINE;
1339 int const w = cellWidth(cell) - cellInfo(cell).width;
1341 switch (getAlignment(cell)) {
1342 case LYX_ALIGN_CENTER:
1345 case LYX_ALIGN_RIGHT:
1348 case LYX_ALIGN_DECIMAL: {
1349 // we center when no decimal point
1350 if (cellInfo(cell).decimal_width == 0) {
1354 col_type const c = cellColumn(cell);
1355 int max_dhoffset = 0;
1356 for(row_type r = 0; r < row_info.size() ; ++r) {
1357 idx_type const i = cellIndex(r, c);
1358 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1359 && cellInfo(i).decimal_width != 0)
1360 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1362 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1365 // LYX_ALIGN_LEFT: nothing :-)
1373 int Tabular::textVOffset(idx_type cell) const
1375 int voffset = cellInfo(cell).voffset;
1376 if (isMultiRow(cell)) {
1377 row_type const row = cellRow(cell);
1378 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1384 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1387 idx_type const numcells = numberOfCellsInRow(row);
1388 // we check against numcells to make sure we do not crash if all the
1389 // cells are multirow (bug #7535), but in that case our return value
1390 // is really invalid, i.e., it is NOT the first cell in the row. but
1391 // i do not know what to do here. (rgh)
1392 while (c < numcells - 1
1393 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1395 return cell_info[row][c].cellno;
1399 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1401 col_type c = ncols() - 1;
1402 // of course we check against 0 so we don't crash. but we have the same
1403 // problem as in the previous routine: if all the cells are part of a
1404 // multirow or part of a multi column, then our return value is invalid.
1406 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1407 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1409 return cell_info[row][c].cellno;
1413 Tabular::row_type Tabular::cellRow(idx_type cell) const
1415 if (cell >= numberofcells)
1419 return rowofcell[cell];
1423 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1425 if (cell >= numberofcells)
1429 return columnofcell[cell];
1433 void Tabular::write(ostream & os) const
1437 << write_attribute("version", 3)
1438 << write_attribute("rows", nrows())
1439 << write_attribute("columns", ncols())
1441 // global longtable options
1443 << write_attribute("rotate", rotate)
1444 << write_attribute("booktabs", use_booktabs)
1445 << write_attribute("islongtable", is_long_tabular)
1446 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1447 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1448 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1449 << write_attribute("headTopDL", endhead.topDL)
1450 << write_attribute("headBottomDL", endhead.bottomDL)
1451 << write_attribute("footTopDL", endfoot.topDL)
1452 << write_attribute("footBottomDL", endfoot.bottomDL)
1453 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1454 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1455 << write_attribute("lastFootEmpty", endlastfoot.empty);
1456 // longtables cannot be aligned vertically
1457 if (!is_long_tabular) {
1458 os << write_attribute("tabularvalignment", tabular_valignment);
1459 os << write_attribute("tabularwidth", tabular_width);
1461 if (is_long_tabular)
1462 os << write_attribute("longtabularalignment", longtabular_alignment);
1464 for (col_type c = 0; c < ncols(); ++c) {
1466 << write_attribute("alignment", column_info[c].alignment);
1467 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1468 os << write_attribute("decimal_point", column_info[c].decimal_point);
1469 os << write_attribute("valignment", column_info[c].valignment)
1470 << write_attribute("width", column_info[c].p_width.asString())
1471 << write_attribute("special", column_info[c].align_special)
1474 for (row_type r = 0; r < nrows(); ++r) {
1475 static const string def("default");
1477 if (row_info[r].top_space_default)
1478 os << write_attribute("topspace", def);
1480 os << write_attribute("topspace", row_info[r].top_space);
1481 if (row_info[r].bottom_space_default)
1482 os << write_attribute("bottomspace", def);
1484 os << write_attribute("bottomspace", row_info[r].bottom_space);
1485 if (row_info[r].interline_space_default)
1486 os << write_attribute("interlinespace", def);
1488 os << write_attribute("interlinespace", row_info[r].interline_space);
1489 os << write_attribute("endhead", row_info[r].endhead)
1490 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1491 << write_attribute("endfoot", row_info[r].endfoot)
1492 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1493 << write_attribute("newpage", row_info[r].newpage)
1494 << write_attribute("caption", row_info[r].caption)
1496 for (col_type c = 0; c < ncols(); ++c) {
1498 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1499 << write_attribute("multirow", cell_info[r][c].multirow)
1500 << write_attribute("mroffset", cell_info[r][c].mroffset)
1501 << write_attribute("alignment", cell_info[r][c].alignment)
1502 << write_attribute("valignment", cell_info[r][c].valignment)
1503 << write_attribute("topline", cell_info[r][c].top_line)
1504 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1505 << write_attribute("leftline", cell_info[r][c].left_line)
1506 << write_attribute("rightline", cell_info[r][c].right_line)
1507 << write_attribute("rotate", cell_info[r][c].rotate)
1508 << write_attribute("usebox", cell_info[r][c].usebox)
1509 << write_attribute("width", cell_info[r][c].p_width)
1510 << write_attribute("special", cell_info[r][c].align_special)
1512 os << "\\begin_inset ";
1513 cell_info[r][c].inset->write(os);
1514 os << "\n\\end_inset\n"
1516 // FIXME This can be removed again once the mystery
1517 // crash has been resolved.
1522 os << "</lyxtabular>\n";
1526 void Tabular::read(Lexer & lex)
1529 istream & is = lex.getStream();
1531 l_getline(is, line);
1532 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1533 LASSERT(false, return);
1537 if (!getTokenValue(line, "version", version))
1539 LATTEST(version >= 2);
1542 if (!getTokenValue(line, "rows", rows_arg))
1545 if (!getTokenValue(line, "columns", columns_arg))
1547 init(buffer_, rows_arg, columns_arg);
1548 l_getline(is, line);
1549 if (!prefixIs(line, "<features")) {
1550 lyxerr << "Wrong tabular format (expected <features ...> got"
1551 << line << ')' << endl;
1554 getTokenValue(line, "rotate", rotate);
1555 getTokenValue(line, "booktabs", use_booktabs);
1556 getTokenValue(line, "islongtable", is_long_tabular);
1557 getTokenValue(line, "tabularvalignment", tabular_valignment);
1558 getTokenValue(line, "tabularwidth", tabular_width);
1559 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1560 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1561 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1562 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1563 getTokenValue(line, "headTopDL", endhead.topDL);
1564 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1565 getTokenValue(line, "footTopDL", endfoot.topDL);
1566 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1567 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1568 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1569 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1571 for (col_type c = 0; c < ncols(); ++c) {
1573 if (!prefixIs(line,"<column")) {
1574 lyxerr << "Wrong tabular format (expected <column ...> got"
1575 << line << ')' << endl;
1578 getTokenValue(line, "alignment", column_info[c].alignment);
1579 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1580 getTokenValue(line, "valignment", column_info[c].valignment);
1581 getTokenValue(line, "width", column_info[c].p_width);
1582 getTokenValue(line, "special", column_info[c].align_special);
1585 for (row_type i = 0; i < nrows(); ++i) {
1586 l_getline(is, line);
1587 if (!prefixIs(line, "<row")) {
1588 lyxerr << "Wrong tabular format (expected <row ...> got"
1589 << line << ')' << endl;
1592 getTokenValue(line, "topspace", row_info[i].top_space,
1593 row_info[i].top_space_default);
1594 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1595 row_info[i].bottom_space_default);
1596 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1597 row_info[i].interline_space_default);
1598 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1599 getTokenValue(line, "endhead", row_info[i].endhead);
1600 getTokenValue(line, "endfoot", row_info[i].endfoot);
1601 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1602 getTokenValue(line, "newpage", row_info[i].newpage);
1603 getTokenValue(line, "caption", row_info[i].caption);
1604 for (col_type j = 0; j < ncols(); ++j) {
1605 l_getline(is, line);
1606 if (!prefixIs(line, "<cell")) {
1607 lyxerr << "Wrong tabular format (expected <cell ...> got"
1608 << line << ')' << endl;
1611 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1612 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1613 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1614 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1615 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1616 getTokenValue(line, "topline", cell_info[i][j].top_line);
1617 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1618 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1619 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1620 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1621 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1622 getTokenValue(line, "width", cell_info[i][j].p_width);
1624 getTokenValue(line, "special", cell_info[i][j].align_special);
1625 l_getline(is, line);
1626 if (prefixIs(line, "\\begin_inset")) {
1627 cell_info[i][j].inset->setBuffer(*buffer_);
1628 cell_info[i][j].inset->read(lex);
1629 l_getline(is, line);
1631 if (!prefixIs(line, "</cell>")) {
1632 lyxerr << "Wrong tabular format (expected </cell> got"
1633 << line << ')' << endl;
1637 l_getline(is, line);
1638 if (!prefixIs(line, "</row>")) {
1639 lyxerr << "Wrong tabular format (expected </row> got"
1640 << line << ')' << endl;
1644 while (!prefixIs(line, "</lyxtabular>")) {
1645 l_getline(is, line);
1651 bool Tabular::isMultiColumn(idx_type cell) const
1653 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1654 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1658 bool Tabular::hasMultiColumn(col_type c) const
1660 for (row_type r = 0; r < nrows(); ++r) {
1661 if (isMultiColumn(cellIndex(r, c)))
1668 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1670 return cell_info[cellRow(cell)][cellColumn(cell)];
1674 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1675 bool const right_border)
1677 idx_type const col = cellColumn(cell);
1678 idx_type const row = cellRow(cell);
1679 for (idx_type i = 0; i < number; ++i)
1680 unsetMultiRow(cellIndex(row, col + i));
1682 // unsetting of multirow may have invalidated cell index
1683 cell = cellIndex(row, col);
1684 CellData & cs = cellInfo(cell);
1685 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1686 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1687 cs.alignment = column_info[col].alignment;
1688 setRightLine(cell, right_border);
1690 idx_type lastcell = cellIndex(row, col + number - 1);
1691 for (idx_type i = 1; i < lastcell - cell + 1; ++i) {
1692 CellData & cs1 = cellInfo(cell + i);
1693 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1694 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1702 bool Tabular::isMultiRow(idx_type cell) const
1704 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1705 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1708 bool Tabular::hasMultiRow(row_type r) const
1710 for (col_type c = 0; c < ncols(); ++c) {
1711 if (isMultiRow(cellIndex(r, c)))
1717 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1718 bool const bottom_border,
1719 LyXAlignment const halign)
1721 idx_type const col = cellColumn(cell);
1722 idx_type const row = cellRow(cell);
1723 for (idx_type i = 0; i < number; ++i)
1724 unsetMultiColumn(cellIndex(row + i, col));
1726 // unsetting of multirow may have invalidated cell index
1727 cell = cellIndex(row, col);
1728 CellData & cs = cellInfo(cell);
1729 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1730 cs.valignment = LYX_VALIGN_MIDDLE;
1731 // the horizontal alignment of multirow cells can only
1732 // be changed for the whole table row,
1733 // support changing this only for the multirow cell can be done via
1735 if (getPWidth(cell).zero())
1736 cs.alignment = halign;
1738 cs.alignment = LYX_ALIGN_LEFT;
1740 // set the bottom line of the last selected cell
1741 setBottomLine(cell, bottom_border);
1743 for (idx_type i = 1; i < number; ++i) {
1744 CellData & cs1 = cell_info[row + i][col];
1745 cs1.multirow = CELL_PART_OF_MULTIROW;
1746 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1754 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1756 row_type const row = cellRow(cell);
1757 col_type const col = cellColumn(cell);
1759 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1766 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1768 col_type const column = cellColumn(cell);
1769 col_type row = cellRow(cell) + 1;
1770 while (row < nrows() && isPartOfMultiRow(row, column))
1773 return row - cellRow(cell);
1777 void Tabular::unsetMultiColumn(idx_type cell)
1779 if (!isMultiColumn(cell))
1782 row_type const row = cellRow(cell);
1783 col_type const col = cellColumn(cell);
1784 row_type const span = columnSpan(cell);
1785 for (col_type c = 0; c < span; ++c) {
1786 // in the table dialog the lines are set in every case
1787 // when unsetting a multicolumn this leads to an additional right
1788 // line for every cell that was part of the former multicolumn cell,
1789 // except if the cell is in the last column
1790 // therefore remove this line
1791 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1792 && (col + c) < (col + span - 1))
1793 cell_info[row][col + c].right_line = false;
1794 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1800 void Tabular::unsetMultiRow(idx_type cell)
1802 if (!isMultiRow(cell))
1805 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1806 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1807 row_type const row = cellRow(cell);
1808 col_type const col = cellColumn(cell);
1809 row_type const span = rowSpan(cell);
1810 for (row_type r = 0; r < span; ++r)
1811 cell_info[row + r][col].multirow = CELL_NORMAL;
1816 void Tabular::setRotateCell(idx_type cell, int value)
1818 cellInfo(cell).rotate = value;
1822 int Tabular::getRotateCell(idx_type cell) const
1824 return cellInfo(cell).rotate;
1828 bool Tabular::needRotating() const
1832 for (row_type r = 0; r < nrows(); ++r)
1833 for (col_type c = 0; c < ncols(); ++c)
1834 if (cell_info[r][c].rotate != 0)
1840 bool Tabular::isLastCell(idx_type cell) const
1842 if (cell + 1 < numberofcells)
1848 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1850 if (cellRow(cell) == 0)
1853 col_type const col = cellColumn(cell);
1854 row_type r = cellRow(cell) - 1;
1855 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1858 return cell_info[r][col].cellno;
1862 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1864 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1865 if (nextrow < nrows())
1866 return cell_info[nextrow][cellColumn(cell)].cellno;
1871 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1873 LASSERT(column != npos && column < ncols(), column = 0);
1874 LASSERT(row != npos && row < nrows(), row = 0);
1875 return cell_info[row][column].cellno;
1879 void Tabular::setUsebox(idx_type cell, BoxType type)
1881 cellInfo(cell).usebox = type;
1885 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1886 // adds line breaks, see bug 4886.
1887 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1889 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1890 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1892 if (cellInfo(cell).usebox > 1)
1893 return cellInfo(cell).usebox;
1894 return useParbox(cell);
1899 // This are functions used for the longtable support
1901 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1907 row_info[row].endfirsthead = flag;
1911 row_info[row].endhead = flag;
1916 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1919 hd.set = haveLTHead();
1920 return row_info[row].endhead;
1924 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1927 hd.set = haveLTFirstHead();
1928 return row_info[row].endfirsthead;
1932 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1938 row_info[row].endlastfoot = flag;
1942 row_info[row].endfoot = flag;
1947 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1950 fd.set = haveLTFoot();
1951 return row_info[row].endfoot;
1955 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1958 fd.set = haveLTLastFoot();
1959 return row_info[row].endlastfoot;
1963 void Tabular::setLTNewPage(row_type row, bool what)
1965 row_info[row].newpage = what;
1969 bool Tabular::getLTNewPage(row_type row) const
1971 return row_info[row].newpage;
1975 bool Tabular::haveLTHead(bool withcaptions) const
1977 if (!is_long_tabular)
1979 for (row_type i = 0; i < nrows(); ++i)
1980 if (row_info[i].endhead &&
1981 (withcaptions || !row_info[i].caption))
1987 bool Tabular::haveLTFirstHead(bool withcaptions) const
1989 if (!is_long_tabular || endfirsthead.empty)
1991 for (row_type r = 0; r < nrows(); ++r)
1992 if (row_info[r].endfirsthead &&
1993 (withcaptions || !row_info[r].caption))
1999 bool Tabular::haveLTFoot(bool withcaptions) const
2001 if (!is_long_tabular)
2003 for (row_type r = 0; r < nrows(); ++r)
2004 if (row_info[r].endfoot &&
2005 (withcaptions || !row_info[r].caption))
2011 bool Tabular::haveLTLastFoot(bool withcaptions) const
2013 if (!is_long_tabular || endlastfoot.empty)
2015 for (row_type r = 0; r < nrows(); ++r)
2016 if (row_info[r].endlastfoot &&
2017 (withcaptions || !row_info[r].caption))
2023 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
2025 idx_type i = getFirstCellInRow(row);
2027 setMultiColumn(i, numberOfCellsInRow(row), false);
2028 setTopLine(i, false);
2029 setBottomLine(i, false);
2030 setLeftLine(i, false);
2031 setRightLine(i, false);
2032 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2033 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2034 setLTHead(row, true, endfirsthead, true);
2035 row_info[row].endfirsthead = true;
2038 unsetMultiColumn(i);
2039 // When unsetting a caption row, also all existing
2040 // captions in this row must be dissolved.
2042 row_info[row].caption = what;
2047 bool Tabular::ltCaption(row_type row) const
2049 return row_info[row].caption;
2053 bool Tabular::haveLTCaption(CaptionType captiontype) const
2055 if (!is_long_tabular)
2057 for (row_type r = 0; r < nrows(); ++r) {
2058 if (row_info[r].caption) {
2059 switch (captiontype) {
2060 case CAPTION_FIRSTHEAD:
2061 if (row_info[r].endfirsthead)
2065 if (row_info[r].endhead)
2069 if (row_info[r].endfoot)
2072 case CAPTION_LASTFOOT:
2073 if (row_info[r].endlastfoot)
2085 // end longtable support functions
2087 void Tabular::setRowAscent(row_type row, int height)
2089 if (row >= nrows() || row_info[row].ascent == height)
2091 row_info[row].ascent = height;
2095 void Tabular::setRowDescent(row_type row, int height)
2097 if (row >= nrows() || row_info[row].descent == height)
2099 row_info[row].descent = height;
2103 int Tabular::rowAscent(row_type row) const
2105 LASSERT(row < nrows(), row = 0);
2106 return row_info[row].ascent;
2110 int Tabular::rowDescent(row_type row) const
2112 LASSERT(row < nrows(), row = 0);
2113 return row_info[row].descent;
2117 int Tabular::height() const
2120 for (row_type row = 0; row < nrows(); ++row)
2121 height += rowAscent(row) + rowDescent(row) +
2127 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2129 LASSERT(row < nrows(), return false);
2130 LASSERT(column < ncols(), return false);
2131 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2135 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2137 LASSERT(row < nrows(), return false);
2138 LASSERT(column < ncols(), return false);
2139 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2143 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const & lang) const
2145 // we only output complete row lines and the 1st row here, the rest
2146 // is done in Tabular::TeXBottomHLine(...)
2148 // get for each column the topline (if any)
2149 vector<bool> topline;
2151 for (col_type c = 0; c < ncols(); ++c) {
2152 topline.push_back(topLine(cellIndex(row, c)));
2153 // If cell is part of a multirow and not the first cell of the
2154 // multirow, no line must be drawn.
2156 if (isMultiRow(cellIndex(row, c))
2157 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2163 // do nothing if empty first row, or incomplete row line after
2164 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2167 // only output complete row lines and the 1st row's clines
2168 if (nset == ncols()) {
2170 os << (row == 0 ? "\\toprule " : "\\midrule ");
2174 } else if (row == 0) {
2175 for (col_type c = 0; c < ncols(); ++c) {
2177 col_type offset = 0;
2178 for (col_type j = 0 ; j < c; ++j)
2179 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2182 //babel makes the "-" character an active one, so we have to suppress this here
2183 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2184 if (lang == "slovak" || lang == "czech")
2185 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2186 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2188 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2190 col_type cstart = c;
2191 for ( ; c < ncols() && topline[c]; ++c) {}
2193 for (col_type j = cstart ; j < c ; ++j)
2194 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2197 os << c + offset << "} ";
2205 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const & lang) const
2207 // we output bottomlines of row r and the toplines of row r+1
2208 // if the latter do not span the whole tabular
2210 // get the bottomlines of row r, and toplines in next row
2211 bool lastrow = row == nrows() - 1;
2212 vector<bool> bottomline, topline;
2213 bool nextrowset = true;
2214 for (col_type c = 0; c < ncols(); ++c) {
2215 bottomline.push_back(bottomLine(cellIndex(row, c)));
2216 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2217 // If cell is part of a multirow and not the last cell of the
2218 // multirow, no line must be drawn.
2220 if (isMultiRow(cellIndex(row, c))
2221 && isMultiRow(cellIndex(row + 1, c))
2222 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2223 bottomline[c] = false;
2226 nextrowset &= topline[c];
2229 // combine this row's bottom lines and next row's toplines if necessary
2231 for (col_type c = 0; c < ncols(); ++c) {
2233 bottomline[c] = bottomline[c] || topline[c];
2238 // do nothing if empty, OR incomplete row line with a topline in next row
2239 if (nset == 0 || (nextrowset && nset != ncols()))
2242 if (nset == ncols()) {
2244 os << (lastrow ? "\\bottomrule" : "\\midrule");
2248 for (col_type c = 0; c < ncols(); ++c) {
2249 if (bottomline[c]) {
2250 col_type offset = 0;
2251 for (col_type j = 0 ; j < c; ++j)
2252 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2255 //babel makes the "-" character an active one, so we have to suppress this here
2256 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2257 if (lang == "slovak" || lang == "czech")
2258 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2259 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2261 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2263 col_type cstart = c;
2264 for ( ; c < ncols() && bottomline[c]; ++c) {}
2266 for (col_type j = cstart ; j < c ; ++j)
2267 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2270 os << c + offset << "} ";
2278 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2279 bool & ismulticol, bool & ismultirow) const
2281 row_type const r = cellRow(cell);
2282 if (is_long_tabular && row_info[r].caption)
2285 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2286 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2287 // figure out how to set the lines
2288 // we always set double lines to the right of the cell
2289 col_type const c = cellColumn(cell);
2290 col_type const nextcol = c + columnSpan(cell);
2291 bool colright = columnRightLine(c);
2292 bool colleft = columnLeftLine(c);
2293 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2294 bool nextcellleft = nextcol < ncols()
2295 && leftLine(cellIndex(r, nextcol));
2296 bool coldouble = colright && nextcolleft;
2297 bool celldouble = rightLine(cell) && nextcellleft;
2299 ismulticol = isMultiColumn(cell)
2300 || (c == 0 && colleft != leftLine(cell))
2301 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2302 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2303 || (coldouble != celldouble);
2305 // we center in multicol when no decimal point
2306 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2307 docstring const align_d = column_info[c].decimal_point;
2308 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2312 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2313 int latexcolspan = columnSpan(cell);
2314 for(col_type col = c; col < c + columnSpan(cell); ++col)
2315 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2319 os << "\\multicolumn{" << latexcolspan << "}{";
2320 if (c ==0 && leftLine(cell))
2322 if (!cellInfo(cell).align_special.empty()) {
2323 os << cellInfo(cell).align_special;
2325 if (!getPWidth(cell).zero()) {
2327 case LYX_ALIGN_LEFT:
2328 os << ">{\\raggedright}";
2330 case LYX_ALIGN_RIGHT:
2331 os << ">{\\raggedleft}";
2333 case LYX_ALIGN_CENTER:
2334 os << ">{\\centering}";
2340 case LYX_VALIGN_TOP:
2343 case LYX_VALIGN_MIDDLE:
2346 case LYX_VALIGN_BOTTOM:
2351 << from_ascii(getPWidth(cell).asLatexString())
2355 case LYX_ALIGN_LEFT:
2358 case LYX_ALIGN_RIGHT:
2365 } // end if else !getPWidth
2366 } // end if else !cellinfo_of_cell
2367 if (rightLine(cell) || nextcellleft)
2370 // add extra vertical line if we want a double one
2373 } // end if ismulticol
2375 // we only need code for the first multirow cell
2376 ismultirow = isMultiRow(cell);
2378 os << "\\multirow{" << rowSpan(cell) << "}{";
2379 if (!getPWidth(cell).zero())
2380 os << from_ascii(getPWidth(cell).asLatexString());
2382 // we need to set a default value
2385 if (!getMROffset(cell).zero())
2386 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2388 } // end if ismultirow
2390 if (getRotateCell(cell) != 0)
2391 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2393 if (getUsebox(cell) == BOX_PARBOX) {
2396 case LYX_VALIGN_TOP:
2399 case LYX_VALIGN_MIDDLE:
2402 case LYX_VALIGN_BOTTOM:
2406 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2408 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2409 os << "\\begin{minipage}[";
2411 case LYX_VALIGN_TOP:
2414 case LYX_VALIGN_MIDDLE:
2417 case LYX_VALIGN_BOTTOM:
2421 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2427 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2428 bool ismulticol, bool ismultirow) const
2430 row_type const r = cellRow(cell);
2431 if (is_long_tabular && row_info[r].caption)
2435 if (getUsebox(cell) == BOX_PARBOX)
2437 else if (getUsebox(cell) == BOX_MINIPAGE)
2438 os << breakln << "\\end{minipage}";
2439 if (getRotateCell(cell) != 0)
2440 os << breakln << "\\end{turn}";
2448 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2449 OutputParams const & runparams) const
2451 if (!is_long_tabular)
2455 // output caption which is in no header or footer
2456 if (haveLTCaption()) {
2457 for (row_type r = 0; r < nrows(); ++r) {
2458 if (row_info[r].caption &&
2459 !row_info[r].endfirsthead && !row_info[r].endhead &&
2460 !row_info[r].endfoot && !row_info[r].endlastfoot)
2461 TeXRow(os, r, runparams);
2464 // output first header info
2465 if (haveLTFirstHead()) {
2466 if (endfirsthead.topDL)
2468 for (row_type r = 0; r < nrows(); ++r) {
2469 if (row_info[r].endfirsthead)
2470 TeXRow(os, r, runparams);
2472 if (endfirsthead.bottomDL)
2474 os << "\\endfirsthead\n";
2476 // output header info
2478 if (endfirsthead.empty && !haveLTFirstHead())
2479 os << "\\endfirsthead\n";
2482 for (row_type r = 0; r < nrows(); ++r) {
2483 if (row_info[r].endhead)
2484 TeXRow(os, r, runparams);
2486 if (endhead.bottomDL)
2488 os << "\\endhead\n";
2490 // output footer info
2494 for (row_type r = 0; r < nrows(); ++r) {
2495 if (row_info[r].endfoot)
2496 TeXRow(os, r, runparams);
2498 if (endfoot.bottomDL)
2500 os << "\\endfoot\n";
2501 if (endlastfoot.empty && !haveLTLastFoot())
2502 os << "\\endlastfoot\n";
2504 // output lastfooter info
2505 if (haveLTLastFoot()) {
2506 if (endlastfoot.topDL)
2508 for (row_type r = 0; r < nrows(); ++r) {
2509 if (row_info[r].endlastfoot)
2510 TeXRow(os, r, runparams);
2512 if (endlastfoot.bottomDL)
2514 os << "\\endlastfoot\n";
2519 bool Tabular::isValidRow(row_type row) const
2521 if (!is_long_tabular)
2523 return !row_info[row].endhead && !row_info[row].endfirsthead
2524 && !row_info[row].endfoot && !row_info[row].endlastfoot
2525 && !row_info[row].caption;
2529 void Tabular::TeXRow(otexstream & os, row_type row,
2530 OutputParams const & runparams) const
2532 idx_type cell = cellIndex(row, 0);
2533 shared_ptr<InsetTableCell> inset = cellInset(cell);
2534 Paragraph const & par = inset->paragraphs().front();
2535 string const lang = par.getParLanguage(buffer().params())->lang();
2537 //output the top line
2538 TeXTopHLine(os, row, lang);
2540 if (row_info[row].top_space_default) {
2542 os << "\\addlinespace\n";
2544 os << "\\noalign{\\vskip\\doublerulesep}\n";
2545 } else if(!row_info[row].top_space.zero()) {
2547 os << "\\addlinespace["
2548 << from_ascii(row_info[row].top_space.asLatexString())
2551 os << "\\noalign{\\vskip"
2552 << from_ascii(row_info[row].top_space.asLatexString())
2556 bool ismulticol = false;
2557 bool ismultirow = false;
2558 for (col_type c = 0; c < ncols(); ++c) {
2559 if (isPartOfMultiColumn(row, c))
2562 cell = cellIndex(row, c);
2564 if (isPartOfMultiRow(row, c)
2565 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2566 if (cell != getLastCellInRow(row))
2571 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2572 shared_ptr<InsetTableCell> inset = cellInset(cell);
2574 Paragraph const & par = inset->paragraphs().front();
2575 bool rtl = par.isRTL(buffer().params())
2577 && getPWidth(cell).zero()
2578 && !runparams.use_polyglossia;
2582 par.getParLanguage(buffer().params())->lang();
2583 if (lang == "farsi")
2585 else if (lang == "arabic_arabi")
2587 // currently, remaining RTL languages are
2588 // arabic_arabtex and hebrew
2592 // pass to the OutputParams that we are in a cell and
2593 // which alignment we have set.
2594 // InsetNewline needs this context information.
2595 OutputParams newrp(runparams);
2596 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2597 ? OutputParams::PLAIN
2598 : OutputParams::ALIGNED;
2600 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2601 // copy cell and split in 2
2602 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2603 head.setBuffer(buffer());
2604 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2606 dit.push_back(CursorSlice(head));
2607 head.setMacrocontextPositionRecursive(dit);
2608 bool hassep = false;
2609 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2610 head.latex(os, newrp);
2613 tail.setBuffer(head.buffer());
2615 dit.push_back(CursorSlice(tail));
2616 tail.setMacrocontextPositionRecursive(dit);
2617 tail.latex(os, newrp);
2619 } else if (!isPartOfMultiRow(row, c)) {
2620 if (!runparams.nice)
2621 os.texrow().start(par.id(), 0);
2622 inset->latex(os, newrp);
2625 runparams.encoding = newrp.encoding;
2629 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2630 if (cell != getLastCellInRow(row)) { // not last cell in row
2637 os << "\\tabularnewline";
2638 if (row_info[row].bottom_space_default) {
2640 os << "\\addlinespace";
2642 os << "[\\doublerulesep]";
2643 } else if (!row_info[row].bottom_space.zero()) {
2645 os << "\\addlinespace";
2647 << from_ascii(row_info[row].bottom_space.asLatexString())
2652 //output the bottom line
2653 TeXBottomHLine(os, row, lang);
2655 if (row_info[row].interline_space_default) {
2657 os << "\\addlinespace\n";
2659 os << "\\noalign{\\vskip\\doublerulesep}\n";
2660 } else if (!row_info[row].interline_space.zero()) {
2662 os << "\\addlinespace["
2663 << from_ascii(row_info[row].interline_space.asLatexString())
2666 os << "\\noalign{\\vskip"
2667 << from_ascii(row_info[row].interline_space.asLatexString())
2673 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2675 bool const is_tabular_star = !tabular_width.zero();
2677 //+---------------------------------------------------------------------
2678 //+ first the opening preamble +
2679 //+---------------------------------------------------------------------
2682 if (runparams.lastid != -1)
2683 os.texrow().start(runparams.lastid, runparams.lastpos);
2686 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2688 if (is_long_tabular) {
2689 os << "\\begin{longtable}";
2690 switch (longtabular_alignment) {
2691 case LYX_LONGTABULAR_ALIGN_LEFT:
2694 case LYX_LONGTABULAR_ALIGN_CENTER:
2696 case LYX_LONGTABULAR_ALIGN_RIGHT:
2701 if (is_tabular_star)
2702 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2704 os << "\\begin{tabular}";
2705 switch (tabular_valignment) {
2706 case LYX_VALIGN_TOP:
2709 case LYX_VALIGN_MIDDLE:
2711 case LYX_VALIGN_BOTTOM:
2719 if (is_tabular_star)
2720 os << "@{\\extracolsep{\\fill}}";
2722 for (col_type c = 0; c < ncols(); ++c) {
2723 if (columnLeftLine(c))
2725 if (!column_info[c].align_special.empty()) {
2726 os << column_info[c].align_special;
2728 if (!column_info[c].p_width.zero()) {
2729 switch (column_info[c].alignment) {
2730 case LYX_ALIGN_LEFT:
2731 os << ">{\\raggedright}";
2733 case LYX_ALIGN_RIGHT:
2734 os << ">{\\raggedleft}";
2736 case LYX_ALIGN_CENTER:
2737 os << ">{\\centering}";
2739 case LYX_ALIGN_NONE:
2740 case LYX_ALIGN_BLOCK:
2741 case LYX_ALIGN_LAYOUT:
2742 case LYX_ALIGN_SPECIAL:
2743 case LYX_ALIGN_DECIMAL:
2747 switch (column_info[c].valignment) {
2748 case LYX_VALIGN_TOP:
2751 case LYX_VALIGN_MIDDLE:
2754 case LYX_VALIGN_BOTTOM:
2759 << from_ascii(column_info[c].p_width.asLatexString())
2762 switch (column_info[c].alignment) {
2763 case LYX_ALIGN_LEFT:
2766 case LYX_ALIGN_RIGHT:
2769 case LYX_ALIGN_DECIMAL:
2770 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2776 } // end if else !column_info[i].p_width
2777 } // end if else !column_info[i].align_special
2778 if (columnRightLine(c))
2783 TeXLongtableHeaderFooter(os, runparams);
2785 //+---------------------------------------------------------------------
2786 //+ the single row and columns (cells) +
2787 //+---------------------------------------------------------------------
2789 for (row_type r = 0; r < nrows(); ++r) {
2790 if (isValidRow(r)) {
2791 TeXRow(os, r, runparams);
2792 if (is_long_tabular && row_info[r].newpage)
2793 os << "\\newpage\n";
2797 //+---------------------------------------------------------------------
2798 //+ the closing of the tabular +
2799 //+---------------------------------------------------------------------
2801 if (is_long_tabular)
2802 os << "\\end{longtable}";
2804 if (is_tabular_star)
2805 os << "\\end{tabular*}";
2807 os << "\\end{tabular}";
2811 os << breakln << "\\end{turn}";
2815 int Tabular::docbookRow(odocstream & os, row_type row,
2816 OutputParams const & runparams) const
2819 idx_type cell = getFirstCellInRow(row);
2822 for (col_type c = 0; c < ncols(); ++c) {
2823 if (isPartOfMultiColumn(row, c))
2826 os << "<entry align=\"";
2827 switch (getAlignment(cell)) {
2828 case LYX_ALIGN_LEFT:
2831 case LYX_ALIGN_RIGHT:
2839 os << "\" valign=\"";
2840 switch (getVAlignment(cell)) {
2841 case LYX_VALIGN_TOP:
2844 case LYX_VALIGN_BOTTOM:
2847 case LYX_VALIGN_MIDDLE:
2852 if (isMultiColumn(cell)) {
2853 os << " namest=\"col" << c << "\" ";
2854 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2858 ret += cellInset(cell)->docbook(os, runparams);
2867 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2871 //+---------------------------------------------------------------------
2872 //+ first the opening preamble +
2873 //+---------------------------------------------------------------------
2875 os << "<tgroup cols=\"" << ncols()
2876 << "\" colsep=\"1\" rowsep=\"1\">\n";
2878 for (col_type c = 0; c < ncols(); ++c) {
2879 os << "<colspec colname=\"col" << c << "\" align=\"";
2880 switch (column_info[c].alignment) {
2881 case LYX_ALIGN_LEFT:
2884 case LYX_ALIGN_RIGHT:
2892 if (runparams.flavor == OutputParams::XML)
2898 //+---------------------------------------------------------------------
2899 //+ Long Tabular case +
2900 //+---------------------------------------------------------------------
2902 // output caption info
2903 // The caption flag wins over head/foot
2904 if (haveLTCaption()) {
2905 os << "<caption>\n";
2907 for (row_type r = 0; r < nrows(); ++r) {
2908 if (row_info[r].caption) {
2909 ret += docbookRow(os, r, runparams);
2912 os << "</caption>\n";
2915 // output header info
2916 if (haveLTHead(false) || haveLTFirstHead(false)) {
2919 for (row_type r = 0; r < nrows(); ++r) {
2920 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2921 !row_info[r].caption) {
2922 ret += docbookRow(os, r, runparams);
2928 // output footer info
2929 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2932 for (row_type r = 0; r < nrows(); ++r) {
2933 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2934 !row_info[r].caption) {
2935 ret += docbookRow(os, r, runparams);
2942 //+---------------------------------------------------------------------
2943 //+ the single row and columns (cells) +
2944 //+---------------------------------------------------------------------
2948 for (row_type r = 0; r < nrows(); ++r) {
2949 if (isValidRow(r)) {
2950 ret += docbookRow(os, r, runparams);
2955 //+---------------------------------------------------------------------
2956 //+ the closing of the tabular +
2957 //+---------------------------------------------------------------------
2966 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2967 OutputParams const & runparams, bool header) const
2970 string const celltag = header ? "th" : "td";
2971 idx_type cell = getFirstCellInRow(row);
2973 xs << html::StartTag("tr");
2974 for (col_type c = 0; c < ncols(); ++c) {
2975 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2980 switch (getAlignment(cell)) {
2981 case LYX_ALIGN_LEFT:
2984 case LYX_ALIGN_RIGHT:
2992 attr << " valign='";
2993 switch (getVAlignment(cell)) {
2994 case LYX_VALIGN_TOP:
2997 case LYX_VALIGN_BOTTOM:
3000 case LYX_VALIGN_MIDDLE:
3005 if (isMultiColumn(cell))
3006 attr << " colspan='" << columnSpan(cell) << "'";
3007 else if (isMultiRow(cell))
3008 attr << " rowspan='" << rowSpan(cell) << "'";
3010 xs << html::StartTag(celltag, attr.str()) << html::CR();
3011 ret += cellInset(cell)->xhtml(xs, runparams);
3012 xs << html::EndTag(celltag) << html::CR();
3015 xs << html::EndTag("tr");
3020 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
3024 if (is_long_tabular) {
3025 // we'll wrap it in a div, so as to deal with alignment
3027 switch (longtabular_alignment) {
3028 case LYX_LONGTABULAR_ALIGN_LEFT:
3031 case LYX_LONGTABULAR_ALIGN_CENTER:
3034 case LYX_LONGTABULAR_ALIGN_RIGHT:
3038 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
3040 // The caption flag wins over head/foot
3041 if (haveLTCaption()) {
3042 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3044 for (row_type r = 0; r < nrows(); ++r)
3045 if (row_info[r].caption)
3046 ret += xhtmlRow(xs, r, runparams);
3047 xs << html::EndTag("div") << html::CR();
3051 xs << html::StartTag("table") << html::CR();
3053 // output header info
3054 bool const havefirsthead = haveLTFirstHead(false);
3055 // if we have a first head, then we are going to ignore the
3056 // headers for the additional pages, since there aren't any
3057 // in XHTML. this test accomplishes that.
3058 bool const havehead = !havefirsthead && haveLTHead(false);
3059 if (havehead || havefirsthead) {
3060 xs << html::StartTag("thead") << html::CR();
3061 for (row_type r = 0; r < nrows(); ++r) {
3062 if (((havefirsthead && row_info[r].endfirsthead) ||
3063 (havehead && row_info[r].endhead)) &&
3064 !row_info[r].caption) {
3065 ret += xhtmlRow(xs, r, runparams, true);
3068 xs << html::EndTag("thead") << html::CR();
3070 // output footer info
3071 bool const havelastfoot = haveLTLastFoot(false);
3073 bool const havefoot = !havelastfoot && haveLTFoot(false);
3074 if (havefoot || havelastfoot) {
3075 xs << html::StartTag("tfoot") << html::CR();
3076 for (row_type r = 0; r < nrows(); ++r) {
3077 if (((havelastfoot && row_info[r].endlastfoot) ||
3078 (havefoot && row_info[r].endfoot)) &&
3079 !row_info[r].caption) {
3080 ret += xhtmlRow(xs, r, runparams);
3083 xs << html::EndTag("tfoot") << html::CR();
3086 xs << html::StartTag("tbody") << html::CR();
3087 for (row_type r = 0; r < nrows(); ++r) {
3088 if (isValidRow(r)) {
3089 ret += xhtmlRow(xs, r, runparams);
3092 xs << html::EndTag("tbody")
3094 << html::EndTag("table")
3096 if (is_long_tabular)
3097 xs << html::EndTag("div") << html::CR();
3102 bool Tabular::plaintextTopHLine(odocstringstream & os, row_type row,
3103 vector<unsigned int> const & clen) const
3105 idx_type const fcell = getFirstCellInRow(row);
3106 idx_type const n = numberOfCellsInRow(row) + fcell;
3109 for (idx_type i = fcell; i < n; ++i) {
3119 for (idx_type i = fcell; i < n; ++i) {
3130 col_type column = cellColumn(i);
3131 int len = clen[column];
3132 while (column < ncols() - 1
3133 && isPartOfMultiColumn(row, ++column))
3134 len += clen[column] + 4;
3135 os << docstring(len, ch);
3150 bool Tabular::plaintextBottomHLine(odocstringstream & os, row_type row,
3151 vector<unsigned int> const & clen) const
3153 idx_type const fcell = getFirstCellInRow(row);
3154 idx_type const n = numberOfCellsInRow(row) + fcell;
3157 for (idx_type i = fcell; i < n; ++i) {
3158 if (bottomLine(i)) {
3167 for (idx_type i = fcell; i < n; ++i) {
3168 if (bottomLine(i)) {
3178 col_type column = cellColumn(i);
3179 int len = clen[column];
3180 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3181 len += clen[column] + 4;
3182 os << docstring(len, ch);
3183 if (bottomLine(i)) {
3197 void Tabular::plaintextPrintCell(odocstringstream & os,
3198 OutputParams const & runparams,
3199 idx_type cell, row_type row, col_type column,
3200 vector<unsigned int> const & clen,
3201 bool onlydata, size_t max_length) const
3203 odocstringstream sstr;
3204 cellInset(cell)->plaintext(sstr, runparams, max_length);
3216 unsigned int len1 = sstr.str().length();
3217 unsigned int len2 = clen[column];
3218 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3219 len2 += clen[column] + 4;
3222 switch (getAlignment(cell)) {
3224 case LYX_ALIGN_LEFT:
3227 case LYX_ALIGN_RIGHT:
3231 case LYX_ALIGN_CENTER:
3237 os << docstring(len1, ' ') << sstr.str()
3238 << docstring(len2, ' ');
3240 if (rightLine(cell))
3247 void Tabular::plaintext(odocstringstream & os,
3248 OutputParams const & runparams, int const depth,
3249 bool onlydata, char_type delim, size_t max_length) const
3251 // first calculate the width of the single columns
3252 vector<unsigned int> clen(ncols());
3255 // first all non multicolumn cells!
3256 for (col_type c = 0; c < ncols(); ++c) {
3258 for (row_type r = 0; r < nrows(); ++r) {
3259 idx_type cell = cellIndex(r, c);
3260 if (isMultiColumn(cell))
3262 odocstringstream sstr;
3263 cellInset(cell)->plaintext(sstr, runparams, max_length);
3264 if (clen[c] < sstr.str().length())
3265 clen[c] = sstr.str().length();
3268 // then all multicolumn cells!
3269 for (col_type c = 0; c < ncols(); ++c) {
3270 for (row_type r = 0; r < nrows(); ++r) {
3271 idx_type cell = cellIndex(r, c);
3272 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3274 odocstringstream sstr;
3275 cellInset(cell)->plaintext(sstr, runparams, max_length);
3276 int len = int(sstr.str().length());
3277 idx_type const n = columnSpan(cell);
3278 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3280 if (len > int(clen[c + n - 1]))
3281 clen[c + n - 1] = len;
3286 for (row_type r = 0; r < nrows(); ++r) {
3287 if (!onlydata && plaintextTopHLine(os, r, clen))
3288 os << docstring(depth * 2, ' ');
3289 for (col_type c = 0; c < ncols(); ++c) {
3290 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3292 if (onlydata && c > 0)
3293 // we don't use operator<< for single UCS4 character.
3294 // see explanation in docstream.h
3296 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata, max_length);
3298 if (os.str().size() > max_length)
3303 os << docstring(depth * 2, ' ');
3304 if (plaintextBottomHLine(os, r, clen))
3305 os << docstring(depth * 2, ' ');
3307 if (os.str().size() > max_length)
3313 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3315 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3319 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3320 col_type column) const
3322 return cell_info[row][column].inset;
3326 void Tabular::setCellInset(row_type row, col_type column,
3327 shared_ptr<InsetTableCell> ins) const
3329 CellData & cd = cell_info[row][column];
3334 void Tabular::validate(LaTeXFeatures & features) const
3336 features.require("NeedTabularnewline");
3338 features.require("booktabs");
3339 if (is_long_tabular)
3340 features.require("longtable");
3342 features.require("rotating");
3343 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3344 if (isMultiRow(cell))
3345 features.require("multirow");
3346 if (getVAlignment(cell) != LYX_VALIGN_TOP
3347 || !getPWidth(cell).zero())
3348 features.require("array");
3349 cellInset(cell)->validate(features);
3354 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3356 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3357 ParagraphList::const_iterator cit = parlist.begin();
3358 ParagraphList::const_iterator end = parlist.end();
3360 for (; cit != end; ++cit)
3361 for (int i = 0; i < cit->size(); ++i)
3362 if (cit->isNewline(i))
3369 /////////////////////////////////////////////////////////////////////
3373 /////////////////////////////////////////////////////////////////////
3375 InsetTableCell::InsetTableCell(Buffer * buf)
3376 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3377 contentAlign(LYX_ALIGN_CENTER)
3381 bool InsetTableCell::forcePlainLayout(idx_type) const
3383 return !isFixedWidth;
3387 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3389 return isFixedWidth;
3393 bool InsetTableCell::forceLocalFontSwitch() const
3395 return isFixedWidth;
3399 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3400 FuncStatus & status) const
3402 bool enabled = true;
3403 switch (cmd.action()) {
3405 enabled = !forcePlainLayout();
3407 case LFUN_LAYOUT_PARAGRAPH:
3408 enabled = allowParagraphCustomization();
3411 case LFUN_MATH_DISPLAY:
3412 if (!hasFixedWidth()) {
3417 return InsetText::getStatus(cur, cmd, status);
3419 status.setEnabled(enabled);
3423 docstring InsetTableCell::asString(bool intoInsets)
3426 if (paragraphs().empty())
3428 ParagraphList::const_iterator it = paragraphs().begin();
3429 ParagraphList::const_iterator en = paragraphs().end();
3431 for (; it != en; ++it) {
3436 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3442 void InsetTableCell::addToToc(DocIterator const & di, bool output_active,
3443 UpdateType utype) const
3445 InsetText::iterateForToc(di, output_active, utype);
3449 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3452 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3453 return InsetText::xhtml(xs, rp);
3458 /////////////////////////////////////////////////////////////////////
3462 /////////////////////////////////////////////////////////////////////
3464 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3466 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))),
3467 first_visible_cell_(0), offset_valign_(0), rowselect_(false), colselect_(false)
3472 InsetTabular::InsetTabular(InsetTabular const & tab)
3473 : Inset(tab), tabular(tab.tabular)
3478 InsetTabular::~InsetTabular()
3480 hideDialogs("tabular", this);
3484 void InsetTabular::setBuffer(Buffer & buf)
3486 tabular.setBuffer(buf);
3487 Inset::setBuffer(buf);
3491 bool InsetTabular::insetAllowed(InsetCode code) const
3496 case MATHMACRO_CODE:
3501 return tabular.is_long_tabular;
3509 bool InsetTabular::allowsCaptionVariation(std::string const & newtype) const
3511 return tabular.is_long_tabular &&
3512 (newtype == "Standard" || newtype == "LongTableNoNumber");
3516 void InsetTabular::write(ostream & os) const
3518 os << "Tabular" << endl;
3523 string InsetTabular::contextMenu(BufferView const &, int, int) const
3525 // FIXME: depending on the selection state,
3526 // we could offer a different menu.
3527 return cell(0)->contextMenuName() + ";" + contextMenuName();
3531 string InsetTabular::contextMenuName() const
3533 return "context-tabular";
3537 void InsetTabular::read(Lexer & lex)
3539 //bool const old_format = (lex.getString() == "\\LyXTable");
3547 string token = lex.getString();
3548 while (lex && token != "\\end_inset") {
3550 token = lex.getString();
3553 lex.printError("Missing \\end_inset at this point. ");
3557 int InsetTabular::rowFromY(Cursor & cur, int y) const
3559 // top y coordinate of tabular
3560 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3562 for (; r < tabular.nrows() && y > h; ++r)
3563 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3564 + tabular.interRowSpace(r);
3570 int InsetTabular::columnFromX(Cursor & cur, int x) const
3572 // left x coordinate of tabular
3573 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3575 for (; c < tabular.ncols() && x > w; ++c)
3576 w += tabular.cellWidth(c);
3581 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3583 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3584 // mi.base.textwidth << "\n";
3585 LBUFERR(mi.base.bv);
3587 for (row_type r = 0; r < tabular.nrows(); ++r) {
3590 for (col_type c = 0; c < tabular.ncols(); ++c) {
3591 if (tabular.isPartOfMultiColumn(r, c)
3592 || tabular.isPartOfMultiRow(r, c))
3593 // multicolumn or multirow cell, but not first one
3595 idx_type const cell = tabular.cellIndex(r, c);
3598 Length const p_width = tabular.getPWidth(cell);
3599 if (!p_width.zero())
3600 m.base.textwidth = p_width.inPixels(mi.base);
3601 tabular.cellInset(cell)->metrics(m, dim);
3602 if (!p_width.zero())
3603 dim.wid = m.base.textwidth;
3604 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3605 + tabular.interColumnSpace(cell);
3607 // FIXME(?): do we need a second metrics call?
3608 TextMetrics const & tm =
3609 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3611 // determine horizontal offset because of decimal align (if necessary)
3612 int decimal_width = 0;
3613 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3614 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3615 tail.setBuffer(tabular.buffer());
3616 // we need to set macrocontext position everywhere
3617 // otherwise we crash with nested insets (e.g. footnotes)
3618 // after decimal point
3619 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3621 dit.push_back(CursorSlice(tail));
3622 tail.setMacrocontextPositionRecursive(dit);
3624 // remove text leading decimal point
3625 docstring const align_d = tabular.column_info[c].decimal_point;
3626 dit = separatorPos(&tail, align_d);
3628 pit_type const psize = tail.paragraphs().front().size();
3630 tail.paragraphs().front().eraseChars(0,
3631 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3633 tail.metrics(m, dim1);
3634 decimal_width = dim1.width();
3637 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3638 tabular.cell_info[r][c].decimal_width = decimal_width;
3640 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3641 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3642 int const lastpardes = tm.last().second->descent()
3643 + TEXT_TO_INSET_OFFSET;
3645 switch (tabular.getVAlignment(cell)) {
3646 case Tabular::LYX_VALIGN_TOP:
3648 case Tabular::LYX_VALIGN_MIDDLE:
3649 offset = -(dim.des - lastpardes)/2;
3651 case Tabular::LYX_VALIGN_BOTTOM:
3652 offset = -(dim.des - lastpardes);
3655 tabular.cell_info[r][c].voffset = offset;
3656 maxasc = max(maxasc, dim.asc - offset);
3657 maxdes = max(maxdes, dim.des + offset);
3659 int const top_space = tabular.row_info[r].top_space_default ?
3660 default_line_space :
3661 tabular.row_info[r].top_space.inPixels(mi.base);
3662 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3663 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3664 default_line_space :
3665 tabular.row_info[r].bottom_space.inPixels(mi.base);
3666 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3669 // for top-alignment the first horizontal table line must be exactly at
3670 // the position of the base line of the surrounding text line
3671 // for bottom alignment, the same is for the last table line
3672 switch (tabular.tabular_valignment) {
3673 case Tabular::LYX_VALIGN_BOTTOM:
3674 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3676 case Tabular::LYX_VALIGN_MIDDLE:
3677 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3679 case Tabular::LYX_VALIGN_TOP:
3680 offset_valign_ = tabular.rowAscent(0);
3684 tabular.updateColumnWidths();
3685 dim.asc = tabular.rowAscent(0) - offset_valign_;
3686 dim.des = tabular.height() - dim.asc;
3687 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3691 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3694 if (&cur.inset() == this && cur.selection()) {
3695 if (cur.selIsMultiCell()) {
3698 getSelection(cur, rs, re, cs, ce);
3700 idx_type const cell = tabular.cellIndex(row, col);
3701 col_type const cspan = tabular.columnSpan(cell);
3702 row_type const rspan = tabular.rowSpan(cell);
3703 if (col + cspan - 1 >= cs && col <= ce
3704 && row + rspan - 1 >= rs && row <= re)
3707 if (col == tabular.cellColumn(cur.idx())
3708 && row == tabular.cellRow(cur.idx())) {
3709 CursorSlice const & beg = cur.selBegin();
3710 CursorSlice const & end = cur.selEnd();
3712 if ((end.lastpos() > 0 || end.lastpit() > 0)
3713 && end.pos() == end.lastpos() && beg.pos() == 0
3714 && end.pit() == end.lastpit() && beg.pit() == 0)
3722 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3724 x += ADD_TO_TABULAR_WIDTH;
3726 BufferView * bv = pi.base.bv;
3727 Cursor & cur = pi.base.bv->cursor();
3729 // FIXME: As the full background is painted in drawBackground(),
3730 // we have no choice but to do a full repaint for the Text cells.
3731 pi.full_repaint = true;
3733 bool const original_selection_state = pi.selected;
3736 first_visible_cell_ = Tabular::npos;
3738 int yy = y + offset_valign_;
3739 for (row_type r = 0; r < tabular.nrows(); ++r) {
3741 for (col_type c = 0; c < tabular.ncols(); ++c) {
3742 if (tabular.isPartOfMultiColumn(r, c))
3745 idx = tabular.cellIndex(r, c);
3747 if (tabular.isPartOfMultiRow(r, c)) {
3748 nx += tabular.cellWidth(idx);
3752 if (first_visible_cell_ == Tabular::npos)
3753 first_visible_cell_ = idx;
3755 pi.selected |= isCellSelected(cur, r, c);
3756 int const cx = nx + tabular.textHOffset(idx);
3757 int const cy = yy + tabular.textVOffset(idx);
3758 // Cache the Inset position.
3759 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3760 cell(idx)->draw(pi, cx, cy);
3761 drawCellLines(pi, nx, yy, r, idx);
3762 nx += tabular.cellWidth(idx);
3763 pi.selected = original_selection_state;
3766 if (r + 1 < tabular.nrows())
3767 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3768 + tabular.interRowSpace(r + 1);
3773 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3775 x += ADD_TO_TABULAR_WIDTH;
3776 y += offset_valign_ - tabular.rowAscent(0);
3777 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3778 pi.backgroundColor(this));
3782 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3784 Cursor & cur = pi.base.bv->cursor();
3786 x += ADD_TO_TABULAR_WIDTH;
3788 if (!cur.selection())
3790 if (&cur.inset() != this)
3795 bool const full_cell_selected = isCellSelected(cur,
3796 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3798 if (cur.selIsMultiCell() || full_cell_selected) {
3799 for (row_type r = 0; r < tabular.nrows(); ++r) {
3801 for (col_type c = 0; c < tabular.ncols(); ++c) {
3802 if (tabular.isPartOfMultiColumn(r, c))
3805 idx_type const cell = tabular.cellIndex(r, c);
3807 if (tabular.isPartOfMultiRow(r, c)) {
3808 xx += tabular.cellWidth(cell);
3811 int const w = tabular.cellWidth(cell);
3812 int const h = tabular.cellHeight(cell);
3813 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3814 if (isCellSelected(cur, r, c))
3815 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3818 if (r + 1 < tabular.nrows())
3819 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3820 + tabular.interRowSpace(r + 1);
3824 // FIXME: This code has no effect because InsetTableCell does not handle
3825 // drawSelection other than the trivial implementation in Inset.
3827 // x += cellXPos(cur.idx());
3828 // x += tabular.textHOffset(cur.idx());
3829 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3834 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3835 row_type row, idx_type cell) const
3837 y -= tabular.rowAscent(row);
3838 int const w = tabular.cellWidth(cell);
3839 int const h = tabular.cellHeight(cell);
3840 Color const linecolor = pi.textColor(Color_tabularline);
3841 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3844 bool drawline = tabular.topLine(cell)
3845 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3846 pi.pain.line(x, y, x + w, y,
3847 drawline ? linecolor : gridcolor,
3848 drawline ? Painter::line_solid : Painter::line_onoffdash);
3851 drawline = tabular.bottomLine(cell);
3852 pi.pain.line(x, y + h, x + w, y + h,
3853 drawline ? linecolor : gridcolor,
3854 drawline ? Painter::line_solid : Painter::line_onoffdash);
3857 col_type const col = tabular.cellColumn(cell);
3858 drawline = tabular.leftLine(cell)
3859 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3860 pi.pain.line(x, y, x, y + h,
3861 drawline ? linecolor : gridcolor,
3862 drawline ? Painter::line_solid : Painter::line_onoffdash);
3865 x -= tabular.interColumnSpace(cell);
3866 col_type next_cell_col = col + 1;
3867 while (next_cell_col < tabular.ncols()
3868 && tabular.isMultiColumn(tabular.cellIndex(row, next_cell_col)))
3870 drawline = tabular.rightLine(cell)
3871 || (next_cell_col < tabular.ncols()
3872 && tabular.leftLine(tabular.cellIndex(row, next_cell_col)));
3873 pi.pain.line(x + w, y, x + w, y + h,
3874 drawline ? linecolor : gridcolor,
3875 drawline ? Painter::line_solid : Painter::line_onoffdash);
3879 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3881 //lyxerr << "InsetTabular::edit: " << this << endl;
3885 if (isRightToLeft(cur))
3886 cur.idx() = tabular.getLastCellInRow(0);
3892 if (isRightToLeft(cur))
3893 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3895 cur.idx() = tabular.numberofcells - 1;
3897 cur.pos() = cur.lastpos(); // FIXME crude guess
3899 cur.setCurrentFont();
3900 // FIXME: this accesses the position cache before it is initialized
3901 //cur.bv().fitCursor();
3905 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3907 // In a longtable, tell captions what the current float is
3908 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3909 string const saveflt = cnts.current_float();
3910 if (tabular.is_long_tabular) {
3911 cnts.current_float("table");
3912 // in longtables, we only step the counter once
3913 cnts.step(from_ascii("table"), utype);
3914 cnts.isLongtable(true);
3917 ParIterator it2 = it;
3919 size_t const end = it2.nargs();
3920 for ( ; it2.idx() < end; it2.top().forwardIdx())
3921 buffer().updateBuffer(it2, utype);
3924 if (tabular.is_long_tabular) {
3925 cnts.current_float(saveflt);
3926 cnts.isLongtable(false);
3931 void InsetTabular::addToToc(DocIterator const & cpit, bool output_active,
3932 UpdateType utype) const
3934 DocIterator dit = cpit;
3936 size_t const end = dit.nargs();
3937 for ( ; dit.idx() < end; dit.top().forwardIdx())
3938 cell(dit.idx())->addToToc(dit, output_active, utype);
3942 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3944 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3945 << "\n cur:" << cur);
3946 CursorSlice sl = cur.top();
3947 Cursor & bvcur = cur.bv().cursor();
3949 FuncCode const act = cmd.action();
3953 case LFUN_MOUSE_PRESS: {
3954 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3956 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3957 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3958 row_type r = rowFromY(cur, cmd.y());
3959 cur.idx() = tabular.getFirstCellInRow(r);
3962 cur.idx() = tabular.getLastCellInRow(r);
3963 cur.pos() = cur.lastpos();
3964 cur.setSelection(true);
3970 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3971 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3972 || cmd.y() > y0 + tabular.height()) {
3973 col_type c = columnFromX(cur, cmd.x());
3974 cur.idx() = tabular.cellIndex(0, c);
3977 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3978 cur.pos() = cur.lastpos();
3979 cur.setSelection(true);
3984 // do not reset cursor/selection if we have selected
3985 // some cells (bug 2715).
3986 if (cmd.button() == mouse_button::button3
3987 && &bvcur.selBegin().inset() == this
3988 && bvcur.selIsMultiCell())
3991 // Let InsetTableCell do it
3992 cell(cur.idx())->dispatch(cur, cmd);
3995 case LFUN_MOUSE_MOTION:
3996 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3997 if (cmd.button() == mouse_button::button1) {
3998 // only accept motions to places not deeper nested than the real anchor
3999 if (!bvcur.realAnchor().hasPart(cur)) {
4003 // select (additional) row
4005 row_type r = rowFromY(cur, cmd.y());
4006 cur.idx() = tabular.getLastCellInRow(r);
4007 // we need to reset the cursor's pit and pos now, as the old ones
4008 // may no longer be valid.
4011 bvcur.setCursor(cur);
4012 bvcur.setSelection(true);
4015 // select (additional) column
4017 col_type c = columnFromX(cur, cmd.x());
4018 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
4019 // we need to reset the cursor's pit and pos now, as the old ones
4020 // may no longer be valid.
4023 bvcur.setCursor(cur);
4024 bvcur.setSelection(true);
4027 // only update if selection changes
4028 if (bvcur.idx() == cur.idx() &&
4029 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
4030 cur.noScreenUpdate();
4031 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
4032 bvcur.setCursor(cur);
4033 bvcur.setSelection(true);
4034 // if this is a multicell selection, we just set the cursor to
4035 // the beginning of the cell's text.
4036 if (bvcur.selIsMultiCell()) {
4037 bvcur.pit() = bvcur.lastpit();
4038 bvcur.pos() = bvcur.lastpos();
4043 case LFUN_MOUSE_RELEASE:
4048 case LFUN_CELL_BACKWARD:
4050 cur.setSelection(false);
4053 case LFUN_CELL_FORWARD:
4055 cur.setSelection(false);
4058 case LFUN_CHAR_FORWARD_SELECT:
4059 case LFUN_CHAR_FORWARD:
4060 case LFUN_CHAR_BACKWARD_SELECT:
4061 case LFUN_CHAR_BACKWARD:
4062 case LFUN_CHAR_RIGHT_SELECT:
4063 case LFUN_CHAR_RIGHT:
4064 case LFUN_CHAR_LEFT_SELECT:
4065 case LFUN_CHAR_LEFT:
4066 case LFUN_WORD_FORWARD:
4067 case LFUN_WORD_FORWARD_SELECT:
4068 case LFUN_WORD_BACKWARD:
4069 case LFUN_WORD_BACKWARD_SELECT:
4070 case LFUN_WORD_RIGHT:
4071 case LFUN_WORD_RIGHT_SELECT:
4072 case LFUN_WORD_LEFT:
4073 case LFUN_WORD_LEFT_SELECT: {
4074 // determine whether we move to next or previous cell, where to enter
4075 // the new cell from, and which command to "finish" (i.e., exit the
4078 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4079 FuncCode finish_lfun;
4081 if (act == LFUN_CHAR_FORWARD
4082 || act == LFUN_CHAR_FORWARD_SELECT
4083 || act == LFUN_WORD_FORWARD
4084 || act == LFUN_WORD_FORWARD_SELECT) {
4086 finish_lfun = LFUN_FINISHED_FORWARD;
4088 else if (act == LFUN_CHAR_BACKWARD
4089 || act == LFUN_CHAR_BACKWARD_SELECT
4090 || act == LFUN_WORD_BACKWARD
4091 || act == LFUN_WORD_BACKWARD_SELECT) {
4093 finish_lfun = LFUN_FINISHED_BACKWARD;
4095 // LEFT or RIGHT commands --- the interpretation will depend on the
4096 // table's direction.
4098 bool const right = act == LFUN_CHAR_RIGHT
4099 || act == LFUN_CHAR_RIGHT_SELECT
4100 || act == LFUN_WORD_RIGHT
4101 || act == LFUN_WORD_RIGHT_SELECT;
4102 next_cell = isRightToLeft(cur) != right;
4104 if (lyxrc.visual_cursor)
4105 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4107 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4110 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4111 || act == LFUN_CHAR_BACKWARD_SELECT
4112 || act == LFUN_CHAR_RIGHT_SELECT
4113 || act == LFUN_CHAR_LEFT_SELECT
4114 || act == LFUN_WORD_FORWARD_SELECT
4115 || act == LFUN_WORD_RIGHT_SELECT
4116 || act == LFUN_WORD_BACKWARD_SELECT
4117 || act == LFUN_WORD_LEFT_SELECT;
4119 // If we have a multicell selection or we're
4120 // not doing some LFUN_*_SELECT thing anyway...
4121 if (!cur.selIsMultiCell() || !select) {
4122 col_type const c = tabular.cellColumn(cur.idx());
4123 row_type const r = tabular.cellRow(cur.idx());
4124 // Are we trying to select the whole cell and is the whole cell
4125 // not yet selected?
4126 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4127 ((next_cell && cur.pit() == cur.lastpit()
4128 && cur.pos() == cur.lastpos())
4129 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4131 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4133 // ...try to dispatch to the cell's inset.
4134 cell(cur.idx())->dispatch(cur, cmd);
4136 // When we already have a selection we want to select the whole cell
4137 // before going to the next cell.
4138 if (select_whole && !empty_cell){
4139 getText(cur.idx())->selectAll(cur);
4141 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4145 // FIXME: When we support the selection of an empty cell, remove
4146 // the !empty_cell from this condition. For now we jump to the next
4147 // cell if the current cell is empty.
4148 if (cur.result().dispatched() && !empty_cell)
4152 // move to next/prev cell, as appropriate
4153 // note that we will always do this if we're selecting and we have
4154 // a multicell selection
4155 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4156 << " cell from: " << int(entry_from));
4158 moveNextCell(cur, entry_from);
4160 movePrevCell(cur, entry_from);
4161 // if we're exiting the table, call the appropriate FINISHED lfun
4162 if (sl == cur.top()) {
4163 cmd = FuncRequest(finish_lfun);
4168 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4173 case LFUN_DOWN_SELECT:
4175 if (!(cur.selection() && cur.selIsMultiCell()))
4176 cell(cur.idx())->dispatch(cur, cmd);
4178 cur.dispatched(); // override the cell's decision
4179 if (sl == cur.top()) {
4180 // if our Text didn't do anything to the cursor
4181 // then we try to put the cursor into the cell below
4182 // setting also the right targetX.
4183 cur.selHandle(act == LFUN_DOWN_SELECT);
4184 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4185 int const xtarget = cur.targetX();
4186 // WARNING: Once cur.idx() has been reset, the cursor is in
4187 // an inconsistent state until pos() has been set. Be careful
4188 // what you do with it!
4189 cur.idx() = tabular.cellBelow(cur.idx());
4191 TextMetrics const & tm =
4192 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4193 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4194 cur.setCurrentFont();
4197 if (sl == cur.top()) {
4198 // we trick it to go to forward after leaving the
4200 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4203 if (cur.selIsMultiCell()) {
4204 cur.pit() = cur.lastpit();
4205 cur.pos() = cur.lastpos();
4206 cur.setCurrentFont();
4207 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4210 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4213 case LFUN_UP_SELECT:
4215 if (!(cur.selection() && cur.selIsMultiCell()))
4216 cell(cur.idx())->dispatch(cur, cmd);
4217 cur.dispatched(); // override the cell's decision
4218 if (sl == cur.top()) {
4219 // if our Text didn't do anything to the cursor
4220 // then we try to put the cursor into the cell above
4221 // setting also the right targetX.
4222 cur.selHandle(act == LFUN_UP_SELECT);
4223 if (tabular.cellRow(cur.idx()) != 0) {
4224 int const xtarget = cur.targetX();
4225 // WARNING: Once cur.idx() has been reset, the cursor is in
4226 // an inconsistent state until pos() has been set. Be careful
4227 // what you do with it!
4228 cur.idx() = tabular.cellAbove(cur.idx());
4229 cur.pit() = cur.lastpit();
4230 Text const * text = cell(cur.idx())->getText(0);
4231 TextMetrics const & tm = cur.bv().textMetrics(text);
4232 ParagraphMetrics const & pm =
4233 tm.parMetrics(cur.lastpit());
4234 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4235 cur.setCurrentFont();
4238 if (sl == cur.top()) {
4239 cmd = FuncRequest(LFUN_UP);
4242 if (cur.selIsMultiCell()) {
4244 cur.pos() = cur.lastpos();
4245 cur.setCurrentFont();
4246 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4249 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4252 // case LFUN_SCREEN_DOWN: {
4253 // //if (hasSelection())
4254 // // cur.selection() = false;
4255 // col_type const col = tabular.cellColumn(cur.idx());
4256 // int const t = cur.bv().top_y() + cur.bv().height();
4257 // if (t < yo() + tabular.getHeightOfTabular()) {
4258 // cur.bv().scrollDocView(t, true);
4259 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4261 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4268 // case LFUN_SCREEN_UP: {
4269 // //if (hasSelection())
4270 // // cur.selection() = false;
4271 // col_type const col = tabular.cellColumn(cur.idx());
4272 // int const t = cur.bv().top_y() + cur.bv().height();
4274 // cur.bv().scrollDocView(t, true);
4278 // cur.idx() = tabular.cellBelow(first_visible_cell_) + col;
4282 // cur.par() = cur.lastpar();
4283 // cur.pos() = cur.lastpos();
4287 case LFUN_LAYOUT_TABULAR:
4288 cur.bv().showDialog("tabular");
4291 case LFUN_INSET_MODIFY: {
4293 if (cmd.getArg(1) == "from-dialog")
4294 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4296 arg = to_utf8(cmd.argument());
4297 if (!tabularFeatures(cur, arg))
4302 // insert file functions
4303 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4304 case LFUN_FILE_INSERT_PLAINTEXT:
4306 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4307 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4308 FileName(to_utf8(cmd.argument())));
4311 cur.recordUndoInset();
4312 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4313 // content has been replaced,
4314 // so cursor might be invalid
4315 cur.pos() = cur.lastpos();
4316 cur.pit() = cur.lastpit();
4317 bvcur.setCursor(cur);
4324 if (cur.selIsMultiCell()) {
4325 if (copySelection(cur)) {
4326 cur.recordUndoInset();
4330 cell(cur.idx())->dispatch(cur, cmd);
4333 case LFUN_SELF_INSERT:
4334 if (cur.selIsMultiCell()) {
4335 cur.recordUndoInset();
4337 BufferView * bv = &cur.bv();
4338 docstring::const_iterator cit = cmd.argument().begin();
4339 docstring::const_iterator const end = cmd.argument().end();
4340 for (; cit != end; ++cit)
4341 bv->translateAndInsert(*cit, getText(cur.idx()), cur);
4344 bv->bookmarkEditPosition();
4346 cell(cur.idx())->dispatch(cur, cmd);
4349 case LFUN_CHAR_DELETE_BACKWARD:
4350 case LFUN_CHAR_DELETE_FORWARD:
4351 if (cur.selIsMultiCell()) {
4352 cur.recordUndoInset();
4355 cell(cur.idx())->dispatch(cur, cmd);
4359 if (!cur.selection())
4361 if (cur.selIsMultiCell()) {
4365 cell(cur.idx())->dispatch(cur, cmd);
4368 case LFUN_CLIPBOARD_PASTE:
4369 case LFUN_PRIMARY_SELECTION_PASTE: {
4370 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4371 theClipboard().getAsText(Clipboard::PlainTextType) :
4372 theSelection().get();
4375 // pass to InsertPlaintextString, but
4376 // only if we have multi-cell content
4377 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4378 cur.recordUndoInset();
4379 if (insertPlaintextString(cur.bv(), clip, false)) {
4380 // content has been replaced,
4381 // so cursor might be invalid
4382 cur.pos() = cur.lastpos();
4383 cur.pit() = cur.lastpit();
4384 bvcur.setCursor(cur);
4388 // Let the cell handle normal text
4389 cell(cur.idx())->dispatch(cur, cmd);
4394 if (!tabularStackDirty()) {
4395 if (!cur.selIsMultiCell())
4396 cell(cur.idx())->dispatch(cur, cmd);
4399 if (theClipboard().isInternal()) {
4400 cur.recordUndoInset();
4401 pasteClipboard(cur);
4405 case LFUN_FONT_EMPH:
4406 case LFUN_FONT_BOLD:
4407 case LFUN_FONT_BOLDSYMBOL:
4408 case LFUN_FONT_ROMAN:
4409 case LFUN_FONT_NOUN:
4410 case LFUN_FONT_ITAL:
4411 case LFUN_FONT_FRAK:
4412 case LFUN_FONT_TYPEWRITER:
4413 case LFUN_FONT_SANS:
4414 case LFUN_TEXTSTYLE_APPLY:
4415 case LFUN_TEXTSTYLE_UPDATE:
4416 case LFUN_FONT_SIZE:
4417 case LFUN_FONT_UNDERLINE:
4418 case LFUN_FONT_STRIKEOUT:
4419 case LFUN_FONT_UNDERUNDERLINE:
4420 case LFUN_FONT_UNDERWAVE:
4422 case LFUN_PARAGRAPH_PARAMS_APPLY:
4423 case LFUN_PARAGRAPH_PARAMS:
4424 case LFUN_WORD_CAPITALIZE:
4425 case LFUN_WORD_UPCASE:
4426 case LFUN_WORD_LOWCASE:
4427 case LFUN_CHARS_TRANSPOSE:
4428 if (cur.selIsMultiCell()) {
4431 getSelection(cur, rs, re, cs, ce);
4432 Cursor tmpcur = cur;
4433 for (row_type r = rs; r <= re; ++r) {
4434 for (col_type c = cs; c <= ce; ++c) {
4435 // cursor follows cell:
4436 tmpcur.idx() = tabular.cellIndex(r, c);
4437 // select this cell only:
4440 tmpcur.resetAnchor();
4441 tmpcur.pit() = tmpcur.lastpit();
4442 tmpcur.pos() = tmpcur.top().lastpos();
4443 tmpcur.setCursor(tmpcur);
4444 tmpcur.setSelection();
4445 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4450 cell(cur.idx())->dispatch(cur, cmd);
4454 case LFUN_INSET_SETTINGS:
4455 // relay this lfun to Inset, not to the cell.
4456 Inset::doDispatch(cur, cmd);
4460 // we try to handle this event in the insets dispatch function.
4461 cell(cur.idx())->dispatch(cur, cmd);
4467 // function sets an object as defined in func_status.h:
4468 // states OK, Unknown, Disabled, On, Off.
4469 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4470 FuncStatus & status) const
4472 switch (cmd.action()) {
4473 case LFUN_INSET_MODIFY: {
4474 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4477 // FIXME: We only check for the very first argument...
4478 string const s = cmd.getArg(1);
4479 // We always enable the lfun if it is coming from the dialog
4480 // because the dialog makes sure all the settings are valid,
4481 // even though the first argument might not be valid now.
4482 if (s == "from-dialog") {
4483 status.setEnabled(true);
4487 int action = Tabular::LAST_ACTION;
4489 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4490 if (tabularFeature[i].feature == s) {
4491 action = tabularFeature[i].action;
4495 if (action == Tabular::LAST_ACTION) {
4497 status.setUnknown(true);
4501 string const argument = cmd.getLongArg(2);
4503 row_type sel_row_start = 0;
4504 row_type sel_row_end = 0;
4505 col_type sel_col_start = 0;
4506 col_type sel_col_end = 0;
4507 Tabular::ltType dummyltt;
4510 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4513 case Tabular::SET_PWIDTH:
4514 case Tabular::SET_MPWIDTH:
4515 case Tabular::SET_SPECIAL_COLUMN:
4516 case Tabular::SET_SPECIAL_MULTICOLUMN:
4517 case Tabular::APPEND_ROW:
4518 case Tabular::APPEND_COLUMN:
4519 case Tabular::DELETE_ROW:
4520 case Tabular::DELETE_COLUMN:
4521 case Tabular::COPY_ROW:
4522 case Tabular::COPY_COLUMN:
4523 case Tabular::SET_TOP_SPACE:
4524 case Tabular::SET_BOTTOM_SPACE:
4525 case Tabular::SET_INTERLINE_SPACE:
4529 case Tabular::SET_TABULAR_WIDTH:
4530 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4531 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4534 case Tabular::MOVE_COLUMN_RIGHT:
4535 case Tabular::MOVE_COLUMN_LEFT:
4536 case Tabular::MOVE_ROW_DOWN:
4537 case Tabular::MOVE_ROW_UP: {
4538 if (cur.selection()) {
4539 status.message(_("Selections not supported."));
4540 status.setEnabled(false);
4544 if ((action == Tabular::MOVE_COLUMN_RIGHT &&
4545 tabular.ncols() == tabular.cellColumn(cur.idx()) + 1) ||
4546 (action == Tabular::MOVE_COLUMN_LEFT &&
4547 tabular.cellColumn(cur.idx()) == 0) ||
4548 (action == Tabular::MOVE_ROW_DOWN &&
4549 tabular.nrows() == tabular.cellRow(cur.idx()) + 1) ||
4550 (action == Tabular::MOVE_ROW_UP &&
4551 tabular.cellRow(cur.idx()) == 0)) {
4552 status.setEnabled(false);
4556 if (action == Tabular::MOVE_COLUMN_RIGHT ||
4557 action == Tabular::MOVE_COLUMN_LEFT) {
4558 if (tabular.hasMultiColumn(tabular.cellColumn(cur.idx())) ||
4559 tabular.hasMultiColumn(tabular.cellColumn(cur.idx()) +
4560 (action == Tabular::MOVE_COLUMN_RIGHT ? 1 : -1))) {
4561 status.message(_("Multi-column in current or"
4562 " destination column."));
4563 status.setEnabled(false);
4568 if (action == Tabular::MOVE_ROW_DOWN ||
4569 action == Tabular::MOVE_ROW_UP) {
4570 if (tabular.hasMultiRow(tabular.cellRow(cur.idx())) ||
4571 tabular.hasMultiRow(tabular.cellRow(cur.idx()) +
4572 (action == Tabular::MOVE_ROW_DOWN ? 1 : -1))) {
4573 status.message(_("Multi-row in current or"
4574 " destination row."));
4575 status.setEnabled(false);
4580 status.setEnabled(true);
4584 case Tabular::SET_DECIMAL_POINT:
4586 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4589 case Tabular::SET_MULTICOLUMN:
4590 case Tabular::UNSET_MULTICOLUMN:
4591 case Tabular::MULTICOLUMN:
4592 // If a row is set as longtable caption, it must not be allowed
4593 // to unset that this row is a multicolumn.
4594 status.setEnabled(sel_row_start == sel_row_end
4595 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4596 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4599 case Tabular::SET_MULTIROW:
4600 case Tabular::UNSET_MULTIROW:
4601 case Tabular::MULTIROW:
4602 // If a row is set as longtable caption, it must not be allowed
4603 // to unset that this row is a multirow.
4604 status.setEnabled(sel_col_start == sel_col_end
4605 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4606 status.setOnOff(tabular.isMultiRow(cur.idx()));
4609 case Tabular::SET_ALL_LINES:
4610 case Tabular::UNSET_ALL_LINES:
4611 case Tabular::SET_BORDER_LINES:
4612 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4615 case Tabular::SET_LINE_TOP:
4616 case Tabular::SET_LINE_BOTTOM:
4617 case Tabular::SET_LINE_LEFT:
4618 case Tabular::SET_LINE_RIGHT:
4619 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4622 case Tabular::TOGGLE_LINE_TOP:
4623 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4624 status.setOnOff(tabular.topLine(cur.idx()));
4627 case Tabular::TOGGLE_LINE_BOTTOM:
4628 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4629 status.setOnOff(tabular.bottomLine(cur.idx()));
4632 case Tabular::TOGGLE_LINE_LEFT:
4633 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4634 status.setOnOff(tabular.leftLine(cur.idx()));
4637 case Tabular::TOGGLE_LINE_RIGHT:
4638 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4639 status.setOnOff(tabular.rightLine(cur.idx()));
4642 // multirow cells only inherit the alignment of the column if the column has
4643 // no width, otherwise they are left-aligned
4644 // therefore allow always left but right and center only if there is no width
4645 case Tabular::M_ALIGN_LEFT:
4647 case Tabular::ALIGN_LEFT:
4648 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4651 case Tabular::M_ALIGN_RIGHT:
4653 case Tabular::ALIGN_RIGHT:
4654 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4655 && !tabular.getPWidth(cur.idx()).zero()));
4656 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4659 case Tabular::M_ALIGN_CENTER:
4661 case Tabular::ALIGN_CENTER:
4662 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4663 && !tabular.getPWidth(cur.idx()).zero()));
4664 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4667 case Tabular::ALIGN_BLOCK:
4668 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4669 && !tabular.isMultiRow(cur.idx()));
4670 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4673 case Tabular::ALIGN_DECIMAL:
4674 status.setEnabled(!tabular.isMultiRow(cur.idx())
4675 && !tabular.isMultiColumn(cur.idx()));
4676 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4679 case Tabular::M_VALIGN_TOP:
4681 case Tabular::VALIGN_TOP:
4682 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4683 && !tabular.isMultiRow(cur.idx()));
4685 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4688 case Tabular::M_VALIGN_BOTTOM:
4690 case Tabular::VALIGN_BOTTOM:
4691 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4692 && !tabular.isMultiRow(cur.idx()));
4694 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4697 case Tabular::M_VALIGN_MIDDLE:
4699 case Tabular::VALIGN_MIDDLE:
4700 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4701 && !tabular.isMultiRow(cur.idx()));
4703 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4706 case Tabular::SET_LONGTABULAR:
4707 // setting as longtable is not allowed when table is inside a float
4708 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4709 || cur.innerInsetOfType(WRAP_CODE) != 0)
4710 status.setEnabled(false);
4712 status.setEnabled(true);
4713 status.setOnOff(tabular.is_long_tabular);
4716 case Tabular::UNSET_LONGTABULAR:
4717 status.setOnOff(!tabular.is_long_tabular);
4720 case Tabular::TOGGLE_ROTATE_TABULAR:
4721 case Tabular::SET_ROTATE_TABULAR:
4722 status.setOnOff(tabular.rotate != 0);
4725 case Tabular::TABULAR_VALIGN_TOP:
4726 status.setEnabled(tabular.tabular_width.zero());
4727 status.setOnOff(tabular.tabular_valignment
4728 == Tabular::LYX_VALIGN_TOP);
4730 case Tabular::TABULAR_VALIGN_MIDDLE:
4731 status.setEnabled(tabular.tabular_width.zero());
4732 status.setOnOff(tabular.tabular_valignment
4733 == Tabular::LYX_VALIGN_MIDDLE);
4735 case Tabular::TABULAR_VALIGN_BOTTOM:
4736 status.setEnabled(tabular.tabular_width.zero());
4737 status.setOnOff(tabular.tabular_valignment
4738 == Tabular::LYX_VALIGN_BOTTOM);
4741 case Tabular::LONGTABULAR_ALIGN_LEFT:
4742 status.setOnOff(tabular.longtabular_alignment
4743 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4745 case Tabular::LONGTABULAR_ALIGN_CENTER:
4746 status.setOnOff(tabular.longtabular_alignment
4747 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4749 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4750 status.setOnOff(tabular.longtabular_alignment
4751 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4754 case Tabular::UNSET_ROTATE_TABULAR:
4755 status.setOnOff(tabular.rotate == 0);
4758 case Tabular::TOGGLE_ROTATE_CELL:
4759 case Tabular::SET_ROTATE_CELL:
4760 status.setOnOff(!oneCellHasRotationState(false,
4761 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4764 case Tabular::UNSET_ROTATE_CELL:
4765 status.setOnOff(!oneCellHasRotationState(true,
4766 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4769 case Tabular::SET_USEBOX:
4770 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4773 // every row can only be one thing:
4774 // either a footer or header
4775 case Tabular::SET_LTFIRSTHEAD:
4776 status.setEnabled(sel_row_start == sel_row_end);
4777 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4780 case Tabular::UNSET_LTFIRSTHEAD:
4781 status.setEnabled(sel_row_start == sel_row_end);
4782 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4785 case Tabular::SET_LTHEAD:
4786 status.setEnabled(sel_row_start == sel_row_end);
4787 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4790 case Tabular::UNSET_LTHEAD:
4791 status.setEnabled(sel_row_start == sel_row_end);
4792 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4795 case Tabular::SET_LTFOOT:
4796 status.setEnabled(sel_row_start == sel_row_end);
4797 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4800 case Tabular::UNSET_LTFOOT:
4801 status.setEnabled(sel_row_start == sel_row_end);
4802 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4805 case Tabular::SET_LTLASTFOOT:
4806 status.setEnabled(sel_row_start == sel_row_end);
4807 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4810 case Tabular::UNSET_LTLASTFOOT:
4811 status.setEnabled(sel_row_start == sel_row_end);
4812 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4815 case Tabular::SET_LTNEWPAGE:
4816 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4818 case Tabular::UNSET_LTNEWPAGE:
4819 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4822 // only one row in head/firsthead/foot/lasthead can be the caption
4823 // and a multirow cannot be set as caption
4824 case Tabular::SET_LTCAPTION:
4825 status.setEnabled(sel_row_start == sel_row_end
4826 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4827 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4828 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4829 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4830 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4831 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4832 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4833 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4834 && !tabular.isMultiRow(sel_row_start));
4835 status.setOnOff(tabular.ltCaption(sel_row_start));
4838 case Tabular::UNSET_LTCAPTION:
4839 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4842 case Tabular::TOGGLE_LTCAPTION:
4843 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4844 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4845 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4846 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4847 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4848 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4849 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4850 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4851 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4852 status.setOnOff(tabular.ltCaption(sel_row_start));
4855 case Tabular::SET_BOOKTABS:
4856 status.setOnOff(tabular.use_booktabs);
4859 case Tabular::UNSET_BOOKTABS:
4860 status.setOnOff(!tabular.use_booktabs);
4865 status.setEnabled(false);
4871 case LFUN_CAPTION_INSERT: {
4872 // caption is only allowed in caption cell of longtable
4873 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4874 status.setEnabled(false);
4877 // only standard caption is allowed
4878 string arg = cmd.getArg(0);
4879 if (!arg.empty() && arg != "Standard") {
4880 status.setEnabled(false);
4883 // check if there is already a caption
4884 bool have_caption = false;
4885 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4886 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4887 ParagraphList::const_iterator pend = itc.paragraphs().end();
4888 for (; pit != pend; ++pit) {
4889 InsetList::const_iterator it = pit->insetList().begin();
4890 InsetList::const_iterator end = pit->insetList().end();
4891 for (; it != end; ++it) {
4892 if (it->inset->lyxCode() == CAPTION_CODE) {
4893 have_caption = true;
4898 status.setEnabled(!have_caption);
4902 // These are only enabled inside tabular
4903 case LFUN_CELL_BACKWARD:
4904 case LFUN_CELL_FORWARD:
4905 status.setEnabled(true);
4908 // disable these with multiple cells selected
4909 case LFUN_INSET_INSERT:
4910 case LFUN_TABULAR_INSERT:
4911 case LFUN_FLEX_INSERT:
4912 case LFUN_FLOAT_INSERT:
4913 case LFUN_FLOAT_WIDE_INSERT:
4914 case LFUN_FOOTNOTE_INSERT:
4915 case LFUN_MARGINALNOTE_INSERT:
4916 case LFUN_MATH_INSERT:
4917 case LFUN_MATH_MODE:
4918 case LFUN_MATH_MUTATE:
4919 case LFUN_MATH_DISPLAY:
4920 case LFUN_NOTE_INSERT:
4921 case LFUN_ARGUMENT_INSERT:
4922 case LFUN_BOX_INSERT:
4923 case LFUN_BRANCH_INSERT:
4924 case LFUN_PHANTOM_INSERT:
4925 case LFUN_WRAP_INSERT:
4926 case LFUN_PREVIEW_INSERT:
4927 case LFUN_ERT_INSERT: {
4928 if (cur.selIsMultiCell()) {
4929 status.setEnabled(false);
4932 return cell(cur.idx())->getStatus(cur, cmd, status);
4935 // disable in non-fixed-width cells
4936 case LFUN_PARAGRAPH_BREAK:
4937 // multirow does not allow paragraph breaks
4938 if (tabular.isMultiRow(cur.idx())) {
4939 status.setEnabled(false);
4943 case LFUN_NEWLINE_INSERT: {
4944 if (tabular.getPWidth(cur.idx()).zero()) {
4945 status.setEnabled(false);
4948 return cell(cur.idx())->getStatus(cur, cmd, status);
4951 case LFUN_NEWPAGE_INSERT:
4952 status.setEnabled(false);
4956 if (tabularStackDirty() && theClipboard().isInternal()) {
4957 if (cur.selIsMultiCell()) {
4960 getSelection(cur, rs, re, cs, ce);
4961 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4962 && paste_tabular->nrows() == re - rs + 1)
4963 status.setEnabled(true);
4965 status.setEnabled(false);
4966 status.message(_("Selection size should match clipboard content."));
4969 status.setEnabled(true);
4972 return cell(cur.idx())->getStatus(cur, cmd, status);
4974 case LFUN_INSET_SETTINGS:
4975 // relay this lfun to Inset, not to the cell.
4976 return Inset::getStatus(cur, cmd, status);
4979 // we try to handle this event in the insets dispatch function.
4980 return cell(cur.idx())->getStatus(cur, cmd, status);
4986 Inset::DisplayType InsetTabular::display() const
4988 if (tabular.is_long_tabular) {
4989 switch (tabular.longtabular_alignment) {
4990 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4992 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4994 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
5004 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
5006 tabular.latex(os, runparams);
5010 int InsetTabular::plaintext(odocstringstream & os,
5011 OutputParams const & runparams, size_t max_length) const
5013 os << '\n'; // output table on a new line
5014 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
5015 tabular.plaintext(os, runparams, dp, false, 0, max_length);
5016 return PLAINTEXT_NEWLINE;
5020 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
5025 // FIXME: Why not pass a proper DocIterator here?
5027 // if the table is inside a float it doesn't need the informaltable
5028 // wrapper. Search for it.
5029 for (master = owner(); master; master = master->owner())
5030 if (master->lyxCode() == FLOAT_CODE)
5035 os << "<informaltable>";
5038 ret += tabular.docbook(os, runparams);
5040 os << "</informaltable>";
5047 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
5049 return tabular.xhtml(xs, rp);
5053 void InsetTabular::validate(LaTeXFeatures & features) const
5055 tabular.validate(features);
5057 // It'd be better to be able to get this from an InsetLayout, but at present
5058 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
5059 if (features.runparams().flavor == OutputParams::HTML)
5060 features.addCSSSnippet(
5061 "table { border: 1px solid black; display: inline-block; }\n"
5062 "td { border: 1px solid black; padding: 0.5ex; }");
5066 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
5068 return tabular.cellInset(idx);
5072 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
5074 return tabular.cellInset(idx);
5078 void InsetTabular::cursorPos(BufferView const & bv,
5079 CursorSlice const & sl, bool boundary, int & x, int & y) const
5081 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
5083 // y offset correction
5084 y += cellYPos(sl.idx());
5085 y += tabular.textVOffset(sl.idx());
5086 y += offset_valign_;
5088 // x offset correction
5089 x += cellXPos(sl.idx());
5090 x += tabular.textHOffset(sl.idx());
5091 x += ADD_TO_TABULAR_WIDTH;
5095 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
5099 Inset const & inset = *tabular.cellInset(cell);
5100 Point o = bv.coordCache().getInsets().xy(&inset);
5101 int const xbeg = o.x_ - tabular.textHOffset(cell);
5102 int const xend = xbeg + tabular.cellWidth(cell);
5103 row_type const row = tabular.cellRow(cell);
5104 int const ybeg = o.y_ - tabular.rowAscent(row)
5105 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
5106 int const yend = ybeg + tabular.cellHeight(cell);
5118 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
5119 // << " ybeg=" << ybeg << " yend=" << yend
5120 // << " xx=" << xx << " yy=" << yy
5121 // << " dist=" << xx + yy << endl;
5126 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
5128 //lyxerr << "InsetTabular::editXY: " << this << endl;
5129 cur.setSelection(false);
5131 cur.idx() = getNearestCell(cur.bv(), x, y);
5132 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
5136 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
5138 cur.idx() = getNearestCell(cur.bv(), x, y);
5139 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
5143 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
5145 idx_type idx_min = 0;
5146 int dist_min = numeric_limits<int>::max();
5147 for (idx_type i = 0, n = nargs(); i != n; ++i) {
5148 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
5149 int const d = dist(bv, i, x, y);
5160 int InsetTabular::cellYPos(idx_type const cell) const
5162 row_type row = tabular.cellRow(cell);
5164 for (row_type r = 0; r < row; ++r)
5165 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
5166 + tabular.interRowSpace(r + 1);
5171 int InsetTabular::cellXPos(idx_type const cell) const
5173 col_type col = tabular.cellColumn(cell);
5175 for (col_type c = 0; c < col; ++c)
5176 lx += tabular.column_info[c].width;
5181 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5183 row_type const row = tabular.cellRow(cur.idx());
5184 col_type const col = tabular.cellColumn(cur.idx());
5186 if (isRightToLeft(cur)) {
5187 if (tabular.cellColumn(cur.idx()) == 0) {
5188 if (row == tabular.nrows() - 1)
5190 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5195 cur.idx() = tabular.getLastCellInRow(row - 1);
5197 cur.idx() = tabular.cellIndex(row, col - 1);
5200 if (tabular.isLastCell(cur.idx()))
5202 if (cur.idx() == tabular.getLastCellInRow(row))
5203 cur.idx() = tabular.cellIndex(row + 1, 0);
5205 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5206 cur.idx() = tabular.cellIndex(row, colnextcell);
5210 cur.boundary(false);
5212 if (cur.selIsMultiCell()) {
5213 cur.pit() = cur.lastpit();
5214 cur.pos() = cur.lastpos();
5221 // in visual mode, place cursor at extreme left or right
5223 switch(entry_from) {
5225 case ENTRY_DIRECTION_RIGHT:
5226 cur.posVisToRowExtremity(false /* !left */);
5228 case ENTRY_DIRECTION_LEFT:
5229 cur.posVisToRowExtremity(true /* left */);
5231 case ENTRY_DIRECTION_IGNORE:
5232 // nothing to do in this case
5236 cur.setCurrentFont();
5240 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5242 row_type const row = tabular.cellRow(cur.idx());
5243 col_type const col = tabular.cellColumn(cur.idx());
5245 if (isRightToLeft(cur)) {
5246 if (cur.idx() == tabular.getLastCellInRow(row)) {
5249 cur.idx() = tabular.getFirstCellInRow(row);
5250 cur.idx() = tabular.cellAbove(cur.idx());
5252 if (tabular.isLastCell(cur.idx()))
5254 if (cur.idx() == tabular.getLastCellInRow(row))
5255 cur.idx() = tabular.cellIndex(row + 1, 0);
5257 cur.idx() = tabular.cellIndex(row, col + 1);
5260 if (cur.idx() == 0) // first cell
5263 cur.idx() = tabular.getLastCellInRow(row - 1);
5265 cur.idx() = tabular.cellIndex(row, col - 1);
5268 if (cur.selIsMultiCell()) {
5269 cur.pit() = cur.lastpit();
5270 cur.pos() = cur.lastpos();
5274 cur.pit() = cur.lastpit();
5275 cur.pos() = cur.lastpos();
5277 // in visual mode, place cursor at extreme left or right
5279 switch(entry_from) {
5281 case ENTRY_DIRECTION_RIGHT:
5282 cur.posVisToRowExtremity(false /* !left */);
5284 case ENTRY_DIRECTION_LEFT:
5285 cur.posVisToRowExtremity(true /* left */);
5287 case ENTRY_DIRECTION_IGNORE:
5288 // nothing to do in this case
5292 cur.setCurrentFont();
5296 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5298 istringstream is(argument);
5301 if (insetCode(s) != TABULAR_CODE)
5305 size_t safe_guard = 0;
5310 if (safe_guard > 1000) {
5311 LYXERR0("parameter max count reached!");
5315 Tabular::Feature action = Tabular::LAST_ACTION;
5318 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5319 if (s != tabularFeature[i].feature)
5321 action = tabularFeature[i].action;
5324 if (action == Tabular::LAST_ACTION) {
5325 LYXERR0("Feature not found " << s);
5329 if (tabularFeature[i].need_value)
5331 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5332 tabularFeatures(cur, action, val);
5338 static void checkLongtableSpecial(Tabular::ltType & ltt,
5339 string const & special, bool & flag)
5341 if (special == "dl_above") {
5344 } else if (special == "dl_below") {
5345 ltt.bottomDL = flag;
5347 } else if (special == "empty") {
5357 bool InsetTabular::oneCellHasRotationState(bool rotated,
5358 row_type row_start, row_type row_end,
5359 col_type col_start, col_type col_end) const
5361 for (row_type r = row_start; r <= row_end; ++r)
5362 for (col_type c = col_start; c <= col_end; ++c)
5364 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5367 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5374 void InsetTabular::tabularFeatures(Cursor & cur,
5375 Tabular::Feature feature, string const & value)
5377 col_type sel_col_start;
5378 col_type sel_col_end;
5379 row_type sel_row_start;
5380 row_type sel_row_end;
5381 bool setLines = false;
5382 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5383 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5387 case Tabular::M_ALIGN_LEFT:
5388 case Tabular::ALIGN_LEFT:
5389 setAlign = LYX_ALIGN_LEFT;
5392 case Tabular::M_ALIGN_RIGHT:
5393 case Tabular::ALIGN_RIGHT:
5394 setAlign = LYX_ALIGN_RIGHT;
5397 case Tabular::M_ALIGN_CENTER:
5398 case Tabular::ALIGN_CENTER:
5399 setAlign = LYX_ALIGN_CENTER;
5402 case Tabular::ALIGN_BLOCK:
5403 setAlign = LYX_ALIGN_BLOCK;
5406 case Tabular::ALIGN_DECIMAL:
5407 setAlign = LYX_ALIGN_DECIMAL;
5410 case Tabular::M_VALIGN_TOP:
5411 case Tabular::VALIGN_TOP:
5412 setVAlign = Tabular::LYX_VALIGN_TOP;
5415 case Tabular::M_VALIGN_BOTTOM:
5416 case Tabular::VALIGN_BOTTOM:
5417 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5420 case Tabular::M_VALIGN_MIDDLE:
5421 case Tabular::VALIGN_MIDDLE:
5422 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5429 cur.recordUndoInset(this);
5431 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5432 row_type const row = tabular.cellRow(cur.idx());
5433 col_type const column = tabular.cellColumn(cur.idx());
5435 Tabular::ltType ltt;
5439 case Tabular::SET_TABULAR_WIDTH:
5440 tabular.setTabularWidth(Length(value));
5443 case Tabular::SET_PWIDTH: {
5444 Length const len(value);
5445 tabular.setColumnPWidth(cur, cur.idx(), len);
5447 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5448 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5452 case Tabular::SET_MPWIDTH:
5453 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5456 case Tabular::SET_MROFFSET:
5457 tabular.setMROffset(cur, cur.idx(), Length(value));
5460 case Tabular::SET_SPECIAL_COLUMN:
5461 case Tabular::SET_SPECIAL_MULTICOLUMN:
5462 if (value == "none")
5463 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5465 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5468 case Tabular::APPEND_ROW:
5469 // append the row into the tabular
5470 tabular.appendRow(row);
5473 case Tabular::APPEND_COLUMN:
5474 // append the column into the tabular
5475 tabular.appendColumn(column);
5476 cur.idx() = tabular.cellIndex(row, column);
5479 case Tabular::DELETE_ROW:
5480 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5481 for (col_type c = 0; c < tabular.ncols(); c++)
5482 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5483 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5486 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5487 tabular.deleteRow(sel_row_start);
5488 if (sel_row_start >= tabular.nrows())
5490 cur.idx() = tabular.cellIndex(sel_row_start, column);
5493 cur.setSelection(false);
5496 case Tabular::DELETE_COLUMN:
5497 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5498 for (row_type r = 0; r < tabular.nrows(); r++)
5499 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5500 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5503 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5504 for (row_type r = 0; r < tabular.nrows(); r++)
5505 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5506 tabular.leftLine(tabular.cellIndex(r, 0)));
5509 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5510 tabular.deleteColumn(sel_col_start);
5511 if (sel_col_start >= tabular.ncols())
5513 cur.idx() = tabular.cellIndex(row, sel_col_start);
5516 cur.setSelection(false);
5519 case Tabular::COPY_ROW:
5520 tabular.copyRow(row);
5523 case Tabular::COPY_COLUMN:
5524 tabular.copyColumn(column);
5525 cur.idx() = tabular.cellIndex(row, column);
5528 case Tabular::MOVE_COLUMN_RIGHT:
5529 tabular.moveColumn(column, Tabular::RIGHT);
5530 cur.idx() = tabular.cellIndex(row, column + 1);
5533 case Tabular::MOVE_COLUMN_LEFT:
5534 tabular.moveColumn(column, Tabular::LEFT);
5535 cur.idx() = tabular.cellIndex(row, column - 1);
5538 case Tabular::MOVE_ROW_DOWN:
5539 tabular.moveRow(row, Tabular::DOWN);
5540 cur.idx() = tabular.cellIndex(row + 1, column);
5543 case Tabular::MOVE_ROW_UP:
5544 tabular.moveRow(row, Tabular::UP);
5545 cur.idx() = tabular.cellIndex(row - 1, column);
5548 case Tabular::SET_LINE_TOP:
5549 case Tabular::TOGGLE_LINE_TOP: {
5550 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5551 ? (value == "true") : !tabular.topLine(cur.idx());
5552 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5553 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5554 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5558 case Tabular::SET_LINE_BOTTOM:
5559 case Tabular::TOGGLE_LINE_BOTTOM: {
5560 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5561 ? (value == "true") : !tabular.bottomLine(cur.idx());
5562 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5563 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5564 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5568 case Tabular::SET_LINE_LEFT:
5569 case Tabular::TOGGLE_LINE_LEFT: {
5570 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5571 ? (value == "true") : !tabular.leftLine(cur.idx());
5572 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5573 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5574 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5578 case Tabular::SET_LINE_RIGHT:
5579 case Tabular::TOGGLE_LINE_RIGHT: {
5580 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5581 ? (value == "true") : !tabular.rightLine(cur.idx());
5582 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5583 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5584 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5588 case Tabular::M_ALIGN_LEFT:
5589 case Tabular::M_ALIGN_RIGHT:
5590 case Tabular::M_ALIGN_CENTER:
5591 case Tabular::ALIGN_LEFT:
5592 case Tabular::ALIGN_RIGHT:
5593 case Tabular::ALIGN_CENTER:
5594 case Tabular::ALIGN_BLOCK:
5595 case Tabular::ALIGN_DECIMAL:
5596 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5597 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5598 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5599 !tabular.getPWidth(c).zero());
5602 case Tabular::M_VALIGN_TOP:
5603 case Tabular::M_VALIGN_BOTTOM:
5604 case Tabular::M_VALIGN_MIDDLE:
5606 case Tabular::VALIGN_TOP:
5607 case Tabular::VALIGN_BOTTOM:
5608 case Tabular::VALIGN_MIDDLE:
5609 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5610 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5611 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5614 case Tabular::SET_MULTICOLUMN: {
5615 if (!cur.selection()) {
5616 // just multicol for one single cell
5617 // check whether we are completely in a multicol
5618 if (!tabular.isMultiColumn(cur.idx()))
5619 tabular.setMultiColumn(cur.idx(), 1,
5620 tabular.rightLine(cur.idx()));
5623 // we have a selection so this means we just add all these
5624 // cells to form a multicolumn cell
5625 idx_type const s_start = cur.selBegin().idx();
5626 row_type const col_start = tabular.cellColumn(s_start);
5627 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5628 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5629 tabular.rightLine(cur.selEnd().idx()));
5632 cur.setSelection(false);
5636 case Tabular::UNSET_MULTICOLUMN: {
5637 if (!cur.selection()) {
5638 if (tabular.isMultiColumn(cur.idx()))
5639 tabular.unsetMultiColumn(cur.idx());
5644 case Tabular::MULTICOLUMN: {
5645 if (!cur.selection()) {
5646 if (tabular.isMultiColumn(cur.idx()))
5647 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5649 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5653 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5654 row_type const r = sel_row_start;
5655 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5656 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5659 // If the selection contains at least one singlecol cell
5660 // or multiple multicol cells,
5661 // we assume the user will merge is to a single multicol
5663 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5665 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5669 case Tabular::SET_MULTIROW: {
5670 if (!cur.selection()) {
5671 // just multirow for one single cell
5672 // check whether we are completely in a multirow
5673 if (!tabular.isMultiRow(cur.idx()))
5674 tabular.setMultiRow(cur.idx(), 1,
5675 tabular.bottomLine(cur.idx()),
5676 tabular.getAlignment(cur.idx()));
5679 // we have a selection so this means we just add all this
5680 // cells to form a multirow cell
5681 idx_type const s_start = cur.selBegin().idx();
5682 row_type const row_start = tabular.cellRow(s_start);
5683 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5684 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5685 tabular.bottomLine(cur.selEnd().idx()),
5686 tabular.getAlignment(cur.selEnd().idx()));
5689 cur.setSelection(false);
5693 case Tabular::UNSET_MULTIROW: {
5694 if (!cur.selection()) {
5695 if (tabular.isMultiRow(cur.idx()))
5696 tabular.unsetMultiRow(cur.idx());
5701 case Tabular::MULTIROW: {
5702 if (!cur.selection()) {
5703 if (tabular.isMultiRow(cur.idx()))
5704 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5706 tabularFeatures(cur, Tabular::SET_MULTIROW);
5710 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5711 col_type const c = sel_col_start;
5712 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5713 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5716 // If the selection contains at least one singlerow cell
5717 // or multiple multirow cells,
5718 // we assume the user will merge is to a single multirow
5720 tabularFeatures(cur, Tabular::SET_MULTIROW);
5722 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5726 case Tabular::SET_ALL_LINES:
5728 case Tabular::UNSET_ALL_LINES:
5729 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5730 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5731 idx_type const cell = tabular.cellIndex(r, c);
5732 tabular.setTopLine(cell, setLines);
5733 tabular.setBottomLine(cell, setLines);
5734 tabular.setRightLine(cell, setLines);
5735 tabular.setLeftLine(cell, setLines);
5739 case Tabular::SET_BORDER_LINES:
5740 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5741 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5742 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5744 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5745 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5746 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5750 case Tabular::SET_LONGTABULAR:
5751 tabular.is_long_tabular = true;
5754 case Tabular::UNSET_LONGTABULAR:
5755 for (row_type r = 0; r < tabular.nrows(); ++r) {
5756 if (tabular.ltCaption(r)) {
5757 cur.idx() = tabular.cellIndex(r, 0);
5760 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5763 tabular.is_long_tabular = false;
5766 case Tabular::SET_ROTATE_TABULAR:
5767 tabular.rotate = convert<int>(value);
5770 case Tabular::UNSET_ROTATE_TABULAR:
5774 case Tabular::TOGGLE_ROTATE_TABULAR:
5775 // when pressing the rotate button we default to 90° rotation
5776 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5779 case Tabular::TABULAR_VALIGN_TOP:
5780 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5783 case Tabular::TABULAR_VALIGN_MIDDLE:
5784 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5787 case Tabular::TABULAR_VALIGN_BOTTOM:
5788 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5791 case Tabular::LONGTABULAR_ALIGN_LEFT:
5792 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5795 case Tabular::LONGTABULAR_ALIGN_CENTER:
5796 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5799 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5800 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5805 case Tabular::SET_ROTATE_CELL:
5806 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5807 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5808 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5811 case Tabular::UNSET_ROTATE_CELL:
5812 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5813 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5814 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5817 case Tabular::TOGGLE_ROTATE_CELL:
5819 bool oneNotRotated = oneCellHasRotationState(false,
5820 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5822 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5823 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5824 // when pressing the rotate cell button we default to 90° rotation
5826 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5828 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5833 case Tabular::SET_USEBOX: {
5834 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5835 if (val == tabular.getUsebox(cur.idx()))
5836 val = Tabular::BOX_NONE;
5837 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5838 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5839 tabular.setUsebox(tabular.cellIndex(r, c), val);
5843 case Tabular::UNSET_LTFIRSTHEAD:
5845 case Tabular::SET_LTFIRSTHEAD:
5846 tabular.getRowOfLTFirstHead(row, ltt);
5847 checkLongtableSpecial(ltt, value, flag);
5848 tabular.setLTHead(row, flag, ltt, true);
5851 case Tabular::UNSET_LTHEAD:
5853 case Tabular::SET_LTHEAD:
5854 tabular.getRowOfLTHead(row, ltt);
5855 checkLongtableSpecial(ltt, value, flag);
5856 tabular.setLTHead(row, flag, ltt, false);
5859 case Tabular::UNSET_LTFOOT:
5861 case Tabular::SET_LTFOOT:
5862 tabular.getRowOfLTFoot(row, ltt);
5863 checkLongtableSpecial(ltt, value, flag);
5864 tabular.setLTFoot(row, flag, ltt, false);
5867 case Tabular::UNSET_LTLASTFOOT:
5869 case Tabular::SET_LTLASTFOOT:
5870 tabular.getRowOfLTLastFoot(row, ltt);
5871 checkLongtableSpecial(ltt, value, flag);
5872 tabular.setLTFoot(row, flag, ltt, true);
5875 case Tabular::UNSET_LTNEWPAGE:
5877 case Tabular::SET_LTNEWPAGE:
5878 tabular.setLTNewPage(row, flag);
5881 case Tabular::SET_LTCAPTION: {
5882 if (tabular.ltCaption(row))
5884 cur.idx() = tabular.setLTCaption(row, true);
5887 cur.setSelection(false);
5888 // If a row is set as caption, then also insert
5889 // a caption. Otherwise the LaTeX output is broken.
5890 // Select cell if it is non-empty
5891 if (cur.lastpos() > 0 || cur.lastpit() > 0)
5892 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5893 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5897 case Tabular::UNSET_LTCAPTION: {
5898 if (!tabular.ltCaption(row))
5900 cur.idx() = tabular.setLTCaption(row, false);
5903 cur.setSelection(false);
5904 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5905 if (lyx::getStatus(fr).enabled())
5910 case Tabular::TOGGLE_LTCAPTION: {
5911 if (tabular.ltCaption(row))
5912 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5914 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5918 case Tabular::SET_BOOKTABS:
5919 tabular.use_booktabs = true;
5922 case Tabular::UNSET_BOOKTABS:
5923 tabular.use_booktabs = false;
5926 case Tabular::SET_TOP_SPACE: {
5928 if (value == "default")
5929 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5930 tabular.row_info[r].top_space_default = true;
5931 else if (value == "none")
5932 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5933 tabular.row_info[r].top_space_default = false;
5934 tabular.row_info[r].top_space = len;
5936 else if (isValidLength(value, &len))
5937 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5938 tabular.row_info[r].top_space_default = false;
5939 tabular.row_info[r].top_space = len;
5944 case Tabular::SET_BOTTOM_SPACE: {
5946 if (value == "default")
5947 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5948 tabular.row_info[r].bottom_space_default = true;
5949 else if (value == "none")
5950 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5951 tabular.row_info[r].bottom_space_default = false;
5952 tabular.row_info[r].bottom_space = len;
5954 else if (isValidLength(value, &len))
5955 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5956 tabular.row_info[r].bottom_space_default = false;
5957 tabular.row_info[r].bottom_space = len;
5962 case Tabular::SET_INTERLINE_SPACE: {
5964 if (value == "default")
5965 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5966 tabular.row_info[r].interline_space_default = true;
5967 else if (value == "none")
5968 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5969 tabular.row_info[r].interline_space_default = false;
5970 tabular.row_info[r].interline_space = len;
5972 else if (isValidLength(value, &len))
5973 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5974 tabular.row_info[r].interline_space_default = false;
5975 tabular.row_info[r].interline_space = len;
5980 case Tabular::SET_DECIMAL_POINT:
5981 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5982 tabular.column_info[c].decimal_point = from_utf8(value);
5985 // dummy stuff just to avoid warnings
5986 case Tabular::LAST_ACTION:
5992 bool InsetTabular::copySelection(Cursor & cur)
5994 if (!cur.selection())
5999 getSelection(cur, rs, re, cs, ce);
6001 paste_tabular.reset(new Tabular(tabular));
6003 for (row_type r = 0; r < rs; ++r)
6004 paste_tabular->deleteRow(0);
6006 row_type const rows = re - rs + 1;
6007 while (paste_tabular->nrows() > rows)
6008 paste_tabular->deleteRow(rows);
6010 for (col_type c = 0; c < cs; ++c)
6011 paste_tabular->deleteColumn(0);
6013 col_type const columns = ce - cs + 1;
6014 while (paste_tabular->ncols() > columns)
6015 paste_tabular->deleteColumn(columns);
6017 paste_tabular->setBuffer(tabular.buffer());
6019 odocstringstream os;
6020 OutputParams const runparams(0);
6021 paste_tabular->plaintext(os, runparams, 0, true, '\t', INT_MAX);
6022 // Needed for the "Edit->Paste recent" menu and the system clipboard.
6023 cap::copySelection(cur, os.str());
6025 // mark tabular stack dirty
6026 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
6027 // when we (hopefully) have a one-for-all paste mechanism.
6028 // This must be called after cap::copySelection.
6029 dirtyTabularStack(true);
6035 bool InsetTabular::pasteClipboard(Cursor & cur)
6039 col_type actcol = tabular.cellColumn(cur.idx());
6040 row_type actrow = tabular.cellRow(cur.idx());
6042 if (cur.selIsMultiCell()) {
6045 getSelection(cur, actrow, re, actcol, ce);
6048 for (row_type r1 = 0, r2 = actrow;
6049 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
6051 for (col_type c1 = 0, c2 = actcol;
6052 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
6054 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
6055 tabular.isPartOfMultiColumn(r2, c2))
6057 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
6061 if (tabular.isPartOfMultiColumn(r2, c2)) {
6065 shared_ptr<InsetTableCell> inset(
6066 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
6067 tabular.setCellInset(r2, c2, inset);
6068 // FIXME?: why do we need to do this explicitly? (EL)
6069 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
6071 // FIXME: change tracking (MG)
6072 inset->setChange(Change(buffer().params().track_changes ?
6073 Change::INSERTED : Change::UNCHANGED));
6081 void InsetTabular::cutSelection(Cursor & cur)
6083 if (!cur.selection())
6088 getSelection(cur, rs, re, cs, ce);
6089 for (row_type r = rs; r <= re; ++r) {
6090 for (col_type c = cs; c <= ce; ++c) {
6091 shared_ptr<InsetTableCell> t
6092 = cell(tabular.cellIndex(r, c));
6093 if (buffer().params().track_changes)
6094 // FIXME: Change tracking (MG)
6095 t->setChange(Change(Change::DELETED));
6101 // cursor position might be invalid now
6102 if (cur.pit() > cur.lastpit())
6103 cur.pit() = cur.lastpit();
6104 if (cur.pos() > cur.lastpos())
6105 cur.pos() = cur.lastpos();
6106 cur.clearSelection();
6110 bool InsetTabular::isRightToLeft(Cursor & cur) const
6112 // LASSERT: It might be better to abandon this Buffer.
6113 LASSERT(cur.depth() > 1, return false);
6114 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
6115 pos_type const parentpos = cur[cur.depth() - 2].pos();
6116 return parentpar.getFontSettings(buffer().params(),
6117 parentpos).language()->rightToLeft();
6121 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
6124 LASSERT(stidx <= enidx, return docstring());
6126 col_type const col1 = tabular.cellColumn(stidx);
6127 col_type const col2 = tabular.cellColumn(enidx);
6128 row_type const row1 = tabular.cellRow(stidx);
6129 row_type const row2 = tabular.cellRow(enidx);
6131 for (col_type col = col1; col <= col2; col++)
6132 for (row_type row = row1; row <= row2; row++) {
6137 retval += tabular.cellInset(row, col)->asString(intoInsets);
6143 void InsetTabular::getSelection(Cursor & cur,
6144 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
6146 CursorSlice const & beg = cur.selBegin();
6147 CursorSlice const & end = cur.selEnd();
6148 cs = tabular.cellColumn(beg.idx());
6149 ce = tabular.cellColumn(end.idx());
6153 rs = tabular.cellRow(beg.idx());
6154 re = tabular.cellRow(end.idx());
6160 Text * InsetTabular::getText(int idx) const
6162 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6166 void InsetTabular::setChange(Change const & change)
6168 for (idx_type idx = 0; idx < nargs(); ++idx)
6169 cell(idx)->setChange(change);
6173 void InsetTabular::acceptChanges()
6175 for (idx_type idx = 0; idx < nargs(); ++idx)
6176 cell(idx)->acceptChanges();
6180 void InsetTabular::rejectChanges()
6182 for (idx_type idx = 0; idx < nargs(); ++idx)
6183 cell(idx)->rejectChanges();
6187 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6189 return tabular.getPWidth(cell).zero();
6193 bool InsetTabular::forcePlainLayout(idx_type cell) const
6195 return !tabular.getPWidth(cell).zero();
6199 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6202 if (buf.length() <= 0)
6207 col_type maxCols = 1;
6208 size_t const len = buf.length();
6212 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6220 maxCols = max(cols, maxCols);
6226 maxCols = max(cols, maxCols);
6232 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6233 loctab = paste_tabular.get();
6234 dirtyTabularStack(true);
6237 cell = bv.cursor().idx();
6238 ocol = tabular.cellColumn(cell);
6239 row = tabular.cellRow(cell);
6243 idx_type const cells = loctab->numberofcells;
6246 rows = loctab->nrows();
6247 col_type const columns = loctab->ncols();
6249 while (cell < cells && p < len && row < rows &&
6250 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6256 // we can only set this if we are not too far right
6257 if (cols < columns) {
6258 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6259 Font const font = bv.textMetrics(&inset->text()).
6261 inset->setText(buf.substr(op, p - op), font,
6262 buffer().params().track_changes);
6268 // we can only set this if we are not too far right
6269 if (cols < columns) {
6270 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6271 Font const font = bv.textMetrics(&inset->text()).
6273 inset->setText(buf.substr(op, p - op), font,
6274 buffer().params().track_changes);
6279 cell = loctab->cellIndex(row, cols);
6285 // check for the last cell if there is no trailing '\n'
6286 if (cell < cells && op < len) {
6287 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6288 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6289 inset->setText(buf.substr(op, len - op), font,
6290 buffer().params().track_changes);
6296 void InsetTabular::addPreview(DocIterator const & inset_pos,
6297 PreviewLoader & loader) const
6299 DocIterator cell_pos = inset_pos;
6301 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6302 for (row_type r = 0; r < tabular.nrows(); ++r) {
6303 for (col_type c = 0; c < tabular.ncols(); ++c) {
6304 cell_pos.top().idx() = tabular.cellIndex(r, c);
6305 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6311 bool InsetTabular::completionSupported(Cursor const & cur) const
6313 Cursor const & bvCur = cur.bv().cursor();
6314 if (&bvCur.inset() != this)
6316 return cur.text()->completionSupported(cur);
6320 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6322 return completionSupported(cur);
6326 bool InsetTabular::automaticInlineCompletion() const
6328 return lyxrc.completion_inline_text;
6332 bool InsetTabular::automaticPopupCompletion() const
6334 return lyxrc.completion_popup_text;
6338 bool InsetTabular::showCompletionCursor() const
6340 return lyxrc.completion_cursor_text;
6344 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6346 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6350 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6352 if (!completionSupported(cur))
6354 return cur.text()->completionPrefix(cur);
6358 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6360 if (!completionSupported(cur))
6363 return cur.text()->insertCompletion(cur, s, finished);
6367 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6368 Dimension & dim) const
6370 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6371 tm.completionPosAndDim(cur, x, y, dim);
6375 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6377 istringstream data(in);
6379 lex.setStream(data);
6386 if (!lex || token != "tabular") {
6387 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6391 // This is part of the inset proper that is usually swallowed
6392 // by Buffer::readInset
6394 if (!lex || token != "Tabular") {
6395 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6403 string InsetTabular::params2string(InsetTabular const & inset)
6406 data << "tabular" << ' ';
6408 data << "\\end_inset\n";
6413 void InsetTabular::setLayoutForHiddenCells(DocumentClass const & dc)
6415 for (Tabular::col_type c = 0; c < tabular.ncols(); ++c) {
6416 for (Tabular::row_type r = 0; r < tabular.nrows(); ++r) {
6417 if (!tabular.isPartOfMultiColumn(r,c) &&
6418 !tabular.isPartOfMultiRow(r,c))
6421 ParagraphList & parlist = tabular.cellInset(r,c)->paragraphs();
6422 ParagraphList::iterator it = parlist.begin();
6423 ParagraphList::iterator const en = parlist.end();
6424 for (; it != en; ++it)
6425 it->setLayout(dc.plainLayout());