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()
838 for (row_type row = 0; row < nrows(); ++row)
839 for (col_type column = 0; column < ncols(); ++column) {
840 if (!isPartOfMultiColumn(row, column)
841 && !isPartOfMultiRow(row, column))
843 if (isPartOfMultiRow(row, column))
844 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
846 cell_info[row][column].cellno = numberofcells - 1;
849 rowofcell.resize(numberofcells);
850 columnofcell.resize(numberofcells);
852 // reset column and row of cells and update their width and alignment
853 for (row_type row = 0; row < nrows(); ++row)
854 for (col_type column = 0; column < ncols(); ++column) {
855 if (isPartOfMultiColumn(row, column))
857 // columnofcell nneds to be called before setting width and aligment
858 // multirow cells inherit the width from the column width
859 if (!isPartOfMultiRow(row, column)) {
860 columnofcell[i] = column;
863 setFixedWidth(row, column);
864 if (isPartOfMultiRow(row, column))
866 cell_info[row][column].inset->setContentAlignment(
867 getAlignment(cellIndex(row, column)));
873 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
876 for (col_type c = 0; c < ncols(); ++c)
877 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
883 bool Tabular::topLine(idx_type const cell) const
885 return cellInfo(cell).top_line;
889 bool Tabular::bottomLine(idx_type const cell) const
891 return cellInfo(cell).bottom_line;
895 bool Tabular::leftLine(idx_type cell) const
899 return cellInfo(cell).left_line;
903 bool Tabular::rightLine(idx_type cell) const
907 return cellInfo(cell).right_line;
911 int Tabular::interRowSpace(row_type row) const
913 if (!row || row >= nrows())
916 int const interline_space = row_info[row - 1].interline_space_default ?
918 row_info[row - 1].interline_space.inPixels(width());
919 if (rowTopLine(row) && rowBottomLine(row - 1))
920 return interline_space + WIDTH_OF_LINE;
921 return interline_space;
925 int Tabular::interColumnSpace(idx_type cell) const
927 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
928 if (rightLine(cell) && nextcol < ncols()
929 && leftLine(cellIndex(cellRow(cell), nextcol)))
930 return WIDTH_OF_LINE;
935 int Tabular::cellWidth(idx_type cell) const
938 col_type const span = columnSpan(cell);
939 col_type const col = cellColumn(cell);
940 for(col_type c = col; c < col + span ; ++c)
941 w += column_info[c].width;
946 int Tabular::cellHeight(idx_type cell) const
948 row_type const span = rowSpan(cell);
949 row_type const row = cellRow(cell);
951 for(row_type r = row; r < row + span ; ++r) {
952 h += rowAscent(r) + rowDescent(r);
953 if (r != row + span - 1)
954 h += interRowSpace(r + 1);
961 bool Tabular::updateColumnWidths()
963 vector<int> max_dwidth(ncols(), 0);
964 for(col_type c = 0; c < ncols(); ++c)
965 for(row_type r = 0; r < nrows(); ++r) {
966 idx_type const i = cellIndex(r, c);
967 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
968 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
972 // for each col get max of single col cells
973 for(col_type c = 0; c < ncols(); ++c) {
975 for(row_type r = 0; r < nrows(); ++r) {
976 idx_type const i = cellIndex(r, c);
977 if (columnSpan(i) == 1) {
978 if (getAlignment(i) == LYX_ALIGN_DECIMAL
979 && cell_info[r][c].decimal_width!=0)
980 new_width = max(new_width, cellInfo(i).width
981 + max_dwidth[c] - cellInfo(i).decimal_width);
983 new_width = max(new_width, cellInfo(i).width);
987 if (column_info[c].width != new_width) {
988 column_info[c].width = new_width;
992 // update col widths to fit merged cells
993 for(col_type c = 0; c < ncols(); ++c)
994 for(row_type r = 0; r < nrows(); ++r) {
995 idx_type const i = cellIndex(r, c);
996 int const span = columnSpan(i);
997 if (span == 1 || c > cellColumn(i))
1001 for(col_type j = c; j < c + span ; ++j)
1002 old_width += column_info[j].width;
1004 if (cellInfo(i).width > old_width) {
1005 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1014 int Tabular::width() const
1017 for (col_type c = 0; c < ncols(); ++c)
1018 width += column_info[c].width;
1023 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1026 col_type const col = cellColumn(cell);
1027 if (onlycolumn || !isMultiColumn(cell)) {
1028 column_info[col].alignment = align;
1029 docstring & dpoint = column_info[col].decimal_point;
1030 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1031 dpoint = from_utf8(lyxrc.default_decimal_point);
1033 cellInfo(cell).alignment = align;
1034 cellInset(cell).get()->setContentAlignment(align);
1039 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1042 if (!isMultiColumn(cell) || onlycolumn)
1043 column_info[cellColumn(cell)].valignment = align;
1045 cellInfo(cell).valignment = align;
1052 * Allow line and paragraph breaks for fixed width cells or disallow them,
1053 * merge cell paragraphs and reset layout to standard for variable width
1056 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1058 inset->setAutoBreakRows(fixedWidth);
1059 inset->toggleFixedWidth(fixedWidth);
1063 // merge all paragraphs to one
1064 BufferParams const & bp = cur.bv().buffer().params();
1065 while (inset->paragraphs().size() > 1)
1066 mergeParagraph(bp, inset->paragraphs(), 0);
1070 // undo information has already been recorded
1071 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1072 bp.documentClass().plainLayoutName());
1079 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1080 Length const & width)
1082 col_type const c = cellColumn(cell);
1084 column_info[c].p_width = width;
1085 // reset the vertical alignment to top if the fixed with
1086 // is removed or zero because only fixed width columns can
1087 // have a vertical alignment
1088 if (column_info[c].p_width.zero())
1089 column_info[c].valignment = LYX_VALIGN_TOP;
1090 for (row_type r = 0; r < nrows(); ++r) {
1091 idx_type const cell = cellIndex(r, c);
1092 // because of multicolumns
1093 toggleFixedWidth(cur, cellInset(cell).get(),
1094 !getPWidth(cell).zero());
1096 // cur paragraph can become invalid after paragraphs were merged
1097 if (cur.pit() > cur.lastpit())
1098 cur.pit() = cur.lastpit();
1099 // cur position can become invalid after newlines were removed
1100 if (cur.pos() > cur.lastpos())
1101 cur.pos() = cur.lastpos();
1105 bool Tabular::setFixedWidth(row_type r, col_type c)
1107 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1108 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1109 || (multicol && !cell_info[r][c].p_width.zero());
1110 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1115 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1116 Length const & width)
1118 if (!isMultiColumn(cell))
1121 cellInfo(cell).p_width = width;
1122 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1123 // cur paragraph can become invalid after paragraphs were merged
1124 if (cur.pit() > cur.lastpit())
1125 cur.pit() = cur.lastpit();
1126 // cur position can become invalid after newlines were removed
1127 if (cur.pos() > cur.lastpos())
1128 cur.pos() = cur.lastpos();
1133 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1134 Tabular::Feature what)
1136 if (what == SET_SPECIAL_MULTICOLUMN)
1137 cellInfo(cell).align_special = special;
1139 column_info[cellColumn(cell)].align_special = special;
1143 void Tabular::setTopLine(idx_type i, bool line)
1145 cellInfo(i).top_line = line;
1149 void Tabular::setBottomLine(idx_type i, bool line)
1151 cellInfo(i).bottom_line = line;
1155 void Tabular::setLeftLine(idx_type cell, bool line)
1157 cellInfo(cell).left_line = line;
1161 void Tabular::setRightLine(idx_type cell, bool line)
1163 cellInfo(cell).right_line = line;
1166 bool Tabular::rowTopLine(row_type r) const
1168 bool all_rows_set = true;
1169 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1170 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1171 return all_rows_set;
1175 bool Tabular::rowBottomLine(row_type r) const
1177 bool all_rows_set = true;
1178 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1179 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1180 return all_rows_set;
1184 bool Tabular::columnLeftLine(col_type c) const
1191 for (row_type r = 0; r < nrows(); ++r) {
1192 idx_type const i = cellIndex(r, c);
1193 if (c == cellColumn(i)) {
1195 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1196 if (cellInfo(i).left_line || right)
1200 return 2 * nrows_left >= total;
1204 bool Tabular::columnRightLine(col_type c) const
1209 int nrows_right = 0;
1211 for (row_type r = 0; r < nrows(); ++r) {
1212 idx_type i = cellIndex(r, c);
1213 if (c == cellColumn(i) + columnSpan(i) - 1) {
1215 bool left = (c + 1 < ncols()
1216 && cellInfo(cellIndex(r, c + 1)).left_line)
1217 || c + 1 == ncols();
1218 if (cellInfo(i).right_line && left)
1222 return 2 * nrows_right >= total;
1226 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1228 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1229 return cellInfo(cell).alignment;
1231 return column_info[cellColumn(cell)].alignment;
1236 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1238 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1239 return cellInfo(cell).valignment;
1240 return column_info[cellColumn(cell)].valignment;
1244 Length const Tabular::getPWidth(idx_type cell) const
1246 if (isMultiColumn(cell))
1247 return cellInfo(cell).p_width;
1248 return column_info[cellColumn(cell)].p_width;
1252 int Tabular::textHOffset(idx_type cell) const
1254 // the LaTeX Way :-(
1255 int x = WIDTH_OF_LINE;
1257 int const w = cellWidth(cell) - cellInfo(cell).width;
1259 switch (getAlignment(cell)) {
1260 case LYX_ALIGN_CENTER:
1263 case LYX_ALIGN_RIGHT:
1266 case LYX_ALIGN_DECIMAL: {
1267 // we center when no decimal point
1268 if (cellInfo(cell).decimal_width == 0) {
1272 col_type const c = cellColumn(cell);
1273 int max_dhoffset = 0;
1274 for(row_type r = 0; r < row_info.size() ; ++r) {
1275 idx_type const i = cellIndex(r, c);
1276 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1277 && cellInfo(i).decimal_width != 0)
1278 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1280 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1283 // LYX_ALIGN_LEFT: nothing :-)
1291 int Tabular::textVOffset(idx_type cell) const
1293 int voffset = cellInfo(cell).voffset;
1294 if (isMultiRow(cell)) {
1295 row_type const row = cellRow(cell);
1296 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1302 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1305 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1307 return cell_info[row][c].cellno;
1311 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1313 col_type c = ncols() - 1;
1314 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1315 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1317 return cell_info[row][c].cellno;
1321 Tabular::row_type Tabular::cellRow(idx_type cell) const
1323 if (cell >= numberofcells)
1327 return rowofcell[cell];
1331 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1333 if (cell >= numberofcells)
1337 return columnofcell[cell];
1341 void Tabular::write(ostream & os) const
1345 << write_attribute("version", 3)
1346 << write_attribute("rows", nrows())
1347 << write_attribute("columns", ncols())
1349 // global longtable options
1351 << write_attribute("rotate", rotate)
1352 << write_attribute("booktabs", use_booktabs)
1353 << write_attribute("islongtable", is_long_tabular)
1354 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1355 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1356 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1357 << write_attribute("headTopDL", endhead.topDL)
1358 << write_attribute("headBottomDL", endhead.bottomDL)
1359 << write_attribute("footTopDL", endfoot.topDL)
1360 << write_attribute("footBottomDL", endfoot.bottomDL)
1361 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1362 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1363 << write_attribute("lastFootEmpty", endlastfoot.empty);
1364 // longtables cannot be aligned vertically
1365 if (!is_long_tabular)
1366 os << write_attribute("tabularvalignment", tabular_valignment);
1367 if (is_long_tabular)
1368 os << write_attribute("longtabularalignment",
1369 longtabular_alignment);
1371 for (col_type c = 0; c < ncols(); ++c) {
1373 << write_attribute("alignment", column_info[c].alignment);
1374 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1375 os << write_attribute("decimal_point", column_info[c].decimal_point);
1376 os << write_attribute("valignment", column_info[c].valignment)
1377 << write_attribute("width", column_info[c].p_width.asString())
1378 << write_attribute("special", column_info[c].align_special)
1381 for (row_type r = 0; r < nrows(); ++r) {
1382 static const string def("default");
1384 if (row_info[r].top_space_default)
1385 os << write_attribute("topspace", def);
1387 os << write_attribute("topspace", row_info[r].top_space);
1388 if (row_info[r].bottom_space_default)
1389 os << write_attribute("bottomspace", def);
1391 os << write_attribute("bottomspace", row_info[r].bottom_space);
1392 if (row_info[r].interline_space_default)
1393 os << write_attribute("interlinespace", def);
1395 os << write_attribute("interlinespace", row_info[r].interline_space);
1396 os << write_attribute("endhead", row_info[r].endhead)
1397 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1398 << write_attribute("endfoot", row_info[r].endfoot)
1399 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1400 << write_attribute("newpage", row_info[r].newpage)
1401 << write_attribute("caption", row_info[r].caption)
1403 for (col_type c = 0; c < ncols(); ++c) {
1405 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1406 << write_attribute("multirow", cell_info[r][c].multirow)
1407 << write_attribute("alignment", cell_info[r][c].alignment)
1408 << write_attribute("valignment", cell_info[r][c].valignment)
1409 << write_attribute("topline", cell_info[r][c].top_line)
1410 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1411 << write_attribute("leftline", cell_info[r][c].left_line)
1412 << write_attribute("rightline", cell_info[r][c].right_line)
1413 << write_attribute("rotate", cell_info[r][c].rotate)
1414 << write_attribute("usebox", cell_info[r][c].usebox)
1415 << write_attribute("width", cell_info[r][c].p_width)
1416 << write_attribute("special", cell_info[r][c].align_special)
1418 os << "\\begin_inset ";
1419 cell_info[r][c].inset->write(os);
1420 os << "\n\\end_inset\n"
1425 os << "</lyxtabular>\n";
1429 void Tabular::read(Lexer & lex)
1432 istream & is = lex.getStream();
1434 l_getline(is, line);
1435 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1436 LASSERT(false, /**/);
1441 if (!getTokenValue(line, "version", version))
1443 LASSERT(version >= 2, /**/);
1446 if (!getTokenValue(line, "rows", rows_arg))
1449 if (!getTokenValue(line, "columns", columns_arg))
1451 init(buffer_, rows_arg, columns_arg);
1452 l_getline(is, line);
1453 if (!prefixIs(line, "<features")) {
1454 lyxerr << "Wrong tabular format (expected <features ...> got"
1455 << line << ')' << endl;
1458 getTokenValue(line, "rotate", rotate);
1459 getTokenValue(line, "booktabs", use_booktabs);
1460 getTokenValue(line, "islongtable", is_long_tabular);
1461 getTokenValue(line, "tabularvalignment", tabular_valignment);
1462 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1463 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1464 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1465 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1466 getTokenValue(line, "headTopDL", endhead.topDL);
1467 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1468 getTokenValue(line, "footTopDL", endfoot.topDL);
1469 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1470 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1471 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1472 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1474 for (col_type c = 0; c < ncols(); ++c) {
1476 if (!prefixIs(line,"<column")) {
1477 lyxerr << "Wrong tabular format (expected <column ...> got"
1478 << line << ')' << endl;
1481 getTokenValue(line, "alignment", column_info[c].alignment);
1482 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1483 getTokenValue(line, "valignment", column_info[c].valignment);
1484 getTokenValue(line, "width", column_info[c].p_width);
1485 getTokenValue(line, "special", column_info[c].align_special);
1488 for (row_type i = 0; i < nrows(); ++i) {
1489 l_getline(is, line);
1490 if (!prefixIs(line, "<row")) {
1491 lyxerr << "Wrong tabular format (expected <row ...> got"
1492 << line << ')' << endl;
1495 getTokenValue(line, "topspace", row_info[i].top_space,
1496 row_info[i].top_space_default);
1497 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1498 row_info[i].bottom_space_default);
1499 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1500 row_info[i].interline_space_default);
1501 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1502 getTokenValue(line, "endhead", row_info[i].endhead);
1503 getTokenValue(line, "endfoot", row_info[i].endfoot);
1504 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1505 getTokenValue(line, "newpage", row_info[i].newpage);
1506 getTokenValue(line, "caption", row_info[i].caption);
1507 for (col_type j = 0; j < ncols(); ++j) {
1508 l_getline(is, line);
1509 if (!prefixIs(line, "<cell")) {
1510 lyxerr << "Wrong tabular format (expected <cell ...> got"
1511 << line << ')' << endl;
1514 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1515 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1516 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1517 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1518 getTokenValue(line, "topline", cell_info[i][j].top_line);
1519 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1520 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1521 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1522 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1523 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1524 getTokenValue(line, "width", cell_info[i][j].p_width);
1526 getTokenValue(line, "special", cell_info[i][j].align_special);
1527 l_getline(is, line);
1528 if (prefixIs(line, "\\begin_inset")) {
1529 cell_info[i][j].inset->setBuffer(*buffer_);
1530 cell_info[i][j].inset->read(lex);
1531 l_getline(is, line);
1533 if (!prefixIs(line, "</cell>")) {
1534 lyxerr << "Wrong tabular format (expected </cell> got"
1535 << line << ')' << endl;
1539 l_getline(is, line);
1540 if (!prefixIs(line, "</row>")) {
1541 lyxerr << "Wrong tabular format (expected </row> got"
1542 << line << ')' << endl;
1546 while (!prefixIs(line, "</lyxtabular>")) {
1547 l_getline(is, line);
1553 bool Tabular::isMultiColumn(idx_type cell) const
1555 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1556 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1560 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1562 return cell_info[cellRow(cell)][cellColumn(cell)];
1566 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1568 idx_type const col = cellColumn(cell);
1569 idx_type const row = cellRow(cell);
1570 for (idx_type i = 0; i < number; ++i)
1571 unsetMultiRow(cellIndex(row, col + i));
1573 // unsetting of multirow may have invalidated cell index
1574 cell = cellIndex(row, col);
1575 CellData & cs = cellInfo(cell);
1576 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1577 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1578 cs.alignment = column_info[col].alignment;
1580 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1582 for (idx_type i = 1; i < number; ++i) {
1583 CellData & cs1 = cellInfo(cell + i);
1584 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1585 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1593 bool Tabular::isMultiRow(idx_type cell) const
1595 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1596 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1600 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1602 idx_type const col = cellColumn(cell);
1603 idx_type const row = cellRow(cell);
1604 for (idx_type i = 0; i < number; ++i)
1605 unsetMultiColumn(cellIndex(row + i, col));
1607 // unsetting of multirow may have invalidated cell index
1608 cell = cellIndex(row, col);
1609 CellData & cs = cellInfo(cell);
1610 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1611 cs.valignment = LYX_VALIGN_MIDDLE;
1612 // the horizontal alignment of multirow cells can only
1613 // be changed for the whole table row,
1614 // support changing this only for the multirow cell can be done via
1616 // this feature would be a fileformat change
1617 // until LyX supports this, use the deault alignment of multirow
1619 cs.alignment = LYX_ALIGN_LEFT;
1621 // set the bottom row of the last selected cell
1622 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1624 for (idx_type i = 1; i < number; ++i) {
1625 CellData & cs1 = cell_info[row + i][col];
1626 cs1.multirow = CELL_PART_OF_MULTIROW;
1627 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1635 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1637 row_type const row = cellRow(cell);
1638 col_type const col = cellColumn(cell);
1640 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1647 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1649 col_type const column = cellColumn(cell);
1650 col_type row = cellRow(cell) + 1;
1651 while (row < nrows() && isPartOfMultiRow(row, column))
1654 return row - cellRow(cell);
1658 void Tabular::unsetMultiColumn(idx_type cell)
1660 if (!isMultiColumn(cell))
1663 row_type const row = cellRow(cell);
1664 col_type const col = cellColumn(cell);
1665 row_type const span = columnSpan(cell);
1666 for (col_type c = 0; c < span; ++c) {
1667 // in the table dialog the lines are set in every case
1668 // when unsetting a multicolumn this leads to an additional right
1669 // line for every cell that was part of the former multicolumn cell,
1670 // except if the cell is in the last column
1671 // therefore remove this line
1672 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1673 && (col + c) < (col + span - 1))
1674 cell_info[row][col + c].right_line = false;
1675 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1681 void Tabular::unsetMultiRow(idx_type cell)
1683 if (!isMultiRow(cell))
1686 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1687 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1688 row_type const row = cellRow(cell);
1689 col_type const col = cellColumn(cell);
1690 row_type const span = rowSpan(cell);
1691 for (row_type r = 0; r < span; ++r)
1692 cell_info[row + r][col].multirow = CELL_NORMAL;
1697 void Tabular::setRotateCell(idx_type cell, bool flag)
1699 cellInfo(cell).rotate = flag;
1703 bool Tabular::getRotateCell(idx_type cell) const
1705 return cellInfo(cell).rotate;
1709 bool Tabular::needRotating() const
1713 for (row_type r = 0; r < nrows(); ++r)
1714 for (col_type c = 0; c < ncols(); ++c)
1715 if (cell_info[r][c].rotate)
1721 bool Tabular::isLastCell(idx_type cell) const
1723 if (cell + 1 < numberofcells)
1729 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1731 if (cellRow(cell) == 0)
1734 col_type const col = cellColumn(cell);
1735 row_type r = cellRow(cell) - 1;
1736 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1739 return cell_info[r][col].cellno;
1743 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1745 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1746 if (nextrow < nrows())
1747 return cell_info[nextrow][cellColumn(cell)].cellno;
1752 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1754 LASSERT(column != npos && column < ncols()
1755 && row != npos && row < nrows(), /**/);
1756 return cell_info[row][column].cellno;
1760 void Tabular::setUsebox(idx_type cell, BoxType type)
1762 cellInfo(cell).usebox = type;
1766 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1767 // adds line breaks, see bug 4886.
1768 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1770 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1771 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1773 if (cellInfo(cell).usebox > 1)
1774 return cellInfo(cell).usebox;
1775 return useParbox(cell);
1780 // This are functions used for the longtable support
1782 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1788 row_info[row].endfirsthead = flag;
1792 row_info[row].endhead = flag;
1797 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1800 hd.set = haveLTHead();
1801 return row_info[row].endhead;
1805 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1808 hd.set = haveLTFirstHead();
1809 return row_info[row].endfirsthead;
1813 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1819 row_info[row].endlastfoot = flag;
1823 row_info[row].endfoot = flag;
1828 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1831 fd.set = haveLTFoot();
1832 return row_info[row].endfoot;
1836 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1839 fd.set = haveLTLastFoot();
1840 return row_info[row].endlastfoot;
1844 void Tabular::setLTNewPage(row_type row, bool what)
1846 row_info[row].newpage = what;
1850 bool Tabular::getLTNewPage(row_type row) const
1852 return row_info[row].newpage;
1856 bool Tabular::haveLTHead() const
1858 for (row_type i = 0; i < nrows(); ++i)
1859 if (row_info[i].endhead)
1865 bool Tabular::haveLTFirstHead() const
1867 if (endfirsthead.empty)
1869 for (row_type r = 0; r < nrows(); ++r)
1870 if (row_info[r].endfirsthead)
1876 bool Tabular::haveLTFoot() const
1878 for (row_type r = 0; r < nrows(); ++r)
1879 if (row_info[r].endfoot)
1885 bool Tabular::haveLTLastFoot() const
1887 if (endlastfoot.empty)
1889 for (row_type r = 0; r < nrows(); ++r)
1890 if (row_info[r].endlastfoot)
1896 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1898 idx_type i = getFirstCellInRow(row);
1900 setMultiColumn(i, numberOfCellsInRow(row));
1901 setTopLine(i, false);
1902 setBottomLine(i, false);
1903 setLeftLine(i, false);
1904 setRightLine(i, false);
1906 unsetMultiColumn(i);
1907 // When unsetting a caption row, also all existing
1908 // captions in this row must be dissolved.
1910 row_info[row].caption = what;
1915 bool Tabular::ltCaption(row_type row) const
1917 return row_info[row].caption;
1921 bool Tabular::haveLTCaption() const
1923 for (row_type r = 0; r < nrows(); ++r)
1924 if (row_info[r].caption)
1930 // end longtable support functions
1932 void Tabular::setRowAscent(row_type row, int height)
1934 if (row >= nrows() || row_info[row].ascent == height)
1936 row_info[row].ascent = height;
1940 void Tabular::setRowDescent(row_type row, int height)
1942 if (row >= nrows() || row_info[row].descent == height)
1944 row_info[row].descent = height;
1948 int Tabular::rowAscent(row_type row) const
1950 LASSERT(row < nrows(), /**/);
1951 return row_info[row].ascent;
1955 int Tabular::rowDescent(row_type row) const
1957 LASSERT(row < nrows(), /**/);
1958 return row_info[row].descent;
1962 int Tabular::height() const
1965 for (row_type row = 0; row < nrows(); ++row)
1966 height += rowAscent(row) + rowDescent(row) +
1972 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1974 LASSERT(row < nrows(), /**/);
1975 LASSERT(column < ncols(), /**/);
1976 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1980 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1982 LASSERT(row < nrows(), /**/);
1983 LASSERT(column < ncols(), /**/);
1984 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1988 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1990 // we only output complete row lines and the 1st row here, the rest
1991 // is done in Tabular::TeXBottomHLine(...)
1993 // get for each column the topline (if any)
1994 vector<bool> topline;
1996 for (col_type c = 0; c < ncols(); ++c) {
1997 topline.push_back(topLine(cellIndex(row, c)));
1998 // If cell is part of a multirow and not the first cell of the
1999 // multirow, no line must be drawn.
2001 if (isMultiRow(cellIndex(row, c))
2002 && isMultiRow(cellIndex(row - 1, c)))
2008 // do nothing if empty first row, or incomplete row line after
2009 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2012 // only output complete row lines and the 1st row's clines
2013 if (nset == ncols()) {
2015 os << (row == 0 ? "\\toprule " : "\\midrule ");
2019 } else if (row == 0) {
2020 for (col_type c = 0; c < ncols(); ++c) {
2022 col_type offset = 0;
2023 for (col_type j = 0 ; j < c; ++j)
2024 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2027 //babel makes the "-" character an active one, so we have to suppress this here
2028 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2029 if (lang == "slovak" || lang == "czech")
2030 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2031 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2033 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2035 col_type cstart = c;
2036 for ( ; c < ncols() && topline[c]; ++c) {}
2038 for (col_type j = cstart ; j < c ; ++j)
2039 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2042 os << c + offset << "} ";
2051 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2053 // we output bottomlines of row r and the toplines of row r+1
2054 // if the latter do not span the whole tabular
2056 // get the bottomlines of row r, and toplines in next row
2057 bool lastrow = row == nrows() - 1;
2058 vector<bool> bottomline, topline;
2059 bool nextrowset = true;
2060 for (col_type c = 0; c < ncols(); ++c) {
2061 bottomline.push_back(bottomLine(cellIndex(row, c)));
2062 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2063 // If cell is part of a multirow and not the last or first cell of the
2064 // multirow, no line must be drawn.
2066 if (isMultiRow(cellIndex(row, c))
2067 && isMultiRow(cellIndex(row + 1, c))) {
2068 bottomline[c] = false;
2071 nextrowset &= topline[c];
2074 // combine this row's bottom lines and next row's toplines if necessary
2076 for (col_type c = 0; c < ncols(); ++c) {
2078 bottomline[c] = bottomline[c] || topline[c];
2083 // do nothing if empty, OR incomplete row line with a topline in next row
2084 if (nset == 0 || (nextrowset && nset != ncols()))
2087 if (nset == ncols()) {
2089 os << (lastrow ? "\\bottomrule" : "\\midrule");
2093 for (col_type c = 0; c < ncols(); ++c) {
2094 if (bottomline[c]) {
2095 col_type offset = 0;
2096 for (col_type j = 0 ; j < c; ++j)
2097 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2100 //babel makes the "-" character an active one, so we have to suppress this here
2101 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2102 if (lang == "slovak" || lang == "czech")
2103 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2104 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2106 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2108 col_type cstart = c;
2109 for ( ; c < ncols() && bottomline[c]; ++c) {}
2111 for (col_type j = cstart ; j < c ; ++j)
2112 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2115 os << c + offset << "} ";
2124 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2125 bool & ismulticol, bool & ismultirow) const
2128 row_type const r = cellRow(cell);
2129 if (is_long_tabular && row_info[r].caption)
2132 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2133 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2134 // figure out how to set the lines
2135 // we always set double lines to the right of the cell
2136 col_type const c = cellColumn(cell);
2137 col_type const nextcol = c + columnSpan(cell);
2138 bool colright = columnRightLine(c);
2139 bool colleft = columnLeftLine(c);
2140 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2141 bool nextcellleft = nextcol < ncols()
2142 && leftLine(cellIndex(r, nextcol));
2143 bool coldouble = colright && nextcolleft;
2144 bool celldouble = rightLine(cell) && nextcellleft;
2146 ismulticol = isMultiColumn(cell)
2147 || (c == 0 && colleft != leftLine(cell))
2148 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2149 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2150 || (coldouble != celldouble);
2152 // we center in multicol when no decimal point
2153 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2154 && (cellInfo(cell).decimal_width == 0));
2156 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2157 int latexcolspan = columnSpan(cell);
2158 for(col_type col = c; col < c + columnSpan(cell); ++col)
2159 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2163 os << "\\multicolumn{" << latexcolspan << "}{";
2164 if (c ==0 && leftLine(cell))
2166 if (!cellInfo(cell).align_special.empty()) {
2167 os << cellInfo(cell).align_special;
2169 if (!getPWidth(cell).zero()) {
2171 case LYX_ALIGN_LEFT:
2172 os << ">{\\raggedright}";
2174 case LYX_ALIGN_RIGHT:
2175 os << ">{\\raggedleft}";
2177 case LYX_ALIGN_CENTER:
2178 os << ">{\\centering}";
2184 case LYX_VALIGN_TOP:
2187 case LYX_VALIGN_MIDDLE:
2190 case LYX_VALIGN_BOTTOM:
2195 << from_ascii(getPWidth(cell).asLatexString())
2199 case LYX_ALIGN_LEFT:
2202 case LYX_ALIGN_RIGHT:
2209 } // end if else !getPWidth
2210 } // end if else !cellinfo_of_cell
2211 if (rightLine(cell) || nextcellleft)
2214 // add extra vertical line if we want a double one
2217 } // end if ismulticol
2219 // we only need code for the first multirow cell
2220 ismultirow = isMultiRow(cell);
2222 os << "\\multirow{" << rowSpan(cell) << "}{";
2223 if (!getPWidth(cell).zero())
2224 os << from_ascii(getPWidth(cell).asLatexString());
2226 // we need to set a default value
2229 } // end if ismultirow
2231 if (getRotateCell(cell)) {
2232 os << "\\begin{sideways}\n";
2235 if (getUsebox(cell) == BOX_PARBOX) {
2238 case LYX_VALIGN_TOP:
2241 case LYX_VALIGN_MIDDLE:
2244 case LYX_VALIGN_BOTTOM:
2248 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2250 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2251 os << "\\begin{minipage}[";
2253 case LYX_VALIGN_TOP:
2256 case LYX_VALIGN_MIDDLE:
2259 case LYX_VALIGN_BOTTOM:
2263 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2271 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2272 bool ismulticol, bool ismultirow) const
2275 row_type const r = cellRow(cell);
2276 if (is_long_tabular && row_info[r].caption)
2280 if (getUsebox(cell) == BOX_PARBOX)
2282 else if (getUsebox(cell) == BOX_MINIPAGE) {
2283 os << "%\n\\end{minipage}";
2286 if (getRotateCell(cell)) {
2287 os << "%\n\\end{sideways}";
2299 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2300 OutputParams const & runparams) const
2302 if (!is_long_tabular)
2307 // the caption must be output before the headers
2308 if (haveLTCaption()) {
2309 for (row_type r = 0; r < nrows(); ++r) {
2310 if (row_info[r].caption) {
2311 ret += TeXRow(os, r, runparams);
2315 // output first header info
2316 // first header must be output before the header, otherwise the
2317 // correct caption placement becomes really weird
2318 if (haveLTFirstHead()) {
2319 if (endfirsthead.topDL) {
2323 for (row_type r = 0; r < nrows(); ++r) {
2324 if (row_info[r].endfirsthead) {
2325 ret += TeXRow(os, r, runparams);
2328 if (endfirsthead.bottomDL) {
2332 os << "\\endfirsthead\n";
2335 // output header info
2337 if (endfirsthead.empty && !haveLTFirstHead()) {
2338 os << "\\endfirsthead\n";
2341 if (endhead.topDL) {
2345 for (row_type r = 0; r < nrows(); ++r) {
2346 if (row_info[r].endhead) {
2347 ret += TeXRow(os, r, runparams);
2350 if (endhead.bottomDL) {
2354 os << "\\endhead\n";
2357 // output footer info
2359 if (endfoot.topDL) {
2363 for (row_type r = 0; r < nrows(); ++r) {
2364 if (row_info[r].endfoot) {
2365 ret += TeXRow(os, r, runparams);
2368 if (endfoot.bottomDL) {
2372 os << "\\endfoot\n";
2374 if (endlastfoot.empty && !haveLTLastFoot()) {
2375 os << "\\endlastfoot\n";
2379 // output lastfooter info
2380 if (haveLTLastFoot()) {
2381 if (endlastfoot.topDL) {
2385 for (row_type r = 0; r < nrows(); ++r) {
2386 if (row_info[r].endlastfoot) {
2387 ret += TeXRow(os, r, runparams);
2390 if (endlastfoot.bottomDL) {
2394 os << "\\endlastfoot\n";
2401 bool Tabular::isValidRow(row_type row) const
2403 if (!is_long_tabular)
2405 return !row_info[row].endhead && !row_info[row].endfirsthead
2406 && !row_info[row].endfoot && !row_info[row].endlastfoot
2407 && !row_info[row].caption;
2411 int Tabular::TeXRow(odocstream & os, row_type row,
2412 OutputParams const & runparams) const
2414 idx_type cell = cellIndex(row, 0);
2415 shared_ptr<InsetTableCell> inset = cellInset(cell);
2416 Paragraph const & par = inset->paragraphs().front();
2417 string const lang = par.getParLanguage(buffer().params())->lang();
2419 //output the top line
2420 int ret = TeXTopHLine(os, row, lang);
2422 if (row_info[row].top_space_default) {
2424 os << "\\addlinespace\n";
2426 os << "\\noalign{\\vskip\\doublerulesep}\n";
2428 } else if(!row_info[row].top_space.zero()) {
2430 os << "\\addlinespace["
2431 << from_ascii(row_info[row].top_space.asLatexString())
2434 os << "\\noalign{\\vskip"
2435 << from_ascii(row_info[row].top_space.asLatexString())
2440 bool ismulticol = false;
2441 bool ismultirow = false;
2442 for (col_type c = 0; c < ncols(); ++c) {
2443 if (isPartOfMultiColumn(row, c))
2446 if (isPartOfMultiRow(row, c) &&
2447 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2452 cell = cellIndex(row, c);
2453 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2454 shared_ptr<InsetTableCell> inset = cellInset(cell);
2456 Paragraph const & par = inset->paragraphs().front();
2457 bool rtl = par.isRTL(buffer().params())
2459 && getPWidth(cell).zero();
2463 par.getParLanguage(buffer().params())->lang();
2464 if (lang == "farsi")
2466 else if (lang == "arabic_arabi")
2468 // currently, remaning RTL languages are
2469 // arabic_arabtex and hebrew
2473 // pass to the OutputParams that we are in a cell and
2474 // which alignment we have set.
2475 // InsetNewline needs this context information.
2476 OutputParams newrp(runparams);
2477 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2478 ? OutputParams::PLAIN
2479 : OutputParams::ALIGNED;
2481 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2482 && cellInfo(cell).decimal_width != 0) {
2483 // copy cell and split in 2
2484 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2485 head.getText(0)->setMacrocontextPosition(
2486 cellInset(cell)->getText(0)->macrocontextPosition());
2487 head.setBuffer(buffer());
2488 bool hassep = false;
2489 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2490 head.latex(os, newrp);
2492 ret += tail.latex(os, newrp);
2493 } else if (!isPartOfMultiRow(row, c))
2494 ret += inset->latex(os, newrp);
2496 runparams.encoding = newrp.encoding;
2500 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2501 if (cell != getLastCellInRow(row)) { // not last cell in row
2505 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2506 // if no first header and no empty first header is used,
2507 // the caption needs to be terminated by \endfirsthead
2509 os << "\\endfirsthead";
2511 os << "\\tabularnewline";
2512 if (row_info[row].bottom_space_default) {
2514 os << "\\addlinespace";
2516 os << "[\\doublerulesep]";
2517 } else if (!row_info[row].bottom_space.zero()) {
2519 os << "\\addlinespace";
2521 << from_ascii(row_info[row].bottom_space.asLatexString())
2527 //output the bottom line
2528 ret += TeXBottomHLine(os, row, lang);
2530 if (row_info[row].interline_space_default) {
2532 os << "\\addlinespace\n";
2534 os << "\\noalign{\\vskip\\doublerulesep}\n";
2536 } else if (!row_info[row].interline_space.zero()) {
2538 os << "\\addlinespace["
2539 << from_ascii(row_info[row].interline_space.asLatexString())
2542 os << "\\noalign{\\vskip"
2543 << from_ascii(row_info[row].interline_space.asLatexString())
2551 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2555 //+---------------------------------------------------------------------
2556 //+ first the opening preamble +
2557 //+---------------------------------------------------------------------
2560 os << "\\begin{sideways}\n";
2563 if (is_long_tabular) {
2564 os << "\\begin{longtable}";
2565 switch (longtabular_alignment) {
2566 case LYX_LONGTABULAR_ALIGN_LEFT:
2569 case LYX_LONGTABULAR_ALIGN_CENTER:
2571 case LYX_LONGTABULAR_ALIGN_RIGHT:
2576 os << "\\begin{tabular}";
2577 switch (tabular_valignment) {
2578 case LYX_VALIGN_TOP:
2581 case LYX_VALIGN_MIDDLE:
2583 case LYX_VALIGN_BOTTOM:
2591 for (col_type c = 0; c < ncols(); ++c) {
2592 if (columnLeftLine(c))
2594 if (!column_info[c].align_special.empty()) {
2595 os << column_info[c].align_special;
2597 if (!column_info[c].p_width.zero()) {
2598 switch (column_info[c].alignment) {
2599 case LYX_ALIGN_LEFT:
2600 os << ">{\\raggedright}";
2602 case LYX_ALIGN_RIGHT:
2603 os << ">{\\raggedleft}";
2605 case LYX_ALIGN_CENTER:
2606 os << ">{\\centering}";
2608 case LYX_ALIGN_NONE:
2609 case LYX_ALIGN_BLOCK:
2610 case LYX_ALIGN_LAYOUT:
2611 case LYX_ALIGN_SPECIAL:
2612 case LYX_ALIGN_DECIMAL:
2616 switch (column_info[c].valignment) {
2617 case LYX_VALIGN_TOP:
2620 case LYX_VALIGN_MIDDLE:
2623 case LYX_VALIGN_BOTTOM:
2628 << from_ascii(column_info[c].p_width.asLatexString())
2631 switch (column_info[c].alignment) {
2632 case LYX_ALIGN_LEFT:
2635 case LYX_ALIGN_RIGHT:
2638 case LYX_ALIGN_DECIMAL:
2639 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2645 } // end if else !column_info[i].p_width
2646 } // end if else !column_info[i].align_special
2647 if (columnRightLine(c))
2653 ret += TeXLongtableHeaderFooter(os, runparams);
2655 //+---------------------------------------------------------------------
2656 //+ the single row and columns (cells) +
2657 //+---------------------------------------------------------------------
2659 for (row_type r = 0; r < nrows(); ++r) {
2660 if (isValidRow(r)) {
2661 ret += TeXRow(os, r, runparams);
2662 if (is_long_tabular && row_info[r].newpage) {
2663 os << "\\newpage\n";
2669 //+---------------------------------------------------------------------
2670 //+ the closing of the tabular +
2671 //+---------------------------------------------------------------------
2673 if (is_long_tabular)
2674 os << "\\end{longtable}";
2676 os << "\\end{tabular}";
2678 os << "\n\\end{sideways}";
2686 int Tabular::docbookRow(odocstream & os, row_type row,
2687 OutputParams const & runparams) const
2690 idx_type cell = getFirstCellInRow(row);
2693 for (col_type c = 0; c < ncols(); ++c) {
2694 if (isPartOfMultiColumn(row, c))
2697 os << "<entry align=\"";
2698 switch (getAlignment(cell)) {
2699 case LYX_ALIGN_LEFT:
2702 case LYX_ALIGN_RIGHT:
2710 os << "\" valign=\"";
2711 switch (getVAlignment(cell)) {
2712 case LYX_VALIGN_TOP:
2715 case LYX_VALIGN_BOTTOM:
2718 case LYX_VALIGN_MIDDLE:
2723 if (isMultiColumn(cell)) {
2724 os << " namest=\"col" << c << "\" ";
2725 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2729 ret += cellInset(cell)->docbook(os, runparams);
2738 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2742 //+---------------------------------------------------------------------
2743 //+ first the opening preamble +
2744 //+---------------------------------------------------------------------
2746 os << "<tgroup cols=\"" << ncols()
2747 << "\" colsep=\"1\" rowsep=\"1\">\n";
2749 for (col_type c = 0; c < ncols(); ++c) {
2750 os << "<colspec colname=\"col" << c << "\" align=\"";
2751 switch (column_info[c].alignment) {
2752 case LYX_ALIGN_LEFT:
2755 case LYX_ALIGN_RIGHT:
2763 if (runparams.flavor == OutputParams::XML)
2769 //+---------------------------------------------------------------------
2770 //+ Long Tabular case +
2771 //+---------------------------------------------------------------------
2773 // output caption info
2774 if (haveLTCaption()) {
2775 os << "<caption>\n";
2777 for (row_type r = 0; r < nrows(); ++r) {
2778 if (row_info[r].caption) {
2779 ret += docbookRow(os, r, runparams);
2782 os << "</caption>\n";
2785 // output header info
2786 if (haveLTHead() || haveLTFirstHead()) {
2789 for (row_type r = 0; r < nrows(); ++r) {
2790 if (row_info[r].endhead || row_info[r].endfirsthead) {
2791 ret += docbookRow(os, r, runparams);
2797 // output footer info
2798 if (haveLTFoot() || haveLTLastFoot()) {
2801 for (row_type r = 0; r < nrows(); ++r) {
2802 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2803 ret += docbookRow(os, r, runparams);
2810 //+---------------------------------------------------------------------
2811 //+ the single row and columns (cells) +
2812 //+---------------------------------------------------------------------
2816 for (row_type r = 0; r < nrows(); ++r) {
2817 if (isValidRow(r)) {
2818 ret += docbookRow(os, r, runparams);
2823 //+---------------------------------------------------------------------
2824 //+ the closing of the tabular +
2825 //+---------------------------------------------------------------------
2834 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2835 OutputParams const & runparams) const
2838 idx_type cell = getFirstCellInRow(row);
2840 xs << html::StartTag("tr");
2841 for (col_type c = 0; c < ncols(); ++c) {
2842 if (isPartOfMultiColumn(row, c))
2847 switch (getAlignment(cell)) {
2848 case LYX_ALIGN_LEFT:
2851 case LYX_ALIGN_RIGHT:
2859 attr << " valign='";
2860 switch (getVAlignment(cell)) {
2861 case LYX_VALIGN_TOP:
2864 case LYX_VALIGN_BOTTOM:
2867 case LYX_VALIGN_MIDDLE:
2872 if (isMultiColumn(cell))
2873 attr << " colspan='" << columnSpan(cell) << "'";
2875 xs << html::StartTag("td", attr.str());
2876 ret += cellInset(cell)->xhtml(xs, runparams);
2877 xs << html::EndTag("td");
2880 xs << html::EndTag("tr");
2885 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2888 // It's unclear to me if we need to mess with the long table stuff.
2889 // We can borrow that too from docbook, if so.
2891 xs << html::StartTag("tbody");
2892 for (row_type r = 0; r < nrows(); ++r) {
2893 if (isValidRow(r)) {
2894 ret += xhtmlRow(xs, r, runparams);
2897 xs << html::EndTag("tbody");
2902 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2903 vector<unsigned int> const & clen) const
2905 idx_type const fcell = getFirstCellInRow(row);
2906 idx_type const n = numberOfCellsInRow(row) + fcell;
2909 for (idx_type i = fcell; i < n; ++i) {
2919 for (idx_type i = fcell; i < n; ++i) {
2930 col_type column = cellColumn(i);
2931 int len = clen[column];
2932 while (column < ncols() - 1
2933 && isPartOfMultiColumn(row, ++column))
2934 len += clen[column] + 4;
2935 os << docstring(len, ch);
2950 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2951 vector<unsigned int> const & clen) const
2953 idx_type const fcell = getFirstCellInRow(row);
2954 idx_type const n = numberOfCellsInRow(row) + fcell;
2957 for (idx_type i = fcell; i < n; ++i) {
2958 if (bottomLine(i)) {
2967 for (idx_type i = fcell; i < n; ++i) {
2968 if (bottomLine(i)) {
2978 col_type column = cellColumn(i);
2979 int len = clen[column];
2980 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2981 len += clen[column] + 4;
2982 os << docstring(len, ch);
2983 if (bottomLine(i)) {
2997 void Tabular::plaintextPrintCell(odocstream & os,
2998 OutputParams const & runparams,
2999 idx_type cell, row_type row, col_type column,
3000 vector<unsigned int> const & clen,
3001 bool onlydata) const
3003 odocstringstream sstr;
3004 cellInset(cell)->plaintext(sstr, runparams);
3016 unsigned int len1 = sstr.str().length();
3017 unsigned int len2 = clen[column];
3018 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3019 len2 += clen[column] + 4;
3022 switch (getAlignment(cell)) {
3024 case LYX_ALIGN_LEFT:
3027 case LYX_ALIGN_RIGHT:
3031 case LYX_ALIGN_CENTER:
3037 os << docstring(len1, ' ') << sstr.str()
3038 << docstring(len2, ' ');
3040 if (rightLine(cell))
3047 void Tabular::plaintext(odocstream & os,
3048 OutputParams const & runparams, int const depth,
3049 bool onlydata, char_type delim) const
3051 // first calculate the width of the single columns
3052 vector<unsigned int> clen(ncols());
3055 // first all non multicolumn cells!
3056 for (col_type c = 0; c < ncols(); ++c) {
3058 for (row_type r = 0; r < nrows(); ++r) {
3059 idx_type cell = cellIndex(r, c);
3060 if (isMultiColumn(cell))
3062 odocstringstream sstr;
3063 cellInset(cell)->plaintext(sstr, runparams);
3064 if (clen[c] < sstr.str().length())
3065 clen[c] = sstr.str().length();
3068 // then all multicolumn cells!
3069 for (col_type c = 0; c < ncols(); ++c) {
3070 for (row_type r = 0; r < nrows(); ++r) {
3071 idx_type cell = cellIndex(r, c);
3072 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3074 odocstringstream sstr;
3075 cellInset(cell)->plaintext(sstr, runparams);
3076 int len = int(sstr.str().length());
3077 idx_type const n = columnSpan(cell);
3078 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3080 if (len > int(clen[c + n - 1]))
3081 clen[c + n - 1] = len;
3086 for (row_type r = 0; r < nrows(); ++r) {
3087 if (!onlydata && plaintextTopHLine(os, r, clen))
3088 os << docstring(depth * 2, ' ');
3089 for (col_type c = 0; c < ncols(); ++c) {
3090 if (isPartOfMultiColumn(r, c))
3092 if (onlydata && c > 0)
3093 // we don't use operator<< for single UCS4 character.
3094 // see explanation in docstream.h
3096 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3101 os << docstring(depth * 2, ' ');
3102 if (plaintextBottomHLine(os, r, clen))
3103 os << docstring(depth * 2, ' ');
3109 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3111 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3115 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3116 col_type column) const
3118 return cell_info[row][column].inset;
3122 void Tabular::setCellInset(row_type row, col_type column,
3123 shared_ptr<InsetTableCell> ins) const
3125 CellData & cd = cell_info[row][column];
3130 void Tabular::validate(LaTeXFeatures & features) const
3132 features.require("NeedTabularnewline");
3134 features.require("booktabs");
3135 if (is_long_tabular)
3136 features.require("longtable");
3138 features.require("rotating");
3139 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3140 if (isMultiRow(cell))
3141 features.require("multirow");
3142 if (getVAlignment(cell) != LYX_VALIGN_TOP
3143 || !getPWidth(cell).zero())
3144 features.require("array");
3145 cellInset(cell)->validate(features);
3150 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3152 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3153 ParagraphList::const_iterator cit = parlist.begin();
3154 ParagraphList::const_iterator end = parlist.end();
3156 for (; cit != end; ++cit)
3157 for (int i = 0; i < cit->size(); ++i)
3158 if (cit->isNewline(i))
3165 /////////////////////////////////////////////////////////////////////
3169 /////////////////////////////////////////////////////////////////////
3171 InsetTableCell::InsetTableCell(Buffer * buf)
3172 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3173 contentAlign(LYX_ALIGN_CENTER)
3177 bool InsetTableCell::forcePlainLayout(idx_type) const
3179 return !isFixedWidth;
3183 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3185 return isFixedWidth;
3189 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3190 FuncStatus & status) const
3193 switch (cmd.action()) {
3195 enabled = !forcePlainLayout();
3197 case LFUN_LAYOUT_PARAGRAPH:
3198 enabled = allowParagraphCustomization();
3201 return InsetText::getStatus(cur, cmd, status);
3203 status.setEnabled(enabled);
3207 docstring InsetTableCell::asString(bool intoInsets)
3210 if (paragraphs().empty())
3212 ParagraphList::const_iterator it = paragraphs().begin();
3213 ParagraphList::const_iterator en = paragraphs().end();
3215 for (; it != en; ++it) {
3220 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3226 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3229 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3230 return InsetText::xhtml(xs, rp);
3235 /////////////////////////////////////////////////////////////////////
3239 /////////////////////////////////////////////////////////////////////
3241 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3243 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3244 rowselect_(false), colselect_(false)
3249 InsetTabular::InsetTabular(InsetTabular const & tab)
3250 : Inset(tab), tabular(tab.tabular), scx_(0)
3255 InsetTabular::~InsetTabular()
3257 hideDialogs("tabular", this);
3261 void InsetTabular::setBuffer(Buffer & buf)
3263 tabular.setBuffer(buf);
3264 Inset::setBuffer(buf);
3268 bool InsetTabular::insetAllowed(InsetCode code) const
3273 case MATHMACRO_CODE:
3278 return tabular.is_long_tabular;
3286 void InsetTabular::write(ostream & os) const
3288 os << "Tabular" << endl;
3293 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3295 // FIXME: depending on the selection state, we could offer a different menu.
3296 return from_ascii("context-tabular");
3300 void InsetTabular::read(Lexer & lex)
3302 //bool const old_format = (lex.getString() == "\\LyXTable");
3310 string token = lex.getString();
3311 while (lex && token != "\\end_inset") {
3313 token = lex.getString();
3316 lex.printError("Missing \\end_inset at this point. ");
3320 int InsetTabular::rowFromY(Cursor & cur, int y) const
3322 // top y coordinate of tabular
3323 int h = yo(cur.bv()) - tabular.rowAscent(0);
3325 for (; r < tabular.nrows() && y > h; ++r)
3326 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3327 + tabular.interRowSpace(r);
3333 int InsetTabular::columnFromX(Cursor & cur, int x) const
3335 // left x coordinate of tabular
3336 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3338 for (; c < tabular.ncols() && x > w; ++c)
3339 w += tabular.cellWidth(c);
3344 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3346 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3347 // mi.base.textwidth << "\n";
3350 LASSERT(false, /**/);
3353 for (row_type r = 0; r < tabular.nrows(); ++r) {
3356 for (col_type c = 0; c < tabular.ncols(); ++c) {
3357 if (tabular.isPartOfMultiColumn(r, c)
3358 || tabular.isPartOfMultiRow(r, c))
3359 // multicolumn or multirow cell, but not first one
3361 idx_type const cell = tabular.cellIndex(r, c);
3364 Length const p_width = tabular.getPWidth(cell);
3365 if (!p_width.zero())
3366 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3367 tabular.cellInset(cell)->metrics(m, dim);
3368 if (!p_width.zero())
3369 dim.wid = m.base.textwidth;
3370 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3371 + tabular.interColumnSpace(cell);
3373 // FIXME(?): do we need a second metrics call?
3374 TextMetrics const & tm =
3375 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3377 // determine horiz offset because of decimal align (if necessary)
3378 int decimal_hoffset = 0;
3379 int decimal_width = 0;
3380 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3381 // make a copy which we will split in 2
3382 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3383 head.getText(0)->setMacrocontextPosition(
3384 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3385 head.setBuffer(tabular.buffer());
3386 // split in 2 and calculate width of each part
3387 bool hassep = false;
3388 InsetTableCell tail =
3389 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3391 head.metrics(m, dim1);
3392 decimal_hoffset = dim1.width();
3394 tail.metrics(m, dim1);
3395 decimal_width = dim1.width();
3398 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3399 tabular.cell_info[r][c].decimal_width = decimal_width;
3401 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3402 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3403 int const lastpardes = tm.last().second->descent()
3404 + TEXT_TO_INSET_OFFSET;
3406 switch (tabular.getVAlignment(cell)) {
3407 case Tabular::LYX_VALIGN_TOP:
3409 case Tabular::LYX_VALIGN_MIDDLE:
3410 offset = -(dim.des - lastpardes)/2;
3412 case Tabular::LYX_VALIGN_BOTTOM:
3413 offset = -(dim.des - lastpardes);
3416 tabular.cell_info[r][c].voffset = offset;
3417 maxasc = max(maxasc, dim.asc - offset);
3418 maxdes = max(maxdes, dim.des + offset);
3420 int const top_space = tabular.row_info[r].top_space_default ?
3421 default_line_space :
3422 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3423 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3424 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3425 default_line_space :
3426 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3427 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3429 tabular.updateColumnWidths();
3430 dim.asc = tabular.rowAscent(0);
3431 dim.des = tabular.height() - dim.asc;
3432 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3436 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3439 if (&cur.inset() == this && cur.selection()) {
3440 if (cur.selIsMultiCell()) {
3443 getSelection(cur, rs, re, cs, ce);
3445 idx_type const cell = tabular.cellIndex(row, col);
3446 col_type const cspan = tabular.columnSpan(cell);
3447 row_type const rspan = tabular.rowSpan(cell);
3448 if (col + cspan - 1 >= cs && col <= ce
3449 && row + rspan - 1 >= rs && row <= re)
3452 if (col == tabular.cellColumn(cur.idx())
3453 && row == tabular.cellRow(cur.idx())) {
3454 CursorSlice const & beg = cur.selBegin();
3455 CursorSlice const & end = cur.selEnd();
3457 if ((end.lastpos() > 0 || end.lastpit() > 0)
3458 && end.pos() == end.lastpos() && beg.pos() == 0
3459 && end.pit() == end.lastpit() && beg.pit() == 0)
3467 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3469 x += scx_ + ADD_TO_TABULAR_WIDTH;
3471 BufferView * bv = pi.base.bv;
3472 Cursor & cur = pi.base.bv->cursor();
3475 // FIXME: As the full background is painted in drawSelection(),
3476 // we have no choice but to do a full repaint for the Text cells.
3477 pi.full_repaint = true;
3479 bool const original_selection_state = pi.selected;
3482 first_visible_cell = Tabular::npos;
3483 for (row_type r = 0; r < tabular.nrows(); ++r) {
3485 for (col_type c = 0; c < tabular.ncols(); ++c) {
3486 if (tabular.isPartOfMultiColumn(r, c))
3489 idx = tabular.cellIndex(r, c);
3491 if (tabular.isPartOfMultiRow(r, c)) {
3492 nx += tabular.cellWidth(idx);
3496 if (first_visible_cell == Tabular::npos)
3497 first_visible_cell = idx;
3499 pi.selected |= isCellSelected(cur, r, c);
3500 int const cx = nx + tabular.textHOffset(idx);
3501 int const cy = y + tabular.textVOffset(idx);
3502 // Cache the Inset position.
3503 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3504 cell(idx)->draw(pi, cx, cy);
3505 drawCellLines(pi, nx, y, r, idx);
3506 nx += tabular.cellWidth(idx);
3507 pi.selected = original_selection_state;
3510 if (r + 1 < tabular.nrows())
3511 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3512 + tabular.interRowSpace(r + 1);
3517 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3519 Cursor & cur = pi.base.bv->cursor();
3522 x += scx_ + ADD_TO_TABULAR_WIDTH;
3524 // FIXME: it is wrong to completely paint the background
3525 // if we want to do single row painting.
3527 // Paint background of current tabular
3528 int const w = tabular.width();
3529 int const h = tabular.height();
3530 int yy = y - tabular.rowAscent(0);
3531 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3533 if (!cur.selection())
3535 if (&cur.inset() != this)
3540 bool const full_cell_selected = isCellSelected(cur,
3541 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3543 if (cur.selIsMultiCell() || full_cell_selected) {
3544 for (row_type r = 0; r < tabular.nrows(); ++r) {
3546 for (col_type c = 0; c < tabular.ncols(); ++c) {
3547 if (tabular.isPartOfMultiColumn(r, c))
3550 idx_type const cell = tabular.cellIndex(r, c);
3552 if (tabular.isPartOfMultiRow(r, c)) {
3553 xx += tabular.cellWidth(cell);
3556 int const w = tabular.cellWidth(cell);
3557 int const h = tabular.cellHeight(cell);
3558 int const yy = y - tabular.rowAscent(r);
3559 if (isCellSelected(cur, r, c))
3560 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3563 if (r + 1 < tabular.nrows())
3564 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3565 + tabular.interRowSpace(r + 1);
3569 x += cellXPos(cur.idx());
3570 x += tabular.textHOffset(cur.idx());
3571 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3576 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3577 row_type row, idx_type cell) const
3579 y -= tabular.rowAscent(row);
3580 int const w = tabular.cellWidth(cell);
3581 int const h = tabular.cellHeight(cell);
3582 Color const linecolor = pi.textColor(Color_tabularline);
3583 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3586 bool drawline = tabular.topLine(cell)
3587 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3588 pi.pain.line(x, y, x + w, y,
3589 drawline ? linecolor : gridcolor,
3590 drawline ? Painter::line_solid : Painter::line_onoffdash);
3593 drawline = tabular.bottomLine(cell);
3594 pi.pain.line(x, y + h, x + w, y + h,
3595 drawline ? linecolor : gridcolor,
3596 drawline ? Painter::line_solid : Painter::line_onoffdash);
3599 col_type const col = tabular.cellColumn(cell);
3600 drawline = tabular.leftLine(cell)
3601 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3602 pi.pain.line(x, y, x, y + h,
3603 drawline ? linecolor : gridcolor,
3604 drawline ? Painter::line_solid : Painter::line_onoffdash);
3607 x -= tabular.interColumnSpace(cell);
3608 drawline = tabular.rightLine(cell)
3609 || (col + 1 < tabular.ncols()
3610 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3611 pi.pain.line(x + w, y, x + w, y + h,
3612 drawline ? linecolor : gridcolor,
3613 drawline ? Painter::line_solid : Painter::line_onoffdash);
3617 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3619 //lyxerr << "InsetTabular::edit: " << this << endl;
3621 cur.setSelection(false);
3624 if (isRightToLeft(cur))
3625 cur.idx() = tabular.getLastCellInRow(0);
3631 if (isRightToLeft(cur))
3632 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3634 cur.idx() = tabular.numberofcells - 1;
3636 cur.pos() = cur.lastpos(); // FIXME crude guess
3638 cur.setCurrentFont();
3639 // FIXME: this accesses the position cache before it is initialized
3641 //cur.bv().fitCursor();
3645 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3647 // In a longtable, tell captions what the current float is
3648 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3649 string const saveflt = cnts.current_float();
3650 if (tabular.is_long_tabular)
3651 cnts.current_float("table");
3653 ParIterator it2 = it;
3655 size_t const end = it2.nargs();
3656 for ( ; it2.idx() < end; it2.top().forwardIdx())
3657 buffer().updateBuffer(it2, utype);
3660 if (tabular.is_long_tabular)
3661 cnts.current_float(saveflt);
3665 void InsetTabular::addToToc(DocIterator const & cpit)
3667 DocIterator dit = cpit;
3669 size_t const end = dit.nargs();
3670 for ( ; dit.idx() < end; dit.top().forwardIdx())
3671 cell(dit.idx())->addToToc(dit);
3675 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3677 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3678 << "\n cur:" << cur);
3679 CursorSlice sl = cur.top();
3680 Cursor & bvcur = cur.bv().cursor();
3682 FuncCode const act = cmd.action();
3686 case LFUN_MOUSE_PRESS: {
3687 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3689 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3690 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3691 row_type r = rowFromY(cur, cmd.y());
3692 cur.idx() = tabular.getFirstCellInRow(r);
3695 cur.idx() = tabular.getLastCellInRow(r);
3696 cur.pos() = cur.lastpos();
3697 cur.setSelection(true);
3703 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3704 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3705 || cmd.y() > y0 + tabular.height()) {
3706 col_type c = columnFromX(cur, cmd.x());
3707 cur.idx() = tabular.cellIndex(0, c);
3710 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3711 cur.pos() = cur.lastpos();
3712 cur.setSelection(true);
3717 // do not reset cursor/selection if we have selected
3718 // some cells (bug 2715).
3719 if (cmd.button() == mouse_button::button3
3720 && &bvcur.selBegin().inset() == this
3721 && bvcur.selIsMultiCell())
3724 // Let InsetTableCell do it
3725 cell(cur.idx())->dispatch(cur, cmd);
3728 case LFUN_MOUSE_MOTION:
3729 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3730 if (cmd.button() == mouse_button::button1) {
3731 // only accept motions to places not deeper nested than the real anchor
3732 if (!bvcur.realAnchor().hasPart(cur)) {
3736 // select (additional) row
3738 row_type r = rowFromY(cur, cmd.y());
3739 cur.idx() = tabular.getLastCellInRow(r);
3740 // we need to reset the cursor's pit and pos now, as the old ones
3741 // may no longer be valid.
3744 bvcur.setCursor(cur);
3745 bvcur.setSelection(true);
3748 // select (additional) column
3750 col_type c = columnFromX(cur, cmd.x());
3751 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3752 // we need to reset the cursor's pit and pos now, as the old ones
3753 // may no longer be valid.
3756 bvcur.setCursor(cur);
3757 bvcur.setSelection(true);
3760 // only update if selection changes
3761 if (bvcur.idx() == cur.idx() &&
3762 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3763 cur.noScreenUpdate();
3764 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3765 bvcur.setCursor(cur);
3766 bvcur.setSelection(true);
3767 // if this is a multicell selection, we just set the cursor to
3768 // the beginning of the cell's text.
3769 if (bvcur.selIsMultiCell()) {
3770 bvcur.pit() = bvcur.lastpit();
3771 bvcur.pos() = bvcur.lastpos();
3776 case LFUN_MOUSE_RELEASE:
3781 case LFUN_CELL_BACKWARD:
3783 cur.setSelection(false);
3786 case LFUN_CELL_FORWARD:
3788 cur.setSelection(false);
3791 case LFUN_CHAR_FORWARD_SELECT:
3792 case LFUN_CHAR_FORWARD:
3793 case LFUN_CHAR_BACKWARD_SELECT:
3794 case LFUN_CHAR_BACKWARD:
3795 case LFUN_CHAR_RIGHT_SELECT:
3796 case LFUN_CHAR_RIGHT:
3797 case LFUN_CHAR_LEFT_SELECT:
3798 case LFUN_CHAR_LEFT:
3799 case LFUN_WORD_FORWARD:
3800 case LFUN_WORD_FORWARD_SELECT:
3801 case LFUN_WORD_BACKWARD:
3802 case LFUN_WORD_BACKWARD_SELECT:
3803 case LFUN_WORD_RIGHT:
3804 case LFUN_WORD_RIGHT_SELECT:
3805 case LFUN_WORD_LEFT:
3806 case LFUN_WORD_LEFT_SELECT: {
3807 // determine whether we move to next or previous cell, where to enter
3808 // the new cell from, and which command to "finish" (i.e., exit the
3811 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3812 FuncCode finish_lfun;
3814 if (act == LFUN_CHAR_FORWARD
3815 || act == LFUN_CHAR_FORWARD_SELECT
3816 || act == LFUN_WORD_FORWARD
3817 || act == LFUN_WORD_FORWARD_SELECT) {
3819 finish_lfun = LFUN_FINISHED_FORWARD;
3821 else if (act == LFUN_CHAR_BACKWARD
3822 || act == LFUN_CHAR_BACKWARD_SELECT
3823 || act == LFUN_WORD_BACKWARD
3824 || act == LFUN_WORD_BACKWARD_SELECT) {
3826 finish_lfun = LFUN_FINISHED_BACKWARD;
3828 // LEFT or RIGHT commands --- the interpretation will depend on the
3829 // table's direction.
3831 bool const right = act == LFUN_CHAR_RIGHT
3832 || act == LFUN_CHAR_RIGHT_SELECT
3833 || act == LFUN_WORD_RIGHT
3834 || act == LFUN_WORD_RIGHT_SELECT;
3835 next_cell = isRightToLeft(cur) != right;
3837 if (lyxrc.visual_cursor)
3838 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3840 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3843 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3844 || act == LFUN_CHAR_BACKWARD_SELECT
3845 || act == LFUN_CHAR_RIGHT_SELECT
3846 || act == LFUN_CHAR_LEFT_SELECT
3847 || act == LFUN_WORD_FORWARD_SELECT
3848 || act == LFUN_WORD_RIGHT_SELECT
3849 || act == LFUN_WORD_BACKWARD_SELECT
3850 || act == LFUN_WORD_LEFT_SELECT;
3852 // If we have a multicell selection or we're
3853 // not doing some LFUN_*_SELECT thing anyway...
3854 if (!cur.selIsMultiCell() || !select) {
3855 col_type const c = tabular.cellColumn(cur.idx());
3856 row_type const r = tabular.cellRow(cur.idx());
3857 // Are we trying to select the whole cell and is the whole cell
3858 // not yet selected?
3859 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3860 ((next_cell && cur.pit() == cur.lastpit()
3861 && cur.pos() == cur.lastpos())
3862 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3864 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3866 // ...try to dispatch to the cell's inset.
3867 cell(cur.idx())->dispatch(cur, cmd);
3869 // When we already have a selection we want to select the whole cell
3870 // before going to the next cell.
3871 if (select_whole && !empty_cell){
3872 getText(cur.idx())->selectAll(cur);
3877 // FIXME: When we support the selection of an empty cell, remove
3878 // the !empty_cell from this condition. For now we jump to the next
3879 // cell if the current cell is empty.
3880 if (cur.result().dispatched() && !empty_cell)
3884 // move to next/prev cell, as appropriate
3885 // note that we will always do this if we're selecting and we have
3886 // a multicell selection
3887 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3888 << " cell from: " << int(entry_from));
3890 moveNextCell(cur, entry_from);
3892 movePrevCell(cur, entry_from);
3893 // if we're exiting the table, call the appropriate FINISHED lfun
3894 if (sl == cur.top())
3895 cmd = FuncRequest(finish_lfun);
3902 case LFUN_DOWN_SELECT:
3904 if (!(cur.selection() && cur.selIsMultiCell()))
3905 cell(cur.idx())->dispatch(cur, cmd);
3907 cur.dispatched(); // override the cell's decision
3908 if (sl == cur.top()) {
3909 // if our Text didn't do anything to the cursor
3910 // then we try to put the cursor into the cell below
3911 // setting also the right targetX.
3912 cur.selHandle(act == LFUN_DOWN_SELECT);
3913 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3914 cur.idx() = tabular.cellBelow(cur.idx());
3916 TextMetrics const & tm =
3917 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3918 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3919 cur.setCurrentFont();
3922 if (sl == cur.top()) {
3923 // we trick it to go to forward after leaving the
3925 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3928 if (cur.selIsMultiCell()) {
3929 cur.pit() = cur.lastpit();
3930 cur.pos() = cur.lastpos();
3931 cur.setCurrentFont();
3936 case LFUN_UP_SELECT:
3938 if (!(cur.selection() && cur.selIsMultiCell()))
3939 cell(cur.idx())->dispatch(cur, cmd);
3940 cur.dispatched(); // override the cell's decision
3941 if (sl == cur.top()) {
3942 // if our Text didn't do anything to the cursor
3943 // then we try to put the cursor into the cell above
3944 // setting also the right targetX.
3945 cur.selHandle(act == LFUN_UP_SELECT);
3946 if (tabular.cellRow(cur.idx()) != 0) {
3947 cur.idx() = tabular.cellAbove(cur.idx());
3948 cur.pit() = cur.lastpit();
3949 Text const * text = cell(cur.idx())->getText(0);
3950 TextMetrics const & tm = cur.bv().textMetrics(text);
3951 ParagraphMetrics const & pm =
3952 tm.parMetrics(cur.lastpit());
3953 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3954 cur.setCurrentFont();
3957 if (sl == cur.top()) {
3958 cmd = FuncRequest(LFUN_UP);
3961 if (cur.selIsMultiCell()) {
3963 cur.pos() = cur.lastpos();
3964 cur.setCurrentFont();
3969 // case LFUN_SCREEN_DOWN: {
3970 // //if (hasSelection())
3971 // // cur.selection() = false;
3972 // col_type const col = tabular.cellColumn(cur.idx());
3973 // int const t = cur.bv().top_y() + cur.bv().height();
3974 // if (t < yo() + tabular.getHeightOfTabular()) {
3975 // cur.bv().scrollDocView(t, true);
3976 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3978 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3985 // case LFUN_SCREEN_UP: {
3986 // //if (hasSelection())
3987 // // cur.selection() = false;
3988 // col_type const col = tabular.cellColumn(cur.idx());
3989 // int const t = cur.bv().top_y() + cur.bv().height();
3991 // cur.bv().scrollDocView(t, true);
3995 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3999 // cur.par() = cur.lastpar();
4000 // cur.pos() = cur.lastpos();
4004 case LFUN_LAYOUT_TABULAR:
4005 cur.bv().showDialog("tabular");
4008 case LFUN_INSET_MODIFY:
4009 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4013 // insert file functions
4014 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4015 case LFUN_FILE_INSERT_PLAINTEXT:
4017 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4018 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4019 FileName(to_utf8(cmd.argument())));
4022 cur.recordUndoInset(INSERT_UNDO);
4023 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4024 // content has been replaced,
4025 // so cursor might be invalid
4026 cur.pos() = cur.lastpos();
4027 cur.pit() = cur.lastpit();
4028 bvcur.setCursor(cur);
4035 if (cur.selIsMultiCell()) {
4036 if (copySelection(cur)) {
4037 cur.recordUndoInset(DELETE_UNDO);
4041 cell(cur.idx())->dispatch(cur, cmd);
4044 case LFUN_SELF_INSERT:
4045 if (cur.selIsMultiCell()) {
4046 cur.recordUndoInset(DELETE_UNDO);
4049 cell(cur.idx())->dispatch(cur, cmd);
4052 case LFUN_CHAR_DELETE_BACKWARD:
4053 case LFUN_CHAR_DELETE_FORWARD:
4054 if (cur.selIsMultiCell()) {
4055 cur.recordUndoInset(DELETE_UNDO);
4058 cell(cur.idx())->dispatch(cur, cmd);
4062 if (!cur.selection())
4064 if (cur.selIsMultiCell()) {
4068 cell(cur.idx())->dispatch(cur, cmd);
4071 case LFUN_CLIPBOARD_PASTE:
4072 case LFUN_PRIMARY_SELECTION_PASTE: {
4073 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4074 theClipboard().getAsText() :
4075 theSelection().get();
4078 // pass to InsertPlaintextString, but
4079 // only if we have multi-cell content
4080 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4081 cur.recordUndoInset(INSERT_UNDO);
4082 if (insertPlaintextString(cur.bv(), clip, false)) {
4083 // content has been replaced,
4084 // so cursor might be invalid
4085 cur.pos() = cur.lastpos();
4086 cur.pit() = cur.lastpit();
4087 bvcur.setCursor(cur);
4091 // Let the cell handle normal text
4092 cell(cur.idx())->dispatch(cur, cmd);
4097 if (!tabularStackDirty()) {
4098 if (!cur.selIsMultiCell())
4099 cell(cur.idx())->dispatch(cur, cmd);
4102 if (theClipboard().isInternal() ||
4103 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4104 cur.recordUndoInset(INSERT_UNDO);
4105 pasteClipboard(cur);
4109 case LFUN_FONT_EMPH:
4110 case LFUN_FONT_BOLD:
4111 case LFUN_FONT_BOLDSYMBOL:
4112 case LFUN_FONT_ROMAN:
4113 case LFUN_FONT_NOUN:
4114 case LFUN_FONT_ITAL:
4115 case LFUN_FONT_FRAK:
4116 case LFUN_FONT_TYPEWRITER:
4117 case LFUN_FONT_SANS:
4118 case LFUN_TEXTSTYLE_APPLY:
4119 case LFUN_TEXTSTYLE_UPDATE:
4120 case LFUN_FONT_SIZE:
4121 case LFUN_FONT_UNDERLINE:
4122 case LFUN_FONT_STRIKEOUT:
4123 case LFUN_FONT_UULINE:
4124 case LFUN_FONT_UWAVE:
4126 case LFUN_WORD_CAPITALIZE:
4127 case LFUN_WORD_UPCASE:
4128 case LFUN_WORD_LOWCASE:
4129 case LFUN_CHARS_TRANSPOSE:
4130 if (cur.selIsMultiCell()) {
4133 getSelection(cur, rs, re, cs, ce);
4134 Cursor tmpcur = cur;
4135 for (row_type r = rs; r <= re; ++r) {
4136 for (col_type c = cs; c <= ce; ++c) {
4137 // cursor follows cell:
4138 tmpcur.idx() = tabular.cellIndex(r, c);
4139 // select this cell only:
4142 tmpcur.resetAnchor();
4143 tmpcur.pit() = tmpcur.lastpit();
4144 tmpcur.pos() = tmpcur.top().lastpos();
4145 tmpcur.setCursor(tmpcur);
4146 tmpcur.setSelection();
4147 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4152 cell(cur.idx())->dispatch(cur, cmd);
4156 case LFUN_INSET_SETTINGS:
4157 // relay this lfun to Inset, not to the cell.
4158 Inset::doDispatch(cur, cmd);
4162 // we try to handle this event in the insets dispatch function.
4163 cell(cur.idx())->dispatch(cur, cmd);
4169 // function sets an object as defined in func_status.h:
4170 // states OK, Unknown, Disabled, On, Off.
4171 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4172 FuncStatus & status) const
4174 switch (cmd.action()) {
4175 case LFUN_INSET_MODIFY: {
4176 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4179 string const s = cmd.getArg(1);
4180 // FIXME: We only check for the very first argument...
4181 int action = Tabular::LAST_ACTION;
4183 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4184 if (tabularFeature[i].feature == s) {
4185 action = tabularFeature[i].action;
4189 if (action == Tabular::LAST_ACTION) {
4191 status.setUnknown(true);
4195 string const argument = cmd.getLongArg(2);
4197 row_type sel_row_start = 0;
4198 row_type sel_row_end = 0;
4199 col_type sel_col_start = 0;
4200 col_type sel_col_end = 0;
4201 Tabular::ltType dummyltt;
4204 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4207 case Tabular::SET_PWIDTH:
4208 case Tabular::SET_MPWIDTH:
4209 case Tabular::SET_SPECIAL_COLUMN:
4210 case Tabular::SET_SPECIAL_MULTICOLUMN:
4211 case Tabular::APPEND_ROW:
4212 case Tabular::APPEND_COLUMN:
4213 case Tabular::DELETE_ROW:
4214 case Tabular::DELETE_COLUMN:
4215 case Tabular::COPY_ROW:
4216 case Tabular::COPY_COLUMN:
4217 case Tabular::SET_TOP_SPACE:
4218 case Tabular::SET_BOTTOM_SPACE:
4219 case Tabular::SET_INTERLINE_SPACE:
4223 case Tabular::SET_DECIMAL_POINT:
4225 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4228 case Tabular::SET_MULTICOLUMN:
4229 case Tabular::UNSET_MULTICOLUMN:
4230 case Tabular::MULTICOLUMN:
4231 // If a row is set as longtable caption, it must not be allowed
4232 // to unset that this row is a multicolumn.
4233 status.setEnabled(sel_row_start == sel_row_end
4234 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4235 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4238 case Tabular::SET_MULTIROW:
4239 case Tabular::UNSET_MULTIROW:
4240 case Tabular::MULTIROW:
4241 // If a row is set as longtable caption, it must not be allowed
4242 // to unset that this row is a multirow.
4243 status.setEnabled(sel_col_start == sel_col_end
4244 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4245 status.setOnOff(tabular.isMultiRow(cur.idx()));
4248 case Tabular::SET_ALL_LINES:
4249 case Tabular::UNSET_ALL_LINES:
4250 case Tabular::SET_BORDER_LINES:
4251 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4254 case Tabular::SET_LINE_TOP:
4255 case Tabular::SET_LINE_BOTTOM:
4256 case Tabular::SET_LINE_LEFT:
4257 case Tabular::SET_LINE_RIGHT:
4258 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4261 case Tabular::TOGGLE_LINE_TOP:
4262 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4263 status.setOnOff(tabular.topLine(cur.idx()));
4266 case Tabular::TOGGLE_LINE_BOTTOM:
4267 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4268 status.setOnOff(tabular.bottomLine(cur.idx()));
4271 case Tabular::TOGGLE_LINE_LEFT:
4272 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4273 status.setOnOff(tabular.leftLine(cur.idx()));
4276 case Tabular::TOGGLE_LINE_RIGHT:
4277 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4278 status.setOnOff(tabular.rightLine(cur.idx()));
4281 case Tabular::M_ALIGN_LEFT:
4283 case Tabular::ALIGN_LEFT:
4284 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4285 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4288 case Tabular::M_ALIGN_RIGHT:
4290 case Tabular::ALIGN_RIGHT:
4291 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4292 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4295 case Tabular::M_ALIGN_CENTER:
4297 case Tabular::ALIGN_CENTER:
4298 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4301 case Tabular::ALIGN_BLOCK:
4302 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4303 && !tabular.isMultiRow(cur.idx()));
4304 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4307 case Tabular::ALIGN_DECIMAL:
4308 status.setEnabled(!tabular.isMultiRow(cur.idx())
4309 && !tabular.isMultiColumn(cur.idx()));
4310 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4313 case Tabular::M_VALIGN_TOP:
4315 case Tabular::VALIGN_TOP:
4316 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4317 && !tabular.isMultiRow(cur.idx()));
4319 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4322 case Tabular::M_VALIGN_BOTTOM:
4324 case Tabular::VALIGN_BOTTOM:
4325 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4326 && !tabular.isMultiRow(cur.idx()));
4328 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4331 case Tabular::M_VALIGN_MIDDLE:
4333 case Tabular::VALIGN_MIDDLE:
4334 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4335 && !tabular.isMultiRow(cur.idx()));
4337 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4340 case Tabular::SET_LONGTABULAR:
4341 // setting as longtable is not allowed when table is inside a float
4342 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4343 || cur.innerInsetOfType(WRAP_CODE) != 0)
4344 status.setEnabled(false);
4346 status.setEnabled(true);
4347 status.setOnOff(tabular.is_long_tabular);
4350 case Tabular::UNSET_LONGTABULAR:
4351 status.setOnOff(!tabular.is_long_tabular);
4354 case Tabular::TOGGLE_ROTATE_TABULAR:
4355 case Tabular::SET_ROTATE_TABULAR:
4356 status.setOnOff(tabular.rotate);
4359 case Tabular::TABULAR_VALIGN_TOP:
4360 status.setOnOff(tabular.tabular_valignment
4361 == Tabular::LYX_VALIGN_TOP);
4363 case Tabular::TABULAR_VALIGN_MIDDLE:
4364 status.setOnOff(tabular.tabular_valignment
4365 == Tabular::LYX_VALIGN_MIDDLE);
4367 case Tabular::TABULAR_VALIGN_BOTTOM:
4368 status.setOnOff(tabular.tabular_valignment
4369 == Tabular::LYX_VALIGN_BOTTOM);
4372 case Tabular::LONGTABULAR_ALIGN_LEFT:
4373 status.setOnOff(tabular.longtabular_alignment
4374 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4376 case Tabular::LONGTABULAR_ALIGN_CENTER:
4377 status.setOnOff(tabular.longtabular_alignment
4378 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4380 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4381 status.setOnOff(tabular.longtabular_alignment
4382 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4385 case Tabular::UNSET_ROTATE_TABULAR:
4386 status.setOnOff(!tabular.rotate);
4389 case Tabular::TOGGLE_ROTATE_CELL:
4390 case Tabular::SET_ROTATE_CELL:
4391 status.setOnOff(!oneCellHasRotationState(false,
4392 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4395 case Tabular::UNSET_ROTATE_CELL:
4396 status.setOnOff(!oneCellHasRotationState(true,
4397 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4400 case Tabular::SET_USEBOX:
4401 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4404 // every row can only be one thing:
4405 // either a footer or header or caption
4406 case Tabular::SET_LTFIRSTHEAD:
4407 status.setEnabled(sel_row_start == sel_row_end
4408 && !tabular.ltCaption(sel_row_start));
4409 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4412 case Tabular::UNSET_LTFIRSTHEAD:
4413 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4416 case Tabular::SET_LTHEAD:
4417 status.setEnabled(sel_row_start == sel_row_end
4418 && !tabular.ltCaption(sel_row_start));
4419 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4422 case Tabular::UNSET_LTHEAD:
4423 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4426 case Tabular::SET_LTFOOT:
4427 status.setEnabled(sel_row_start == sel_row_end
4428 && !tabular.ltCaption(sel_row_start));
4429 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4432 case Tabular::UNSET_LTFOOT:
4433 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4436 case Tabular::SET_LTLASTFOOT:
4437 status.setEnabled(sel_row_start == sel_row_end
4438 && !tabular.ltCaption(sel_row_start));
4439 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4442 case Tabular::UNSET_LTLASTFOOT:
4443 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4446 case Tabular::SET_LTNEWPAGE:
4447 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4450 // only one row can be the caption
4451 // and a multirow cannot be set as caption
4452 case Tabular::SET_LTCAPTION:
4453 case Tabular::UNSET_LTCAPTION:
4454 case Tabular::TOGGLE_LTCAPTION:
4455 status.setEnabled(sel_row_start == sel_row_end
4456 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4457 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4458 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4459 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4460 && (!tabular.haveLTCaption()
4461 || tabular.ltCaption(sel_row_start))
4462 && !tabular.isMultiRow(sel_row_start));
4463 status.setOnOff(tabular.ltCaption(sel_row_start));
4466 case Tabular::SET_BOOKTABS:
4467 status.setOnOff(tabular.use_booktabs);
4470 case Tabular::UNSET_BOOKTABS:
4471 status.setOnOff(!tabular.use_booktabs);
4476 status.setEnabled(false);
4482 // These are only enabled inside tabular
4483 case LFUN_CELL_BACKWARD:
4484 case LFUN_CELL_FORWARD:
4485 status.setEnabled(true);
4488 // disable these with multiple cells selected
4489 case LFUN_INSET_INSERT:
4490 case LFUN_TABULAR_INSERT:
4491 case LFUN_FLEX_INSERT:
4492 case LFUN_FLOAT_INSERT:
4493 case LFUN_FLOAT_WIDE_INSERT:
4494 case LFUN_FOOTNOTE_INSERT:
4495 case LFUN_MARGINALNOTE_INSERT:
4496 case LFUN_MATH_INSERT:
4497 case LFUN_MATH_MODE:
4498 case LFUN_MATH_MUTATE:
4499 case LFUN_MATH_DISPLAY:
4500 case LFUN_NOTE_INSERT:
4501 case LFUN_ARGUMENT_INSERT:
4502 case LFUN_BOX_INSERT:
4503 case LFUN_BRANCH_INSERT:
4504 case LFUN_PHANTOM_INSERT:
4505 case LFUN_WRAP_INSERT:
4506 case LFUN_PREVIEW_INSERT:
4507 case LFUN_ERT_INSERT: {
4508 if (cur.selIsMultiCell()) {
4509 status.setEnabled(false);
4512 return cell(cur.idx())->getStatus(cur, cmd, status);
4515 // disable in non-fixed-width cells
4516 case LFUN_NEWLINE_INSERT:
4517 case LFUN_BREAK_PARAGRAPH: {
4518 if (tabular.getPWidth(cur.idx()).zero()) {
4519 status.setEnabled(false);
4522 return cell(cur.idx())->getStatus(cur, cmd, status);
4525 case LFUN_NEWPAGE_INSERT:
4526 status.setEnabled(false);
4530 if (tabularStackDirty() && theClipboard().isInternal()) {
4531 if (cur.selIsMultiCell()) {
4534 getSelection(cur, rs, re, cs, ce);
4535 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4536 && paste_tabular->nrows() == re - rs + 1)
4537 status.setEnabled(true);
4539 status.setEnabled(false);
4540 status.message(_("Selection size should match clipboard content."));
4543 status.setEnabled(true);
4546 return cell(cur.idx())->getStatus(cur, cmd, status);
4548 case LFUN_INSET_SETTINGS:
4549 // relay this lfun to Inset, not to the cell.
4550 return Inset::getStatus(cur, cmd, status);
4553 // we try to handle this event in the insets dispatch function.
4554 return cell(cur.idx())->getStatus(cur, cmd, status);
4560 Inset::DisplayType InsetTabular::display() const
4562 if (tabular.is_long_tabular) {
4563 switch (tabular.longtabular_alignment) {
4564 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4566 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4568 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4578 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4580 return tabular.latex(os, runparams);
4584 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4586 os << '\n'; // output table on a new line
4587 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4588 tabular.plaintext(os, runparams, dp, false, 0);
4589 return PLAINTEXT_NEWLINE;
4593 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4598 // FIXME: Why not pass a proper DocIterator here?
4600 // if the table is inside a float it doesn't need the informaltable
4601 // wrapper. Search for it.
4602 for (master = owner(); master; master = master->owner())
4603 if (master->lyxCode() == FLOAT_CODE)
4608 os << "<informaltable>";
4611 ret += tabular.docbook(os, runparams);
4613 os << "</informaltable>";
4620 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4623 // It'd be better to be able to get this from an InsetLayout, but at present
4624 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4625 xs << html::StartTag("table");
4626 docstring ret = tabular.xhtml(xs, rp);
4627 xs << html::EndTag("table");
4632 void InsetTabular::validate(LaTeXFeatures & features) const
4634 tabular.validate(features);
4636 // It'd be better to be able to get this from an InsetLayout, but at present
4637 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4638 if (features.runparams().flavor == OutputParams::HTML)
4639 features.addPreambleSnippet("<style type=\"text/css\">\n"
4640 "table { border: 1px solid black; display: inline-block; }\n"
4641 "td { border: 1px solid black; padding: 0.5ex; }\n"
4646 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4648 return tabular.cellInset(idx);
4652 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4654 return tabular.cellInset(idx);
4658 void InsetTabular::cursorPos(BufferView const & bv,
4659 CursorSlice const & sl, bool boundary, int & x, int & y) const
4661 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4663 int const row = tabular.cellRow(sl.idx());
4664 int const col = tabular.cellColumn(sl.idx());
4666 // y offset correction
4667 for (int r = 0; r < row; ++r)
4668 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4669 + tabular.interRowSpace(r + 1);
4671 y += tabular.textVOffset(sl.idx());
4673 // x offset correction
4674 for (int c = 0; c < col; ++c)
4675 x += tabular.column_info[c].width;
4677 x += tabular.textHOffset(sl.idx());
4678 x += ADD_TO_TABULAR_WIDTH;
4683 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4687 Inset const & inset = *tabular.cellInset(cell);
4688 Point o = bv.coordCache().getInsets().xy(&inset);
4689 int const xbeg = o.x_ - tabular.textHOffset(cell);
4690 int const xend = xbeg + tabular.cellWidth(cell);
4691 row_type const row = tabular.cellRow(cell);
4692 int const ybeg = o.y_ - tabular.rowAscent(row)
4693 - tabular.interRowSpace(row);
4694 int const yend = ybeg + tabular.cellHeight(cell);
4706 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4707 // << " ybeg=" << ybeg << " yend=" << yend
4708 // << " xx=" << xx << " yy=" << yy
4709 // << " dist=" << xx + yy << endl;
4714 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4716 //lyxerr << "InsetTabular::editXY: " << this << endl;
4717 cur.setSelection(false);
4719 cur.idx() = getNearestCell(cur.bv(), x, y);
4721 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4725 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4727 cur.idx() = getNearestCell(cur.bv(), x, y);
4728 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4732 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4734 idx_type idx_min = 0;
4735 int dist_min = numeric_limits<int>::max();
4736 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4737 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4738 int const d = dist(bv, i, x, y);
4749 int InsetTabular::cellXPos(idx_type const cell) const
4751 col_type col = tabular.cellColumn(cell);
4753 for (col_type c = 0; c < col; ++c)
4754 lx += tabular.column_info[c].width;
4760 void InsetTabular::resetPos(Cursor & cur) const
4762 BufferView & bv = cur.bv();
4763 int const maxwidth = bv.workWidth();
4765 int const scx_old = scx_;
4766 int const i = cur.find(this);
4771 int const X2 = maxwidth;
4772 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4773 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4774 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4777 scx_ = X1 + 20 - x1;
4779 scx_ = X2 - 20 - x2;
4784 // only update if offset changed
4785 if (scx_ != scx_old)
4786 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4790 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4792 row_type const row = tabular.cellRow(cur.idx());
4793 col_type const col = tabular.cellColumn(cur.idx());
4795 if (isRightToLeft(cur)) {
4796 if (tabular.cellColumn(cur.idx()) == 0) {
4797 if (row == tabular.nrows() - 1)
4799 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4804 cur.idx() = tabular.getLastCellInRow(row - 1);
4806 cur.idx() = tabular.cellIndex(row, col - 1);
4809 if (tabular.isLastCell(cur.idx()))
4811 if (cur.idx() == tabular.getLastCellInRow(row))
4812 cur.idx() = tabular.cellIndex(row + 1, 0);
4814 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4815 cur.idx() = tabular.cellIndex(row, colnextcell);
4819 cur.boundary(false);
4821 if (cur.selIsMultiCell()) {
4822 cur.pit() = cur.lastpit();
4823 cur.pos() = cur.lastpos();
4831 // in visual mode, place cursor at extreme left or right
4833 switch(entry_from) {
4835 case ENTRY_DIRECTION_RIGHT:
4836 cur.posVisToRowExtremity(false /* !left */);
4838 case ENTRY_DIRECTION_LEFT:
4839 cur.posVisToRowExtremity(true /* left */);
4841 case ENTRY_DIRECTION_IGNORE:
4842 // nothing to do in this case
4846 cur.setCurrentFont();
4851 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4853 row_type const row = tabular.cellRow(cur.idx());
4854 col_type const col = tabular.cellColumn(cur.idx());
4856 if (isRightToLeft(cur)) {
4857 if (cur.idx() == tabular.getLastCellInRow(row)) {
4860 cur.idx() = tabular.getFirstCellInRow(row);
4861 cur.idx() = tabular.cellAbove(cur.idx());
4863 if (tabular.isLastCell(cur.idx()))
4865 if (cur.idx() == tabular.getLastCellInRow(row))
4866 cur.idx() = tabular.cellIndex(row + 1, 0);
4868 cur.idx() = tabular.cellIndex(row, col + 1);
4871 if (cur.idx() == 0) // first cell
4874 cur.idx() = tabular.getLastCellInRow(row - 1);
4876 cur.idx() = tabular.cellIndex(row, col - 1);
4879 if (cur.selIsMultiCell()) {
4880 cur.pit() = cur.lastpit();
4881 cur.pos() = cur.lastpos();
4886 cur.pit() = cur.lastpit();
4887 cur.pos() = cur.lastpos();
4889 // in visual mode, place cursor at extreme left or right
4891 switch(entry_from) {
4893 case ENTRY_DIRECTION_RIGHT:
4894 cur.posVisToRowExtremity(false /* !left */);
4896 case ENTRY_DIRECTION_LEFT:
4897 cur.posVisToRowExtremity(true /* left */);
4899 case ENTRY_DIRECTION_IGNORE:
4900 // nothing to do in this case
4904 cur.setCurrentFont();
4909 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4911 istringstream is(argument);
4914 if (insetCode(s) != TABULAR_CODE)
4918 size_t safe_guard = 0;
4923 if (safe_guard > 1000) {
4924 LYXERR0("parameter max count reached!");
4928 Tabular::Feature action = Tabular::LAST_ACTION;
4931 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4932 if (s != tabularFeature[i].feature)
4935 action = tabularFeature[i].action;
4938 if (action == Tabular::LAST_ACTION) {
4939 LYXERR0("Feature not found " << s);
4943 if (tabularFeature[i].need_value)
4945 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4946 tabularFeatures(cur, action, val);
4952 static void checkLongtableSpecial(Tabular::ltType & ltt,
4953 string const & special, bool & flag)
4955 if (special == "dl_above") {
4958 } else if (special == "dl_below") {
4959 ltt.bottomDL = flag;
4961 } else if (special == "empty") {
4971 bool InsetTabular::oneCellHasRotationState(bool rotated,
4972 row_type row_start, row_type row_end,
4973 col_type col_start, col_type col_end) const
4975 for (row_type r = row_start; r <= row_end; ++r)
4976 for (col_type c = col_start; c <= col_end; ++c)
4977 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4983 void InsetTabular::tabularFeatures(Cursor & cur,
4984 Tabular::Feature feature, string const & value)
4986 col_type sel_col_start;
4987 col_type sel_col_end;
4988 row_type sel_row_start;
4989 row_type sel_row_end;
4990 bool setLines = false;
4991 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4992 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4996 case Tabular::M_ALIGN_LEFT:
4997 case Tabular::ALIGN_LEFT:
4998 setAlign = LYX_ALIGN_LEFT;
5001 case Tabular::M_ALIGN_RIGHT:
5002 case Tabular::ALIGN_RIGHT:
5003 setAlign = LYX_ALIGN_RIGHT;
5006 case Tabular::M_ALIGN_CENTER:
5007 case Tabular::ALIGN_CENTER:
5008 setAlign = LYX_ALIGN_CENTER;
5011 case Tabular::ALIGN_BLOCK:
5012 setAlign = LYX_ALIGN_BLOCK;
5015 case Tabular::ALIGN_DECIMAL:
5016 setAlign = LYX_ALIGN_DECIMAL;
5019 case Tabular::M_VALIGN_TOP:
5020 case Tabular::VALIGN_TOP:
5021 setVAlign = Tabular::LYX_VALIGN_TOP;
5024 case Tabular::M_VALIGN_BOTTOM:
5025 case Tabular::VALIGN_BOTTOM:
5026 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5029 case Tabular::M_VALIGN_MIDDLE:
5030 case Tabular::VALIGN_MIDDLE:
5031 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5038 cur.recordUndoInset(ATOMIC_UNDO);
5040 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5041 row_type const row = tabular.cellRow(cur.idx());
5042 col_type const column = tabular.cellColumn(cur.idx());
5044 Tabular::ltType ltt;
5048 case Tabular::SET_PWIDTH: {
5049 Length const len(value);
5050 tabular.setColumnPWidth(cur, cur.idx(), len);
5052 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5053 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5057 case Tabular::SET_MPWIDTH:
5058 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5061 case Tabular::SET_SPECIAL_COLUMN:
5062 case Tabular::SET_SPECIAL_MULTICOLUMN:
5063 if (value == "none")
5064 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5066 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5069 case Tabular::APPEND_ROW:
5070 // append the row into the tabular
5071 tabular.appendRow(cur.idx());
5074 case Tabular::APPEND_COLUMN:
5075 // append the column into the tabular
5076 tabular.appendColumn(cur.idx());
5077 cur.idx() = tabular.cellIndex(row, column);
5080 case Tabular::DELETE_ROW:
5081 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5082 tabular.deleteRow(sel_row_start);
5083 if (sel_row_start >= tabular.nrows())
5085 cur.idx() = tabular.cellIndex(sel_row_start, column);
5088 cur.setSelection(false);
5091 case Tabular::DELETE_COLUMN:
5092 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5093 tabular.deleteColumn(sel_col_start);
5094 if (sel_col_start >= tabular.ncols())
5096 cur.idx() = tabular.cellIndex(row, sel_col_start);
5099 cur.setSelection(false);
5102 case Tabular::COPY_ROW:
5103 tabular.copyRow(row);
5106 case Tabular::COPY_COLUMN:
5107 tabular.copyColumn(column);
5108 cur.idx() = tabular.cellIndex(row, column);
5111 case Tabular::SET_LINE_TOP:
5112 case Tabular::TOGGLE_LINE_TOP: {
5113 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5114 ? (value == "true") : !tabular.topLine(cur.idx());
5115 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5116 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5117 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5121 case Tabular::SET_LINE_BOTTOM:
5122 case Tabular::TOGGLE_LINE_BOTTOM: {
5123 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5124 ? (value == "true") : !tabular.bottomLine(cur.idx());
5125 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5126 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5127 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5131 case Tabular::SET_LINE_LEFT:
5132 case Tabular::TOGGLE_LINE_LEFT: {
5133 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5134 ? (value == "true") : !tabular.leftLine(cur.idx());
5135 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5136 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5137 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5141 case Tabular::SET_LINE_RIGHT:
5142 case Tabular::TOGGLE_LINE_RIGHT: {
5143 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5144 ? (value == "true") : !tabular.rightLine(cur.idx());
5145 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5146 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5147 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5151 case Tabular::M_ALIGN_LEFT:
5152 case Tabular::M_ALIGN_RIGHT:
5153 case Tabular::M_ALIGN_CENTER:
5155 case Tabular::ALIGN_LEFT:
5156 case Tabular::ALIGN_RIGHT:
5157 case Tabular::ALIGN_CENTER:
5158 case Tabular::ALIGN_BLOCK:
5159 case Tabular::ALIGN_DECIMAL:
5160 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5161 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5162 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5165 case Tabular::M_VALIGN_TOP:
5166 case Tabular::M_VALIGN_BOTTOM:
5167 case Tabular::M_VALIGN_MIDDLE:
5169 case Tabular::VALIGN_TOP:
5170 case Tabular::VALIGN_BOTTOM:
5171 case Tabular::VALIGN_MIDDLE:
5172 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5173 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5174 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5177 case Tabular::SET_MULTICOLUMN: {
5178 if (!cur.selection()) {
5179 // just multicol for one single cell
5180 // check whether we are completely in a multicol
5181 if (!tabular.isMultiColumn(cur.idx()))
5182 tabular.setMultiColumn(cur.idx(), 1);
5185 // we have a selection so this means we just add all this
5186 // cells to form a multicolumn cell
5187 idx_type const s_start = cur.selBegin().idx();
5188 row_type const col_start = tabular.cellColumn(s_start);
5189 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5190 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5193 cur.setSelection(false);
5197 case Tabular::UNSET_MULTICOLUMN: {
5198 if (!cur.selection()) {
5199 if (tabular.isMultiColumn(cur.idx()))
5200 tabular.unsetMultiColumn(cur.idx());
5205 case Tabular::MULTICOLUMN: {
5206 if (tabular.isMultiColumn(cur.idx()))
5207 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5209 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5213 case Tabular::SET_MULTIROW: {
5214 if (!cur.selection()) {
5215 // just multirow for one single cell
5216 // check whether we are completely in a multirow
5217 if (!tabular.isMultiRow(cur.idx()))
5218 tabular.setMultiRow(cur.idx(), 1);
5221 // we have a selection so this means we just add all this
5222 // cells to form a multirow cell
5223 idx_type const s_start = cur.selBegin().idx();
5224 row_type const row_start = tabular.cellRow(s_start);
5225 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5226 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5229 cur.setSelection(false);
5233 case Tabular::UNSET_MULTIROW: {
5234 if (!cur.selection()) {
5235 if (tabular.isMultiRow(cur.idx()))
5236 tabular.unsetMultiRow(cur.idx());
5241 case Tabular::MULTIROW: {
5242 if (tabular.isMultiRow(cur.idx()))
5243 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5245 tabularFeatures(cur, Tabular::SET_MULTIROW);
5249 case Tabular::SET_ALL_LINES:
5251 case Tabular::UNSET_ALL_LINES:
5252 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5253 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5254 idx_type const cell = tabular.cellIndex(r, c);
5255 tabular.setTopLine(cell, setLines);
5256 tabular.setBottomLine(cell, setLines);
5257 tabular.setRightLine(cell, setLines);
5258 tabular.setLeftLine(cell, setLines);
5262 case Tabular::SET_BORDER_LINES:
5263 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5264 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5265 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5267 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5268 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5269 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5273 case Tabular::SET_LONGTABULAR:
5274 tabular.is_long_tabular = true;
5277 case Tabular::UNSET_LONGTABULAR:
5278 for (row_type r = 0; r < tabular.nrows(); ++r) {
5279 if (tabular.ltCaption(r)) {
5280 cur.idx() = tabular.cellIndex(r, 0);
5283 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5286 tabular.is_long_tabular = false;
5289 case Tabular::SET_ROTATE_TABULAR:
5290 tabular.rotate = true;
5293 case Tabular::UNSET_ROTATE_TABULAR:
5294 tabular.rotate = false;
5297 case Tabular::TOGGLE_ROTATE_TABULAR:
5298 tabular.rotate = !tabular.rotate;
5301 case Tabular::TABULAR_VALIGN_TOP:
5302 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5305 case Tabular::TABULAR_VALIGN_MIDDLE:
5306 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5309 case Tabular::TABULAR_VALIGN_BOTTOM:
5310 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5313 case Tabular::LONGTABULAR_ALIGN_LEFT:
5314 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5317 case Tabular::LONGTABULAR_ALIGN_CENTER:
5318 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5321 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5322 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5327 case Tabular::SET_ROTATE_CELL:
5328 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5329 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5330 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5333 case Tabular::UNSET_ROTATE_CELL:
5334 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5335 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5336 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5339 case Tabular::TOGGLE_ROTATE_CELL:
5341 bool oneNotRotated = oneCellHasRotationState(false,
5342 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5344 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5345 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5346 tabular.setRotateCell(tabular.cellIndex(r, c),
5351 case Tabular::SET_USEBOX: {
5352 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5353 if (val == tabular.getUsebox(cur.idx()))
5354 val = Tabular::BOX_NONE;
5355 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5356 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5357 tabular.setUsebox(tabular.cellIndex(r, c), val);
5361 case Tabular::UNSET_LTFIRSTHEAD:
5363 case Tabular::SET_LTFIRSTHEAD:
5364 tabular.getRowOfLTFirstHead(row, ltt);
5365 checkLongtableSpecial(ltt, value, flag);
5366 tabular.setLTHead(row, flag, ltt, true);
5369 case Tabular::UNSET_LTHEAD:
5371 case Tabular::SET_LTHEAD:
5372 tabular.getRowOfLTHead(row, ltt);
5373 checkLongtableSpecial(ltt, value, flag);
5374 tabular.setLTHead(row, flag, ltt, false);
5377 case Tabular::UNSET_LTFOOT:
5379 case Tabular::SET_LTFOOT:
5380 tabular.getRowOfLTFoot(row, ltt);
5381 checkLongtableSpecial(ltt, value, flag);
5382 tabular.setLTFoot(row, flag, ltt, false);
5385 case Tabular::UNSET_LTLASTFOOT:
5387 case Tabular::SET_LTLASTFOOT:
5388 tabular.getRowOfLTLastFoot(row, ltt);
5389 checkLongtableSpecial(ltt, value, flag);
5390 tabular.setLTFoot(row, flag, ltt, true);
5393 case Tabular::SET_LTNEWPAGE:
5394 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5397 case Tabular::SET_LTCAPTION: {
5398 if (tabular.ltCaption(row))
5400 cur.idx() = tabular.setLTCaption(row, true);
5403 cur.setSelection(false);
5404 // If a row is set as caption, then also insert
5405 // a caption. Otherwise the LaTeX output is broken.
5406 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5407 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5411 case Tabular::UNSET_LTCAPTION: {
5412 if (!tabular.ltCaption(row))
5414 cur.idx() = tabular.setLTCaption(row, false);
5417 cur.setSelection(false);
5418 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5419 if (lyx::getStatus(fr).enabled())
5424 case Tabular::TOGGLE_LTCAPTION: {
5425 if (tabular.ltCaption(row))
5426 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5428 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5432 case Tabular::SET_BOOKTABS:
5433 tabular.use_booktabs = true;
5436 case Tabular::UNSET_BOOKTABS:
5437 tabular.use_booktabs = false;
5440 case Tabular::SET_TOP_SPACE: {
5442 if (value == "default")
5443 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5444 tabular.row_info[r].top_space_default = true;
5445 else if (value == "none")
5446 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5447 tabular.row_info[r].top_space_default = false;
5448 tabular.row_info[r].top_space = len;
5450 else if (isValidLength(value, &len))
5451 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5452 tabular.row_info[r].top_space_default = false;
5453 tabular.row_info[r].top_space = len;
5458 case Tabular::SET_BOTTOM_SPACE: {
5460 if (value == "default")
5461 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5462 tabular.row_info[r].bottom_space_default = true;
5463 else if (value == "none")
5464 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5465 tabular.row_info[r].bottom_space_default = false;
5466 tabular.row_info[r].bottom_space = len;
5468 else if (isValidLength(value, &len))
5469 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5470 tabular.row_info[r].bottom_space_default = false;
5471 tabular.row_info[r].bottom_space = len;
5476 case Tabular::SET_INTERLINE_SPACE: {
5478 if (value == "default")
5479 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5480 tabular.row_info[r].interline_space_default = true;
5481 else if (value == "none")
5482 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5483 tabular.row_info[r].interline_space_default = false;
5484 tabular.row_info[r].interline_space = len;
5486 else if (isValidLength(value, &len))
5487 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5488 tabular.row_info[r].interline_space_default = false;
5489 tabular.row_info[r].interline_space = len;
5494 case Tabular::SET_DECIMAL_POINT:
5495 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5496 tabular.column_info[c].decimal_point = from_utf8(value);
5499 // dummy stuff just to avoid warnings
5500 case Tabular::LAST_ACTION:
5506 bool InsetTabular::copySelection(Cursor & cur)
5508 if (!cur.selection())
5513 getSelection(cur, rs, re, cs, ce);
5515 paste_tabular.reset(new Tabular(tabular));
5517 for (row_type r = 0; r < rs; ++r)
5518 paste_tabular->deleteRow(0);
5520 row_type const rows = re - rs + 1;
5521 while (paste_tabular->nrows() > rows)
5522 paste_tabular->deleteRow(rows);
5524 for (col_type c = 0; c < cs; ++c)
5525 paste_tabular->deleteColumn(0);
5527 col_type const columns = ce - cs + 1;
5528 while (paste_tabular->ncols() > columns)
5529 paste_tabular->deleteColumn(columns);
5531 paste_tabular->setBuffer(tabular.buffer());
5533 odocstringstream os;
5534 OutputParams const runparams(0);
5535 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5536 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5537 cap::copySelection(cur, os.str());
5539 // mark tabular stack dirty
5540 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5541 // when we (hopefully) have a one-for-all paste mechanism.
5542 // This must be called after cap::copySelection.
5543 dirtyTabularStack(true);
5549 bool InsetTabular::pasteClipboard(Cursor & cur)
5553 col_type actcol = tabular.cellColumn(cur.idx());
5554 row_type actrow = tabular.cellRow(cur.idx());
5556 if (cur.selIsMultiCell()) {
5559 getSelection(cur, actrow, re, actcol, ce);
5562 for (row_type r1 = 0, r2 = actrow;
5563 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5565 for (col_type c1 = 0, c2 = actcol;
5566 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5568 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5569 tabular.isPartOfMultiColumn(r2, c2))
5571 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5575 if (tabular.isPartOfMultiColumn(r2, c2)) {
5579 shared_ptr<InsetTableCell> inset(
5580 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5581 tabular.setCellInset(r2, c2, inset);
5582 // FIXME?: why do we need to do this explicitly? (EL)
5583 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5585 // FIXME: change tracking (MG)
5586 inset->setChange(Change(buffer().params().trackChanges ?
5587 Change::INSERTED : Change::UNCHANGED));
5595 void InsetTabular::cutSelection(Cursor & cur)
5597 if (!cur.selection())
5602 getSelection(cur, rs, re, cs, ce);
5603 for (row_type r = rs; r <= re; ++r) {
5604 for (col_type c = cs; c <= ce; ++c) {
5605 shared_ptr<InsetTableCell> t
5606 = cell(tabular.cellIndex(r, c));
5607 if (buffer().params().trackChanges)
5608 // FIXME: Change tracking (MG)
5609 t->setChange(Change(Change::DELETED));
5615 // cursor position might be invalid now
5616 if (cur.pit() > cur.lastpit())
5617 cur.pit() = cur.lastpit();
5618 if (cur.pos() > cur.lastpos())
5619 cur.pos() = cur.lastpos();
5620 cur.clearSelection();
5624 bool InsetTabular::isRightToLeft(Cursor & cur) const
5626 LASSERT(cur.depth() > 1, /**/);
5627 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5628 pos_type const parentpos = cur[cur.depth() - 2].pos();
5629 return parentpar.getFontSettings(buffer().params(),
5630 parentpos).language()->rightToLeft();
5634 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5637 LASSERT(stidx <= enidx, return docstring());
5639 col_type const col1 = tabular.cellColumn(stidx);
5640 col_type const col2 = tabular.cellColumn(enidx);
5641 row_type const row1 = tabular.cellRow(stidx);
5642 row_type const row2 = tabular.cellRow(enidx);
5644 for (col_type col = col1; col <= col2; col++)
5645 for (row_type row = row1; row <= row2; row++) {
5650 retval += tabular.cellInset(row, col)->asString(intoInsets);
5656 void InsetTabular::getSelection(Cursor & cur,
5657 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5659 CursorSlice const & beg = cur.selBegin();
5660 CursorSlice const & end = cur.selEnd();
5661 cs = tabular.cellColumn(beg.idx());
5662 ce = tabular.cellColumn(end.idx());
5666 rs = tabular.cellRow(beg.idx());
5667 re = tabular.cellRow(end.idx());
5673 Text * InsetTabular::getText(int idx) const
5675 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5679 void InsetTabular::setChange(Change const & change)
5681 for (idx_type idx = 0; idx < nargs(); ++idx)
5682 cell(idx)->setChange(change);
5686 void InsetTabular::acceptChanges()
5688 for (idx_type idx = 0; idx < nargs(); ++idx)
5689 cell(idx)->acceptChanges();
5693 void InsetTabular::rejectChanges()
5695 for (idx_type idx = 0; idx < nargs(); ++idx)
5696 cell(idx)->rejectChanges();
5700 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5702 return tabular.getPWidth(cell).zero();
5706 bool InsetTabular::forcePlainLayout(idx_type cell) const
5708 return !tabular.getPWidth(cell).zero();
5712 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5715 if (buf.length() <= 0)
5720 col_type maxCols = 1;
5721 size_t const len = buf.length();
5725 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5733 maxCols = max(cols, maxCols);
5739 maxCols = max(cols, maxCols);
5745 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5746 loctab = paste_tabular.get();
5748 dirtyTabularStack(true);
5751 cell = bv.cursor().idx();
5752 ocol = tabular.cellColumn(cell);
5753 row = tabular.cellRow(cell);
5757 idx_type const cells = loctab->numberofcells;
5760 rows = loctab->nrows();
5761 col_type const columns = loctab->ncols();
5763 while (cell < cells && p < len && row < rows &&
5764 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5770 // we can only set this if we are not too far right
5771 if (cols < columns) {
5772 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5773 Font const font = bv.textMetrics(&inset->text()).
5775 inset->setText(buf.substr(op, p - op), font,
5776 buffer().params().trackChanges);
5782 // we can only set this if we are not too far right
5783 if (cols < columns) {
5784 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5785 Font const font = bv.textMetrics(&inset->text()).
5787 inset->setText(buf.substr(op, p - op), font,
5788 buffer().params().trackChanges);
5793 cell = loctab->cellIndex(row, cols);
5799 // check for the last cell if there is no trailing '\n'
5800 if (cell < cells && op < len) {
5801 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5802 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5803 inset->setText(buf.substr(op, len - op), font,
5804 buffer().params().trackChanges);
5810 void InsetTabular::addPreview(DocIterator const & inset_pos,
5811 PreviewLoader & loader) const
5813 DocIterator cell_pos = inset_pos;
5815 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5816 for (row_type r = 0; r < tabular.nrows(); ++r) {
5817 for (col_type c = 0; c < tabular.ncols(); ++c) {
5818 cell_pos.top().idx() = tabular.cellIndex(r, c);
5819 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5825 bool InsetTabular::completionSupported(Cursor const & cur) const
5827 Cursor const & bvCur = cur.bv().cursor();
5828 if (&bvCur.inset() != this)
5830 return cur.text()->completionSupported(cur);
5834 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5836 return completionSupported(cur);
5840 bool InsetTabular::automaticInlineCompletion() const
5842 return lyxrc.completion_inline_text;
5846 bool InsetTabular::automaticPopupCompletion() const
5848 return lyxrc.completion_popup_text;
5852 bool InsetTabular::showCompletionCursor() const
5854 return lyxrc.completion_cursor_text;
5858 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5860 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5864 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5866 if (!completionSupported(cur))
5868 return cur.text()->completionPrefix(cur);
5872 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5874 if (!completionSupported(cur))
5877 return cur.text()->insertCompletion(cur, s, finished);
5881 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5882 Dimension & dim) const
5884 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5885 tm.completionPosAndDim(cur, x, y, dim);
5889 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5891 istringstream data(in);
5893 lex.setStream(data);
5900 if (!lex || token != "tabular") {
5901 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5905 // This is part of the inset proper that is usually swallowed
5906 // by Buffer::readInset
5908 if (!lex || token != "Tabular") {
5909 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5917 string InsetTabular::params2string(InsetTabular const & inset)
5920 data << "tabular" << ' ';
5922 data << "\\end_inset\n";