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; // horiz 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_ALL_LINES, "set-all-lines", false },
146 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
147 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
148 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
149 { Tabular::SET_PWIDTH, "set-pwidth", true },
150 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
151 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
152 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
153 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
154 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
155 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
156 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
157 { Tabular::SET_USEBOX, "set-usebox", true },
158 { Tabular::SET_LTHEAD, "set-lthead", true },
159 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
160 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
161 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
162 { Tabular::SET_LTFOOT, "set-ltfoot", true },
163 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
164 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
165 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
166 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
167 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
168 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
169 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
170 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
171 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
172 { Tabular::SET_BOOKTABS, "set-booktabs", false },
173 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
174 { Tabular::SET_TOP_SPACE, "set-top-space", true },
175 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
176 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
177 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
178 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
179 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
180 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
181 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
182 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
183 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
184 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
185 { Tabular::LAST_ACTION, "", false }
190 string const write_attribute(string const & name, T const & t)
192 string const s = tostr(t);
193 return s.empty() ? s : " " + name + "=\"" + s + "\"";
197 string const write_attribute(string const & name, string const & t)
199 return t.empty() ? t : " " + name + "=\"" + t + "\"";
204 string const write_attribute(string const & name, docstring const & t)
206 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
211 string const write_attribute(string const & name, bool const & b)
213 // we write only true attribute values so we remove a bit of the
214 // file format bloat for tabulars.
215 return b ? write_attribute(name, convert<string>(b)) : string();
220 string const write_attribute(string const & name, int const & i)
222 // we write only true attribute values so we remove a bit of the
223 // file format bloat for tabulars.
224 return i ? write_attribute(name, convert<string>(i)) : string();
229 string const write_attribute(string const & name, Tabular::idx_type const & i)
231 // we write only true attribute values so we remove a bit of the
232 // file format bloat for tabulars.
233 return i ? write_attribute(name, convert<string>(i)) : string();
238 string const write_attribute(string const & name, Length const & value)
240 // we write only the value if we really have one same reson as above.
241 return value.zero() ? string() : write_attribute(name, value.asString());
245 string const tostr(LyXAlignment const & num)
250 case LYX_ALIGN_BLOCK:
254 case LYX_ALIGN_CENTER:
256 case LYX_ALIGN_RIGHT:
258 case LYX_ALIGN_LAYOUT:
260 case LYX_ALIGN_SPECIAL:
262 case LYX_ALIGN_DECIMAL:
269 string const tostr(Tabular::HAlignment const & num)
272 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
274 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
276 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
283 string const tostr(Tabular::VAlignment const & num)
286 case Tabular::LYX_VALIGN_TOP:
288 case Tabular::LYX_VALIGN_MIDDLE:
290 case Tabular::LYX_VALIGN_BOTTOM:
297 string const tostr(Tabular::BoxType const & num)
300 case Tabular::BOX_NONE:
302 case Tabular::BOX_PARBOX:
304 case Tabular::BOX_MINIPAGE:
311 // I would have liked a fromstr template a lot better. (Lgb)
312 bool string2type(string const str, LyXAlignment & num)
315 num = LYX_ALIGN_NONE;
316 else if (str == "block")
317 num = LYX_ALIGN_BLOCK;
318 else if (str == "left")
319 num = LYX_ALIGN_LEFT;
320 else if (str == "center")
321 num = LYX_ALIGN_CENTER;
322 else if (str == "right")
323 num = LYX_ALIGN_RIGHT;
324 else if (str == "decimal")
325 num = LYX_ALIGN_DECIMAL;
332 bool string2type(string const str, Tabular::HAlignment & num)
335 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
336 else if (str == "center" )
337 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
338 else if (str == "right")
339 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
346 bool string2type(string const str, Tabular::VAlignment & num)
349 num = Tabular::LYX_VALIGN_TOP;
350 else if (str == "middle" )
351 num = Tabular::LYX_VALIGN_MIDDLE;
352 else if (str == "bottom")
353 num = Tabular::LYX_VALIGN_BOTTOM;
360 bool string2type(string const str, Tabular::BoxType & num)
363 num = Tabular::BOX_NONE;
364 else if (str == "parbox")
365 num = Tabular::BOX_PARBOX;
366 else if (str == "minipage")
367 num = Tabular::BOX_MINIPAGE;
374 bool string2type(string const str, bool & num)
378 else if (str == "false")
386 bool getTokenValue(string const & str, char const * token, string & ret)
389 size_t token_length = strlen(token);
390 size_t pos = str.find(token);
392 if (pos == string::npos || pos + token_length + 1 >= str.length()
393 || str[pos + token_length] != '=')
395 pos += token_length + 1;
397 if (ch != '"' && ch != '\'') { // only read till next space
401 while (pos < str.length() - 1 && str[++pos] != ch)
408 bool getTokenValue(string const & str, char const * token, docstring & ret)
411 bool const success = getTokenValue(str, token, tmp);
412 ret = from_utf8(tmp);
417 bool getTokenValue(string const & str, char const * token, int & num)
421 if (!getTokenValue(str, token, tmp))
423 num = convert<int>(tmp);
428 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
431 return getTokenValue(str, token, tmp) && string2type(tmp, num);
435 bool getTokenValue(string const & str, char const * token,
436 Tabular::HAlignment & num)
439 return getTokenValue(str, token, tmp) && string2type(tmp, num);
443 bool getTokenValue(string const & str, char const * token,
444 Tabular::VAlignment & num)
447 return getTokenValue(str, token, tmp) && string2type(tmp, num);
451 bool getTokenValue(string const & str, char const * token,
452 Tabular::BoxType & num)
455 return getTokenValue(str, token, tmp) && string2type(tmp, num);
459 bool getTokenValue(string const & str, char const * token, bool & flag)
461 // set the flag always to false as this should be the default for bools
462 // not in the file-format.
465 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
469 bool getTokenValue(string const & str, char const * token, Length & len)
471 // set the length to be zero() as default as this it should be if not
472 // in the file format.
475 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
479 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
484 if (!getTokenValue(str, token, tmp))
486 if (tmp == "default") {
490 return isValidLength(tmp, &len);
494 void l_getline(istream & is, string & str)
497 while (str.empty()) {
499 if (!str.empty() && str[str.length() - 1] == '\r')
500 str.erase(str.length() - 1);
507 string const featureAsString(Tabular::Feature action)
509 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
510 if (tabularFeature[i].action == action)
511 return tabularFeature[i].feature;
517 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
519 InsetTableCell tail = InsetTableCell(head);
520 tail.getText(0)->setMacrocontextPosition(
521 head.getText(0)->macrocontextPosition());
522 tail.setBuffer(head.buffer());
524 DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
525 for (; dit; dit.forwardChar())
526 if (dit.inTexted() && dit.depth()==1
527 && dit.paragraph().find(align_d, false, false, dit.pos()))
530 pit_type const psize = head.paragraphs().front().size();
533 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
534 tail.paragraphs().front().eraseChars(0,
535 dit.pos() < psize ? dit.pos() + 1 : psize, false);
542 /////////////////////////////////////////////////////////////////////
546 /////////////////////////////////////////////////////////////////////
549 Tabular::CellData::CellData(Buffer * buf)
552 multicolumn(Tabular::CELL_NORMAL),
553 multirow(Tabular::CELL_NORMAL),
554 alignment(LYX_ALIGN_CENTER),
555 valignment(LYX_VALIGN_TOP),
565 inset(new InsetTableCell(buf))
567 inset->setBuffer(*buf);
571 Tabular::CellData::CellData(CellData const & cs)
574 multicolumn(cs.multicolumn),
575 multirow(cs.multirow),
576 alignment(cs.alignment),
577 valignment(cs.valignment),
578 decimal_hoffset(cs.decimal_hoffset),
579 decimal_width(cs.decimal_width),
581 top_line(cs.top_line),
582 bottom_line(cs.bottom_line),
583 left_line(cs.left_line),
584 right_line(cs.right_line),
587 align_special(cs.align_special),
589 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
593 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
599 void Tabular::CellData::swap(CellData & rhs)
601 std::swap(cellno, rhs.cellno);
602 std::swap(width, rhs.width);
603 std::swap(multicolumn, rhs.multicolumn);
604 std::swap(multirow, rhs.multirow);
605 std::swap(alignment, rhs.alignment);
606 std::swap(valignment, rhs.valignment);
607 std::swap(decimal_hoffset, rhs.decimal_hoffset);
608 std::swap(decimal_width, rhs.decimal_width);
609 std::swap(voffset, rhs.voffset);
610 std::swap(top_line, rhs.top_line);
611 std::swap(bottom_line, rhs.bottom_line);
612 std::swap(left_line, rhs.left_line);
613 std::swap(right_line, rhs.right_line);
614 std::swap(usebox, rhs.usebox);
615 std::swap(rotate, rhs.rotate);
616 std::swap(align_special, rhs.align_special);
617 p_width.swap(rhs.p_width);
618 inset.swap(rhs.inset);
622 Tabular::RowData::RowData()
625 top_space_default(false),
626 bottom_space_default(false),
627 interline_space_default(false),
637 Tabular::ColumnData::ColumnData()
638 : alignment(LYX_ALIGN_CENTER),
639 valignment(LYX_VALIGN_TOP),
645 Tabular::ltType::ltType()
652 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
654 init(buffer, rows_arg, columns_arg);
658 void Tabular::setBuffer(Buffer & buffer)
661 for (row_type i = 0; i < nrows(); ++i)
662 for (col_type j = 0; j < ncols(); ++j)
663 cell_info[i][j].inset->setBuffer(*buffer_);
667 // activates all lines and sets all widths to 0
668 void Tabular::init(Buffer * buf, row_type rows_arg,
669 col_type columns_arg)
672 row_info = row_vector(rows_arg);
673 column_info = column_vector(columns_arg);
674 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
675 row_info.reserve(10);
676 column_info.reserve(10);
677 cell_info.reserve(100);
679 is_long_tabular = false;
680 tabular_valignment = LYX_VALIGN_MIDDLE;
681 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
683 use_booktabs = false;
684 // set silly default lines
685 for (row_type r = 0; r < nrows(); ++r)
686 for (col_type c = 0; c < ncols(); ++c) {
687 cell_info[r][c].inset->setBuffer(*buffer_);
688 cell_info[r][c].top_line = true;
689 cell_info[r][c].left_line = true;
690 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
691 cell_info[r][c].right_line = c == ncols() - 1;
696 void Tabular::appendRow(idx_type const cell)
698 row_type const row = cellRow(cell);
700 row_info.insert(row_info.begin() + row + 1, RowData());
701 row_info[row + 1] = row_info[row];
703 cell_info.insert(cell_info.begin() + row + 1,
704 cell_vector(ncols(), CellData(buffer_)));
705 for (col_type c = 0; c < ncols(); ++c) {
706 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
707 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
709 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
713 for (col_type c = 0; c < ncols(); ++c) {
714 if (isPartOfMultiRow(row, c))
716 // inherit line settings
717 idx_type const i = cellIndex(row + 1, c);
718 idx_type const j = cellIndex(row, c);
719 setLeftLine(i, leftLine(j));
720 setRightLine(i, rightLine(j));
721 setTopLine(i, topLine(j));
722 if (topLine(j) && bottomLine(j)) {
723 setBottomLine(i, true);
724 setBottomLine(j, false);
726 // mark track changes
727 if (buffer().params().trackChanges)
728 cellInfo(i).inset->setChange(Change(Change::INSERTED));
733 void Tabular::deleteRow(row_type const row)
735 // Not allowed to delete last row
739 for (col_type c = 0; c < ncols(); ++c) {
740 // Care about multirow cells
741 if (row + 1 < nrows() &&
742 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
743 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
744 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
747 row_info.erase(row_info.begin() + row);
748 cell_info.erase(cell_info.begin() + row);
753 void Tabular::copyRow(row_type const row)
755 row_info.insert(row_info.begin() + row, row_info[row]);
756 cell_info.insert(cell_info.begin() + row, cell_info[row]);
758 if (buffer().params().trackChanges)
759 for (col_type c = 0; c < ncols(); ++c)
760 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
766 void Tabular::appendColumn(idx_type const cell)
768 col_type const c = cellColumn(cell);
770 column_info.insert(column_info.begin() + c + 1, ColumnData());
771 column_info[c + 1] = column_info[c];
773 for (row_type r = 0; r < nrows(); ++r) {
774 cell_info[r].insert(cell_info[r].begin() + c + 1,
776 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
777 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
779 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
782 for (row_type r = 0; r < nrows(); ++r) {
783 // inherit line settings
784 idx_type const i = cellIndex(r, c + 1);
785 idx_type const j = cellIndex(r, c);
786 setBottomLine(i, bottomLine(j));
787 setTopLine(i, topLine(j));
788 setLeftLine(i, leftLine(j));
789 if (rightLine(j) && rightLine(j)) {
790 setRightLine(i, true);
791 setRightLine(j, false);
793 if (buffer().params().trackChanges)
794 cellInfo(i).inset->setChange(Change(Change::INSERTED));
799 void Tabular::deleteColumn(col_type const col)
801 // Not allowed to delete last column
805 for (row_type r = 0; r < nrows(); ++r) {
806 // Care about multicolumn cells
807 if (col + 1 < ncols() &&
808 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
809 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
810 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
812 cell_info[r].erase(cell_info[r].begin() + col);
814 column_info.erase(column_info.begin() + col);
819 void Tabular::copyColumn(col_type const col)
821 BufferParams const & bp = buffer().params();
822 column_info.insert(column_info.begin() + col, column_info[col]);
824 for (row_type r = 0; r < nrows(); ++r) {
825 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
827 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
833 void Tabular::updateIndexes()
837 for (row_type row = 0; row < nrows(); ++row)
838 for (col_type column = 0; column < ncols(); ++column) {
839 if (!isPartOfMultiColumn(row, column)
840 && !isPartOfMultiRow(row, column))
842 if (isPartOfMultiRow(row, column))
843 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
845 cell_info[row][column].cellno = numberofcells - 1;
848 rowofcell.resize(numberofcells);
849 columnofcell.resize(numberofcells);
851 for (row_type row = 0; row < nrows(); ++row)
852 for (col_type column = 0; column < ncols(); ++column) {
853 if (isPartOfMultiColumn(row, column))
855 if (!isPartOfMultiRow(row, column)) {
856 columnofcell[i] = column;
859 setFixedWidth(row, column);
860 cell_info[row][column].inset->setContentAlignment(
861 getAlignment(cellIndex(row, column)));
862 if (isPartOfMultiRow(row, column))
869 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
872 for (col_type c = 0; c < ncols(); ++c)
873 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
879 bool Tabular::topLine(idx_type const cell) const
881 return cellInfo(cell).top_line;
885 bool Tabular::bottomLine(idx_type const cell) const
887 return cellInfo(cell).bottom_line;
891 bool Tabular::leftLine(idx_type cell) const
895 return cellInfo(cell).left_line;
899 bool Tabular::rightLine(idx_type cell) const
903 return cellInfo(cell).right_line;
907 int Tabular::interRowSpace(row_type row) const
909 if (!row || row >= nrows())
912 int const interline_space = row_info[row - 1].interline_space_default ?
914 row_info[row - 1].interline_space.inPixels(width());
915 if (rowTopLine(row) && rowBottomLine(row - 1))
916 return interline_space + WIDTH_OF_LINE;
917 return interline_space;
921 int Tabular::interColumnSpace(idx_type cell) const
923 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
924 if (rightLine(cell) && nextcol < ncols()
925 && leftLine(cellIndex(cellRow(cell), nextcol)))
926 return WIDTH_OF_LINE;
931 int Tabular::cellWidth(idx_type cell) const
934 col_type const span = columnSpan(cell);
935 col_type const col = cellColumn(cell);
936 for(col_type c = col; c < col + span ; ++c)
937 w += column_info[c].width;
942 int Tabular::cellHeight(idx_type cell) const
944 row_type const span = rowSpan(cell);
945 row_type const row = cellRow(cell);
947 for(row_type r = row; r < row + span ; ++r) {
948 h += rowAscent(r) + rowDescent(r);
949 if (r != row + span - 1)
950 h += interRowSpace(r + 1);
957 bool Tabular::updateColumnWidths()
959 vector<int> max_dwidth(ncols(), 0);
960 for(col_type c = 0; c < ncols(); ++c)
961 for(row_type r = 0; r < nrows(); ++r) {
962 idx_type const i = cellIndex(r, c);
963 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
964 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
968 // for each col get max of single col cells
969 for(col_type c = 0; c < ncols(); ++c) {
971 for(row_type r = 0; r < nrows(); ++r) {
972 idx_type const i = cellIndex(r, c);
973 if (columnSpan(i) == 1) {
974 if (getAlignment(i) == LYX_ALIGN_DECIMAL
975 && cell_info[r][c].decimal_width!=0)
976 new_width = max(new_width, cellInfo(i).width
977 + max_dwidth[c] - cellInfo(i).decimal_width);
979 new_width = max(new_width, cellInfo(i).width);
983 if (column_info[c].width != new_width) {
984 column_info[c].width = new_width;
988 // update col widths to fit merged cells
989 for(col_type c = 0; c < ncols(); ++c)
990 for(row_type r = 0; r < nrows(); ++r) {
991 idx_type const i = cellIndex(r, c);
992 int const span = columnSpan(i);
993 if (span == 1 || c > cellColumn(i))
997 for(col_type j = c; j < c + span ; ++j)
998 old_width += column_info[j].width;
1000 if (cellInfo(i).width > old_width) {
1001 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1010 int Tabular::width() const
1013 for (col_type c = 0; c < ncols(); ++c)
1014 width += column_info[c].width;
1019 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1022 col_type const col = cellColumn(cell);
1023 if (onlycolumn || !isMultiColumn(cell)) {
1024 column_info[col].alignment = align;
1025 docstring & dpoint = column_info[col].decimal_point;
1026 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1027 dpoint = from_utf8(lyxrc.default_decimal_point);
1029 cellInfo(cell).alignment = align;
1030 cellInset(cell).get()->setContentAlignment(align);
1035 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1038 if (!isMultiColumn(cell) || onlycolumn)
1039 column_info[cellColumn(cell)].valignment = align;
1041 cellInfo(cell).valignment = align;
1048 * Allow line and paragraph breaks for fixed width cells or disallow them,
1049 * merge cell paragraphs and reset layout to standard for variable width
1052 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1054 inset->setAutoBreakRows(fixedWidth);
1055 inset->toggleFixedWidth(fixedWidth);
1059 // merge all paragraphs to one
1060 BufferParams const & bp = cur.bv().buffer().params();
1061 while (inset->paragraphs().size() > 1)
1062 mergeParagraph(bp, inset->paragraphs(), 0);
1066 // undo information has already been recorded
1067 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1068 bp.documentClass().plainLayoutName());
1075 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1076 Length const & width)
1078 col_type const c = cellColumn(cell);
1080 column_info[c].p_width = width;
1081 // reset the vertical alignment to top if the fixed with
1082 // is removed or zero because only fixed width columns can
1083 // have a vertical alignment
1084 if (column_info[c].p_width.zero())
1085 column_info[c].valignment = LYX_VALIGN_TOP;
1086 for (row_type r = 0; r < nrows(); ++r) {
1087 idx_type const cell = cellIndex(r, c);
1088 // because of multicolumns
1089 toggleFixedWidth(cur, cellInset(cell).get(),
1090 !getPWidth(cell).zero());
1092 // cur paragraph can become invalid after paragraphs were merged
1093 if (cur.pit() > cur.lastpit())
1094 cur.pit() = cur.lastpit();
1095 // cur position can become invalid after newlines were removed
1096 if (cur.pos() > cur.lastpos())
1097 cur.pos() = cur.lastpos();
1101 bool Tabular::setFixedWidth(row_type r, col_type c)
1103 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1104 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1105 || (multicol && !cell_info[r][c].p_width.zero());
1106 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1111 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1112 Length const & width)
1114 if (!isMultiColumn(cell))
1117 cellInfo(cell).p_width = width;
1118 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1119 // cur paragraph can become invalid after paragraphs were merged
1120 if (cur.pit() > cur.lastpit())
1121 cur.pit() = cur.lastpit();
1122 // cur position can become invalid after newlines were removed
1123 if (cur.pos() > cur.lastpos())
1124 cur.pos() = cur.lastpos();
1129 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1130 Tabular::Feature what)
1132 if (what == SET_SPECIAL_MULTICOLUMN)
1133 cellInfo(cell).align_special = special;
1135 column_info[cellColumn(cell)].align_special = special;
1139 void Tabular::setTopLine(idx_type i, bool line)
1141 cellInfo(i).top_line = line;
1145 void Tabular::setBottomLine(idx_type i, bool line)
1147 cellInfo(i).bottom_line = line;
1151 void Tabular::setLeftLine(idx_type cell, bool line)
1153 cellInfo(cell).left_line = line;
1157 void Tabular::setRightLine(idx_type cell, bool line)
1159 cellInfo(cell).right_line = line;
1162 bool Tabular::rowTopLine(row_type r) const
1164 bool all_rows_set = true;
1165 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1166 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1167 return all_rows_set;
1171 bool Tabular::rowBottomLine(row_type r) const
1173 bool all_rows_set = true;
1174 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1175 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1176 return all_rows_set;
1180 bool Tabular::columnLeftLine(col_type c) const
1187 for (row_type r = 0; r < nrows(); ++r) {
1188 idx_type const i = cellIndex(r, c);
1189 if (c == cellColumn(i)) {
1191 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1192 if (cellInfo(i).left_line || right)
1196 return 2 * nrows_left >= total;
1200 bool Tabular::columnRightLine(col_type c) const
1205 int nrows_right = 0;
1207 for (row_type r = 0; r < nrows(); ++r) {
1208 idx_type i = cellIndex(r, c);
1209 if (c == cellColumn(i) + columnSpan(i) - 1) {
1211 bool left = (c + 1 < ncols()
1212 && cellInfo(cellIndex(r, c + 1)).left_line)
1213 || c + 1 == ncols();
1214 if (cellInfo(i).right_line && left)
1218 return 2 * nrows_right >= total;
1222 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1224 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1225 return cellInfo(cell).alignment;
1227 return column_info[cellColumn(cell)].alignment;
1232 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1234 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1235 return cellInfo(cell).valignment;
1236 return column_info[cellColumn(cell)].valignment;
1240 Length const Tabular::getPWidth(idx_type cell) const
1242 if (isMultiColumn(cell))
1243 return cellInfo(cell).p_width;
1244 return column_info[cellColumn(cell)].p_width;
1248 int Tabular::textHOffset(idx_type cell) const
1250 // the LaTeX Way :-(
1251 int x = WIDTH_OF_LINE;
1253 int const w = cellWidth(cell) - cellInfo(cell).width;
1255 switch (getAlignment(cell)) {
1256 case LYX_ALIGN_CENTER:
1259 case LYX_ALIGN_RIGHT:
1262 case LYX_ALIGN_DECIMAL: {
1263 // we center when no decimal point
1264 if (cellInfo(cell).decimal_width == 0) {
1268 col_type const c = cellColumn(cell);
1269 int max_dhoffset = 0;
1270 for(row_type r = 0; r < row_info.size() ; ++r) {
1271 idx_type const i = cellIndex(r, c);
1272 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1273 && cellInfo(i).decimal_width != 0)
1274 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1276 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1279 // LYX_ALIGN_LEFT: nothing :-)
1287 int Tabular::textVOffset(idx_type cell) const
1289 int voffset = cellInfo(cell).voffset;
1290 if (isMultiRow(cell)) {
1291 row_type const row = cellRow(cell);
1292 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1298 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1301 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1303 return cell_info[row][c].cellno;
1307 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1309 col_type c = ncols() - 1;
1310 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1311 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1313 return cell_info[row][c].cellno;
1317 Tabular::row_type Tabular::cellRow(idx_type cell) const
1319 if (cell >= numberofcells)
1323 return rowofcell[cell];
1327 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1329 if (cell >= numberofcells)
1333 return columnofcell[cell];
1337 void Tabular::write(ostream & os) const
1341 << write_attribute("version", 3)
1342 << write_attribute("rows", nrows())
1343 << write_attribute("columns", ncols())
1345 // global longtable options
1347 << write_attribute("rotate", rotate)
1348 << write_attribute("booktabs", use_booktabs)
1349 << write_attribute("islongtable", is_long_tabular)
1350 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1351 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1352 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1353 << write_attribute("headTopDL", endhead.topDL)
1354 << write_attribute("headBottomDL", endhead.bottomDL)
1355 << write_attribute("footTopDL", endfoot.topDL)
1356 << write_attribute("footBottomDL", endfoot.bottomDL)
1357 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1358 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1359 << write_attribute("lastFootEmpty", endlastfoot.empty);
1360 // longtables cannot be aligned vertically
1361 if (!is_long_tabular)
1362 os << write_attribute("tabularvalignment", tabular_valignment);
1363 if (is_long_tabular)
1364 os << write_attribute("longtabularalignment",
1365 longtabular_alignment);
1367 for (col_type c = 0; c < ncols(); ++c) {
1369 << write_attribute("alignment", column_info[c].alignment);
1370 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1371 os << write_attribute("decimal_point", column_info[c].decimal_point);
1372 os << write_attribute("valignment", column_info[c].valignment)
1373 << write_attribute("width", column_info[c].p_width.asString())
1374 << write_attribute("special", column_info[c].align_special)
1377 for (row_type r = 0; r < nrows(); ++r) {
1378 static const string def("default");
1380 if (row_info[r].top_space_default)
1381 os << write_attribute("topspace", def);
1383 os << write_attribute("topspace", row_info[r].top_space);
1384 if (row_info[r].bottom_space_default)
1385 os << write_attribute("bottomspace", def);
1387 os << write_attribute("bottomspace", row_info[r].bottom_space);
1388 if (row_info[r].interline_space_default)
1389 os << write_attribute("interlinespace", def);
1391 os << write_attribute("interlinespace", row_info[r].interline_space);
1392 os << write_attribute("endhead", row_info[r].endhead)
1393 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1394 << write_attribute("endfoot", row_info[r].endfoot)
1395 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1396 << write_attribute("newpage", row_info[r].newpage)
1397 << write_attribute("caption", row_info[r].caption)
1399 for (col_type c = 0; c < ncols(); ++c) {
1401 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1402 << write_attribute("multirow", cell_info[r][c].multirow)
1403 << write_attribute("alignment", cell_info[r][c].alignment)
1404 << write_attribute("valignment", cell_info[r][c].valignment)
1405 << write_attribute("topline", cell_info[r][c].top_line)
1406 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1407 << write_attribute("leftline", cell_info[r][c].left_line)
1408 << write_attribute("rightline", cell_info[r][c].right_line)
1409 << write_attribute("rotate", cell_info[r][c].rotate)
1410 << write_attribute("usebox", cell_info[r][c].usebox)
1411 << write_attribute("width", cell_info[r][c].p_width)
1412 << write_attribute("special", cell_info[r][c].align_special)
1414 os << "\\begin_inset ";
1415 cell_info[r][c].inset->write(os);
1416 os << "\n\\end_inset\n"
1421 os << "</lyxtabular>\n";
1425 void Tabular::read(Lexer & lex)
1428 istream & is = lex.getStream();
1430 l_getline(is, line);
1431 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1432 LASSERT(false, /**/);
1437 if (!getTokenValue(line, "version", version))
1439 LASSERT(version >= 2, /**/);
1442 if (!getTokenValue(line, "rows", rows_arg))
1445 if (!getTokenValue(line, "columns", columns_arg))
1447 init(buffer_, rows_arg, columns_arg);
1448 l_getline(is, line);
1449 if (!prefixIs(line, "<features")) {
1450 lyxerr << "Wrong tabular format (expected <features ...> got"
1451 << line << ')' << endl;
1454 getTokenValue(line, "rotate", rotate);
1455 getTokenValue(line, "booktabs", use_booktabs);
1456 getTokenValue(line, "islongtable", is_long_tabular);
1457 getTokenValue(line, "tabularvalignment", tabular_valignment);
1458 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1459 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1460 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1461 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1462 getTokenValue(line, "headTopDL", endhead.topDL);
1463 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1464 getTokenValue(line, "footTopDL", endfoot.topDL);
1465 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1466 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1467 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1468 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1470 for (col_type c = 0; c < ncols(); ++c) {
1472 if (!prefixIs(line,"<column")) {
1473 lyxerr << "Wrong tabular format (expected <column ...> got"
1474 << line << ')' << endl;
1477 getTokenValue(line, "alignment", column_info[c].alignment);
1478 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1479 getTokenValue(line, "valignment", column_info[c].valignment);
1480 getTokenValue(line, "width", column_info[c].p_width);
1481 getTokenValue(line, "special", column_info[c].align_special);
1484 for (row_type i = 0; i < nrows(); ++i) {
1485 l_getline(is, line);
1486 if (!prefixIs(line, "<row")) {
1487 lyxerr << "Wrong tabular format (expected <row ...> got"
1488 << line << ')' << endl;
1491 getTokenValue(line, "topspace", row_info[i].top_space,
1492 row_info[i].top_space_default);
1493 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1494 row_info[i].bottom_space_default);
1495 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1496 row_info[i].interline_space_default);
1497 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1498 getTokenValue(line, "endhead", row_info[i].endhead);
1499 getTokenValue(line, "endfoot", row_info[i].endfoot);
1500 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1501 getTokenValue(line, "newpage", row_info[i].newpage);
1502 getTokenValue(line, "caption", row_info[i].caption);
1503 for (col_type j = 0; j < ncols(); ++j) {
1504 l_getline(is, line);
1505 if (!prefixIs(line, "<cell")) {
1506 lyxerr << "Wrong tabular format (expected <cell ...> got"
1507 << line << ')' << endl;
1510 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1511 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1512 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1513 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1514 getTokenValue(line, "topline", cell_info[i][j].top_line);
1515 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1516 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1517 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1518 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1519 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1520 getTokenValue(line, "width", cell_info[i][j].p_width);
1522 getTokenValue(line, "special", cell_info[i][j].align_special);
1523 l_getline(is, line);
1524 if (prefixIs(line, "\\begin_inset")) {
1525 cell_info[i][j].inset->setBuffer(*buffer_);
1526 cell_info[i][j].inset->read(lex);
1527 l_getline(is, line);
1529 if (!prefixIs(line, "</cell>")) {
1530 lyxerr << "Wrong tabular format (expected </cell> got"
1531 << line << ')' << endl;
1535 l_getline(is, line);
1536 if (!prefixIs(line, "</row>")) {
1537 lyxerr << "Wrong tabular format (expected </row> got"
1538 << line << ')' << endl;
1542 while (!prefixIs(line, "</lyxtabular>")) {
1543 l_getline(is, line);
1549 bool Tabular::isMultiColumn(idx_type cell) const
1551 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1552 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1556 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1558 return cell_info[cellRow(cell)][cellColumn(cell)];
1562 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1564 idx_type const col = cellColumn(cell);
1565 idx_type const row = cellRow(cell);
1566 for (idx_type i = 0; i < number; ++i)
1567 unsetMultiRow(cellIndex(row, col + i));
1569 // unsetting of multirow may have invalidated cell index
1570 cell = cellIndex(row, col);
1571 CellData & cs = cellInfo(cell);
1572 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1573 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1574 cs.alignment = column_info[col].alignment;
1576 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1578 for (idx_type i = 1; i < number; ++i) {
1579 CellData & cs1 = cellInfo(cell + i);
1580 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1581 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1589 bool Tabular::isMultiRow(idx_type cell) const
1591 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1592 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1596 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1598 idx_type const col = cellColumn(cell);
1599 idx_type const row = cellRow(cell);
1600 for (idx_type i = 0; i < number; ++i)
1601 unsetMultiColumn(cellIndex(row + i, col));
1603 // unsetting of multirow may have invalidated cell index
1604 cell = cellIndex(row, col);
1605 CellData & cs = cellInfo(cell);
1606 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1607 cs.valignment = LYX_VALIGN_MIDDLE;
1608 // the horizontal alignment of multirow cells can only
1609 // be changed for the whole table row,
1610 // support changing this only for the multirow cell can be done via
1612 // this feature would be a fileformat change
1613 // until LyX supports this, use the deault alignment of multirow
1615 cs.alignment = LYX_ALIGN_CENTER;
1617 // set the bottom row of the last selected cell
1618 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1620 for (idx_type i = 1; i < number; ++i) {
1621 CellData & cs1 = cell_info[row + i][col];
1622 cs1.multirow = CELL_PART_OF_MULTIROW;
1623 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1631 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1633 row_type const row = cellRow(cell);
1634 col_type const col = cellColumn(cell);
1636 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1643 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1645 col_type const column = cellColumn(cell);
1646 col_type row = cellRow(cell) + 1;
1647 while (row < nrows() && isPartOfMultiRow(row, column))
1650 return row - cellRow(cell);
1654 void Tabular::unsetMultiColumn(idx_type cell)
1656 if (!isMultiColumn(cell))
1659 row_type const row = cellRow(cell);
1660 col_type const col = cellColumn(cell);
1661 row_type const span = columnSpan(cell);
1662 for (col_type c = 0; c < span; ++c) {
1663 // in the table dialog the lines are set in every case
1664 // when unsetting a multicolumn this leads to an additional right
1665 // line for every cell that was part of the former multicolumn cell,
1666 // except if the cell is in the last column
1667 // therefore remove this line
1668 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1669 && (col + c) < (col + span - 1))
1670 cell_info[row][col + c].right_line = false;
1671 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1677 void Tabular::unsetMultiRow(idx_type cell)
1679 if (!isMultiRow(cell))
1682 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1683 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1684 row_type const row = cellRow(cell);
1685 col_type const col = cellColumn(cell);
1686 row_type const span = rowSpan(cell);
1687 for (row_type r = 0; r < span; ++r)
1688 cell_info[row + r][col].multirow = CELL_NORMAL;
1693 void Tabular::setRotateCell(idx_type cell, bool flag)
1695 cellInfo(cell).rotate = flag;
1699 bool Tabular::getRotateCell(idx_type cell) const
1701 return cellInfo(cell).rotate;
1705 bool Tabular::needRotating() const
1709 for (row_type r = 0; r < nrows(); ++r)
1710 for (col_type c = 0; c < ncols(); ++c)
1711 if (cell_info[r][c].rotate)
1717 bool Tabular::isLastCell(idx_type cell) const
1719 if (cell + 1 < numberofcells)
1725 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1727 if (cellRow(cell) == 0)
1730 col_type const col = cellColumn(cell);
1731 row_type r = cellRow(cell) - 1;
1732 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1735 return cell_info[r][col].cellno;
1739 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1741 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1742 if (nextrow < nrows())
1743 return cell_info[nextrow][cellColumn(cell)].cellno;
1748 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1750 LASSERT(column != npos && column < ncols()
1751 && row != npos && row < nrows(), /**/);
1752 return cell_info[row][column].cellno;
1756 void Tabular::setUsebox(idx_type cell, BoxType type)
1758 cellInfo(cell).usebox = type;
1762 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1763 // adds line breaks, see bug 4886.
1764 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1766 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1767 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1769 if (cellInfo(cell).usebox > 1)
1770 return cellInfo(cell).usebox;
1771 return useParbox(cell);
1776 // This are functions used for the longtable support
1778 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1784 row_info[row].endfirsthead = flag;
1788 row_info[row].endhead = flag;
1793 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1796 hd.set = haveLTHead();
1797 return row_info[row].endhead;
1801 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1804 hd.set = haveLTFirstHead();
1805 return row_info[row].endfirsthead;
1809 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1815 row_info[row].endlastfoot = flag;
1819 row_info[row].endfoot = flag;
1824 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1827 fd.set = haveLTFoot();
1828 return row_info[row].endfoot;
1832 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1835 fd.set = haveLTLastFoot();
1836 return row_info[row].endlastfoot;
1840 void Tabular::setLTNewPage(row_type row, bool what)
1842 row_info[row].newpage = what;
1846 bool Tabular::getLTNewPage(row_type row) const
1848 return row_info[row].newpage;
1852 bool Tabular::haveLTHead() const
1854 for (row_type i = 0; i < nrows(); ++i)
1855 if (row_info[i].endhead)
1861 bool Tabular::haveLTFirstHead() const
1863 if (endfirsthead.empty)
1865 for (row_type r = 0; r < nrows(); ++r)
1866 if (row_info[r].endfirsthead)
1872 bool Tabular::haveLTFoot() const
1874 for (row_type r = 0; r < nrows(); ++r)
1875 if (row_info[r].endfoot)
1881 bool Tabular::haveLTLastFoot() const
1883 if (endlastfoot.empty)
1885 for (row_type r = 0; r < nrows(); ++r)
1886 if (row_info[r].endlastfoot)
1892 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1894 idx_type i = getFirstCellInRow(row);
1896 setMultiColumn(i, numberOfCellsInRow(row));
1897 setTopLine(i, false);
1898 setBottomLine(i, false);
1899 setLeftLine(i, false);
1900 setRightLine(i, false);
1902 unsetMultiColumn(i);
1903 // When unsetting a caption row, also all existing
1904 // captions in this row must be dissolved.
1906 row_info[row].caption = what;
1911 bool Tabular::ltCaption(row_type row) const
1913 return row_info[row].caption;
1917 bool Tabular::haveLTCaption() const
1919 for (row_type r = 0; r < nrows(); ++r)
1920 if (row_info[r].caption)
1926 // end longtable support functions
1928 void Tabular::setRowAscent(row_type row, int height)
1930 if (row >= nrows() || row_info[row].ascent == height)
1932 row_info[row].ascent = height;
1936 void Tabular::setRowDescent(row_type row, int height)
1938 if (row >= nrows() || row_info[row].descent == height)
1940 row_info[row].descent = height;
1944 int Tabular::rowAscent(row_type row) const
1946 LASSERT(row < nrows(), /**/);
1947 return row_info[row].ascent;
1951 int Tabular::rowDescent(row_type row) const
1953 LASSERT(row < nrows(), /**/);
1954 return row_info[row].descent;
1958 int Tabular::height() const
1961 for (row_type row = 0; row < nrows(); ++row)
1962 height += rowAscent(row) + rowDescent(row) +
1968 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1970 LASSERT(row < nrows(), /**/);
1971 LASSERT(column < ncols(), /**/);
1972 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1976 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1978 LASSERT(row < nrows(), /**/);
1979 LASSERT(column < ncols(), /**/);
1980 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1984 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1986 // we only output complete row lines and the 1st row here, the rest
1987 // is done in Tabular::TeXBottomHLine(...)
1989 // get for each column the topline (if any)
1990 vector<bool> topline;
1992 for (col_type c = 0; c < ncols(); ++c) {
1993 topline.push_back(topLine(cellIndex(row, c)));
1994 // If cell is part of a multirow and not the first cell of the
1995 // multirow, no line must be drawn.
1997 if (isMultiRow(cellIndex(row, c))
1998 && isMultiRow(cellIndex(row - 1, c)))
2004 // do nothing if empty first row, or incomplete row line after
2005 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2008 // only output complete row lines and the 1st row's clines
2009 if (nset == ncols()) {
2011 os << (row == 0 ? "\\toprule " : "\\midrule ");
2015 } else if (row == 0) {
2016 for (col_type c = 0; c < ncols(); ++c) {
2018 col_type offset = 0;
2019 for (col_type j = 0 ; j < c; ++j)
2020 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2023 //babel makes the "-" character an active one, so we have to suppress this here
2024 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2025 if (lang == "slovak" || lang == "czech")
2026 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2027 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2029 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2031 col_type cstart = c;
2032 for ( ; c < ncols() && topline[c]; ++c) {}
2034 for (col_type j = cstart ; j < c ; ++j)
2035 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2038 os << c + offset << "} ";
2047 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2049 // we output bottomlines of row r and the toplines of row r+1
2050 // if the latter do not span the whole tabular
2052 // get the bottomlines of row r, and toplines in next row
2053 bool lastrow = row == nrows() - 1;
2054 vector<bool> bottomline, topline;
2055 bool nextrowset = true;
2056 for (col_type c = 0; c < ncols(); ++c) {
2057 bottomline.push_back(bottomLine(cellIndex(row, c)));
2058 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2059 // If cell is part of a multirow and not the last or first cell of the
2060 // multirow, no line must be drawn.
2062 if (isMultiRow(cellIndex(row, c))
2063 && isMultiRow(cellIndex(row + 1, c))) {
2064 bottomline[c] = false;
2067 nextrowset &= topline[c];
2070 // combine this row's bottom lines and next row's toplines if necessary
2072 for (col_type c = 0; c < ncols(); ++c) {
2074 bottomline[c] = bottomline[c] || topline[c];
2079 // do nothing if empty, OR incomplete row line with a topline in next row
2080 if (nset == 0 || (nextrowset && nset != ncols()))
2083 if (nset == ncols()) {
2085 os << (lastrow ? "\\bottomrule" : "\\midrule");
2089 for (col_type c = 0; c < ncols(); ++c) {
2090 if (bottomline[c]) {
2091 col_type offset = 0;
2092 for (col_type j = 0 ; j < c; ++j)
2093 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2096 //babel makes the "-" character an active one, so we have to suppress this here
2097 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2098 if (lang == "slovak" || lang == "czech")
2099 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2100 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2102 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2104 col_type cstart = c;
2105 for ( ; c < ncols() && bottomline[c]; ++c) {}
2107 for (col_type j = cstart ; j < c ; ++j)
2108 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2111 os << c + offset << "} ";
2120 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2121 bool & ismulticol, bool & ismultirow) const
2124 row_type const r = cellRow(cell);
2125 if (is_long_tabular && row_info[r].caption)
2128 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2129 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2130 // figure out how to set the lines
2131 // we always set double lines to the right of the cell
2132 col_type const c = cellColumn(cell);
2133 col_type const nextcol = c + columnSpan(cell);
2134 bool colright = columnRightLine(c);
2135 bool colleft = columnLeftLine(c);
2136 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2137 bool nextcellleft = nextcol < ncols()
2138 && leftLine(cellIndex(r, nextcol));
2139 bool coldouble = colright && nextcolleft;
2140 bool celldouble = rightLine(cell) && nextcellleft;
2142 ismulticol = isMultiColumn(cell)
2143 || (c == 0 && colleft != leftLine(cell))
2144 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2145 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2146 || (coldouble != celldouble);
2148 // we center in multicol when no decimal point
2149 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2150 && (cellInfo(cell).decimal_width == 0));
2152 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2153 int latexcolspan = columnSpan(cell);
2154 for(col_type col = c; col < c + columnSpan(cell); ++col)
2155 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2159 os << "\\multicolumn{" << latexcolspan << "}{";
2160 if (c ==0 && leftLine(cell))
2162 if (!cellInfo(cell).align_special.empty()) {
2163 os << cellInfo(cell).align_special;
2165 if (!getPWidth(cell).zero()) {
2167 case LYX_ALIGN_LEFT:
2168 os << ">{\\raggedright}";
2170 case LYX_ALIGN_RIGHT:
2171 os << ">{\\raggedleft}";
2173 case LYX_ALIGN_CENTER:
2174 os << ">{\\centering}";
2180 case LYX_VALIGN_TOP:
2183 case LYX_VALIGN_MIDDLE:
2186 case LYX_VALIGN_BOTTOM:
2191 << from_ascii(getPWidth(cell).asLatexString())
2195 case LYX_ALIGN_LEFT:
2198 case LYX_ALIGN_RIGHT:
2205 } // end if else !getPWidth
2206 } // end if else !cellinfo_of_cell
2207 if (rightLine(cell) || nextcellleft)
2210 // add extra vertical line if we want a double one
2213 } // end if ismulticol
2215 // we only need code for the first multirow cell
2216 ismultirow = isMultiRow(cell);
2218 os << "\\multirow{" << rowSpan(cell) << "}{";
2219 if (!getPWidth(cell).zero())
2220 os << from_ascii(getPWidth(cell).asLatexString());
2222 // we need to set a default value
2225 } // end if ismultirow
2227 if (getRotateCell(cell)) {
2228 os << "\\begin{sideways}\n";
2231 if (getUsebox(cell) == BOX_PARBOX) {
2234 case LYX_VALIGN_TOP:
2237 case LYX_VALIGN_MIDDLE:
2240 case LYX_VALIGN_BOTTOM:
2244 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2246 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2247 os << "\\begin{minipage}[";
2249 case LYX_VALIGN_TOP:
2252 case LYX_VALIGN_MIDDLE:
2255 case LYX_VALIGN_BOTTOM:
2259 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2267 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2268 bool ismulticol, bool ismultirow) const
2271 row_type const r = cellRow(cell);
2272 if (is_long_tabular && row_info[r].caption)
2276 if (getUsebox(cell) == BOX_PARBOX)
2278 else if (getUsebox(cell) == BOX_MINIPAGE) {
2279 os << "%\n\\end{minipage}";
2282 if (getRotateCell(cell)) {
2283 os << "%\n\\end{sideways}";
2295 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2296 OutputParams const & runparams) const
2298 if (!is_long_tabular)
2303 // the caption must be output before the headers
2304 if (haveLTCaption()) {
2305 for (row_type r = 0; r < nrows(); ++r) {
2306 if (row_info[r].caption) {
2307 ret += TeXRow(os, r, runparams);
2311 // output first header info
2312 // first header must be output before the header, otherwise the
2313 // correct caption placement becomes really weird
2314 if (haveLTFirstHead()) {
2315 if (endfirsthead.topDL) {
2319 for (row_type r = 0; r < nrows(); ++r) {
2320 if (row_info[r].endfirsthead) {
2321 ret += TeXRow(os, r, runparams);
2324 if (endfirsthead.bottomDL) {
2328 os << "\\endfirsthead\n";
2331 // output header info
2333 if (endfirsthead.empty && !haveLTFirstHead()) {
2334 os << "\\endfirsthead\n";
2337 if (endhead.topDL) {
2341 for (row_type r = 0; r < nrows(); ++r) {
2342 if (row_info[r].endhead) {
2343 ret += TeXRow(os, r, runparams);
2346 if (endhead.bottomDL) {
2350 os << "\\endhead\n";
2353 // output footer info
2355 if (endfoot.topDL) {
2359 for (row_type r = 0; r < nrows(); ++r) {
2360 if (row_info[r].endfoot) {
2361 ret += TeXRow(os, r, runparams);
2364 if (endfoot.bottomDL) {
2368 os << "\\endfoot\n";
2370 if (endlastfoot.empty && !haveLTLastFoot()) {
2371 os << "\\endlastfoot\n";
2375 // output lastfooter info
2376 if (haveLTLastFoot()) {
2377 if (endlastfoot.topDL) {
2381 for (row_type r = 0; r < nrows(); ++r) {
2382 if (row_info[r].endlastfoot) {
2383 ret += TeXRow(os, r, runparams);
2386 if (endlastfoot.bottomDL) {
2390 os << "\\endlastfoot\n";
2397 bool Tabular::isValidRow(row_type row) const
2399 if (!is_long_tabular)
2401 return !row_info[row].endhead && !row_info[row].endfirsthead
2402 && !row_info[row].endfoot && !row_info[row].endlastfoot
2403 && !row_info[row].caption;
2407 int Tabular::TeXRow(odocstream & os, row_type row,
2408 OutputParams const & runparams) const
2410 idx_type cell = cellIndex(row, 0);
2411 shared_ptr<InsetTableCell> inset = cellInset(cell);
2412 Paragraph const & par = inset->paragraphs().front();
2413 string const lang = par.getParLanguage(buffer().params())->lang();
2415 //output the top line
2416 int ret = TeXTopHLine(os, row, lang);
2418 if (row_info[row].top_space_default) {
2420 os << "\\addlinespace\n";
2422 os << "\\noalign{\\vskip\\doublerulesep}\n";
2424 } else if(!row_info[row].top_space.zero()) {
2426 os << "\\addlinespace["
2427 << from_ascii(row_info[row].top_space.asLatexString())
2430 os << "\\noalign{\\vskip"
2431 << from_ascii(row_info[row].top_space.asLatexString())
2436 bool ismulticol = false;
2437 bool ismultirow = false;
2438 for (col_type c = 0; c < ncols(); ++c) {
2439 if (isPartOfMultiColumn(row, c))
2442 if (isPartOfMultiRow(row, c) &&
2443 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2448 cell = cellIndex(row, c);
2449 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2450 shared_ptr<InsetTableCell> inset = cellInset(cell);
2452 Paragraph const & par = inset->paragraphs().front();
2453 bool rtl = par.isRTL(buffer().params())
2455 && getPWidth(cell).zero();
2459 par.getParLanguage(buffer().params())->lang();
2460 if (lang == "farsi")
2462 else if (lang == "arabic_arabi")
2464 // currently, remaning RTL languages are
2465 // arabic_arabtex and hebrew
2469 // pass to the OutputParams that we are in a cell and
2470 // which alignment we have set.
2471 // InsetNewline needs this context information.
2472 OutputParams newrp(runparams);
2473 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2474 ? OutputParams::PLAIN
2475 : OutputParams::ALIGNED;
2477 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2478 && cellInfo(cell).decimal_width != 0) {
2479 // copy cell and split in 2
2480 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2481 head.getText(0)->setMacrocontextPosition(
2482 cellInset(cell)->getText(0)->macrocontextPosition());
2483 head.setBuffer(buffer());
2484 bool hassep = false;
2485 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2486 head.latex(os, newrp);
2488 ret += tail.latex(os, newrp);
2489 } else if (!isPartOfMultiRow(row, c))
2490 ret += inset->latex(os, newrp);
2492 runparams.encoding = newrp.encoding;
2496 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2497 if (cell != getLastCellInRow(row)) { // not last cell in row
2501 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2502 // if no first header and no empty first header is used,
2503 // the caption needs to be terminated by \endfirsthead
2505 os << "\\endfirsthead";
2507 os << "\\tabularnewline";
2508 if (row_info[row].bottom_space_default) {
2510 os << "\\addlinespace";
2512 os << "[\\doublerulesep]";
2513 } else if (!row_info[row].bottom_space.zero()) {
2515 os << "\\addlinespace";
2517 << from_ascii(row_info[row].bottom_space.asLatexString())
2523 //output the bottom line
2524 ret += TeXBottomHLine(os, row, lang);
2526 if (row_info[row].interline_space_default) {
2528 os << "\\addlinespace\n";
2530 os << "\\noalign{\\vskip\\doublerulesep}\n";
2532 } else if (!row_info[row].interline_space.zero()) {
2534 os << "\\addlinespace["
2535 << from_ascii(row_info[row].interline_space.asLatexString())
2538 os << "\\noalign{\\vskip"
2539 << from_ascii(row_info[row].interline_space.asLatexString())
2547 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2551 //+---------------------------------------------------------------------
2552 //+ first the opening preamble +
2553 //+---------------------------------------------------------------------
2556 os << "\\begin{sideways}\n";
2559 if (is_long_tabular) {
2560 os << "\\begin{longtable}";
2561 switch (longtabular_alignment) {
2562 case LYX_LONGTABULAR_ALIGN_LEFT:
2565 case LYX_LONGTABULAR_ALIGN_CENTER:
2567 case LYX_LONGTABULAR_ALIGN_RIGHT:
2572 os << "\\begin{tabular}";
2573 switch (tabular_valignment) {
2574 case LYX_VALIGN_TOP:
2577 case LYX_VALIGN_MIDDLE:
2579 case LYX_VALIGN_BOTTOM:
2587 for (col_type c = 0; c < ncols(); ++c) {
2588 if (columnLeftLine(c))
2590 if (!column_info[c].align_special.empty()) {
2591 os << column_info[c].align_special;
2593 if (!column_info[c].p_width.zero()) {
2594 switch (column_info[c].alignment) {
2595 case LYX_ALIGN_LEFT:
2596 os << ">{\\raggedright}";
2598 case LYX_ALIGN_RIGHT:
2599 os << ">{\\raggedleft}";
2601 case LYX_ALIGN_CENTER:
2602 os << ">{\\centering}";
2604 case LYX_ALIGN_NONE:
2605 case LYX_ALIGN_BLOCK:
2606 case LYX_ALIGN_LAYOUT:
2607 case LYX_ALIGN_SPECIAL:
2608 case LYX_ALIGN_DECIMAL:
2612 switch (column_info[c].valignment) {
2613 case LYX_VALIGN_TOP:
2616 case LYX_VALIGN_MIDDLE:
2619 case LYX_VALIGN_BOTTOM:
2624 << from_ascii(column_info[c].p_width.asLatexString())
2627 switch (column_info[c].alignment) {
2628 case LYX_ALIGN_LEFT:
2631 case LYX_ALIGN_RIGHT:
2634 case LYX_ALIGN_DECIMAL:
2635 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2641 } // end if else !column_info[i].p_width
2642 } // end if else !column_info[i].align_special
2643 if (columnRightLine(c))
2649 ret += TeXLongtableHeaderFooter(os, runparams);
2651 //+---------------------------------------------------------------------
2652 //+ the single row and columns (cells) +
2653 //+---------------------------------------------------------------------
2655 for (row_type r = 0; r < nrows(); ++r) {
2656 if (isValidRow(r)) {
2657 ret += TeXRow(os, r, runparams);
2658 if (is_long_tabular && row_info[r].newpage) {
2659 os << "\\newpage\n";
2665 //+---------------------------------------------------------------------
2666 //+ the closing of the tabular +
2667 //+---------------------------------------------------------------------
2669 if (is_long_tabular)
2670 os << "\\end{longtable}";
2672 os << "\\end{tabular}";
2674 os << "\n\\end{sideways}";
2682 int Tabular::docbookRow(odocstream & os, row_type row,
2683 OutputParams const & runparams) const
2686 idx_type cell = getFirstCellInRow(row);
2689 for (col_type c = 0; c < ncols(); ++c) {
2690 if (isPartOfMultiColumn(row, c))
2693 os << "<entry align=\"";
2694 switch (getAlignment(cell)) {
2695 case LYX_ALIGN_LEFT:
2698 case LYX_ALIGN_RIGHT:
2706 os << "\" valign=\"";
2707 switch (getVAlignment(cell)) {
2708 case LYX_VALIGN_TOP:
2711 case LYX_VALIGN_BOTTOM:
2714 case LYX_VALIGN_MIDDLE:
2719 if (isMultiColumn(cell)) {
2720 os << " namest=\"col" << c << "\" ";
2721 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2725 ret += cellInset(cell)->docbook(os, runparams);
2734 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2738 //+---------------------------------------------------------------------
2739 //+ first the opening preamble +
2740 //+---------------------------------------------------------------------
2742 os << "<tgroup cols=\"" << ncols()
2743 << "\" colsep=\"1\" rowsep=\"1\">\n";
2745 for (col_type c = 0; c < ncols(); ++c) {
2746 os << "<colspec colname=\"col" << c << "\" align=\"";
2747 switch (column_info[c].alignment) {
2748 case LYX_ALIGN_LEFT:
2751 case LYX_ALIGN_RIGHT:
2759 if (runparams.flavor == OutputParams::XML)
2765 //+---------------------------------------------------------------------
2766 //+ Long Tabular case +
2767 //+---------------------------------------------------------------------
2769 // output caption info
2770 if (haveLTCaption()) {
2771 os << "<caption>\n";
2773 for (row_type r = 0; r < nrows(); ++r) {
2774 if (row_info[r].caption) {
2775 ret += docbookRow(os, r, runparams);
2778 os << "</caption>\n";
2781 // output header info
2782 if (haveLTHead() || haveLTFirstHead()) {
2785 for (row_type r = 0; r < nrows(); ++r) {
2786 if (row_info[r].endhead || row_info[r].endfirsthead) {
2787 ret += docbookRow(os, r, runparams);
2793 // output footer info
2794 if (haveLTFoot() || haveLTLastFoot()) {
2797 for (row_type r = 0; r < nrows(); ++r) {
2798 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2799 ret += docbookRow(os, r, runparams);
2806 //+---------------------------------------------------------------------
2807 //+ the single row and columns (cells) +
2808 //+---------------------------------------------------------------------
2812 for (row_type r = 0; r < nrows(); ++r) {
2813 if (isValidRow(r)) {
2814 ret += docbookRow(os, r, runparams);
2819 //+---------------------------------------------------------------------
2820 //+ the closing of the tabular +
2821 //+---------------------------------------------------------------------
2830 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2831 OutputParams const & runparams) const
2834 idx_type cell = getFirstCellInRow(row);
2836 xs << html::StartTag("tr");
2837 for (col_type c = 0; c < ncols(); ++c) {
2838 if (isPartOfMultiColumn(row, c))
2843 switch (getAlignment(cell)) {
2844 case LYX_ALIGN_LEFT:
2847 case LYX_ALIGN_RIGHT:
2855 attr << " valign='";
2856 switch (getVAlignment(cell)) {
2857 case LYX_VALIGN_TOP:
2860 case LYX_VALIGN_BOTTOM:
2863 case LYX_VALIGN_MIDDLE:
2868 if (isMultiColumn(cell))
2869 attr << " colspan='" << columnSpan(cell) << "'";
2871 xs << html::StartTag("td", attr.str());
2872 ret += cellInset(cell)->xhtml(xs, runparams);
2873 xs << html::EndTag("td");
2876 xs << html::EndTag("tr");
2881 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2884 // It's unclear to me if we need to mess with the long table stuff.
2885 // We can borrow that too from docbook, if so.
2887 xs << html::StartTag("tbody");
2888 for (row_type r = 0; r < nrows(); ++r) {
2889 if (isValidRow(r)) {
2890 ret += xhtmlRow(xs, r, runparams);
2893 xs << html::EndTag("tbody");
2898 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2899 vector<unsigned int> const & clen) const
2901 idx_type const fcell = getFirstCellInRow(row);
2902 idx_type const n = numberOfCellsInRow(row) + fcell;
2905 for (idx_type i = fcell; i < n; ++i) {
2915 for (idx_type i = fcell; i < n; ++i) {
2926 col_type column = cellColumn(i);
2927 int len = clen[column];
2928 while (column < ncols() - 1
2929 && isPartOfMultiColumn(row, ++column))
2930 len += clen[column] + 4;
2931 os << docstring(len, ch);
2946 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2947 vector<unsigned int> const & clen) const
2949 idx_type const fcell = getFirstCellInRow(row);
2950 idx_type const n = numberOfCellsInRow(row) + fcell;
2953 for (idx_type i = fcell; i < n; ++i) {
2954 if (bottomLine(i)) {
2963 for (idx_type i = fcell; i < n; ++i) {
2964 if (bottomLine(i)) {
2974 col_type column = cellColumn(i);
2975 int len = clen[column];
2976 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2977 len += clen[column] + 4;
2978 os << docstring(len, ch);
2979 if (bottomLine(i)) {
2993 void Tabular::plaintextPrintCell(odocstream & os,
2994 OutputParams const & runparams,
2995 idx_type cell, row_type row, col_type column,
2996 vector<unsigned int> const & clen,
2997 bool onlydata) const
2999 odocstringstream sstr;
3000 cellInset(cell)->plaintext(sstr, runparams);
3012 unsigned int len1 = sstr.str().length();
3013 unsigned int len2 = clen[column];
3014 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3015 len2 += clen[column] + 4;
3018 switch (getAlignment(cell)) {
3020 case LYX_ALIGN_LEFT:
3023 case LYX_ALIGN_RIGHT:
3027 case LYX_ALIGN_CENTER:
3033 os << docstring(len1, ' ') << sstr.str()
3034 << docstring(len2, ' ');
3036 if (rightLine(cell))
3043 void Tabular::plaintext(odocstream & os,
3044 OutputParams const & runparams, int const depth,
3045 bool onlydata, char_type delim) const
3047 // first calculate the width of the single columns
3048 vector<unsigned int> clen(ncols());
3051 // first all non multicolumn cells!
3052 for (col_type c = 0; c < ncols(); ++c) {
3054 for (row_type r = 0; r < nrows(); ++r) {
3055 idx_type cell = cellIndex(r, c);
3056 if (isMultiColumn(cell))
3058 odocstringstream sstr;
3059 cellInset(cell)->plaintext(sstr, runparams);
3060 if (clen[c] < sstr.str().length())
3061 clen[c] = sstr.str().length();
3064 // then all multicolumn cells!
3065 for (col_type c = 0; c < ncols(); ++c) {
3066 for (row_type r = 0; r < nrows(); ++r) {
3067 idx_type cell = cellIndex(r, c);
3068 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3070 odocstringstream sstr;
3071 cellInset(cell)->plaintext(sstr, runparams);
3072 int len = int(sstr.str().length());
3073 idx_type const n = columnSpan(cell);
3074 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3076 if (len > int(clen[c + n - 1]))
3077 clen[c + n - 1] = len;
3082 for (row_type r = 0; r < nrows(); ++r) {
3083 if (!onlydata && plaintextTopHLine(os, r, clen))
3084 os << docstring(depth * 2, ' ');
3085 for (col_type c = 0; c < ncols(); ++c) {
3086 if (isPartOfMultiColumn(r, c))
3088 if (onlydata && c > 0)
3089 // we don't use operator<< for single UCS4 character.
3090 // see explanation in docstream.h
3092 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3097 os << docstring(depth * 2, ' ');
3098 if (plaintextBottomHLine(os, r, clen))
3099 os << docstring(depth * 2, ' ');
3105 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3107 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3111 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3112 col_type column) const
3114 return cell_info[row][column].inset;
3118 void Tabular::setCellInset(row_type row, col_type column,
3119 shared_ptr<InsetTableCell> ins) const
3121 CellData & cd = cell_info[row][column];
3126 void Tabular::validate(LaTeXFeatures & features) const
3128 features.require("NeedTabularnewline");
3130 features.require("booktabs");
3131 if (is_long_tabular)
3132 features.require("longtable");
3134 features.require("rotating");
3135 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3136 if (isMultiRow(cell))
3137 features.require("multirow");
3138 if (getVAlignment(cell) != LYX_VALIGN_TOP
3139 || !getPWidth(cell).zero())
3140 features.require("array");
3141 cellInset(cell)->validate(features);
3146 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3148 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3149 ParagraphList::const_iterator cit = parlist.begin();
3150 ParagraphList::const_iterator end = parlist.end();
3152 for (; cit != end; ++cit)
3153 for (int i = 0; i < cit->size(); ++i)
3154 if (cit->isNewline(i))
3161 /////////////////////////////////////////////////////////////////////
3165 /////////////////////////////////////////////////////////////////////
3167 InsetTableCell::InsetTableCell(Buffer * buf)
3168 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3169 contentAlign(LYX_ALIGN_CENTER)
3173 bool InsetTableCell::forcePlainLayout(idx_type) const
3175 return !isFixedWidth;
3179 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3181 return isFixedWidth;
3185 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3186 FuncStatus & status) const
3189 switch (cmd.action()) {
3191 enabled = !forcePlainLayout();
3193 case LFUN_LAYOUT_PARAGRAPH:
3194 enabled = allowParagraphCustomization();
3197 return InsetText::getStatus(cur, cmd, status);
3199 status.setEnabled(enabled);
3203 docstring InsetTableCell::asString(bool intoInsets)
3206 if (paragraphs().empty())
3208 ParagraphList::const_iterator it = paragraphs().begin();
3209 ParagraphList::const_iterator en = paragraphs().end();
3211 for (; it != en; ++it) {
3216 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3222 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3225 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3226 return InsetText::xhtml(xs, rp);
3231 /////////////////////////////////////////////////////////////////////
3235 /////////////////////////////////////////////////////////////////////
3237 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3239 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3240 rowselect_(false), colselect_(false)
3245 InsetTabular::InsetTabular(InsetTabular const & tab)
3246 : Inset(tab), tabular(tab.tabular), scx_(0)
3251 InsetTabular::~InsetTabular()
3253 hideDialogs("tabular", this);
3257 void InsetTabular::setBuffer(Buffer & buf)
3259 tabular.setBuffer(buf);
3260 Inset::setBuffer(buf);
3264 bool InsetTabular::insetAllowed(InsetCode code) const
3269 case MATHMACRO_CODE:
3274 return tabular.is_long_tabular;
3282 void InsetTabular::write(ostream & os) const
3284 os << "Tabular" << endl;
3289 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3291 // FIXME: depending on the selection state, we could offer a different menu.
3292 return from_ascii("context-tabular");
3296 void InsetTabular::read(Lexer & lex)
3298 //bool const old_format = (lex.getString() == "\\LyXTable");
3306 string token = lex.getString();
3307 while (lex && token != "\\end_inset") {
3309 token = lex.getString();
3312 lex.printError("Missing \\end_inset at this point. ");
3316 int InsetTabular::rowFromY(Cursor & cur, int y) const
3318 // top y coordinate of tabular
3319 int h = yo(cur.bv()) - tabular.rowAscent(0);
3321 for (; r < tabular.nrows() && y > h; ++r)
3322 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3323 + tabular.interRowSpace(r);
3329 int InsetTabular::columnFromX(Cursor & cur, int x) const
3331 // left x coordinate of tabular
3332 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3334 for (; c < tabular.ncols() && x > w; ++c)
3335 w += tabular.cellWidth(c);
3340 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3342 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3343 // mi.base.textwidth << "\n";
3346 LASSERT(false, /**/);
3349 for (row_type r = 0; r < tabular.nrows(); ++r) {
3352 for (col_type c = 0; c < tabular.ncols(); ++c) {
3353 if (tabular.isPartOfMultiColumn(r, c)
3354 || tabular.isPartOfMultiRow(r, c))
3355 // multicolumn or multirow cell, but not first one
3357 idx_type const cell = tabular.cellIndex(r, c);
3360 Length const p_width = tabular.getPWidth(cell);
3361 if (!p_width.zero())
3362 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3363 tabular.cellInset(cell)->metrics(m, dim);
3364 if (!p_width.zero())
3365 dim.wid = m.base.textwidth;
3366 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3367 + tabular.interColumnSpace(cell);
3369 // FIXME(?): do we need a second metrics call?
3370 TextMetrics const & tm =
3371 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3373 // determine horiz offset because of decimal align (if necessary)
3374 int decimal_hoffset = 0;
3375 int decimal_width = 0;
3376 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3377 // make a copy which we will split in 2
3378 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3379 head.getText(0)->setMacrocontextPosition(
3380 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3381 head.setBuffer(tabular.buffer());
3382 // split in 2 and calculate width of each part
3383 bool hassep = false;
3384 InsetTableCell tail =
3385 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3387 head.metrics(m, dim1);
3388 decimal_hoffset = dim1.width();
3390 tail.metrics(m, dim1);
3391 decimal_width = dim1.width();
3394 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3395 tabular.cell_info[r][c].decimal_width = decimal_width;
3397 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3398 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3399 int const lastpardes = tm.last().second->descent()
3400 + TEXT_TO_INSET_OFFSET;
3402 switch (tabular.getVAlignment(cell)) {
3403 case Tabular::LYX_VALIGN_TOP:
3405 case Tabular::LYX_VALIGN_MIDDLE:
3406 offset = -(dim.des - lastpardes)/2;
3408 case Tabular::LYX_VALIGN_BOTTOM:
3409 offset = -(dim.des - lastpardes);
3412 tabular.cell_info[r][c].voffset = offset;
3413 maxasc = max(maxasc, dim.asc - offset);
3414 maxdes = max(maxdes, dim.des + offset);
3416 int const top_space = tabular.row_info[r].top_space_default ?
3417 default_line_space :
3418 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3419 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3420 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3421 default_line_space :
3422 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3423 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3425 tabular.updateColumnWidths();
3426 dim.asc = tabular.rowAscent(0);
3427 dim.des = tabular.height() - dim.asc;
3428 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3432 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3435 if (&cur.inset() == this && cur.selection()) {
3436 if (cur.selIsMultiCell()) {
3439 getSelection(cur, rs, re, cs, ce);
3441 idx_type const cell = tabular.cellIndex(row, col);
3442 col_type const cspan = tabular.columnSpan(cell);
3443 row_type const rspan = tabular.rowSpan(cell);
3444 if (col + cspan - 1 >= cs && col <= ce
3445 && row + rspan - 1 >= rs && row <= re)
3448 if (col == tabular.cellColumn(cur.idx())
3449 && row == tabular.cellRow(cur.idx())) {
3450 CursorSlice const & beg = cur.selBegin();
3451 CursorSlice const & end = cur.selEnd();
3453 if ((end.lastpos() > 0 || end.lastpit() > 0)
3454 && end.pos() == end.lastpos() && beg.pos() == 0
3455 && end.pit() == end.lastpit() && beg.pit() == 0)
3463 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3465 x += scx_ + ADD_TO_TABULAR_WIDTH;
3467 BufferView * bv = pi.base.bv;
3468 Cursor & cur = pi.base.bv->cursor();
3471 // FIXME: As the full background is painted in drawSelection(),
3472 // we have no choice but to do a full repaint for the Text cells.
3473 pi.full_repaint = true;
3475 bool const original_selection_state = pi.selected;
3478 first_visible_cell = Tabular::npos;
3479 for (row_type r = 0; r < tabular.nrows(); ++r) {
3481 for (col_type c = 0; c < tabular.ncols(); ++c) {
3482 if (tabular.isPartOfMultiColumn(r, c))
3485 idx = tabular.cellIndex(r, c);
3487 if (tabular.isPartOfMultiRow(r, c)) {
3488 nx += tabular.cellWidth(idx);
3492 if (first_visible_cell == Tabular::npos)
3493 first_visible_cell = idx;
3495 pi.selected |= isCellSelected(cur, r, c);
3496 int const cx = nx + tabular.textHOffset(idx);
3497 int const cy = y + tabular.textVOffset(idx);
3498 // Cache the Inset position.
3499 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3500 cell(idx)->draw(pi, cx, cy);
3501 drawCellLines(pi, nx, y, r, idx);
3502 nx += tabular.cellWidth(idx);
3503 pi.selected = original_selection_state;
3506 if (r + 1 < tabular.nrows())
3507 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3508 + tabular.interRowSpace(r + 1);
3513 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3515 Cursor & cur = pi.base.bv->cursor();
3518 x += scx_ + ADD_TO_TABULAR_WIDTH;
3520 // FIXME: it is wrong to completely paint the background
3521 // if we want to do single row painting.
3523 // Paint background of current tabular
3524 int const w = tabular.width();
3525 int const h = tabular.height();
3526 int yy = y - tabular.rowAscent(0);
3527 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3529 if (!cur.selection())
3531 if (&cur.inset() != this)
3536 bool const full_cell_selected = isCellSelected(cur,
3537 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3539 if (cur.selIsMultiCell() || full_cell_selected) {
3540 for (row_type r = 0; r < tabular.nrows(); ++r) {
3542 for (col_type c = 0; c < tabular.ncols(); ++c) {
3543 if (tabular.isPartOfMultiColumn(r, c))
3546 idx_type const cell = tabular.cellIndex(r, c);
3548 if (tabular.isPartOfMultiRow(r, c)) {
3549 xx += tabular.cellWidth(cell);
3552 int const w = tabular.cellWidth(cell);
3553 int const h = tabular.cellHeight(cell);
3554 int const yy = y - tabular.rowAscent(r);
3555 if (isCellSelected(cur, r, c))
3556 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3559 if (r + 1 < tabular.nrows())
3560 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3561 + tabular.interRowSpace(r + 1);
3565 x += cellXPos(cur.idx());
3566 x += tabular.textHOffset(cur.idx());
3567 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3572 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3573 row_type row, idx_type cell) const
3575 y -= tabular.rowAscent(row);
3576 int const w = tabular.cellWidth(cell);
3577 int const h = tabular.cellHeight(cell);
3578 Color const linecolor = pi.textColor(Color_tabularline);
3579 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3582 bool drawline = tabular.topLine(cell)
3583 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3584 pi.pain.line(x, y, x + w, y,
3585 drawline ? linecolor : gridcolor,
3586 drawline ? Painter::line_solid : Painter::line_onoffdash);
3589 drawline = tabular.bottomLine(cell);
3590 pi.pain.line(x, y + h, x + w, y + h,
3591 drawline ? linecolor : gridcolor,
3592 drawline ? Painter::line_solid : Painter::line_onoffdash);
3595 col_type const col = tabular.cellColumn(cell);
3596 drawline = tabular.leftLine(cell)
3597 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3598 pi.pain.line(x, y, x, y + h,
3599 drawline ? linecolor : gridcolor,
3600 drawline ? Painter::line_solid : Painter::line_onoffdash);
3603 x -= tabular.interColumnSpace(cell);
3604 drawline = tabular.rightLine(cell)
3605 || (col + 1 < tabular.ncols()
3606 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3607 pi.pain.line(x + w, y, x + w, y + h,
3608 drawline ? linecolor : gridcolor,
3609 drawline ? Painter::line_solid : Painter::line_onoffdash);
3613 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3615 //lyxerr << "InsetTabular::edit: " << this << endl;
3617 cur.setSelection(false);
3620 if (isRightToLeft(cur))
3621 cur.idx() = tabular.getLastCellInRow(0);
3627 if (isRightToLeft(cur))
3628 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3630 cur.idx() = tabular.numberofcells - 1;
3632 cur.pos() = cur.lastpos(); // FIXME crude guess
3634 cur.setCurrentFont();
3635 // FIXME: this accesses the position cache before it is initialized
3637 //cur.bv().fitCursor();
3641 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3643 // In a longtable, tell captions what the current float is
3644 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3645 string const saveflt = cnts.current_float();
3646 if (tabular.is_long_tabular)
3647 cnts.current_float("table");
3649 ParIterator it2 = it;
3651 size_t const end = it2.nargs();
3652 for ( ; it2.idx() < end; it2.top().forwardIdx())
3653 buffer().updateBuffer(it2, utype);
3656 if (tabular.is_long_tabular)
3657 cnts.current_float(saveflt);
3661 void InsetTabular::addToToc(DocIterator const & cpit)
3663 DocIterator dit = cpit;
3665 size_t const end = dit.nargs();
3666 for ( ; dit.idx() < end; dit.top().forwardIdx())
3667 cell(dit.idx())->addToToc(dit);
3671 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3673 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3674 << "\n cur:" << cur);
3675 CursorSlice sl = cur.top();
3676 Cursor & bvcur = cur.bv().cursor();
3678 FuncCode const act = cmd.action();
3682 case LFUN_MOUSE_PRESS: {
3683 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3685 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3686 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3687 row_type r = rowFromY(cur, cmd.y());
3688 cur.idx() = tabular.getFirstCellInRow(r);
3691 cur.idx() = tabular.getLastCellInRow(r);
3692 cur.pos() = cur.lastpos();
3693 cur.setSelection(true);
3699 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3700 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3701 || cmd.y() > y0 + tabular.height()) {
3702 col_type c = columnFromX(cur, cmd.x());
3703 cur.idx() = tabular.cellIndex(0, c);
3706 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3707 cur.pos() = cur.lastpos();
3708 cur.setSelection(true);
3713 // do not reset cursor/selection if we have selected
3714 // some cells (bug 2715).
3715 if (cmd.button() == mouse_button::button3
3716 && &bvcur.selBegin().inset() == this
3717 && bvcur.selIsMultiCell())
3720 // Let InsetTableCell do it
3721 cell(cur.idx())->dispatch(cur, cmd);
3724 case LFUN_MOUSE_MOTION:
3725 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3726 if (cmd.button() == mouse_button::button1) {
3727 // only accept motions to places not deeper nested than the real anchor
3728 if (!bvcur.realAnchor().hasPart(cur)) {
3732 // select (additional) row
3734 row_type r = rowFromY(cur, cmd.y());
3735 cur.idx() = tabular.getLastCellInRow(r);
3736 // we need to reset the cursor's pit and pos now, as the old ones
3737 // may no longer be valid.
3740 bvcur.setCursor(cur);
3741 bvcur.setSelection(true);
3744 // select (additional) column
3746 col_type c = columnFromX(cur, cmd.x());
3747 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3748 // we need to reset the cursor's pit and pos now, as the old ones
3749 // may no longer be valid.
3752 bvcur.setCursor(cur);
3753 bvcur.setSelection(true);
3756 // only update if selection changes
3757 if (bvcur.idx() == cur.idx() &&
3758 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3759 cur.noScreenUpdate();
3760 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3761 bvcur.setCursor(cur);
3762 bvcur.setSelection(true);
3763 // if this is a multicell selection, we just set the cursor to
3764 // the beginning of the cell's text.
3765 if (bvcur.selIsMultiCell()) {
3766 bvcur.pit() = bvcur.lastpit();
3767 bvcur.pos() = bvcur.lastpos();
3772 case LFUN_MOUSE_RELEASE:
3777 case LFUN_CELL_BACKWARD:
3779 cur.setSelection(false);
3782 case LFUN_CELL_FORWARD:
3784 cur.setSelection(false);
3787 case LFUN_CHAR_FORWARD_SELECT:
3788 case LFUN_CHAR_FORWARD:
3789 case LFUN_CHAR_BACKWARD_SELECT:
3790 case LFUN_CHAR_BACKWARD:
3791 case LFUN_CHAR_RIGHT_SELECT:
3792 case LFUN_CHAR_RIGHT:
3793 case LFUN_CHAR_LEFT_SELECT:
3794 case LFUN_CHAR_LEFT:
3795 case LFUN_WORD_FORWARD:
3796 case LFUN_WORD_FORWARD_SELECT:
3797 case LFUN_WORD_BACKWARD:
3798 case LFUN_WORD_BACKWARD_SELECT:
3799 case LFUN_WORD_RIGHT:
3800 case LFUN_WORD_RIGHT_SELECT:
3801 case LFUN_WORD_LEFT:
3802 case LFUN_WORD_LEFT_SELECT: {
3803 // determine whether we move to next or previous cell, where to enter
3804 // the new cell from, and which command to "finish" (i.e., exit the
3807 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3808 FuncCode finish_lfun;
3810 if (act == LFUN_CHAR_FORWARD
3811 || act == LFUN_CHAR_FORWARD_SELECT
3812 || act == LFUN_WORD_FORWARD
3813 || act == LFUN_WORD_FORWARD_SELECT) {
3815 finish_lfun = LFUN_FINISHED_FORWARD;
3817 else if (act == LFUN_CHAR_BACKWARD
3818 || act == LFUN_CHAR_BACKWARD_SELECT
3819 || act == LFUN_WORD_BACKWARD
3820 || act == LFUN_WORD_BACKWARD_SELECT) {
3822 finish_lfun = LFUN_FINISHED_BACKWARD;
3824 // LEFT or RIGHT commands --- the interpretation will depend on the
3825 // table's direction.
3827 bool const right = act == LFUN_CHAR_RIGHT
3828 || act == LFUN_CHAR_RIGHT_SELECT
3829 || act == LFUN_WORD_RIGHT
3830 || act == LFUN_WORD_RIGHT_SELECT;
3831 next_cell = isRightToLeft(cur) != right;
3833 if (lyxrc.visual_cursor)
3834 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3836 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3839 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3840 || act == LFUN_CHAR_BACKWARD_SELECT
3841 || act == LFUN_CHAR_RIGHT_SELECT
3842 || act == LFUN_CHAR_LEFT_SELECT
3843 || act == LFUN_WORD_FORWARD_SELECT
3844 || act == LFUN_WORD_RIGHT_SELECT
3845 || act == LFUN_WORD_BACKWARD_SELECT
3846 || act == LFUN_WORD_LEFT_SELECT;
3848 // If we have a multicell selection or we're
3849 // not doing some LFUN_*_SELECT thing anyway...
3850 if (!cur.selIsMultiCell() || !select) {
3851 col_type const c = tabular.cellColumn(cur.idx());
3852 row_type const r = tabular.cellRow(cur.idx());
3853 // Are we trying to select the whole cell and is the whole cell
3854 // not yet selected?
3855 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3856 ((next_cell && cur.pit() == cur.lastpit()
3857 && cur.pos() == cur.lastpos())
3858 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3860 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3862 // ...try to dispatch to the cell's inset.
3863 cell(cur.idx())->dispatch(cur, cmd);
3865 // When we already have a selection we want to select the whole cell
3866 // before going to the next cell.
3867 if (select_whole && !empty_cell){
3868 getText(cur.idx())->selectAll(cur);
3873 // FIXME: When we support the selection of an empty cell, remove
3874 // the !empty_cell from this condition. For now we jump to the next
3875 // cell if the current cell is empty.
3876 if (cur.result().dispatched() && !empty_cell)
3880 // move to next/prev cell, as appropriate
3881 // note that we will always do this if we're selecting and we have
3882 // a multicell selection
3883 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3884 << " cell from: " << int(entry_from));
3886 moveNextCell(cur, entry_from);
3888 movePrevCell(cur, entry_from);
3889 // if we're exiting the table, call the appropriate FINISHED lfun
3890 if (sl == cur.top())
3891 cmd = FuncRequest(finish_lfun);
3898 case LFUN_DOWN_SELECT:
3900 if (!(cur.selection() && cur.selIsMultiCell()))
3901 cell(cur.idx())->dispatch(cur, cmd);
3903 cur.dispatched(); // override the cell's decision
3904 if (sl == cur.top()) {
3905 // if our Text didn't do anything to the cursor
3906 // then we try to put the cursor into the cell below
3907 // setting also the right targetX.
3908 cur.selHandle(act == LFUN_DOWN_SELECT);
3909 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3910 cur.idx() = tabular.cellBelow(cur.idx());
3912 TextMetrics const & tm =
3913 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3914 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3915 cur.setCurrentFont();
3918 if (sl == cur.top()) {
3919 // we trick it to go to forward after leaving the
3921 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3924 if (cur.selIsMultiCell()) {
3925 cur.pit() = cur.lastpit();
3926 cur.pos() = cur.lastpos();
3927 cur.setCurrentFont();
3932 case LFUN_UP_SELECT:
3934 if (!(cur.selection() && cur.selIsMultiCell()))
3935 cell(cur.idx())->dispatch(cur, cmd);
3936 cur.dispatched(); // override the cell's decision
3937 if (sl == cur.top()) {
3938 // if our Text didn't do anything to the cursor
3939 // then we try to put the cursor into the cell above
3940 // setting also the right targetX.
3941 cur.selHandle(act == LFUN_UP_SELECT);
3942 if (tabular.cellRow(cur.idx()) != 0) {
3943 cur.idx() = tabular.cellAbove(cur.idx());
3944 cur.pit() = cur.lastpit();
3945 Text const * text = cell(cur.idx())->getText(0);
3946 TextMetrics const & tm = cur.bv().textMetrics(text);
3947 ParagraphMetrics const & pm =
3948 tm.parMetrics(cur.lastpit());
3949 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3950 cur.setCurrentFont();
3953 if (sl == cur.top()) {
3954 cmd = FuncRequest(LFUN_UP);
3957 if (cur.selIsMultiCell()) {
3959 cur.pos() = cur.lastpos();
3960 cur.setCurrentFont();
3965 // case LFUN_SCREEN_DOWN: {
3966 // //if (hasSelection())
3967 // // cur.selection() = false;
3968 // col_type const col = tabular.cellColumn(cur.idx());
3969 // int const t = cur.bv().top_y() + cur.bv().height();
3970 // if (t < yo() + tabular.getHeightOfTabular()) {
3971 // cur.bv().scrollDocView(t, true);
3972 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3974 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3981 // case LFUN_SCREEN_UP: {
3982 // //if (hasSelection())
3983 // // cur.selection() = false;
3984 // col_type const col = tabular.cellColumn(cur.idx());
3985 // int const t = cur.bv().top_y() + cur.bv().height();
3987 // cur.bv().scrollDocView(t, true);
3991 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3995 // cur.par() = cur.lastpar();
3996 // cur.pos() = cur.lastpos();
4000 case LFUN_LAYOUT_TABULAR:
4001 cur.bv().showDialog("tabular");
4004 case LFUN_INSET_MODIFY:
4005 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4009 // insert file functions
4010 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4011 case LFUN_FILE_INSERT_PLAINTEXT:
4013 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4014 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4015 FileName(to_utf8(cmd.argument())));
4018 cur.recordUndoInset(INSERT_UNDO);
4019 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4020 // content has been replaced,
4021 // so cursor might be invalid
4022 cur.pos() = cur.lastpos();
4023 cur.pit() = cur.lastpit();
4024 bvcur.setCursor(cur);
4031 if (cur.selIsMultiCell()) {
4032 if (copySelection(cur)) {
4033 cur.recordUndoInset(DELETE_UNDO);
4037 cell(cur.idx())->dispatch(cur, cmd);
4040 case LFUN_SELF_INSERT:
4041 if (cur.selIsMultiCell()) {
4042 cur.recordUndoInset(DELETE_UNDO);
4045 cell(cur.idx())->dispatch(cur, cmd);
4048 case LFUN_CHAR_DELETE_BACKWARD:
4049 case LFUN_CHAR_DELETE_FORWARD:
4050 if (cur.selIsMultiCell()) {
4051 cur.recordUndoInset(DELETE_UNDO);
4054 cell(cur.idx())->dispatch(cur, cmd);
4058 if (!cur.selection())
4060 if (cur.selIsMultiCell()) {
4064 cell(cur.idx())->dispatch(cur, cmd);
4067 case LFUN_CLIPBOARD_PASTE:
4068 case LFUN_PRIMARY_SELECTION_PASTE: {
4069 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4070 theClipboard().getAsText() :
4071 theSelection().get();
4074 // pass to InsertPlaintextString, but
4075 // only if we have multi-cell content
4076 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4077 cur.recordUndoInset(INSERT_UNDO);
4078 if (insertPlaintextString(cur.bv(), clip, false)) {
4079 // content has been replaced,
4080 // so cursor might be invalid
4081 cur.pos() = cur.lastpos();
4082 cur.pit() = cur.lastpit();
4083 bvcur.setCursor(cur);
4087 // Let the cell handle normal text
4088 cell(cur.idx())->dispatch(cur, cmd);
4093 if (!tabularStackDirty()) {
4094 if (!cur.selIsMultiCell())
4095 cell(cur.idx())->dispatch(cur, cmd);
4098 if (theClipboard().isInternal() ||
4099 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4100 cur.recordUndoInset(INSERT_UNDO);
4101 pasteClipboard(cur);
4105 case LFUN_FONT_EMPH:
4106 case LFUN_FONT_BOLD:
4107 case LFUN_FONT_BOLDSYMBOL:
4108 case LFUN_FONT_ROMAN:
4109 case LFUN_FONT_NOUN:
4110 case LFUN_FONT_ITAL:
4111 case LFUN_FONT_FRAK:
4112 case LFUN_FONT_TYPEWRITER:
4113 case LFUN_FONT_SANS:
4114 case LFUN_TEXTSTYLE_APPLY:
4115 case LFUN_TEXTSTYLE_UPDATE:
4116 case LFUN_FONT_SIZE:
4117 case LFUN_FONT_UNDERLINE:
4118 case LFUN_FONT_STRIKEOUT:
4119 case LFUN_FONT_UULINE:
4120 case LFUN_FONT_UWAVE:
4122 case LFUN_WORD_CAPITALIZE:
4123 case LFUN_WORD_UPCASE:
4124 case LFUN_WORD_LOWCASE:
4125 case LFUN_CHARS_TRANSPOSE:
4126 if (cur.selIsMultiCell()) {
4129 getSelection(cur, rs, re, cs, ce);
4130 Cursor tmpcur = cur;
4131 for (row_type r = rs; r <= re; ++r) {
4132 for (col_type c = cs; c <= ce; ++c) {
4133 // cursor follows cell:
4134 tmpcur.idx() = tabular.cellIndex(r, c);
4135 // select this cell only:
4138 tmpcur.resetAnchor();
4139 tmpcur.pit() = tmpcur.lastpit();
4140 tmpcur.pos() = tmpcur.top().lastpos();
4141 tmpcur.setCursor(tmpcur);
4142 tmpcur.setSelection();
4143 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4148 cell(cur.idx())->dispatch(cur, cmd);
4152 case LFUN_INSET_SETTINGS:
4153 // relay this lfun to Inset, not to the cell.
4154 Inset::doDispatch(cur, cmd);
4158 // we try to handle this event in the insets dispatch function.
4159 cell(cur.idx())->dispatch(cur, cmd);
4165 // function sets an object as defined in func_status.h:
4166 // states OK, Unknown, Disabled, On, Off.
4167 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4168 FuncStatus & status) const
4170 switch (cmd.action()) {
4171 case LFUN_INSET_MODIFY: {
4172 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4175 string const s = cmd.getArg(1);
4176 // FIXME: We only check for the very first argument...
4177 int action = Tabular::LAST_ACTION;
4179 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4180 if (tabularFeature[i].feature == s) {
4181 action = tabularFeature[i].action;
4185 if (action == Tabular::LAST_ACTION) {
4187 status.setUnknown(true);
4191 string const argument = cmd.getLongArg(2);
4193 row_type sel_row_start = 0;
4194 row_type sel_row_end = 0;
4195 col_type sel_col_start = 0;
4196 col_type sel_col_end = 0;
4197 Tabular::ltType dummyltt;
4200 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4203 case Tabular::SET_PWIDTH:
4204 case Tabular::SET_MPWIDTH:
4205 case Tabular::SET_SPECIAL_COLUMN:
4206 case Tabular::SET_SPECIAL_MULTICOLUMN:
4207 case Tabular::APPEND_ROW:
4208 case Tabular::APPEND_COLUMN:
4209 case Tabular::DELETE_ROW:
4210 case Tabular::DELETE_COLUMN:
4211 case Tabular::COPY_ROW:
4212 case Tabular::COPY_COLUMN:
4213 case Tabular::SET_TOP_SPACE:
4214 case Tabular::SET_BOTTOM_SPACE:
4215 case Tabular::SET_INTERLINE_SPACE:
4219 case Tabular::SET_DECIMAL_POINT:
4221 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4224 case Tabular::SET_MULTICOLUMN:
4225 case Tabular::UNSET_MULTICOLUMN:
4226 case Tabular::MULTICOLUMN:
4227 // If a row is set as longtable caption, it must not be allowed
4228 // to unset that this row is a multicolumn.
4229 status.setEnabled(sel_row_start == sel_row_end
4230 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4231 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4234 case Tabular::SET_MULTIROW:
4235 case Tabular::UNSET_MULTIROW:
4236 case Tabular::MULTIROW:
4237 // If a row is set as longtable caption, it must not be allowed
4238 // to unset that this row is a multirow.
4239 status.setEnabled(sel_col_start == sel_col_end
4240 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4241 status.setOnOff(tabular.isMultiRow(cur.idx()));
4244 case Tabular::SET_ALL_LINES:
4245 case Tabular::UNSET_ALL_LINES:
4246 case Tabular::SET_BORDER_LINES:
4247 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4250 case Tabular::SET_LINE_TOP:
4251 case Tabular::SET_LINE_BOTTOM:
4252 case Tabular::SET_LINE_LEFT:
4253 case Tabular::SET_LINE_RIGHT:
4254 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4257 case Tabular::TOGGLE_LINE_TOP:
4258 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4259 status.setOnOff(tabular.topLine(cur.idx()));
4262 case Tabular::TOGGLE_LINE_BOTTOM:
4263 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4264 status.setOnOff(tabular.bottomLine(cur.idx()));
4267 case Tabular::TOGGLE_LINE_LEFT:
4268 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4269 status.setOnOff(tabular.leftLine(cur.idx()));
4272 case Tabular::TOGGLE_LINE_RIGHT:
4273 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4274 status.setOnOff(tabular.rightLine(cur.idx()));
4277 case Tabular::M_ALIGN_LEFT:
4279 case Tabular::ALIGN_LEFT:
4280 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4281 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4284 case Tabular::M_ALIGN_RIGHT:
4286 case Tabular::ALIGN_RIGHT:
4287 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4288 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4291 case Tabular::M_ALIGN_CENTER:
4293 case Tabular::ALIGN_CENTER:
4294 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4297 case Tabular::ALIGN_BLOCK:
4298 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4299 && !tabular.isMultiRow(cur.idx()));
4300 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4303 case Tabular::ALIGN_DECIMAL:
4304 status.setEnabled(!tabular.isMultiRow(cur.idx())
4305 && !tabular.isMultiColumn(cur.idx()));
4306 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4309 case Tabular::M_VALIGN_TOP:
4311 case Tabular::VALIGN_TOP:
4312 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4313 && !tabular.isMultiRow(cur.idx()));
4315 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4318 case Tabular::M_VALIGN_BOTTOM:
4320 case Tabular::VALIGN_BOTTOM:
4321 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4322 && !tabular.isMultiRow(cur.idx()));
4324 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4327 case Tabular::M_VALIGN_MIDDLE:
4329 case Tabular::VALIGN_MIDDLE:
4330 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4331 && !tabular.isMultiRow(cur.idx()));
4333 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4336 case Tabular::SET_LONGTABULAR:
4337 // setting as longtable is not allowed when table is inside a float
4338 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4339 || cur.innerInsetOfType(WRAP_CODE) != 0)
4340 status.setEnabled(false);
4342 status.setEnabled(true);
4343 status.setOnOff(tabular.is_long_tabular);
4346 case Tabular::UNSET_LONGTABULAR:
4347 status.setOnOff(!tabular.is_long_tabular);
4350 case Tabular::TOGGLE_ROTATE_TABULAR:
4351 case Tabular::SET_ROTATE_TABULAR:
4352 status.setOnOff(tabular.rotate);
4355 case Tabular::TABULAR_VALIGN_TOP:
4356 status.setOnOff(tabular.tabular_valignment
4357 == Tabular::LYX_VALIGN_TOP);
4359 case Tabular::TABULAR_VALIGN_MIDDLE:
4360 status.setOnOff(tabular.tabular_valignment
4361 == Tabular::LYX_VALIGN_MIDDLE);
4363 case Tabular::TABULAR_VALIGN_BOTTOM:
4364 status.setOnOff(tabular.tabular_valignment
4365 == Tabular::LYX_VALIGN_BOTTOM);
4368 case Tabular::LONGTABULAR_ALIGN_LEFT:
4369 status.setOnOff(tabular.longtabular_alignment
4370 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4372 case Tabular::LONGTABULAR_ALIGN_CENTER:
4373 status.setOnOff(tabular.longtabular_alignment
4374 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4376 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4377 status.setOnOff(tabular.longtabular_alignment
4378 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4381 case Tabular::UNSET_ROTATE_TABULAR:
4382 status.setOnOff(!tabular.rotate);
4385 case Tabular::TOGGLE_ROTATE_CELL:
4386 case Tabular::SET_ROTATE_CELL:
4387 status.setOnOff(!oneCellHasRotationState(false,
4388 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4391 case Tabular::UNSET_ROTATE_CELL:
4392 status.setOnOff(!oneCellHasRotationState(true,
4393 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4396 case Tabular::SET_USEBOX:
4397 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4400 // every row can only be one thing:
4401 // either a footer or header or caption
4402 case Tabular::SET_LTFIRSTHEAD:
4403 status.setEnabled(sel_row_start == sel_row_end
4404 && !tabular.ltCaption(sel_row_start));
4405 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4408 case Tabular::UNSET_LTFIRSTHEAD:
4409 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4412 case Tabular::SET_LTHEAD:
4413 status.setEnabled(sel_row_start == sel_row_end
4414 && !tabular.ltCaption(sel_row_start));
4415 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4418 case Tabular::UNSET_LTHEAD:
4419 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4422 case Tabular::SET_LTFOOT:
4423 status.setEnabled(sel_row_start == sel_row_end
4424 && !tabular.ltCaption(sel_row_start));
4425 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4428 case Tabular::UNSET_LTFOOT:
4429 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4432 case Tabular::SET_LTLASTFOOT:
4433 status.setEnabled(sel_row_start == sel_row_end
4434 && !tabular.ltCaption(sel_row_start));
4435 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4438 case Tabular::UNSET_LTLASTFOOT:
4439 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4442 case Tabular::SET_LTNEWPAGE:
4443 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4446 // only one row can be the caption
4447 // and a multirow cannot be set as caption
4448 case Tabular::SET_LTCAPTION:
4449 case Tabular::UNSET_LTCAPTION:
4450 case Tabular::TOGGLE_LTCAPTION:
4451 status.setEnabled(sel_row_start == sel_row_end
4452 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4453 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4454 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4455 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4456 && (!tabular.haveLTCaption()
4457 || tabular.ltCaption(sel_row_start))
4458 && !tabular.isMultiRow(sel_row_start));
4459 status.setOnOff(tabular.ltCaption(sel_row_start));
4462 case Tabular::SET_BOOKTABS:
4463 status.setOnOff(tabular.use_booktabs);
4466 case Tabular::UNSET_BOOKTABS:
4467 status.setOnOff(!tabular.use_booktabs);
4472 status.setEnabled(false);
4478 // These are only enabled inside tabular
4479 case LFUN_CELL_BACKWARD:
4480 case LFUN_CELL_FORWARD:
4481 status.setEnabled(true);
4484 // disable these with multiple cells selected
4485 case LFUN_INSET_INSERT:
4486 case LFUN_TABULAR_INSERT:
4487 case LFUN_FLEX_INSERT:
4488 case LFUN_FLOAT_INSERT:
4489 case LFUN_FLOAT_WIDE_INSERT:
4490 case LFUN_FOOTNOTE_INSERT:
4491 case LFUN_MARGINALNOTE_INSERT:
4492 case LFUN_MATH_INSERT:
4493 case LFUN_MATH_MODE:
4494 case LFUN_MATH_MUTATE:
4495 case LFUN_MATH_DISPLAY:
4496 case LFUN_NOTE_INSERT:
4497 case LFUN_ARGUMENT_INSERT:
4498 case LFUN_BOX_INSERT:
4499 case LFUN_BRANCH_INSERT:
4500 case LFUN_PHANTOM_INSERT:
4501 case LFUN_WRAP_INSERT:
4502 case LFUN_PREVIEW_INSERT:
4503 case LFUN_ERT_INSERT: {
4504 if (cur.selIsMultiCell()) {
4505 status.setEnabled(false);
4508 return cell(cur.idx())->getStatus(cur, cmd, status);
4511 // disable in non-fixed-width cells
4512 case LFUN_NEWLINE_INSERT:
4513 case LFUN_BREAK_PARAGRAPH: {
4514 if (tabular.getPWidth(cur.idx()).zero()) {
4515 status.setEnabled(false);
4518 return cell(cur.idx())->getStatus(cur, cmd, status);
4521 case LFUN_NEWPAGE_INSERT:
4522 status.setEnabled(false);
4526 if (tabularStackDirty() && theClipboard().isInternal()) {
4527 if (cur.selIsMultiCell()) {
4530 getSelection(cur, rs, re, cs, ce);
4531 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4532 && paste_tabular->nrows() == re - rs + 1)
4533 status.setEnabled(true);
4535 status.setEnabled(false);
4536 status.message(_("Selection size should match clipboard content."));
4539 status.setEnabled(true);
4542 return cell(cur.idx())->getStatus(cur, cmd, status);
4544 case LFUN_INSET_SETTINGS:
4545 // relay this lfun to Inset, not to the cell.
4546 return Inset::getStatus(cur, cmd, status);
4549 // we try to handle this event in the insets dispatch function.
4550 return cell(cur.idx())->getStatus(cur, cmd, status);
4556 Inset::DisplayType InsetTabular::display() const
4558 if (tabular.is_long_tabular) {
4559 switch (tabular.longtabular_alignment) {
4560 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4562 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4564 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4574 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4576 return tabular.latex(os, runparams);
4580 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4582 os << '\n'; // output table on a new line
4583 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4584 tabular.plaintext(os, runparams, dp, false, 0);
4585 return PLAINTEXT_NEWLINE;
4589 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4594 // FIXME: Why not pass a proper DocIterator here?
4596 // if the table is inside a float it doesn't need the informaltable
4597 // wrapper. Search for it.
4598 for (master = owner(); master; master = master->owner())
4599 if (master->lyxCode() == FLOAT_CODE)
4604 os << "<informaltable>";
4607 ret += tabular.docbook(os, runparams);
4609 os << "</informaltable>";
4616 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4619 // It'd be better to be able to get this from an InsetLayout, but at present
4620 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4621 xs << html::StartTag("table");
4622 docstring ret = tabular.xhtml(xs, rp);
4623 xs << html::EndTag("table");
4628 void InsetTabular::validate(LaTeXFeatures & features) const
4630 tabular.validate(features);
4632 // It'd be better to be able to get this from an InsetLayout, but at present
4633 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4634 if (features.runparams().flavor == OutputParams::HTML)
4635 features.addPreambleSnippet("<style type=\"text/css\">\n"
4636 "table { border: 1px solid black; display: inline-block; }\n"
4637 "td { border: 1px solid black; padding: 0.5ex; }\n"
4642 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4644 return tabular.cellInset(idx);
4648 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4650 return tabular.cellInset(idx);
4654 void InsetTabular::cursorPos(BufferView const & bv,
4655 CursorSlice const & sl, bool boundary, int & x, int & y) const
4657 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4659 int const row = tabular.cellRow(sl.idx());
4660 int const col = tabular.cellColumn(sl.idx());
4662 // y offset correction
4663 for (int r = 0; r < row; ++r)
4664 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4665 + tabular.interRowSpace(r + 1);
4667 y += tabular.textVOffset(sl.idx());
4669 // x offset correction
4670 for (int c = 0; c < col; ++c)
4671 x += tabular.column_info[c].width;
4673 x += tabular.textHOffset(sl.idx());
4674 x += ADD_TO_TABULAR_WIDTH;
4679 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4683 Inset const & inset = *tabular.cellInset(cell);
4684 Point o = bv.coordCache().getInsets().xy(&inset);
4685 int const xbeg = o.x_ - tabular.textHOffset(cell);
4686 int const xend = xbeg + tabular.cellWidth(cell);
4687 row_type const row = tabular.cellRow(cell);
4688 int const ybeg = o.y_ - tabular.rowAscent(row)
4689 - tabular.interRowSpace(row);
4690 int const yend = ybeg + tabular.cellHeight(cell);
4702 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4703 // << " ybeg=" << ybeg << " yend=" << yend
4704 // << " xx=" << xx << " yy=" << yy
4705 // << " dist=" << xx + yy << endl;
4710 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4712 //lyxerr << "InsetTabular::editXY: " << this << endl;
4713 cur.setSelection(false);
4715 cur.idx() = getNearestCell(cur.bv(), x, y);
4717 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4721 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4723 cur.idx() = getNearestCell(cur.bv(), x, y);
4724 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4728 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4730 idx_type idx_min = 0;
4731 int dist_min = numeric_limits<int>::max();
4732 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4733 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4734 int const d = dist(bv, i, x, y);
4745 int InsetTabular::cellXPos(idx_type const cell) const
4747 col_type col = tabular.cellColumn(cell);
4749 for (col_type c = 0; c < col; ++c)
4750 lx += tabular.column_info[c].width;
4756 void InsetTabular::resetPos(Cursor & cur) const
4758 BufferView & bv = cur.bv();
4759 int const maxwidth = bv.workWidth();
4761 int const scx_old = scx_;
4762 int const i = cur.find(this);
4767 int const X2 = maxwidth;
4768 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4769 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4770 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4773 scx_ = X1 + 20 - x1;
4775 scx_ = X2 - 20 - x2;
4780 // only update if offset changed
4781 if (scx_ != scx_old)
4782 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4786 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4788 row_type const row = tabular.cellRow(cur.idx());
4789 col_type const col = tabular.cellColumn(cur.idx());
4791 if (isRightToLeft(cur)) {
4792 if (tabular.cellColumn(cur.idx()) == 0) {
4793 if (row == tabular.nrows() - 1)
4795 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4800 cur.idx() = tabular.getLastCellInRow(row - 1);
4802 cur.idx() = tabular.cellIndex(row, col - 1);
4805 if (tabular.isLastCell(cur.idx()))
4807 if (cur.idx() == tabular.getLastCellInRow(row))
4808 cur.idx() = tabular.cellIndex(row + 1, 0);
4810 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4811 cur.idx() = tabular.cellIndex(row, colnextcell);
4815 cur.boundary(false);
4817 if (cur.selIsMultiCell()) {
4818 cur.pit() = cur.lastpit();
4819 cur.pos() = cur.lastpos();
4827 // in visual mode, place cursor at extreme left or right
4829 switch(entry_from) {
4831 case ENTRY_DIRECTION_RIGHT:
4832 cur.posVisToRowExtremity(false /* !left */);
4834 case ENTRY_DIRECTION_LEFT:
4835 cur.posVisToRowExtremity(true /* left */);
4837 case ENTRY_DIRECTION_IGNORE:
4838 // nothing to do in this case
4842 cur.setCurrentFont();
4847 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4849 row_type const row = tabular.cellRow(cur.idx());
4850 col_type const col = tabular.cellColumn(cur.idx());
4852 if (isRightToLeft(cur)) {
4853 if (cur.idx() == tabular.getLastCellInRow(row)) {
4856 cur.idx() = tabular.getFirstCellInRow(row);
4857 cur.idx() = tabular.cellAbove(cur.idx());
4859 if (tabular.isLastCell(cur.idx()))
4861 if (cur.idx() == tabular.getLastCellInRow(row))
4862 cur.idx() = tabular.cellIndex(row + 1, 0);
4864 cur.idx() = tabular.cellIndex(row, col + 1);
4867 if (cur.idx() == 0) // first cell
4870 cur.idx() = tabular.getLastCellInRow(row - 1);
4872 cur.idx() = tabular.cellIndex(row, col - 1);
4875 if (cur.selIsMultiCell()) {
4876 cur.pit() = cur.lastpit();
4877 cur.pos() = cur.lastpos();
4882 cur.pit() = cur.lastpit();
4883 cur.pos() = cur.lastpos();
4885 // in visual mode, place cursor at extreme left or right
4887 switch(entry_from) {
4889 case ENTRY_DIRECTION_RIGHT:
4890 cur.posVisToRowExtremity(false /* !left */);
4892 case ENTRY_DIRECTION_LEFT:
4893 cur.posVisToRowExtremity(true /* left */);
4895 case ENTRY_DIRECTION_IGNORE:
4896 // nothing to do in this case
4900 cur.setCurrentFont();
4905 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4907 istringstream is(argument);
4910 if (insetCode(s) != TABULAR_CODE)
4914 size_t safe_guard = 0;
4919 if (safe_guard > 1000) {
4920 LYXERR0("parameter max count reached!");
4924 Tabular::Feature action = Tabular::LAST_ACTION;
4927 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4928 if (s != tabularFeature[i].feature)
4931 action = tabularFeature[i].action;
4934 if (action == Tabular::LAST_ACTION) {
4935 LYXERR0("Feature not found " << s);
4939 if (tabularFeature[i].need_value)
4941 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4942 tabularFeatures(cur, action, val);
4948 static void checkLongtableSpecial(Tabular::ltType & ltt,
4949 string const & special, bool & flag)
4951 if (special == "dl_above") {
4954 } else if (special == "dl_below") {
4955 ltt.bottomDL = flag;
4957 } else if (special == "empty") {
4967 bool InsetTabular::oneCellHasRotationState(bool rotated,
4968 row_type row_start, row_type row_end,
4969 col_type col_start, col_type col_end) const
4971 for (row_type r = row_start; r <= row_end; ++r)
4972 for (col_type c = col_start; c <= col_end; ++c)
4973 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4979 void InsetTabular::tabularFeatures(Cursor & cur,
4980 Tabular::Feature feature, string const & value)
4982 col_type sel_col_start;
4983 col_type sel_col_end;
4984 row_type sel_row_start;
4985 row_type sel_row_end;
4986 bool setLines = false;
4987 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4988 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4992 case Tabular::M_ALIGN_LEFT:
4993 case Tabular::ALIGN_LEFT:
4994 setAlign = LYX_ALIGN_LEFT;
4997 case Tabular::M_ALIGN_RIGHT:
4998 case Tabular::ALIGN_RIGHT:
4999 setAlign = LYX_ALIGN_RIGHT;
5002 case Tabular::M_ALIGN_CENTER:
5003 case Tabular::ALIGN_CENTER:
5004 setAlign = LYX_ALIGN_CENTER;
5007 case Tabular::ALIGN_BLOCK:
5008 setAlign = LYX_ALIGN_BLOCK;
5011 case Tabular::ALIGN_DECIMAL:
5012 setAlign = LYX_ALIGN_DECIMAL;
5015 case Tabular::M_VALIGN_TOP:
5016 case Tabular::VALIGN_TOP:
5017 setVAlign = Tabular::LYX_VALIGN_TOP;
5020 case Tabular::M_VALIGN_BOTTOM:
5021 case Tabular::VALIGN_BOTTOM:
5022 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5025 case Tabular::M_VALIGN_MIDDLE:
5026 case Tabular::VALIGN_MIDDLE:
5027 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5034 cur.recordUndoInset(ATOMIC_UNDO);
5036 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5037 row_type const row = tabular.cellRow(cur.idx());
5038 col_type const column = tabular.cellColumn(cur.idx());
5040 Tabular::ltType ltt;
5044 case Tabular::SET_PWIDTH: {
5045 Length const len(value);
5046 tabular.setColumnPWidth(cur, cur.idx(), len);
5048 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5049 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5053 case Tabular::SET_MPWIDTH:
5054 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5057 case Tabular::SET_SPECIAL_COLUMN:
5058 case Tabular::SET_SPECIAL_MULTICOLUMN:
5059 if (value == "none")
5060 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5062 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5065 case Tabular::APPEND_ROW:
5066 // append the row into the tabular
5067 tabular.appendRow(cur.idx());
5070 case Tabular::APPEND_COLUMN:
5071 // append the column into the tabular
5072 tabular.appendColumn(cur.idx());
5073 cur.idx() = tabular.cellIndex(row, column);
5076 case Tabular::DELETE_ROW:
5077 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5078 tabular.deleteRow(sel_row_start);
5079 if (sel_row_start >= tabular.nrows())
5081 cur.idx() = tabular.cellIndex(sel_row_start, column);
5084 cur.setSelection(false);
5087 case Tabular::DELETE_COLUMN:
5088 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5089 tabular.deleteColumn(sel_col_start);
5090 if (sel_col_start >= tabular.ncols())
5092 cur.idx() = tabular.cellIndex(row, sel_col_start);
5095 cur.setSelection(false);
5098 case Tabular::COPY_ROW:
5099 tabular.copyRow(row);
5102 case Tabular::COPY_COLUMN:
5103 tabular.copyColumn(column);
5104 cur.idx() = tabular.cellIndex(row, column);
5107 case Tabular::SET_LINE_TOP:
5108 case Tabular::TOGGLE_LINE_TOP: {
5109 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5110 ? (value == "true") : !tabular.topLine(cur.idx());
5111 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5112 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5113 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5117 case Tabular::SET_LINE_BOTTOM:
5118 case Tabular::TOGGLE_LINE_BOTTOM: {
5119 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5120 ? (value == "true") : !tabular.bottomLine(cur.idx());
5121 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5122 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5123 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5127 case Tabular::SET_LINE_LEFT:
5128 case Tabular::TOGGLE_LINE_LEFT: {
5129 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5130 ? (value == "true") : !tabular.leftLine(cur.idx());
5131 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5132 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5133 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5137 case Tabular::SET_LINE_RIGHT:
5138 case Tabular::TOGGLE_LINE_RIGHT: {
5139 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5140 ? (value == "true") : !tabular.rightLine(cur.idx());
5141 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5142 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5143 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5147 case Tabular::M_ALIGN_LEFT:
5148 case Tabular::M_ALIGN_RIGHT:
5149 case Tabular::M_ALIGN_CENTER:
5151 case Tabular::ALIGN_LEFT:
5152 case Tabular::ALIGN_RIGHT:
5153 case Tabular::ALIGN_CENTER:
5154 case Tabular::ALIGN_BLOCK:
5155 case Tabular::ALIGN_DECIMAL:
5156 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5157 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5158 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5161 case Tabular::M_VALIGN_TOP:
5162 case Tabular::M_VALIGN_BOTTOM:
5163 case Tabular::M_VALIGN_MIDDLE:
5165 case Tabular::VALIGN_TOP:
5166 case Tabular::VALIGN_BOTTOM:
5167 case Tabular::VALIGN_MIDDLE:
5168 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5169 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5170 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5173 case Tabular::SET_MULTICOLUMN: {
5174 if (!cur.selection()) {
5175 // just multicol for one single cell
5176 // check whether we are completely in a multicol
5177 if (!tabular.isMultiColumn(cur.idx()))
5178 tabular.setMultiColumn(cur.idx(), 1);
5181 // we have a selection so this means we just add all this
5182 // cells to form a multicolumn cell
5183 idx_type const s_start = cur.selBegin().idx();
5184 row_type const col_start = tabular.cellColumn(s_start);
5185 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5186 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5189 cur.setSelection(false);
5193 case Tabular::UNSET_MULTICOLUMN: {
5194 if (!cur.selection()) {
5195 if (tabular.isMultiColumn(cur.idx()))
5196 tabular.unsetMultiColumn(cur.idx());
5201 case Tabular::MULTICOLUMN: {
5202 if (tabular.isMultiColumn(cur.idx()))
5203 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5205 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5209 case Tabular::SET_MULTIROW: {
5210 if (!cur.selection()) {
5211 // just multirow for one single cell
5212 // check whether we are completely in a multirow
5213 if (!tabular.isMultiRow(cur.idx()))
5214 tabular.setMultiRow(cur.idx(), 1);
5217 // we have a selection so this means we just add all this
5218 // cells to form a multirow cell
5219 idx_type const s_start = cur.selBegin().idx();
5220 row_type const row_start = tabular.cellRow(s_start);
5221 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5222 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5225 cur.setSelection(false);
5229 case Tabular::UNSET_MULTIROW: {
5230 if (!cur.selection()) {
5231 if (tabular.isMultiRow(cur.idx()))
5232 tabular.unsetMultiRow(cur.idx());
5237 case Tabular::MULTIROW: {
5238 if (tabular.isMultiRow(cur.idx()))
5239 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5241 tabularFeatures(cur, Tabular::SET_MULTIROW);
5245 case Tabular::SET_ALL_LINES:
5247 case Tabular::UNSET_ALL_LINES:
5248 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5249 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5250 idx_type const cell = tabular.cellIndex(r, c);
5251 tabular.setTopLine(cell, setLines);
5252 tabular.setBottomLine(cell, setLines);
5253 tabular.setRightLine(cell, setLines);
5254 tabular.setLeftLine(cell, setLines);
5258 case Tabular::SET_BORDER_LINES:
5259 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5260 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5261 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5263 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5264 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5265 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5269 case Tabular::SET_LONGTABULAR:
5270 tabular.is_long_tabular = true;
5273 case Tabular::UNSET_LONGTABULAR:
5274 for (row_type r = 0; r < tabular.nrows(); ++r) {
5275 if (tabular.ltCaption(r)) {
5276 cur.idx() = tabular.cellIndex(r, 0);
5279 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5282 tabular.is_long_tabular = false;
5285 case Tabular::SET_ROTATE_TABULAR:
5286 tabular.rotate = true;
5289 case Tabular::UNSET_ROTATE_TABULAR:
5290 tabular.rotate = false;
5293 case Tabular::TOGGLE_ROTATE_TABULAR:
5294 tabular.rotate = !tabular.rotate;
5297 case Tabular::TABULAR_VALIGN_TOP:
5298 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5301 case Tabular::TABULAR_VALIGN_MIDDLE:
5302 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5305 case Tabular::TABULAR_VALIGN_BOTTOM:
5306 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5309 case Tabular::LONGTABULAR_ALIGN_LEFT:
5310 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5313 case Tabular::LONGTABULAR_ALIGN_CENTER:
5314 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5317 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5318 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5323 case Tabular::SET_ROTATE_CELL:
5324 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5325 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5326 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5329 case Tabular::UNSET_ROTATE_CELL:
5330 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5331 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5332 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5335 case Tabular::TOGGLE_ROTATE_CELL:
5337 bool oneNotRotated = oneCellHasRotationState(false,
5338 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5340 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5341 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5342 tabular.setRotateCell(tabular.cellIndex(r, c),
5347 case Tabular::SET_USEBOX: {
5348 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5349 if (val == tabular.getUsebox(cur.idx()))
5350 val = Tabular::BOX_NONE;
5351 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5352 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5353 tabular.setUsebox(tabular.cellIndex(r, c), val);
5357 case Tabular::UNSET_LTFIRSTHEAD:
5359 case Tabular::SET_LTFIRSTHEAD:
5360 tabular.getRowOfLTFirstHead(row, ltt);
5361 checkLongtableSpecial(ltt, value, flag);
5362 tabular.setLTHead(row, flag, ltt, true);
5365 case Tabular::UNSET_LTHEAD:
5367 case Tabular::SET_LTHEAD:
5368 tabular.getRowOfLTHead(row, ltt);
5369 checkLongtableSpecial(ltt, value, flag);
5370 tabular.setLTHead(row, flag, ltt, false);
5373 case Tabular::UNSET_LTFOOT:
5375 case Tabular::SET_LTFOOT:
5376 tabular.getRowOfLTFoot(row, ltt);
5377 checkLongtableSpecial(ltt, value, flag);
5378 tabular.setLTFoot(row, flag, ltt, false);
5381 case Tabular::UNSET_LTLASTFOOT:
5383 case Tabular::SET_LTLASTFOOT:
5384 tabular.getRowOfLTLastFoot(row, ltt);
5385 checkLongtableSpecial(ltt, value, flag);
5386 tabular.setLTFoot(row, flag, ltt, true);
5389 case Tabular::SET_LTNEWPAGE:
5390 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5393 case Tabular::SET_LTCAPTION: {
5394 if (tabular.ltCaption(row))
5396 cur.idx() = tabular.setLTCaption(row, true);
5399 cur.setSelection(false);
5400 // If a row is set as caption, then also insert
5401 // a caption. Otherwise the LaTeX output is broken.
5402 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5403 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5407 case Tabular::UNSET_LTCAPTION: {
5408 if (!tabular.ltCaption(row))
5410 cur.idx() = tabular.setLTCaption(row, false);
5413 cur.setSelection(false);
5414 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5415 if (lyx::getStatus(fr).enabled())
5420 case Tabular::TOGGLE_LTCAPTION: {
5421 if (tabular.ltCaption(row))
5422 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5424 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5428 case Tabular::SET_BOOKTABS:
5429 tabular.use_booktabs = true;
5432 case Tabular::UNSET_BOOKTABS:
5433 tabular.use_booktabs = false;
5436 case Tabular::SET_TOP_SPACE: {
5438 if (value == "default")
5439 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5440 tabular.row_info[r].top_space_default = true;
5441 else if (value == "none")
5442 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5443 tabular.row_info[r].top_space_default = false;
5444 tabular.row_info[r].top_space = len;
5446 else if (isValidLength(value, &len))
5447 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5448 tabular.row_info[r].top_space_default = false;
5449 tabular.row_info[r].top_space = len;
5454 case Tabular::SET_BOTTOM_SPACE: {
5456 if (value == "default")
5457 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5458 tabular.row_info[r].bottom_space_default = true;
5459 else if (value == "none")
5460 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5461 tabular.row_info[r].bottom_space_default = false;
5462 tabular.row_info[r].bottom_space = len;
5464 else if (isValidLength(value, &len))
5465 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5466 tabular.row_info[r].bottom_space_default = false;
5467 tabular.row_info[r].bottom_space = len;
5472 case Tabular::SET_INTERLINE_SPACE: {
5474 if (value == "default")
5475 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5476 tabular.row_info[r].interline_space_default = true;
5477 else if (value == "none")
5478 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5479 tabular.row_info[r].interline_space_default = false;
5480 tabular.row_info[r].interline_space = len;
5482 else if (isValidLength(value, &len))
5483 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5484 tabular.row_info[r].interline_space_default = false;
5485 tabular.row_info[r].interline_space = len;
5490 case Tabular::SET_DECIMAL_POINT:
5491 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5492 tabular.column_info[c].decimal_point = from_utf8(value);
5495 // dummy stuff just to avoid warnings
5496 case Tabular::LAST_ACTION:
5502 bool InsetTabular::copySelection(Cursor & cur)
5504 if (!cur.selection())
5509 getSelection(cur, rs, re, cs, ce);
5511 paste_tabular.reset(new Tabular(tabular));
5513 for (row_type r = 0; r < rs; ++r)
5514 paste_tabular->deleteRow(0);
5516 row_type const rows = re - rs + 1;
5517 while (paste_tabular->nrows() > rows)
5518 paste_tabular->deleteRow(rows);
5520 for (col_type c = 0; c < cs; ++c)
5521 paste_tabular->deleteColumn(0);
5523 col_type const columns = ce - cs + 1;
5524 while (paste_tabular->ncols() > columns)
5525 paste_tabular->deleteColumn(columns);
5527 paste_tabular->setBuffer(tabular.buffer());
5529 odocstringstream os;
5530 OutputParams const runparams(0);
5531 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5532 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5533 cap::copySelection(cur, os.str());
5535 // mark tabular stack dirty
5536 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5537 // when we (hopefully) have a one-for-all paste mechanism.
5538 // This must be called after cap::copySelection.
5539 dirtyTabularStack(true);
5545 bool InsetTabular::pasteClipboard(Cursor & cur)
5549 col_type actcol = tabular.cellColumn(cur.idx());
5550 row_type actrow = tabular.cellRow(cur.idx());
5552 if (cur.selIsMultiCell()) {
5555 getSelection(cur, actrow, re, actcol, ce);
5558 for (row_type r1 = 0, r2 = actrow;
5559 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5561 for (col_type c1 = 0, c2 = actcol;
5562 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5564 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5565 tabular.isPartOfMultiColumn(r2, c2))
5567 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5571 if (tabular.isPartOfMultiColumn(r2, c2)) {
5575 shared_ptr<InsetTableCell> inset(
5576 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5577 tabular.setCellInset(r2, c2, inset);
5578 // FIXME?: why do we need to do this explicitly? (EL)
5579 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5581 // FIXME: change tracking (MG)
5582 inset->setChange(Change(buffer().params().trackChanges ?
5583 Change::INSERTED : Change::UNCHANGED));
5591 void InsetTabular::cutSelection(Cursor & cur)
5593 if (!cur.selection())
5598 getSelection(cur, rs, re, cs, ce);
5599 for (row_type r = rs; r <= re; ++r) {
5600 for (col_type c = cs; c <= ce; ++c) {
5601 shared_ptr<InsetTableCell> t
5602 = cell(tabular.cellIndex(r, c));
5603 if (buffer().params().trackChanges)
5604 // FIXME: Change tracking (MG)
5605 t->setChange(Change(Change::DELETED));
5611 // cursor position might be invalid now
5612 if (cur.pit() > cur.lastpit())
5613 cur.pit() = cur.lastpit();
5614 if (cur.pos() > cur.lastpos())
5615 cur.pos() = cur.lastpos();
5616 cur.clearSelection();
5620 bool InsetTabular::isRightToLeft(Cursor & cur) const
5622 LASSERT(cur.depth() > 1, /**/);
5623 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5624 pos_type const parentpos = cur[cur.depth() - 2].pos();
5625 return parentpar.getFontSettings(buffer().params(),
5626 parentpos).language()->rightToLeft();
5630 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5633 LASSERT(stidx <= enidx, return docstring());
5635 col_type const col1 = tabular.cellColumn(stidx);
5636 col_type const col2 = tabular.cellColumn(enidx);
5637 row_type const row1 = tabular.cellRow(stidx);
5638 row_type const row2 = tabular.cellRow(enidx);
5640 for (col_type col = col1; col <= col2; col++)
5641 for (row_type row = row1; row <= row2; row++) {
5646 retval += tabular.cellInset(row, col)->asString(intoInsets);
5652 void InsetTabular::getSelection(Cursor & cur,
5653 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5655 CursorSlice const & beg = cur.selBegin();
5656 CursorSlice const & end = cur.selEnd();
5657 cs = tabular.cellColumn(beg.idx());
5658 ce = tabular.cellColumn(end.idx());
5662 rs = tabular.cellRow(beg.idx());
5663 re = tabular.cellRow(end.idx());
5669 Text * InsetTabular::getText(int idx) const
5671 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5675 void InsetTabular::setChange(Change const & change)
5677 for (idx_type idx = 0; idx < nargs(); ++idx)
5678 cell(idx)->setChange(change);
5682 void InsetTabular::acceptChanges()
5684 for (idx_type idx = 0; idx < nargs(); ++idx)
5685 cell(idx)->acceptChanges();
5689 void InsetTabular::rejectChanges()
5691 for (idx_type idx = 0; idx < nargs(); ++idx)
5692 cell(idx)->rejectChanges();
5696 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5698 return tabular.getPWidth(cell).zero();
5702 bool InsetTabular::forcePlainLayout(idx_type cell) const
5704 return !tabular.getPWidth(cell).zero();
5708 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5711 if (buf.length() <= 0)
5716 col_type maxCols = 1;
5717 size_t const len = buf.length();
5721 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5729 maxCols = max(cols, maxCols);
5735 maxCols = max(cols, maxCols);
5741 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5742 loctab = paste_tabular.get();
5744 dirtyTabularStack(true);
5747 cell = bv.cursor().idx();
5748 ocol = tabular.cellColumn(cell);
5749 row = tabular.cellRow(cell);
5753 idx_type const cells = loctab->numberofcells;
5756 rows = loctab->nrows();
5757 col_type const columns = loctab->ncols();
5759 while (cell < cells && p < len && row < rows &&
5760 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5766 // we can only set this if we are not too far right
5767 if (cols < columns) {
5768 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5769 Font const font = bv.textMetrics(&inset->text()).
5771 inset->setText(buf.substr(op, p - op), font,
5772 buffer().params().trackChanges);
5778 // we can only set this if we are not too far right
5779 if (cols < columns) {
5780 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5781 Font const font = bv.textMetrics(&inset->text()).
5783 inset->setText(buf.substr(op, p - op), font,
5784 buffer().params().trackChanges);
5789 cell = loctab->cellIndex(row, cols);
5795 // check for the last cell if there is no trailing '\n'
5796 if (cell < cells && op < len) {
5797 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5798 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5799 inset->setText(buf.substr(op, len - op), font,
5800 buffer().params().trackChanges);
5806 void InsetTabular::addPreview(DocIterator const & inset_pos,
5807 PreviewLoader & loader) const
5809 DocIterator cell_pos = inset_pos;
5811 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5812 for (row_type r = 0; r < tabular.nrows(); ++r) {
5813 for (col_type c = 0; c < tabular.ncols(); ++c) {
5814 cell_pos.top().idx() = tabular.cellIndex(r, c);
5815 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5821 bool InsetTabular::completionSupported(Cursor const & cur) const
5823 Cursor const & bvCur = cur.bv().cursor();
5824 if (&bvCur.inset() != this)
5826 return cur.text()->completionSupported(cur);
5830 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5832 return completionSupported(cur);
5836 bool InsetTabular::automaticInlineCompletion() const
5838 return lyxrc.completion_inline_text;
5842 bool InsetTabular::automaticPopupCompletion() const
5844 return lyxrc.completion_popup_text;
5848 bool InsetTabular::showCompletionCursor() const
5850 return lyxrc.completion_cursor_text;
5854 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5856 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5860 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5862 if (!completionSupported(cur))
5864 return cur.text()->completionPrefix(cur);
5868 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5870 if (!completionSupported(cur))
5873 return cur.text()->insertCompletion(cur, s, finished);
5877 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5878 Dimension & dim) const
5880 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5881 tm.completionPosAndDim(cur, x, y, dim);
5885 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5887 istringstream data(in);
5889 lex.setStream(data);
5896 if (!lex || token != "tabular") {
5897 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5901 // This is part of the inset proper that is usually swallowed
5902 // by Buffer::readInset
5904 if (!lex || token != "Tabular") {
5905 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5913 string InsetTabular::params2string(InsetTabular const & inset)
5916 data << "tabular" << ' ';
5918 data << "\\end_inset\n";