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", false},
173 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
174 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
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 for (row_type i = 0; i < nrows(); ++i)
618 for (col_type j = 0; j < ncols(); ++j)
619 cell_info[i][j].inset->setBuffer(*buffer_);
623 // activates all lines and sets all widths to 0
624 void Tabular::init(Buffer * buf, row_type rows_arg,
625 col_type columns_arg)
628 row_info = row_vector(rows_arg);
629 column_info = column_vector(columns_arg);
630 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
631 row_info.reserve(10);
632 column_info.reserve(10);
633 cell_info.reserve(100);
635 is_long_tabular = false;
636 tabular_valignment = LYX_VALIGN_MIDDLE;
637 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
639 use_booktabs = false;
640 // set silly default lines
641 for (row_type r = 0; r < nrows(); ++r)
642 for (col_type c = 0; c < ncols(); ++c) {
643 cell_info[r][c].inset->setBuffer(*buffer_);
644 cell_info[r][c].top_line = true;
645 cell_info[r][c].left_line = true;
646 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
647 cell_info[r][c].right_line = c == ncols() - 1;
652 void Tabular::appendRow(idx_type const cell)
654 row_type const row = cellRow(cell);
656 row_info.insert(row_info.begin() + row + 1, RowData());
657 row_info[row + 1] = row_info[row];
659 cell_info.insert(cell_info.begin() + row + 1,
660 cell_vector(ncols(), CellData(buffer_)));
661 for (col_type c = 0; c < ncols(); ++c) {
662 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
663 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
665 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
669 for (col_type c = 0; c < ncols(); ++c) {
670 if (isPartOfMultiRow(row, c))
672 // inherit line settings
673 idx_type const i = cellIndex(row + 1, c);
674 idx_type const j = cellIndex(row, c);
675 setLeftLine(i, leftLine(j));
676 setRightLine(i, rightLine(j));
677 setTopLine(i, topLine(j));
678 if (topLine(j) && bottomLine(j)) {
679 setBottomLine(i, true);
680 setBottomLine(j, false);
682 // mark track changes
683 if (buffer().params().trackChanges)
684 cellInfo(i).inset->setChange(Change(Change::INSERTED));
689 void Tabular::deleteRow(row_type const row)
691 // Not allowed to delete last row
695 for (col_type c = 0; c < ncols(); ++c) {
696 // Care about multirow cells
697 if (row + 1 < nrows() &&
698 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
699 cell_info[row][c + 1].multirow == CELL_PART_OF_MULTIROW) {
700 cell_info[row][c + 1].multirow = CELL_BEGIN_OF_MULTIROW;
703 row_info.erase(row_info.begin() + row);
704 cell_info.erase(cell_info.begin() + row);
709 void Tabular::copyRow(row_type const row)
711 row_info.insert(row_info.begin() + row, row_info[row]);
712 cell_info.insert(cell_info.begin() + row, cell_info[row]);
714 if (buffer().params().trackChanges)
715 for (col_type c = 0; c < ncols(); ++c)
716 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
722 void Tabular::appendColumn(idx_type const cell)
724 col_type const c = cellColumn(cell);
726 column_info.insert(column_info.begin() + c + 1, ColumnData());
727 column_info[c + 1] = column_info[c];
729 for (row_type r = 0; r < nrows(); ++r) {
730 cell_info[r].insert(cell_info[r].begin() + c + 1,
732 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
733 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
735 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
738 for (row_type r = 0; r < nrows(); ++r) {
739 // inherit line settings
740 idx_type const i = cellIndex(r, c + 1);
741 idx_type const j = cellIndex(r, c);
742 setBottomLine(i, bottomLine(j));
743 setTopLine(i, topLine(j));
744 setLeftLine(i, leftLine(j));
745 if (rightLine(j) && rightLine(j)) {
746 setRightLine(i, true);
747 setRightLine(j, false);
749 if (buffer().params().trackChanges)
750 cellInfo(i).inset->setChange(Change(Change::INSERTED));
755 void Tabular::deleteColumn(col_type const col)
757 // Not allowed to delete last column
761 for (row_type r = 0; r < nrows(); ++r) {
762 // Care about multicolumn cells
763 if (col + 1 < ncols() &&
764 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
765 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
766 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
768 cell_info[r].erase(cell_info[r].begin() + col);
770 column_info.erase(column_info.begin() + col);
775 void Tabular::copyColumn(col_type const col)
777 BufferParams const & bp = buffer().params();
778 column_info.insert(column_info.begin() + col, column_info[col]);
780 for (row_type r = 0; r < nrows(); ++r) {
781 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
783 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
789 void Tabular::updateIndexes()
793 for (row_type row = 0; row < nrows(); ++row)
794 for (col_type column = 0; column < ncols(); ++column) {
795 if (!isPartOfMultiColumn(row, column)
796 && !isPartOfMultiRow(row, column))
798 if (isPartOfMultiRow(row, column))
799 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
801 cell_info[row][column].cellno = numberofcells - 1;
804 rowofcell.resize(numberofcells);
805 columnofcell.resize(numberofcells);
807 for (row_type row = 0; row < nrows(); ++row)
808 for (col_type column = 0; column < ncols(); ++column) {
809 if (isPartOfMultiColumn(row, column)
810 || isPartOfMultiRow(row, column))
813 columnofcell[i] = column;
814 setFixedWidth(row, column);
815 updateContentAlignment(row, column);
821 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
824 for (col_type c = 0; c < ncols(); ++c)
825 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
831 bool Tabular::topLine(idx_type const cell) const
833 return cellInfo(cell).top_line;
837 bool Tabular::bottomLine(idx_type const cell) const
839 return cellInfo(cell).bottom_line;
843 bool Tabular::leftLine(idx_type cell) const
847 return cellInfo(cell).left_line;
851 bool Tabular::rightLine(idx_type cell) const
855 return cellInfo(cell).right_line;
859 int Tabular::interRowSpace(row_type row) const
861 if (!row || row >= nrows())
864 int const interline_space = row_info[row - 1].interline_space_default ?
866 row_info[row - 1].interline_space.inPixels(width());
867 if (rowTopLine(row) && rowBottomLine(row - 1))
868 return interline_space + WIDTH_OF_LINE;
869 return interline_space;
873 int Tabular::interColumnSpace(idx_type cell) const
875 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
876 if (rightLine(cell) && nextcol < ncols()
877 && leftLine(cellIndex(cellRow(cell), nextcol)))
878 return WIDTH_OF_LINE;
883 int Tabular::columnWidth(idx_type cell) const
886 col_type const span = columnSpan(cell);
887 col_type const col = cellColumn(cell);
888 for(col_type c = col; c < col + span ; ++c)
889 w += column_info[c].width;
894 int Tabular::rowHeight(idx_type cell) const
896 row_type const span = rowSpan(cell);
897 row_type const row = cellRow(cell);
899 for(row_type r = row; r < row + span ; ++r) {
900 h += rowAscent(r) + rowDescent(r);
901 if (r != row + span - 1)
902 h += interRowSpace(r + 1);
909 bool Tabular::updateColumnWidths()
912 // for each col get max of single col cells
913 for(col_type c = 0; c < ncols(); ++c) {
915 for(row_type r = 0; r < nrows(); ++r) {
916 idx_type const i = cellIndex(r, c);
917 if (columnSpan(i) == 1)
918 new_width = max(new_width, cellInfo(i).width);
921 if (column_info[c].width != new_width) {
922 column_info[c].width = new_width;
926 // update col widths to fit merged cells
927 for(col_type c = 0; c < ncols(); ++c)
928 for(row_type r = 0; r < nrows(); ++r) {
929 idx_type const i = cellIndex(r, c);
930 int const span = columnSpan(i);
931 if (span == 1 || c > cellColumn(i))
935 for(col_type j = c; j < c + span ; ++j)
936 old_width += column_info[j].width;
938 if (cellInfo(i).width > old_width) {
939 column_info[c + span - 1].width += cellInfo(i).width - old_width;
948 int Tabular::width() const
951 for (col_type c = 0; c < ncols(); ++c)
952 width += column_info[c].width;
957 void Tabular::setCellWidth(idx_type cell, int new_width)
959 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
960 + interColumnSpace(cell);
964 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
967 if (!isMultiColumn(cell) || onlycolumn)
968 column_info[cellColumn(cell)].alignment = align;
970 cellInfo(cell).alignment = align;
971 cellInset(cell).get()->setContentAlignment(align);
975 void Tabular::setVAlignment(idx_type cell, VAlignment align,
978 if (!isMultiColumn(cell) || onlycolumn)
979 column_info[cellColumn(cell)].valignment = align;
981 cellInfo(cell).valignment = align;
988 * Allow line and paragraph breaks for fixed width cells or disallow them,
989 * merge cell paragraphs and reset layout to standard for variable width
992 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
994 inset->setAutoBreakRows(fixedWidth);
995 inset->toggleFixedWidth(fixedWidth);
999 // merge all paragraphs to one
1000 BufferParams const & bp = cur.bv().buffer().params();
1001 while (inset->paragraphs().size() > 1)
1002 mergeParagraph(bp, inset->paragraphs(), 0);
1006 // undo information has already been recorded
1007 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1008 bp.documentClass().plainLayoutName());
1015 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1016 Length const & width)
1018 col_type const c = cellColumn(cell);
1020 column_info[c].p_width = width;
1021 // reset the vertical alignment to top if the fixed with
1022 // is removed or zero because only fixed width columns can
1023 // have a vertical alignment
1024 if (column_info[c].p_width.zero())
1025 column_info[c].valignment = LYX_VALIGN_TOP;
1026 for (row_type r = 0; r < nrows(); ++r) {
1027 idx_type const cell = cellIndex(r, c);
1028 // because of multicolumns
1029 toggleFixedWidth(cur, cellInset(cell).get(),
1030 !getPWidth(cell).zero());
1032 // cur paragraph can become invalid after paragraphs were merged
1033 if (cur.pit() > cur.lastpit())
1034 cur.pit() = cur.lastpit();
1035 // cur position can become invalid after newlines were removed
1036 if (cur.pos() > cur.lastpos())
1037 cur.pos() = cur.lastpos();
1041 bool Tabular::setFixedWidth(row_type r, col_type c)
1043 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1044 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1045 || (multicol && !cell_info[r][c].p_width.zero());
1046 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1051 void Tabular::updateContentAlignment(row_type r, col_type c)
1053 cell_info[r][c].inset->setContentAlignment(
1054 getAlignment(cellIndex(r, c)));
1058 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1059 Length const & width)
1061 if (!isMultiColumn(cell))
1064 cellInfo(cell).p_width = width;
1065 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1066 // cur paragraph can become invalid after paragraphs were merged
1067 if (cur.pit() > cur.lastpit())
1068 cur.pit() = cur.lastpit();
1069 // cur position can become invalid after newlines were removed
1070 if (cur.pos() > cur.lastpos())
1071 cur.pos() = cur.lastpos();
1076 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1077 Tabular::Feature what)
1079 if (what == SET_SPECIAL_MULTICOLUMN)
1080 cellInfo(cell).align_special = special;
1082 column_info[cellColumn(cell)].align_special = special;
1086 void Tabular::setTopLine(idx_type i, bool line)
1088 cellInfo(i).top_line = line;
1092 void Tabular::setBottomLine(idx_type i, bool line)
1094 cellInfo(i).bottom_line = line;
1098 void Tabular::setLeftLine(idx_type cell, bool line)
1100 cellInfo(cell).left_line = line;
1104 void Tabular::setRightLine(idx_type cell, bool line)
1106 cellInfo(cell).right_line = line;
1109 bool Tabular::rowTopLine(row_type r) const
1111 bool all_rows_set = true;
1112 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1113 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1114 return all_rows_set;
1118 bool Tabular::rowBottomLine(row_type r) const
1120 bool all_rows_set = true;
1121 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1122 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1123 return all_rows_set;
1127 bool Tabular::columnLeftLine(col_type c) const
1134 for (row_type r = 0; r < nrows(); ++r) {
1135 idx_type const i = cellIndex(r, c);
1136 if (c == cellColumn(i)) {
1138 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1139 if (cellInfo(i).left_line || right)
1143 return 2 * nrows_left >= total;
1147 bool Tabular::columnRightLine(col_type c) const
1152 int nrows_right = 0;
1154 for (row_type r = 0; r < nrows(); ++r) {
1155 idx_type i = cellIndex(r, c);
1156 if (c == cellColumn(i) + columnSpan(i) - 1) {
1158 bool left = (c + 1 < ncols()
1159 && cellInfo(cellIndex(r, c + 1)).left_line)
1160 || c + 1 == ncols();
1161 if (cellInfo(i).right_line && left)
1165 return 2 * nrows_right >= total;
1169 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1171 if (!onlycolumn && isMultiColumn(cell))
1172 return cellInfo(cell).alignment;
1173 return column_info[cellColumn(cell)].alignment;
1178 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1180 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1181 return cellInfo(cell).valignment;
1182 return column_info[cellColumn(cell)].valignment;
1186 Length const Tabular::getPWidth(idx_type cell) const
1188 if (isMultiColumn(cell))
1189 return cellInfo(cell).p_width;
1190 return column_info[cellColumn(cell)].p_width;
1194 int Tabular::cellWidth(idx_type cell) const
1196 return cellInfo(cell).width;
1200 int Tabular::textHOffset(idx_type cell) const
1202 // the LaTeX Way :-(
1203 int x = WIDTH_OF_LINE;
1205 switch (getAlignment(cell)) {
1206 case LYX_ALIGN_CENTER:
1207 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1209 case LYX_ALIGN_RIGHT:
1210 x += columnWidth(cell) - cellWidth(cell);
1211 // + interColumnSpace(cell);
1214 // LYX_ALIGN_LEFT: nothing :-)
1222 int Tabular::textVOffset(idx_type cell) const
1224 int h = rowHeight(cell);
1226 row_type const r = cellRow(cell);
1227 if (rowSpan(cell) > 1)
1228 h -= rowDescent(r) + rowAscent(r);
1231 switch (getVAlignment(cell)) {
1232 case LYX_VALIGN_TOP:
1234 case LYX_VALIGN_MIDDLE:
1237 case LYX_VALIGN_BOTTOM:
1246 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1249 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1251 return cell_info[row][c].cellno;
1255 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1257 col_type c = ncols() - 1;
1258 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1259 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1261 return cell_info[row][c].cellno;
1265 Tabular::row_type Tabular::cellRow(idx_type cell) const
1267 if (cell >= numberofcells)
1271 return rowofcell[cell];
1275 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1277 if (cell >= numberofcells)
1281 return columnofcell[cell];
1285 void Tabular::write(ostream & os) const
1289 << write_attribute("version", 3)
1290 << write_attribute("rows", nrows())
1291 << write_attribute("columns", ncols())
1293 // global longtable options
1295 << write_attribute("rotate", rotate)
1296 << write_attribute("booktabs", use_booktabs)
1297 << write_attribute("islongtable", is_long_tabular)
1298 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1299 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1300 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1301 << write_attribute("headTopDL", endhead.topDL)
1302 << write_attribute("headBottomDL", endhead.bottomDL)
1303 << write_attribute("footTopDL", endfoot.topDL)
1304 << write_attribute("footBottomDL", endfoot.bottomDL)
1305 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1306 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1307 << write_attribute("lastFootEmpty", endlastfoot.empty);
1308 // longtables cannot be aligned vertically
1309 if (!is_long_tabular)
1310 os << write_attribute("tabularvalignment", tabular_valignment);
1311 if (is_long_tabular)
1312 os << write_attribute("longtabularalignment",
1313 longtabular_alignment);
1315 for (col_type c = 0; c < ncols(); ++c) {
1317 << write_attribute("alignment", column_info[c].alignment)
1318 << write_attribute("valignment", column_info[c].valignment)
1319 << write_attribute("width", column_info[c].p_width.asString())
1320 << write_attribute("special", column_info[c].align_special)
1323 for (row_type r = 0; r < nrows(); ++r) {
1324 static const string def("default");
1326 if (row_info[r].top_space_default)
1327 os << write_attribute("topspace", def);
1329 os << write_attribute("topspace", row_info[r].top_space);
1330 if (row_info[r].bottom_space_default)
1331 os << write_attribute("bottomspace", def);
1333 os << write_attribute("bottomspace", row_info[r].bottom_space);
1334 if (row_info[r].interline_space_default)
1335 os << write_attribute("interlinespace", def);
1337 os << write_attribute("interlinespace", row_info[r].interline_space);
1338 os << write_attribute("endhead", row_info[r].endhead)
1339 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1340 << write_attribute("endfoot", row_info[r].endfoot)
1341 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1342 << write_attribute("newpage", row_info[r].newpage)
1343 << write_attribute("caption", row_info[r].caption)
1345 for (col_type c = 0; c < ncols(); ++c) {
1347 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1348 << write_attribute("multirow", cell_info[r][c].multirow)
1349 << write_attribute("alignment", cell_info[r][c].alignment)
1350 << write_attribute("valignment", cell_info[r][c].valignment)
1351 << write_attribute("topline", cell_info[r][c].top_line)
1352 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1353 << write_attribute("leftline", cell_info[r][c].left_line)
1354 << write_attribute("rightline", cell_info[r][c].right_line)
1355 << write_attribute("rotate", cell_info[r][c].rotate)
1356 << write_attribute("usebox", cell_info[r][c].usebox)
1357 << write_attribute("width", cell_info[r][c].p_width)
1358 << write_attribute("special", cell_info[r][c].align_special)
1360 os << "\\begin_inset ";
1361 cell_info[r][c].inset->write(os);
1362 os << "\n\\end_inset\n"
1367 os << "</lyxtabular>\n";
1371 void Tabular::read(Lexer & lex)
1374 istream & is = lex.getStream();
1376 l_getline(is, line);
1377 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1378 LASSERT(false, /**/);
1383 if (!getTokenValue(line, "version", version))
1385 LASSERT(version >= 2, /**/);
1388 if (!getTokenValue(line, "rows", rows_arg))
1391 if (!getTokenValue(line, "columns", columns_arg))
1393 init(buffer_, rows_arg, columns_arg);
1394 l_getline(is, line);
1395 if (!prefixIs(line, "<features")) {
1396 lyxerr << "Wrong tabular format (expected <features ...> got"
1397 << line << ')' << endl;
1400 getTokenValue(line, "rotate", rotate);
1401 getTokenValue(line, "booktabs", use_booktabs);
1402 getTokenValue(line, "islongtable", is_long_tabular);
1403 getTokenValue(line, "tabularvalignment", tabular_valignment);
1404 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1405 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1406 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1407 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1408 getTokenValue(line, "headTopDL", endhead.topDL);
1409 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1410 getTokenValue(line, "footTopDL", endfoot.topDL);
1411 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1412 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1413 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1414 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1416 for (col_type c = 0; c < ncols(); ++c) {
1418 if (!prefixIs(line,"<column")) {
1419 lyxerr << "Wrong tabular format (expected <column ...> got"
1420 << line << ')' << endl;
1423 getTokenValue(line, "alignment", column_info[c].alignment);
1424 getTokenValue(line, "valignment", column_info[c].valignment);
1425 getTokenValue(line, "width", column_info[c].p_width);
1426 getTokenValue(line, "special", column_info[c].align_special);
1429 for (row_type i = 0; i < nrows(); ++i) {
1430 l_getline(is, line);
1431 if (!prefixIs(line, "<row")) {
1432 lyxerr << "Wrong tabular format (expected <row ...> got"
1433 << line << ')' << endl;
1436 getTokenValue(line, "topspace", row_info[i].top_space,
1437 row_info[i].top_space_default);
1438 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1439 row_info[i].bottom_space_default);
1440 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1441 row_info[i].interline_space_default);
1442 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1443 getTokenValue(line, "endhead", row_info[i].endhead);
1444 getTokenValue(line, "endfoot", row_info[i].endfoot);
1445 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1446 getTokenValue(line, "newpage", row_info[i].newpage);
1447 getTokenValue(line, "caption", row_info[i].caption);
1448 for (col_type j = 0; j < ncols(); ++j) {
1449 l_getline(is, line);
1450 if (!prefixIs(line, "<cell")) {
1451 lyxerr << "Wrong tabular format (expected <cell ...> got"
1452 << line << ')' << endl;
1455 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1456 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1457 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1458 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1459 getTokenValue(line, "topline", cell_info[i][j].top_line);
1460 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1461 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1462 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1463 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1464 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1465 getTokenValue(line, "width", cell_info[i][j].p_width);
1467 getTokenValue(line, "special", cell_info[i][j].align_special);
1468 l_getline(is, line);
1469 if (prefixIs(line, "\\begin_inset")) {
1470 cell_info[i][j].inset->setBuffer(*buffer_);
1471 cell_info[i][j].inset->read(lex);
1472 l_getline(is, line);
1474 if (!prefixIs(line, "</cell>")) {
1475 lyxerr << "Wrong tabular format (expected </cell> got"
1476 << line << ')' << endl;
1480 l_getline(is, line);
1481 if (!prefixIs(line, "</row>")) {
1482 lyxerr << "Wrong tabular format (expected </row> got"
1483 << line << ')' << endl;
1487 while (!prefixIs(line, "</lyxtabular>")) {
1488 l_getline(is, line);
1494 bool Tabular::isMultiColumn(idx_type cell) const
1496 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1497 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1501 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1503 return cell_info[cellRow(cell)][cellColumn(cell)];
1507 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1509 idx_type const col = cellColumn(cell);
1510 idx_type const row = cellRow(cell);
1511 for (idx_type i = 0; i < number; ++i)
1512 unsetMultiRow(cellIndex(row, col + i));
1514 // unsetting of multirow may have invalidated cell index
1515 cell = cellIndex(row, col);
1516 CellData & cs = cellInfo(cell);
1517 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1518 cs.alignment = column_info[col].alignment;
1520 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1522 for (idx_type i = 1; i < number; ++i) {
1523 CellData & cs1 = cellInfo(cell + i);
1524 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1525 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1533 bool Tabular::isMultiRow(idx_type cell) const
1535 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1536 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1540 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1542 idx_type const col = cellColumn(cell);
1543 idx_type const row = cellRow(cell);
1544 for (idx_type i = 0; i < number; ++i)
1545 unsetMultiColumn(cellIndex(row + i, col));
1547 // unsetting of multicol may have invalidated cell index
1548 cell = cellIndex(row, col);
1549 CellData & cs = cellInfo(cell);
1550 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1551 cs.valignment = LYX_VALIGN_MIDDLE;
1553 // set the bottom row of the last selected cell
1554 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1556 for (idx_type i = 1; i < number; ++i) {
1557 CellData & cs1 = cell_info[row + i][col];
1558 cs1.multirow = CELL_PART_OF_MULTIROW;
1559 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1567 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1569 row_type const row = cellRow(cell);
1570 col_type column = cellColumn(cell) + 1;
1571 while (column < ncols() && isPartOfMultiColumn(row, column))
1574 return column - cellColumn(cell);
1578 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1580 col_type const column = cellColumn(cell);
1581 col_type row = cellRow(cell) + 1;
1582 while (row < nrows() && isPartOfMultiRow(row, column))
1585 return row - cellRow(cell);
1589 void Tabular::unsetMultiColumn(idx_type cell)
1591 if (!isMultiColumn(cell))
1594 row_type const row = cellRow(cell);
1595 col_type const col = cellColumn(cell);
1596 row_type const span = columnSpan(cell);
1597 for (col_type c = 0; c < span; ++c)
1598 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1603 void Tabular::unsetMultiRow(idx_type cell)
1605 if (!isMultiRow(cell))
1608 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1609 row_type const row = cellRow(cell);
1610 col_type const col = cellColumn(cell);
1611 row_type const span = rowSpan(cell);
1612 for (row_type r = 0; r < span; ++r)
1613 cell_info[row + r][col].multirow = CELL_NORMAL;
1618 void Tabular::setRotateCell(idx_type cell, bool flag)
1620 cellInfo(cell).rotate = flag;
1624 bool Tabular::getRotateCell(idx_type cell) const
1626 return cellInfo(cell).rotate;
1630 bool Tabular::needRotating() const
1634 for (row_type r = 0; r < nrows(); ++r)
1635 for (col_type c = 0; c < ncols(); ++c)
1636 if (cell_info[r][c].rotate)
1642 bool Tabular::isLastCell(idx_type cell) const
1644 if (cell + 1 < numberofcells)
1650 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1652 if (cellRow(cell) == 0)
1655 col_type const col = cellColumn(cell);
1656 row_type r = cellRow(cell) - 1;
1657 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1660 return cell_info[r][col].cellno;
1664 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1666 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1667 if (nextrow < nrows())
1668 return cell_info[nextrow][cellColumn(cell)].cellno;
1673 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1675 LASSERT(column != npos && column < ncols()
1676 && row != npos && row < nrows(), /**/);
1677 return cell_info[row][column].cellno;
1681 void Tabular::setUsebox(idx_type cell, BoxType type)
1683 cellInfo(cell).usebox = type;
1687 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1688 // adds line breaks, see bug 4886.
1689 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1691 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1692 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1694 if (cellInfo(cell).usebox > 1)
1695 return cellInfo(cell).usebox;
1696 return useParbox(cell);
1701 // This are functions used for the longtable support
1703 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1709 row_info[row].endfirsthead = flag;
1713 row_info[row].endhead = flag;
1718 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1721 hd.set = haveLTHead();
1722 return row_info[row].endhead;
1726 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1729 hd.set = haveLTFirstHead();
1730 return row_info[row].endfirsthead;
1734 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1740 row_info[row].endlastfoot = flag;
1744 row_info[row].endfoot = flag;
1749 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1752 fd.set = haveLTFoot();
1753 return row_info[row].endfoot;
1757 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1760 fd.set = haveLTLastFoot();
1761 return row_info[row].endlastfoot;
1765 void Tabular::setLTNewPage(row_type row, bool what)
1767 row_info[row].newpage = what;
1771 bool Tabular::getLTNewPage(row_type row) const
1773 return row_info[row].newpage;
1777 bool Tabular::haveLTHead() const
1779 for (row_type i = 0; i < nrows(); ++i)
1780 if (row_info[i].endhead)
1786 bool Tabular::haveLTFirstHead() const
1788 if (endfirsthead.empty)
1790 for (row_type r = 0; r < nrows(); ++r)
1791 if (row_info[r].endfirsthead)
1797 bool Tabular::haveLTFoot() const
1799 for (row_type r = 0; r < nrows(); ++r)
1800 if (row_info[r].endfoot)
1806 bool Tabular::haveLTLastFoot() const
1808 if (endlastfoot.empty)
1810 for (row_type r = 0; r < nrows(); ++r)
1811 if (row_info[r].endlastfoot)
1817 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1819 idx_type i = getFirstCellInRow(row);
1821 setMultiColumn(i, numberOfCellsInRow(row));
1822 setTopLine(i, false);
1823 setBottomLine(i, false);
1824 setLeftLine(i, false);
1825 setRightLine(i, false);
1827 unsetMultiColumn(i);
1828 // When unsetting a caption row, also all existing
1829 // captions in this row must be dissolved.
1831 row_info[row].caption = what;
1836 bool Tabular::ltCaption(row_type row) const
1838 return row_info[row].caption;
1842 bool Tabular::haveLTCaption() const
1844 for (row_type r = 0; r < nrows(); ++r)
1845 if (row_info[r].caption)
1851 // end longtable support functions
1853 void Tabular::setRowAscent(row_type row, int height)
1855 if (row >= nrows() || row_info[row].ascent == height)
1857 row_info[row].ascent = height;
1861 void Tabular::setRowDescent(row_type row, int height)
1863 if (row >= nrows() || row_info[row].descent == height)
1865 row_info[row].descent = height;
1869 int Tabular::rowAscent(row_type row) const
1871 LASSERT(row < nrows(), /**/);
1872 return row_info[row].ascent;
1876 int Tabular::rowDescent(row_type row) const
1878 LASSERT(row < nrows(), /**/);
1879 return row_info[row].descent;
1883 int Tabular::height() const
1886 for (row_type row = 0; row < nrows(); ++row)
1887 height += rowAscent(row) + rowDescent(row) +
1893 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1895 LASSERT(row < nrows(), /**/);
1896 LASSERT(column < ncols(), /**/);
1897 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1901 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1903 LASSERT(row < nrows(), /**/);
1904 LASSERT(column < ncols(), /**/);
1905 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1909 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1911 // we only output complete row lines and the 1st row here, the rest
1912 // is done in Tabular::TeXBottomHLine(...)
1914 // get for each column the topline (if any)
1915 vector<bool> topline;
1917 for (col_type c = 0; c < ncols(); ++c) {
1918 topline.push_back(topLine(cellIndex(row, c)));
1919 // If cell is part of a multirow and not the first cell of the
1920 // multirow, no line must be drawn.
1922 if (isMultiRow(cellIndex(row, c))
1923 && isMultiRow(cellIndex(row - 1, c)))
1929 // do nothing if empty first row, or incomplete row line after
1930 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
1933 // only output complete row lines and the 1st row's clines
1934 if (nset == ncols()) {
1936 os << (row == 0 ? "\\toprule " : "\\midrule ");
1940 } else if (row == 0) {
1941 for (col_type c = 0; c < ncols(); ++c) {
1943 //babel makes the "-" character an active one, so we have to suppress this here
1944 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1945 if (lang == "slovak" || lang == "czech")
1946 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1947 "\\expandafter\\cline\\expandafter{\\expandafter")
1948 << c + 1 << "\\string-";
1950 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1951 // get to last column of line span
1952 while (c < ncols() && topline[c])
1963 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1965 // we output bottomlines of row r and the toplines of row r+1
1966 // if the latter do not span the whole tabular
1968 // get the bottomlines of row r, and toplines in next row
1969 bool lastrow = row == nrows() - 1;
1970 vector<bool> bottomline, topline;
1971 bool nextrowset = true;
1972 for (col_type c = 0; c < ncols(); ++c) {
1973 bottomline.push_back(bottomLine(cellIndex(row, c)));
1974 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1975 // If cell is part of a multirow and not the last or first cell of the
1976 // multirow, no line must be drawn.
1978 if (isMultiRow(cellIndex(row, c))
1979 && isMultiRow(cellIndex(row + 1, c))) {
1980 bottomline[c] = false;
1983 nextrowset &= topline[c];
1986 // combine this row's bottom lines and next row's toplines if necessary
1988 for (col_type c = 0; c < ncols(); ++c) {
1990 bottomline[c] = bottomline[c] || topline[c];
1995 // do nothing if empty, OR incomplete row line with a topline in next row
1996 if (nset == 0 || (nextrowset && nset != ncols()))
1999 if (nset == ncols()) {
2001 os << (lastrow ? "\\bottomrule" : "\\midrule");
2005 for (col_type c = 0; c < ncols(); ++c) {
2006 if (bottomline[c]) {
2007 //babel makes the "-" character an active one, so we have to suppress this here
2008 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2009 if (lang == "slovak" || lang == "czech")
2010 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
2011 "\\expandafter\\cline\\expandafter{\\expandafter")
2012 << c + 1 << "\\string-";
2014 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
2015 // get to last column of line span
2016 while (c < ncols() && bottomline[c])
2027 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2028 bool & ismulticol, bool & ismultirow) const
2031 row_type const r = cellRow(cell);
2032 if (is_long_tabular && row_info[r].caption)
2035 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2036 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2037 // figure out how to set the lines
2038 // we always set double lines to the right of the cell
2039 col_type const c = cellColumn(cell);
2040 col_type const nextcol = c + columnSpan(cell);
2041 bool colright = columnRightLine(c);
2042 bool colleft = columnLeftLine(c);
2043 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2044 bool nextcellleft = nextcol < ncols()
2045 && leftLine(cellIndex(r, nextcol));
2046 bool coldouble = colright && nextcolleft;
2047 bool celldouble = rightLine(cell) && nextcellleft;
2049 ismulticol = isMultiColumn(cell)
2050 || (c == 0 && colleft != leftLine(cell))
2051 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2052 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2053 || (coldouble != celldouble);
2055 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2056 if (c ==0 && leftLine(cell))
2058 if (!cellInfo(cell).align_special.empty()) {
2059 os << cellInfo(cell).align_special;
2061 if (!getPWidth(cell).zero()) {
2063 case LYX_ALIGN_LEFT:
2064 os << ">{\\raggedright}";
2066 case LYX_ALIGN_RIGHT:
2067 os << ">{\\raggedleft}";
2069 case LYX_ALIGN_CENTER:
2070 os << ">{\\centering}";
2076 case LYX_VALIGN_TOP:
2079 case LYX_VALIGN_MIDDLE:
2082 case LYX_VALIGN_BOTTOM:
2087 << from_ascii(getPWidth(cell).asLatexString())
2091 case LYX_ALIGN_LEFT:
2094 case LYX_ALIGN_RIGHT:
2101 } // end if else !getPWidth
2102 } // end if else !cellinfo_of_cell
2103 if (rightLine(cell) || nextcellleft)
2106 // add extra vertical line if we want a double one
2109 } // end if ismulticol
2111 // we only need code for the first multirow cell
2112 ismultirow = isMultiRow(cell);
2114 os << "\\multirow{" << rowSpan(cell) << "}{";
2115 if (!getPWidth(cell).zero())
2116 os << from_ascii(getPWidth(cell).asLatexString());
2118 // we need to set a default value
2119 // needs to be discussed
2122 } // end if ismultirow
2124 if (getRotateCell(cell)) {
2125 os << "\\begin{sideways}\n";
2128 if (getUsebox(cell) == BOX_PARBOX) {
2131 case LYX_VALIGN_TOP:
2134 case LYX_VALIGN_MIDDLE:
2137 case LYX_VALIGN_BOTTOM:
2141 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2143 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2144 os << "\\begin{minipage}[";
2146 case LYX_VALIGN_TOP:
2149 case LYX_VALIGN_MIDDLE:
2152 case LYX_VALIGN_BOTTOM:
2156 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2164 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2165 bool ismulticol, bool ismultirow) const
2168 row_type const r = cellRow(cell);
2169 if (is_long_tabular && row_info[r].caption)
2173 if (getUsebox(cell) == BOX_PARBOX)
2175 else if (getUsebox(cell) == BOX_MINIPAGE) {
2176 os << "%\n\\end{minipage}";
2179 if (getRotateCell(cell)) {
2180 os << "%\n\\end{sideways}";
2183 if (ismulticol || ismultirow) {
2190 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2191 OutputParams const & runparams) const
2193 if (!is_long_tabular)
2198 // the caption must be output before the headers
2199 if (haveLTCaption()) {
2200 for (row_type r = 0; r < nrows(); ++r) {
2201 if (row_info[r].caption) {
2202 ret += TeXRow(os, r, runparams);
2206 // output first header info
2207 // first header must be output before the header, otherwise the
2208 // correct caption placement becomes really weird
2209 if (haveLTFirstHead()) {
2210 if (endfirsthead.topDL) {
2214 for (row_type r = 0; r < nrows(); ++r) {
2215 if (row_info[r].endfirsthead) {
2216 ret += TeXRow(os, r, runparams);
2219 if (endfirsthead.bottomDL) {
2223 os << "\\endfirsthead\n";
2226 // output header info
2228 if (endfirsthead.empty && !haveLTFirstHead()) {
2229 os << "\\endfirsthead\n";
2232 if (endhead.topDL) {
2236 for (row_type r = 0; r < nrows(); ++r) {
2237 if (row_info[r].endhead) {
2238 ret += TeXRow(os, r, runparams);
2241 if (endhead.bottomDL) {
2245 os << "\\endhead\n";
2248 // output footer info
2250 if (endfoot.topDL) {
2254 for (row_type r = 0; r < nrows(); ++r) {
2255 if (row_info[r].endfoot) {
2256 ret += TeXRow(os, r, runparams);
2259 if (endfoot.bottomDL) {
2263 os << "\\endfoot\n";
2265 if (endlastfoot.empty && !haveLTLastFoot()) {
2266 os << "\\endlastfoot\n";
2270 // output lastfooter info
2271 if (haveLTLastFoot()) {
2272 if (endlastfoot.topDL) {
2276 for (row_type r = 0; r < nrows(); ++r) {
2277 if (row_info[r].endlastfoot) {
2278 ret += TeXRow(os, r, runparams);
2281 if (endlastfoot.bottomDL) {
2285 os << "\\endlastfoot\n";
2292 bool Tabular::isValidRow(row_type row) const
2294 if (!is_long_tabular)
2296 return !row_info[row].endhead && !row_info[row].endfirsthead
2297 && !row_info[row].endfoot && !row_info[row].endlastfoot
2298 && !row_info[row].caption;
2302 int Tabular::TeXRow(odocstream & os, row_type row,
2303 OutputParams const & runparams) const
2305 idx_type cell = cellIndex(row, 0);
2306 shared_ptr<InsetTableCell> inset = cellInset(cell);
2307 Paragraph const & par = inset->paragraphs().front();
2308 string const lang = par.getParLanguage(buffer().params())->lang();
2310 //output the top line
2311 int ret = TeXTopHLine(os, row, lang);
2313 if (row_info[row].top_space_default) {
2315 os << "\\addlinespace\n";
2317 os << "\\noalign{\\vskip\\doublerulesep}\n";
2319 } else if(!row_info[row].top_space.zero()) {
2321 os << "\\addlinespace["
2322 << from_ascii(row_info[row].top_space.asLatexString())
2325 os << "\\noalign{\\vskip"
2326 << from_ascii(row_info[row].top_space.asLatexString())
2331 bool ismulticol = false;
2332 bool ismultirow = false;
2333 for (col_type c = 0; c < ncols(); ++c) {
2334 if (isPartOfMultiRow(row, c))
2335 os << " & "; // we need to add a further column
2336 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2338 cell = cellIndex(row, c);
2339 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2340 shared_ptr<InsetTableCell> inset = cellInset(cell);
2342 Paragraph const & par = inset->paragraphs().front();
2343 bool rtl = par.isRTL(buffer().params())
2345 && getPWidth(cell).zero();
2349 par.getParLanguage(buffer().params())->lang();
2350 if (lang == "farsi")
2352 else if (lang == "arabic_arabi")
2354 // currently, remaning RTL languages are
2355 // arabic_arabtex and hebrew
2359 // pass to the OutputParams that we are in a cell and
2360 // which alignment we have set.
2361 // InsetNewline needs this context information.
2362 OutputParams newrp(runparams);
2363 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2364 ? OutputParams::PLAIN
2365 : OutputParams::ALIGNED;
2366 ret += inset->latex(os, newrp);
2367 runparams.encoding = newrp.encoding;
2371 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2372 if (cell != getLastCellInRow(row)) { // not last cell in row
2376 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2377 // if no first header and no empty first header is used,
2378 // the caption needs to be terminated by \endfirsthead
2380 os << "\\endfirsthead";
2382 os << "\\tabularnewline";
2383 if (row_info[row].bottom_space_default) {
2385 os << "\\addlinespace";
2387 os << "[\\doublerulesep]";
2388 } else if (!row_info[row].bottom_space.zero()) {
2390 os << "\\addlinespace";
2392 << from_ascii(row_info[row].bottom_space.asLatexString())
2398 //output the bottom line
2399 ret += TeXBottomHLine(os, row, lang);
2401 if (row_info[row].interline_space_default) {
2403 os << "\\addlinespace\n";
2405 os << "\\noalign{\\vskip\\doublerulesep}\n";
2407 } else if (!row_info[row].interline_space.zero()) {
2409 os << "\\addlinespace["
2410 << from_ascii(row_info[row].interline_space.asLatexString())
2413 os << "\\noalign{\\vskip"
2414 << from_ascii(row_info[row].interline_space.asLatexString())
2422 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2426 //+---------------------------------------------------------------------
2427 //+ first the opening preamble +
2428 //+---------------------------------------------------------------------
2431 os << "\\begin{sideways}\n";
2434 if (is_long_tabular) {
2435 os << "\\begin{longtable}";
2436 switch (longtabular_alignment) {
2437 case LYX_LONGTABULAR_ALIGN_LEFT:
2440 case LYX_LONGTABULAR_ALIGN_CENTER:
2442 case LYX_LONGTABULAR_ALIGN_RIGHT:
2447 os << "\\begin{tabular}";
2448 switch (tabular_valignment) {
2449 case LYX_VALIGN_TOP:
2452 case LYX_VALIGN_MIDDLE:
2454 case LYX_VALIGN_BOTTOM:
2462 for (col_type c = 0; c < ncols(); ++c) {
2463 if (columnLeftLine(c))
2465 if (!column_info[c].align_special.empty()) {
2466 os << column_info[c].align_special;
2468 if (!column_info[c].p_width.zero()) {
2469 switch (column_info[c].alignment) {
2470 case LYX_ALIGN_LEFT:
2471 os << ">{\\raggedright}";
2473 case LYX_ALIGN_RIGHT:
2474 os << ">{\\raggedleft}";
2476 case LYX_ALIGN_CENTER:
2477 os << ">{\\centering}";
2479 case LYX_ALIGN_NONE:
2480 case LYX_ALIGN_BLOCK:
2481 case LYX_ALIGN_LAYOUT:
2482 case LYX_ALIGN_SPECIAL:
2486 switch (column_info[c].valignment) {
2487 case LYX_VALIGN_TOP:
2490 case LYX_VALIGN_MIDDLE:
2493 case LYX_VALIGN_BOTTOM:
2498 << from_ascii(column_info[c].p_width.asLatexString())
2501 switch (column_info[c].alignment) {
2502 case LYX_ALIGN_LEFT:
2505 case LYX_ALIGN_RIGHT:
2512 } // end if else !column_info[i].p_width
2513 } // end if else !column_info[i].align_special
2514 if (columnRightLine(c))
2520 ret += TeXLongtableHeaderFooter(os, runparams);
2522 //+---------------------------------------------------------------------
2523 //+ the single row and columns (cells) +
2524 //+---------------------------------------------------------------------
2526 for (row_type r = 0; r < nrows(); ++r) {
2527 if (isValidRow(r)) {
2528 ret += TeXRow(os, r, runparams);
2529 if (is_long_tabular && row_info[r].newpage) {
2530 os << "\\newpage\n";
2536 //+---------------------------------------------------------------------
2537 //+ the closing of the tabular +
2538 //+---------------------------------------------------------------------
2540 if (is_long_tabular)
2541 os << "\\end{longtable}";
2543 os << "\\end{tabular}";
2545 os << "\n\\end{sideways}";
2553 int Tabular::docbookRow(odocstream & os, row_type row,
2554 OutputParams const & runparams) const
2557 idx_type cell = getFirstCellInRow(row);
2560 for (col_type c = 0; c < ncols(); ++c) {
2561 if (isPartOfMultiColumn(row, c))
2564 os << "<entry align=\"";
2565 switch (getAlignment(cell)) {
2566 case LYX_ALIGN_LEFT:
2569 case LYX_ALIGN_RIGHT:
2577 os << "\" valign=\"";
2578 switch (getVAlignment(cell)) {
2579 case LYX_VALIGN_TOP:
2582 case LYX_VALIGN_BOTTOM:
2585 case LYX_VALIGN_MIDDLE:
2590 if (isMultiColumn(cell)) {
2591 os << " namest=\"col" << c << "\" ";
2592 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2596 ret += cellInset(cell)->docbook(os, runparams);
2605 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2609 //+---------------------------------------------------------------------
2610 //+ first the opening preamble +
2611 //+---------------------------------------------------------------------
2613 os << "<tgroup cols=\"" << ncols()
2614 << "\" colsep=\"1\" rowsep=\"1\">\n";
2616 for (col_type c = 0; c < ncols(); ++c) {
2617 os << "<colspec colname=\"col" << c << "\" align=\"";
2618 switch (column_info[c].alignment) {
2619 case LYX_ALIGN_LEFT:
2622 case LYX_ALIGN_RIGHT:
2630 if (runparams.flavor == OutputParams::XML)
2636 //+---------------------------------------------------------------------
2637 //+ Long Tabular case +
2638 //+---------------------------------------------------------------------
2640 // output caption info
2641 if (haveLTCaption()) {
2642 os << "<caption>\n";
2644 for (row_type r = 0; r < nrows(); ++r) {
2645 if (row_info[r].caption) {
2646 ret += docbookRow(os, r, runparams);
2649 os << "</caption>\n";
2652 // output header info
2653 if (haveLTHead() || haveLTFirstHead()) {
2656 for (row_type r = 0; r < nrows(); ++r) {
2657 if (row_info[r].endhead || row_info[r].endfirsthead) {
2658 ret += docbookRow(os, r, runparams);
2664 // output footer info
2665 if (haveLTFoot() || haveLTLastFoot()) {
2668 for (row_type r = 0; r < nrows(); ++r) {
2669 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2670 ret += docbookRow(os, r, runparams);
2677 //+---------------------------------------------------------------------
2678 //+ the single row and columns (cells) +
2679 //+---------------------------------------------------------------------
2683 for (row_type r = 0; r < nrows(); ++r) {
2684 if (isValidRow(r)) {
2685 ret += docbookRow(os, r, runparams);
2690 //+---------------------------------------------------------------------
2691 //+ the closing of the tabular +
2692 //+---------------------------------------------------------------------
2701 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2702 OutputParams const & runparams) const
2705 idx_type cell = getFirstCellInRow(row);
2707 xs << html::StartTag("tr");
2708 for (col_type c = 0; c < ncols(); ++c) {
2709 if (isPartOfMultiColumn(row, c))
2714 switch (getAlignment(cell)) {
2715 case LYX_ALIGN_LEFT:
2718 case LYX_ALIGN_RIGHT:
2726 attr << " valign='";
2727 switch (getVAlignment(cell)) {
2728 case LYX_VALIGN_TOP:
2731 case LYX_VALIGN_BOTTOM:
2734 case LYX_VALIGN_MIDDLE:
2739 if (isMultiColumn(cell))
2740 attr << " colspan='" << columnSpan(cell) << "'";
2742 xs << html::StartTag("td", attr.str());
2743 ret += cellInset(cell)->xhtml(xs, runparams);
2744 xs << html::EndTag("td");
2747 xs << html::EndTag("tr");
2752 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2755 // It's unclear to me if we need to mess with the long table stuff.
2756 // We can borrow that too from docbook, if so.
2758 xs << html::StartTag("tbody");
2759 for (row_type r = 0; r < nrows(); ++r) {
2760 if (isValidRow(r)) {
2761 ret += xhtmlRow(xs, r, runparams);
2764 xs << html::EndTag("tbody");
2769 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2770 vector<unsigned int> const & clen) const
2772 idx_type const fcell = getFirstCellInRow(row);
2773 idx_type const n = numberOfCellsInRow(row) + fcell;
2776 for (idx_type i = fcell; i < n; ++i) {
2786 for (idx_type i = fcell; i < n; ++i) {
2797 col_type column = cellColumn(i);
2798 int len = clen[column];
2799 while (column < ncols() - 1
2800 && isPartOfMultiColumn(row, ++column))
2801 len += clen[column] + 4;
2802 os << docstring(len, ch);
2817 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2818 vector<unsigned int> const & clen) const
2820 idx_type const fcell = getFirstCellInRow(row);
2821 idx_type const n = numberOfCellsInRow(row) + fcell;
2824 for (idx_type i = fcell; i < n; ++i) {
2825 if (bottomLine(i)) {
2834 for (idx_type i = fcell; i < n; ++i) {
2835 if (bottomLine(i)) {
2845 col_type column = cellColumn(i);
2846 int len = clen[column];
2847 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2848 len += clen[column] + 4;
2849 os << docstring(len, ch);
2850 if (bottomLine(i)) {
2864 void Tabular::plaintextPrintCell(odocstream & os,
2865 OutputParams const & runparams,
2866 idx_type cell, row_type row, col_type column,
2867 vector<unsigned int> const & clen,
2868 bool onlydata) const
2870 odocstringstream sstr;
2871 cellInset(cell)->plaintext(sstr, runparams);
2883 unsigned int len1 = sstr.str().length();
2884 unsigned int len2 = clen[column];
2885 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2886 len2 += clen[column] + 4;
2889 switch (getAlignment(cell)) {
2891 case LYX_ALIGN_LEFT:
2894 case LYX_ALIGN_RIGHT:
2898 case LYX_ALIGN_CENTER:
2904 os << docstring(len1, ' ') << sstr.str()
2905 << docstring(len2, ' ');
2907 if (rightLine(cell))
2914 void Tabular::plaintext(odocstream & os,
2915 OutputParams const & runparams, int const depth,
2916 bool onlydata, char_type delim) const
2918 // first calculate the width of the single columns
2919 vector<unsigned int> clen(ncols());
2922 // first all non multicolumn cells!
2923 for (col_type c = 0; c < ncols(); ++c) {
2925 for (row_type r = 0; r < nrows(); ++r) {
2926 idx_type cell = cellIndex(r, c);
2927 if (isMultiColumn(cell))
2929 odocstringstream sstr;
2930 cellInset(cell)->plaintext(sstr, runparams);
2931 if (clen[c] < sstr.str().length())
2932 clen[c] = sstr.str().length();
2935 // then all multicolumn cells!
2936 for (col_type c = 0; c < ncols(); ++c) {
2937 for (row_type r = 0; r < nrows(); ++r) {
2938 idx_type cell = cellIndex(r, c);
2939 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
2941 odocstringstream sstr;
2942 cellInset(cell)->plaintext(sstr, runparams);
2943 int len = int(sstr.str().length());
2944 idx_type const n = columnSpan(cell);
2945 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
2947 if (len > int(clen[c + n - 1]))
2948 clen[c + n - 1] = len;
2953 for (row_type r = 0; r < nrows(); ++r) {
2954 if (!onlydata && plaintextTopHLine(os, r, clen))
2955 os << docstring(depth * 2, ' ');
2956 for (col_type c = 0; c < ncols(); ++c) {
2957 if (isPartOfMultiColumn(r, c))
2959 if (onlydata && c > 0)
2960 // we don't use operator<< for single UCS4 character.
2961 // see explanation in docstream.h
2963 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
2968 os << docstring(depth * 2, ' ');
2969 if (plaintextBottomHLine(os, r, clen))
2970 os << docstring(depth * 2, ' ');
2976 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2978 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2982 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2983 col_type column) const
2985 return cell_info[row][column].inset;
2989 void Tabular::setCellInset(row_type row, col_type column,
2990 shared_ptr<InsetTableCell> ins) const
2992 CellData & cd = cell_info[row][column];
2997 void Tabular::validate(LaTeXFeatures & features) const
2999 features.require("NeedTabularnewline");
3001 features.require("booktabs");
3002 if (is_long_tabular)
3003 features.require("longtable");
3005 features.require("rotating");
3006 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3007 if (isMultiRow(cell))
3008 features.require("multirow");
3009 if (getVAlignment(cell) != LYX_VALIGN_TOP
3010 || !getPWidth(cell).zero())
3011 features.require("array");
3012 cellInset(cell)->validate(features);
3017 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3019 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3020 ParagraphList::const_iterator cit = parlist.begin();
3021 ParagraphList::const_iterator end = parlist.end();
3023 for (; cit != end; ++cit)
3024 for (int i = 0; i < cit->size(); ++i)
3025 if (cit->isNewline(i))
3032 /////////////////////////////////////////////////////////////////////
3036 /////////////////////////////////////////////////////////////////////
3038 InsetTableCell::InsetTableCell(Buffer * buf)
3039 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3040 contentAlign(LYX_ALIGN_CENTER)
3044 bool InsetTableCell::forcePlainLayout(idx_type) const
3046 return !isFixedWidth;
3050 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3052 return isFixedWidth;
3056 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3057 FuncStatus & status) const
3060 switch (cmd.action) {
3062 enabled = !forcePlainLayout();
3064 case LFUN_LAYOUT_PARAGRAPH:
3065 enabled = allowParagraphCustomization();
3068 return InsetText::getStatus(cur, cmd, status);
3070 status.setEnabled(enabled);
3074 docstring InsetTableCell::asString(bool intoInsets)
3077 if (paragraphs().empty())
3079 ParagraphList::const_iterator it = paragraphs().begin();
3080 ParagraphList::const_iterator en = paragraphs().end();
3082 for (; it != en; ++it) {
3087 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3093 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3096 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3097 return InsetText::xhtml(xs, rp);
3102 /////////////////////////////////////////////////////////////////////
3106 /////////////////////////////////////////////////////////////////////
3108 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3110 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3111 rowselect_(false), colselect_(false)
3116 InsetTabular::InsetTabular(InsetTabular const & tab)
3117 : Inset(tab), tabular(tab.tabular), scx_(0)
3122 InsetTabular::~InsetTabular()
3124 hideDialogs("tabular", this);
3128 void InsetTabular::setBuffer(Buffer & buf)
3130 tabular.setBuffer(buf);
3131 Inset::setBuffer(buf);
3135 bool InsetTabular::insetAllowed(InsetCode code) const
3137 if (code == MATHMACRO_CODE
3138 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3145 void InsetTabular::write(ostream & os) const
3147 os << "Tabular" << endl;
3152 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3154 // FIXME: depending on the selection state, we could offer a different menu.
3155 return from_ascii("context-tabular");
3159 void InsetTabular::read(Lexer & lex)
3161 //bool const old_format = (lex.getString() == "\\LyXTable");
3169 string token = lex.getString();
3170 while (lex && token != "\\end_inset") {
3172 token = lex.getString();
3175 lex.printError("Missing \\end_inset at this point. ");
3179 int InsetTabular::rowFromY(Cursor & cur, int y) const
3181 // top y coordinate of tabular
3182 int h = yo(cur.bv()) - tabular.rowAscent(0);
3184 for (; r < tabular.nrows() && y > h; ++r)
3185 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3186 + tabular.interRowSpace(r);
3192 int InsetTabular::columnFromX(Cursor & cur, int x) const
3194 // left x coordinate of tabular
3195 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3197 for (; c < tabular.ncols() && x > w; ++c)
3198 w += tabular.columnWidth(c);
3203 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3205 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3206 // mi.base.textwidth << "\n";
3209 LASSERT(false, /**/);
3212 for (row_type r = 0; r < tabular.nrows(); ++r) {
3215 for (col_type c = 0; c < tabular.ncols(); ++c) {
3216 if (tabular.isPartOfMultiColumn(r, c)
3217 || tabular.isPartOfMultiRow(r, c))
3218 // multicolumn or multirow cell, but not first one
3220 idx_type const cell = tabular.cellIndex(r, c);
3224 if (tabular.cell_info[r][c].multicolumn ==
3225 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3226 p_width = tabular.cellInfo(cell).p_width;
3228 p_width = tabular.column_info[c].p_width;
3229 if (!p_width.zero())
3230 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3231 tabular.cellInset(cell)->metrics(m, dim);
3232 if (!p_width.zero())
3233 dim.wid = m.base.textwidth;
3234 tabular.setCellWidth(cell, dim.wid);
3235 maxAsc = max(maxAsc, dim.asc);
3236 maxDesc = max(maxDesc, dim.des);
3238 int const top_space = tabular.row_info[r].top_space_default ?
3239 default_line_space :
3240 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3241 tabular.setRowAscent(r, maxAsc + ADD_TO_HEIGHT + top_space);
3242 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3243 default_line_space :
3244 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3245 tabular.setRowDescent(r, maxDesc + ADD_TO_HEIGHT + bottom_space);
3247 tabular.updateColumnWidths();
3248 dim.asc = tabular.rowAscent(0);
3249 dim.des = tabular.height() - dim.asc;
3250 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3253 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3256 if (&cur.inset() == this && cur.selection()) {
3257 if (cur.selIsMultiCell()) {
3260 getSelection(cur, rs, re, cs, ce);
3262 idx_type const cell = tabular.cellIndex(row, col);
3263 col_type const cspan = tabular.columnSpan(cell);
3264 row_type const rspan = tabular.rowSpan(cell);
3265 if (col + cspan - 1 >= cs && col <= ce
3266 && row + rspan - 1 >= rs && row <= re)
3269 if (col == tabular.cellColumn(cur.idx())
3270 && row == tabular.cellRow(cur.idx())) {
3271 CursorSlice const & beg = cur.selBegin();
3272 CursorSlice const & end = cur.selEnd();
3274 if ((end.lastpos() > 0 || end.lastpit() > 0)
3275 && end.pos() == end.lastpos() && beg.pos() == 0
3276 && end.pit() == end.lastpit() && beg.pit() == 0)
3284 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3286 x += scx_ + ADD_TO_TABULAR_WIDTH;
3288 BufferView * bv = pi.base.bv;
3289 Cursor & cur = pi.base.bv->cursor();
3292 // FIXME: As the full background is painted in drawSelection(),
3293 // we have no choice but to do a full repaint for the Text cells.
3294 pi.full_repaint = true;
3296 bool const original_selection_state = pi.selected;
3299 first_visible_cell = Tabular::npos;
3300 for (row_type r = 0; r < tabular.nrows(); ++r) {
3302 for (col_type c = 0; c < tabular.ncols(); ++c) {
3303 if (tabular.isPartOfMultiColumn(r, c))
3306 idx = tabular.cellIndex(r, c);
3308 if (tabular.isPartOfMultiRow(r, c)) {
3309 nx += tabular.columnWidth(idx);
3313 if (first_visible_cell == Tabular::npos)
3314 first_visible_cell = idx;
3316 pi.selected |= isCellSelected(cur, r, c);
3317 int const cx = nx + tabular.textHOffset(idx);
3318 int const cy = y + tabular.textVOffset(idx);
3319 // Cache the Inset position.
3320 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3321 cell(idx)->draw(pi, cx, cy);
3322 drawCellLines(pi.pain, nx, y, r, idx, pi.change_);
3323 nx += tabular.columnWidth(idx);
3324 pi.selected = original_selection_state;
3327 if (r + 1 < tabular.nrows())
3328 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3329 + tabular.interRowSpace(r + 1);
3334 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3336 Cursor & cur = pi.base.bv->cursor();
3339 x += scx_ + ADD_TO_TABULAR_WIDTH;
3341 // FIXME: it is wrong to completely paint the background
3342 // if we want to do single row painting.
3344 // Paint background of current tabular
3345 int const w = tabular.width();
3346 int const h = tabular.height();
3347 int yy = y - tabular.rowAscent(0);
3348 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3350 if (!cur.selection())
3352 if (&cur.inset() != this)
3357 bool const full_cell_selected = isCellSelected(cur,
3358 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3360 if (cur.selIsMultiCell() || full_cell_selected) {
3361 for (row_type r = 0; r < tabular.nrows(); ++r) {
3363 for (col_type c = 0; c < tabular.ncols(); ++c) {
3364 if (tabular.isPartOfMultiColumn(r, c))
3367 idx_type const cell = tabular.cellIndex(r, c);
3369 if (tabular.isPartOfMultiRow(r, c)) {
3370 xx += tabular.columnWidth(cell);
3373 int const w = tabular.columnWidth(cell);
3374 int const h = tabular.rowHeight(cell);
3375 int const yy = y - tabular.rowAscent(r);
3376 if (isCellSelected(cur, r, c))
3377 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3380 if (r + 1 < tabular.nrows())
3381 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3382 + tabular.interRowSpace(r + 1);
3386 x += cellXPos(cur.idx());
3387 x += tabular.textHOffset(cur.idx());
3388 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3393 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3394 row_type row, idx_type cell, Change const & change) const
3396 y = y - tabular.rowAscent(row);
3397 int const w = tabular.columnWidth(cell);
3398 int const h = tabular.rowHeight(cell);
3399 bool on_off = false;
3400 Color col = Color_tabularline;
3401 Color onoffcol = Color_tabularonoffline;
3403 if (change.changed()) {
3404 col = change.color();
3405 onoffcol = change.color();
3408 bool topalreadydrawn = row > 0 && !tabular.rowTopLine(row)
3409 && tabular.bottomLine(tabular.cellAbove(cell));
3411 if (!topalreadydrawn) {
3412 on_off = !tabular.topLine(cell);
3413 pain.line(x, y, x + w, y,
3414 on_off ? onoffcol : col,
3415 on_off ? Painter::line_onoffdash : Painter::line_solid);
3417 on_off = !tabular.bottomLine(cell);
3418 pain.line(x, y + h, x + w, y + h,
3419 on_off ? onoffcol : col,
3420 on_off ? Painter::line_onoffdash : Painter::line_solid);
3422 col_type const column = tabular.cellColumn(cell);
3423 bool leftalreadydrawn = column > 0 && !tabular.leftLine(cell)
3424 && tabular.rightLine(tabular.cellIndex(row, column - 1));
3426 if (!leftalreadydrawn) {
3427 on_off = !tabular.leftLine(cell);
3428 pain.line(x, y, x, y + h,
3429 on_off ? onoffcol : col,
3430 on_off ? Painter::line_onoffdash : Painter::line_solid);
3432 on_off = !tabular.rightLine(cell);
3433 pain.line(x + w - tabular.interColumnSpace(cell), y,
3434 x + w - tabular.interColumnSpace(cell), y + h,
3435 on_off ? onoffcol : col,
3436 on_off ? Painter::line_onoffdash : Painter::line_solid);
3440 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3442 //lyxerr << "InsetTabular::edit: " << this << endl;
3444 cur.setSelection(false);
3447 if (isRightToLeft(cur))
3448 cur.idx() = tabular.getLastCellInRow(0);
3454 if (isRightToLeft(cur))
3455 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3457 cur.idx() = tabular.numberofcells - 1;
3459 cur.pos() = cur.lastpos(); // FIXME crude guess
3461 cur.setCurrentFont();
3462 // FIXME: this accesses the position cache before it is initialized
3464 //cur.bv().fitCursor();
3468 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3470 // In a longtable, tell captions what the current float is
3471 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3472 string const saveflt = cnts.current_float();
3473 if (tabular.is_long_tabular)
3474 cnts.current_float("table");
3476 ParIterator it2 = it;
3478 size_t const end = it2.nargs();
3479 for ( ; it2.idx() < end; it2.top().forwardIdx())
3480 buffer().updateBuffer(it2, utype);
3483 if (tabular.is_long_tabular)
3484 cnts.current_float(saveflt);
3488 void InsetTabular::addToToc(DocIterator const & cpit)
3490 DocIterator dit = cpit;
3492 size_t const end = dit.nargs();
3493 for ( ; dit.idx() < end; dit.top().forwardIdx())
3494 cell(dit.idx())->addToToc(dit);
3498 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3500 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3501 << "\n cur:" << cur);
3502 CursorSlice sl = cur.top();
3503 Cursor & bvcur = cur.bv().cursor();
3505 switch (cmd.action) {
3507 case LFUN_MOUSE_PRESS: {
3508 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3510 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3511 || cmd.x > xo(cur.bv()) + tabular.width()) {
3512 row_type r = rowFromY(cur, cmd.y);
3513 cur.idx() = tabular.getFirstCellInRow(r);
3516 cur.idx() = tabular.getLastCellInRow(r);
3517 cur.pos() = cur.lastpos();
3518 cur.setSelection(true);
3524 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3525 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3526 || cmd.y > y0 + tabular.height()) {
3527 col_type c = columnFromX(cur, cmd.x);
3528 cur.idx() = tabular.cellIndex(0, c);
3531 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3532 cur.pos() = cur.lastpos();
3533 cur.setSelection(true);
3538 // do not reset cursor/selection if we have selected
3539 // some cells (bug 2715).
3540 if (cmd.button() == mouse_button::button3
3541 && &bvcur.selBegin().inset() == this
3542 && bvcur.selIsMultiCell())
3545 // Let InsetTableCell do it
3546 cell(cur.idx())->dispatch(cur, cmd);
3549 case LFUN_MOUSE_MOTION:
3550 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3551 if (cmd.button() == mouse_button::button1) {
3552 // only accept motions to places not deeper nested than the real anchor
3553 if (!bvcur.anchor_.hasPart(cur)) {
3557 // select (additional) row
3559 row_type r = rowFromY(cur, cmd.y);
3560 cur.idx() = tabular.getLastCellInRow(r);
3561 // we need to reset the cursor's pit and pos now, as the old ones
3562 // may no longer be valid.
3565 bvcur.setCursor(cur);
3566 bvcur.setSelection(true);
3569 // select (additional) column
3571 col_type c = columnFromX(cur, cmd.x);
3572 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3573 // we need to reset the cursor's pit and pos now, as the old ones
3574 // may no longer be valid.
3577 bvcur.setCursor(cur);
3578 bvcur.setSelection(true);
3581 // only update if selection changes
3582 if (bvcur.idx() == cur.idx() &&
3583 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3585 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3586 bvcur.setCursor(cur);
3587 bvcur.setSelection(true);
3588 // if this is a multicell selection, we just set the cursor to
3589 // the beginning of the cell's text.
3590 if (bvcur.selIsMultiCell()) {
3591 bvcur.pit() = bvcur.lastpit();
3592 bvcur.pos() = bvcur.lastpos();
3597 case LFUN_MOUSE_RELEASE:
3602 case LFUN_CELL_BACKWARD:
3604 cur.setSelection(false);
3607 case LFUN_CELL_FORWARD:
3609 cur.setSelection(false);
3612 case LFUN_CHAR_FORWARD_SELECT:
3613 case LFUN_CHAR_FORWARD:
3614 case LFUN_CHAR_BACKWARD_SELECT:
3615 case LFUN_CHAR_BACKWARD:
3616 case LFUN_CHAR_RIGHT_SELECT:
3617 case LFUN_CHAR_RIGHT:
3618 case LFUN_CHAR_LEFT_SELECT:
3619 case LFUN_CHAR_LEFT: {
3620 // determine whether we move to next or previous cell, where to enter
3621 // the new cell from, and which command to "finish" (i.e., exit the
3624 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3625 FuncCode finish_lfun;
3627 if (cmd.action == LFUN_CHAR_FORWARD
3628 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3630 finish_lfun = LFUN_FINISHED_FORWARD;
3632 else if (cmd.action == LFUN_CHAR_BACKWARD
3633 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3635 finish_lfun = LFUN_FINISHED_BACKWARD;
3637 // LEFT or RIGHT commands --- the interpretation will depend on the
3638 // table's direction.
3640 bool const right = cmd.action == LFUN_CHAR_RIGHT
3641 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3642 next_cell = isRightToLeft(cur) != right;
3644 if (lyxrc.visual_cursor)
3645 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3647 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3650 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3651 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3652 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3653 cmd.action == LFUN_CHAR_LEFT_SELECT;
3655 // If we have a multicell selection or we're
3656 // not doing some LFUN_*_SELECT thing anyway...
3657 if (!cur.selIsMultiCell() || !select) {
3658 col_type const c = tabular.cellColumn(cur.idx());
3659 row_type const r = tabular.cellRow(cur.idx());
3660 // Are we trying to select the whole cell and is the whole cell
3661 // not yet selected?
3662 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3663 ((next_cell && cur.pit() == cur.lastpit()
3664 && cur.pos() == cur.lastpos())
3665 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3667 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3669 // ...try to dispatch to the cell's inset.
3670 cell(cur.idx())->dispatch(cur, cmd);
3672 // When we already have a selection we want to select the whole cell
3673 // before going to the next cell.
3674 if (select_whole && !empty_cell){
3675 getText(cur.idx())->selectAll(cur);
3680 // FIXME: When we support the selection of an empty cell, remove
3681 // the !empty_cell from this condition. For now we jump to the next
3682 // cell if the current cell is empty.
3683 if (cur.result().dispatched() && !empty_cell)
3687 // move to next/prev cell, as appropriate
3688 // note that we will always do this if we're selecting and we have
3689 // a multicell selection
3690 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3691 << " cell from: " << int(entry_from));
3693 moveNextCell(cur, entry_from);
3695 movePrevCell(cur, entry_from);
3696 // if we're exiting the table, call the appropriate FINISHED lfun
3697 if (sl == cur.top())
3698 cmd = FuncRequest(finish_lfun);
3705 case LFUN_DOWN_SELECT:
3707 if (!(cur.selection() && cur.selIsMultiCell()))
3708 cell(cur.idx())->dispatch(cur, cmd);
3710 cur.dispatched(); // override the cell's decision
3711 if (sl == cur.top()) {
3712 // if our Text didn't do anything to the cursor
3713 // then we try to put the cursor into the cell below
3714 // setting also the right targetX.
3715 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3716 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3717 cur.idx() = tabular.cellBelow(cur.idx());
3719 TextMetrics const & tm =
3720 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3721 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3722 cur.setCurrentFont();
3725 if (sl == cur.top()) {
3726 // we trick it to go to forward after leaving the
3728 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3731 if (cur.selIsMultiCell()) {
3732 cur.pit() = cur.lastpit();
3733 cur.pos() = cur.lastpos();
3734 cur.setCurrentFont();
3739 case LFUN_UP_SELECT:
3741 if (!(cur.selection() && cur.selIsMultiCell()))
3742 cell(cur.idx())->dispatch(cur, cmd);
3743 cur.dispatched(); // override the cell's decision
3744 if (sl == cur.top()) {
3745 // if our Text didn't do anything to the cursor
3746 // then we try to put the cursor into the cell above
3747 // setting also the right targetX.
3748 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3749 if (tabular.cellRow(cur.idx()) != 0) {
3750 cur.idx() = tabular.cellAbove(cur.idx());
3751 cur.pit() = cur.lastpit();
3752 Text const * text = cell(cur.idx())->getText(0);
3753 TextMetrics const & tm = cur.bv().textMetrics(text);
3754 ParagraphMetrics const & pm =
3755 tm.parMetrics(cur.lastpit());
3756 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3757 cur.setCurrentFont();
3760 if (sl == cur.top()) {
3761 cmd = FuncRequest(LFUN_UP);
3764 if (cur.selIsMultiCell()) {
3766 cur.pos() = cur.lastpos();
3767 cur.setCurrentFont();
3772 // case LFUN_SCREEN_DOWN: {
3773 // //if (hasSelection())
3774 // // cur.selection() = false;
3775 // col_type const col = tabular.cellColumn(cur.idx());
3776 // int const t = cur.bv().top_y() + cur.bv().height();
3777 // if (t < yo() + tabular.getHeightOfTabular()) {
3778 // cur.bv().scrollDocView(t);
3779 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3781 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3788 // case LFUN_SCREEN_UP: {
3789 // //if (hasSelection())
3790 // // cur.selection() = false;
3791 // col_type const col = tabular.cellColumn(cur.idx());
3792 // int const t = cur.bv().top_y() + cur.bv().height();
3794 // cur.bv().scrollDocView(t);
3798 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3802 // cur.par() = cur.lastpar();
3803 // cur.pos() = cur.lastpos();
3807 case LFUN_LAYOUT_TABULAR:
3808 cur.bv().showDialog("tabular");
3811 case LFUN_INSET_MODIFY:
3812 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3816 // insert file functions
3817 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3818 case LFUN_FILE_INSERT_PLAINTEXT:
3820 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3821 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3822 FileName(to_utf8(cmd.argument())));
3825 cur.recordUndoInset(INSERT_UNDO);
3826 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3827 // content has been replaced,
3828 // so cursor might be invalid
3829 cur.pos() = cur.lastpos();
3830 cur.pit() = cur.lastpit();
3831 bvcur.setCursor(cur);
3838 if (cur.selIsMultiCell()) {
3839 if (copySelection(cur)) {
3840 cur.recordUndoInset(DELETE_UNDO);
3844 cell(cur.idx())->dispatch(cur, cmd);
3847 case LFUN_SELF_INSERT:
3848 if (cur.selIsMultiCell()) {
3849 cur.recordUndoInset(DELETE_UNDO);
3852 cell(cur.idx())->dispatch(cur, cmd);
3855 case LFUN_CHAR_DELETE_BACKWARD:
3856 case LFUN_CHAR_DELETE_FORWARD:
3857 if (cur.selIsMultiCell()) {
3858 cur.recordUndoInset(DELETE_UNDO);
3861 cell(cur.idx())->dispatch(cur, cmd);
3865 if (!cur.selection())
3867 if (cur.selIsMultiCell()) {
3871 cell(cur.idx())->dispatch(cur, cmd);
3874 case LFUN_CLIPBOARD_PASTE:
3875 case LFUN_PRIMARY_SELECTION_PASTE: {
3876 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3877 theClipboard().getAsText() :
3878 theSelection().get();
3881 // pass to InsertPlaintextString, but
3882 // only if we have multi-cell content
3883 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3884 cur.recordUndoInset(INSERT_UNDO);
3885 if (insertPlaintextString(cur.bv(), clip, false)) {
3886 // content has been replaced,
3887 // so cursor might be invalid
3888 cur.pos() = cur.lastpos();
3889 cur.pit() = cur.lastpit();
3890 bvcur.setCursor(cur);
3894 // Let the cell handle normal text
3895 cell(cur.idx())->dispatch(cur, cmd);
3900 if (!tabularStackDirty()) {
3901 if (!cur.selIsMultiCell())
3902 cell(cur.idx())->dispatch(cur, cmd);
3905 if (theClipboard().isInternal() ||
3906 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3907 cur.recordUndoInset(INSERT_UNDO);
3908 pasteClipboard(cur);
3912 case LFUN_FONT_EMPH:
3913 case LFUN_FONT_BOLD:
3914 case LFUN_FONT_BOLDSYMBOL:
3915 case LFUN_FONT_ROMAN:
3916 case LFUN_FONT_NOUN:
3917 case LFUN_FONT_ITAL:
3918 case LFUN_FONT_FRAK:
3919 case LFUN_FONT_TYPEWRITER:
3920 case LFUN_FONT_SANS:
3921 case LFUN_TEXTSTYLE_APPLY:
3922 case LFUN_TEXTSTYLE_UPDATE:
3923 case LFUN_FONT_SIZE:
3924 case LFUN_FONT_UNDERLINE:
3925 case LFUN_FONT_STRIKEOUT:
3926 case LFUN_FONT_UULINE:
3927 case LFUN_FONT_UWAVE:
3929 case LFUN_WORD_CAPITALIZE:
3930 case LFUN_WORD_UPCASE:
3931 case LFUN_WORD_LOWCASE:
3932 case LFUN_CHARS_TRANSPOSE:
3933 if (cur.selIsMultiCell()) {
3936 getSelection(cur, rs, re, cs, ce);
3937 Cursor tmpcur = cur;
3938 for (row_type r = rs; r <= re; ++r) {
3939 for (col_type c = cs; c <= ce; ++c) {
3940 // cursor follows cell:
3941 tmpcur.idx() = tabular.cellIndex(r, c);
3942 // select this cell only:
3945 tmpcur.resetAnchor();
3946 tmpcur.pit() = tmpcur.lastpit();
3947 tmpcur.pos() = tmpcur.top().lastpos();
3948 tmpcur.setCursor(tmpcur);
3949 tmpcur.setSelection();
3950 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3955 cell(cur.idx())->dispatch(cur, cmd);
3959 case LFUN_INSET_SETTINGS:
3960 // relay this lfun to Inset, not to the cell.
3961 Inset::doDispatch(cur, cmd);
3965 // we try to handle this event in the insets dispatch function.
3966 cell(cur.idx())->dispatch(cur, cmd);
3972 // function sets an object as defined in func_status.h:
3973 // states OK, Unknown, Disabled, On, Off.
3974 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3975 FuncStatus & status) const
3977 switch (cmd.action) {
3978 case LFUN_INSET_MODIFY: {
3979 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
3982 string const s = cmd.getArg(1);
3983 // FIXME: We only check for the very first argument...
3984 int action = Tabular::LAST_ACTION;
3986 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3987 if (tabularFeature[i].feature == s) {
3988 action = tabularFeature[i].action;
3992 if (action == Tabular::LAST_ACTION) {
3994 status.unknown(true);
3998 string const argument = cmd.getLongArg(2);
4000 row_type sel_row_start = 0;
4001 row_type sel_row_end = 0;
4002 col_type sel_col_start = 0;
4003 col_type sel_col_end = 0;
4004 Tabular::ltType dummyltt;
4007 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4010 case Tabular::SET_PWIDTH:
4011 case Tabular::SET_MPWIDTH:
4012 case Tabular::SET_SPECIAL_COLUMN:
4013 case Tabular::SET_SPECIAL_MULTICOLUMN:
4014 case Tabular::SET_SPECIAL_MULTIROW:
4015 case Tabular::APPEND_ROW:
4016 case Tabular::APPEND_COLUMN:
4017 case Tabular::DELETE_ROW:
4018 case Tabular::DELETE_COLUMN:
4019 case Tabular::COPY_ROW:
4020 case Tabular::COPY_COLUMN:
4021 case Tabular::SET_TOP_SPACE:
4022 case Tabular::SET_BOTTOM_SPACE:
4023 case Tabular::SET_INTERLINE_SPACE:
4027 case Tabular::MULTICOLUMN:
4028 // If a row is set as longtable caption, it must not be allowed
4029 // to unset that this row is a multicolumn.
4030 status.setEnabled(sel_row_start == sel_row_end
4031 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4032 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4035 case Tabular::MULTIROW:
4036 // If a row is set as longtable caption, it must not be allowed
4037 // to unset that this row is a multirow.
4038 status.setEnabled(sel_col_start == sel_col_end
4039 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4040 status.setOnOff(tabular.isMultiRow(cur.idx()));
4043 case Tabular::SET_ALL_LINES:
4044 case Tabular::UNSET_ALL_LINES:
4045 case Tabular::SET_BORDER_LINES:
4046 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4049 case Tabular::SET_LINE_TOP:
4050 case Tabular::SET_LINE_BOTTOM:
4051 case Tabular::SET_LINE_LEFT:
4052 case Tabular::SET_LINE_RIGHT:
4053 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4056 case Tabular::TOGGLE_LINE_TOP:
4057 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4058 status.setOnOff(tabular.topLine(cur.idx()));
4061 case Tabular::TOGGLE_LINE_BOTTOM:
4062 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4063 status.setOnOff(tabular.bottomLine(cur.idx()));
4066 case Tabular::TOGGLE_LINE_LEFT:
4067 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4068 status.setOnOff(tabular.leftLine(cur.idx()));
4071 case Tabular::TOGGLE_LINE_RIGHT:
4072 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4073 status.setOnOff(tabular.rightLine(cur.idx()));
4076 case Tabular::M_ALIGN_LEFT:
4078 case Tabular::ALIGN_LEFT:
4079 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4080 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4083 case Tabular::M_ALIGN_RIGHT:
4085 case Tabular::ALIGN_RIGHT:
4086 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4087 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4090 case Tabular::M_ALIGN_CENTER:
4092 case Tabular::ALIGN_CENTER:
4093 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4094 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4097 case Tabular::ALIGN_BLOCK:
4098 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4099 && !tabular.isMultiRow(cur.idx()));
4100 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4103 case Tabular::M_VALIGN_TOP:
4105 case Tabular::VALIGN_TOP:
4106 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4107 && !tabular.isMultiRow(cur.idx()));
4109 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4112 case Tabular::M_VALIGN_BOTTOM:
4114 case Tabular::VALIGN_BOTTOM:
4115 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4116 && !tabular.isMultiRow(cur.idx()));
4118 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4121 case Tabular::M_VALIGN_MIDDLE:
4123 case Tabular::VALIGN_MIDDLE:
4124 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4125 && !tabular.isMultiRow(cur.idx()));
4127 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4130 case Tabular::SET_LONGTABULAR:
4131 status.setOnOff(tabular.is_long_tabular);
4134 case Tabular::UNSET_LONGTABULAR:
4135 status.setOnOff(!tabular.is_long_tabular);
4138 case Tabular::TOGGLE_ROTATE_TABULAR:
4139 case Tabular::SET_ROTATE_TABULAR:
4140 status.setOnOff(tabular.rotate);
4143 case Tabular::TABULAR_VALIGN_TOP:
4144 status.setOnOff(tabular.tabular_valignment
4145 == Tabular::LYX_VALIGN_TOP);
4147 case Tabular::TABULAR_VALIGN_MIDDLE:
4148 status.setOnOff(tabular.tabular_valignment
4149 == Tabular::LYX_VALIGN_MIDDLE);
4151 case Tabular::TABULAR_VALIGN_BOTTOM:
4152 status.setOnOff(tabular.tabular_valignment
4153 == Tabular::LYX_VALIGN_BOTTOM);
4156 case Tabular::LONGTABULAR_ALIGN_LEFT:
4157 status.setOnOff(tabular.longtabular_alignment
4158 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4160 case Tabular::LONGTABULAR_ALIGN_CENTER:
4161 status.setOnOff(tabular.longtabular_alignment
4162 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4164 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4165 status.setOnOff(tabular.longtabular_alignment
4166 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4169 case Tabular::UNSET_ROTATE_TABULAR:
4170 status.setOnOff(!tabular.rotate);
4173 case Tabular::TOGGLE_ROTATE_CELL:
4174 case Tabular::SET_ROTATE_CELL:
4175 status.setOnOff(!oneCellHasRotationState(false,
4176 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4179 case Tabular::UNSET_ROTATE_CELL:
4180 status.setOnOff(!oneCellHasRotationState(true,
4181 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4184 case Tabular::SET_USEBOX:
4185 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4188 // every row can only be one thing:
4189 // either a footer or header or caption
4190 case Tabular::SET_LTFIRSTHEAD:
4191 status.setEnabled(sel_row_start == sel_row_end
4192 && !tabular.ltCaption(sel_row_start));
4193 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4196 case Tabular::UNSET_LTFIRSTHEAD:
4197 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4200 case Tabular::SET_LTHEAD:
4201 status.setEnabled(sel_row_start == sel_row_end
4202 && !tabular.ltCaption(sel_row_start));
4203 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4206 case Tabular::UNSET_LTHEAD:
4207 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4210 case Tabular::SET_LTFOOT:
4211 status.setEnabled(sel_row_start == sel_row_end
4212 && !tabular.ltCaption(sel_row_start));
4213 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4216 case Tabular::UNSET_LTFOOT:
4217 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4220 case Tabular::SET_LTLASTFOOT:
4221 status.setEnabled(sel_row_start == sel_row_end
4222 && !tabular.ltCaption(sel_row_start));
4223 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4226 case Tabular::UNSET_LTLASTFOOT:
4227 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4230 case Tabular::SET_LTNEWPAGE:
4231 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4234 // only one row can be the caption
4235 // and a multirow cannot be set as caption
4236 case Tabular::TOGGLE_LTCAPTION:
4237 status.setEnabled(sel_row_start == sel_row_end
4238 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4239 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4240 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4241 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4242 && (!tabular.haveLTCaption()
4243 || tabular.ltCaption(sel_row_start))
4244 && !tabular.isMultiRow(sel_row_start));
4245 status.setOnOff(tabular.ltCaption(sel_row_start));
4248 case Tabular::SET_BOOKTABS:
4249 status.setOnOff(tabular.use_booktabs);
4252 case Tabular::UNSET_BOOKTABS:
4253 status.setOnOff(!tabular.use_booktabs);
4258 status.setEnabled(false);
4264 // These are only enabled inside tabular
4265 case LFUN_CELL_BACKWARD:
4266 case LFUN_CELL_FORWARD:
4267 status.setEnabled(true);
4270 // disable these with multiple cells selected
4271 case LFUN_INSET_INSERT:
4272 case LFUN_TABULAR_INSERT:
4273 case LFUN_FLEX_INSERT:
4274 case LFUN_FLOAT_INSERT:
4275 case LFUN_FLOAT_WIDE_INSERT:
4276 case LFUN_FOOTNOTE_INSERT:
4277 case LFUN_MARGINALNOTE_INSERT:
4278 case LFUN_MATH_INSERT:
4279 case LFUN_MATH_MODE:
4280 case LFUN_MATH_MUTATE:
4281 case LFUN_MATH_DISPLAY:
4282 case LFUN_NOTE_INSERT:
4283 case LFUN_OPTIONAL_INSERT:
4284 case LFUN_BOX_INSERT:
4285 case LFUN_BRANCH_INSERT:
4286 case LFUN_PHANTOM_INSERT:
4287 case LFUN_WRAP_INSERT:
4288 case LFUN_ERT_INSERT: {
4289 if (cur.selIsMultiCell()) {
4290 status.setEnabled(false);
4293 return cell(cur.idx())->getStatus(cur, cmd, status);
4296 // disable in non-fixed-width cells
4297 case LFUN_NEWLINE_INSERT:
4298 case LFUN_BREAK_PARAGRAPH: {
4299 if (tabular.getPWidth(cur.idx()).zero()) {
4300 status.setEnabled(false);
4303 return cell(cur.idx())->getStatus(cur, cmd, status);
4306 case LFUN_NEWPAGE_INSERT:
4307 status.setEnabled(false);
4311 if (tabularStackDirty() && theClipboard().isInternal()) {
4312 if (cur.selIsMultiCell()) {
4315 getSelection(cur, rs, re, cs, ce);
4316 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4317 && paste_tabular->nrows() == re - rs + 1)
4318 status.setEnabled(true);
4320 status.setEnabled(false);
4321 status.message(_("Selection size should match clipboard content."));
4324 status.setEnabled(true);
4327 return cell(cur.idx())->getStatus(cur, cmd, status);
4329 case LFUN_INSET_SETTINGS:
4330 // relay this lfun to Inset, not to the cell.
4331 return Inset::getStatus(cur, cmd, status);
4334 // we try to handle this event in the insets dispatch function.
4335 return cell(cur.idx())->getStatus(cur, cmd, status);
4341 Inset::DisplayType InsetTabular::display() const
4343 if (tabular.is_long_tabular) {
4344 switch (tabular.longtabular_alignment) {
4345 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4347 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4349 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4359 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4361 return tabular.latex(os, runparams);
4365 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4367 os << '\n'; // output table on a new line
4368 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4369 tabular.plaintext(os, runparams, dp, false, 0);
4370 return PLAINTEXT_NEWLINE;
4374 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4379 // FIXME: Why not pass a proper DocIterator here?
4381 // if the table is inside a float it doesn't need the informaltable
4382 // wrapper. Search for it.
4383 for (master = owner(); master; master = master->owner())
4384 if (master->lyxCode() == FLOAT_CODE)
4389 os << "<informaltable>";
4392 ret += tabular.docbook(os, runparams);
4394 os << "</informaltable>";
4401 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4404 // It'd be better to be able to get this from an InsetLayout, but at present
4405 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4406 xs << html::StartTag("table");
4407 docstring ret = tabular.xhtml(xs, rp);
4408 xs << html::EndTag("table");
4413 void InsetTabular::validate(LaTeXFeatures & features) const
4415 tabular.validate(features);
4417 // It'd be better to be able to get this from an InsetLayout, but at present
4418 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4419 if (features.runparams().flavor == OutputParams::HTML)
4420 features.addPreambleSnippet("<style type=\"text/css\">\n"
4421 "table { border: 1px solid black; display: inline-block; }\n"
4422 "td { border: 1px solid black; padding: 0.5ex; }\n"
4427 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4429 return tabular.cellInset(idx);
4433 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4435 return tabular.cellInset(idx);
4439 void InsetTabular::cursorPos(BufferView const & bv,
4440 CursorSlice const & sl, bool boundary, int & x, int & y) const
4442 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4444 int const row = tabular.cellRow(sl.idx());
4445 int const col = tabular.cellColumn(sl.idx());
4447 // y offset correction
4448 for (int r = 0; r < row; ++r)
4449 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4450 + tabular.interRowSpace(r + 1);
4452 y += tabular.textVOffset(sl.idx());
4454 // x offset correction
4455 for (int c = 0; c < col; ++c)
4456 x += tabular.column_info[c].width;
4458 x += tabular.textHOffset(sl.idx());
4459 x += ADD_TO_TABULAR_WIDTH;
4464 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4468 Inset const & inset = *tabular.cellInset(cell);
4469 Point o = bv.coordCache().getInsets().xy(&inset);
4470 int const xbeg = o.x_ - tabular.textHOffset(cell);
4471 int const xend = xbeg + tabular.columnWidth(cell);
4472 row_type const row = tabular.cellRow(cell);
4473 int const ybeg = o.y_ - tabular.rowAscent(row)
4474 - tabular.interRowSpace(row);
4475 int const yend = ybeg + tabular.rowHeight(cell);
4487 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4488 // << " ybeg=" << ybeg << " yend=" << yend
4489 // << " xx=" << xx << " yy=" << yy
4490 // << " dist=" << xx + yy << endl;
4495 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4497 //lyxerr << "InsetTabular::editXY: " << this << endl;
4498 cur.setSelection(false);
4500 cur.idx() = getNearestCell(cur.bv(), x, y);
4502 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4506 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4508 cur.idx() = getNearestCell(cur.bv(), x, y);
4509 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4513 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4515 idx_type idx_min = 0;
4516 int dist_min = numeric_limits<int>::max();
4517 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4518 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4519 int const d = dist(bv, i, x, y);
4530 int InsetTabular::cellXPos(idx_type const cell) const
4532 col_type col = tabular.cellColumn(cell);
4534 for (col_type c = 0; c < col; ++c)
4535 lx += tabular.columnWidth(c);
4541 void InsetTabular::resetPos(Cursor & cur) const
4543 BufferView & bv = cur.bv();
4544 int const maxwidth = bv.workWidth();
4546 int const scx_old = scx_;
4547 int const i = cur.find(this);
4552 int const X2 = maxwidth;
4553 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4554 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4555 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4558 scx_ = X1 + 20 - x1;
4560 scx_ = X2 - 20 - x2;
4565 // only update if offset changed
4566 if (scx_ != scx_old)
4567 cur.updateFlags(Update::Force | Update::FitCursor);
4571 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4573 row_type const row = tabular.cellRow(cur.idx());
4574 col_type const col = tabular.cellColumn(cur.idx());
4576 if (isRightToLeft(cur)) {
4577 if (tabular.cellColumn(cur.idx()) == 0) {
4578 if (row == tabular.nrows() - 1)
4580 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4585 cur.idx() = tabular.getLastCellInRow(row - 1);
4587 cur.idx() = tabular.cellIndex(row, col - 1);
4590 if (tabular.isLastCell(cur.idx()))
4592 if (cur.idx() == tabular.getLastCellInRow(row))
4593 cur.idx() = tabular.cellIndex(row + 1, 0);
4595 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4596 cur.idx() = tabular.cellIndex(row, colnextcell);
4600 cur.boundary(false);
4602 if (cur.selIsMultiCell()) {
4603 cur.pit() = cur.lastpit();
4604 cur.pos() = cur.lastpos();
4612 // in visual mode, place cursor at extreme left or right
4614 switch(entry_from) {
4616 case ENTRY_DIRECTION_RIGHT:
4617 cur.posVisToRowExtremity(false /* !left */);
4619 case ENTRY_DIRECTION_LEFT:
4620 cur.posVisToRowExtremity(true /* left */);
4622 case ENTRY_DIRECTION_IGNORE:
4623 // nothing to do in this case
4627 cur.setCurrentFont();
4632 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4634 row_type const row = tabular.cellRow(cur.idx());
4635 col_type const col = tabular.cellColumn(cur.idx());
4637 if (isRightToLeft(cur)) {
4638 if (cur.idx() == tabular.getLastCellInRow(row)) {
4641 cur.idx() = tabular.getFirstCellInRow(row);
4642 cur.idx() = tabular.cellAbove(cur.idx());
4644 if (tabular.isLastCell(cur.idx()))
4646 if (cur.idx() == tabular.getLastCellInRow(row))
4647 cur.idx() = tabular.cellIndex(row + 1, 0);
4649 cur.idx() = tabular.cellIndex(row, col + 1);
4652 if (cur.idx() == 0) // first cell
4655 cur.idx() = tabular.getLastCellInRow(row - 1);
4657 cur.idx() = tabular.cellIndex(row, col - 1);
4660 if (cur.selIsMultiCell()) {
4661 cur.pit() = cur.lastpit();
4662 cur.pos() = cur.lastpos();
4667 cur.pit() = cur.lastpit();
4668 cur.pos() = cur.lastpos();
4670 // in visual mode, place cursor at extreme left or right
4672 switch(entry_from) {
4674 case ENTRY_DIRECTION_RIGHT:
4675 cur.posVisToRowExtremity(false /* !left */);
4677 case ENTRY_DIRECTION_LEFT:
4678 cur.posVisToRowExtremity(true /* left */);
4680 case ENTRY_DIRECTION_IGNORE:
4681 // nothing to do in this case
4685 cur.setCurrentFont();
4690 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4692 istringstream is(argument);
4695 if (insetCode(s) != TABULAR_CODE)
4699 size_t safe_guard = 0;
4704 if (safe_guard > 1000) {
4705 LYXERR0("parameter max count reached!");
4709 Tabular::Feature action = Tabular::LAST_ACTION;
4712 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4713 if (s != tabularFeature[i].feature)
4716 action = tabularFeature[i].action;
4719 if (action == Tabular::LAST_ACTION) {
4720 LYXERR0("Feature not found " << s);
4724 if (tabularFeature[i].need_value)
4726 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4727 tabularFeatures(cur, action, val);
4733 static void checkLongtableSpecial(Tabular::ltType & ltt,
4734 string const & special, bool & flag)
4736 if (special == "dl_above") {
4739 } else if (special == "dl_below") {
4740 ltt.bottomDL = flag;
4742 } else if (special == "empty") {
4752 bool InsetTabular::oneCellHasRotationState(bool rotated,
4753 row_type row_start, row_type row_end,
4754 col_type col_start, col_type col_end) const
4756 for (row_type r = row_start; r <= row_end; ++r)
4757 for (col_type c = col_start; c <= col_end; ++c)
4758 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4764 void InsetTabular::tabularFeatures(Cursor & cur,
4765 Tabular::Feature feature, string const & value)
4767 col_type sel_col_start;
4768 col_type sel_col_end;
4769 row_type sel_row_start;
4770 row_type sel_row_end;
4771 bool setLines = false;
4772 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4773 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4777 case Tabular::M_ALIGN_LEFT:
4778 case Tabular::ALIGN_LEFT:
4779 setAlign = LYX_ALIGN_LEFT;
4782 case Tabular::M_ALIGN_RIGHT:
4783 case Tabular::ALIGN_RIGHT:
4784 setAlign = LYX_ALIGN_RIGHT;
4787 case Tabular::M_ALIGN_CENTER:
4788 case Tabular::ALIGN_CENTER:
4789 setAlign = LYX_ALIGN_CENTER;
4792 case Tabular::ALIGN_BLOCK:
4793 setAlign = LYX_ALIGN_BLOCK;
4796 case Tabular::M_VALIGN_TOP:
4797 case Tabular::VALIGN_TOP:
4798 setVAlign = Tabular::LYX_VALIGN_TOP;
4801 case Tabular::M_VALIGN_BOTTOM:
4802 case Tabular::VALIGN_BOTTOM:
4803 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4806 case Tabular::M_VALIGN_MIDDLE:
4807 case Tabular::VALIGN_MIDDLE:
4808 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4815 cur.recordUndoInset(ATOMIC_UNDO);
4817 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4818 row_type const row = tabular.cellRow(cur.idx());
4819 col_type const column = tabular.cellColumn(cur.idx());
4821 Tabular::ltType ltt;
4825 case Tabular::SET_PWIDTH: {
4826 Length const len(value);
4827 tabular.setColumnPWidth(cur, cur.idx(), len);
4829 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4830 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4834 case Tabular::SET_MPWIDTH:
4835 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4838 case Tabular::SET_SPECIAL_COLUMN:
4839 case Tabular::SET_SPECIAL_MULTICOLUMN:
4840 if (value == "none")
4841 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
4843 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4846 case Tabular::SET_SPECIAL_MULTIROW:
4847 //FIXME: noting to do here?
4850 case Tabular::APPEND_ROW:
4851 // append the row into the tabular
4852 tabular.appendRow(cur.idx());
4855 case Tabular::APPEND_COLUMN:
4856 // append the column into the tabular
4857 tabular.appendColumn(cur.idx());
4858 cur.idx() = tabular.cellIndex(row, column);
4861 case Tabular::DELETE_ROW:
4862 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4863 tabular.deleteRow(sel_row_start);
4864 if (sel_row_start >= tabular.nrows())
4866 cur.idx() = tabular.cellIndex(sel_row_start, column);
4869 cur.setSelection(false);
4872 case Tabular::DELETE_COLUMN:
4873 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4874 tabular.deleteColumn(sel_col_start);
4875 if (sel_col_start >= tabular.ncols())
4877 cur.idx() = tabular.cellIndex(row, sel_col_start);
4880 cur.setSelection(false);
4883 case Tabular::COPY_ROW:
4884 tabular.copyRow(row);
4887 case Tabular::COPY_COLUMN:
4888 tabular.copyColumn(column);
4889 cur.idx() = tabular.cellIndex(row, column);
4892 case Tabular::SET_LINE_TOP:
4893 case Tabular::TOGGLE_LINE_TOP: {
4894 bool lineSet = (feature == Tabular::SET_LINE_TOP)
4895 ? (value == "true") : !tabular.topLine(cur.idx());
4896 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4897 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4898 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
4902 case Tabular::SET_LINE_BOTTOM:
4903 case Tabular::TOGGLE_LINE_BOTTOM: {
4904 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
4905 ? (value == "true") : !tabular.bottomLine(cur.idx());
4906 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4907 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4908 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
4912 case Tabular::SET_LINE_LEFT:
4913 case Tabular::TOGGLE_LINE_LEFT: {
4914 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
4915 ? (value == "true") : !tabular.leftLine(cur.idx());
4916 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4917 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4918 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
4922 case Tabular::SET_LINE_RIGHT:
4923 case Tabular::TOGGLE_LINE_RIGHT: {
4924 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
4925 ? (value == "true") : !tabular.rightLine(cur.idx());
4926 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4927 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4928 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
4932 case Tabular::M_ALIGN_LEFT:
4933 case Tabular::M_ALIGN_RIGHT:
4934 case Tabular::M_ALIGN_CENTER:
4936 case Tabular::ALIGN_LEFT:
4937 case Tabular::ALIGN_RIGHT:
4938 case Tabular::ALIGN_CENTER:
4939 case Tabular::ALIGN_BLOCK:
4940 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4941 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4942 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
4945 case Tabular::M_VALIGN_TOP:
4946 case Tabular::M_VALIGN_BOTTOM:
4947 case Tabular::M_VALIGN_MIDDLE:
4949 case Tabular::VALIGN_TOP:
4950 case Tabular::VALIGN_BOTTOM:
4951 case Tabular::VALIGN_MIDDLE:
4952 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
4953 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
4954 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
4957 case Tabular::MULTICOLUMN: {
4958 if (!cur.selection()) {
4959 // just multicol for one single cell
4960 // check whether we are completely in a multicol
4961 if (tabular.isMultiColumn(cur.idx()))
4962 tabular.unsetMultiColumn(cur.idx());
4964 tabular.setMultiColumn(cur.idx(), 1);
4967 // we have a selection so this means we just add all this
4968 // cells to form a multicolumn cell
4969 idx_type const s_start = cur.selBegin().idx();
4970 row_type const col_start = tabular.cellColumn(s_start);
4971 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
4972 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
4975 cur.setSelection(false);
4979 case Tabular::MULTIROW: {
4980 if (!cur.selection()) {
4981 // just multirow for one single cell
4982 // check whether we are completely in a multirow
4983 if (tabular.isMultiRow(cur.idx()))
4984 tabular.unsetMultiRow(cur.idx());
4986 tabular.setMultiRow(cur.idx(), 1);
4989 // we have a selection so this means we just add all this
4990 // cells to form a multirow cell
4991 idx_type const s_start = cur.selBegin().idx();
4992 row_type const row_start = tabular.cellRow(s_start);
4993 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
4994 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
4997 cur.setSelection(false);
5001 case Tabular::SET_ALL_LINES:
5003 case Tabular::UNSET_ALL_LINES:
5004 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5005 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5006 idx_type const cell = tabular.cellIndex(r, c);
5007 tabular.setTopLine(cell, setLines);
5008 tabular.setBottomLine(cell, setLines);
5009 tabular.setRightLine(cell, setLines);
5010 tabular.setLeftLine(cell, setLines);
5014 case Tabular::SET_BORDER_LINES:
5015 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5016 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5017 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5019 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5020 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5021 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5025 case Tabular::SET_LONGTABULAR:
5026 tabular.is_long_tabular = true;
5029 case Tabular::UNSET_LONGTABULAR:
5030 for (row_type r = 0; r < tabular.nrows(); ++r) {
5031 if (tabular.ltCaption(r)) {
5032 cur.idx() = tabular.cellIndex(r, 0);
5035 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5038 tabular.is_long_tabular = false;
5041 case Tabular::SET_ROTATE_TABULAR:
5042 tabular.rotate = true;
5045 case Tabular::UNSET_ROTATE_TABULAR:
5046 tabular.rotate = false;
5049 case Tabular::TOGGLE_ROTATE_TABULAR:
5050 tabular.rotate = !tabular.rotate;
5053 case Tabular::TABULAR_VALIGN_TOP:
5054 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5057 case Tabular::TABULAR_VALIGN_MIDDLE:
5058 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5061 case Tabular::TABULAR_VALIGN_BOTTOM:
5062 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5065 case Tabular::LONGTABULAR_ALIGN_LEFT:
5066 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5069 case Tabular::LONGTABULAR_ALIGN_CENTER:
5070 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5073 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5074 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5079 case Tabular::SET_ROTATE_CELL:
5080 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5081 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5082 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5085 case Tabular::UNSET_ROTATE_CELL:
5086 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5087 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5088 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5091 case Tabular::TOGGLE_ROTATE_CELL:
5093 bool oneNotRotated = oneCellHasRotationState(false,
5094 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5096 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5097 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5098 tabular.setRotateCell(tabular.cellIndex(r, c),
5103 case Tabular::SET_USEBOX: {
5104 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5105 if (val == tabular.getUsebox(cur.idx()))
5106 val = Tabular::BOX_NONE;
5107 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5108 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5109 tabular.setUsebox(tabular.cellIndex(r, c), val);
5113 case Tabular::UNSET_LTFIRSTHEAD:
5115 case Tabular::SET_LTFIRSTHEAD:
5116 tabular.getRowOfLTFirstHead(row, ltt);
5117 checkLongtableSpecial(ltt, value, flag);
5118 tabular.setLTHead(row, flag, ltt, true);
5121 case Tabular::UNSET_LTHEAD:
5123 case Tabular::SET_LTHEAD:
5124 tabular.getRowOfLTHead(row, ltt);
5125 checkLongtableSpecial(ltt, value, flag);
5126 tabular.setLTHead(row, flag, ltt, false);
5129 case Tabular::UNSET_LTFOOT:
5131 case Tabular::SET_LTFOOT:
5132 tabular.getRowOfLTFoot(row, ltt);
5133 checkLongtableSpecial(ltt, value, flag);
5134 tabular.setLTFoot(row, flag, ltt, false);
5137 case Tabular::UNSET_LTLASTFOOT:
5139 case Tabular::SET_LTLASTFOOT:
5140 tabular.getRowOfLTLastFoot(row, ltt);
5141 checkLongtableSpecial(ltt, value, flag);
5142 tabular.setLTFoot(row, flag, ltt, true);
5145 case Tabular::SET_LTNEWPAGE:
5146 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5149 case Tabular::TOGGLE_LTCAPTION: {
5150 bool const set = !tabular.ltCaption(row);
5151 cur.idx() = tabular.setLTCaption(row, set);
5154 cur.setSelection(false);
5157 // When a row is set as caption, then also insert
5158 // a caption. Otherwise the LaTeX output is broken.
5159 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5160 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5162 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5163 if (lyx::getStatus(fr).enabled())
5169 case Tabular::SET_BOOKTABS:
5170 tabular.use_booktabs = true;
5173 case Tabular::UNSET_BOOKTABS:
5174 tabular.use_booktabs = false;
5177 case Tabular::SET_TOP_SPACE: {
5179 if (value == "default")
5180 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5181 tabular.row_info[r].top_space_default = true;
5182 else if (value == "none")
5183 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5184 tabular.row_info[r].top_space_default = false;
5185 tabular.row_info[r].top_space = len;
5187 else if (isValidLength(value, &len))
5188 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5189 tabular.row_info[r].top_space_default = false;
5190 tabular.row_info[r].top_space = len;
5195 case Tabular::SET_BOTTOM_SPACE: {
5197 if (value == "default")
5198 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5199 tabular.row_info[r].bottom_space_default = true;
5200 else if (value == "none")
5201 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5202 tabular.row_info[r].bottom_space_default = false;
5203 tabular.row_info[r].bottom_space = len;
5205 else if (isValidLength(value, &len))
5206 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5207 tabular.row_info[r].bottom_space_default = false;
5208 tabular.row_info[r].bottom_space = len;
5213 case Tabular::SET_INTERLINE_SPACE: {
5215 if (value == "default")
5216 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5217 tabular.row_info[r].interline_space_default = true;
5218 else if (value == "none")
5219 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5220 tabular.row_info[r].interline_space_default = false;
5221 tabular.row_info[r].interline_space = len;
5223 else if (isValidLength(value, &len))
5224 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5225 tabular.row_info[r].interline_space_default = false;
5226 tabular.row_info[r].interline_space = len;
5231 // dummy stuff just to avoid warnings
5232 case Tabular::LAST_ACTION:
5238 bool InsetTabular::copySelection(Cursor & cur)
5240 if (!cur.selection())
5245 getSelection(cur, rs, re, cs, ce);
5247 paste_tabular.reset(new Tabular(tabular));
5249 for (row_type r = 0; r < rs; ++r)
5250 paste_tabular->deleteRow(0);
5252 row_type const rows = re - rs + 1;
5253 while (paste_tabular->nrows() > rows)
5254 paste_tabular->deleteRow(rows);
5256 for (col_type c = 0; c < cs; ++c)
5257 paste_tabular->deleteColumn(0);
5259 col_type const columns = ce - cs + 1;
5260 while (paste_tabular->ncols() > columns)
5261 paste_tabular->deleteColumn(columns);
5263 paste_tabular->setBuffer(tabular.buffer());
5265 odocstringstream os;
5266 OutputParams const runparams(0);
5267 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5268 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5269 cap::copySelection(cur, os.str());
5271 // mark tabular stack dirty
5272 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5273 // when we (hopefully) have a one-for-all paste mechanism.
5274 // This must be called after cap::copySelection.
5275 dirtyTabularStack(true);
5281 bool InsetTabular::pasteClipboard(Cursor & cur)
5285 col_type actcol = tabular.cellColumn(cur.idx());
5286 row_type actrow = tabular.cellRow(cur.idx());
5288 if (cur.selIsMultiCell()) {
5291 getSelection(cur, actrow, re, actcol, ce);
5294 for (row_type r1 = 0, r2 = actrow;
5295 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5297 for (col_type c1 = 0, c2 = actcol;
5298 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5300 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5301 tabular.isPartOfMultiColumn(r2, c2))
5303 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5307 if (tabular.isPartOfMultiColumn(r2, c2)) {
5311 shared_ptr<InsetTableCell> inset(
5312 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5313 tabular.setCellInset(r2, c2, inset);
5314 // FIXME?: why do we need to do this explicitly? (EL)
5315 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5317 // FIXME: change tracking (MG)
5318 inset->setChange(Change(buffer().params().trackChanges ?
5319 Change::INSERTED : Change::UNCHANGED));
5327 void InsetTabular::cutSelection(Cursor & cur)
5329 if (!cur.selection())
5334 getSelection(cur, rs, re, cs, ce);
5335 for (row_type r = rs; r <= re; ++r) {
5336 for (col_type c = cs; c <= ce; ++c) {
5337 shared_ptr<InsetTableCell> t
5338 = cell(tabular.cellIndex(r, c));
5339 if (buffer().params().trackChanges)
5340 // FIXME: Change tracking (MG)
5341 t->setChange(Change(Change::DELETED));
5347 // cursor position might be invalid now
5348 if (cur.pit() > cur.lastpit())
5349 cur.pit() = cur.lastpit();
5350 if (cur.pos() > cur.lastpos())
5351 cur.pos() = cur.lastpos();
5352 cur.clearSelection();
5356 bool InsetTabular::isRightToLeft(Cursor & cur) const
5358 LASSERT(cur.depth() > 1, /**/);
5359 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5360 pos_type const parentpos = cur[cur.depth() - 2].pos();
5361 return parentpar.getFontSettings(buffer().params(),
5362 parentpos).language()->rightToLeft();
5366 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5369 LASSERT(stidx <= enidx, return docstring());
5371 col_type const col1 = tabular.cellColumn(stidx);
5372 col_type const col2 = tabular.cellColumn(enidx);
5373 row_type const row1 = tabular.cellRow(stidx);
5374 row_type const row2 = tabular.cellRow(enidx);
5376 for (col_type col = col1; col <= col2; col++)
5377 for (row_type row = row1; row <= row2; row++) {
5382 retval += tabular.cellInset(row, col)->asString(intoInsets);
5388 void InsetTabular::getSelection(Cursor & cur,
5389 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5391 CursorSlice const & beg = cur.selBegin();
5392 CursorSlice const & end = cur.selEnd();
5393 cs = tabular.cellColumn(beg.idx());
5394 ce = tabular.cellColumn(end.idx());
5398 rs = tabular.cellRow(beg.idx());
5399 re = tabular.cellRow(end.idx());
5405 Text * InsetTabular::getText(int idx) const
5407 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5411 void InsetTabular::setChange(Change const & change)
5413 for (idx_type idx = 0; idx < nargs(); ++idx)
5414 cell(idx)->setChange(change);
5418 void InsetTabular::acceptChanges()
5420 for (idx_type idx = 0; idx < nargs(); ++idx)
5421 cell(idx)->acceptChanges();
5425 void InsetTabular::rejectChanges()
5427 for (idx_type idx = 0; idx < nargs(); ++idx)
5428 cell(idx)->rejectChanges();
5432 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5434 return tabular.getPWidth(cell).zero();
5438 bool InsetTabular::forcePlainLayout(idx_type cell) const
5440 return !tabular.getPWidth(cell).zero();
5444 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5447 if (buf.length() <= 0)
5452 col_type maxCols = 1;
5453 size_t const len = buf.length();
5457 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5465 maxCols = max(cols, maxCols);
5471 maxCols = max(cols, maxCols);
5477 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5478 loctab = paste_tabular.get();
5480 dirtyTabularStack(true);
5483 cell = bv.cursor().idx();
5484 ocol = tabular.cellColumn(cell);
5485 row = tabular.cellRow(cell);
5489 idx_type const cells = loctab->numberofcells;
5492 rows = loctab->nrows();
5493 col_type const columns = loctab->ncols();
5495 while (cell < cells && p < len && row < rows &&
5496 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5502 // we can only set this if we are not too far right
5503 if (cols < columns) {
5504 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5505 Font const font = bv.textMetrics(&inset->text()).
5507 inset->setText(buf.substr(op, p - op), font,
5508 buffer().params().trackChanges);
5514 // we can only set this if we are not too far right
5515 if (cols < columns) {
5516 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5517 Font const font = bv.textMetrics(&inset->text()).
5519 inset->setText(buf.substr(op, p - op), font,
5520 buffer().params().trackChanges);
5525 cell = loctab->cellIndex(row, cols);
5531 // check for the last cell if there is no trailing '\n'
5532 if (cell < cells && op < len) {
5533 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5534 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5535 inset->setText(buf.substr(op, len - op), font,
5536 buffer().params().trackChanges);
5542 void InsetTabular::addPreview(DocIterator const & inset_pos,
5543 PreviewLoader & loader) const
5545 DocIterator cell_pos = inset_pos;
5547 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5548 for (row_type r = 0; r < tabular.nrows(); ++r) {
5549 for (col_type c = 0; c < tabular.ncols(); ++c) {
5550 cell_pos.top().idx() = tabular.cellIndex(r, c);
5551 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5557 bool InsetTabular::completionSupported(Cursor const & cur) const
5559 Cursor const & bvCur = cur.bv().cursor();
5560 if (&bvCur.inset() != this)
5562 return cur.text()->completionSupported(cur);
5566 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5568 return completionSupported(cur);
5572 bool InsetTabular::automaticInlineCompletion() const
5574 return lyxrc.completion_inline_text;
5578 bool InsetTabular::automaticPopupCompletion() const
5580 return lyxrc.completion_popup_text;
5584 bool InsetTabular::showCompletionCursor() const
5586 return lyxrc.completion_cursor_text;
5590 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5592 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5596 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5598 if (!completionSupported(cur))
5600 return cur.text()->completionPrefix(cur);
5604 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5606 if (!completionSupported(cur))
5609 return cur.text()->insertCompletion(cur, s, finished);
5613 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5614 Dimension & dim) const
5616 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5617 tm.completionPosAndDim(cur, x, y, dim);
5621 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5623 istringstream data(in);
5625 lex.setStream(data);
5632 if (!lex || token != "tabular") {
5633 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5637 // This is part of the inset proper that is usually swallowed
5638 // by Buffer::readInset
5640 if (!lex || token != "Tabular") {
5641 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5649 string InsetTabular::params2string(InsetTabular const & inset)
5652 data << "tabular" << ' ';
5654 data << "\\end_inset\n";