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
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
169 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
170 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
172 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
173 { Tabular::SET_BOOKTABS, "set-booktabs", false },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
175 { Tabular::SET_TOP_SPACE, "set-top-space", true },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
178 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
179 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
180 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
181 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
182 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
183 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
184 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
185 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
186 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
187 { Tabular::LAST_ACTION, "", false }
191 string const tostr(LyXAlignment const & num)
196 case LYX_ALIGN_BLOCK:
200 case LYX_ALIGN_CENTER:
202 case LYX_ALIGN_RIGHT:
204 case LYX_ALIGN_LAYOUT:
206 case LYX_ALIGN_SPECIAL:
208 case LYX_ALIGN_DECIMAL:
215 string const tostr(Tabular::HAlignment const & num)
218 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
220 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
222 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
229 string const tostr(Tabular::VAlignment const & num)
232 case Tabular::LYX_VALIGN_TOP:
234 case Tabular::LYX_VALIGN_MIDDLE:
236 case Tabular::LYX_VALIGN_BOTTOM:
243 string const tostr(Tabular::BoxType const & num)
246 case Tabular::BOX_NONE:
248 case Tabular::BOX_PARBOX:
250 case Tabular::BOX_MINIPAGE:
257 // I would have liked a fromstr template a lot better. (Lgb)
258 bool string2type(string const str, LyXAlignment & num)
261 num = LYX_ALIGN_NONE;
262 else if (str == "block")
263 num = LYX_ALIGN_BLOCK;
264 else if (str == "left")
265 num = LYX_ALIGN_LEFT;
266 else if (str == "center")
267 num = LYX_ALIGN_CENTER;
268 else if (str == "right")
269 num = LYX_ALIGN_RIGHT;
270 else if (str == "decimal")
271 num = LYX_ALIGN_DECIMAL;
278 bool string2type(string const str, Tabular::HAlignment & num)
281 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
282 else if (str == "center" )
283 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
284 else if (str == "right")
285 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
292 bool string2type(string const str, Tabular::VAlignment & num)
295 num = Tabular::LYX_VALIGN_TOP;
296 else if (str == "middle" )
297 num = Tabular::LYX_VALIGN_MIDDLE;
298 else if (str == "bottom")
299 num = Tabular::LYX_VALIGN_BOTTOM;
306 bool string2type(string const str, Tabular::BoxType & num)
309 num = Tabular::BOX_NONE;
310 else if (str == "parbox")
311 num = Tabular::BOX_PARBOX;
312 else if (str == "minipage")
313 num = Tabular::BOX_MINIPAGE;
320 bool string2type(string const str, bool & num)
324 else if (str == "false")
332 bool getTokenValue(string const & str, char const * token, string & ret)
335 size_t token_length = strlen(token);
336 size_t pos = str.find(token);
338 if (pos == string::npos || pos + token_length + 1 >= str.length()
339 || str[pos + token_length] != '=')
341 pos += token_length + 1;
343 if (ch != '"' && ch != '\'') { // only read till next space
347 while (pos < str.length() - 1 && str[++pos] != ch)
354 bool getTokenValue(string const & str, char const * token, docstring & ret)
357 bool const success = getTokenValue(str, token, tmp);
358 ret = from_utf8(tmp);
363 bool getTokenValue(string const & str, char const * token, int & num)
367 if (!getTokenValue(str, token, tmp))
369 num = convert<int>(tmp);
374 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
377 return getTokenValue(str, token, tmp) && string2type(tmp, num);
381 bool getTokenValue(string const & str, char const * token,
382 Tabular::HAlignment & num)
385 return getTokenValue(str, token, tmp) && string2type(tmp, num);
389 bool getTokenValue(string const & str, char const * token,
390 Tabular::VAlignment & num)
393 return getTokenValue(str, token, tmp) && string2type(tmp, num);
397 bool getTokenValue(string const & str, char const * token,
398 Tabular::BoxType & num)
401 return getTokenValue(str, token, tmp) && string2type(tmp, num);
405 bool getTokenValue(string const & str, char const * token, bool & flag)
407 // set the flag always to false as this should be the default for bools
408 // not in the file-format.
411 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
415 bool getTokenValue(string const & str, char const * token, Length & len)
417 // set the length to be zero() as default as this it should be if not
418 // in the file format.
421 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
425 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
430 if (!getTokenValue(str, token, tmp))
432 if (tmp == "default") {
436 return isValidLength(tmp, &len);
440 void l_getline(istream & is, string & str)
443 while (str.empty()) {
445 if (!str.empty() && str[str.length() - 1] == '\r')
446 str.erase(str.length() - 1);
451 string const write_attribute(string const & name, T const & t)
453 string const s = tostr(t);
454 return s.empty() ? s : " " + name + "=\"" + s + "\"";
458 string const write_attribute(string const & name, string const & t)
460 return t.empty() ? t : " " + name + "=\"" + t + "\"";
465 string const write_attribute(string const & name, docstring const & t)
467 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
472 string const write_attribute(string const & name, bool const & b)
474 // we write only true attribute values so we remove a bit of the
475 // file format bloat for tabulars.
476 return b ? write_attribute(name, convert<string>(b)) : string();
481 string const write_attribute(string const & name, int const & i)
483 // we write only true attribute values so we remove a bit of the
484 // file format bloat for tabulars.
485 return i ? write_attribute(name, convert<string>(i)) : string();
490 string const write_attribute(string const & name, Tabular::idx_type const & i)
492 // we write only true attribute values so we remove a bit of the
493 // file format bloat for tabulars.
494 return i ? write_attribute(name, convert<string>(i)) : string();
499 string const write_attribute(string const & name, Length const & value)
501 // we write only the value if we really have one same reson as above.
502 return value.zero() ? string() : write_attribute(name, value.asString());
508 string const featureAsString(Tabular::Feature action)
510 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
511 if (tabularFeature[i].action == action)
512 return tabularFeature[i].feature;
518 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
520 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
521 for (; dit; dit.forwardChar())
522 if (dit.inTexted() && dit.depth() == 1
523 && dit.paragraph().find(align_d, false, false, dit.pos()))
530 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
532 InsetTableCell tail = InsetTableCell(head);
533 DocIterator const dit = separatorPos(&head, align_d);
536 pit_type const psize = head.paragraphs().front().size();
537 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
538 tail.paragraphs().front().eraseChars(0,
539 dit.pos() < psize ? dit.pos() + 1 : psize, false);
546 /////////////////////////////////////////////////////////////////////
550 /////////////////////////////////////////////////////////////////////
553 Tabular::CellData::CellData(Buffer * buf)
556 multicolumn(Tabular::CELL_NORMAL),
557 multirow(Tabular::CELL_NORMAL),
558 alignment(LYX_ALIGN_CENTER),
559 valignment(LYX_VALIGN_TOP),
569 inset(new InsetTableCell(buf))
571 inset->setBuffer(*buf);
575 Tabular::CellData::CellData(CellData const & cs)
578 multicolumn(cs.multicolumn),
579 multirow(cs.multirow),
580 mroffset(cs.mroffset),
581 alignment(cs.alignment),
582 valignment(cs.valignment),
583 decimal_hoffset(cs.decimal_hoffset),
584 decimal_width(cs.decimal_width),
586 top_line(cs.top_line),
587 bottom_line(cs.bottom_line),
588 left_line(cs.left_line),
589 right_line(cs.right_line),
592 align_special(cs.align_special),
594 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
598 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
604 void Tabular::CellData::swap(CellData & rhs)
606 std::swap(cellno, rhs.cellno);
607 std::swap(width, rhs.width);
608 std::swap(multicolumn, rhs.multicolumn);
609 std::swap(multirow, rhs.multirow);
610 std::swap(mroffset, rhs.mroffset);
611 std::swap(alignment, rhs.alignment);
612 std::swap(valignment, rhs.valignment);
613 std::swap(decimal_hoffset, rhs.decimal_hoffset);
614 std::swap(decimal_width, rhs.decimal_width);
615 std::swap(voffset, rhs.voffset);
616 std::swap(top_line, rhs.top_line);
617 std::swap(bottom_line, rhs.bottom_line);
618 std::swap(left_line, rhs.left_line);
619 std::swap(right_line, rhs.right_line);
620 std::swap(usebox, rhs.usebox);
621 std::swap(rotate, rhs.rotate);
622 std::swap(align_special, rhs.align_special);
623 p_width.swap(rhs.p_width);
624 inset.swap(rhs.inset);
628 Tabular::RowData::RowData()
631 top_space_default(false),
632 bottom_space_default(false),
633 interline_space_default(false),
643 Tabular::ColumnData::ColumnData()
644 : alignment(LYX_ALIGN_CENTER),
645 valignment(LYX_VALIGN_TOP),
651 Tabular::ltType::ltType()
658 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
660 init(buffer, rows_arg, columns_arg);
664 void Tabular::setBuffer(Buffer & buffer)
667 for (row_type i = 0; i < nrows(); ++i)
668 for (col_type j = 0; j < ncols(); ++j)
669 cell_info[i][j].inset->setBuffer(*buffer_);
673 // activates all lines and sets all widths to 0
674 void Tabular::init(Buffer * buf, row_type rows_arg,
675 col_type columns_arg)
678 row_info = row_vector(rows_arg);
679 column_info = column_vector(columns_arg);
680 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
681 row_info.reserve(10);
682 column_info.reserve(10);
683 cell_info.reserve(100);
685 is_long_tabular = false;
686 tabular_valignment = LYX_VALIGN_MIDDLE;
687 tabular_width = Length();
688 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
690 use_booktabs = false;
691 // set silly default lines
692 for (row_type r = 0; r < nrows(); ++r)
693 for (col_type c = 0; c < ncols(); ++c) {
694 cell_info[r][c].inset->setBuffer(*buffer_);
695 cell_info[r][c].top_line = true;
696 cell_info[r][c].left_line = true;
697 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
698 cell_info[r][c].right_line = c == ncols() - 1;
703 void Tabular::appendRow(row_type row)
705 row_info.insert(row_info.begin() + row + 1, RowData());
706 row_info[row + 1] = row_info[row];
708 cell_info.insert(cell_info.begin() + row + 1,
709 cell_vector(ncols(), CellData(buffer_)));
710 for (col_type c = 0; c < ncols(); ++c) {
711 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
712 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
714 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
718 for (col_type c = 0; c < ncols(); ++c) {
719 if (isPartOfMultiRow(row, c))
721 // inherit line settings
722 idx_type const i = cellIndex(row + 1, c);
723 idx_type const j = cellIndex(row, c);
724 setLeftLine(i, leftLine(j));
725 setRightLine(i, rightLine(j));
726 setTopLine(i, topLine(j));
727 if (topLine(j) && bottomLine(j)) {
728 setBottomLine(i, true);
729 setBottomLine(j, false);
731 // mark track changes
732 if (buffer().params().trackChanges)
733 cellInfo(i).inset->setChange(Change(Change::INSERTED));
738 void Tabular::deleteRow(row_type const row)
740 // Not allowed to delete last row
744 for (col_type c = 0; c < ncols(); ++c) {
745 // Care about multirow cells
746 if (row + 1 < nrows() &&
747 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
748 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
749 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
752 row_info.erase(row_info.begin() + row);
753 cell_info.erase(cell_info.begin() + row);
758 void Tabular::copyRow(row_type const row)
760 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
761 cell_info.insert(cell_info.begin() + row + 1,
762 cell_vector(0, CellData(buffer_)));
764 for (col_type c = 0; c < ncols(); ++c) {
765 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
766 CellData(cell_info[row][c]));
767 if (buffer().params().trackChanges)
768 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
769 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
770 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
774 for (col_type c = 0; c < ncols(); ++c) {
775 if (isPartOfMultiRow(row, c))
777 // inherit line settings
778 idx_type const i = cellIndex(row + 1, c);
779 idx_type const j = cellIndex(row, c);
780 setLeftLine(i, leftLine(j));
781 setRightLine(i, rightLine(j));
782 setTopLine(i, topLine(j));
783 if (topLine(j) && bottomLine(j)) {
784 setBottomLine(i, true);
785 setBottomLine(j, false);
787 // mark track changes
788 if (buffer().params().trackChanges)
789 cellInfo(i).inset->setChange(Change(Change::INSERTED));
794 void Tabular::appendColumn(col_type c)
796 column_info.insert(column_info.begin() + c + 1, ColumnData());
797 column_info[c + 1] = column_info[c];
799 for (row_type r = 0; r < nrows(); ++r) {
800 cell_info[r].insert(cell_info[r].begin() + c + 1,
802 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
803 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
805 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
808 for (row_type r = 0; r < nrows(); ++r) {
809 // inherit line settings
810 idx_type const i = cellIndex(r, c + 1);
811 idx_type const j = cellIndex(r, c);
812 setBottomLine(i, bottomLine(j));
813 setTopLine(i, topLine(j));
814 setLeftLine(i, leftLine(j));
815 if (rightLine(j) && rightLine(j)) {
816 setRightLine(i, true);
817 setRightLine(j, false);
819 if (buffer().params().trackChanges)
820 cellInfo(i).inset->setChange(Change(Change::INSERTED));
825 void Tabular::deleteColumn(col_type const col)
827 // Not allowed to delete last column
831 for (row_type r = 0; r < nrows(); ++r) {
832 // Care about multicolumn cells
833 if (col + 1 < ncols() &&
834 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
835 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
836 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
838 cell_info[r].erase(cell_info[r].begin() + col);
840 column_info.erase(column_info.begin() + col);
845 void Tabular::copyColumn(col_type const col)
847 BufferParams const & bp = buffer().params();
848 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
850 for (row_type r = 0; r < nrows(); ++r) {
851 cell_info[r].insert(cell_info[r].begin() + col + 1, CellData(cell_info[r][col]));
853 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
854 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
855 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
858 for (row_type r = 0; r < nrows(); ++r) {
859 // inherit line settings
860 idx_type const i = cellIndex(r, col + 1);
861 idx_type const j = cellIndex(r, col);
862 setBottomLine(i, bottomLine(j));
863 setTopLine(i, topLine(j));
864 setLeftLine(i, leftLine(j));
865 if (rightLine(j) && rightLine(j)) {
866 setRightLine(i, true);
867 setRightLine(j, false);
869 if (buffer().params().trackChanges)
870 cellInfo(i).inset->setChange(Change(Change::INSERTED));
875 void Tabular::updateIndexes()
880 for (row_type row = 0; row < nrows(); ++row)
881 for (col_type column = 0; column < ncols(); ++column) {
882 if (!isPartOfMultiColumn(row, column)
883 && !isPartOfMultiRow(row, column))
885 if (isPartOfMultiRow(row, column))
886 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
888 cell_info[row][column].cellno = numberofcells - 1;
891 rowofcell.resize(numberofcells);
892 columnofcell.resize(numberofcells);
894 // reset column and row of cells and update their width and alignment
895 for (row_type row = 0; row < nrows(); ++row)
896 for (col_type column = 0; column < ncols(); ++column) {
897 if (isPartOfMultiColumn(row, column))
899 // columnofcell needs to be called before setting width and aligment
900 // multirow cells inherit the width from the column width
901 if (!isPartOfMultiRow(row, column)) {
902 columnofcell[i] = column;
905 setFixedWidth(row, column);
906 if (isPartOfMultiRow(row, column))
908 cell_info[row][column].inset->setContentAlignment(
909 getAlignment(cellIndex(row, column)));
915 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
918 for (col_type c = 0; c < ncols(); ++c)
919 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
925 bool Tabular::topLine(idx_type const cell) const
927 return cellInfo(cell).top_line;
931 bool Tabular::bottomLine(idx_type const cell) const
933 return cellInfo(cell).bottom_line;
937 bool Tabular::leftLine(idx_type cell) const
941 return cellInfo(cell).left_line;
945 bool Tabular::rightLine(idx_type cell) const
949 return cellInfo(cell).right_line;
953 int Tabular::interRowSpace(row_type row) const
955 if (!row || row >= nrows())
958 int const interline_space = row_info[row - 1].interline_space_default ?
960 row_info[row - 1].interline_space.inPixels(width());
961 if (rowTopLine(row) && rowBottomLine(row - 1))
962 return interline_space + WIDTH_OF_LINE;
963 return interline_space;
967 int Tabular::interColumnSpace(idx_type cell) const
969 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
970 if (rightLine(cell) && nextcol < ncols()
971 && leftLine(cellIndex(cellRow(cell), nextcol)))
972 return WIDTH_OF_LINE;
977 int Tabular::cellWidth(idx_type cell) const
980 col_type const span = columnSpan(cell);
981 col_type const col = cellColumn(cell);
982 for(col_type c = col; c < col + span ; ++c)
983 w += column_info[c].width;
988 int Tabular::cellHeight(idx_type cell) const
990 row_type const span = rowSpan(cell);
991 row_type const row = cellRow(cell);
993 for(row_type r = row; r < row + span ; ++r) {
994 h += rowAscent(r) + rowDescent(r);
995 if (r != row + span - 1)
996 h += interRowSpace(r + 1);
1003 bool Tabular::updateColumnWidths()
1005 vector<int> max_dwidth(ncols(), 0);
1006 for(col_type c = 0; c < ncols(); ++c)
1007 for(row_type r = 0; r < nrows(); ++r) {
1008 idx_type const i = cellIndex(r, c);
1009 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
1010 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
1013 bool update = false;
1014 // for each col get max of single col cells
1015 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 (columnSpan(i) == 1) {
1020 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1021 && cell_info[r][c].decimal_width!=0)
1022 new_width = max(new_width, cellInfo(i).width
1023 + max_dwidth[c] - cellInfo(i).decimal_width);
1025 new_width = max(new_width, cellInfo(i).width);
1029 if (column_info[c].width != new_width) {
1030 column_info[c].width = new_width;
1034 // update col widths to fit merged cells
1035 for(col_type c = 0; c < ncols(); ++c)
1036 for(row_type r = 0; r < nrows(); ++r) {
1037 idx_type const i = cellIndex(r, c);
1038 int const span = columnSpan(i);
1039 if (span == 1 || c > cellColumn(i))
1043 for(col_type j = c; j < c + span ; ++j)
1044 old_width += column_info[j].width;
1046 if (cellInfo(i).width > old_width) {
1047 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1056 int Tabular::width() const
1059 for (col_type c = 0; c < ncols(); ++c)
1060 width += column_info[c].width;
1065 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1068 col_type const col = cellColumn(cell);
1069 // set alignment for the whole row if we are not in a multicolumn cell,
1070 // exclude possible multicolumn cells in the row
1071 if (!isMultiColumn(cell)) {
1072 for (row_type r = 0; r < nrows(); ++r) {
1073 // only if the column has no width the multirow inherits the
1074 // alignment of the column, otherwise it is left aligned
1075 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1076 && !isMultiColumn(cellIndex(r, col))) {
1077 cell_info[r][col].alignment = align;
1078 cell_info[r][col].inset->setContentAlignment(align);
1080 if ((isMultiRow(cellIndex(r, col)) && has_width)
1081 && !isMultiColumn(cellIndex(r, col))) {
1082 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1083 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1086 column_info[col].alignment = align;
1087 docstring & dpoint = column_info[col].decimal_point;
1088 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1089 dpoint = from_utf8(lyxrc.default_decimal_point);
1091 cellInfo(cell).alignment = align;
1092 cellInset(cell).get()->setContentAlignment(align);
1097 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1100 if (!isMultiColumn(cell) || onlycolumn)
1101 column_info[cellColumn(cell)].valignment = align;
1103 cellInfo(cell).valignment = align;
1110 * Allow line and paragraph breaks for fixed width cells or disallow them,
1111 * merge cell paragraphs and reset layout to standard for variable width
1114 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1116 inset->setAutoBreakRows(fixedWidth);
1117 inset->toggleFixedWidth(fixedWidth);
1121 // merge all paragraphs to one
1122 BufferParams const & bp = cur.bv().buffer().params();
1123 while (inset->paragraphs().size() > 1)
1124 mergeParagraph(bp, inset->paragraphs(), 0);
1128 // undo information has already been recorded
1129 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1130 bp.documentClass().plainLayoutName());
1137 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1138 Length const & width)
1140 col_type const c = cellColumn(cell);
1142 column_info[c].p_width = width;
1143 // reset the vertical alignment to top if the fixed with
1144 // is removed or zero because only fixed width columns can
1145 // have a vertical alignment
1146 if (column_info[c].p_width.zero())
1147 column_info[c].valignment = LYX_VALIGN_TOP;
1148 for (row_type r = 0; r < nrows(); ++r) {
1149 idx_type const cell = cellIndex(r, c);
1150 // because of multicolumns
1151 toggleFixedWidth(cur, cellInset(cell).get(),
1152 !getPWidth(cell).zero());
1154 // cur paragraph can become invalid after paragraphs were merged
1155 if (cur.pit() > cur.lastpit())
1156 cur.pit() = cur.lastpit();
1157 // cur position can become invalid after newlines were removed
1158 if (cur.pos() > cur.lastpos())
1159 cur.pos() = cur.lastpos();
1163 bool Tabular::setFixedWidth(row_type r, col_type c)
1165 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1166 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1167 || (multicol && !cell_info[r][c].p_width.zero());
1168 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1173 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1174 Length const & width)
1176 if (!isMultiColumn(cell))
1179 cellInfo(cell).p_width = width;
1180 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1181 // cur paragraph can become invalid after paragraphs were merged
1182 if (cur.pit() > cur.lastpit())
1183 cur.pit() = cur.lastpit();
1184 // cur position can become invalid after newlines were removed
1185 if (cur.pos() > cur.lastpos())
1186 cur.pos() = cur.lastpos();
1191 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1193 cellInfo(cell).mroffset = mroffset;
1198 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1199 Tabular::Feature what)
1201 if (what == SET_SPECIAL_MULTICOLUMN)
1202 cellInfo(cell).align_special = special;
1204 column_info[cellColumn(cell)].align_special = special;
1208 void Tabular::setTopLine(idx_type i, bool line)
1210 cellInfo(i).top_line = line;
1214 void Tabular::setBottomLine(idx_type i, bool line)
1216 cellInfo(i).bottom_line = line;
1220 void Tabular::setLeftLine(idx_type cell, bool line)
1222 cellInfo(cell).left_line = line;
1226 void Tabular::setRightLine(idx_type cell, bool line)
1228 cellInfo(cell).right_line = line;
1231 bool Tabular::rowTopLine(row_type r) const
1233 bool all_rows_set = true;
1234 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1235 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1236 return all_rows_set;
1240 bool Tabular::rowBottomLine(row_type r) const
1242 bool all_rows_set = true;
1243 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1244 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1245 return all_rows_set;
1249 bool Tabular::columnLeftLine(col_type c) const
1256 for (row_type r = 0; r < nrows(); ++r) {
1257 idx_type const i = cellIndex(r, c);
1258 if (c == cellColumn(i)) {
1260 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1261 if (cellInfo(i).left_line || right)
1265 return 2 * nrows_left >= total;
1269 bool Tabular::columnRightLine(col_type c) const
1274 int nrows_right = 0;
1276 for (row_type r = 0; r < nrows(); ++r) {
1277 idx_type i = cellIndex(r, c);
1278 if (c == cellColumn(i) + columnSpan(i) - 1) {
1280 bool left = (c + 1 < ncols()
1281 && cellInfo(cellIndex(r, c + 1)).left_line)
1282 || c + 1 == ncols();
1283 if (cellInfo(i).right_line && left)
1287 return 2 * nrows_right >= total;
1291 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1293 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1294 return cellInfo(cell).alignment;
1296 return column_info[cellColumn(cell)].alignment;
1301 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1303 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1304 return cellInfo(cell).valignment;
1305 return column_info[cellColumn(cell)].valignment;
1309 Length const Tabular::getPWidth(idx_type cell) const
1311 if (isMultiColumn(cell))
1312 return cellInfo(cell).p_width;
1313 return column_info[cellColumn(cell)].p_width;
1317 Length const Tabular::getMROffset(idx_type cell) const
1319 return cellInfo(cell).mroffset;
1323 int Tabular::textHOffset(idx_type cell) const
1325 // the LaTeX Way :-(
1326 int x = WIDTH_OF_LINE;
1328 int const w = cellWidth(cell) - cellInfo(cell).width;
1330 switch (getAlignment(cell)) {
1331 case LYX_ALIGN_CENTER:
1334 case LYX_ALIGN_RIGHT:
1337 case LYX_ALIGN_DECIMAL: {
1338 // we center when no decimal point
1339 if (cellInfo(cell).decimal_width == 0) {
1343 col_type const c = cellColumn(cell);
1344 int max_dhoffset = 0;
1345 for(row_type r = 0; r < row_info.size() ; ++r) {
1346 idx_type const i = cellIndex(r, c);
1347 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1348 && cellInfo(i).decimal_width != 0)
1349 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1351 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1354 // LYX_ALIGN_LEFT: nothing :-)
1362 int Tabular::textVOffset(idx_type cell) const
1364 int voffset = cellInfo(cell).voffset;
1365 if (isMultiRow(cell)) {
1366 row_type const row = cellRow(cell);
1367 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1373 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1376 idx_type const numcells = numberOfCellsInRow(row);
1377 // we check against numcells to make sure we do not crash if all the
1378 // cells are multirow (bug #7535), but in that case our return value
1379 // is really invalid, i.e., it is NOT the first cell in the row. but
1380 // i do not know what to do here. (rgh)
1381 while (c < numcells - 1
1382 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1384 return cell_info[row][c].cellno;
1388 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1390 col_type c = ncols() - 1;
1391 // of course we check against 0 so we don't crash. but we have the same
1392 // problem as in the previous routine: if all the cells are part of a
1393 // multirow or part of a multi column, then our return value is invalid.
1395 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1396 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1398 return cell_info[row][c].cellno;
1402 Tabular::row_type Tabular::cellRow(idx_type cell) const
1404 if (cell >= numberofcells)
1408 return rowofcell[cell];
1412 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1414 if (cell >= numberofcells)
1418 return columnofcell[cell];
1422 void Tabular::write(ostream & os) const
1426 << write_attribute("version", 3)
1427 << write_attribute("rows", nrows())
1428 << write_attribute("columns", ncols())
1430 // global longtable options
1432 << write_attribute("rotate", rotate)
1433 << write_attribute("booktabs", use_booktabs)
1434 << write_attribute("islongtable", is_long_tabular)
1435 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1436 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1437 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1438 << write_attribute("headTopDL", endhead.topDL)
1439 << write_attribute("headBottomDL", endhead.bottomDL)
1440 << write_attribute("footTopDL", endfoot.topDL)
1441 << write_attribute("footBottomDL", endfoot.bottomDL)
1442 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1443 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1444 << write_attribute("lastFootEmpty", endlastfoot.empty);
1445 // longtables cannot be aligned vertically
1446 if (!is_long_tabular) {
1447 os << write_attribute("tabularvalignment", tabular_valignment);
1448 os << write_attribute("tabularwidth", tabular_width);
1450 if (is_long_tabular)
1451 os << write_attribute("longtabularalignment",
1452 longtabular_alignment);
1454 for (col_type c = 0; c < ncols(); ++c) {
1456 << write_attribute("alignment", column_info[c].alignment);
1457 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1458 os << write_attribute("decimal_point", column_info[c].decimal_point);
1459 os << write_attribute("valignment", column_info[c].valignment)
1460 << write_attribute("width", column_info[c].p_width.asString())
1461 << write_attribute("special", column_info[c].align_special)
1464 for (row_type r = 0; r < nrows(); ++r) {
1465 static const string def("default");
1467 if (row_info[r].top_space_default)
1468 os << write_attribute("topspace", def);
1470 os << write_attribute("topspace", row_info[r].top_space);
1471 if (row_info[r].bottom_space_default)
1472 os << write_attribute("bottomspace", def);
1474 os << write_attribute("bottomspace", row_info[r].bottom_space);
1475 if (row_info[r].interline_space_default)
1476 os << write_attribute("interlinespace", def);
1478 os << write_attribute("interlinespace", row_info[r].interline_space);
1479 os << write_attribute("endhead", row_info[r].endhead)
1480 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1481 << write_attribute("endfoot", row_info[r].endfoot)
1482 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1483 << write_attribute("newpage", row_info[r].newpage)
1484 << write_attribute("caption", row_info[r].caption)
1486 for (col_type c = 0; c < ncols(); ++c) {
1488 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1489 << write_attribute("multirow", cell_info[r][c].multirow)
1490 << write_attribute("mroffset", cell_info[r][c].mroffset)
1491 << write_attribute("alignment", cell_info[r][c].alignment)
1492 << write_attribute("valignment", cell_info[r][c].valignment)
1493 << write_attribute("topline", cell_info[r][c].top_line)
1494 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1495 << write_attribute("leftline", cell_info[r][c].left_line)
1496 << write_attribute("rightline", cell_info[r][c].right_line)
1497 << write_attribute("rotate", cell_info[r][c].rotate)
1498 << write_attribute("usebox", cell_info[r][c].usebox)
1499 << write_attribute("width", cell_info[r][c].p_width)
1500 << write_attribute("special", cell_info[r][c].align_special)
1502 os << "\\begin_inset ";
1503 cell_info[r][c].inset->write(os);
1504 os << "\n\\end_inset\n"
1509 os << "</lyxtabular>\n";
1513 void Tabular::read(Lexer & lex)
1516 istream & is = lex.getStream();
1518 l_getline(is, line);
1519 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1520 LASSERT(false, /**/);
1525 if (!getTokenValue(line, "version", version))
1527 LASSERT(version >= 2, /**/);
1530 if (!getTokenValue(line, "rows", rows_arg))
1533 if (!getTokenValue(line, "columns", columns_arg))
1535 init(buffer_, rows_arg, columns_arg);
1536 l_getline(is, line);
1537 if (!prefixIs(line, "<features")) {
1538 lyxerr << "Wrong tabular format (expected <features ...> got"
1539 << line << ')' << endl;
1542 getTokenValue(line, "rotate", rotate);
1543 getTokenValue(line, "booktabs", use_booktabs);
1544 getTokenValue(line, "islongtable", is_long_tabular);
1545 getTokenValue(line, "tabularvalignment", tabular_valignment);
1546 getTokenValue(line, "tabularwidth", tabular_width);
1547 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1548 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1549 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1550 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1551 getTokenValue(line, "headTopDL", endhead.topDL);
1552 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1553 getTokenValue(line, "footTopDL", endfoot.topDL);
1554 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1555 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1556 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1557 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1559 for (col_type c = 0; c < ncols(); ++c) {
1561 if (!prefixIs(line,"<column")) {
1562 lyxerr << "Wrong tabular format (expected <column ...> got"
1563 << line << ')' << endl;
1566 getTokenValue(line, "alignment", column_info[c].alignment);
1567 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1568 getTokenValue(line, "valignment", column_info[c].valignment);
1569 getTokenValue(line, "width", column_info[c].p_width);
1570 getTokenValue(line, "special", column_info[c].align_special);
1573 for (row_type i = 0; i < nrows(); ++i) {
1574 l_getline(is, line);
1575 if (!prefixIs(line, "<row")) {
1576 lyxerr << "Wrong tabular format (expected <row ...> got"
1577 << line << ')' << endl;
1580 getTokenValue(line, "topspace", row_info[i].top_space,
1581 row_info[i].top_space_default);
1582 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1583 row_info[i].bottom_space_default);
1584 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1585 row_info[i].interline_space_default);
1586 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1587 getTokenValue(line, "endhead", row_info[i].endhead);
1588 getTokenValue(line, "endfoot", row_info[i].endfoot);
1589 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1590 getTokenValue(line, "newpage", row_info[i].newpage);
1591 getTokenValue(line, "caption", row_info[i].caption);
1592 for (col_type j = 0; j < ncols(); ++j) {
1593 l_getline(is, line);
1594 if (!prefixIs(line, "<cell")) {
1595 lyxerr << "Wrong tabular format (expected <cell ...> got"
1596 << line << ')' << endl;
1599 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1600 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1601 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1602 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1603 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1604 getTokenValue(line, "topline", cell_info[i][j].top_line);
1605 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1606 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1607 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1608 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1609 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1610 getTokenValue(line, "width", cell_info[i][j].p_width);
1612 getTokenValue(line, "special", cell_info[i][j].align_special);
1613 l_getline(is, line);
1614 if (prefixIs(line, "\\begin_inset")) {
1615 cell_info[i][j].inset->setBuffer(*buffer_);
1616 cell_info[i][j].inset->read(lex);
1617 l_getline(is, line);
1619 if (!prefixIs(line, "</cell>")) {
1620 lyxerr << "Wrong tabular format (expected </cell> got"
1621 << line << ')' << endl;
1625 l_getline(is, line);
1626 if (!prefixIs(line, "</row>")) {
1627 lyxerr << "Wrong tabular format (expected </row> got"
1628 << line << ')' << endl;
1632 while (!prefixIs(line, "</lyxtabular>")) {
1633 l_getline(is, line);
1639 bool Tabular::isMultiColumn(idx_type cell) const
1641 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1642 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1646 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1648 return cell_info[cellRow(cell)][cellColumn(cell)];
1652 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1653 bool const right_border)
1655 idx_type const col = cellColumn(cell);
1656 idx_type const row = cellRow(cell);
1657 for (idx_type i = 0; i < number; ++i)
1658 unsetMultiRow(cellIndex(row, col + i));
1660 // unsetting of multirow may have invalidated cell index
1661 cell = cellIndex(row, col);
1662 CellData & cs = cellInfo(cell);
1663 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1664 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1665 cs.alignment = column_info[col].alignment;
1667 setRightLine(cell, right_border);
1669 for (idx_type i = 1; i < number; ++i) {
1670 CellData & cs1 = cellInfo(cell + i);
1671 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1672 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1680 bool Tabular::isMultiRow(idx_type cell) const
1682 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1683 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1687 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1688 bool const bottom_border)
1690 idx_type const col = cellColumn(cell);
1691 idx_type const row = cellRow(cell);
1692 for (idx_type i = 0; i < number; ++i)
1693 unsetMultiColumn(cellIndex(row + i, col));
1695 // unsetting of multirow may have invalidated cell index
1696 cell = cellIndex(row, col);
1697 CellData & cs = cellInfo(cell);
1698 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1699 cs.valignment = LYX_VALIGN_MIDDLE;
1700 // the horizontal alignment of multirow cells can only
1701 // be changed for the whole table row,
1702 // support changing this only for the multirow cell can be done via
1704 // this feature would be a fileformat change
1705 // until LyX supports this, use the deault alignment of multirow
1707 cs.alignment = LYX_ALIGN_LEFT;
1709 // set the bottom line of the last selected cell
1710 setBottomLine(cell, bottom_border);
1712 for (idx_type i = 1; i < number; ++i) {
1713 CellData & cs1 = cell_info[row + i][col];
1714 cs1.multirow = CELL_PART_OF_MULTIROW;
1715 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1723 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1725 row_type const row = cellRow(cell);
1726 col_type const col = cellColumn(cell);
1728 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1735 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1737 col_type const column = cellColumn(cell);
1738 col_type row = cellRow(cell) + 1;
1739 while (row < nrows() && isPartOfMultiRow(row, column))
1742 return row - cellRow(cell);
1746 void Tabular::unsetMultiColumn(idx_type cell)
1748 if (!isMultiColumn(cell))
1751 row_type const row = cellRow(cell);
1752 col_type const col = cellColumn(cell);
1753 row_type const span = columnSpan(cell);
1754 for (col_type c = 0; c < span; ++c) {
1755 // in the table dialog the lines are set in every case
1756 // when unsetting a multicolumn this leads to an additional right
1757 // line for every cell that was part of the former multicolumn cell,
1758 // except if the cell is in the last column
1759 // therefore remove this line
1760 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1761 && (col + c) < (col + span - 1))
1762 cell_info[row][col + c].right_line = false;
1763 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1769 void Tabular::unsetMultiRow(idx_type cell)
1771 if (!isMultiRow(cell))
1774 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1775 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1776 row_type const row = cellRow(cell);
1777 col_type const col = cellColumn(cell);
1778 row_type const span = rowSpan(cell);
1779 for (row_type r = 0; r < span; ++r)
1780 cell_info[row + r][col].multirow = CELL_NORMAL;
1785 void Tabular::setRotateCell(idx_type cell, bool flag)
1787 cellInfo(cell).rotate = flag;
1791 bool Tabular::getRotateCell(idx_type cell) const
1793 return cellInfo(cell).rotate;
1797 bool Tabular::needRotating() const
1801 for (row_type r = 0; r < nrows(); ++r)
1802 for (col_type c = 0; c < ncols(); ++c)
1803 if (cell_info[r][c].rotate)
1809 bool Tabular::isLastCell(idx_type cell) const
1811 if (cell + 1 < numberofcells)
1817 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1819 if (cellRow(cell) == 0)
1822 col_type const col = cellColumn(cell);
1823 row_type r = cellRow(cell) - 1;
1824 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1827 return cell_info[r][col].cellno;
1831 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1833 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1834 if (nextrow < nrows())
1835 return cell_info[nextrow][cellColumn(cell)].cellno;
1840 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1842 LASSERT(column != npos && column < ncols()
1843 && row != npos && row < nrows(), /**/);
1844 return cell_info[row][column].cellno;
1848 void Tabular::setUsebox(idx_type cell, BoxType type)
1850 cellInfo(cell).usebox = type;
1854 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1855 // adds line breaks, see bug 4886.
1856 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1858 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1859 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1861 if (cellInfo(cell).usebox > 1)
1862 return cellInfo(cell).usebox;
1863 return useParbox(cell);
1868 // This are functions used for the longtable support
1870 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1876 row_info[row].endfirsthead = flag;
1880 row_info[row].endhead = flag;
1885 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1888 hd.set = haveLTHead();
1889 return row_info[row].endhead;
1893 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1896 hd.set = haveLTFirstHead();
1897 return row_info[row].endfirsthead;
1901 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1907 row_info[row].endlastfoot = flag;
1911 row_info[row].endfoot = flag;
1916 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1919 fd.set = haveLTFoot();
1920 return row_info[row].endfoot;
1924 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1927 fd.set = haveLTLastFoot();
1928 return row_info[row].endlastfoot;
1932 void Tabular::setLTNewPage(row_type row, bool what)
1934 row_info[row].newpage = what;
1938 bool Tabular::getLTNewPage(row_type row) const
1940 return row_info[row].newpage;
1944 bool Tabular::haveLTHead(bool withcaptions) const
1946 if (!is_long_tabular)
1948 for (row_type i = 0; i < nrows(); ++i)
1949 if (row_info[i].endhead &&
1950 (withcaptions || !row_info[i].caption))
1956 bool Tabular::haveLTFirstHead(bool withcaptions) const
1958 if (!is_long_tabular || endfirsthead.empty)
1960 for (row_type r = 0; r < nrows(); ++r)
1961 if (row_info[r].endfirsthead &&
1962 (withcaptions || !row_info[r].caption))
1968 bool Tabular::haveLTFoot(bool withcaptions) const
1970 if (!is_long_tabular)
1972 for (row_type r = 0; r < nrows(); ++r)
1973 if (row_info[r].endfoot &&
1974 (withcaptions || !row_info[r].caption))
1980 bool Tabular::haveLTLastFoot(bool withcaptions) const
1982 if (!is_long_tabular || endlastfoot.empty)
1984 for (row_type r = 0; r < nrows(); ++r)
1985 if (row_info[r].endlastfoot &&
1986 (withcaptions || !row_info[r].caption))
1992 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1994 idx_type i = getFirstCellInRow(row);
1996 setMultiColumn(i, numberOfCellsInRow(row), false);
1997 setTopLine(i, false);
1998 setBottomLine(i, false);
1999 setLeftLine(i, false);
2000 setRightLine(i, false);
2001 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
2002 !row_info[row].endfoot && !row_info[row].endlastfoot) {
2003 setLTHead(row, true, endfirsthead, true);
2004 row_info[row].endfirsthead = true;
2007 unsetMultiColumn(i);
2008 // When unsetting a caption row, also all existing
2009 // captions in this row must be dissolved.
2011 row_info[row].caption = what;
2016 bool Tabular::ltCaption(row_type row) const
2018 return row_info[row].caption;
2022 bool Tabular::haveLTCaption(CaptionType captiontype) const
2024 if (!is_long_tabular)
2026 for (row_type r = 0; r < nrows(); ++r) {
2027 if (row_info[r].caption) {
2028 switch (captiontype) {
2029 case CAPTION_FIRSTHEAD:
2030 if (row_info[r].endfirsthead)
2034 if (row_info[r].endhead)
2038 if (row_info[r].endfoot)
2041 case CAPTION_LASTFOOT:
2042 if (row_info[r].endlastfoot)
2054 // end longtable support functions
2056 void Tabular::setRowAscent(row_type row, int height)
2058 if (row >= nrows() || row_info[row].ascent == height)
2060 row_info[row].ascent = height;
2064 void Tabular::setRowDescent(row_type row, int height)
2066 if (row >= nrows() || row_info[row].descent == height)
2068 row_info[row].descent = height;
2072 int Tabular::rowAscent(row_type row) const
2074 LASSERT(row < nrows(), /**/);
2075 return row_info[row].ascent;
2079 int Tabular::rowDescent(row_type row) const
2081 LASSERT(row < nrows(), /**/);
2082 return row_info[row].descent;
2086 int Tabular::height() const
2089 for (row_type row = 0; row < nrows(); ++row)
2090 height += rowAscent(row) + rowDescent(row) +
2096 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2098 LASSERT(row < nrows(), /**/);
2099 LASSERT(column < ncols(), /**/);
2100 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2104 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2106 LASSERT(row < nrows(), /**/);
2107 LASSERT(column < ncols(), /**/);
2108 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2112 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2114 // we only output complete row lines and the 1st row here, the rest
2115 // is done in Tabular::TeXBottomHLine(...)
2117 // get for each column the topline (if any)
2118 vector<bool> topline;
2120 for (col_type c = 0; c < ncols(); ++c) {
2121 topline.push_back(topLine(cellIndex(row, c)));
2122 // If cell is part of a multirow and not the first cell of the
2123 // multirow, no line must be drawn.
2125 if (isMultiRow(cellIndex(row, c))
2126 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2132 // do nothing if empty first row, or incomplete row line after
2133 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2136 // only output complete row lines and the 1st row's clines
2137 if (nset == ncols()) {
2139 os << (row == 0 ? "\\toprule " : "\\midrule ");
2143 } else if (row == 0) {
2144 for (col_type c = 0; c < ncols(); ++c) {
2146 col_type offset = 0;
2147 for (col_type j = 0 ; j < c; ++j)
2148 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2151 //babel makes the "-" character an active one, so we have to suppress this here
2152 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2153 if (lang == "slovak" || lang == "czech")
2154 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2155 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2157 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2159 col_type cstart = c;
2160 for ( ; c < ncols() && topline[c]; ++c) {}
2162 for (col_type j = cstart ; j < c ; ++j)
2163 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2166 os << c + offset << "} ";
2174 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2176 // we output bottomlines of row r and the toplines of row r+1
2177 // if the latter do not span the whole tabular
2179 // get the bottomlines of row r, and toplines in next row
2180 bool lastrow = row == nrows() - 1;
2181 vector<bool> bottomline, topline;
2182 bool nextrowset = true;
2183 for (col_type c = 0; c < ncols(); ++c) {
2184 bottomline.push_back(bottomLine(cellIndex(row, c)));
2185 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2186 // If cell is part of a multirow and not the last cell of the
2187 // multirow, no line must be drawn.
2189 if (isMultiRow(cellIndex(row, c))
2190 && isMultiRow(cellIndex(row + 1, c))
2191 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2192 bottomline[c] = false;
2195 nextrowset &= topline[c];
2198 // combine this row's bottom lines and next row's toplines if necessary
2200 for (col_type c = 0; c < ncols(); ++c) {
2202 bottomline[c] = bottomline[c] || topline[c];
2207 // do nothing if empty, OR incomplete row line with a topline in next row
2208 if (nset == 0 || (nextrowset && nset != ncols()))
2211 if (nset == ncols()) {
2213 os << (lastrow ? "\\bottomrule" : "\\midrule");
2217 for (col_type c = 0; c < ncols(); ++c) {
2218 if (bottomline[c]) {
2219 col_type offset = 0;
2220 for (col_type j = 0 ; j < c; ++j)
2221 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2224 //babel makes the "-" character an active one, so we have to suppress this here
2225 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2226 if (lang == "slovak" || lang == "czech")
2227 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2228 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2230 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2232 col_type cstart = c;
2233 for ( ; c < ncols() && bottomline[c]; ++c) {}
2235 for (col_type j = cstart ; j < c ; ++j)
2236 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2239 os << c + offset << "} ";
2247 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2248 bool & ismulticol, bool & ismultirow) const
2250 row_type const r = cellRow(cell);
2251 if (is_long_tabular && row_info[r].caption)
2254 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2255 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2256 // figure out how to set the lines
2257 // we always set double lines to the right of the cell
2258 col_type const c = cellColumn(cell);
2259 col_type const nextcol = c + columnSpan(cell);
2260 bool colright = columnRightLine(c);
2261 bool colleft = columnLeftLine(c);
2262 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2263 bool nextcellleft = nextcol < ncols()
2264 && leftLine(cellIndex(r, nextcol));
2265 bool coldouble = colright && nextcolleft;
2266 bool celldouble = rightLine(cell) && nextcellleft;
2268 ismulticol = isMultiColumn(cell)
2269 || (c == 0 && colleft != leftLine(cell))
2270 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2271 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2272 || (coldouble != celldouble);
2274 // we center in multicol when no decimal point
2275 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2276 docstring const align_d = column_info[c].decimal_point;
2277 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2281 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2282 int latexcolspan = columnSpan(cell);
2283 for(col_type col = c; col < c + columnSpan(cell); ++col)
2284 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2288 os << "\\multicolumn{" << latexcolspan << "}{";
2289 if (c ==0 && leftLine(cell))
2291 if (!cellInfo(cell).align_special.empty()) {
2292 os << cellInfo(cell).align_special;
2294 if (!getPWidth(cell).zero()) {
2296 case LYX_ALIGN_LEFT:
2297 os << ">{\\raggedright}";
2299 case LYX_ALIGN_RIGHT:
2300 os << ">{\\raggedleft}";
2302 case LYX_ALIGN_CENTER:
2303 os << ">{\\centering}";
2309 case LYX_VALIGN_TOP:
2312 case LYX_VALIGN_MIDDLE:
2315 case LYX_VALIGN_BOTTOM:
2320 << from_ascii(getPWidth(cell).asLatexString())
2324 case LYX_ALIGN_LEFT:
2327 case LYX_ALIGN_RIGHT:
2334 } // end if else !getPWidth
2335 } // end if else !cellinfo_of_cell
2336 if (rightLine(cell) || nextcellleft)
2339 // add extra vertical line if we want a double one
2342 } // end if ismulticol
2344 // we only need code for the first multirow cell
2345 ismultirow = isMultiRow(cell);
2347 os << "\\multirow{" << rowSpan(cell) << "}{";
2348 if (!getPWidth(cell).zero())
2349 os << from_ascii(getPWidth(cell).asLatexString());
2351 // we need to set a default value
2354 if (!getMROffset(cell).zero())
2355 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2357 } // end if ismultirow
2359 if (getRotateCell(cell)) {
2360 os << "\\begin{sideways}\n";
2362 if (getUsebox(cell) == BOX_PARBOX) {
2365 case LYX_VALIGN_TOP:
2368 case LYX_VALIGN_MIDDLE:
2371 case LYX_VALIGN_BOTTOM:
2375 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2377 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2378 os << "\\begin{minipage}[";
2380 case LYX_VALIGN_TOP:
2383 case LYX_VALIGN_MIDDLE:
2386 case LYX_VALIGN_BOTTOM:
2390 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2396 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2397 bool ismulticol, bool ismultirow) const
2399 row_type const r = cellRow(cell);
2400 if (is_long_tabular && row_info[r].caption)
2404 if (getUsebox(cell) == BOX_PARBOX)
2406 else if (getUsebox(cell) == BOX_MINIPAGE)
2407 os << breakln << "\\end{minipage}";
2408 if (getRotateCell(cell))
2409 os << breakln << "\\end{sideways}";
2417 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2418 OutputParams const & runparams) const
2420 if (!is_long_tabular)
2423 // output first header info
2424 if (haveLTFirstHead()) {
2425 if (endfirsthead.topDL)
2427 for (row_type r = 0; r < nrows(); ++r) {
2428 if (row_info[r].endfirsthead)
2429 TeXRow(os, r, runparams);
2431 if (endfirsthead.bottomDL)
2433 os << "\\endfirsthead\n";
2435 // output header info
2437 if (endfirsthead.empty && !haveLTFirstHead())
2438 os << "\\endfirsthead\n";
2441 for (row_type r = 0; r < nrows(); ++r) {
2442 if (row_info[r].endhead)
2443 TeXRow(os, r, runparams);
2445 if (endhead.bottomDL)
2447 os << "\\endhead\n";
2449 // output footer info
2453 for (row_type r = 0; r < nrows(); ++r) {
2454 if (row_info[r].endfoot)
2455 TeXRow(os, r, runparams);
2457 if (endfoot.bottomDL)
2459 os << "\\endfoot\n";
2460 if (endlastfoot.empty && !haveLTLastFoot())
2461 os << "\\endlastfoot\n";
2463 // output lastfooter info
2464 if (haveLTLastFoot()) {
2465 if (endlastfoot.topDL)
2467 for (row_type r = 0; r < nrows(); ++r) {
2468 if (row_info[r].endlastfoot)
2469 TeXRow(os, r, runparams);
2471 if (endlastfoot.bottomDL)
2473 os << "\\endlastfoot\n";
2478 bool Tabular::isValidRow(row_type row) const
2480 if (!is_long_tabular)
2482 return !row_info[row].endhead && !row_info[row].endfirsthead
2483 && !row_info[row].endfoot && !row_info[row].endlastfoot
2484 && !row_info[row].caption;
2488 void Tabular::TeXRow(otexstream & os, row_type row,
2489 OutputParams const & runparams) const
2491 idx_type cell = cellIndex(row, 0);
2492 shared_ptr<InsetTableCell> inset = cellInset(cell);
2493 Paragraph const & par = inset->paragraphs().front();
2494 string const lang = par.getParLanguage(buffer().params())->lang();
2496 //output the top line
2497 TeXTopHLine(os, row, lang);
2499 if (row_info[row].top_space_default) {
2501 os << "\\addlinespace\n";
2503 os << "\\noalign{\\vskip\\doublerulesep}\n";
2504 } else if(!row_info[row].top_space.zero()) {
2506 os << "\\addlinespace["
2507 << from_ascii(row_info[row].top_space.asLatexString())
2510 os << "\\noalign{\\vskip"
2511 << from_ascii(row_info[row].top_space.asLatexString())
2515 bool ismulticol = false;
2516 bool ismultirow = false;
2517 for (col_type c = 0; c < ncols(); ++c) {
2518 if (isPartOfMultiColumn(row, c))
2521 cell = cellIndex(row, c);
2523 if (isPartOfMultiRow(row, c)
2524 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2525 if (cell != getLastCellInRow(row))
2530 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2531 shared_ptr<InsetTableCell> inset = cellInset(cell);
2533 Paragraph const & par = inset->paragraphs().front();
2534 bool rtl = par.isRTL(buffer().params())
2536 && getPWidth(cell).zero();
2540 par.getParLanguage(buffer().params())->lang();
2541 if (lang == "farsi")
2543 else if (lang == "arabic_arabi")
2545 // currently, remaining RTL languages are
2546 // arabic_arabtex and hebrew
2550 // pass to the OutputParams that we are in a cell and
2551 // which alignment we have set.
2552 // InsetNewline needs this context information.
2553 OutputParams newrp(runparams);
2554 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2555 ? OutputParams::PLAIN
2556 : OutputParams::ALIGNED;
2558 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2559 // copy cell and split in 2
2560 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2561 head.setBuffer(buffer());
2562 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2564 dit.push_back(CursorSlice(head));
2565 head.setMacrocontextPositionRecursive(dit);
2566 bool hassep = false;
2567 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2568 head.latex(os, newrp);
2571 tail.setBuffer(head.buffer());
2573 dit.push_back(CursorSlice(tail));
2574 tail.setMacrocontextPositionRecursive(dit);
2575 tail.latex(os, newrp);
2577 } else if (!isPartOfMultiRow(row, c)) {
2578 if (!runparams.nice)
2579 os.texrow().start(par.id(), 0);
2580 inset->latex(os, newrp);
2583 runparams.encoding = newrp.encoding;
2587 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2588 if (cell != getLastCellInRow(row)) { // not last cell in row
2595 os << "\\tabularnewline";
2596 if (row_info[row].bottom_space_default) {
2598 os << "\\addlinespace";
2600 os << "[\\doublerulesep]";
2601 } else if (!row_info[row].bottom_space.zero()) {
2603 os << "\\addlinespace";
2605 << from_ascii(row_info[row].bottom_space.asLatexString())
2610 //output the bottom line
2611 TeXBottomHLine(os, row, lang);
2613 if (row_info[row].interline_space_default) {
2615 os << "\\addlinespace\n";
2617 os << "\\noalign{\\vskip\\doublerulesep}\n";
2618 } else if (!row_info[row].interline_space.zero()) {
2620 os << "\\addlinespace["
2621 << from_ascii(row_info[row].interline_space.asLatexString())
2624 os << "\\noalign{\\vskip"
2625 << from_ascii(row_info[row].interline_space.asLatexString())
2631 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2633 bool const is_tabular_star = !tabular_width.zero();
2635 //+---------------------------------------------------------------------
2636 //+ first the opening preamble +
2637 //+---------------------------------------------------------------------
2640 if (runparams.lastid != -1)
2641 os.texrow().start(runparams.lastid, runparams.lastpos);
2644 os << "\\begin{sideways}\n";
2646 if (is_long_tabular) {
2647 os << "\\begin{longtable}";
2648 switch (longtabular_alignment) {
2649 case LYX_LONGTABULAR_ALIGN_LEFT:
2652 case LYX_LONGTABULAR_ALIGN_CENTER:
2654 case LYX_LONGTABULAR_ALIGN_RIGHT:
2659 if (is_tabular_star)
2660 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2662 os << "\\begin{tabular}";
2663 switch (tabular_valignment) {
2664 case LYX_VALIGN_TOP:
2667 case LYX_VALIGN_MIDDLE:
2669 case LYX_VALIGN_BOTTOM:
2677 if (is_tabular_star)
2678 os << "@{\\extracolsep{\\fill}}";
2680 for (col_type c = 0; c < ncols(); ++c) {
2681 if (columnLeftLine(c))
2683 if (!column_info[c].align_special.empty()) {
2684 os << column_info[c].align_special;
2686 if (!column_info[c].p_width.zero()) {
2687 switch (column_info[c].alignment) {
2688 case LYX_ALIGN_LEFT:
2689 os << ">{\\raggedright}";
2691 case LYX_ALIGN_RIGHT:
2692 os << ">{\\raggedleft}";
2694 case LYX_ALIGN_CENTER:
2695 os << ">{\\centering}";
2697 case LYX_ALIGN_NONE:
2698 case LYX_ALIGN_BLOCK:
2699 case LYX_ALIGN_LAYOUT:
2700 case LYX_ALIGN_SPECIAL:
2701 case LYX_ALIGN_DECIMAL:
2705 switch (column_info[c].valignment) {
2706 case LYX_VALIGN_TOP:
2709 case LYX_VALIGN_MIDDLE:
2712 case LYX_VALIGN_BOTTOM:
2717 << from_ascii(column_info[c].p_width.asLatexString())
2720 switch (column_info[c].alignment) {
2721 case LYX_ALIGN_LEFT:
2724 case LYX_ALIGN_RIGHT:
2727 case LYX_ALIGN_DECIMAL:
2728 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2734 } // end if else !column_info[i].p_width
2735 } // end if else !column_info[i].align_special
2736 if (columnRightLine(c))
2741 TeXLongtableHeaderFooter(os, runparams);
2743 //+---------------------------------------------------------------------
2744 //+ the single row and columns (cells) +
2745 //+---------------------------------------------------------------------
2747 for (row_type r = 0; r < nrows(); ++r) {
2748 if (isValidRow(r)) {
2749 TeXRow(os, r, runparams);
2750 if (is_long_tabular && row_info[r].newpage)
2751 os << "\\newpage\n";
2755 //+---------------------------------------------------------------------
2756 //+ the closing of the tabular +
2757 //+---------------------------------------------------------------------
2759 if (is_long_tabular)
2760 os << "\\end{longtable}";
2762 if (is_tabular_star)
2763 os << "\\end{tabular*}";
2765 os << "\\end{tabular}";
2769 os << breakln << "\\end{sideways}";
2773 int Tabular::docbookRow(odocstream & os, row_type row,
2774 OutputParams const & runparams) const
2777 idx_type cell = getFirstCellInRow(row);
2780 for (col_type c = 0; c < ncols(); ++c) {
2781 if (isPartOfMultiColumn(row, c))
2784 os << "<entry align=\"";
2785 switch (getAlignment(cell)) {
2786 case LYX_ALIGN_LEFT:
2789 case LYX_ALIGN_RIGHT:
2797 os << "\" valign=\"";
2798 switch (getVAlignment(cell)) {
2799 case LYX_VALIGN_TOP:
2802 case LYX_VALIGN_BOTTOM:
2805 case LYX_VALIGN_MIDDLE:
2810 if (isMultiColumn(cell)) {
2811 os << " namest=\"col" << c << "\" ";
2812 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2816 ret += cellInset(cell)->docbook(os, runparams);
2825 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2829 //+---------------------------------------------------------------------
2830 //+ first the opening preamble +
2831 //+---------------------------------------------------------------------
2833 os << "<tgroup cols=\"" << ncols()
2834 << "\" colsep=\"1\" rowsep=\"1\">\n";
2836 for (col_type c = 0; c < ncols(); ++c) {
2837 os << "<colspec colname=\"col" << c << "\" align=\"";
2838 switch (column_info[c].alignment) {
2839 case LYX_ALIGN_LEFT:
2842 case LYX_ALIGN_RIGHT:
2850 if (runparams.flavor == OutputParams::XML)
2856 //+---------------------------------------------------------------------
2857 //+ Long Tabular case +
2858 //+---------------------------------------------------------------------
2860 // output caption info
2861 // The caption flag wins over head/foot
2862 if (haveLTCaption()) {
2863 os << "<caption>\n";
2865 for (row_type r = 0; r < nrows(); ++r) {
2866 if (row_info[r].caption) {
2867 ret += docbookRow(os, r, runparams);
2870 os << "</caption>\n";
2873 // output header info
2874 if (haveLTHead(false) || haveLTFirstHead(false)) {
2877 for (row_type r = 0; r < nrows(); ++r) {
2878 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2879 !row_info[r].caption) {
2880 ret += docbookRow(os, r, runparams);
2886 // output footer info
2887 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2890 for (row_type r = 0; r < nrows(); ++r) {
2891 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2892 !row_info[r].caption) {
2893 ret += docbookRow(os, r, runparams);
2900 //+---------------------------------------------------------------------
2901 //+ the single row and columns (cells) +
2902 //+---------------------------------------------------------------------
2906 for (row_type r = 0; r < nrows(); ++r) {
2907 if (isValidRow(r)) {
2908 ret += docbookRow(os, r, runparams);
2913 //+---------------------------------------------------------------------
2914 //+ the closing of the tabular +
2915 //+---------------------------------------------------------------------
2924 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2925 OutputParams const & runparams, bool header) const
2928 string const celltag = header ? "th" : "td";
2929 idx_type cell = getFirstCellInRow(row);
2931 xs << html::StartTag("tr");
2932 for (col_type c = 0; c < ncols(); ++c) {
2933 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2938 switch (getAlignment(cell)) {
2939 case LYX_ALIGN_LEFT:
2942 case LYX_ALIGN_RIGHT:
2950 attr << " valign='";
2951 switch (getVAlignment(cell)) {
2952 case LYX_VALIGN_TOP:
2955 case LYX_VALIGN_BOTTOM:
2958 case LYX_VALIGN_MIDDLE:
2963 if (isMultiColumn(cell))
2964 attr << " colspan='" << columnSpan(cell) << "'";
2965 else if (isMultiRow(cell))
2966 attr << " rowspan='" << rowSpan(cell) << "'";
2968 xs << html::StartTag(celltag, attr.str()) << html::CR();
2969 ret += cellInset(cell)->xhtml(xs, runparams);
2970 xs << html::EndTag(celltag) << html::CR();
2973 xs << html::EndTag("tr");
2978 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2982 if (is_long_tabular) {
2983 // we'll wrap it in a div, so as to deal with alignment
2985 switch (longtabular_alignment) {
2986 case LYX_LONGTABULAR_ALIGN_LEFT:
2989 case LYX_LONGTABULAR_ALIGN_CENTER:
2992 case LYX_LONGTABULAR_ALIGN_RIGHT:
2996 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2998 // The caption flag wins over head/foot
2999 if (haveLTCaption()) {
3000 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
3002 for (row_type r = 0; r < nrows(); ++r)
3003 if (row_info[r].caption)
3004 ret += xhtmlRow(xs, r, runparams);
3005 xs << html::EndTag("div") << html::CR();
3009 xs << html::StartTag("table") << html::CR();
3011 // output header info
3012 bool const havefirsthead = haveLTFirstHead(false);
3013 // if we have a first head, then we are going to ignore the
3014 // headers for the additional pages, since there aren't any
3015 // in XHTML. this test accomplishes that.
3016 bool const havehead = !havefirsthead && haveLTHead(false);
3017 if (havehead || havefirsthead) {
3018 xs << html::StartTag("thead") << html::CR();
3019 for (row_type r = 0; r < nrows(); ++r) {
3020 if (((havefirsthead && row_info[r].endfirsthead) ||
3021 (havehead && row_info[r].endhead)) &&
3022 !row_info[r].caption) {
3023 ret += xhtmlRow(xs, r, runparams, true);
3026 xs << html::EndTag("thead") << html::CR();
3028 // output footer info
3029 bool const havelastfoot = haveLTLastFoot(false);
3031 bool const havefoot = !havelastfoot && haveLTFoot(false);
3032 if (havefoot || havelastfoot) {
3033 xs << html::StartTag("tfoot") << html::CR();
3034 for (row_type r = 0; r < nrows(); ++r) {
3035 if (((havelastfoot && row_info[r].endlastfoot) ||
3036 (havefoot && row_info[r].endfoot)) &&
3037 !row_info[r].caption) {
3038 ret += xhtmlRow(xs, r, runparams);
3041 xs << html::EndTag("tfoot") << html::CR();
3044 xs << html::StartTag("tbody") << html::CR();
3045 for (row_type r = 0; r < nrows(); ++r) {
3046 if (isValidRow(r)) {
3047 ret += xhtmlRow(xs, r, runparams);
3050 xs << html::EndTag("tbody")
3052 << html::EndTag("table")
3054 if (is_long_tabular)
3055 xs << html::EndTag("div") << html::CR();
3060 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3061 vector<unsigned int> const & clen) const
3063 idx_type const fcell = getFirstCellInRow(row);
3064 idx_type const n = numberOfCellsInRow(row) + fcell;
3067 for (idx_type i = fcell; i < n; ++i) {
3077 for (idx_type i = fcell; i < n; ++i) {
3088 col_type column = cellColumn(i);
3089 int len = clen[column];
3090 while (column < ncols() - 1
3091 && isPartOfMultiColumn(row, ++column))
3092 len += clen[column] + 4;
3093 os << docstring(len, ch);
3108 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3109 vector<unsigned int> const & clen) const
3111 idx_type const fcell = getFirstCellInRow(row);
3112 idx_type const n = numberOfCellsInRow(row) + fcell;
3115 for (idx_type i = fcell; i < n; ++i) {
3116 if (bottomLine(i)) {
3125 for (idx_type i = fcell; i < n; ++i) {
3126 if (bottomLine(i)) {
3136 col_type column = cellColumn(i);
3137 int len = clen[column];
3138 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3139 len += clen[column] + 4;
3140 os << docstring(len, ch);
3141 if (bottomLine(i)) {
3155 void Tabular::plaintextPrintCell(odocstream & os,
3156 OutputParams const & runparams,
3157 idx_type cell, row_type row, col_type column,
3158 vector<unsigned int> const & clen,
3159 bool onlydata) const
3161 odocstringstream sstr;
3162 cellInset(cell)->plaintext(sstr, runparams);
3174 unsigned int len1 = sstr.str().length();
3175 unsigned int len2 = clen[column];
3176 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3177 len2 += clen[column] + 4;
3180 switch (getAlignment(cell)) {
3182 case LYX_ALIGN_LEFT:
3185 case LYX_ALIGN_RIGHT:
3189 case LYX_ALIGN_CENTER:
3195 os << docstring(len1, ' ') << sstr.str()
3196 << docstring(len2, ' ');
3198 if (rightLine(cell))
3205 void Tabular::plaintext(odocstream & os,
3206 OutputParams const & runparams, int const depth,
3207 bool onlydata, char_type delim) const
3209 // first calculate the width of the single columns
3210 vector<unsigned int> clen(ncols());
3213 // first all non multicolumn cells!
3214 for (col_type c = 0; c < ncols(); ++c) {
3216 for (row_type r = 0; r < nrows(); ++r) {
3217 idx_type cell = cellIndex(r, c);
3218 if (isMultiColumn(cell))
3220 odocstringstream sstr;
3221 cellInset(cell)->plaintext(sstr, runparams);
3222 if (clen[c] < sstr.str().length())
3223 clen[c] = sstr.str().length();
3226 // then all multicolumn cells!
3227 for (col_type c = 0; c < ncols(); ++c) {
3228 for (row_type r = 0; r < nrows(); ++r) {
3229 idx_type cell = cellIndex(r, c);
3230 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3232 odocstringstream sstr;
3233 cellInset(cell)->plaintext(sstr, runparams);
3234 int len = int(sstr.str().length());
3235 idx_type const n = columnSpan(cell);
3236 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3238 if (len > int(clen[c + n - 1]))
3239 clen[c + n - 1] = len;
3244 for (row_type r = 0; r < nrows(); ++r) {
3245 if (!onlydata && plaintextTopHLine(os, r, clen))
3246 os << docstring(depth * 2, ' ');
3247 for (col_type c = 0; c < ncols(); ++c) {
3248 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3250 if (onlydata && c > 0)
3251 // we don't use operator<< for single UCS4 character.
3252 // see explanation in docstream.h
3254 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3259 os << docstring(depth * 2, ' ');
3260 if (plaintextBottomHLine(os, r, clen))
3261 os << docstring(depth * 2, ' ');
3267 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3269 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3273 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3274 col_type column) const
3276 return cell_info[row][column].inset;
3280 void Tabular::setCellInset(row_type row, col_type column,
3281 shared_ptr<InsetTableCell> ins) const
3283 CellData & cd = cell_info[row][column];
3288 void Tabular::validate(LaTeXFeatures & features) const
3290 features.require("NeedTabularnewline");
3292 features.require("booktabs");
3293 if (is_long_tabular)
3294 features.require("longtable");
3296 features.require("rotating");
3297 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3298 if (isMultiRow(cell))
3299 features.require("multirow");
3300 if (getVAlignment(cell) != LYX_VALIGN_TOP
3301 || !getPWidth(cell).zero())
3302 features.require("array");
3303 cellInset(cell)->validate(features);
3308 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3310 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3311 ParagraphList::const_iterator cit = parlist.begin();
3312 ParagraphList::const_iterator end = parlist.end();
3314 for (; cit != end; ++cit)
3315 for (int i = 0; i < cit->size(); ++i)
3316 if (cit->isNewline(i))
3323 /////////////////////////////////////////////////////////////////////
3327 /////////////////////////////////////////////////////////////////////
3329 InsetTableCell::InsetTableCell(Buffer * buf)
3330 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3331 contentAlign(LYX_ALIGN_CENTER)
3335 bool InsetTableCell::forcePlainLayout(idx_type) const
3337 return !isFixedWidth;
3341 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3343 return isFixedWidth;
3347 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3348 FuncStatus & status) const
3350 bool enabled = true;
3351 switch (cmd.action()) {
3353 enabled = !forcePlainLayout();
3355 case LFUN_LAYOUT_PARAGRAPH:
3356 enabled = allowParagraphCustomization();
3359 case LFUN_MATH_DISPLAY:
3360 if (!hasFixedWidth()) {
3365 return InsetText::getStatus(cur, cmd, status);
3367 status.setEnabled(enabled);
3371 docstring InsetTableCell::asString(bool intoInsets)
3374 if (paragraphs().empty())
3376 ParagraphList::const_iterator it = paragraphs().begin();
3377 ParagraphList::const_iterator en = paragraphs().end();
3379 for (; it != en; ++it) {
3384 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3390 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3393 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3394 return InsetText::xhtml(xs, rp);
3399 /////////////////////////////////////////////////////////////////////
3403 /////////////////////////////////////////////////////////////////////
3405 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3407 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3408 rowselect_(false), colselect_(false)
3413 InsetTabular::InsetTabular(InsetTabular const & tab)
3414 : Inset(tab), tabular(tab.tabular), scx_(0)
3419 InsetTabular::~InsetTabular()
3421 hideDialogs("tabular", this);
3425 void InsetTabular::setBuffer(Buffer & buf)
3427 tabular.setBuffer(buf);
3428 Inset::setBuffer(buf);
3432 bool InsetTabular::insetAllowed(InsetCode code) const
3437 case MATHMACRO_CODE:
3442 return tabular.is_long_tabular;
3450 void InsetTabular::write(ostream & os) const
3452 os << "Tabular" << endl;
3457 string InsetTabular::contextMenu(BufferView const &, int, int) const
3459 // FIXME: depending on the selection state,
3460 // we could offer a different menu.
3461 return cell(0)->contextMenuName() + ";" + contextMenuName();
3465 string InsetTabular::contextMenuName() const
3467 return "context-tabular";
3471 void InsetTabular::read(Lexer & lex)
3473 //bool const old_format = (lex.getString() == "\\LyXTable");
3481 string token = lex.getString();
3482 while (lex && token != "\\end_inset") {
3484 token = lex.getString();
3487 lex.printError("Missing \\end_inset at this point. ");
3491 int InsetTabular::rowFromY(Cursor & cur, int y) const
3493 // top y coordinate of tabular
3494 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3496 for (; r < tabular.nrows() && y > h; ++r)
3497 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3498 + tabular.interRowSpace(r);
3504 int InsetTabular::columnFromX(Cursor & cur, int x) const
3506 // left x coordinate of tabular
3507 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3509 for (; c < tabular.ncols() && x > w; ++c)
3510 w += tabular.cellWidth(c);
3515 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3517 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3518 // mi.base.textwidth << "\n";
3521 LASSERT(false, /**/);
3524 for (row_type r = 0; r < tabular.nrows(); ++r) {
3527 for (col_type c = 0; c < tabular.ncols(); ++c) {
3528 if (tabular.isPartOfMultiColumn(r, c)
3529 || tabular.isPartOfMultiRow(r, c))
3530 // multicolumn or multirow cell, but not first one
3532 idx_type const cell = tabular.cellIndex(r, c);
3535 Length const p_width = tabular.getPWidth(cell);
3536 if (!p_width.zero())
3537 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3538 tabular.cellInset(cell)->metrics(m, dim);
3539 if (!p_width.zero())
3540 dim.wid = m.base.textwidth;
3541 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3542 + tabular.interColumnSpace(cell);
3544 // FIXME(?): do we need a second metrics call?
3545 TextMetrics const & tm =
3546 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3548 // determine horizontal offset because of decimal align (if necessary)
3549 int decimal_width = 0;
3550 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3551 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3552 tail.setBuffer(tabular.buffer());
3553 // we need to set macrocontext position everywhere
3554 // otherwise we crash with nested insets (e.g. footnotes)
3555 // after decimal point
3556 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3558 dit.push_back(CursorSlice(tail));
3559 tail.setMacrocontextPositionRecursive(dit);
3561 // remove text leading decimal point
3562 docstring const align_d = tabular.column_info[c].decimal_point;
3563 dit = separatorPos(&tail, align_d);
3565 pit_type const psize = tail.paragraphs().front().size();
3567 tail.paragraphs().front().eraseChars(0,
3568 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3570 tail.metrics(m, dim1);
3571 decimal_width = dim1.width();
3574 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3575 tabular.cell_info[r][c].decimal_width = decimal_width;
3577 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3578 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3579 int const lastpardes = tm.last().second->descent()
3580 + TEXT_TO_INSET_OFFSET;
3582 switch (tabular.getVAlignment(cell)) {
3583 case Tabular::LYX_VALIGN_TOP:
3585 case Tabular::LYX_VALIGN_MIDDLE:
3586 offset = -(dim.des - lastpardes)/2;
3588 case Tabular::LYX_VALIGN_BOTTOM:
3589 offset = -(dim.des - lastpardes);
3592 tabular.cell_info[r][c].voffset = offset;
3593 maxasc = max(maxasc, dim.asc - offset);
3594 maxdes = max(maxdes, dim.des + offset);
3596 int const top_space = tabular.row_info[r].top_space_default ?
3597 default_line_space :
3598 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3599 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3600 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3601 default_line_space :
3602 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3603 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3606 // for top-alignment the first horizontal table line must be exactly at
3607 // the position of the base line of the surrounding text line
3608 // for bottom alignment, the same is for the last table line
3609 switch (tabular.tabular_valignment) {
3610 case Tabular::LYX_VALIGN_BOTTOM:
3611 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3613 case Tabular::LYX_VALIGN_MIDDLE:
3614 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3616 case Tabular::LYX_VALIGN_TOP:
3617 offset_valign_ = tabular.rowAscent(0);
3621 tabular.updateColumnWidths();
3622 dim.asc = tabular.rowAscent(0) - offset_valign_;
3623 dim.des = tabular.height() - dim.asc;
3624 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3628 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3631 if (&cur.inset() == this && cur.selection()) {
3632 if (cur.selIsMultiCell()) {
3635 getSelection(cur, rs, re, cs, ce);
3637 idx_type const cell = tabular.cellIndex(row, col);
3638 col_type const cspan = tabular.columnSpan(cell);
3639 row_type const rspan = tabular.rowSpan(cell);
3640 if (col + cspan - 1 >= cs && col <= ce
3641 && row + rspan - 1 >= rs && row <= re)
3644 if (col == tabular.cellColumn(cur.idx())
3645 && row == tabular.cellRow(cur.idx())) {
3646 CursorSlice const & beg = cur.selBegin();
3647 CursorSlice const & end = cur.selEnd();
3649 if ((end.lastpos() > 0 || end.lastpit() > 0)
3650 && end.pos() == end.lastpos() && beg.pos() == 0
3651 && end.pit() == end.lastpit() && beg.pit() == 0)
3659 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3661 x += scx_ + ADD_TO_TABULAR_WIDTH;
3663 BufferView * bv = pi.base.bv;
3664 Cursor & cur = pi.base.bv->cursor();
3667 // FIXME: As the full background is painted in drawBackground(),
3668 // we have no choice but to do a full repaint for the Text cells.
3669 pi.full_repaint = true;
3671 bool const original_selection_state = pi.selected;
3674 first_visible_cell = Tabular::npos;
3676 int yy = y + offset_valign_;
3677 for (row_type r = 0; r < tabular.nrows(); ++r) {
3679 for (col_type c = 0; c < tabular.ncols(); ++c) {
3680 if (tabular.isPartOfMultiColumn(r, c))
3683 idx = tabular.cellIndex(r, c);
3685 if (tabular.isPartOfMultiRow(r, c)) {
3686 nx += tabular.cellWidth(idx);
3690 if (first_visible_cell == Tabular::npos)
3691 first_visible_cell = idx;
3693 pi.selected |= isCellSelected(cur, r, c);
3694 int const cx = nx + tabular.textHOffset(idx);
3695 int const cy = yy + tabular.textVOffset(idx);
3696 // Cache the Inset position.
3697 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3698 cell(idx)->draw(pi, cx, cy);
3699 drawCellLines(pi, nx, yy, r, idx);
3700 nx += tabular.cellWidth(idx);
3701 pi.selected = original_selection_state;
3704 if (r + 1 < tabular.nrows())
3705 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3706 + tabular.interRowSpace(r + 1);
3711 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3713 x += scx_ + ADD_TO_TABULAR_WIDTH;
3714 y += offset_valign_ - tabular.rowAscent(0);
3715 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3716 pi.backgroundColor(this));
3720 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3722 Cursor & cur = pi.base.bv->cursor();
3725 x += scx_ + ADD_TO_TABULAR_WIDTH;
3727 if (!cur.selection())
3729 if (&cur.inset() != this)
3734 bool const full_cell_selected = isCellSelected(cur,
3735 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3737 if (cur.selIsMultiCell() || full_cell_selected) {
3738 for (row_type r = 0; r < tabular.nrows(); ++r) {
3740 for (col_type c = 0; c < tabular.ncols(); ++c) {
3741 if (tabular.isPartOfMultiColumn(r, c))
3744 idx_type const cell = tabular.cellIndex(r, c);
3746 if (tabular.isPartOfMultiRow(r, c)) {
3747 xx += tabular.cellWidth(cell);
3750 int const w = tabular.cellWidth(cell);
3751 int const h = tabular.cellHeight(cell);
3752 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3753 if (isCellSelected(cur, r, c))
3754 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3757 if (r + 1 < tabular.nrows())
3758 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3759 + tabular.interRowSpace(r + 1);
3763 // FIXME: This code has no effect because InsetTableCell does not handle
3764 // drawSelection other than the trivial implementation in Inset.
3766 // x += cellXPos(cur.idx());
3767 // x += tabular.textHOffset(cur.idx());
3768 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3773 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3774 row_type row, idx_type cell) const
3776 y -= tabular.rowAscent(row);
3777 int const w = tabular.cellWidth(cell);
3778 int const h = tabular.cellHeight(cell);
3779 Color const linecolor = pi.textColor(Color_tabularline);
3780 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3783 bool drawline = tabular.topLine(cell)
3784 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3785 pi.pain.line(x, y, x + w, y,
3786 drawline ? linecolor : gridcolor,
3787 drawline ? Painter::line_solid : Painter::line_onoffdash);
3790 drawline = tabular.bottomLine(cell);
3791 pi.pain.line(x, y + h, x + w, y + h,
3792 drawline ? linecolor : gridcolor,
3793 drawline ? Painter::line_solid : Painter::line_onoffdash);
3796 col_type const col = tabular.cellColumn(cell);
3797 drawline = tabular.leftLine(cell)
3798 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3799 pi.pain.line(x, y, x, y + h,
3800 drawline ? linecolor : gridcolor,
3801 drawline ? Painter::line_solid : Painter::line_onoffdash);
3804 x -= tabular.interColumnSpace(cell);
3805 drawline = tabular.rightLine(cell)
3806 || (col + 1 < tabular.ncols()
3807 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3808 pi.pain.line(x + w, y, x + w, y + h,
3809 drawline ? linecolor : gridcolor,
3810 drawline ? Painter::line_solid : Painter::line_onoffdash);
3814 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3816 //lyxerr << "InsetTabular::edit: " << this << endl;
3818 cur.setSelection(false);
3821 if (isRightToLeft(cur))
3822 cur.idx() = tabular.getLastCellInRow(0);
3828 if (isRightToLeft(cur))
3829 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3831 cur.idx() = tabular.numberofcells - 1;
3833 cur.pos() = cur.lastpos(); // FIXME crude guess
3835 cur.setCurrentFont();
3836 // FIXME: this accesses the position cache before it is initialized
3838 //cur.bv().fitCursor();
3842 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3844 // In a longtable, tell captions what the current float is
3845 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3846 string const saveflt = cnts.current_float();
3847 if (tabular.is_long_tabular)
3848 cnts.current_float("table");
3850 ParIterator it2 = it;
3852 size_t const end = it2.nargs();
3853 for ( ; it2.idx() < end; it2.top().forwardIdx())
3854 buffer().updateBuffer(it2, utype);
3857 if (tabular.is_long_tabular)
3858 cnts.current_float(saveflt);
3862 void InsetTabular::addToToc(DocIterator const & cpit) const
3864 DocIterator dit = cpit;
3866 size_t const end = dit.nargs();
3867 for ( ; dit.idx() < end; dit.top().forwardIdx())
3868 cell(dit.idx())->addToToc(dit);
3872 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3874 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3875 << "\n cur:" << cur);
3876 CursorSlice sl = cur.top();
3877 Cursor & bvcur = cur.bv().cursor();
3879 FuncCode const act = cmd.action();
3883 case LFUN_MOUSE_PRESS: {
3884 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3886 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3887 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3888 row_type r = rowFromY(cur, cmd.y());
3889 cur.idx() = tabular.getFirstCellInRow(r);
3892 cur.idx() = tabular.getLastCellInRow(r);
3893 cur.pos() = cur.lastpos();
3894 cur.setSelection(true);
3900 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3901 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3902 || cmd.y() > y0 + tabular.height()) {
3903 col_type c = columnFromX(cur, cmd.x());
3904 cur.idx() = tabular.cellIndex(0, c);
3907 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3908 cur.pos() = cur.lastpos();
3909 cur.setSelection(true);
3914 // do not reset cursor/selection if we have selected
3915 // some cells (bug 2715).
3916 if (cmd.button() == mouse_button::button3
3917 && &bvcur.selBegin().inset() == this
3918 && bvcur.selIsMultiCell())
3921 // Let InsetTableCell do it
3922 cell(cur.idx())->dispatch(cur, cmd);
3925 case LFUN_MOUSE_MOTION:
3926 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3927 if (cmd.button() == mouse_button::button1) {
3928 // only accept motions to places not deeper nested than the real anchor
3929 if (!bvcur.realAnchor().hasPart(cur)) {
3933 // select (additional) row
3935 row_type r = rowFromY(cur, cmd.y());
3936 cur.idx() = tabular.getLastCellInRow(r);
3937 // we need to reset the cursor's pit and pos now, as the old ones
3938 // may no longer be valid.
3941 bvcur.setCursor(cur);
3942 bvcur.setSelection(true);
3945 // select (additional) column
3947 col_type c = columnFromX(cur, cmd.x());
3948 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3949 // we need to reset the cursor's pit and pos now, as the old ones
3950 // may no longer be valid.
3953 bvcur.setCursor(cur);
3954 bvcur.setSelection(true);
3957 // only update if selection changes
3958 if (bvcur.idx() == cur.idx() &&
3959 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3960 cur.noScreenUpdate();
3961 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3962 bvcur.setCursor(cur);
3963 bvcur.setSelection(true);
3964 // if this is a multicell selection, we just set the cursor to
3965 // the beginning of the cell's text.
3966 if (bvcur.selIsMultiCell()) {
3967 bvcur.pit() = bvcur.lastpit();
3968 bvcur.pos() = bvcur.lastpos();
3973 case LFUN_MOUSE_RELEASE:
3978 case LFUN_CELL_BACKWARD:
3980 cur.setSelection(false);
3983 case LFUN_CELL_FORWARD:
3985 cur.setSelection(false);
3988 case LFUN_CHAR_FORWARD_SELECT:
3989 case LFUN_CHAR_FORWARD:
3990 case LFUN_CHAR_BACKWARD_SELECT:
3991 case LFUN_CHAR_BACKWARD:
3992 case LFUN_CHAR_RIGHT_SELECT:
3993 case LFUN_CHAR_RIGHT:
3994 case LFUN_CHAR_LEFT_SELECT:
3995 case LFUN_CHAR_LEFT:
3996 case LFUN_WORD_FORWARD:
3997 case LFUN_WORD_FORWARD_SELECT:
3998 case LFUN_WORD_BACKWARD:
3999 case LFUN_WORD_BACKWARD_SELECT:
4000 case LFUN_WORD_RIGHT:
4001 case LFUN_WORD_RIGHT_SELECT:
4002 case LFUN_WORD_LEFT:
4003 case LFUN_WORD_LEFT_SELECT: {
4004 // determine whether we move to next or previous cell, where to enter
4005 // the new cell from, and which command to "finish" (i.e., exit the
4008 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
4009 FuncCode finish_lfun;
4011 if (act == LFUN_CHAR_FORWARD
4012 || act == LFUN_CHAR_FORWARD_SELECT
4013 || act == LFUN_WORD_FORWARD
4014 || act == LFUN_WORD_FORWARD_SELECT) {
4016 finish_lfun = LFUN_FINISHED_FORWARD;
4018 else if (act == LFUN_CHAR_BACKWARD
4019 || act == LFUN_CHAR_BACKWARD_SELECT
4020 || act == LFUN_WORD_BACKWARD
4021 || act == LFUN_WORD_BACKWARD_SELECT) {
4023 finish_lfun = LFUN_FINISHED_BACKWARD;
4025 // LEFT or RIGHT commands --- the interpretation will depend on the
4026 // table's direction.
4028 bool const right = act == LFUN_CHAR_RIGHT
4029 || act == LFUN_CHAR_RIGHT_SELECT
4030 || act == LFUN_WORD_RIGHT
4031 || act == LFUN_WORD_RIGHT_SELECT;
4032 next_cell = isRightToLeft(cur) != right;
4034 if (lyxrc.visual_cursor)
4035 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4037 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4040 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4041 || act == LFUN_CHAR_BACKWARD_SELECT
4042 || act == LFUN_CHAR_RIGHT_SELECT
4043 || act == LFUN_CHAR_LEFT_SELECT
4044 || act == LFUN_WORD_FORWARD_SELECT
4045 || act == LFUN_WORD_RIGHT_SELECT
4046 || act == LFUN_WORD_BACKWARD_SELECT
4047 || act == LFUN_WORD_LEFT_SELECT;
4049 // If we have a multicell selection or we're
4050 // not doing some LFUN_*_SELECT thing anyway...
4051 if (!cur.selIsMultiCell() || !select) {
4052 col_type const c = tabular.cellColumn(cur.idx());
4053 row_type const r = tabular.cellRow(cur.idx());
4054 // Are we trying to select the whole cell and is the whole cell
4055 // not yet selected?
4056 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4057 ((next_cell && cur.pit() == cur.lastpit()
4058 && cur.pos() == cur.lastpos())
4059 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4061 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4063 // ...try to dispatch to the cell's inset.
4064 cell(cur.idx())->dispatch(cur, cmd);
4066 // When we already have a selection we want to select the whole cell
4067 // before going to the next cell.
4068 if (select_whole && !empty_cell){
4069 getText(cur.idx())->selectAll(cur);
4074 // FIXME: When we support the selection of an empty cell, remove
4075 // the !empty_cell from this condition. For now we jump to the next
4076 // cell if the current cell is empty.
4077 if (cur.result().dispatched() && !empty_cell)
4081 // move to next/prev cell, as appropriate
4082 // note that we will always do this if we're selecting and we have
4083 // a multicell selection
4084 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4085 << " cell from: " << int(entry_from));
4087 moveNextCell(cur, entry_from);
4089 movePrevCell(cur, entry_from);
4090 // if we're exiting the table, call the appropriate FINISHED lfun
4091 if (sl == cur.top())
4092 cmd = FuncRequest(finish_lfun);
4096 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4101 case LFUN_DOWN_SELECT:
4103 if (!(cur.selection() && cur.selIsMultiCell()))
4104 cell(cur.idx())->dispatch(cur, cmd);
4106 cur.dispatched(); // override the cell's decision
4107 if (sl == cur.top()) {
4108 // if our Text didn't do anything to the cursor
4109 // then we try to put the cursor into the cell below
4110 // setting also the right targetX.
4111 cur.selHandle(act == LFUN_DOWN_SELECT);
4112 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4113 int const xtarget = cur.targetX();
4114 // WARNING: Once cur.idx() has been reset, the cursor is in
4115 // an inconsistent state until pos() has been set. Be careful
4116 // what you do with it!
4117 cur.idx() = tabular.cellBelow(cur.idx());
4119 TextMetrics const & tm =
4120 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4121 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4122 cur.setCurrentFont();
4125 if (sl == cur.top()) {
4126 // we trick it to go to forward after leaving the
4128 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4131 if (cur.selIsMultiCell()) {
4132 cur.pit() = cur.lastpit();
4133 cur.pos() = cur.lastpos();
4134 cur.setCurrentFont();
4137 cur.screenUpdateFlags(Update::FitCursor);
4140 case LFUN_UP_SELECT:
4142 if (!(cur.selection() && cur.selIsMultiCell()))
4143 cell(cur.idx())->dispatch(cur, cmd);
4144 cur.dispatched(); // override the cell's decision
4145 if (sl == cur.top()) {
4146 // if our Text didn't do anything to the cursor
4147 // then we try to put the cursor into the cell above
4148 // setting also the right targetX.
4149 cur.selHandle(act == LFUN_UP_SELECT);
4150 if (tabular.cellRow(cur.idx()) != 0) {
4151 int const xtarget = cur.targetX();
4152 // WARNING: Once cur.idx() has been reset, the cursor is in
4153 // an inconsistent state until pos() has been set. Be careful
4154 // what you do with it!
4155 cur.idx() = tabular.cellAbove(cur.idx());
4156 cur.pit() = cur.lastpit();
4157 Text const * text = cell(cur.idx())->getText(0);
4158 TextMetrics const & tm = cur.bv().textMetrics(text);
4159 ParagraphMetrics const & pm =
4160 tm.parMetrics(cur.lastpit());
4161 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4162 cur.setCurrentFont();
4165 if (sl == cur.top()) {
4166 cmd = FuncRequest(LFUN_UP);
4169 if (cur.selIsMultiCell()) {
4171 cur.pos() = cur.lastpos();
4172 cur.setCurrentFont();
4175 cur.screenUpdateFlags(Update::FitCursor);
4178 // case LFUN_SCREEN_DOWN: {
4179 // //if (hasSelection())
4180 // // cur.selection() = false;
4181 // col_type const col = tabular.cellColumn(cur.idx());
4182 // int const t = cur.bv().top_y() + cur.bv().height();
4183 // if (t < yo() + tabular.getHeightOfTabular()) {
4184 // cur.bv().scrollDocView(t, true);
4185 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4187 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4194 // case LFUN_SCREEN_UP: {
4195 // //if (hasSelection())
4196 // // cur.selection() = false;
4197 // col_type const col = tabular.cellColumn(cur.idx());
4198 // int const t = cur.bv().top_y() + cur.bv().height();
4200 // cur.bv().scrollDocView(t, true);
4204 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4208 // cur.par() = cur.lastpar();
4209 // cur.pos() = cur.lastpos();
4213 case LFUN_LAYOUT_TABULAR:
4214 cur.bv().showDialog("tabular");
4217 case LFUN_INSET_MODIFY: {
4219 if (cmd.getArg(1) == "from-dialog")
4220 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4222 arg = to_utf8(cmd.argument());
4223 if (!tabularFeatures(cur, arg))
4228 // insert file functions
4229 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4230 case LFUN_FILE_INSERT_PLAINTEXT:
4232 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4233 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4234 FileName(to_utf8(cmd.argument())));
4237 cur.recordUndoInset(INSERT_UNDO);
4238 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4239 // content has been replaced,
4240 // so cursor might be invalid
4241 cur.pos() = cur.lastpos();
4242 cur.pit() = cur.lastpit();
4243 bvcur.setCursor(cur);
4250 if (cur.selIsMultiCell()) {
4251 if (copySelection(cur)) {
4252 cur.recordUndoInset(DELETE_UNDO);
4256 cell(cur.idx())->dispatch(cur, cmd);
4259 case LFUN_SELF_INSERT:
4260 if (cur.selIsMultiCell()) {
4261 cur.recordUndoInset(DELETE_UNDO);
4264 cell(cur.idx())->dispatch(cur, cmd);
4267 case LFUN_CHAR_DELETE_BACKWARD:
4268 case LFUN_CHAR_DELETE_FORWARD:
4269 if (cur.selIsMultiCell()) {
4270 cur.recordUndoInset(DELETE_UNDO);
4273 cell(cur.idx())->dispatch(cur, cmd);
4277 if (!cur.selection())
4279 if (cur.selIsMultiCell()) {
4283 cell(cur.idx())->dispatch(cur, cmd);
4286 case LFUN_CLIPBOARD_PASTE:
4287 case LFUN_PRIMARY_SELECTION_PASTE: {
4288 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4289 theClipboard().getAsText() :
4290 theSelection().get();
4293 // pass to InsertPlaintextString, but
4294 // only if we have multi-cell content
4295 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4296 cur.recordUndoInset(INSERT_UNDO);
4297 if (insertPlaintextString(cur.bv(), clip, false)) {
4298 // content has been replaced,
4299 // so cursor might be invalid
4300 cur.pos() = cur.lastpos();
4301 cur.pit() = cur.lastpit();
4302 bvcur.setCursor(cur);
4306 // Let the cell handle normal text
4307 cell(cur.idx())->dispatch(cur, cmd);
4312 if (!tabularStackDirty()) {
4313 if (!cur.selIsMultiCell())
4314 cell(cur.idx())->dispatch(cur, cmd);
4317 if (theClipboard().isInternal()) {
4318 cur.recordUndoInset(INSERT_UNDO);
4319 pasteClipboard(cur);
4323 case LFUN_FONT_EMPH:
4324 case LFUN_FONT_BOLD:
4325 case LFUN_FONT_BOLDSYMBOL:
4326 case LFUN_FONT_ROMAN:
4327 case LFUN_FONT_NOUN:
4328 case LFUN_FONT_ITAL:
4329 case LFUN_FONT_FRAK:
4330 case LFUN_FONT_TYPEWRITER:
4331 case LFUN_FONT_SANS:
4332 case LFUN_TEXTSTYLE_APPLY:
4333 case LFUN_TEXTSTYLE_UPDATE:
4334 case LFUN_FONT_SIZE:
4335 case LFUN_FONT_UNDERLINE:
4336 case LFUN_FONT_STRIKEOUT:
4337 case LFUN_FONT_UULINE:
4338 case LFUN_FONT_UWAVE:
4340 case LFUN_WORD_CAPITALIZE:
4341 case LFUN_WORD_UPCASE:
4342 case LFUN_WORD_LOWCASE:
4343 case LFUN_CHARS_TRANSPOSE:
4344 if (cur.selIsMultiCell()) {
4347 getSelection(cur, rs, re, cs, ce);
4348 Cursor tmpcur = cur;
4349 for (row_type r = rs; r <= re; ++r) {
4350 for (col_type c = cs; c <= ce; ++c) {
4351 // cursor follows cell:
4352 tmpcur.idx() = tabular.cellIndex(r, c);
4353 // select this cell only:
4356 tmpcur.resetAnchor();
4357 tmpcur.pit() = tmpcur.lastpit();
4358 tmpcur.pos() = tmpcur.top().lastpos();
4359 tmpcur.setCursor(tmpcur);
4360 tmpcur.setSelection();
4361 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4366 cell(cur.idx())->dispatch(cur, cmd);
4370 case LFUN_INSET_SETTINGS:
4371 // relay this lfun to Inset, not to the cell.
4372 Inset::doDispatch(cur, cmd);
4376 // we try to handle this event in the insets dispatch function.
4377 cell(cur.idx())->dispatch(cur, cmd);
4383 // function sets an object as defined in func_status.h:
4384 // states OK, Unknown, Disabled, On, Off.
4385 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4386 FuncStatus & status) const
4388 switch (cmd.action()) {
4389 case LFUN_INSET_MODIFY: {
4390 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4393 // FIXME: We only check for the very first argument...
4394 string const s = cmd.getArg(1);
4395 // We always enable the lfun if it is coming from the dialog
4396 // because the dialog makes sure all the settings are valid,
4397 // even though the first argument might not be valid now.
4398 if (s == "from-dialog") {
4399 status.setEnabled(true);
4403 int action = Tabular::LAST_ACTION;
4405 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4406 if (tabularFeature[i].feature == s) {
4407 action = tabularFeature[i].action;
4411 if (action == Tabular::LAST_ACTION) {
4413 status.setUnknown(true);
4417 string const argument = cmd.getLongArg(2);
4419 row_type sel_row_start = 0;
4420 row_type sel_row_end = 0;
4421 col_type sel_col_start = 0;
4422 col_type sel_col_end = 0;
4423 Tabular::ltType dummyltt;
4426 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4429 case Tabular::SET_PWIDTH:
4430 case Tabular::SET_MPWIDTH:
4431 case Tabular::SET_SPECIAL_COLUMN:
4432 case Tabular::SET_SPECIAL_MULTICOLUMN:
4433 case Tabular::APPEND_ROW:
4434 case Tabular::APPEND_COLUMN:
4435 case Tabular::DELETE_ROW:
4436 case Tabular::DELETE_COLUMN:
4437 case Tabular::COPY_ROW:
4438 case Tabular::COPY_COLUMN:
4439 case Tabular::SET_TOP_SPACE:
4440 case Tabular::SET_BOTTOM_SPACE:
4441 case Tabular::SET_INTERLINE_SPACE:
4445 case Tabular::SET_TABULAR_WIDTH:
4446 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4447 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4450 case Tabular::SET_DECIMAL_POINT:
4452 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4455 case Tabular::SET_MULTICOLUMN:
4456 case Tabular::UNSET_MULTICOLUMN:
4457 case Tabular::MULTICOLUMN:
4458 // If a row is set as longtable caption, it must not be allowed
4459 // to unset that this row is a multicolumn.
4460 status.setEnabled(sel_row_start == sel_row_end
4461 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4462 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4465 case Tabular::SET_MULTIROW:
4466 case Tabular::UNSET_MULTIROW:
4467 case Tabular::MULTIROW:
4468 // If a row is set as longtable caption, it must not be allowed
4469 // to unset that this row is a multirow.
4470 status.setEnabled(sel_col_start == sel_col_end
4471 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4472 status.setOnOff(tabular.isMultiRow(cur.idx()));
4475 case Tabular::SET_ALL_LINES:
4476 case Tabular::UNSET_ALL_LINES:
4477 case Tabular::SET_BORDER_LINES:
4478 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4481 case Tabular::SET_LINE_TOP:
4482 case Tabular::SET_LINE_BOTTOM:
4483 case Tabular::SET_LINE_LEFT:
4484 case Tabular::SET_LINE_RIGHT:
4485 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4488 case Tabular::TOGGLE_LINE_TOP:
4489 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4490 status.setOnOff(tabular.topLine(cur.idx()));
4493 case Tabular::TOGGLE_LINE_BOTTOM:
4494 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4495 status.setOnOff(tabular.bottomLine(cur.idx()));
4498 case Tabular::TOGGLE_LINE_LEFT:
4499 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4500 status.setOnOff(tabular.leftLine(cur.idx()));
4503 case Tabular::TOGGLE_LINE_RIGHT:
4504 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4505 status.setOnOff(tabular.rightLine(cur.idx()));
4508 // multirow cells only inherit the alignment of the column if the column has
4509 // no width, otherwise they are left-aligned
4510 // therefore allow always left but right and center only if there is no width
4511 case Tabular::M_ALIGN_LEFT:
4513 case Tabular::ALIGN_LEFT:
4514 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4517 case Tabular::M_ALIGN_RIGHT:
4519 case Tabular::ALIGN_RIGHT:
4520 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4521 && !tabular.getPWidth(cur.idx()).zero()));
4522 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4525 case Tabular::M_ALIGN_CENTER:
4527 case Tabular::ALIGN_CENTER:
4528 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4529 && !tabular.getPWidth(cur.idx()).zero()));
4530 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4533 case Tabular::ALIGN_BLOCK:
4534 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4535 && !tabular.isMultiRow(cur.idx()));
4536 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4539 case Tabular::ALIGN_DECIMAL:
4540 status.setEnabled(!tabular.isMultiRow(cur.idx())
4541 && !tabular.isMultiColumn(cur.idx()));
4542 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4545 case Tabular::M_VALIGN_TOP:
4547 case Tabular::VALIGN_TOP:
4548 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4549 && !tabular.isMultiRow(cur.idx()));
4551 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4554 case Tabular::M_VALIGN_BOTTOM:
4556 case Tabular::VALIGN_BOTTOM:
4557 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4558 && !tabular.isMultiRow(cur.idx()));
4560 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4563 case Tabular::M_VALIGN_MIDDLE:
4565 case Tabular::VALIGN_MIDDLE:
4566 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4567 && !tabular.isMultiRow(cur.idx()));
4569 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4572 case Tabular::SET_LONGTABULAR:
4573 // setting as longtable is not allowed when table is inside a float
4574 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4575 || cur.innerInsetOfType(WRAP_CODE) != 0)
4576 status.setEnabled(false);
4578 status.setEnabled(true);
4579 status.setOnOff(tabular.is_long_tabular);
4582 case Tabular::UNSET_LONGTABULAR:
4583 status.setOnOff(!tabular.is_long_tabular);
4586 case Tabular::TOGGLE_ROTATE_TABULAR:
4587 case Tabular::SET_ROTATE_TABULAR:
4588 status.setEnabled(tabular.tabular_width.zero());
4589 status.setOnOff(tabular.rotate);
4592 case Tabular::TABULAR_VALIGN_TOP:
4593 status.setEnabled(tabular.tabular_width.zero());
4594 status.setOnOff(tabular.tabular_valignment
4595 == Tabular::LYX_VALIGN_TOP);
4597 case Tabular::TABULAR_VALIGN_MIDDLE:
4598 status.setEnabled(tabular.tabular_width.zero());
4599 status.setOnOff(tabular.tabular_valignment
4600 == Tabular::LYX_VALIGN_MIDDLE);
4602 case Tabular::TABULAR_VALIGN_BOTTOM:
4603 status.setEnabled(tabular.tabular_width.zero());
4604 status.setOnOff(tabular.tabular_valignment
4605 == Tabular::LYX_VALIGN_BOTTOM);
4608 case Tabular::LONGTABULAR_ALIGN_LEFT:
4609 status.setOnOff(tabular.longtabular_alignment
4610 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4612 case Tabular::LONGTABULAR_ALIGN_CENTER:
4613 status.setOnOff(tabular.longtabular_alignment
4614 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4616 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4617 status.setOnOff(tabular.longtabular_alignment
4618 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4621 case Tabular::UNSET_ROTATE_TABULAR:
4622 status.setOnOff(!tabular.rotate);
4625 case Tabular::TOGGLE_ROTATE_CELL:
4626 case Tabular::SET_ROTATE_CELL:
4627 status.setOnOff(!oneCellHasRotationState(false,
4628 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4631 case Tabular::UNSET_ROTATE_CELL:
4632 status.setOnOff(!oneCellHasRotationState(true,
4633 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4636 case Tabular::SET_USEBOX:
4637 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4640 // every row can only be one thing:
4641 // either a footer or header
4642 case Tabular::SET_LTFIRSTHEAD:
4643 status.setEnabled(sel_row_start == sel_row_end);
4644 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4647 case Tabular::UNSET_LTFIRSTHEAD:
4648 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4649 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4652 case Tabular::SET_LTHEAD:
4653 status.setEnabled(sel_row_start == sel_row_end);
4654 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4657 case Tabular::UNSET_LTHEAD:
4658 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4659 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4662 case Tabular::SET_LTFOOT:
4663 status.setEnabled(sel_row_start == sel_row_end);
4664 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4667 case Tabular::UNSET_LTFOOT:
4668 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4669 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4672 case Tabular::SET_LTLASTFOOT:
4673 status.setEnabled(sel_row_start == sel_row_end);
4674 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4677 case Tabular::UNSET_LTLASTFOOT:
4678 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4679 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4682 case Tabular::SET_LTNEWPAGE:
4683 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4686 // only one row in head/firsthead/foot/lasthead can be the caption
4687 // and a multirow cannot be set as caption
4688 case Tabular::SET_LTCAPTION:
4689 status.setEnabled(sel_row_start == sel_row_end
4690 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4691 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4692 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4693 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4694 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4695 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4696 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4697 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4698 && !tabular.isMultiRow(sel_row_start));
4699 status.setOnOff(tabular.ltCaption(sel_row_start));
4702 case Tabular::UNSET_LTCAPTION:
4703 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4706 case Tabular::TOGGLE_LTCAPTION:
4707 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4708 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4709 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4710 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4711 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4712 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4713 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4714 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4715 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4716 status.setOnOff(tabular.ltCaption(sel_row_start));
4719 case Tabular::SET_BOOKTABS:
4720 status.setOnOff(tabular.use_booktabs);
4723 case Tabular::UNSET_BOOKTABS:
4724 status.setOnOff(!tabular.use_booktabs);
4729 status.setEnabled(false);
4735 // These are only enabled inside tabular
4736 case LFUN_CELL_BACKWARD:
4737 case LFUN_CELL_FORWARD:
4738 status.setEnabled(true);
4741 // disable these with multiple cells selected
4742 case LFUN_INSET_INSERT:
4743 case LFUN_TABULAR_INSERT:
4744 case LFUN_FLEX_INSERT:
4745 case LFUN_FLOAT_INSERT:
4746 case LFUN_FLOAT_WIDE_INSERT:
4747 case LFUN_FOOTNOTE_INSERT:
4748 case LFUN_MARGINALNOTE_INSERT:
4749 case LFUN_MATH_INSERT:
4750 case LFUN_MATH_MODE:
4751 case LFUN_MATH_MUTATE:
4752 case LFUN_MATH_DISPLAY:
4753 case LFUN_NOTE_INSERT:
4754 case LFUN_ARGUMENT_INSERT:
4755 case LFUN_BOX_INSERT:
4756 case LFUN_BRANCH_INSERT:
4757 case LFUN_PHANTOM_INSERT:
4758 case LFUN_WRAP_INSERT:
4759 case LFUN_PREVIEW_INSERT:
4760 case LFUN_ERT_INSERT: {
4761 if (cur.selIsMultiCell()) {
4762 status.setEnabled(false);
4765 return cell(cur.idx())->getStatus(cur, cmd, status);
4768 // disable in non-fixed-width cells
4769 case LFUN_BREAK_PARAGRAPH:
4770 // multirow does not allow paragraph breaks
4771 if (tabular.isMultiRow(cur.idx())) {
4772 status.setEnabled(false);
4775 case LFUN_NEWLINE_INSERT: {
4776 if (tabular.getPWidth(cur.idx()).zero()) {
4777 status.setEnabled(false);
4780 return cell(cur.idx())->getStatus(cur, cmd, status);
4783 case LFUN_NEWPAGE_INSERT:
4784 status.setEnabled(false);
4788 if (tabularStackDirty() && theClipboard().isInternal()) {
4789 if (cur.selIsMultiCell()) {
4792 getSelection(cur, rs, re, cs, ce);
4793 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4794 && paste_tabular->nrows() == re - rs + 1)
4795 status.setEnabled(true);
4797 status.setEnabled(false);
4798 status.message(_("Selection size should match clipboard content."));
4801 status.setEnabled(true);
4804 return cell(cur.idx())->getStatus(cur, cmd, status);
4806 case LFUN_INSET_SETTINGS:
4807 // relay this lfun to Inset, not to the cell.
4808 return Inset::getStatus(cur, cmd, status);
4811 // we try to handle this event in the insets dispatch function.
4812 return cell(cur.idx())->getStatus(cur, cmd, status);
4818 Inset::DisplayType InsetTabular::display() const
4820 if (tabular.is_long_tabular) {
4821 switch (tabular.longtabular_alignment) {
4822 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4824 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4826 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4836 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4838 tabular.latex(os, runparams);
4842 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4844 os << '\n'; // output table on a new line
4845 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4846 tabular.plaintext(os, runparams, dp, false, 0);
4847 return PLAINTEXT_NEWLINE;
4851 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4856 // FIXME: Why not pass a proper DocIterator here?
4858 // if the table is inside a float it doesn't need the informaltable
4859 // wrapper. Search for it.
4860 for (master = owner(); master; master = master->owner())
4861 if (master->lyxCode() == FLOAT_CODE)
4866 os << "<informaltable>";
4869 ret += tabular.docbook(os, runparams);
4871 os << "</informaltable>";
4878 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4880 return tabular.xhtml(xs, rp);
4884 void InsetTabular::validate(LaTeXFeatures & features) const
4886 tabular.validate(features);
4888 // It'd be better to be able to get this from an InsetLayout, but at present
4889 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4890 if (features.runparams().flavor == OutputParams::HTML)
4891 features.addCSSSnippet(
4892 "table { border: 1px solid black; display: inline-block; }\n"
4893 "td { border: 1px solid black; padding: 0.5ex; }");
4897 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4899 return tabular.cellInset(idx);
4903 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4905 return tabular.cellInset(idx);
4909 void InsetTabular::cursorPos(BufferView const & bv,
4910 CursorSlice const & sl, bool boundary, int & x, int & y) const
4912 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4914 // y offset correction
4915 y += cellYPos(sl.idx());
4916 y += tabular.textVOffset(sl.idx());
4917 y += offset_valign_;
4919 // x offset correction
4920 x += cellXPos(sl.idx());
4921 x += tabular.textHOffset(sl.idx());
4922 x += ADD_TO_TABULAR_WIDTH;
4927 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4931 Inset const & inset = *tabular.cellInset(cell);
4932 Point o = bv.coordCache().getInsets().xy(&inset);
4933 int const xbeg = o.x_ - tabular.textHOffset(cell);
4934 int const xend = xbeg + tabular.cellWidth(cell);
4935 row_type const row = tabular.cellRow(cell);
4936 int const ybeg = o.y_ - tabular.rowAscent(row)
4937 - tabular.interRowSpace(row);
4938 int const yend = ybeg + tabular.cellHeight(cell);
4950 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4951 // << " ybeg=" << ybeg << " yend=" << yend
4952 // << " xx=" << xx << " yy=" << yy
4953 // << " dist=" << xx + yy << endl;
4958 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4960 //lyxerr << "InsetTabular::editXY: " << this << endl;
4961 cur.setSelection(false);
4963 cur.idx() = getNearestCell(cur.bv(), x, y);
4965 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4969 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4971 cur.idx() = getNearestCell(cur.bv(), x, y);
4972 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4976 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4978 idx_type idx_min = 0;
4979 int dist_min = numeric_limits<int>::max();
4980 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4981 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4982 int const d = dist(bv, i, x, y);
4993 int InsetTabular::cellYPos(idx_type const cell) const
4995 row_type row = tabular.cellRow(cell);
4997 for (row_type r = 0; r < row; ++r)
4998 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4999 + tabular.interRowSpace(r + 1);
5004 int InsetTabular::cellXPos(idx_type const cell) const
5006 col_type col = tabular.cellColumn(cell);
5008 for (col_type c = 0; c < col; ++c)
5009 lx += tabular.column_info[c].width;
5014 void InsetTabular::resetPos(Cursor & cur) const
5016 BufferView & bv = cur.bv();
5017 int const maxwidth = bv.workWidth();
5019 int const scx_old = scx_;
5020 int const i = cur.find(this);
5025 int const X2 = maxwidth;
5026 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5027 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5028 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5031 scx_ = X1 + 20 - x1;
5033 scx_ = X2 - 20 - x2;
5038 // only update if offset changed
5039 if (scx_ != scx_old)
5040 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5044 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5046 row_type const row = tabular.cellRow(cur.idx());
5047 col_type const col = tabular.cellColumn(cur.idx());
5049 if (isRightToLeft(cur)) {
5050 if (tabular.cellColumn(cur.idx()) == 0) {
5051 if (row == tabular.nrows() - 1)
5053 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5058 cur.idx() = tabular.getLastCellInRow(row - 1);
5060 cur.idx() = tabular.cellIndex(row, col - 1);
5063 if (tabular.isLastCell(cur.idx()))
5065 if (cur.idx() == tabular.getLastCellInRow(row))
5066 cur.idx() = tabular.cellIndex(row + 1, 0);
5068 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5069 cur.idx() = tabular.cellIndex(row, colnextcell);
5073 cur.boundary(false);
5075 if (cur.selIsMultiCell()) {
5076 cur.pit() = cur.lastpit();
5077 cur.pos() = cur.lastpos();
5085 // in visual mode, place cursor at extreme left or right
5087 switch(entry_from) {
5089 case ENTRY_DIRECTION_RIGHT:
5090 cur.posVisToRowExtremity(false /* !left */);
5092 case ENTRY_DIRECTION_LEFT:
5093 cur.posVisToRowExtremity(true /* left */);
5095 case ENTRY_DIRECTION_IGNORE:
5096 // nothing to do in this case
5100 cur.setCurrentFont();
5105 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5107 row_type const row = tabular.cellRow(cur.idx());
5108 col_type const col = tabular.cellColumn(cur.idx());
5110 if (isRightToLeft(cur)) {
5111 if (cur.idx() == tabular.getLastCellInRow(row)) {
5114 cur.idx() = tabular.getFirstCellInRow(row);
5115 cur.idx() = tabular.cellAbove(cur.idx());
5117 if (tabular.isLastCell(cur.idx()))
5119 if (cur.idx() == tabular.getLastCellInRow(row))
5120 cur.idx() = tabular.cellIndex(row + 1, 0);
5122 cur.idx() = tabular.cellIndex(row, col + 1);
5125 if (cur.idx() == 0) // first cell
5128 cur.idx() = tabular.getLastCellInRow(row - 1);
5130 cur.idx() = tabular.cellIndex(row, col - 1);
5133 if (cur.selIsMultiCell()) {
5134 cur.pit() = cur.lastpit();
5135 cur.pos() = cur.lastpos();
5140 cur.pit() = cur.lastpit();
5141 cur.pos() = cur.lastpos();
5143 // in visual mode, place cursor at extreme left or right
5145 switch(entry_from) {
5147 case ENTRY_DIRECTION_RIGHT:
5148 cur.posVisToRowExtremity(false /* !left */);
5150 case ENTRY_DIRECTION_LEFT:
5151 cur.posVisToRowExtremity(true /* left */);
5153 case ENTRY_DIRECTION_IGNORE:
5154 // nothing to do in this case
5158 cur.setCurrentFont();
5163 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5165 istringstream is(argument);
5168 if (insetCode(s) != TABULAR_CODE)
5172 size_t safe_guard = 0;
5177 if (safe_guard > 1000) {
5178 LYXERR0("parameter max count reached!");
5182 Tabular::Feature action = Tabular::LAST_ACTION;
5185 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5186 if (s != tabularFeature[i].feature)
5189 action = tabularFeature[i].action;
5192 if (action == Tabular::LAST_ACTION) {
5193 LYXERR0("Feature not found " << s);
5197 if (tabularFeature[i].need_value)
5199 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5200 tabularFeatures(cur, action, val);
5206 static void checkLongtableSpecial(Tabular::ltType & ltt,
5207 string const & special, bool & flag)
5209 if (special == "dl_above") {
5212 } else if (special == "dl_below") {
5213 ltt.bottomDL = flag;
5215 } else if (special == "empty") {
5225 bool InsetTabular::oneCellHasRotationState(bool rotated,
5226 row_type row_start, row_type row_end,
5227 col_type col_start, col_type col_end) const
5229 for (row_type r = row_start; r <= row_end; ++r)
5230 for (col_type c = col_start; c <= col_end; ++c)
5231 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5237 void InsetTabular::tabularFeatures(Cursor & cur,
5238 Tabular::Feature feature, string const & value)
5240 col_type sel_col_start;
5241 col_type sel_col_end;
5242 row_type sel_row_start;
5243 row_type sel_row_end;
5244 bool setLines = false;
5245 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5246 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5250 case Tabular::M_ALIGN_LEFT:
5251 case Tabular::ALIGN_LEFT:
5252 setAlign = LYX_ALIGN_LEFT;
5255 case Tabular::M_ALIGN_RIGHT:
5256 case Tabular::ALIGN_RIGHT:
5257 setAlign = LYX_ALIGN_RIGHT;
5260 case Tabular::M_ALIGN_CENTER:
5261 case Tabular::ALIGN_CENTER:
5262 setAlign = LYX_ALIGN_CENTER;
5265 case Tabular::ALIGN_BLOCK:
5266 setAlign = LYX_ALIGN_BLOCK;
5269 case Tabular::ALIGN_DECIMAL:
5270 setAlign = LYX_ALIGN_DECIMAL;
5273 case Tabular::M_VALIGN_TOP:
5274 case Tabular::VALIGN_TOP:
5275 setVAlign = Tabular::LYX_VALIGN_TOP;
5278 case Tabular::M_VALIGN_BOTTOM:
5279 case Tabular::VALIGN_BOTTOM:
5280 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5283 case Tabular::M_VALIGN_MIDDLE:
5284 case Tabular::VALIGN_MIDDLE:
5285 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5292 cur.recordUndoInset(ATOMIC_UNDO, this);
5294 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5295 row_type const row = tabular.cellRow(cur.idx());
5296 col_type const column = tabular.cellColumn(cur.idx());
5298 Tabular::ltType ltt;
5302 case Tabular::SET_TABULAR_WIDTH:
5303 tabular.setTabularWidth(Length(value));
5306 case Tabular::SET_PWIDTH: {
5307 Length const len(value);
5308 tabular.setColumnPWidth(cur, cur.idx(), len);
5310 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5311 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5315 case Tabular::SET_MPWIDTH:
5316 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5319 case Tabular::SET_MROFFSET:
5320 tabular.setMROffset(cur, cur.idx(), Length(value));
5323 case Tabular::SET_SPECIAL_COLUMN:
5324 case Tabular::SET_SPECIAL_MULTICOLUMN:
5325 if (value == "none")
5326 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5328 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5331 case Tabular::APPEND_ROW:
5332 // append the row into the tabular
5333 tabular.appendRow(row);
5336 case Tabular::APPEND_COLUMN:
5337 // append the column into the tabular
5338 tabular.appendColumn(column);
5339 cur.idx() = tabular.cellIndex(row, column);
5342 case Tabular::DELETE_ROW:
5343 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5344 tabular.deleteRow(sel_row_start);
5345 if (sel_row_start >= tabular.nrows())
5347 cur.idx() = tabular.cellIndex(sel_row_start, column);
5350 cur.setSelection(false);
5353 case Tabular::DELETE_COLUMN:
5354 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5355 tabular.deleteColumn(sel_col_start);
5356 if (sel_col_start >= tabular.ncols())
5358 cur.idx() = tabular.cellIndex(row, sel_col_start);
5361 cur.setSelection(false);
5364 case Tabular::COPY_ROW:
5365 tabular.copyRow(row);
5368 case Tabular::COPY_COLUMN:
5369 tabular.copyColumn(column);
5370 cur.idx() = tabular.cellIndex(row, column);
5373 case Tabular::SET_LINE_TOP:
5374 case Tabular::TOGGLE_LINE_TOP: {
5375 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5376 ? (value == "true") : !tabular.topLine(cur.idx());
5377 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5378 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5379 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5383 case Tabular::SET_LINE_BOTTOM:
5384 case Tabular::TOGGLE_LINE_BOTTOM: {
5385 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5386 ? (value == "true") : !tabular.bottomLine(cur.idx());
5387 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5388 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5389 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5393 case Tabular::SET_LINE_LEFT:
5394 case Tabular::TOGGLE_LINE_LEFT: {
5395 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5396 ? (value == "true") : !tabular.leftLine(cur.idx());
5397 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5398 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5399 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5403 case Tabular::SET_LINE_RIGHT:
5404 case Tabular::TOGGLE_LINE_RIGHT: {
5405 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5406 ? (value == "true") : !tabular.rightLine(cur.idx());
5407 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5408 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5409 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5413 case Tabular::M_ALIGN_LEFT:
5414 case Tabular::M_ALIGN_RIGHT:
5415 case Tabular::M_ALIGN_CENTER:
5416 case Tabular::ALIGN_LEFT:
5417 case Tabular::ALIGN_RIGHT:
5418 case Tabular::ALIGN_CENTER:
5419 case Tabular::ALIGN_BLOCK:
5420 case Tabular::ALIGN_DECIMAL:
5421 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5422 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5423 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5424 !tabular.getPWidth(c).zero());
5427 case Tabular::M_VALIGN_TOP:
5428 case Tabular::M_VALIGN_BOTTOM:
5429 case Tabular::M_VALIGN_MIDDLE:
5431 case Tabular::VALIGN_TOP:
5432 case Tabular::VALIGN_BOTTOM:
5433 case Tabular::VALIGN_MIDDLE:
5434 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5435 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5436 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5439 case Tabular::SET_MULTICOLUMN: {
5440 if (!cur.selection()) {
5441 // just multicol for one single cell
5442 // check whether we are completely in a multicol
5443 if (!tabular.isMultiColumn(cur.idx()))
5444 tabular.setMultiColumn(cur.idx(), 1,
5445 tabular.rightLine(cur.idx()));
5448 // we have a selection so this means we just add all this
5449 // cells to form a multicolumn cell
5450 idx_type const s_start = cur.selBegin().idx();
5451 row_type const col_start = tabular.cellColumn(s_start);
5452 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5453 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5454 tabular.rightLine(cur.selEnd().idx()));
5457 cur.setSelection(false);
5461 case Tabular::UNSET_MULTICOLUMN: {
5462 if (!cur.selection()) {
5463 if (tabular.isMultiColumn(cur.idx()))
5464 tabular.unsetMultiColumn(cur.idx());
5469 case Tabular::MULTICOLUMN: {
5470 if (!cur.selection()) {
5471 if (tabular.isMultiColumn(cur.idx()))
5472 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5474 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5478 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5479 row_type const r = sel_row_start;
5480 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5481 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5484 // If the selection contains at least one singlecol cell
5485 // or multiple multicol cells,
5486 // we assume the user will merge is to a single multicol
5488 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5490 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5494 case Tabular::SET_MULTIROW: {
5495 if (!cur.selection()) {
5496 // just multirow for one single cell
5497 // check whether we are completely in a multirow
5498 if (!tabular.isMultiRow(cur.idx()))
5499 tabular.setMultiRow(cur.idx(), 1,
5500 tabular.bottomLine(cur.idx()));
5503 // we have a selection so this means we just add all this
5504 // cells to form a multirow cell
5505 idx_type const s_start = cur.selBegin().idx();
5506 row_type const row_start = tabular.cellRow(s_start);
5507 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5508 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5509 tabular.bottomLine(cur.selEnd().idx()));
5512 cur.setSelection(false);
5516 case Tabular::UNSET_MULTIROW: {
5517 if (!cur.selection()) {
5518 if (tabular.isMultiRow(cur.idx()))
5519 tabular.unsetMultiRow(cur.idx());
5524 case Tabular::MULTIROW: {
5525 if (!cur.selection()) {
5526 if (tabular.isMultiRow(cur.idx()))
5527 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5529 tabularFeatures(cur, Tabular::SET_MULTIROW);
5533 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5534 col_type const c = sel_col_start;
5535 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5536 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5539 // If the selection contains at least one singlerow cell
5540 // or multiple multirow cells,
5541 // we assume the user will merge is to a single multirow
5543 tabularFeatures(cur, Tabular::SET_MULTIROW);
5545 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5549 case Tabular::SET_ALL_LINES:
5551 case Tabular::UNSET_ALL_LINES:
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 idx_type const cell = tabular.cellIndex(r, c);
5555 tabular.setTopLine(cell, setLines);
5556 tabular.setBottomLine(cell, setLines);
5557 tabular.setRightLine(cell, setLines);
5558 tabular.setLeftLine(cell, setLines);
5562 case Tabular::SET_BORDER_LINES:
5563 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5564 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5565 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5567 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5568 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5569 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5573 case Tabular::SET_LONGTABULAR:
5574 tabular.is_long_tabular = true;
5577 case Tabular::UNSET_LONGTABULAR:
5578 for (row_type r = 0; r < tabular.nrows(); ++r) {
5579 if (tabular.ltCaption(r)) {
5580 cur.idx() = tabular.cellIndex(r, 0);
5583 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5586 tabular.is_long_tabular = false;
5589 case Tabular::SET_ROTATE_TABULAR:
5590 tabular.rotate = true;
5593 case Tabular::UNSET_ROTATE_TABULAR:
5594 tabular.rotate = false;
5597 case Tabular::TOGGLE_ROTATE_TABULAR:
5598 tabular.rotate = !tabular.rotate;
5601 case Tabular::TABULAR_VALIGN_TOP:
5602 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5605 case Tabular::TABULAR_VALIGN_MIDDLE:
5606 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5609 case Tabular::TABULAR_VALIGN_BOTTOM:
5610 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5613 case Tabular::LONGTABULAR_ALIGN_LEFT:
5614 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5617 case Tabular::LONGTABULAR_ALIGN_CENTER:
5618 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5621 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5622 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5627 case Tabular::SET_ROTATE_CELL:
5628 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5629 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5630 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5633 case Tabular::UNSET_ROTATE_CELL:
5634 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5635 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5636 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5639 case Tabular::TOGGLE_ROTATE_CELL:
5641 bool oneNotRotated = oneCellHasRotationState(false,
5642 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5644 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5645 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5646 tabular.setRotateCell(tabular.cellIndex(r, c),
5651 case Tabular::SET_USEBOX: {
5652 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5653 if (val == tabular.getUsebox(cur.idx()))
5654 val = Tabular::BOX_NONE;
5655 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5656 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5657 tabular.setUsebox(tabular.cellIndex(r, c), val);
5661 case Tabular::UNSET_LTFIRSTHEAD:
5663 case Tabular::SET_LTFIRSTHEAD:
5664 tabular.getRowOfLTFirstHead(row, ltt);
5665 checkLongtableSpecial(ltt, value, flag);
5666 tabular.setLTHead(row, flag, ltt, true);
5669 case Tabular::UNSET_LTHEAD:
5671 case Tabular::SET_LTHEAD:
5672 tabular.getRowOfLTHead(row, ltt);
5673 checkLongtableSpecial(ltt, value, flag);
5674 tabular.setLTHead(row, flag, ltt, false);
5677 case Tabular::UNSET_LTFOOT:
5679 case Tabular::SET_LTFOOT:
5680 tabular.getRowOfLTFoot(row, ltt);
5681 checkLongtableSpecial(ltt, value, flag);
5682 tabular.setLTFoot(row, flag, ltt, false);
5685 case Tabular::UNSET_LTLASTFOOT:
5687 case Tabular::SET_LTLASTFOOT:
5688 tabular.getRowOfLTLastFoot(row, ltt);
5689 checkLongtableSpecial(ltt, value, flag);
5690 tabular.setLTFoot(row, flag, ltt, true);
5693 case Tabular::SET_LTNEWPAGE:
5694 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5697 case Tabular::SET_LTCAPTION: {
5698 if (tabular.ltCaption(row))
5700 cur.idx() = tabular.setLTCaption(row, true);
5703 cur.setSelection(false);
5704 // If a row is set as caption, then also insert
5705 // a caption. Otherwise the LaTeX output is broken.
5706 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5707 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5711 case Tabular::UNSET_LTCAPTION: {
5712 if (!tabular.ltCaption(row))
5714 cur.idx() = tabular.setLTCaption(row, false);
5717 cur.setSelection(false);
5718 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5719 if (lyx::getStatus(fr).enabled())
5724 case Tabular::TOGGLE_LTCAPTION: {
5725 if (tabular.ltCaption(row))
5726 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5728 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5732 case Tabular::SET_BOOKTABS:
5733 tabular.use_booktabs = true;
5736 case Tabular::UNSET_BOOKTABS:
5737 tabular.use_booktabs = false;
5740 case Tabular::SET_TOP_SPACE: {
5742 if (value == "default")
5743 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5744 tabular.row_info[r].top_space_default = true;
5745 else if (value == "none")
5746 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5747 tabular.row_info[r].top_space_default = false;
5748 tabular.row_info[r].top_space = len;
5750 else if (isValidLength(value, &len))
5751 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5752 tabular.row_info[r].top_space_default = false;
5753 tabular.row_info[r].top_space = len;
5758 case Tabular::SET_BOTTOM_SPACE: {
5760 if (value == "default")
5761 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5762 tabular.row_info[r].bottom_space_default = true;
5763 else if (value == "none")
5764 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5765 tabular.row_info[r].bottom_space_default = false;
5766 tabular.row_info[r].bottom_space = len;
5768 else if (isValidLength(value, &len))
5769 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5770 tabular.row_info[r].bottom_space_default = false;
5771 tabular.row_info[r].bottom_space = len;
5776 case Tabular::SET_INTERLINE_SPACE: {
5778 if (value == "default")
5779 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5780 tabular.row_info[r].interline_space_default = true;
5781 else if (value == "none")
5782 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5783 tabular.row_info[r].interline_space_default = false;
5784 tabular.row_info[r].interline_space = len;
5786 else if (isValidLength(value, &len))
5787 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5788 tabular.row_info[r].interline_space_default = false;
5789 tabular.row_info[r].interline_space = len;
5794 case Tabular::SET_DECIMAL_POINT:
5795 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5796 tabular.column_info[c].decimal_point = from_utf8(value);
5799 // dummy stuff just to avoid warnings
5800 case Tabular::LAST_ACTION:
5806 bool InsetTabular::copySelection(Cursor & cur)
5808 if (!cur.selection())
5813 getSelection(cur, rs, re, cs, ce);
5815 paste_tabular.reset(new Tabular(tabular));
5817 for (row_type r = 0; r < rs; ++r)
5818 paste_tabular->deleteRow(0);
5820 row_type const rows = re - rs + 1;
5821 while (paste_tabular->nrows() > rows)
5822 paste_tabular->deleteRow(rows);
5824 for (col_type c = 0; c < cs; ++c)
5825 paste_tabular->deleteColumn(0);
5827 col_type const columns = ce - cs + 1;
5828 while (paste_tabular->ncols() > columns)
5829 paste_tabular->deleteColumn(columns);
5831 paste_tabular->setBuffer(tabular.buffer());
5833 odocstringstream os;
5834 OutputParams const runparams(0);
5835 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5836 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5837 cap::copySelection(cur, os.str());
5839 // mark tabular stack dirty
5840 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5841 // when we (hopefully) have a one-for-all paste mechanism.
5842 // This must be called after cap::copySelection.
5843 dirtyTabularStack(true);
5849 bool InsetTabular::pasteClipboard(Cursor & cur)
5853 col_type actcol = tabular.cellColumn(cur.idx());
5854 row_type actrow = tabular.cellRow(cur.idx());
5856 if (cur.selIsMultiCell()) {
5859 getSelection(cur, actrow, re, actcol, ce);
5862 for (row_type r1 = 0, r2 = actrow;
5863 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5865 for (col_type c1 = 0, c2 = actcol;
5866 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5868 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5869 tabular.isPartOfMultiColumn(r2, c2))
5871 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5875 if (tabular.isPartOfMultiColumn(r2, c2)) {
5879 shared_ptr<InsetTableCell> inset(
5880 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5881 tabular.setCellInset(r2, c2, inset);
5882 // FIXME?: why do we need to do this explicitly? (EL)
5883 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5885 // FIXME: change tracking (MG)
5886 inset->setChange(Change(buffer().params().trackChanges ?
5887 Change::INSERTED : Change::UNCHANGED));
5895 void InsetTabular::cutSelection(Cursor & cur)
5897 if (!cur.selection())
5902 getSelection(cur, rs, re, cs, ce);
5903 for (row_type r = rs; r <= re; ++r) {
5904 for (col_type c = cs; c <= ce; ++c) {
5905 shared_ptr<InsetTableCell> t
5906 = cell(tabular.cellIndex(r, c));
5907 if (buffer().params().trackChanges)
5908 // FIXME: Change tracking (MG)
5909 t->setChange(Change(Change::DELETED));
5915 // cursor position might be invalid now
5916 if (cur.pit() > cur.lastpit())
5917 cur.pit() = cur.lastpit();
5918 if (cur.pos() > cur.lastpos())
5919 cur.pos() = cur.lastpos();
5920 cur.clearSelection();
5924 bool InsetTabular::isRightToLeft(Cursor & cur) const
5926 LASSERT(cur.depth() > 1, /**/);
5927 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5928 pos_type const parentpos = cur[cur.depth() - 2].pos();
5929 return parentpar.getFontSettings(buffer().params(),
5930 parentpos).language()->rightToLeft();
5934 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5937 LASSERT(stidx <= enidx, return docstring());
5939 col_type const col1 = tabular.cellColumn(stidx);
5940 col_type const col2 = tabular.cellColumn(enidx);
5941 row_type const row1 = tabular.cellRow(stidx);
5942 row_type const row2 = tabular.cellRow(enidx);
5944 for (col_type col = col1; col <= col2; col++)
5945 for (row_type row = row1; row <= row2; row++) {
5950 retval += tabular.cellInset(row, col)->asString(intoInsets);
5956 void InsetTabular::getSelection(Cursor & cur,
5957 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5959 CursorSlice const & beg = cur.selBegin();
5960 CursorSlice const & end = cur.selEnd();
5961 cs = tabular.cellColumn(beg.idx());
5962 ce = tabular.cellColumn(end.idx());
5966 rs = tabular.cellRow(beg.idx());
5967 re = tabular.cellRow(end.idx());
5973 Text * InsetTabular::getText(int idx) const
5975 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5979 void InsetTabular::setChange(Change const & change)
5981 for (idx_type idx = 0; idx < nargs(); ++idx)
5982 cell(idx)->setChange(change);
5986 void InsetTabular::acceptChanges()
5988 for (idx_type idx = 0; idx < nargs(); ++idx)
5989 cell(idx)->acceptChanges();
5993 void InsetTabular::rejectChanges()
5995 for (idx_type idx = 0; idx < nargs(); ++idx)
5996 cell(idx)->rejectChanges();
6000 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6002 return tabular.getPWidth(cell).zero();
6006 bool InsetTabular::forcePlainLayout(idx_type cell) const
6008 return !tabular.getPWidth(cell).zero();
6012 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6015 if (buf.length() <= 0)
6020 col_type maxCols = 1;
6021 size_t const len = buf.length();
6025 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6033 maxCols = max(cols, maxCols);
6039 maxCols = max(cols, maxCols);
6045 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6046 loctab = paste_tabular.get();
6048 dirtyTabularStack(true);
6051 cell = bv.cursor().idx();
6052 ocol = tabular.cellColumn(cell);
6053 row = tabular.cellRow(cell);
6057 idx_type const cells = loctab->numberofcells;
6060 rows = loctab->nrows();
6061 col_type const columns = loctab->ncols();
6063 while (cell < cells && p < len && row < rows &&
6064 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6070 // we can only set this if we are not too far right
6071 if (cols < columns) {
6072 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6073 Font const font = bv.textMetrics(&inset->text()).
6075 inset->setText(buf.substr(op, p - op), font,
6076 buffer().params().trackChanges);
6082 // we can only set this if we are not too far right
6083 if (cols < columns) {
6084 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6085 Font const font = bv.textMetrics(&inset->text()).
6087 inset->setText(buf.substr(op, p - op), font,
6088 buffer().params().trackChanges);
6093 cell = loctab->cellIndex(row, cols);
6099 // check for the last cell if there is no trailing '\n'
6100 if (cell < cells && op < len) {
6101 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6102 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6103 inset->setText(buf.substr(op, len - op), font,
6104 buffer().params().trackChanges);
6110 void InsetTabular::addPreview(DocIterator const & inset_pos,
6111 PreviewLoader & loader) const
6113 DocIterator cell_pos = inset_pos;
6115 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6116 for (row_type r = 0; r < tabular.nrows(); ++r) {
6117 for (col_type c = 0; c < tabular.ncols(); ++c) {
6118 cell_pos.top().idx() = tabular.cellIndex(r, c);
6119 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6125 bool InsetTabular::completionSupported(Cursor const & cur) const
6127 Cursor const & bvCur = cur.bv().cursor();
6128 if (&bvCur.inset() != this)
6130 return cur.text()->completionSupported(cur);
6134 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6136 return completionSupported(cur);
6140 bool InsetTabular::automaticInlineCompletion() const
6142 return lyxrc.completion_inline_text;
6146 bool InsetTabular::automaticPopupCompletion() const
6148 return lyxrc.completion_popup_text;
6152 bool InsetTabular::showCompletionCursor() const
6154 return lyxrc.completion_cursor_text;
6158 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6160 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6164 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6166 if (!completionSupported(cur))
6168 return cur.text()->completionPrefix(cur);
6172 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6174 if (!completionSupported(cur))
6177 return cur.text()->insertCompletion(cur, s, finished);
6181 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6182 Dimension & dim) const
6184 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6185 tm.completionPosAndDim(cur, x, y, dim);
6189 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6191 istringstream data(in);
6193 lex.setStream(data);
6200 if (!lex || token != "tabular") {
6201 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6205 // This is part of the inset proper that is usually swallowed
6206 // by Buffer::readInset
6208 if (!lex || token != "Tabular") {
6209 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6217 string InsetTabular::params2string(InsetTabular const & inset)
6220 data << "tabular" << ' ';
6222 data << "\\end_inset\n";