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;
73 using boost::shared_ptr;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
108 TabularFeature tabularFeature[] =
110 { Tabular::APPEND_ROW, "append-row", false },
111 { Tabular::APPEND_COLUMN, "append-column", false },
112 { Tabular::DELETE_ROW, "delete-row", false },
113 { Tabular::DELETE_COLUMN, "delete-column", false },
114 { Tabular::COPY_ROW, "copy-row", false },
115 { Tabular::COPY_COLUMN, "copy-column", false },
116 { Tabular::SET_LINE_TOP, "set-line-top", true },
117 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
118 { Tabular::SET_LINE_LEFT, "set-line-left", true },
119 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
120 //FIXME: get rid of those 4 TOGGLE actions in favor of the 4 above.
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::VALIGN_TOP, "valign-top", false },
130 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
131 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
132 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
133 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
134 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
135 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
136 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
137 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
138 { Tabular::MULTICOLUMN, "multicolumn", false },
139 { Tabular::MULTIROW, "multirow", false },
140 { Tabular::SET_ALL_LINES, "set-all-lines", false },
141 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
142 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
143 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
144 { Tabular::SET_PWIDTH, "set-pwidth", true },
145 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
146 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
147 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
148 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
149 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
150 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
151 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
152 { Tabular::SET_USEBOX, "set-usebox", true },
153 { Tabular::SET_LTHEAD, "set-lthead", true },
154 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
155 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
156 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
157 { Tabular::SET_LTFOOT, "set-ltfoot", true },
158 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
159 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
160 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
161 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
162 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
163 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
164 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
165 { Tabular::SET_SPECIAL_MULTIROW, "set-special-multirow", false },
166 { Tabular::SET_BOOKTABS, "set-booktabs", false },
167 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
168 { Tabular::SET_TOP_SPACE, "set-top-space", true },
169 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
170 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
171 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
172 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top"},
173 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle"},
174 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom"},
175 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
176 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
177 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
178 { Tabular::LAST_ACTION, "", false }
183 string const write_attribute(string const & name, T const & t)
185 string const s = tostr(t);
186 return s.empty() ? s : " " + name + "=\"" + s + "\"";
190 string const write_attribute(string const & name, string const & t)
192 return t.empty() ? t : " " + name + "=\"" + t + "\"";
197 string const write_attribute(string const & name, docstring const & t)
199 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
204 string const write_attribute(string const & name, bool const & b)
206 // we write only true attribute values so we remove a bit of the
207 // file format bloat for tabulars.
208 return b ? write_attribute(name, convert<string>(b)) : string();
213 string const write_attribute(string const & name, int const & i)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return i ? write_attribute(name, convert<string>(i)) : string();
222 string const write_attribute(string const & name, Tabular::idx_type const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Length const & value)
233 // we write only the value if we really have one same reson as above.
234 return value.zero() ? string() : write_attribute(name, value.asString());
238 string const tostr(LyXAlignment const & num)
243 case LYX_ALIGN_BLOCK:
247 case LYX_ALIGN_CENTER:
249 case LYX_ALIGN_RIGHT:
251 case LYX_ALIGN_LAYOUT:
253 case LYX_ALIGN_SPECIAL:
260 string const tostr(Tabular::HAlignment const & num)
263 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
265 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
267 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
274 string const tostr(Tabular::VAlignment const & num)
277 case Tabular::LYX_VALIGN_TOP:
279 case Tabular::LYX_VALIGN_MIDDLE:
281 case Tabular::LYX_VALIGN_BOTTOM:
288 string const tostr(Tabular::BoxType const & num)
291 case Tabular::BOX_NONE:
293 case Tabular::BOX_PARBOX:
295 case Tabular::BOX_MINIPAGE:
302 // I would have liked a fromstr template a lot better. (Lgb)
303 bool string2type(string const str, LyXAlignment & num)
306 num = LYX_ALIGN_NONE;
307 else if (str == "block")
308 num = LYX_ALIGN_BLOCK;
309 else if (str == "left")
310 num = LYX_ALIGN_LEFT;
311 else if (str == "center")
312 num = LYX_ALIGN_CENTER;
313 else if (str == "right")
314 num = LYX_ALIGN_RIGHT;
321 bool string2type(string const str, Tabular::HAlignment & num)
324 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
325 else if (str == "center" )
326 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
327 else if (str == "right")
328 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
335 bool string2type(string const str, Tabular::VAlignment & num)
338 num = Tabular::LYX_VALIGN_TOP;
339 else if (str == "middle" )
340 num = Tabular::LYX_VALIGN_MIDDLE;
341 else if (str == "bottom")
342 num = Tabular::LYX_VALIGN_BOTTOM;
349 bool string2type(string const str, Tabular::BoxType & num)
352 num = Tabular::BOX_NONE;
353 else if (str == "parbox")
354 num = Tabular::BOX_PARBOX;
355 else if (str == "minipage")
356 num = Tabular::BOX_MINIPAGE;
363 bool string2type(string const str, bool & num)
367 else if (str == "false")
375 bool getTokenValue(string const & str, char const * token, string & ret)
378 size_t token_length = strlen(token);
379 size_t pos = str.find(token);
381 if (pos == string::npos || pos + token_length + 1 >= str.length()
382 || str[pos + token_length] != '=')
384 pos += token_length + 1;
386 if (ch != '"' && ch != '\'') { // only read till next space
390 while (pos < str.length() - 1 && str[++pos] != ch)
397 bool getTokenValue(string const & str, char const * token, docstring & ret)
400 bool const success = getTokenValue(str, token, tmp);
401 ret = from_utf8(tmp);
406 bool getTokenValue(string const & str, char const * token, int & num)
410 if (!getTokenValue(str, token, tmp))
412 num = convert<int>(tmp);
417 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
420 return getTokenValue(str, token, tmp) && string2type(tmp, num);
424 bool getTokenValue(string const & str, char const * token,
425 Tabular::HAlignment & num)
428 return getTokenValue(str, token, tmp) && string2type(tmp, num);
432 bool getTokenValue(string const & str, char const * token,
433 Tabular::VAlignment & num)
436 return getTokenValue(str, token, tmp) && string2type(tmp, num);
440 bool getTokenValue(string const & str, char const * token,
441 Tabular::BoxType & num)
444 return getTokenValue(str, token, tmp) && string2type(tmp, num);
448 bool getTokenValue(string const & str, char const * token, bool & flag)
450 // set the flag always to false as this should be the default for bools
451 // not in the file-format.
454 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
458 bool getTokenValue(string const & str, char const * token, Length & len)
460 // set the length to be zero() as default as this it should be if not
461 // in the file format.
464 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
468 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
473 if (!getTokenValue(str, token, tmp))
475 if (tmp == "default") {
479 return isValidLength(tmp, &len);
483 void l_getline(istream & is, string & str)
486 while (str.empty()) {
488 if (!str.empty() && str[str.length() - 1] == '\r')
489 str.erase(str.length() - 1);
496 string const featureAsString(Tabular::Feature action)
498 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
499 if (tabularFeature[i].action == action)
500 return tabularFeature[i].feature;
507 /////////////////////////////////////////////////////////////////////
511 /////////////////////////////////////////////////////////////////////
514 Tabular::CellData::CellData(Buffer * buf)
517 multicolumn(Tabular::CELL_NORMAL),
518 multirow(Tabular::CELL_NORMAL),
519 alignment(LYX_ALIGN_CENTER),
520 valignment(LYX_VALIGN_TOP),
527 inset(new InsetTableCell(buf))
529 inset->setBuffer(*buf);
533 Tabular::CellData::CellData(CellData const & cs)
536 multicolumn(cs.multicolumn),
537 multirow(cs.multirow),
538 alignment(cs.alignment),
539 valignment(cs.valignment),
540 top_line(cs.top_line),
541 bottom_line(cs.bottom_line),
542 left_line(cs.left_line),
543 right_line(cs.right_line),
546 align_special(cs.align_special),
548 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
552 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
558 void Tabular::CellData::swap(CellData & rhs)
560 std::swap(cellno, rhs.cellno);
561 std::swap(width, rhs.width);
562 std::swap(multicolumn, rhs.multicolumn);
563 std::swap(multirow, rhs.multirow);
564 std::swap(alignment, rhs.alignment);
565 std::swap(valignment, rhs.valignment);
566 std::swap(top_line, rhs.top_line);
567 std::swap(bottom_line, rhs.bottom_line);
568 std::swap(left_line, rhs.left_line);
569 std::swap(right_line, rhs.right_line);
570 std::swap(usebox, rhs.usebox);
571 std::swap(rotate, rhs.rotate);
572 std::swap(align_special, rhs.align_special);
573 p_width.swap(rhs.p_width);
574 inset.swap(rhs.inset);
578 Tabular::RowData::RowData()
581 top_space_default(false),
582 bottom_space_default(false),
583 interline_space_default(false),
593 Tabular::ColumnData::ColumnData()
594 : alignment(LYX_ALIGN_CENTER),
595 valignment(LYX_VALIGN_TOP),
601 Tabular::ltType::ltType()
608 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
610 init(buffer, rows_arg, columns_arg);
614 void Tabular::setBuffer(Buffer & buffer)
617 row_type const nrows = row_info.size();
618 col_type const ncols = column_info.size();
619 for (row_type i = 0; i < nrows; ++i)
620 for (col_type j = 0; j < ncols; ++j)
621 cell_info[i][j].inset->setBuffer(*buffer_);
625 // activates all lines and sets all widths to 0
626 void Tabular::init(Buffer * buf, row_type rows_arg,
627 col_type columns_arg)
630 row_info = row_vector(rows_arg);
631 column_info = column_vector(columns_arg);
632 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
633 row_info.reserve(10);
634 column_info.reserve(10);
635 cell_info.reserve(100);
637 is_long_tabular = false;
638 tabular_valignment = LYX_VALIGN_MIDDLE;
639 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
641 use_booktabs = false;
642 size_t row_count = row_info.size();
643 size_t column_count = column_info.size();
644 // set silly default lines
645 for (row_type i = 0; i < row_count; ++i)
646 for (col_type j = 0; j < column_count; ++j) {
647 cell_info[i][j].inset->setBuffer(*buffer_);
648 cell_info[i][j].top_line = true;
649 cell_info[i][j].left_line = true;
650 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
651 cell_info[i][j].right_line = j == column_count - 1;
656 void Tabular::appendRow(idx_type const cell)
658 row_type const row = cellRow(cell);
660 row_info.insert(row_info.begin() + row + 1, RowData());
661 row_info[row + 1] = row_info[row];
663 col_type const ncols = column_info.size();
664 cell_info.insert(cell_info.begin() + row + 1,
665 cell_vector(ncols, CellData(buffer_)));
666 for (col_type c = 0; c < ncols; ++c) {
667 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
668 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
670 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
674 for (col_type c = 0; c < ncols; ++c) {
675 if (isPartOfMultiRow(row, c))
677 // inherit line settings
678 idx_type const i = cellIndex(row + 1, c);
679 idx_type const j = cellIndex(row, c);
680 setLeftLine(i, leftLine(j));
681 setRightLine(i, rightLine(j));
682 setTopLine(i, topLine(j));
683 if (topLine(j) && bottomLine(j)) {
684 setBottomLine(i, true);
685 setBottomLine(j, false);
687 // mark track changes
688 if (buffer().params().trackChanges)
689 cellInfo(i).inset->setChange(Change(Change::INSERTED));
694 void Tabular::deleteRow(row_type const row)
696 // Not allowed to delete last row
697 if (row_info.size() == 1)
700 size_t const column_count = column_info.size();
701 for (col_type i = 0; i < column_count; ++i) {
702 // Care about multirow cells
703 if (row + 1 < row_info.size() &&
704 cell_info[row][i].multirow == CELL_BEGIN_OF_MULTIROW &&
705 cell_info[row][i + 1].multirow == CELL_PART_OF_MULTIROW) {
706 cell_info[row][i + 1].multirow = CELL_BEGIN_OF_MULTIROW;
709 row_info.erase(row_info.begin() + row);
710 cell_info.erase(cell_info.begin() + row);
715 void Tabular::copyRow(row_type const row)
717 row_info.insert(row_info.begin() + row, row_info[row]);
718 cell_info.insert(cell_info.begin() + row, cell_info[row]);
720 if (buffer().params().trackChanges)
721 for (col_type j = 0; j < column_info.size(); ++j)
722 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
728 void Tabular::appendColumn(idx_type const cell)
730 col_type const c = cellColumn(cell);
732 column_info.insert(column_info.begin() + c + 1, ColumnData());
733 column_info[c + 1] = column_info[c];
735 row_type const nrows = row_info.size();
736 for (row_type r = 0; r < nrows; ++r) {
737 cell_info[r].insert(cell_info[r].begin() + c + 1,
739 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
740 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
742 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
745 for (row_type r = 0; r < nrows; ++r) {
746 // inherit line settings
747 idx_type const i = cellIndex(r, c + 1);
748 idx_type const j = cellIndex(r, c);
749 setBottomLine(i, bottomLine(j));
750 setTopLine(i, topLine(j));
751 setLeftLine(i, leftLine(j));
752 if (rightLine(j) && rightLine(j)) {
753 setRightLine(i, true);
754 setRightLine(j, false);
756 if (buffer().params().trackChanges)
757 cellInfo(i).inset->setChange(Change(Change::INSERTED));
762 void Tabular::deleteColumn(col_type const col)
764 // Not allowed to delete last column
765 if (column_info.size() == 1)
768 row_type const nrows = row_info.size();
769 for (row_type r = 0; r < nrows; ++r) {
770 // Care about multicolumn cells
771 if (col + 1 < column_info.size() &&
772 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
773 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
774 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
776 cell_info[r].erase(cell_info[r].begin() + col);
778 column_info.erase(column_info.begin() + col);
783 void Tabular::copyColumn(col_type const col)
785 BufferParams const & bp = buffer().params();
786 column_info.insert(column_info.begin() + col, column_info[col]);
788 row_type nrows = row_info.size();
789 for (row_type r = 0; r < nrows; ++r) {
790 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
792 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
798 void Tabular::updateIndexes()
801 col_type ncols = column_info.size();
802 row_type nrows = row_info.size();
804 for (row_type row = 0; row < nrows; ++row)
805 for (col_type column = 0; column < ncols; ++column) {
806 if (!isPartOfMultiColumn(row, column)
807 && !isPartOfMultiRow(row, column))
809 if (isPartOfMultiRow(row, column))
810 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
812 cell_info[row][column].cellno = numberofcells - 1;
815 rowofcell.resize(numberofcells);
816 columnofcell.resize(numberofcells);
818 for (row_type row = 0; row < nrows; ++row)
819 for (col_type column = 0; column < ncols; ++column) {
820 if (isPartOfMultiColumn(row, column)
821 || isPartOfMultiRow(row, column))
824 columnofcell[i] = column;
825 setFixedWidth(row, column);
826 updateContentAlignment(row, column);
832 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
835 for (col_type i = 0; i < column_info.size(); ++i)
836 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
842 bool Tabular::topLine(idx_type const cell) const
844 return cellInfo(cell).top_line;
848 bool Tabular::bottomLine(idx_type const cell) const
850 return cellInfo(cell).bottom_line;
854 bool Tabular::leftLine(idx_type cell) const
858 return cellInfo(cell).left_line;
862 bool Tabular::rightLine(idx_type cell) const
866 return cellInfo(cell).right_line;
870 bool Tabular::topAlreadyDrawn(idx_type cell) const
872 row_type const row = cellRow(cell);
876 return !rowTopLine(row) && bottomLine(cellAbove(cell));
880 bool Tabular::leftAlreadyDrawn(idx_type cell) const
882 col_type col = cellColumn(cell);
885 idx_type i = cellIndex(cellRow(cell), col - 1);
886 return rightLine(i) && !leftLine(cell);
890 bool Tabular::isLastRow(idx_type cell) const
892 return cellRow(cell) == row_info.size() - 1;
896 int Tabular::interRowSpace(row_type row) const
898 if (!row || row >= row_info.size())
901 int const interline_space = row_info[row - 1].interline_space_default ?
903 row_info[row - 1].interline_space.inPixels(width());
904 if (rowTopLine(row) && rowBottomLine(row - 1))
905 return interline_space + WIDTH_OF_LINE;
906 return interline_space;
910 int Tabular::interColumnSpace(idx_type cell) const
912 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
913 if (rightLine(cell) && nextcol < column_info.size()
914 && leftLine(cellIndex(cellRow(cell), nextcol)))
915 return WIDTH_OF_LINE;
920 int Tabular::columnWidth(idx_type cell) const
923 col_type const span = columnSpan(cell);
924 col_type const col = cellColumn(cell);
925 for(col_type c = col; c < col + span ; ++c)
926 w += column_info[c].width;
931 int Tabular::rowHeight(idx_type cell) const
933 row_type const span = rowSpan(cell);
934 row_type const row = cellRow(cell);
935 int h = rowAscent(row) + rowDescent(row);
937 for(row_type r = row; r < row + span ; ++r) {
940 h += interRowSpace(r);
942 if (r < row + span - 1)
949 bool Tabular::updateColumnWidths()
951 col_type const ncols = column_info.size();
952 row_type const nrows = row_info.size();
954 // for each col get max of single col cells
955 for(col_type c = 0; c < ncols; ++c) {
957 for(row_type r = 0; r < nrows; ++r) {
958 idx_type const i = cellIndex(r, c);
959 if (columnSpan(i) == 1)
960 new_width = max(new_width, cellInfo(i).width);
963 if (column_info[c].width != new_width) {
964 column_info[c].width = new_width;
968 // update col widths to fit merged cells
969 for(col_type c = 0; c < ncols; ++c)
970 for(row_type r = 0; r < nrows; ++r) {
971 idx_type const i = cellIndex(r, c);
972 int const span = columnSpan(i);
973 if (span == 1 || c > cellColumn(i))
977 for(col_type j = c; j < c + span ; ++j)
978 old_width += column_info[j].width;
980 if (cellInfo(i).width > old_width) {
981 column_info[c + span - 1].width += cellInfo(i).width - old_width;
990 int Tabular::width() const
992 col_type const ncols = column_info.size();
994 for (col_type i = 0; i < ncols; ++i)
995 width += column_info[i].width;
1000 void Tabular::setCellWidth(idx_type cell, int new_width)
1002 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
1003 + interColumnSpace(cell);
1007 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1010 if (!isMultiColumn(cell) || onlycolumn)
1011 column_info[cellColumn(cell)].alignment = align;
1013 cellInfo(cell).alignment = align;
1014 cellInset(cell).get()->setContentAlignment(align);
1018 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1021 if (!isMultiColumn(cell) || onlycolumn)
1022 column_info[cellColumn(cell)].valignment = align;
1024 cellInfo(cell).valignment = align;
1031 * Allow line and paragraph breaks for fixed width cells or disallow them,
1032 * merge cell paragraphs and reset layout to standard for variable width
1035 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1037 inset->setAutoBreakRows(fixedWidth);
1038 inset->toggleFixedWidth(fixedWidth);
1042 // merge all paragraphs to one
1043 BufferParams const & bp = cur.bv().buffer().params();
1044 while (inset->paragraphs().size() > 1)
1045 mergeParagraph(bp, inset->paragraphs(), 0);
1049 // undo information has already been recorded
1050 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1051 bp.documentClass().plainLayoutName());
1058 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1059 Length const & width)
1061 col_type const j = cellColumn(cell);
1063 column_info[j].p_width = width;
1064 for (row_type i = 0; i < row_info.size(); ++i) {
1065 idx_type const cell = cellIndex(i, j);
1066 // because of multicolumns
1067 toggleFixedWidth(cur, cellInset(cell).get(),
1068 !getPWidth(cell).zero());
1070 // cur paragraph can become invalid after paragraphs were merged
1071 if (cur.pit() > cur.lastpit())
1072 cur.pit() = cur.lastpit();
1073 // cur position can become invalid after newlines were removed
1074 if (cur.pos() > cur.lastpos())
1075 cur.pos() = cur.lastpos();
1079 bool Tabular::setFixedWidth(row_type r, col_type c)
1081 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1082 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1083 || (multicol && !cell_info[r][c].p_width.zero());
1084 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1089 void Tabular::updateContentAlignment(row_type r, col_type c)
1091 cell_info[r][c].inset->setContentAlignment(
1092 getAlignment(cellIndex(r, c)));
1096 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1097 Length const & width)
1099 if (!isMultiColumn(cell))
1102 cellInfo(cell).p_width = width;
1103 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1104 // cur paragraph can become invalid after paragraphs were merged
1105 if (cur.pit() > cur.lastpit())
1106 cur.pit() = cur.lastpit();
1107 // cur position can become invalid after newlines were removed
1108 if (cur.pos() > cur.lastpos())
1109 cur.pos() = cur.lastpos();
1114 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1115 Tabular::Feature what)
1117 if (what == SET_SPECIAL_MULTICOLUMN)
1118 cellInfo(cell).align_special = special;
1120 column_info[cellColumn(cell)].align_special = special;
1124 void Tabular::setAllLines(idx_type cell, bool line)
1126 setTopLine(cell, line);
1127 setBottomLine(cell, line);
1128 setRightLine(cell, line);
1129 setLeftLine(cell, line);
1133 void Tabular::setTopLine(idx_type i, bool line)
1135 cellInfo(i).top_line = line;
1139 void Tabular::setBottomLine(idx_type i, bool line)
1141 cellInfo(i).bottom_line = line;
1145 void Tabular::setLeftLine(idx_type cell, bool line)
1147 cellInfo(cell).left_line = line;
1151 void Tabular::setRightLine(idx_type cell, bool line)
1153 cellInfo(cell).right_line = line;
1156 bool Tabular::rowTopLine(row_type r) const
1158 col_type const ncols = column_info.size();
1159 bool all_rows_set = true;
1160 for (col_type c = 0; all_rows_set && c < ncols; ++c) {
1161 idx_type const i = cellIndex(r, c);
1162 all_rows_set = cellInfo(i).top_line;
1164 return all_rows_set;
1168 bool Tabular::rowBottomLine(row_type r) const
1170 col_type const ncols = column_info.size();
1171 bool all_rows_set = true;
1172 for (col_type c = 0; all_rows_set && c < ncols; ++c) {
1173 idx_type const i = cellIndex(r, c);
1174 all_rows_set = cellInfo(i).bottom_line;
1176 return all_rows_set;
1180 bool Tabular::columnLeftLine(col_type c) const
1187 row_type const nrows = row_info.size();
1188 for (row_type r = 0; r < nrows; ++r) {
1189 idx_type i = cellIndex(r, c);
1190 if (c == cellColumn(i)) {
1192 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1193 if (cellInfo(i).left_line || right)
1197 return 2 * nrows_left >= total;
1201 bool Tabular::columnRightLine(col_type c) const
1206 int nrows_right = 0;
1208 row_type const nrows = row_info.size();
1209 for (row_type r = 0; r < nrows; ++r) {
1210 idx_type i = cellIndex(r, c);
1211 if (c == cellColumn(i) + columnSpan(i) - 1) {
1213 bool left = (c + 1 < column_info.size()
1214 && cellInfo(cellIndex(r, c + 1)).left_line)
1215 || c + 1 == column_info.size();
1216 if (cellInfo(i).right_line && left)
1220 return 2 * nrows_right >= total;
1224 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1226 if (!onlycolumn && isMultiColumn(cell))
1227 return cellInfo(cell).alignment;
1228 return column_info[cellColumn(cell)].alignment;
1233 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1235 if (!onlycolumn && isMultiColumn(cell))
1236 return cellInfo(cell).valignment;
1237 return column_info[cellColumn(cell)].valignment;
1241 Length const Tabular::getPWidth(idx_type cell) const
1243 if (isMultiColumn(cell))
1244 return cellInfo(cell).p_width;
1245 return column_info[cellColumn(cell)].p_width;
1249 int Tabular::cellWidth(idx_type cell) const
1251 return cellInfo(cell).width;
1255 int Tabular::textHOffset(idx_type cell) const
1257 // the LaTeX Way :-(
1258 int x = WIDTH_OF_LINE;
1260 switch (getAlignment(cell)) {
1261 case LYX_ALIGN_CENTER:
1262 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1264 case LYX_ALIGN_RIGHT:
1265 x += columnWidth(cell) - cellWidth(cell);
1266 // + interColumnSpace(cell);
1269 // LYX_ALIGN_LEFT: nothing :-)
1277 int Tabular::textVOffset(idx_type cell) const
1279 int h = rowHeight(cell);
1282 switch (getVAlignment(cell)) {
1283 case LYX_VALIGN_TOP:
1285 case LYX_VALIGN_MIDDLE:
1288 case LYX_VALIGN_BOTTOM:
1297 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1300 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1302 return cell_info[row][c].cellno;
1306 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1308 col_type c = column_info.size() - 1;
1309 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1311 return cell_info[row][c].cellno;
1315 Tabular::row_type Tabular::cellRow(idx_type cell) const
1317 if (cell >= numberofcells)
1318 return row_info.size() - 1;
1321 return rowofcell[cell];
1325 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1327 if (cell >= numberofcells)
1328 return column_info.size() - 1;
1331 return columnofcell[cell];
1335 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1337 row_type const row = cellRow(cell);
1338 col_type column = cellColumn(cell);
1339 while (column < column_info.size() - 1 &&
1340 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1346 void Tabular::write(ostream & os) const
1350 << write_attribute("version", 3)
1351 << write_attribute("rows", row_info.size())
1352 << write_attribute("columns", column_info.size())
1354 // global longtable options
1356 << write_attribute("rotate", rotate)
1357 << write_attribute("booktabs", use_booktabs)
1358 << write_attribute("islongtable", is_long_tabular)
1359 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1360 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1361 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1362 << write_attribute("headTopDL", endhead.topDL)
1363 << write_attribute("headBottomDL", endhead.bottomDL)
1364 << write_attribute("footTopDL", endfoot.topDL)
1365 << write_attribute("footBottomDL", endfoot.bottomDL)
1366 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1367 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1368 << write_attribute("lastFootEmpty", endlastfoot.empty);
1369 // longtables cannot be aligned vertically
1370 if (!is_long_tabular)
1371 os << write_attribute("tabularvalignment", tabular_valignment);
1372 if (is_long_tabular)
1373 os << write_attribute("longtabularalignment",
1374 longtabular_alignment);
1376 for (col_type j = 0; j < column_info.size(); ++j) {
1378 << write_attribute("alignment", column_info[j].alignment)
1379 << write_attribute("valignment", column_info[j].valignment)
1380 << write_attribute("width", column_info[j].p_width.asString())
1381 << write_attribute("special", column_info[j].align_special)
1384 for (row_type i = 0; i < row_info.size(); ++i) {
1385 static const string def("default");
1387 if (row_info[i].top_space_default)
1388 os << write_attribute("topspace", def);
1390 os << write_attribute("topspace", row_info[i].top_space);
1391 if (row_info[i].bottom_space_default)
1392 os << write_attribute("bottomspace", def);
1394 os << write_attribute("bottomspace", row_info[i].bottom_space);
1395 if (row_info[i].interline_space_default)
1396 os << write_attribute("interlinespace", def);
1398 os << write_attribute("interlinespace", row_info[i].interline_space);
1399 os << write_attribute("endhead", row_info[i].endhead)
1400 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1401 << write_attribute("endfoot", row_info[i].endfoot)
1402 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1403 << write_attribute("newpage", row_info[i].newpage)
1404 << write_attribute("caption", row_info[i].caption)
1406 for (col_type j = 0; j < column_info.size(); ++j) {
1408 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1409 << write_attribute("multirow", cell_info[i][j].multirow)
1410 << write_attribute("alignment", cell_info[i][j].alignment)
1411 << write_attribute("valignment", cell_info[i][j].valignment)
1412 << write_attribute("topline", cell_info[i][j].top_line)
1413 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1414 << write_attribute("leftline", cell_info[i][j].left_line)
1415 << write_attribute("rightline", cell_info[i][j].right_line)
1416 << write_attribute("rotate", cell_info[i][j].rotate)
1417 << write_attribute("usebox", cell_info[i][j].usebox)
1418 << write_attribute("width", cell_info[i][j].p_width)
1419 << write_attribute("special", cell_info[i][j].align_special)
1421 os << "\\begin_inset ";
1422 cell_info[i][j].inset->write(os);
1423 os << "\n\\end_inset\n"
1428 os << "</lyxtabular>\n";
1432 void Tabular::read(Lexer & lex)
1435 istream & is = lex.getStream();
1437 l_getline(is, line);
1438 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1439 LASSERT(false, /**/);
1444 if (!getTokenValue(line, "version", version))
1446 LASSERT(version >= 2, /**/);
1449 if (!getTokenValue(line, "rows", rows_arg))
1452 if (!getTokenValue(line, "columns", columns_arg))
1454 init(buffer_, rows_arg, columns_arg);
1455 l_getline(is, line);
1456 if (!prefixIs(line, "<features")) {
1457 lyxerr << "Wrong tabular format (expected <features ...> got"
1458 << line << ')' << endl;
1461 getTokenValue(line, "rotate", rotate);
1462 getTokenValue(line, "booktabs", use_booktabs);
1463 getTokenValue(line, "islongtable", is_long_tabular);
1464 getTokenValue(line, "tabularvalignment", tabular_valignment);
1465 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1466 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1467 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1468 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1469 getTokenValue(line, "headTopDL", endhead.topDL);
1470 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1471 getTokenValue(line, "footTopDL", endfoot.topDL);
1472 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1473 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1474 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1475 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1477 for (col_type j = 0; j < column_info.size(); ++j) {
1479 if (!prefixIs(line,"<column")) {
1480 lyxerr << "Wrong tabular format (expected <column ...> got"
1481 << line << ')' << endl;
1484 getTokenValue(line, "alignment", column_info[j].alignment);
1485 getTokenValue(line, "valignment", column_info[j].valignment);
1486 getTokenValue(line, "width", column_info[j].p_width);
1487 getTokenValue(line, "special", column_info[j].align_special);
1490 for (row_type i = 0; i < row_info.size(); ++i) {
1491 l_getline(is, line);
1492 if (!prefixIs(line, "<row")) {
1493 lyxerr << "Wrong tabular format (expected <row ...> got"
1494 << line << ')' << endl;
1497 getTokenValue(line, "topspace", row_info[i].top_space,
1498 row_info[i].top_space_default);
1499 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1500 row_info[i].bottom_space_default);
1501 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1502 row_info[i].interline_space_default);
1503 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1504 getTokenValue(line, "endhead", row_info[i].endhead);
1505 getTokenValue(line, "endfoot", row_info[i].endfoot);
1506 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1507 getTokenValue(line, "newpage", row_info[i].newpage);
1508 getTokenValue(line, "caption", row_info[i].caption);
1509 for (col_type j = 0; j < column_info.size(); ++j) {
1510 l_getline(is, line);
1511 if (!prefixIs(line, "<cell")) {
1512 lyxerr << "Wrong tabular format (expected <cell ...> got"
1513 << line << ')' << endl;
1516 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1517 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1518 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1519 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1520 getTokenValue(line, "topline", cell_info[i][j].top_line);
1521 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1522 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1523 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1524 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1525 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1526 getTokenValue(line, "width", cell_info[i][j].p_width);
1528 getTokenValue(line, "special", cell_info[i][j].align_special);
1529 l_getline(is, line);
1530 if (prefixIs(line, "\\begin_inset")) {
1531 cell_info[i][j].inset->setBuffer(*buffer_);
1532 cell_info[i][j].inset->read(lex);
1533 l_getline(is, line);
1535 if (!prefixIs(line, "</cell>")) {
1536 lyxerr << "Wrong tabular format (expected </cell> got"
1537 << line << ')' << endl;
1541 l_getline(is, line);
1542 if (!prefixIs(line, "</row>")) {
1543 lyxerr << "Wrong tabular format (expected </row> got"
1544 << line << ')' << endl;
1548 while (!prefixIs(line, "</lyxtabular>")) {
1549 l_getline(is, line);
1555 bool Tabular::isMultiColumn(idx_type cell) const
1557 if (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1558 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN)
1565 bool Tabular::isMultiColumnReal(idx_type cell) const
1567 return cellColumn(cell) != cellRightColumn(cell) &&
1568 (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1569 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1573 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1575 return cell_info[cellRow(cell)][cellColumn(cell)];
1579 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1581 CellData & cs = cellInfo(cell);
1582 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1583 cs.alignment = column_info[cellColumn(cell)].alignment;
1584 setRightLine(cell, rightLine(cell + number - 1));
1585 for (idx_type i = 1; i < number; ++i) {
1586 CellData & cs1 = cellInfo(cell + i);
1587 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1588 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1595 bool Tabular::isMultiRow(idx_type cell) const
1597 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1598 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1602 void Tabular::setMultiRow(idx_type cell, idx_type number)
1604 idx_type const column = cellColumn(cell);
1605 idx_type row = cellRow(cell);
1606 idx_type const ncolumns = column_info.size();
1607 CellData & cs = cellInfo(cell);
1608 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1609 // FIXME: the horizontal alignment can only be changed for
1610 // the whole table, support for this needs to be implemented
1611 // assigning this to uwestoehr
1612 cs.valignment = LYX_VALIGN_MIDDLE;
1613 // set the bottom row of the last selected cell
1614 setBottomLine(cell, bottomLine(cell + (number - 1)*ncolumns));
1615 for (idx_type i = 1; i < number; ++i) {
1616 CellData & cs1 = cell_info[row + i][column];
1617 cs1.multirow = CELL_PART_OF_MULTIROW;
1618 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1625 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1627 row_type const row = cellRow(cell);
1628 col_type const ncols = column_info.size();
1629 col_type column = cellColumn(cell) + 1;
1630 while (column < ncols && isPartOfMultiColumn(row, column))
1633 return column - cellColumn(cell);
1637 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1639 row_type const nrows = row_info.size();
1640 col_type const column = cellColumn(cell);
1641 col_type row = cellRow(cell) + 1;
1642 while (row < nrows && isPartOfMultiRow(row, column))
1645 return row - cellRow(cell);
1649 void Tabular::unsetMultiColumn(idx_type cell)
1651 row_type const row = cellRow(cell);
1652 col_type const column = cellColumn(cell);
1653 row_type const span = columnSpan(cell);
1654 for (col_type c = 0; c < span; ++c)
1655 cell_info[row][column + c].multicolumn = CELL_NORMAL;
1660 void Tabular::unsetMultiRow(idx_type cell)
1662 row_type const row = cellRow(cell);
1663 col_type const column = cellColumn(cell);
1664 row_type const span = rowSpan(cell);
1665 for (row_type r = 0; r < span; ++r)
1666 cell_info[row + r][column].multirow = CELL_NORMAL;
1671 void Tabular::setRotateCell(idx_type cell, bool flag)
1673 cellInfo(cell).rotate = flag;
1677 bool Tabular::getRotateCell(idx_type cell) const
1679 return cellInfo(cell).rotate;
1683 bool Tabular::needRotating() const
1687 for (row_type i = 0; i < row_info.size(); ++i)
1688 for (col_type j = 0; j < column_info.size(); ++j)
1689 if (cell_info[i][j].rotate)
1695 bool Tabular::isLastCell(idx_type cell) const
1697 if (cell + 1 < numberofcells)
1703 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1705 if (cellRow(cell) == 0)
1708 col_type const col = cellColumn(cell);
1709 row_type r = cellRow(cell) - 1;
1710 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1713 return cell_info[r][col].cellno;
1717 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1719 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1720 if (nextrow < row_info.size())
1721 return cell_info[nextrow][cellColumn(cell)].cellno;
1726 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1728 LASSERT(column != npos && column < column_info.size()
1729 && row != npos && row < row_info.size(), /**/);
1730 return cell_info[row][column].cellno;
1734 void Tabular::setUsebox(idx_type cell, BoxType type)
1736 cellInfo(cell).usebox = type;
1740 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1741 // adds line breaks, see bug 4886.
1742 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1744 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1745 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1747 if (cellInfo(cell).usebox > 1)
1748 return cellInfo(cell).usebox;
1749 return useParbox(cell);
1754 // This are functions used for the longtable support
1756 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1762 row_info[row].endfirsthead = flag;
1766 row_info[row].endhead = flag;
1771 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1774 hd.set = haveLTHead();
1775 return row_info[row].endhead;
1779 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1782 hd.set = haveLTFirstHead();
1783 return row_info[row].endfirsthead;
1787 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1793 row_info[row].endlastfoot = flag;
1797 row_info[row].endfoot = flag;
1802 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1805 fd.set = haveLTFoot();
1806 return row_info[row].endfoot;
1810 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1813 fd.set = haveLTLastFoot();
1814 return row_info[row].endlastfoot;
1818 void Tabular::setLTNewPage(row_type row, bool what)
1820 row_info[row].newpage = what;
1824 bool Tabular::getLTNewPage(row_type row) const
1826 return row_info[row].newpage;
1830 bool Tabular::haveLTHead() const
1832 for (row_type i = 0; i < row_info.size(); ++i)
1833 if (row_info[i].endhead)
1839 bool Tabular::haveLTFirstHead() const
1841 if (endfirsthead.empty)
1843 for (row_type i = 0; i < row_info.size(); ++i)
1844 if (row_info[i].endfirsthead)
1850 bool Tabular::haveLTFoot() const
1852 for (row_type i = 0; i < row_info.size(); ++i)
1853 if (row_info[i].endfoot)
1859 bool Tabular::haveLTLastFoot() const
1861 if (endlastfoot.empty)
1863 for (row_type i = 0; i < row_info.size(); ++i)
1864 if (row_info[i].endlastfoot)
1870 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1872 idx_type i = getFirstCellInRow(row);
1874 setMultiColumn(i, numberOfCellsInRow(row));
1875 setTopLine(i, false);
1876 setBottomLine(i, false);
1877 setLeftLine(i, false);
1878 setRightLine(i, false);
1880 unsetMultiColumn(i);
1881 // When unsetting a caption row, also all existing
1882 // captions in this row must be dissolved.
1884 row_info[row].caption = what;
1889 bool Tabular::ltCaption(row_type row) const
1891 return row_info[row].caption;
1895 bool Tabular::haveLTCaption() const
1897 for (row_type i = 0; i < row_info.size(); ++i)
1898 if (row_info[i].caption)
1904 // end longtable support functions
1906 void Tabular::setRowAscent(row_type row, int height)
1908 if (row >= row_info.size() || row_info[row].ascent == height)
1910 row_info[row].ascent = height;
1914 void Tabular::setRowDescent(row_type row, int height)
1916 if (row >= row_info.size() || row_info[row].descent == height)
1918 row_info[row].descent = height;
1922 int Tabular::rowAscent(row_type row) const
1924 LASSERT(row < row_info.size(), /**/);
1925 return row_info[row].ascent;
1929 int Tabular::rowDescent(row_type row) const
1931 LASSERT(row < row_info.size(), /**/);
1932 return row_info[row].descent;
1936 int Tabular::height() const
1939 for (row_type row = 0; row < row_info.size(); ++row)
1940 height += rowAscent(row) + rowDescent(row) +
1946 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1948 LASSERT(row < row_info.size(), /**/);
1949 LASSERT(column < column_info.size(), /**/);
1950 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1954 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1956 LASSERT(row < row_info.size(), /**/);
1957 LASSERT(column < column_info.size(), /**/);
1958 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1962 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1964 // we only output complete row lines and the 1st row here, the rest
1965 // is done in Tabular::TeXBottomHLine(...)
1967 // get for each column the topline (if any)
1968 col_type const ncols = column_info.size();
1969 vector<bool> topline;
1971 for (col_type c = 0; c < ncols; ++c) {
1972 topline.push_back(topLine(cellIndex(row, c)));
1973 // If cell is part of a multirow and not the first cell of the
1974 // multirow, no line must be drawn.
1976 if (isMultiRow(cellIndex(row, c))
1977 && isMultiRow(cellIndex(row - 1, c)))
1983 // do nothing if empty first row, or incomplete row line after
1984 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1987 // only output complete row lines and the 1st row's clines
1988 if (nset == ncols) {
1990 os << (row == 0 ? "\\toprule " : "\\midrule ");
1994 } else if (row == 0) {
1995 for (col_type c = 0; c < ncols; ++c) {
1997 //babel makes the "-" character an active one, so we have to suppress this here
1998 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1999 if (lang == "slovak" || lang == "czech")
2000 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2001 "\\expandafter\\cline\\expandafter{\\expandafter")
2002 << c + 1 << "\\string-";
2004 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2005 // get to last column of line span
2006 while (c < ncols && topline[c])
2017 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2019 // we output bottomlines of row r and the toplines of row r+1
2020 // if the latter do not span the whole tabular
2022 // get the bottomlines of row r, and toplines in next row
2023 bool lastrow = row == row_info.size() - 1;
2024 col_type const ncols = column_info.size();
2025 vector<bool> bottomline, topline;
2026 bool nextrowset = true;
2027 for (col_type c = 0; c < ncols; ++c) {
2028 bottomline.push_back(bottomLine(cellIndex(row, c)));
2029 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2030 // If cell is part of a multirow and not the last or first cell of the
2031 // multirow, no line must be drawn.
2033 if (isMultiRow(cellIndex(row, c))
2034 && isMultiRow(cellIndex(row + 1, c))) {
2035 bottomline[c] = false;
2038 nextrowset &= topline[c];
2041 // combine this row's bottom lines and next row's toplines if necessary
2043 for (col_type c = 0; c < ncols; ++c) {
2045 bottomline[c] = bottomline[c] || topline[c];
2050 // do nothing if empty, OR incomplete row line with a topline in next row
2051 if (nset == 0 || (nextrowset && nset != ncols))
2054 if (nset == ncols) {
2056 os << (lastrow ? "\\bottomrule" : "\\midrule");
2060 for (col_type c = 0; c < ncols; ++c) {
2061 if (bottomline[c]) {
2062 //babel makes the "-" character an active one, so we have to suppress this here
2063 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2064 if (lang == "slovak" || lang == "czech")
2065 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2066 "\\expandafter\\cline\\expandafter{\\expandafter")
2067 << c + 1 << "\\string-";
2069 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2070 // get to last column of line span
2071 while (c < ncols && bottomline[c])
2082 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2083 bool & ismulticol, bool & ismultirow) const
2086 row_type const r = cellRow(cell);
2087 if (is_long_tabular && row_info[r].caption)
2090 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2091 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2092 // figure out how to set the lines
2093 // we always set double lines to the right of the cell
2094 col_type const c = cellColumn(cell);
2095 col_type const nextcol = c + columnSpan(cell);
2096 bool colright = columnRightLine(c);
2097 bool colleft = columnLeftLine(c);
2098 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
2099 bool nextcellleft = nextcol < column_info.size()
2100 && leftLine(cellIndex(r, nextcol));
2101 bool coldouble = colright && nextcolleft;
2102 bool celldouble = rightLine(cell) && nextcellleft;
2104 ismulticol = isMultiColumn(cell)
2105 || (c == 0 && colleft != leftLine(cell))
2106 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2107 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2108 || (coldouble != celldouble);
2110 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2111 if (c ==0 && leftLine(cell))
2113 if (!cellInfo(cell).align_special.empty()) {
2114 os << cellInfo(cell).align_special;
2116 if (!getPWidth(cell).zero()) {
2118 case LYX_ALIGN_LEFT:
2119 os << ">{\\raggedright}";
2121 case LYX_ALIGN_RIGHT:
2122 os << ">{\\raggedleft}";
2124 case LYX_ALIGN_CENTER:
2125 os << ">{\\centering}";
2131 case LYX_VALIGN_TOP:
2134 case LYX_VALIGN_MIDDLE:
2137 case LYX_VALIGN_BOTTOM:
2142 << from_ascii(getPWidth(cell).asLatexString())
2146 case LYX_ALIGN_LEFT:
2149 case LYX_ALIGN_RIGHT:
2156 } // end if else !getPWidth
2157 } // end if else !cellinfo_of_cell
2158 if (rightLine(cell) || nextcellleft)
2161 // add extra vertical line if we want a double one
2164 } // end if ismulticol
2166 // we only need code for the first multirow cell
2167 ismultirow = isMultiRow(cell);
2169 os << "\\multirow{" << rowSpan(cell) << "}{";
2170 if (!getPWidth(cell).zero())
2171 os << from_ascii(getPWidth(cell).asLatexString());
2173 // we need to set a default value
2174 // needs to be discussed
2177 } // end if ismultirow
2179 if (getRotateCell(cell)) {
2180 os << "\\begin{sideways}\n";
2183 if (getUsebox(cell) == BOX_PARBOX) {
2186 case LYX_VALIGN_TOP:
2189 case LYX_VALIGN_MIDDLE:
2192 case LYX_VALIGN_BOTTOM:
2196 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2198 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2199 os << "\\begin{minipage}[";
2201 case LYX_VALIGN_TOP:
2204 case LYX_VALIGN_MIDDLE:
2207 case LYX_VALIGN_BOTTOM:
2211 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2219 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2220 bool ismulticol, bool ismultirow) const
2223 row_type const r = cellRow(cell);
2224 if (is_long_tabular && row_info[r].caption)
2228 if (getUsebox(cell) == BOX_PARBOX)
2230 else if (getUsebox(cell) == BOX_MINIPAGE) {
2231 os << "%\n\\end{minipage}";
2234 if (getRotateCell(cell)) {
2235 os << "%\n\\end{sideways}";
2238 if (ismulticol || ismultirow) {
2245 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2246 OutputParams const & runparams) const
2248 if (!is_long_tabular)
2253 // the caption must be output before the headers
2254 if (haveLTCaption()) {
2255 for (row_type i = 0; i < row_info.size(); ++i) {
2256 if (row_info[i].caption) {
2257 ret += TeXRow(os, i, runparams);
2261 // output first header info
2262 // first header must be output before the header, otherwise the
2263 // correct caption placement becomes really weird
2264 if (haveLTFirstHead()) {
2265 if (endfirsthead.topDL) {
2269 for (row_type i = 0; i < row_info.size(); ++i) {
2270 if (row_info[i].endfirsthead) {
2271 ret += TeXRow(os, i, runparams);
2274 if (endfirsthead.bottomDL) {
2278 os << "\\endfirsthead\n";
2281 // output header info
2283 if (endfirsthead.empty && !haveLTFirstHead()) {
2284 os << "\\endfirsthead\n";
2287 if (endhead.topDL) {
2291 for (row_type i = 0; i < row_info.size(); ++i) {
2292 if (row_info[i].endhead) {
2293 ret += TeXRow(os, i, runparams);
2296 if (endhead.bottomDL) {
2300 os << "\\endhead\n";
2303 // output footer info
2305 if (endfoot.topDL) {
2309 for (row_type i = 0; i < row_info.size(); ++i) {
2310 if (row_info[i].endfoot) {
2311 ret += TeXRow(os, i, runparams);
2314 if (endfoot.bottomDL) {
2318 os << "\\endfoot\n";
2320 if (endlastfoot.empty && !haveLTLastFoot()) {
2321 os << "\\endlastfoot\n";
2325 // output lastfooter info
2326 if (haveLTLastFoot()) {
2327 if (endlastfoot.topDL) {
2331 for (row_type i = 0; i < row_info.size(); ++i) {
2332 if (row_info[i].endlastfoot) {
2333 ret += TeXRow(os, i, runparams);
2336 if (endlastfoot.bottomDL) {
2340 os << "\\endlastfoot\n";
2347 bool Tabular::isValidRow(row_type row) const
2349 if (!is_long_tabular)
2351 return !row_info[row].endhead && !row_info[row].endfirsthead
2352 && !row_info[row].endfoot && !row_info[row].endlastfoot
2353 && !row_info[row].caption;
2357 int Tabular::TeXRow(odocstream & os, row_type i,
2358 OutputParams const & runparams) const
2360 idx_type cell = cellIndex(i, 0);
2361 shared_ptr<InsetTableCell> inset = cellInset(cell);
2362 Paragraph const & par = inset->paragraphs().front();
2363 string const lang = par.getParLanguage(buffer().params())->lang();
2365 //output the top line
2366 int ret = TeXTopHLine(os, i, lang);
2368 if (row_info[i].top_space_default) {
2370 os << "\\addlinespace\n";
2372 os << "\\noalign{\\vskip\\doublerulesep}\n";
2374 } else if(!row_info[i].top_space.zero()) {
2376 os << "\\addlinespace["
2377 << from_ascii(row_info[i].top_space.asLatexString())
2380 os << "\\noalign{\\vskip"
2381 << from_ascii(row_info[i].top_space.asLatexString())
2386 bool ismulticol = false;
2387 bool ismultirow = false;
2388 for (col_type j = 0; j < column_info.size(); ++j) {
2389 if (isPartOfMultiRow(i, j))
2390 os << " & "; // we need to add a further column
2391 if (isPartOfMultiColumn(i, j) || isPartOfMultiRow(i, j))
2393 cell = cellIndex(i, j);
2394 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2395 shared_ptr<InsetTableCell> inset = cellInset(cell);
2397 Paragraph const & par = inset->paragraphs().front();
2398 bool rtl = par.isRTL(buffer().params())
2400 && getPWidth(cell).zero();
2404 par.getParLanguage(buffer().params())->lang();
2405 if (lang == "farsi")
2407 else if (lang == "arabic_arabi")
2409 // currently, remaning RTL languages are
2410 // arabic_arabtex and hebrew
2414 // pass to the OutputParams that we are in a cell and
2415 // which alignment we have set.
2416 // InsetNewline needs this context information.
2417 OutputParams newrp(runparams);
2418 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2419 ? OutputParams::PLAIN
2420 : OutputParams::ALIGNED;
2421 ret += inset->latex(os, newrp);
2422 runparams.encoding = newrp.encoding;
2426 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2427 if (cell != getLastCellInRow(i)) { // not last cell in row
2431 if (row_info[i].caption && !endfirsthead.empty && !haveLTFirstHead())
2432 // if no first header and no empty first header is used,
2433 // the caption needs to be terminated by \endfirsthead
2435 os << "\\endfirsthead";
2437 os << "\\tabularnewline";
2438 if (row_info[i].bottom_space_default) {
2440 os << "\\addlinespace";
2442 os << "[\\doublerulesep]";
2443 } else if (!row_info[i].bottom_space.zero()) {
2445 os << "\\addlinespace";
2447 << from_ascii(row_info[i].bottom_space.asLatexString())
2453 //output the bottom line
2454 ret += TeXBottomHLine(os, i, lang);
2456 if (row_info[i].interline_space_default) {
2458 os << "\\addlinespace\n";
2460 os << "\\noalign{\\vskip\\doublerulesep}\n";
2462 } else if (!row_info[i].interline_space.zero()) {
2464 os << "\\addlinespace["
2465 << from_ascii(row_info[i].interline_space.asLatexString())
2468 os << "\\noalign{\\vskip"
2469 << from_ascii(row_info[i].interline_space.asLatexString())
2477 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2481 //+---------------------------------------------------------------------
2482 //+ first the opening preamble +
2483 //+---------------------------------------------------------------------
2486 os << "\\begin{sideways}\n";
2489 if (is_long_tabular) {
2490 os << "\\begin{longtable}";
2491 switch (longtabular_alignment) {
2492 case LYX_LONGTABULAR_ALIGN_LEFT:
2495 case LYX_LONGTABULAR_ALIGN_CENTER:
2497 case LYX_LONGTABULAR_ALIGN_RIGHT:
2502 os << "\\begin{tabular}";
2503 switch (tabular_valignment) {
2504 case LYX_VALIGN_TOP:
2507 case LYX_VALIGN_MIDDLE:
2509 case LYX_VALIGN_BOTTOM:
2517 for (col_type i = 0; i < column_info.size(); ++i) {
2518 if (columnLeftLine(i))
2520 if (!column_info[i].align_special.empty()) {
2521 os << column_info[i].align_special;
2523 if (!column_info[i].p_width.zero()) {
2524 switch (column_info[i].alignment) {
2525 case LYX_ALIGN_LEFT:
2526 os << ">{\\raggedright}";
2528 case LYX_ALIGN_RIGHT:
2529 os << ">{\\raggedleft}";
2531 case LYX_ALIGN_CENTER:
2532 os << ">{\\centering}";
2534 case LYX_ALIGN_NONE:
2535 case LYX_ALIGN_BLOCK:
2536 case LYX_ALIGN_LAYOUT:
2537 case LYX_ALIGN_SPECIAL:
2541 switch (column_info[i].valignment) {
2542 case LYX_VALIGN_TOP:
2545 case LYX_VALIGN_MIDDLE:
2548 case LYX_VALIGN_BOTTOM:
2553 << from_ascii(column_info[i].p_width.asLatexString())
2556 switch (column_info[i].alignment) {
2557 case LYX_ALIGN_LEFT:
2560 case LYX_ALIGN_RIGHT:
2567 } // end if else !column_info[i].p_width
2568 } // end if else !column_info[i].align_special
2569 if (columnRightLine(i))
2575 ret += TeXLongtableHeaderFooter(os, runparams);
2577 //+---------------------------------------------------------------------
2578 //+ the single row and columns (cells) +
2579 //+---------------------------------------------------------------------
2581 for (row_type i = 0; i < row_info.size(); ++i) {
2582 if (isValidRow(i)) {
2583 ret += TeXRow(os, i, runparams);
2584 if (is_long_tabular && row_info[i].newpage) {
2585 os << "\\newpage\n";
2591 //+---------------------------------------------------------------------
2592 //+ the closing of the tabular +
2593 //+---------------------------------------------------------------------
2595 if (is_long_tabular)
2596 os << "\\end{longtable}";
2598 os << "\\end{tabular}";
2600 os << "\n\\end{sideways}";
2608 int Tabular::docbookRow(odocstream & os, row_type row,
2609 OutputParams const & runparams) const
2612 idx_type cell = getFirstCellInRow(row);
2615 for (col_type j = 0; j < column_info.size(); ++j) {
2616 if (isPartOfMultiColumn(row, j))
2619 os << "<entry align=\"";
2620 switch (getAlignment(cell)) {
2621 case LYX_ALIGN_LEFT:
2624 case LYX_ALIGN_RIGHT:
2632 os << "\" valign=\"";
2633 switch (getVAlignment(cell)) {
2634 case LYX_VALIGN_TOP:
2637 case LYX_VALIGN_BOTTOM:
2640 case LYX_VALIGN_MIDDLE:
2645 if (isMultiColumn(cell)) {
2646 os << " namest=\"col" << j << "\" ";
2647 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2651 ret += cellInset(cell)->docbook(os, runparams);
2660 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2664 //+---------------------------------------------------------------------
2665 //+ first the opening preamble +
2666 //+---------------------------------------------------------------------
2668 os << "<tgroup cols=\"" << column_info.size()
2669 << "\" colsep=\"1\" rowsep=\"1\">\n";
2671 for (col_type i = 0; i < column_info.size(); ++i) {
2672 os << "<colspec colname=\"col" << i << "\" align=\"";
2673 switch (column_info[i].alignment) {
2674 case LYX_ALIGN_LEFT:
2677 case LYX_ALIGN_RIGHT:
2685 if (runparams.flavor == OutputParams::XML)
2691 //+---------------------------------------------------------------------
2692 //+ Long Tabular case +
2693 //+---------------------------------------------------------------------
2695 // output caption info
2696 if (haveLTCaption()) {
2697 os << "<caption>\n";
2699 for (row_type i = 0; i < row_info.size(); ++i) {
2700 if (row_info[i].caption) {
2701 ret += docbookRow(os, i, runparams);
2704 os << "</caption>\n";
2707 // output header info
2708 if (haveLTHead() || haveLTFirstHead()) {
2711 for (row_type i = 0; i < row_info.size(); ++i) {
2712 if (row_info[i].endhead || row_info[i].endfirsthead) {
2713 ret += docbookRow(os, i, runparams);
2719 // output footer info
2720 if (haveLTFoot() || haveLTLastFoot()) {
2723 for (row_type i = 0; i < row_info.size(); ++i) {
2724 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2725 ret += docbookRow(os, i, runparams);
2732 //+---------------------------------------------------------------------
2733 //+ the single row and columns (cells) +
2734 //+---------------------------------------------------------------------
2738 for (row_type i = 0; i < row_info.size(); ++i) {
2739 if (isValidRow(i)) {
2740 ret += docbookRow(os, i, runparams);
2745 //+---------------------------------------------------------------------
2746 //+ the closing of the tabular +
2747 //+---------------------------------------------------------------------
2756 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2757 OutputParams const & runparams) const
2760 idx_type cell = getFirstCellInRow(row);
2762 xs << html::StartTag("tr");
2763 for (col_type j = 0; j < column_info.size(); ++j) {
2764 if (isPartOfMultiColumn(row, j))
2769 switch (getAlignment(cell)) {
2770 case LYX_ALIGN_LEFT:
2773 case LYX_ALIGN_RIGHT:
2781 attr << " valign='";
2782 switch (getVAlignment(cell)) {
2783 case LYX_VALIGN_TOP:
2786 case LYX_VALIGN_BOTTOM:
2789 case LYX_VALIGN_MIDDLE:
2794 if (isMultiColumn(cell))
2795 attr << " colspan='" << columnSpan(cell) << "'";
2797 xs << html::StartTag("td", attr.str());
2798 ret += cellInset(cell)->xhtml(xs, runparams);
2799 xs << html::EndTag("td");
2802 xs << html::EndTag("tr");
2807 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2810 // It's unclear to me if we need to mess with the long table stuff.
2811 // We can borrow that too from docbook, if so.
2813 xs << html::StartTag("tbody");
2814 for (row_type i = 0; i < row_info.size(); ++i) {
2815 if (isValidRow(i)) {
2816 ret += xhtmlRow(xs, i, runparams);
2819 xs << html::EndTag("tbody");
2824 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2825 vector<unsigned int> const & clen) const
2827 idx_type const fcell = getFirstCellInRow(row);
2828 idx_type const n = numberOfCellsInRow(row) + fcell;
2831 for (idx_type i = fcell; i < n; ++i) {
2841 for (idx_type i = fcell; i < n; ++i) {
2852 col_type column = cellColumn(i);
2853 int len = clen[column];
2854 while (column < column_info.size() - 1
2855 && isPartOfMultiColumn(row, ++column))
2856 len += clen[column] + 4;
2857 os << docstring(len, ch);
2872 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2873 vector<unsigned int> const & clen) const
2875 idx_type const fcell = getFirstCellInRow(row);
2876 idx_type const n = numberOfCellsInRow(row) + fcell;
2879 for (idx_type i = fcell; i < n; ++i) {
2880 if (bottomLine(i)) {
2889 for (idx_type i = fcell; i < n; ++i) {
2890 if (bottomLine(i)) {
2900 col_type column = cellColumn(i);
2901 int len = clen[column];
2902 while (column < column_info.size() -1
2903 && isPartOfMultiColumn(row, ++column))
2904 len += clen[column] + 4;
2905 os << docstring(len, ch);
2906 if (bottomLine(i)) {
2920 void Tabular::plaintextPrintCell(odocstream & os,
2921 OutputParams const & runparams,
2922 idx_type cell, row_type row, col_type column,
2923 vector<unsigned int> const & clen,
2924 bool onlydata) const
2926 odocstringstream sstr;
2927 cellInset(cell)->plaintext(sstr, runparams);
2939 unsigned int len1 = sstr.str().length();
2940 unsigned int len2 = clen[column];
2941 while (column < column_info.size() -1
2942 && isPartOfMultiColumn(row, ++column))
2943 len2 += clen[column] + 4;
2946 switch (getAlignment(cell)) {
2948 case LYX_ALIGN_LEFT:
2951 case LYX_ALIGN_RIGHT:
2955 case LYX_ALIGN_CENTER:
2961 os << docstring(len1, ' ') << sstr.str()
2962 << docstring(len2, ' ');
2964 if (rightLine(cell))
2971 void Tabular::plaintext(odocstream & os,
2972 OutputParams const & runparams, int const depth,
2973 bool onlydata, char_type delim) const
2975 // first calculate the width of the single columns
2976 vector<unsigned int> clen(column_info.size());
2979 // first all non (real) multicolumn cells!
2980 for (col_type j = 0; j < column_info.size(); ++j) {
2982 for (row_type i = 0; i < row_info.size(); ++i) {
2983 idx_type cell = cellIndex(i, j);
2984 if (isMultiColumnReal(cell))
2986 odocstringstream sstr;
2987 cellInset(cell)->plaintext(sstr, runparams);
2988 if (clen[j] < sstr.str().length())
2989 clen[j] = sstr.str().length();
2992 // then all (real) multicolumn cells!
2993 for (col_type j = 0; j < column_info.size(); ++j) {
2994 for (row_type i = 0; i < row_info.size(); ++i) {
2995 idx_type cell = cellIndex(i, j);
2996 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2998 odocstringstream sstr;
2999 cellInset(cell)->plaintext(sstr, runparams);
3000 int len = int(sstr.str().length());
3001 idx_type const n = columnSpan(cell);
3002 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
3004 if (len > int(clen[j + n - 1]))
3005 clen[j + n - 1] = len;
3010 for (row_type i = 0; i < row_info.size(); ++i) {
3011 if (!onlydata && plaintextTopHLine(os, i, clen))
3012 os << docstring(depth * 2, ' ');
3013 for (col_type j = 0; j < column_info.size(); ++j) {
3014 if (isPartOfMultiColumn(i, j))
3016 if (onlydata && j > 0)
3017 // we don't use operator<< for single UCS4 character.
3018 // see explanation in docstream.h
3020 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
3025 os << docstring(depth * 2, ' ');
3026 if (plaintextBottomHLine(os, i, clen))
3027 os << docstring(depth * 2, ' ');
3033 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3035 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3039 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3040 col_type column) const
3042 return cell_info[row][column].inset;
3046 void Tabular::setCellInset(row_type row, col_type column,
3047 shared_ptr<InsetTableCell> ins) const
3049 CellData & cd = cell_info[row][column];
3054 void Tabular::validate(LaTeXFeatures & features) const
3056 features.require("NeedTabularnewline");
3058 features.require("booktabs");
3059 if (is_long_tabular)
3060 features.require("longtable");
3062 features.require("rotating");
3063 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3064 if (isMultiRow(cell))
3065 features.require("multirow");
3066 if (getVAlignment(cell) != LYX_VALIGN_TOP
3067 || !getPWidth(cell).zero())
3068 features.require("array");
3069 cellInset(cell)->validate(features);
3074 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3076 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3077 ParagraphList::const_iterator cit = parlist.begin();
3078 ParagraphList::const_iterator end = parlist.end();
3080 for (; cit != end; ++cit)
3081 for (int i = 0; i < cit->size(); ++i)
3082 if (cit->isNewline(i))
3089 /////////////////////////////////////////////////////////////////////
3093 /////////////////////////////////////////////////////////////////////
3095 InsetTableCell::InsetTableCell(Buffer * buf)
3096 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3097 contentAlign(LYX_ALIGN_CENTER)
3101 bool InsetTableCell::forcePlainLayout(idx_type) const
3103 return !isFixedWidth;
3107 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3109 return isFixedWidth;
3113 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3114 FuncStatus & status) const
3117 switch (cmd.action) {
3119 enabled = !forcePlainLayout();
3121 case LFUN_LAYOUT_PARAGRAPH:
3122 enabled = allowParagraphCustomization();
3125 return InsetText::getStatus(cur, cmd, status);
3127 status.setEnabled(enabled);
3131 docstring InsetTableCell::asString(bool intoInsets)
3134 if (paragraphs().empty())
3136 ParagraphList::const_iterator it = paragraphs().begin();
3137 ParagraphList::const_iterator en = paragraphs().end();
3139 for (; it != en; ++it) {
3144 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3150 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3153 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3154 return InsetText::xhtml(xs, rp);
3159 /////////////////////////////////////////////////////////////////////
3163 /////////////////////////////////////////////////////////////////////
3165 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3167 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3168 rowselect_(false), colselect_(false)
3173 InsetTabular::InsetTabular(InsetTabular const & tab)
3174 : Inset(tab), tabular(tab.tabular), scx_(0)
3179 InsetTabular::~InsetTabular()
3181 hideDialogs("tabular", this);
3185 void InsetTabular::setBuffer(Buffer & buf)
3187 tabular.setBuffer(buf);
3188 Inset::setBuffer(buf);
3192 bool InsetTabular::insetAllowed(InsetCode code) const
3194 if (code == MATHMACRO_CODE
3195 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3202 void InsetTabular::write(ostream & os) const
3204 os << "Tabular" << endl;
3209 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3211 // FIXME: depending on the selection state, we could offer a different menu.
3212 return from_ascii("context-tabular");
3216 void InsetTabular::read(Lexer & lex)
3218 //bool const old_format = (lex.getString() == "\\LyXTable");
3226 string token = lex.getString();
3227 while (lex && token != "\\end_inset") {
3229 token = lex.getString();
3232 lex.printError("Missing \\end_inset at this point. ");
3236 int InsetTabular::rowFromY(Cursor & cur, int y) const
3238 // top y coordinate of tabular
3239 int h = yo(cur.bv()) - tabular.rowAscent(0);
3240 size_t nrows = tabular.row_info.size();
3242 for (; r < nrows && y > h; ++r)
3243 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3244 + tabular.interRowSpace(r);
3250 int InsetTabular::columnFromX(Cursor & cur, int x) const
3252 // left x coordinate of tabular
3253 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3254 size_t ncols = tabular.column_info.size();
3256 for (; c < ncols && x > w; ++c)
3257 w += tabular.columnWidth(c);
3262 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3264 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3265 // mi.base.textwidth << "\n";
3268 LASSERT(false, /**/);
3271 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3274 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3275 if (tabular.isPartOfMultiColumn(i, j)
3276 || tabular.isPartOfMultiRow(i, j))
3277 // multicolumn or multirow cell, but not first one
3279 idx_type const cell = tabular.cellIndex(i, j);
3283 if (tabular.cell_info[i][j].multicolumn ==
3284 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3285 p_width = tabular.cellInfo(cell).p_width;
3287 p_width = tabular.column_info[j].p_width;
3288 if (!p_width.zero())
3289 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3290 tabular.cellInset(cell)->metrics(m, dim);
3291 if (!p_width.zero())
3292 dim.wid = m.base.textwidth;
3293 tabular.setCellWidth(cell, dim.wid);
3294 maxAsc = max(maxAsc, dim.asc);
3295 maxDesc = max(maxDesc, dim.des);
3297 int const top_space = tabular.row_info[i].top_space_default ?
3298 default_line_space :
3299 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3300 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3301 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3302 default_line_space :
3303 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3304 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3306 tabular.updateColumnWidths();
3307 dim.asc = tabular.rowAscent(0);
3308 dim.des = tabular.height() - dim.asc;
3309 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3312 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3315 if (&cur.inset() == this && cur.selection()) {
3316 if (cur.selIsMultiCell()) {
3319 getSelection(cur, rs, re, cs, ce);
3321 if (col >= cs && col <= ce && row >= rs && row <= re)
3324 if (col == tabular.cellColumn(cur.idx())
3325 && row == tabular.cellRow(cur.idx())) {
3326 CursorSlice const & beg = cur.selBegin();
3327 CursorSlice const & end = cur.selEnd();
3329 if ((end.lastpos() > 0 || end.lastpit() > 0)
3330 && end.pos() == end.lastpos() && beg.pos() == 0
3331 && end.pit() == end.lastpit() && beg.pit() == 0)
3339 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3341 x += scx_ + ADD_TO_TABULAR_WIDTH;
3343 BufferView * bv = pi.base.bv;
3344 Cursor & cur = pi.base.bv->cursor();
3347 // FIXME: As the full background is painted in drawSelection(),
3348 // we have no choice but to do a full repaint for the Text cells.
3349 pi.full_repaint = true;
3351 bool const original_selection_state = pi.selected;
3354 first_visible_cell = Tabular::npos;
3355 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3357 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3358 if (tabular.isPartOfMultiColumn(i, j))
3361 idx = tabular.cellIndex(i, j);
3363 if (tabular.isPartOfMultiRow(i, j)) {
3364 nx += tabular.columnWidth(idx);
3368 if (first_visible_cell == Tabular::npos)
3369 first_visible_cell = idx;
3371 pi.selected |= isCellSelected(cur, i, j);
3372 int const cx = nx + tabular.textHOffset(idx);
3373 int const cy = y + tabular.textVOffset(idx);
3374 // Cache the Inset position.
3375 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3376 cell(idx)->draw(pi, cx, cy);
3377 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3378 nx += tabular.columnWidth(idx);
3379 pi.selected = original_selection_state;
3382 if (i + 1 < tabular.row_info.size())
3383 y += tabular.rowDescent(i) + tabular.rowAscent(i + 1)
3384 + tabular.interRowSpace(i + 1);
3389 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3391 Cursor & cur = pi.base.bv->cursor();
3394 x += scx_ + ADD_TO_TABULAR_WIDTH;
3396 // FIXME: it is wrong to completely paint the background
3397 // if we want to do single row painting.
3399 // Paint background of current tabular
3400 int const w = tabular.width();
3401 int const h = tabular.height();
3402 int yy = y - tabular.rowAscent(0);
3403 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3405 if (!cur.selection())
3407 if (&cur.inset() != this)
3412 bool const full_cell_selected = isCellSelected(cur,
3413 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3415 if (cur.selIsMultiCell() || full_cell_selected) {
3416 y -= tabular.rowAscent(0);
3417 for (row_type r = 0; r < tabular.row_info.size(); ++r) {
3419 for (col_type c = 0; c < tabular.column_info.size(); ++c) {
3420 if (tabular.isPartOfMultiColumn(r, c))
3423 idx_type const cell = tabular.cellIndex(r, c);
3425 if (tabular.isPartOfMultiRow(r, c)) {
3426 xx += tabular.columnWidth(cell);
3429 int const w = tabular.columnWidth(cell);
3430 int const h = tabular.rowHeight(cell);
3431 if (isCellSelected(cur, r, c))
3432 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3435 if (r + 1 < tabular.row_info.size())
3436 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3437 + tabular.interRowSpace(r + 1);
3441 x += cellXPos(cur.idx());
3442 x += tabular.textHOffset(cur.idx());
3443 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3448 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3449 row_type row, idx_type cell, Change const & change) const
3451 y = y - tabular.rowAscent(row);
3452 int const w = tabular.columnWidth(cell);
3453 int const h = tabular.rowHeight(cell);
3454 bool on_off = false;
3455 Color col = Color_tabularline;
3456 Color onoffcol = Color_tabularonoffline;
3458 if (change.changed()) {
3459 col = change.color();
3460 onoffcol = change.color();
3463 if (!tabular.topAlreadyDrawn(cell)) {
3464 on_off = !tabular.topLine(cell);
3465 pain.line(x, y, x + w, y,
3466 on_off ? onoffcol : col,
3467 on_off ? Painter::line_onoffdash : Painter::line_solid);
3469 on_off = !tabular.bottomLine(cell);
3470 pain.line(x, y + h, x + w, y + h,
3471 on_off ? onoffcol : col,
3472 on_off ? Painter::line_onoffdash : Painter::line_solid);
3473 if (!tabular.leftAlreadyDrawn(cell)) {
3474 on_off = !tabular.leftLine(cell);
3475 pain.line(x, y, x, y + h,
3476 on_off ? onoffcol : col,
3477 on_off ? Painter::line_onoffdash : Painter::line_solid);
3479 on_off = !tabular.rightLine(cell);
3480 pain.line(x + w - tabular.interColumnSpace(cell), y,
3481 x + w - tabular.interColumnSpace(cell), y + h,
3482 on_off ? onoffcol : col,
3483 on_off ? Painter::line_onoffdash : Painter::line_solid);
3487 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3489 //lyxerr << "InsetTabular::edit: " << this << endl;
3491 cur.setSelection(false);
3494 if (isRightToLeft(cur))
3495 cur.idx() = tabular.getLastCellInRow(0);
3501 if (isRightToLeft(cur))
3502 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3504 cur.idx() = tabular.numberofcells - 1;
3506 cur.pos() = cur.lastpos(); // FIXME crude guess
3508 cur.setCurrentFont();
3509 // FIXME: this accesses the position cache before it is initialized
3511 //cur.bv().fitCursor();
3515 void InsetTabular::updateLabels(ParIterator const & it, UpdateType utype)
3517 // In a longtable, tell captions what the current float is
3518 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3519 string const saveflt = cnts.current_float();
3520 if (tabular.is_long_tabular)
3521 cnts.current_float("table");
3523 ParIterator it2 = it;
3525 size_t const end = it2.nargs();
3526 for ( ; it2.idx() < end; it2.top().forwardIdx())
3527 buffer().updateLabels(it2, utype);
3530 if (tabular.is_long_tabular)
3531 cnts.current_float(saveflt);
3535 void InsetTabular::addToToc(DocIterator const & cpit)
3537 DocIterator dit = cpit;
3539 size_t const end = dit.nargs();
3540 for ( ; dit.idx() < end; dit.top().forwardIdx())
3541 cell(dit.idx())->addToToc(dit);
3545 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3547 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3548 << "\n cur:" << cur);
3549 CursorSlice sl = cur.top();
3550 Cursor & bvcur = cur.bv().cursor();
3552 switch (cmd.action) {
3554 case LFUN_MOUSE_PRESS: {
3555 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3557 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3558 || cmd.x > xo(cur.bv()) + tabular.width()) {
3559 row_type r = rowFromY(cur, cmd.y);
3560 cur.idx() = tabular.getFirstCellInRow(r);
3563 cur.idx() = tabular.getLastCellInRow(r);
3564 cur.pos() = cur.lastpos();
3565 cur.setSelection(true);
3571 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3572 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3573 || cmd.y > y0 + tabular.height()) {
3574 col_type c = columnFromX(cur, cmd.x);
3575 cur.idx() = tabular.cellIndex(0, c);
3578 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3579 cur.pos() = cur.lastpos();
3580 cur.setSelection(true);
3585 // do not reset cursor/selection if we have selected
3586 // some cells (bug 2715).
3587 if (cmd.button() == mouse_button::button3
3588 && &bvcur.selBegin().inset() == this
3589 && bvcur.selIsMultiCell())
3592 // Let InsetTableCell do it
3593 cell(cur.idx())->dispatch(cur, cmd);
3596 case LFUN_MOUSE_MOTION:
3597 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3598 if (cmd.button() == mouse_button::button1) {
3599 // only accept motions to places not deeper nested than the real anchor
3600 if (!bvcur.anchor_.hasPart(cur)) {
3604 // select (additional) row
3606 row_type r = rowFromY(cur, cmd.y);
3607 cur.idx() = tabular.getLastCellInRow(r);
3608 // we need to reset the cursor's pit and pos now, as the old ones
3609 // may no longer be valid.
3612 bvcur.setCursor(cur);
3613 bvcur.setSelection(true);
3616 // select (additional) column
3618 col_type c = columnFromX(cur, cmd.x);
3619 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3620 // we need to reset the cursor's pit and pos now, as the old ones
3621 // may no longer be valid.
3624 bvcur.setCursor(cur);
3625 bvcur.setSelection(true);
3628 // only update if selection changes
3629 if (bvcur.idx() == cur.idx() &&
3630 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3632 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3633 bvcur.setCursor(cur);
3634 bvcur.setSelection(true);
3635 // if this is a multicell selection, we just set the cursor to
3636 // the beginning of the cell's text.
3637 if (bvcur.selIsMultiCell()) {
3638 bvcur.pit() = bvcur.lastpit();
3639 bvcur.pos() = bvcur.lastpos();
3644 case LFUN_MOUSE_RELEASE:
3649 case LFUN_CELL_BACKWARD:
3651 cur.setSelection(false);
3654 case LFUN_CELL_FORWARD:
3656 cur.setSelection(false);
3659 case LFUN_CHAR_FORWARD_SELECT:
3660 case LFUN_CHAR_FORWARD:
3661 case LFUN_CHAR_BACKWARD_SELECT:
3662 case LFUN_CHAR_BACKWARD:
3663 case LFUN_CHAR_RIGHT_SELECT:
3664 case LFUN_CHAR_RIGHT:
3665 case LFUN_CHAR_LEFT_SELECT:
3666 case LFUN_CHAR_LEFT: {
3667 // determine whether we move to next or previous cell, where to enter
3668 // the new cell from, and which command to "finish" (i.e., exit the
3671 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3672 FuncCode finish_lfun;
3674 if (cmd.action == LFUN_CHAR_FORWARD
3675 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3677 finish_lfun = LFUN_FINISHED_FORWARD;
3679 else if (cmd.action == LFUN_CHAR_BACKWARD
3680 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3682 finish_lfun = LFUN_FINISHED_BACKWARD;
3684 // LEFT or RIGHT commands --- the interpretation will depend on the
3685 // table's direction.
3687 bool const right = cmd.action == LFUN_CHAR_RIGHT
3688 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3689 next_cell = isRightToLeft(cur) != right;
3691 if (lyxrc.visual_cursor)
3692 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3694 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3697 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3698 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3699 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3700 cmd.action == LFUN_CHAR_LEFT_SELECT;
3702 // If we have a multicell selection or we're
3703 // not doing some LFUN_*_SELECT thing anyway...
3704 if (!cur.selIsMultiCell() || !select) {
3705 col_type const c = tabular.cellColumn(cur.idx());
3706 row_type const r = tabular.cellRow(cur.idx());
3707 // Are we trying to select the whole cell and is the whole cell
3708 // not yet selected?
3709 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3710 ((next_cell && cur.pit() == cur.lastpit()
3711 && cur.pos() == cur.lastpos())
3712 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3714 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3716 // ...try to dispatch to the cell's inset.
3717 cell(cur.idx())->dispatch(cur, cmd);
3719 // When we already have a selection we want to select the whole cell
3720 // before going to the next cell.
3721 if (select_whole && !empty_cell){
3722 getText(cur.idx())->selectAll(cur);
3727 // FIXME: When we support the selection of an empty cell, remove
3728 // the !empty_cell from this condition. For now we jump to the next
3729 // cell if the current cell is empty.
3730 if (cur.result().dispatched() && !empty_cell)
3734 // move to next/prev cell, as appropriate
3735 // note that we will always do this if we're selecting and we have
3736 // a multicell selection
3737 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3738 << " cell from: " << int(entry_from));
3740 moveNextCell(cur, entry_from);
3742 movePrevCell(cur, entry_from);
3743 // if we're exiting the table, call the appropriate FINISHED lfun
3744 if (sl == cur.top())
3745 cmd = FuncRequest(finish_lfun);
3752 case LFUN_DOWN_SELECT:
3754 if (!(cur.selection() && cur.selIsMultiCell()))
3755 cell(cur.idx())->dispatch(cur, cmd);
3757 cur.dispatched(); // override the cell's decision
3758 if (sl == cur.top()) {
3759 // if our Text didn't do anything to the cursor
3760 // then we try to put the cursor into the cell below
3761 // setting also the right targetX.
3762 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3763 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3764 cur.idx() = tabular.cellBelow(cur.idx());
3766 TextMetrics const & tm =
3767 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3768 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3769 cur.setCurrentFont();
3772 if (sl == cur.top()) {
3773 // we trick it to go to forward after leaving the
3775 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3778 if (cur.selIsMultiCell()) {
3779 cur.pit() = cur.lastpit();
3780 cur.pos() = cur.lastpos();
3781 cur.setCurrentFont();
3786 case LFUN_UP_SELECT:
3788 if (!(cur.selection() && cur.selIsMultiCell()))
3789 cell(cur.idx())->dispatch(cur, cmd);
3790 cur.dispatched(); // override the cell's decision
3791 if (sl == cur.top()) {
3792 // if our Text didn't do anything to the cursor
3793 // then we try to put the cursor into the cell above
3794 // setting also the right targetX.
3795 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3796 if (tabular.cellRow(cur.idx()) != 0) {
3797 cur.idx() = tabular.cellAbove(cur.idx());
3798 cur.pit() = cur.lastpit();
3799 Text const * text = cell(cur.idx())->getText(0);
3800 TextMetrics const & tm = cur.bv().textMetrics(text);
3801 ParagraphMetrics const & pm =
3802 tm.parMetrics(cur.lastpit());
3803 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3804 cur.setCurrentFont();
3807 if (sl == cur.top()) {
3808 cmd = FuncRequest(LFUN_UP);
3811 if (cur.selIsMultiCell()) {
3813 cur.pos() = cur.lastpos();
3814 cur.setCurrentFont();
3819 // case LFUN_SCREEN_DOWN: {
3820 // //if (hasSelection())
3821 // // cur.selection() = false;
3822 // col_type const col = tabular.cellColumn(cur.idx());
3823 // int const t = cur.bv().top_y() + cur.bv().height();
3824 // if (t < yo() + tabular.getHeightOfTabular()) {
3825 // cur.bv().scrollDocView(t);
3826 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3828 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3835 // case LFUN_SCREEN_UP: {
3836 // //if (hasSelection())
3837 // // cur.selection() = false;
3838 // col_type const col = tabular.cellColumn(cur.idx());
3839 // int const t = cur.bv().top_y() + cur.bv().height();
3841 // cur.bv().scrollDocView(t);
3845 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3849 // cur.par() = cur.lastpar();
3850 // cur.pos() = cur.lastpos();
3854 case LFUN_LAYOUT_TABULAR:
3855 cur.bv().showDialog("tabular");
3858 case LFUN_INSET_MODIFY:
3859 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3863 // insert file functions
3864 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3865 case LFUN_FILE_INSERT_PLAINTEXT:
3867 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3868 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3869 FileName(to_utf8(cmd.argument())));
3872 cur.recordUndoInset(INSERT_UNDO);
3873 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3874 // content has been replaced,
3875 // so cursor might be invalid
3876 cur.pos() = cur.lastpos();
3877 cur.pit() = cur.lastpit();
3878 bvcur.setCursor(cur);
3885 if (cur.selIsMultiCell()) {
3886 if (copySelection(cur)) {
3887 cur.recordUndoInset(DELETE_UNDO);
3891 cell(cur.idx())->dispatch(cur, cmd);
3894 case LFUN_SELF_INSERT:
3895 if (cur.selIsMultiCell()) {
3896 cur.recordUndoInset(DELETE_UNDO);
3899 cell(cur.idx())->dispatch(cur, cmd);
3902 case LFUN_CHAR_DELETE_BACKWARD:
3903 case LFUN_CHAR_DELETE_FORWARD:
3904 if (cur.selIsMultiCell()) {
3905 cur.recordUndoInset(DELETE_UNDO);
3908 cell(cur.idx())->dispatch(cur, cmd);
3912 if (!cur.selection())
3914 if (cur.selIsMultiCell()) {
3918 cell(cur.idx())->dispatch(cur, cmd);
3921 case LFUN_CLIPBOARD_PASTE:
3922 case LFUN_PRIMARY_SELECTION_PASTE: {
3923 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3924 theClipboard().getAsText() :
3925 theSelection().get();
3928 // pass to InsertPlaintextString, but
3929 // only if we have multi-cell content
3930 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3931 cur.recordUndoInset(INSERT_UNDO);
3932 if (insertPlaintextString(cur.bv(), clip, false)) {
3933 // content has been replaced,
3934 // so cursor might be invalid
3935 cur.pos() = cur.lastpos();
3936 cur.pit() = cur.lastpit();
3937 bvcur.setCursor(cur);
3941 // Let the cell handle normal text
3942 cell(cur.idx())->dispatch(cur, cmd);
3947 if (!tabularStackDirty()) {
3948 if (!cur.selIsMultiCell())
3949 cell(cur.idx())->dispatch(cur, cmd);
3952 if (theClipboard().isInternal() ||
3953 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3954 cur.recordUndoInset(INSERT_UNDO);
3955 pasteClipboard(cur);
3959 case LFUN_FONT_EMPH:
3960 case LFUN_FONT_BOLD:
3961 case LFUN_FONT_BOLDSYMBOL:
3962 case LFUN_FONT_ROMAN:
3963 case LFUN_FONT_NOUN:
3964 case LFUN_FONT_ITAL:
3965 case LFUN_FONT_FRAK:
3966 case LFUN_FONT_TYPEWRITER:
3967 case LFUN_FONT_SANS:
3968 case LFUN_TEXTSTYLE_APPLY:
3969 case LFUN_TEXTSTYLE_UPDATE:
3970 case LFUN_FONT_SIZE:
3971 case LFUN_FONT_UNDERLINE:
3972 case LFUN_FONT_STRIKEOUT:
3973 case LFUN_FONT_UULINE:
3974 case LFUN_FONT_UWAVE:
3976 case LFUN_WORD_CAPITALIZE:
3977 case LFUN_WORD_UPCASE:
3978 case LFUN_WORD_LOWCASE:
3979 case LFUN_CHARS_TRANSPOSE:
3980 if (cur.selIsMultiCell()) {
3983 getSelection(cur, rs, re, cs, ce);
3984 Cursor tmpcur = cur;
3985 for (row_type i = rs; i <= re; ++i) {
3986 for (col_type j = cs; j <= ce; ++j) {
3987 // cursor follows cell:
3988 tmpcur.idx() = tabular.cellIndex(i, j);
3989 // select this cell only:
3992 tmpcur.resetAnchor();
3993 tmpcur.pit() = tmpcur.lastpit();
3994 tmpcur.pos() = tmpcur.top().lastpos();
3995 tmpcur.setCursor(tmpcur);
3996 tmpcur.setSelection();
3997 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4002 cell(cur.idx())->dispatch(cur, cmd);
4006 case LFUN_INSET_SETTINGS:
4007 // relay this lfun to Inset, not to the cell.
4008 Inset::doDispatch(cur, cmd);
4012 // we try to handle this event in the insets dispatch function.
4013 cell(cur.idx())->dispatch(cur, cmd);
4019 // function sets an object as defined in func_status.h:
4020 // states OK, Unknown, Disabled, On, Off.
4021 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4022 FuncStatus & status) const
4024 switch (cmd.action) {
4025 case LFUN_INSET_MODIFY: {
4026 istringstream is(to_utf8(cmd.argument()));
4029 if (insetCode(s) != TABULAR_CODE) {
4031 status.setEnabled(false);
4035 // FIXME: We only check for the very first argument...
4036 int action = Tabular::LAST_ACTION;
4038 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4039 string const tmp = tabularFeature[i].feature;
4040 if (tmp == s.substr(0, tmp.length())) {
4041 action = tabularFeature[i].action;
4045 if (action == Tabular::LAST_ACTION) {
4047 status.unknown(true);
4051 string const argument
4052 = ltrim(s.substr(tabularFeature[i].feature.length()));
4054 row_type sel_row_start = 0;
4055 row_type sel_row_end = 0;
4056 col_type sel_col_start = 0;
4057 col_type sel_col_end = 0;
4058 Tabular::ltType dummyltt;
4061 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4064 case Tabular::SET_PWIDTH:
4065 case Tabular::SET_MPWIDTH:
4066 case Tabular::SET_SPECIAL_COLUMN:
4067 case Tabular::SET_SPECIAL_MULTICOLUMN:
4068 case Tabular::SET_SPECIAL_MULTIROW:
4069 case Tabular::APPEND_ROW:
4070 case Tabular::APPEND_COLUMN:
4071 case Tabular::DELETE_ROW:
4072 case Tabular::DELETE_COLUMN:
4073 case Tabular::COPY_ROW:
4074 case Tabular::COPY_COLUMN:
4075 case Tabular::SET_TOP_SPACE:
4076 case Tabular::SET_BOTTOM_SPACE:
4077 case Tabular::SET_INTERLINE_SPACE:
4081 case Tabular::MULTICOLUMN:
4082 // If a row is set as longtable caption, it must not be allowed
4083 // to unset that this row is a multicolumn.
4084 status.setEnabled(sel_row_start == sel_row_end
4085 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4086 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4089 case Tabular::MULTIROW:
4090 // If a row is set as longtable caption, it must not be allowed
4091 // to unset that this row is a multirow.
4092 status.setEnabled(sel_col_start == sel_col_end
4093 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4094 status.setOnOff(tabular.isMultiRow(cur.idx()));
4097 case Tabular::SET_ALL_LINES:
4098 case Tabular::UNSET_ALL_LINES:
4099 case Tabular::SET_BORDER_LINES:
4100 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4103 case Tabular::SET_LINE_TOP:
4104 case Tabular::SET_LINE_BOTTOM:
4105 case Tabular::SET_LINE_LEFT:
4106 case Tabular::SET_LINE_RIGHT:
4107 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4110 case Tabular::TOGGLE_LINE_TOP:
4111 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4112 status.setOnOff(tabular.topLine(cur.idx()));
4115 case Tabular::TOGGLE_LINE_BOTTOM:
4116 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4117 status.setOnOff(tabular.bottomLine(cur.idx()));
4120 case Tabular::TOGGLE_LINE_LEFT:
4121 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4122 status.setOnOff(tabular.leftLine(cur.idx()));
4125 case Tabular::TOGGLE_LINE_RIGHT:
4126 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4127 status.setOnOff(tabular.rightLine(cur.idx()));
4130 case Tabular::M_ALIGN_LEFT:
4132 case Tabular::ALIGN_LEFT:
4133 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4136 case Tabular::M_ALIGN_RIGHT:
4138 case Tabular::ALIGN_RIGHT:
4139 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4142 case Tabular::M_ALIGN_CENTER:
4144 case Tabular::ALIGN_CENTER:
4145 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4148 case Tabular::ALIGN_BLOCK:
4149 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
4150 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4153 case Tabular::M_VALIGN_TOP:
4155 case Tabular::VALIGN_TOP:
4157 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4160 case Tabular::M_VALIGN_BOTTOM:
4162 case Tabular::VALIGN_BOTTOM:
4164 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4167 case Tabular::M_VALIGN_MIDDLE:
4169 case Tabular::VALIGN_MIDDLE:
4171 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4174 case Tabular::SET_LONGTABULAR:
4175 status.setOnOff(tabular.is_long_tabular);
4178 case Tabular::UNSET_LONGTABULAR:
4179 status.setOnOff(!tabular.is_long_tabular);
4182 case Tabular::TOGGLE_ROTATE_TABULAR:
4183 case Tabular::SET_ROTATE_TABULAR:
4184 status.setOnOff(tabular.rotate);
4187 case Tabular::TABULAR_VALIGN_TOP:
4188 status.setOnOff(tabular.tabular_valignment
4189 == Tabular::LYX_VALIGN_TOP);
4191 case Tabular::TABULAR_VALIGN_MIDDLE:
4192 status.setOnOff(tabular.tabular_valignment
4193 == Tabular::LYX_VALIGN_MIDDLE);
4195 case Tabular::TABULAR_VALIGN_BOTTOM:
4196 status.setOnOff(tabular.tabular_valignment
4197 == Tabular::LYX_VALIGN_BOTTOM);
4200 case Tabular::LONGTABULAR_ALIGN_LEFT:
4201 status.setOnOff(tabular.longtabular_alignment
4202 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4204 case Tabular::LONGTABULAR_ALIGN_CENTER:
4205 status.setOnOff(tabular.longtabular_alignment
4206 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4208 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4209 status.setOnOff(tabular.longtabular_alignment
4210 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4213 case Tabular::UNSET_ROTATE_TABULAR:
4214 status.setOnOff(!tabular.rotate);
4217 case Tabular::TOGGLE_ROTATE_CELL:
4218 case Tabular::SET_ROTATE_CELL:
4219 status.setOnOff(!oneCellHasRotationState(false,
4220 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4223 case Tabular::UNSET_ROTATE_CELL:
4224 status.setOnOff(!oneCellHasRotationState(true,
4225 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4228 case Tabular::SET_USEBOX:
4229 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4232 // every row can only be one thing:
4233 // either a footer or header or caption
4234 case Tabular::SET_LTFIRSTHEAD:
4235 status.setEnabled(sel_row_start == sel_row_end
4236 && !tabular.ltCaption(sel_row_start));
4237 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4240 case Tabular::UNSET_LTFIRSTHEAD:
4241 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4244 case Tabular::SET_LTHEAD:
4245 status.setEnabled(sel_row_start == sel_row_end
4246 && !tabular.ltCaption(sel_row_start));
4247 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4250 case Tabular::UNSET_LTHEAD:
4251 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4254 case Tabular::SET_LTFOOT:
4255 status.setEnabled(sel_row_start == sel_row_end
4256 && !tabular.ltCaption(sel_row_start));
4257 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4260 case Tabular::UNSET_LTFOOT:
4261 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4264 case Tabular::SET_LTLASTFOOT:
4265 status.setEnabled(sel_row_start == sel_row_end
4266 && !tabular.ltCaption(sel_row_start));
4267 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4270 case Tabular::UNSET_LTLASTFOOT:
4271 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4274 case Tabular::SET_LTNEWPAGE:
4275 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4278 // only one row can be the caption
4279 case Tabular::TOGGLE_LTCAPTION:
4280 status.setEnabled(sel_row_start == sel_row_end
4281 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4282 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4283 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4284 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4285 && (!tabular.haveLTCaption()
4286 || tabular.ltCaption(sel_row_start)));
4287 status.setOnOff(tabular.ltCaption(sel_row_start));
4290 case Tabular::SET_BOOKTABS:
4291 status.setOnOff(tabular.use_booktabs);
4294 case Tabular::UNSET_BOOKTABS:
4295 status.setOnOff(!tabular.use_booktabs);
4300 status.setEnabled(false);
4306 // These are only enabled inside tabular
4307 case LFUN_CELL_BACKWARD:
4308 case LFUN_CELL_FORWARD:
4309 status.setEnabled(true);
4312 // disable these with multiple cells selected
4313 case LFUN_INSET_INSERT:
4314 case LFUN_TABULAR_INSERT:
4315 case LFUN_FLEX_INSERT:
4316 case LFUN_FLOAT_INSERT:
4317 case LFUN_FLOAT_WIDE_INSERT:
4318 case LFUN_FOOTNOTE_INSERT:
4319 case LFUN_MARGINALNOTE_INSERT:
4320 case LFUN_MATH_INSERT:
4321 case LFUN_MATH_MODE:
4322 case LFUN_MATH_MUTATE:
4323 case LFUN_MATH_DISPLAY:
4324 case LFUN_NOTE_INSERT:
4325 case LFUN_OPTIONAL_INSERT:
4326 case LFUN_BOX_INSERT:
4327 case LFUN_BRANCH_INSERT:
4328 case LFUN_PHANTOM_INSERT:
4329 case LFUN_WRAP_INSERT:
4330 case LFUN_ERT_INSERT: {
4331 if (cur.selIsMultiCell()) {
4332 status.setEnabled(false);
4335 return cell(cur.idx())->getStatus(cur, cmd, status);
4338 // disable in non-fixed-width cells
4339 case LFUN_NEWLINE_INSERT:
4340 case LFUN_BREAK_PARAGRAPH: {
4341 if (tabular.getPWidth(cur.idx()).zero()) {
4342 status.setEnabled(false);
4345 return cell(cur.idx())->getStatus(cur, cmd, status);
4348 case LFUN_NEWPAGE_INSERT:
4349 status.setEnabled(false);
4353 if (tabularStackDirty() && theClipboard().isInternal()) {
4354 if (cur.selIsMultiCell()) {
4357 getSelection(cur, rs, re, cs, ce);
4358 if (paste_tabular && paste_tabular->column_info.size() == ce - cs + 1
4359 && paste_tabular->row_info.size() == re - rs + 1)
4360 status.setEnabled(true);
4362 status.setEnabled(false);
4363 status.message(_("Selection size should match clipboard content."));
4366 status.setEnabled(true);
4369 return cell(cur.idx())->getStatus(cur, cmd, status);
4371 case LFUN_INSET_SETTINGS:
4372 // relay this lfun to Inset, not to the cell.
4373 return Inset::getStatus(cur, cmd, status);
4376 // we try to handle this event in the insets dispatch function.
4377 return cell(cur.idx())->getStatus(cur, cmd, status);
4383 Inset::DisplayType InsetTabular::display() const
4385 if (tabular.is_long_tabular) {
4386 switch (tabular.longtabular_alignment) {
4387 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4389 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4391 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4401 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4403 return tabular.latex(os, runparams);
4407 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4409 os << '\n'; // output table on a new line
4410 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4411 tabular.plaintext(os, runparams, dp, false, 0);
4412 return PLAINTEXT_NEWLINE;
4416 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4421 // FIXME: Why not pass a proper DocIterator here?
4423 // if the table is inside a float it doesn't need the informaltable
4424 // wrapper. Search for it.
4425 for (master = owner(); master; master = master->owner())
4426 if (master->lyxCode() == FLOAT_CODE)
4431 os << "<informaltable>";
4434 ret += tabular.docbook(os, runparams);
4436 os << "</informaltable>";
4443 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4446 // It'd be better to be able to get this from an InsetLayout, but at present
4447 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4448 xs << html::StartTag("table");
4449 docstring ret = tabular.xhtml(xs, rp);
4450 xs << html::EndTag("table");
4455 void InsetTabular::validate(LaTeXFeatures & features) const
4457 tabular.validate(features);
4459 // It'd be better to be able to get this from an InsetLayout, but at present
4460 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4461 if (features.runparams().flavor == OutputParams::HTML)
4462 features.addPreambleSnippet("<style type=\"text/css\">\n"
4463 "table { border: 1px solid black; display: inline-block; }\n"
4464 "td { border: 1px solid black; padding: 0.5ex; }\n"
4469 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4471 return tabular.cellInset(idx);
4475 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4477 return tabular.cellInset(idx);
4481 void InsetTabular::cursorPos(BufferView const & bv,
4482 CursorSlice const & sl, bool boundary, int & x, int & y) const
4484 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4486 int const row = tabular.cellRow(sl.idx());
4487 int const col = tabular.cellColumn(sl.idx());
4489 // y offset correction
4490 for (int i = 0; i <= row; ++i) {
4491 if (tabular.isPartOfMultiRow(i, col))
4494 y += tabular.rowAscent(i);
4495 y += tabular.interRowSpace(i);
4498 y += tabular.rowDescent(i);
4500 y += tabular.textVOffset(sl.idx());
4502 // x offset correction
4503 for (int c = 0; c < col; ++c)
4504 x += tabular.column_info[c].width;
4506 x += tabular.textHOffset(sl.idx());
4507 x += ADD_TO_TABULAR_WIDTH;
4512 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4516 Inset const & inset = *tabular.cellInset(cell);
4517 Point o = bv.coordCache().getInsets().xy(&inset);
4518 int const xbeg = o.x_ - tabular.textHOffset(cell);
4519 int const xend = xbeg + tabular.columnWidth(cell);
4520 row_type const row = tabular.cellRow(cell);
4521 int const ybeg = o.y_ - tabular.rowAscent(row)
4522 - tabular.interRowSpace(row);
4523 int const yend = o.y_ + tabular.rowDescent(row);
4535 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4536 // << " ybeg=" << ybeg << " yend=" << yend
4537 // << " xx=" << xx << " yy=" << yy
4538 // << " dist=" << xx + yy << endl;
4543 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4545 //lyxerr << "InsetTabular::editXY: " << this << endl;
4546 cur.setSelection(false);
4548 cur.idx() = getNearestCell(cur.bv(), x, y);
4550 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4554 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4556 cur.idx() = getNearestCell(cur.bv(), x, y);
4557 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4561 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4563 idx_type idx_min = 0;
4564 int dist_min = numeric_limits<int>::max();
4565 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4566 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4567 int const d = dist(bv, i, x, y);
4578 int InsetTabular::cellXPos(idx_type const cell) const
4580 col_type col = tabular.cellColumn(cell);
4582 for (col_type c = 0; c < col; ++c)
4583 lx += tabular.columnWidth(c);
4589 void InsetTabular::resetPos(Cursor & cur) const
4591 BufferView & bv = cur.bv();
4592 int const maxwidth = bv.workWidth();
4594 int const scx_old = scx_;
4595 int const i = cur.find(this);
4600 int const X2 = maxwidth;
4601 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4602 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4603 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4606 scx_ = X1 + 20 - x1;
4608 scx_ = X2 - 20 - x2;
4613 // only update if offset changed
4614 if (scx_ != scx_old)
4615 cur.updateFlags(Update::Force | Update::FitCursor);
4619 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4621 row_type const row = tabular.cellRow(cur.idx());
4622 col_type const col = tabular.cellColumn(cur.idx());
4624 if (isRightToLeft(cur)) {
4625 if (tabular.cellColumn(cur.idx()) == 0) {
4626 if (row == tabular.row_info.size() - 1)
4628 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4633 cur.idx() = tabular.getLastCellInRow(row - 1);
4635 cur.idx() = tabular.cellIndex(row, col - 1);
4638 if (tabular.isLastCell(cur.idx()))
4640 if (cur.idx() == tabular.getLastCellInRow(row))
4641 cur.idx() = tabular.cellIndex(row + 1, 0);
4643 cur.idx() = tabular.cellIndex(row, col + 1);
4646 cur.boundary(false);
4648 if (cur.selIsMultiCell()) {
4649 cur.pit() = cur.lastpit();
4650 cur.pos() = cur.lastpos();
4658 // in visual mode, place cursor at extreme left or right
4660 switch(entry_from) {
4662 case ENTRY_DIRECTION_RIGHT:
4663 cur.posVisToRowExtremity(false /* !left */);
4665 case ENTRY_DIRECTION_LEFT:
4666 cur.posVisToRowExtremity(true /* left */);
4668 case ENTRY_DIRECTION_IGNORE:
4669 // nothing to do in this case
4673 cur.setCurrentFont();
4678 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4680 row_type const row = tabular.cellRow(cur.idx());
4681 col_type const col = tabular.cellColumn(cur.idx());
4683 if (isRightToLeft(cur)) {
4684 if (cur.idx() == tabular.getLastCellInRow(row)) {
4687 cur.idx() = tabular.getFirstCellInRow(row);
4688 cur.idx() = tabular.cellAbove(cur.idx());
4690 if (tabular.isLastCell(cur.idx()))
4692 if (cur.idx() == tabular.getLastCellInRow(row))
4693 cur.idx() = tabular.cellIndex(row + 1, 0);
4695 cur.idx() = tabular.cellIndex(row, col + 1);
4698 if (cur.idx() == 0) // first cell
4701 cur.idx() = tabular.getLastCellInRow(row - 1);
4703 cur.idx() = tabular.cellIndex(row, col - 1);
4706 if (cur.selIsMultiCell()) {
4707 cur.pit() = cur.lastpit();
4708 cur.pos() = cur.lastpos();
4713 cur.pit() = cur.lastpit();
4714 cur.pos() = cur.lastpos();
4716 // in visual mode, place cursor at extreme left or right
4718 switch(entry_from) {
4720 case ENTRY_DIRECTION_RIGHT:
4721 cur.posVisToRowExtremity(false /* !left */);
4723 case ENTRY_DIRECTION_LEFT:
4724 cur.posVisToRowExtremity(true /* left */);
4726 case ENTRY_DIRECTION_IGNORE:
4727 // nothing to do in this case
4731 cur.setCurrentFont();
4736 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4738 istringstream is(argument);
4741 if (insetCode(s) != TABULAR_CODE)
4745 size_t safe_guard = 0;
4750 if (safe_guard > 1000) {
4751 LYXERR0("parameter max count reached!");
4755 Tabular::Feature action = Tabular::LAST_ACTION;
4758 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4759 if (s != tabularFeature[i].feature)
4762 action = tabularFeature[i].action;
4765 if (action == Tabular::LAST_ACTION) {
4766 LYXERR0("Feature not found " << s);
4770 if (tabularFeature[i].need_value)
4772 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4773 tabularFeatures(cur, action, val);
4779 static void checkLongtableSpecial(Tabular::ltType & ltt,
4780 string const & special, bool & flag)
4782 if (special == "dl_above") {
4785 } else if (special == "dl_below") {
4786 ltt.bottomDL = flag;
4788 } else if (special == "empty") {
4797 bool InsetTabular::oneCellHasRotationState(bool rotated,
4798 row_type row_start, row_type row_end,
4799 col_type col_start, col_type col_end) const {
4801 for (row_type i = row_start; i <= row_end; ++i) {
4802 for (col_type j = col_start; j <= col_end; ++j) {
4803 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4812 void InsetTabular::tabularFeatures(Cursor & cur,
4813 Tabular::Feature feature, string const & value)
4815 col_type sel_col_start;
4816 col_type sel_col_end;
4817 row_type sel_row_start;
4818 row_type sel_row_end;
4819 bool setLines = false;
4820 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4821 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4825 case Tabular::M_ALIGN_LEFT:
4826 case Tabular::ALIGN_LEFT:
4827 setAlign = LYX_ALIGN_LEFT;
4830 case Tabular::M_ALIGN_RIGHT:
4831 case Tabular::ALIGN_RIGHT:
4832 setAlign = LYX_ALIGN_RIGHT;
4835 case Tabular::M_ALIGN_CENTER:
4836 case Tabular::ALIGN_CENTER:
4837 setAlign = LYX_ALIGN_CENTER;
4840 case Tabular::ALIGN_BLOCK:
4841 setAlign = LYX_ALIGN_BLOCK;
4844 case Tabular::M_VALIGN_TOP:
4845 case Tabular::VALIGN_TOP:
4846 setVAlign = Tabular::LYX_VALIGN_TOP;
4849 case Tabular::M_VALIGN_BOTTOM:
4850 case Tabular::VALIGN_BOTTOM:
4851 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4854 case Tabular::M_VALIGN_MIDDLE:
4855 case Tabular::VALIGN_MIDDLE:
4856 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4863 cur.recordUndoInset(ATOMIC_UNDO);
4865 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4866 row_type const row = tabular.cellRow(cur.idx());
4867 col_type const column = tabular.cellColumn(cur.idx());
4869 Tabular::ltType ltt;
4873 case Tabular::SET_PWIDTH: {
4874 Length const len(value);
4875 tabular.setColumnPWidth(cur, cur.idx(), len);
4877 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4878 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4882 case Tabular::SET_MPWIDTH:
4883 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4886 case Tabular::SET_SPECIAL_COLUMN:
4887 case Tabular::SET_SPECIAL_MULTICOLUMN:
4888 if (value == "none")
4889 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
4891 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4894 case Tabular::SET_SPECIAL_MULTIROW:
4895 //FIXME: noting to do here?
4898 case Tabular::APPEND_ROW:
4899 // append the row into the tabular
4900 tabular.appendRow(cur.idx());
4903 case Tabular::APPEND_COLUMN:
4904 // append the column into the tabular
4905 tabular.appendColumn(cur.idx());
4906 cur.idx() = tabular.cellIndex(row, column);
4909 case Tabular::DELETE_ROW:
4910 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4911 tabular.deleteRow(sel_row_start);
4912 if (sel_row_start >= tabular.row_info.size())
4914 cur.idx() = tabular.cellIndex(sel_row_start, column);
4917 cur.setSelection(false);
4920 case Tabular::DELETE_COLUMN:
4921 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4922 tabular.deleteColumn(sel_col_start);
4923 if (sel_col_start >= tabular.column_info.size())
4925 cur.idx() = tabular.cellIndex(row, sel_col_start);
4928 cur.setSelection(false);
4931 case Tabular::COPY_ROW:
4932 tabular.copyRow(row);
4935 case Tabular::COPY_COLUMN:
4936 tabular.copyColumn(column);
4937 cur.idx() = tabular.cellIndex(row, column);
4940 case Tabular::SET_LINE_TOP:
4941 case Tabular::TOGGLE_LINE_TOP: {
4942 bool lineSet = (feature == Tabular::SET_LINE_TOP)
4943 ? (value == "true") : !tabular.topLine(cur.idx());
4944 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4945 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4946 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4950 case Tabular::SET_LINE_BOTTOM:
4951 case Tabular::TOGGLE_LINE_BOTTOM: {
4952 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
4953 ? (value == "true") : !tabular.bottomLine(cur.idx());
4954 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4955 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4956 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4960 case Tabular::SET_LINE_LEFT:
4961 case Tabular::TOGGLE_LINE_LEFT: {
4962 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
4963 ? (value == "true") : !tabular.leftLine(cur.idx());
4964 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4965 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4966 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4970 case Tabular::SET_LINE_RIGHT:
4971 case Tabular::TOGGLE_LINE_RIGHT: {
4972 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
4973 ? (value == "true") : !tabular.rightLine(cur.idx());
4974 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4975 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4976 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4980 case Tabular::M_ALIGN_LEFT:
4981 case Tabular::M_ALIGN_RIGHT:
4982 case Tabular::M_ALIGN_CENTER:
4984 case Tabular::ALIGN_LEFT:
4985 case Tabular::ALIGN_RIGHT:
4986 case Tabular::ALIGN_CENTER:
4987 case Tabular::ALIGN_BLOCK:
4988 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4989 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4990 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4993 case Tabular::M_VALIGN_TOP:
4994 case Tabular::M_VALIGN_BOTTOM:
4995 case Tabular::M_VALIGN_MIDDLE:
4997 case Tabular::VALIGN_TOP:
4998 case Tabular::VALIGN_BOTTOM:
4999 case Tabular::VALIGN_MIDDLE:
5000 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5001 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5002 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
5005 case Tabular::MULTICOLUMN: {
5006 if (!cur.selection()) {
5007 // just multicol for one single cell
5008 // check whether we are completely in a multicol
5009 if (tabular.isMultiColumn(cur.idx()))
5010 tabular.unsetMultiColumn(cur.idx());
5012 tabular.setMultiColumn(cur.idx(), 1);
5015 // we have a selection so this means we just add all this
5016 // cells to form a multicolumn cell
5017 idx_type const s_start = cur.selBegin().idx();
5018 row_type const col_start = tabular.cellColumn(s_start);
5019 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5020 tabular.setMultiColumn(s_start, col_end - col_start + 1);
5021 cur.idx() = s_start;
5024 cur.setSelection(false);
5028 case Tabular::MULTIROW: {
5029 if (!cur.selection()) {
5030 // just multirow for one single cell
5031 // check whether we are completely in a multirow
5032 if (tabular.isMultiRow(cur.idx()))
5033 tabular.unsetMultiRow(cur.idx());
5035 tabular.setMultiRow(cur.idx(), 1);
5038 // we have a selection so this means we just add all this
5039 // cells to form a multirow cell
5040 idx_type const s_start = cur.selBegin().idx();
5041 row_type const row_start = tabular.cellRow(s_start);
5042 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5043 tabular.setMultiRow(s_start, row_end - row_start + 1);
5044 cur.idx() = s_start;
5047 cur.setSelection(false);
5051 case Tabular::SET_ALL_LINES:
5053 case Tabular::UNSET_ALL_LINES:
5054 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5055 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5056 tabular.setAllLines(
5057 tabular.cellIndex(i,j), setLines);
5060 case Tabular::SET_BORDER_LINES:
5061 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5062 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
5063 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
5065 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
5066 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
5067 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
5071 case Tabular::SET_LONGTABULAR:
5072 tabular.is_long_tabular = true;
5075 case Tabular::UNSET_LONGTABULAR:
5076 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
5077 if (tabular.ltCaption(i)) {
5078 cur.idx() = tabular.cellIndex(i, 0);
5081 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5084 tabular.is_long_tabular = false;
5087 case Tabular::SET_ROTATE_TABULAR:
5088 tabular.rotate = true;
5091 case Tabular::UNSET_ROTATE_TABULAR:
5092 tabular.rotate = false;
5095 case Tabular::TOGGLE_ROTATE_TABULAR:
5096 tabular.rotate = !tabular.rotate;
5099 case Tabular::TABULAR_VALIGN_TOP:
5100 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5103 case Tabular::TABULAR_VALIGN_MIDDLE:
5104 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5107 case Tabular::TABULAR_VALIGN_BOTTOM:
5108 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5111 case Tabular::LONGTABULAR_ALIGN_LEFT:
5112 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5115 case Tabular::LONGTABULAR_ALIGN_CENTER:
5116 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5119 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5120 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5125 case Tabular::SET_ROTATE_CELL:
5126 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5127 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5128 tabular.setRotateCell(
5129 tabular.cellIndex(i, j), true);
5132 case Tabular::UNSET_ROTATE_CELL:
5133 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5134 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5135 tabular.setRotateCell(tabular.cellIndex(i, j), false);
5138 case Tabular::TOGGLE_ROTATE_CELL:
5140 bool oneNotRotated = oneCellHasRotationState(false,
5141 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5143 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5144 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5145 tabular.setRotateCell(tabular.cellIndex(i, j),
5150 case Tabular::SET_USEBOX: {
5151 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5152 if (val == tabular.getUsebox(cur.idx()))
5153 val = Tabular::BOX_NONE;
5154 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5155 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
5156 tabular.setUsebox(tabular.cellIndex(i, j), val);
5160 case Tabular::UNSET_LTFIRSTHEAD:
5162 case Tabular::SET_LTFIRSTHEAD:
5163 tabular.getRowOfLTFirstHead(row, ltt);
5164 checkLongtableSpecial(ltt, value, flag);
5165 tabular.setLTHead(row, flag, ltt, true);
5168 case Tabular::UNSET_LTHEAD:
5170 case Tabular::SET_LTHEAD:
5171 tabular.getRowOfLTHead(row, ltt);
5172 checkLongtableSpecial(ltt, value, flag);
5173 tabular.setLTHead(row, flag, ltt, false);
5176 case Tabular::UNSET_LTFOOT:
5178 case Tabular::SET_LTFOOT:
5179 tabular.getRowOfLTFoot(row, ltt);
5180 checkLongtableSpecial(ltt, value, flag);
5181 tabular.setLTFoot(row, flag, ltt, false);
5184 case Tabular::UNSET_LTLASTFOOT:
5186 case Tabular::SET_LTLASTFOOT:
5187 tabular.getRowOfLTLastFoot(row, ltt);
5188 checkLongtableSpecial(ltt, value, flag);
5189 tabular.setLTFoot(row, flag, ltt, true);
5192 case Tabular::SET_LTNEWPAGE:
5193 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5196 case Tabular::TOGGLE_LTCAPTION: {
5197 bool const set = !tabular.ltCaption(row);
5198 cur.idx() = tabular.setLTCaption(row, set);
5201 cur.setSelection(false);
5204 // When a row is set as caption, then also insert
5205 // a caption. Otherwise the LaTeX output is broken.
5206 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5207 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5209 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5210 if (lyx::getStatus(fr).enabled())
5216 case Tabular::SET_BOOKTABS:
5217 tabular.use_booktabs = true;
5220 case Tabular::UNSET_BOOKTABS:
5221 tabular.use_booktabs = false;
5224 case Tabular::SET_TOP_SPACE: {
5226 if (value == "default")
5227 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5228 tabular.row_info[i].top_space_default = true;
5229 else if (value == "none")
5230 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5231 tabular.row_info[i].top_space_default = false;
5232 tabular.row_info[i].top_space = len;
5234 else if (isValidLength(value, &len))
5235 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5236 tabular.row_info[i].top_space_default = false;
5237 tabular.row_info[i].top_space = len;
5242 case Tabular::SET_BOTTOM_SPACE: {
5244 if (value == "default")
5245 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5246 tabular.row_info[i].bottom_space_default = true;
5247 else if (value == "none")
5248 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5249 tabular.row_info[i].bottom_space_default = false;
5250 tabular.row_info[i].bottom_space = len;
5252 else if (isValidLength(value, &len))
5253 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5254 tabular.row_info[i].bottom_space_default = false;
5255 tabular.row_info[i].bottom_space = len;
5260 case Tabular::SET_INTERLINE_SPACE: {
5262 if (value == "default")
5263 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5264 tabular.row_info[i].interline_space_default = true;
5265 else if (value == "none")
5266 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5267 tabular.row_info[i].interline_space_default = false;
5268 tabular.row_info[i].interline_space = len;
5270 else if (isValidLength(value, &len))
5271 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5272 tabular.row_info[i].interline_space_default = false;
5273 tabular.row_info[i].interline_space = len;
5278 // dummy stuff just to avoid warnings
5279 case Tabular::LAST_ACTION:
5285 bool InsetTabular::copySelection(Cursor & cur)
5287 if (!cur.selection())
5292 getSelection(cur, rs, re, cs, ce);
5294 paste_tabular.reset(new Tabular(tabular));
5296 for (row_type i = 0; i < rs; ++i)
5297 paste_tabular->deleteRow(0);
5299 row_type const rows = re - rs + 1;
5300 while (paste_tabular->row_info.size() > rows)
5301 paste_tabular->deleteRow(rows);
5303 for (col_type i = 0; i < cs; ++i)
5304 paste_tabular->deleteColumn(0);
5306 col_type const columns = ce - cs + 1;
5307 while (paste_tabular->column_info.size() > columns)
5308 paste_tabular->deleteColumn(columns);
5310 paste_tabular->setBuffer(tabular.buffer());
5312 odocstringstream os;
5313 OutputParams const runparams(0);
5314 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5315 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5316 cap::copySelection(cur, os.str());
5318 // mark tabular stack dirty
5319 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5320 // when we (hopefully) have a one-for-all paste mechanism.
5321 // This must be called after cap::copySelection.
5322 dirtyTabularStack(true);
5328 bool InsetTabular::pasteClipboard(Cursor & cur)
5332 col_type actcol = tabular.cellColumn(cur.idx());
5333 row_type actrow = tabular.cellRow(cur.idx());
5335 if (cur.selIsMultiCell()) {
5338 getSelection(cur, actrow, re, actcol, ce);
5341 for (row_type r1 = 0, r2 = actrow;
5342 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
5344 for (col_type c1 = 0, c2 = actcol;
5345 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
5347 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5348 tabular.isPartOfMultiColumn(r2, c2))
5350 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5354 if (tabular.isPartOfMultiColumn(r2, c2)) {
5358 shared_ptr<InsetTableCell> inset(
5359 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5360 tabular.setCellInset(r2, c2, inset);
5361 // FIXME?: why do we need to do this explicitly? (EL)
5362 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5364 // FIXME: change tracking (MG)
5365 inset->setChange(Change(buffer().params().trackChanges ?
5366 Change::INSERTED : Change::UNCHANGED));
5374 void InsetTabular::cutSelection(Cursor & cur)
5376 if (!cur.selection())
5381 getSelection(cur, rs, re, cs, ce);
5382 for (row_type i = rs; i <= re; ++i) {
5383 for (col_type j = cs; j <= ce; ++j) {
5384 shared_ptr<InsetTableCell> t
5385 = cell(tabular.cellIndex(i, j));
5386 if (buffer().params().trackChanges)
5387 // FIXME: Change tracking (MG)
5388 t->setChange(Change(Change::DELETED));
5394 // cursor position might be invalid now
5395 if (cur.pit() > cur.lastpit())
5396 cur.pit() = cur.lastpit();
5397 if (cur.pos() > cur.lastpos())
5398 cur.pos() = cur.lastpos();
5399 cur.clearSelection();
5403 bool InsetTabular::isRightToLeft(Cursor & cur) const
5405 LASSERT(cur.depth() > 1, /**/);
5406 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5407 pos_type const parentpos = cur[cur.depth() - 2].pos();
5408 return parentpar.getFontSettings(buffer().params(),
5409 parentpos).language()->rightToLeft();
5412 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5415 LASSERT(stidx <= enidx, return docstring());
5417 col_type const col1 = tabular.cellColumn(stidx);
5418 col_type const col2 = tabular.cellColumn(enidx);
5419 row_type const row1 = tabular.cellRow(stidx);
5420 row_type const row2 = tabular.cellRow(enidx);
5422 for (col_type col = col1; col <= col2; col++)
5423 for (row_type row = row1; row <= row2; row++) {
5428 retval += tabular.cellInset(row, col)->asString(intoInsets);
5434 void InsetTabular::getSelection(Cursor & cur,
5435 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5437 CursorSlice const & beg = cur.selBegin();
5438 CursorSlice const & end = cur.selEnd();
5439 cs = tabular.cellColumn(beg.idx());
5440 ce = tabular.cellColumn(end.idx());
5443 cs = tabular.cellColumn(end.idx());
5445 ce = tabular.cellRightColumn(end.idx());
5448 rs = tabular.cellRow(beg.idx());
5449 re = tabular.cellRow(end.idx());
5455 Text * InsetTabular::getText(int idx) const
5457 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5461 void InsetTabular::setChange(Change const & change)
5463 for (idx_type idx = 0; idx < nargs(); ++idx)
5464 cell(idx)->setChange(change);
5468 void InsetTabular::acceptChanges()
5470 for (idx_type idx = 0; idx < nargs(); ++idx)
5471 cell(idx)->acceptChanges();
5475 void InsetTabular::rejectChanges()
5477 for (idx_type idx = 0; idx < nargs(); ++idx)
5478 cell(idx)->rejectChanges();
5482 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5484 return tabular.getPWidth(cell).zero();
5488 bool InsetTabular::forcePlainLayout(idx_type cell) const
5490 return !tabular.getPWidth(cell).zero();
5494 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5497 if (buf.length() <= 0)
5502 col_type maxCols = 1;
5503 size_t const len = buf.length();
5507 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5515 maxCols = max(cols, maxCols);
5521 maxCols = max(cols, maxCols);
5527 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5528 loctab = paste_tabular.get();
5530 dirtyTabularStack(true);
5533 cell = bv.cursor().idx();
5534 ocol = tabular.cellColumn(cell);
5535 row = tabular.cellRow(cell);
5539 idx_type const cells = loctab->numberofcells;
5542 rows = loctab->row_info.size();
5543 col_type const columns = loctab->column_info.size();
5545 while (cell < cells && p < len && row < rows &&
5546 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5552 // we can only set this if we are not too far right
5553 if (cols < columns) {
5554 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5555 Font const font = bv.textMetrics(&inset->text()).
5557 inset->setText(buf.substr(op, p - op), font,
5558 buffer().params().trackChanges);
5564 // we can only set this if we are not too far right
5565 if (cols < columns) {
5566 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5567 Font const font = bv.textMetrics(&inset->text()).
5569 inset->setText(buf.substr(op, p - op), font,
5570 buffer().params().trackChanges);
5575 cell = loctab->cellIndex(row, cols);
5581 // check for the last cell if there is no trailing '\n'
5582 if (cell < cells && op < len) {
5583 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5584 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5585 inset->setText(buf.substr(op, len - op), font,
5586 buffer().params().trackChanges);
5592 void InsetTabular::addPreview(DocIterator const & inset_pos,
5593 PreviewLoader & loader) const
5595 row_type const rows = tabular.row_info.size();
5596 col_type const columns = tabular.column_info.size();
5597 DocIterator cell_pos = inset_pos;
5599 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5600 for (row_type i = 0; i < rows; ++i) {
5601 for (col_type j = 0; j < columns; ++j) {
5602 cell_pos.top().idx() = tabular.cellIndex(i, j);
5603 tabular.cellInset(i, j)->addPreview(cell_pos, loader);
5609 bool InsetTabular::completionSupported(Cursor const & cur) const
5611 Cursor const & bvCur = cur.bv().cursor();
5612 if (&bvCur.inset() != this)
5614 return cur.text()->completionSupported(cur);
5618 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5620 return completionSupported(cur);
5624 bool InsetTabular::automaticInlineCompletion() const
5626 return lyxrc.completion_inline_text;
5630 bool InsetTabular::automaticPopupCompletion() const
5632 return lyxrc.completion_popup_text;
5636 bool InsetTabular::showCompletionCursor() const
5638 return lyxrc.completion_cursor_text;
5642 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5644 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5648 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5650 if (!completionSupported(cur))
5652 return cur.text()->completionPrefix(cur);
5656 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5658 if (!completionSupported(cur))
5661 return cur.text()->insertCompletion(cur, s, finished);
5665 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5666 Dimension & dim) const
5668 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5669 tm.completionPosAndDim(cur, x, y, dim);
5673 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5675 istringstream data(in);
5677 lex.setStream(data);
5684 if (!lex || token != "tabular") {
5685 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5689 // This is part of the inset proper that is usually swallowed
5690 // by Buffer::readInset
5692 if (!lex || token != "Tabular") {
5693 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5701 string InsetTabular::params2string(InsetTabular const & inset)
5704 data << "tabular" << ' ';
5706 data << "\\end_inset\n";