2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 { Tabular::APPEND_ROW, "append-row", false },
110 { Tabular::APPEND_COLUMN, "append-column", false },
111 { Tabular::DELETE_ROW, "delete-row", false },
112 { Tabular::DELETE_COLUMN, "delete-column", false },
113 { Tabular::COPY_ROW, "copy-row", false },
114 { Tabular::COPY_COLUMN, "copy-column", false },
115 { Tabular::SET_LINE_TOP, "set-line-top", true },
116 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
117 { Tabular::SET_LINE_LEFT, "set-line-left", true },
118 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
119 //FIXME: get rid of those 4 TOGGLE actions in favor of the 4 above.
120 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
121 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
122 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
123 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
124 { Tabular::ALIGN_LEFT, "align-left", false },
125 { Tabular::ALIGN_RIGHT, "align-right", false },
126 { Tabular::ALIGN_CENTER, "align-center", false },
127 { Tabular::ALIGN_BLOCK, "align-block", false },
128 { Tabular::ALIGN_DECIMAL, "align-decimal", 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::SET_DECIMAL_POINT, "set-decimal-point", true },
179 { Tabular::LAST_ACTION, "", false }
184 string const write_attribute(string const & name, T const & t)
186 string const s = tostr(t);
187 return s.empty() ? s : " " + name + "=\"" + s + "\"";
191 string const write_attribute(string const & name, string const & t)
193 return t.empty() ? t : " " + name + "=\"" + t + "\"";
198 string const write_attribute(string const & name, docstring const & t)
200 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
205 string const write_attribute(string const & name, bool const & b)
207 // we write only true attribute values so we remove a bit of the
208 // file format bloat for tabulars.
209 return b ? write_attribute(name, convert<string>(b)) : string();
214 string const write_attribute(string const & name, int const & i)
216 // we write only true attribute values so we remove a bit of the
217 // file format bloat for tabulars.
218 return i ? write_attribute(name, convert<string>(i)) : string();
223 string const write_attribute(string const & name, Tabular::idx_type const & i)
225 // we write only true attribute values so we remove a bit of the
226 // file format bloat for tabulars.
227 return i ? write_attribute(name, convert<string>(i)) : string();
232 string const write_attribute(string const & name, Length const & value)
234 // we write only the value if we really have one same reson as above.
235 return value.zero() ? string() : write_attribute(name, value.asString());
239 string const tostr(LyXAlignment const & num)
244 case LYX_ALIGN_BLOCK:
248 case LYX_ALIGN_CENTER:
250 case LYX_ALIGN_RIGHT:
252 case LYX_ALIGN_LAYOUT:
254 case LYX_ALIGN_SPECIAL:
256 case LYX_ALIGN_DECIMAL:
263 string const tostr(Tabular::HAlignment const & num)
266 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
268 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
270 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
277 string const tostr(Tabular::VAlignment const & num)
280 case Tabular::LYX_VALIGN_TOP:
282 case Tabular::LYX_VALIGN_MIDDLE:
284 case Tabular::LYX_VALIGN_BOTTOM:
291 string const tostr(Tabular::BoxType const & num)
294 case Tabular::BOX_NONE:
296 case Tabular::BOX_PARBOX:
298 case Tabular::BOX_MINIPAGE:
305 // I would have liked a fromstr template a lot better. (Lgb)
306 bool string2type(string const str, LyXAlignment & num)
309 num = LYX_ALIGN_NONE;
310 else if (str == "block")
311 num = LYX_ALIGN_BLOCK;
312 else if (str == "left")
313 num = LYX_ALIGN_LEFT;
314 else if (str == "center")
315 num = LYX_ALIGN_CENTER;
316 else if (str == "right")
317 num = LYX_ALIGN_RIGHT;
318 else if (str == "decimal")
319 num = LYX_ALIGN_DECIMAL;
326 bool string2type(string const str, Tabular::HAlignment & num)
329 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
330 else if (str == "center" )
331 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
332 else if (str == "right")
333 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
340 bool string2type(string const str, Tabular::VAlignment & num)
343 num = Tabular::LYX_VALIGN_TOP;
344 else if (str == "middle" )
345 num = Tabular::LYX_VALIGN_MIDDLE;
346 else if (str == "bottom")
347 num = Tabular::LYX_VALIGN_BOTTOM;
354 bool string2type(string const str, Tabular::BoxType & num)
357 num = Tabular::BOX_NONE;
358 else if (str == "parbox")
359 num = Tabular::BOX_PARBOX;
360 else if (str == "minipage")
361 num = Tabular::BOX_MINIPAGE;
368 bool string2type(string const str, bool & num)
372 else if (str == "false")
380 bool getTokenValue(string const & str, char const * token, string & ret)
383 size_t token_length = strlen(token);
384 size_t pos = str.find(token);
386 if (pos == string::npos || pos + token_length + 1 >= str.length()
387 || str[pos + token_length] != '=')
389 pos += token_length + 1;
391 if (ch != '"' && ch != '\'') { // only read till next space
395 while (pos < str.length() - 1 && str[++pos] != ch)
402 bool getTokenValue(string const & str, char const * token, docstring & ret)
405 bool const success = getTokenValue(str, token, tmp);
406 ret = from_utf8(tmp);
411 bool getTokenValue(string const & str, char const * token, int & num)
415 if (!getTokenValue(str, token, tmp))
417 num = convert<int>(tmp);
422 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
425 return getTokenValue(str, token, tmp) && string2type(tmp, num);
429 bool getTokenValue(string const & str, char const * token,
430 Tabular::HAlignment & num)
433 return getTokenValue(str, token, tmp) && string2type(tmp, num);
437 bool getTokenValue(string const & str, char const * token,
438 Tabular::VAlignment & num)
441 return getTokenValue(str, token, tmp) && string2type(tmp, num);
445 bool getTokenValue(string const & str, char const * token,
446 Tabular::BoxType & num)
449 return getTokenValue(str, token, tmp) && string2type(tmp, num);
453 bool getTokenValue(string const & str, char const * token, bool & flag)
455 // set the flag always to false as this should be the default for bools
456 // not in the file-format.
459 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
463 bool getTokenValue(string const & str, char const * token, Length & len)
465 // set the length to be zero() as default as this it should be if not
466 // in the file format.
469 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
473 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
478 if (!getTokenValue(str, token, tmp))
480 if (tmp == "default") {
484 return isValidLength(tmp, &len);
488 void l_getline(istream & is, string & str)
491 while (str.empty()) {
493 if (!str.empty() && str[str.length() - 1] == '\r')
494 str.erase(str.length() - 1);
501 string const featureAsString(Tabular::Feature action)
503 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
504 if (tabularFeature[i].action == action)
505 return tabularFeature[i].feature;
511 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
513 InsetTableCell tail = InsetTableCell(head);
514 tail.getText(0)->setMacrocontextPosition(
515 head.getText(0)->macrocontextPosition());
516 tail.setBuffer(head.buffer());
518 DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
519 for (; dit; dit.forwardChar())
520 if (dit.inTexted() && dit.depth()==1
521 && dit.paragraph().find(align_d, false, false, dit.pos()))
524 pit_type const psize = head.paragraphs().front().size();
527 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
528 tail.paragraphs().front().eraseChars(0,
529 dit.pos() < psize ? dit.pos() + 1 : psize, false);
536 /////////////////////////////////////////////////////////////////////
540 /////////////////////////////////////////////////////////////////////
543 Tabular::CellData::CellData(Buffer * buf)
546 multicolumn(Tabular::CELL_NORMAL),
547 multirow(Tabular::CELL_NORMAL),
548 alignment(LYX_ALIGN_CENTER),
549 valignment(LYX_VALIGN_TOP),
559 inset(new InsetTableCell(buf))
561 inset->setBuffer(*buf);
565 Tabular::CellData::CellData(CellData const & cs)
568 multicolumn(cs.multicolumn),
569 multirow(cs.multirow),
570 alignment(cs.alignment),
571 valignment(cs.valignment),
572 decimal_hoffset(cs.decimal_hoffset),
573 decimal_width(cs.decimal_width),
575 top_line(cs.top_line),
576 bottom_line(cs.bottom_line),
577 left_line(cs.left_line),
578 right_line(cs.right_line),
581 align_special(cs.align_special),
583 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
587 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
593 void Tabular::CellData::swap(CellData & rhs)
595 std::swap(cellno, rhs.cellno);
596 std::swap(width, rhs.width);
597 std::swap(multicolumn, rhs.multicolumn);
598 std::swap(multirow, rhs.multirow);
599 std::swap(alignment, rhs.alignment);
600 std::swap(valignment, rhs.valignment);
601 std::swap(decimal_hoffset, rhs.decimal_hoffset);
602 std::swap(decimal_width, rhs.decimal_width);
603 std::swap(voffset, rhs.voffset);
604 std::swap(top_line, rhs.top_line);
605 std::swap(bottom_line, rhs.bottom_line);
606 std::swap(left_line, rhs.left_line);
607 std::swap(right_line, rhs.right_line);
608 std::swap(usebox, rhs.usebox);
609 std::swap(rotate, rhs.rotate);
610 std::swap(align_special, rhs.align_special);
611 p_width.swap(rhs.p_width);
612 inset.swap(rhs.inset);
616 Tabular::RowData::RowData()
619 top_space_default(false),
620 bottom_space_default(false),
621 interline_space_default(false),
631 Tabular::ColumnData::ColumnData()
632 : alignment(LYX_ALIGN_CENTER),
633 valignment(LYX_VALIGN_TOP),
639 Tabular::ltType::ltType()
646 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
648 init(buffer, rows_arg, columns_arg);
652 void Tabular::setBuffer(Buffer & buffer)
655 for (row_type i = 0; i < nrows(); ++i)
656 for (col_type j = 0; j < ncols(); ++j)
657 cell_info[i][j].inset->setBuffer(*buffer_);
661 // activates all lines and sets all widths to 0
662 void Tabular::init(Buffer * buf, row_type rows_arg,
663 col_type columns_arg)
666 row_info = row_vector(rows_arg);
667 column_info = column_vector(columns_arg);
668 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
669 row_info.reserve(10);
670 column_info.reserve(10);
671 cell_info.reserve(100);
673 is_long_tabular = false;
674 tabular_valignment = LYX_VALIGN_MIDDLE;
675 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
677 use_booktabs = false;
678 // set silly default lines
679 for (row_type r = 0; r < nrows(); ++r)
680 for (col_type c = 0; c < ncols(); ++c) {
681 cell_info[r][c].inset->setBuffer(*buffer_);
682 cell_info[r][c].top_line = true;
683 cell_info[r][c].left_line = true;
684 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
685 cell_info[r][c].right_line = c == ncols() - 1;
690 void Tabular::appendRow(idx_type const cell)
692 row_type const row = cellRow(cell);
694 row_info.insert(row_info.begin() + row + 1, RowData());
695 row_info[row + 1] = row_info[row];
697 cell_info.insert(cell_info.begin() + row + 1,
698 cell_vector(ncols(), CellData(buffer_)));
699 for (col_type c = 0; c < ncols(); ++c) {
700 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
701 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
703 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
707 for (col_type c = 0; c < ncols(); ++c) {
708 if (isPartOfMultiRow(row, c))
710 // inherit line settings
711 idx_type const i = cellIndex(row + 1, c);
712 idx_type const j = cellIndex(row, c);
713 setLeftLine(i, leftLine(j));
714 setRightLine(i, rightLine(j));
715 setTopLine(i, topLine(j));
716 if (topLine(j) && bottomLine(j)) {
717 setBottomLine(i, true);
718 setBottomLine(j, false);
720 // mark track changes
721 if (buffer().params().trackChanges)
722 cellInfo(i).inset->setChange(Change(Change::INSERTED));
727 void Tabular::deleteRow(row_type const row)
729 // Not allowed to delete last row
733 for (col_type c = 0; c < ncols(); ++c) {
734 // Care about multirow cells
735 if (row + 1 < nrows() &&
736 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
737 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
738 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
741 row_info.erase(row_info.begin() + row);
742 cell_info.erase(cell_info.begin() + row);
747 void Tabular::copyRow(row_type const row)
749 row_info.insert(row_info.begin() + row, row_info[row]);
750 cell_info.insert(cell_info.begin() + row, cell_info[row]);
752 if (buffer().params().trackChanges)
753 for (col_type c = 0; c < ncols(); ++c)
754 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
760 void Tabular::appendColumn(idx_type const cell)
762 col_type const c = cellColumn(cell);
764 column_info.insert(column_info.begin() + c + 1, ColumnData());
765 column_info[c + 1] = column_info[c];
767 for (row_type r = 0; r < nrows(); ++r) {
768 cell_info[r].insert(cell_info[r].begin() + c + 1,
770 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
771 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
773 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
776 for (row_type r = 0; r < nrows(); ++r) {
777 // inherit line settings
778 idx_type const i = cellIndex(r, c + 1);
779 idx_type const j = cellIndex(r, c);
780 setBottomLine(i, bottomLine(j));
781 setTopLine(i, topLine(j));
782 setLeftLine(i, leftLine(j));
783 if (rightLine(j) && rightLine(j)) {
784 setRightLine(i, true);
785 setRightLine(j, false);
787 if (buffer().params().trackChanges)
788 cellInfo(i).inset->setChange(Change(Change::INSERTED));
793 void Tabular::deleteColumn(col_type const col)
795 // Not allowed to delete last column
799 for (row_type r = 0; r < nrows(); ++r) {
800 // Care about multicolumn cells
801 if (col + 1 < ncols() &&
802 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
803 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
804 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
806 cell_info[r].erase(cell_info[r].begin() + col);
808 column_info.erase(column_info.begin() + col);
813 void Tabular::copyColumn(col_type const col)
815 BufferParams const & bp = buffer().params();
816 column_info.insert(column_info.begin() + col, column_info[col]);
818 for (row_type r = 0; r < nrows(); ++r) {
819 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
821 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
827 void Tabular::updateIndexes()
831 for (row_type row = 0; row < nrows(); ++row)
832 for (col_type column = 0; column < ncols(); ++column) {
833 if (!isPartOfMultiColumn(row, column)
834 && !isPartOfMultiRow(row, column))
836 if (isPartOfMultiRow(row, column))
837 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
839 cell_info[row][column].cellno = numberofcells - 1;
842 rowofcell.resize(numberofcells);
843 columnofcell.resize(numberofcells);
845 for (row_type row = 0; row < nrows(); ++row)
846 for (col_type column = 0; column < ncols(); ++column) {
847 if (isPartOfMultiColumn(row, column)
848 || isPartOfMultiRow(row, column))
851 columnofcell[i] = column;
852 setFixedWidth(row, column);
853 cell_info[row][column].inset->setContentAlignment(
854 getAlignment(cellIndex(row, column)));
860 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
863 for (col_type c = 0; c < ncols(); ++c)
864 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
870 bool Tabular::topLine(idx_type const cell) const
872 return cellInfo(cell).top_line;
876 bool Tabular::bottomLine(idx_type const cell) const
878 return cellInfo(cell).bottom_line;
882 bool Tabular::leftLine(idx_type cell) const
886 return cellInfo(cell).left_line;
890 bool Tabular::rightLine(idx_type cell) const
894 return cellInfo(cell).right_line;
898 int Tabular::interRowSpace(row_type row) const
900 if (!row || row >= nrows())
903 int const interline_space = row_info[row - 1].interline_space_default ?
905 row_info[row - 1].interline_space.inPixels(width());
906 if (rowTopLine(row) && rowBottomLine(row - 1))
907 return interline_space + WIDTH_OF_LINE;
908 return interline_space;
912 int Tabular::interColumnSpace(idx_type cell) const
914 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
915 if (rightLine(cell) && nextcol < ncols()
916 && leftLine(cellIndex(cellRow(cell), nextcol)))
917 return WIDTH_OF_LINE;
922 int Tabular::cellWidth(idx_type cell) const
925 col_type const span = columnSpan(cell);
926 col_type const col = cellColumn(cell);
927 for(col_type c = col; c < col + span ; ++c)
928 w += column_info[c].width;
933 int Tabular::cellHeight(idx_type cell) const
935 row_type const span = rowSpan(cell);
936 row_type const row = cellRow(cell);
938 for(row_type r = row; r < row + span ; ++r) {
939 h += rowAscent(r) + rowDescent(r);
940 if (r != row + span - 1)
941 h += interRowSpace(r + 1);
948 bool Tabular::updateColumnWidths()
950 vector<int> max_dwidth(ncols(), 0);
951 for(col_type c = 0; c < ncols(); ++c)
952 for(row_type r = 0; r < nrows(); ++r) {
953 idx_type const i = cellIndex(r, c);
954 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
955 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
959 // for each col get max of single col cells
960 for(col_type c = 0; c < ncols(); ++c) {
962 for(row_type r = 0; r < nrows(); ++r) {
963 idx_type const i = cellIndex(r, c);
964 if (columnSpan(i) == 1) {
965 if (getAlignment(i) == LYX_ALIGN_DECIMAL
966 && cell_info[r][c].decimal_width!=0)
967 new_width = max(new_width, cellInfo(i).width
968 + max_dwidth[c] - cellInfo(i).decimal_width);
970 new_width = max(new_width, cellInfo(i).width);
974 if (column_info[c].width != new_width) {
975 column_info[c].width = new_width;
979 // update col widths to fit merged cells
980 for(col_type c = 0; c < ncols(); ++c)
981 for(row_type r = 0; r < nrows(); ++r) {
982 idx_type const i = cellIndex(r, c);
983 int const span = columnSpan(i);
984 if (span == 1 || c > cellColumn(i))
988 for(col_type j = c; j < c + span ; ++j)
989 old_width += column_info[j].width;
991 if (cellInfo(i).width > old_width) {
992 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1001 int Tabular::width() const
1004 for (col_type c = 0; c < ncols(); ++c)
1005 width += column_info[c].width;
1010 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1013 col_type const col = cellColumn(cell);
1014 if (onlycolumn || !isMultiColumn(cell)) {
1015 column_info[col].alignment = align;
1016 docstring & dpoint = column_info[col].decimal_point;
1017 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1018 dpoint = from_utf8(lyxrc.default_decimal_point);
1020 cellInfo(cell).alignment = align;
1021 cellInset(cell).get()->setContentAlignment(align);
1026 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1029 if (!isMultiColumn(cell) || onlycolumn)
1030 column_info[cellColumn(cell)].valignment = align;
1032 cellInfo(cell).valignment = align;
1039 * Allow line and paragraph breaks for fixed width cells or disallow them,
1040 * merge cell paragraphs and reset layout to standard for variable width
1043 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1045 inset->setAutoBreakRows(fixedWidth);
1046 inset->toggleFixedWidth(fixedWidth);
1050 // merge all paragraphs to one
1051 BufferParams const & bp = cur.bv().buffer().params();
1052 while (inset->paragraphs().size() > 1)
1053 mergeParagraph(bp, inset->paragraphs(), 0);
1057 // undo information has already been recorded
1058 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1059 bp.documentClass().plainLayoutName());
1066 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1067 Length const & width)
1069 col_type const c = cellColumn(cell);
1071 column_info[c].p_width = width;
1072 // reset the vertical alignment to top if the fixed with
1073 // is removed or zero because only fixed width columns can
1074 // have a vertical alignment
1075 if (column_info[c].p_width.zero())
1076 column_info[c].valignment = LYX_VALIGN_TOP;
1077 for (row_type r = 0; r < nrows(); ++r) {
1078 idx_type const cell = cellIndex(r, c);
1079 // because of multicolumns
1080 toggleFixedWidth(cur, cellInset(cell).get(),
1081 !getPWidth(cell).zero());
1083 // cur paragraph can become invalid after paragraphs were merged
1084 if (cur.pit() > cur.lastpit())
1085 cur.pit() = cur.lastpit();
1086 // cur position can become invalid after newlines were removed
1087 if (cur.pos() > cur.lastpos())
1088 cur.pos() = cur.lastpos();
1092 bool Tabular::setFixedWidth(row_type r, col_type c)
1094 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1095 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1096 || (multicol && !cell_info[r][c].p_width.zero());
1097 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1102 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1103 Length const & width)
1105 if (!isMultiColumn(cell))
1108 cellInfo(cell).p_width = width;
1109 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1110 // cur paragraph can become invalid after paragraphs were merged
1111 if (cur.pit() > cur.lastpit())
1112 cur.pit() = cur.lastpit();
1113 // cur position can become invalid after newlines were removed
1114 if (cur.pos() > cur.lastpos())
1115 cur.pos() = cur.lastpos();
1120 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1121 Tabular::Feature what)
1123 if (what == SET_SPECIAL_MULTICOLUMN)
1124 cellInfo(cell).align_special = special;
1126 column_info[cellColumn(cell)].align_special = special;
1130 void Tabular::setTopLine(idx_type i, bool line)
1132 cellInfo(i).top_line = line;
1136 void Tabular::setBottomLine(idx_type i, bool line)
1138 cellInfo(i).bottom_line = line;
1142 void Tabular::setLeftLine(idx_type cell, bool line)
1144 cellInfo(cell).left_line = line;
1148 void Tabular::setRightLine(idx_type cell, bool line)
1150 cellInfo(cell).right_line = line;
1153 bool Tabular::rowTopLine(row_type r) const
1155 bool all_rows_set = true;
1156 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1157 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1158 return all_rows_set;
1162 bool Tabular::rowBottomLine(row_type r) const
1164 bool all_rows_set = true;
1165 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1166 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1167 return all_rows_set;
1171 bool Tabular::columnLeftLine(col_type c) const
1178 for (row_type r = 0; r < nrows(); ++r) {
1179 idx_type const i = cellIndex(r, c);
1180 if (c == cellColumn(i)) {
1182 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1183 if (cellInfo(i).left_line || right)
1187 return 2 * nrows_left >= total;
1191 bool Tabular::columnRightLine(col_type c) const
1196 int nrows_right = 0;
1198 for (row_type r = 0; r < nrows(); ++r) {
1199 idx_type i = cellIndex(r, c);
1200 if (c == cellColumn(i) + columnSpan(i) - 1) {
1202 bool left = (c + 1 < ncols()
1203 && cellInfo(cellIndex(r, c + 1)).left_line)
1204 || c + 1 == ncols();
1205 if (cellInfo(i).right_line && left)
1209 return 2 * nrows_right >= total;
1213 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1215 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1216 return cellInfo(cell).alignment;
1218 return column_info[cellColumn(cell)].alignment;
1223 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1225 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1226 return cellInfo(cell).valignment;
1227 return column_info[cellColumn(cell)].valignment;
1231 Length const Tabular::getPWidth(idx_type cell) const
1233 if (isMultiColumn(cell))
1234 return cellInfo(cell).p_width;
1235 return column_info[cellColumn(cell)].p_width;
1239 int Tabular::textHOffset(idx_type cell) const
1241 // the LaTeX Way :-(
1242 int x = WIDTH_OF_LINE;
1244 int const w = cellWidth(cell) - cellInfo(cell).width;
1246 switch (getAlignment(cell)) {
1247 case LYX_ALIGN_CENTER:
1250 case LYX_ALIGN_RIGHT:
1253 case LYX_ALIGN_DECIMAL: {
1254 // we center when no decimal point
1255 if (cellInfo(cell).decimal_width == 0) {
1259 col_type const c = cellColumn(cell);
1260 int max_dhoffset = 0;
1261 for(row_type r = 0; r < row_info.size() ; ++r) {
1262 idx_type const i = cellIndex(r, c);
1263 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1264 && cellInfo(i).decimal_width != 0)
1265 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1267 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1270 // LYX_ALIGN_LEFT: nothing :-)
1278 int Tabular::textVOffset(idx_type cell) const
1280 int voffset = cellInfo(cell).voffset;
1281 if (isMultiRow(cell)) {
1282 row_type const row = cellRow(cell);
1283 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1289 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1292 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1294 return cell_info[row][c].cellno;
1298 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1300 col_type c = ncols() - 1;
1301 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1302 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1304 return cell_info[row][c].cellno;
1308 Tabular::row_type Tabular::cellRow(idx_type cell) const
1310 if (cell >= numberofcells)
1314 return rowofcell[cell];
1318 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1320 if (cell >= numberofcells)
1324 return columnofcell[cell];
1328 void Tabular::write(ostream & os) const
1332 << write_attribute("version", 3)
1333 << write_attribute("rows", nrows())
1334 << write_attribute("columns", ncols())
1336 // global longtable options
1338 << write_attribute("rotate", rotate)
1339 << write_attribute("booktabs", use_booktabs)
1340 << write_attribute("islongtable", is_long_tabular)
1341 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1342 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1343 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1344 << write_attribute("headTopDL", endhead.topDL)
1345 << write_attribute("headBottomDL", endhead.bottomDL)
1346 << write_attribute("footTopDL", endfoot.topDL)
1347 << write_attribute("footBottomDL", endfoot.bottomDL)
1348 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1349 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1350 << write_attribute("lastFootEmpty", endlastfoot.empty);
1351 // longtables cannot be aligned vertically
1352 if (!is_long_tabular)
1353 os << write_attribute("tabularvalignment", tabular_valignment);
1354 if (is_long_tabular)
1355 os << write_attribute("longtabularalignment",
1356 longtabular_alignment);
1358 for (col_type c = 0; c < ncols(); ++c) {
1360 << write_attribute("alignment", column_info[c].alignment);
1361 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1362 os << write_attribute("decimal_point", column_info[c].decimal_point);
1363 os << write_attribute("valignment", column_info[c].valignment)
1364 << write_attribute("width", column_info[c].p_width.asString())
1365 << write_attribute("special", column_info[c].align_special)
1368 for (row_type r = 0; r < nrows(); ++r) {
1369 static const string def("default");
1371 if (row_info[r].top_space_default)
1372 os << write_attribute("topspace", def);
1374 os << write_attribute("topspace", row_info[r].top_space);
1375 if (row_info[r].bottom_space_default)
1376 os << write_attribute("bottomspace", def);
1378 os << write_attribute("bottomspace", row_info[r].bottom_space);
1379 if (row_info[r].interline_space_default)
1380 os << write_attribute("interlinespace", def);
1382 os << write_attribute("interlinespace", row_info[r].interline_space);
1383 os << write_attribute("endhead", row_info[r].endhead)
1384 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1385 << write_attribute("endfoot", row_info[r].endfoot)
1386 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1387 << write_attribute("newpage", row_info[r].newpage)
1388 << write_attribute("caption", row_info[r].caption)
1390 for (col_type c = 0; c < ncols(); ++c) {
1392 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1393 << write_attribute("multirow", cell_info[r][c].multirow)
1394 << write_attribute("alignment", cell_info[r][c].alignment)
1395 << write_attribute("valignment", cell_info[r][c].valignment)
1396 << write_attribute("topline", cell_info[r][c].top_line)
1397 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1398 << write_attribute("leftline", cell_info[r][c].left_line)
1399 << write_attribute("rightline", cell_info[r][c].right_line)
1400 << write_attribute("rotate", cell_info[r][c].rotate)
1401 << write_attribute("usebox", cell_info[r][c].usebox)
1402 << write_attribute("width", cell_info[r][c].p_width)
1403 << write_attribute("special", cell_info[r][c].align_special)
1405 os << "\\begin_inset ";
1406 cell_info[r][c].inset->write(os);
1407 os << "\n\\end_inset\n"
1412 os << "</lyxtabular>\n";
1416 void Tabular::read(Lexer & lex)
1419 istream & is = lex.getStream();
1421 l_getline(is, line);
1422 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1423 LASSERT(false, /**/);
1428 if (!getTokenValue(line, "version", version))
1430 LASSERT(version >= 2, /**/);
1433 if (!getTokenValue(line, "rows", rows_arg))
1436 if (!getTokenValue(line, "columns", columns_arg))
1438 init(buffer_, rows_arg, columns_arg);
1439 l_getline(is, line);
1440 if (!prefixIs(line, "<features")) {
1441 lyxerr << "Wrong tabular format (expected <features ...> got"
1442 << line << ')' << endl;
1445 getTokenValue(line, "rotate", rotate);
1446 getTokenValue(line, "booktabs", use_booktabs);
1447 getTokenValue(line, "islongtable", is_long_tabular);
1448 getTokenValue(line, "tabularvalignment", tabular_valignment);
1449 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1450 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1451 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1452 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1453 getTokenValue(line, "headTopDL", endhead.topDL);
1454 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1455 getTokenValue(line, "footTopDL", endfoot.topDL);
1456 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1457 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1458 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1459 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1461 for (col_type c = 0; c < ncols(); ++c) {
1463 if (!prefixIs(line,"<column")) {
1464 lyxerr << "Wrong tabular format (expected <column ...> got"
1465 << line << ')' << endl;
1468 getTokenValue(line, "alignment", column_info[c].alignment);
1469 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1470 getTokenValue(line, "valignment", column_info[c].valignment);
1471 getTokenValue(line, "width", column_info[c].p_width);
1472 getTokenValue(line, "special", column_info[c].align_special);
1475 for (row_type i = 0; i < nrows(); ++i) {
1476 l_getline(is, line);
1477 if (!prefixIs(line, "<row")) {
1478 lyxerr << "Wrong tabular format (expected <row ...> got"
1479 << line << ')' << endl;
1482 getTokenValue(line, "topspace", row_info[i].top_space,
1483 row_info[i].top_space_default);
1484 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1485 row_info[i].bottom_space_default);
1486 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1487 row_info[i].interline_space_default);
1488 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1489 getTokenValue(line, "endhead", row_info[i].endhead);
1490 getTokenValue(line, "endfoot", row_info[i].endfoot);
1491 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1492 getTokenValue(line, "newpage", row_info[i].newpage);
1493 getTokenValue(line, "caption", row_info[i].caption);
1494 for (col_type j = 0; j < ncols(); ++j) {
1495 l_getline(is, line);
1496 if (!prefixIs(line, "<cell")) {
1497 lyxerr << "Wrong tabular format (expected <cell ...> got"
1498 << line << ')' << endl;
1501 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1502 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1503 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1504 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1505 getTokenValue(line, "topline", cell_info[i][j].top_line);
1506 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1507 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1508 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1509 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1510 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1511 getTokenValue(line, "width", cell_info[i][j].p_width);
1513 getTokenValue(line, "special", cell_info[i][j].align_special);
1514 l_getline(is, line);
1515 if (prefixIs(line, "\\begin_inset")) {
1516 cell_info[i][j].inset->setBuffer(*buffer_);
1517 cell_info[i][j].inset->read(lex);
1518 l_getline(is, line);
1520 if (!prefixIs(line, "</cell>")) {
1521 lyxerr << "Wrong tabular format (expected </cell> got"
1522 << line << ')' << endl;
1526 l_getline(is, line);
1527 if (!prefixIs(line, "</row>")) {
1528 lyxerr << "Wrong tabular format (expected </row> got"
1529 << line << ')' << endl;
1533 while (!prefixIs(line, "</lyxtabular>")) {
1534 l_getline(is, line);
1540 bool Tabular::isMultiColumn(idx_type cell) const
1542 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1543 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1547 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1549 return cell_info[cellRow(cell)][cellColumn(cell)];
1553 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1555 idx_type const col = cellColumn(cell);
1556 idx_type const row = cellRow(cell);
1557 for (idx_type i = 0; i < number; ++i)
1558 unsetMultiRow(cellIndex(row, col + i));
1560 // unsetting of multirow may have invalidated cell index
1561 cell = cellIndex(row, col);
1562 CellData & cs = cellInfo(cell);
1563 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1564 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1565 cs.alignment = column_info[col].alignment;
1567 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1569 for (idx_type i = 1; i < number; ++i) {
1570 CellData & cs1 = cellInfo(cell + i);
1571 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1572 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1580 bool Tabular::isMultiRow(idx_type cell) const
1582 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1583 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1587 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1589 idx_type const col = cellColumn(cell);
1590 idx_type const row = cellRow(cell);
1591 for (idx_type i = 0; i < number; ++i)
1592 unsetMultiColumn(cellIndex(row + i, col));
1594 // unsetting of multicol may have invalidated cell index
1595 cell = cellIndex(row, col);
1596 CellData & cs = cellInfo(cell);
1597 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1598 cs.valignment = LYX_VALIGN_MIDDLE;
1599 // FIXME: the horizontal alignment of multirow cells can only
1600 // be changed for the whole table,
1601 // support for this needs to be implemented but us a fileformat
1602 // change (assigning this to uwestoehr)
1603 // until LyX supports this, use the deault alignment of multirow
1605 cs.alignment = LYX_ALIGN_CENTER;
1607 // set the bottom row of the last selected cell
1608 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1610 for (idx_type i = 1; i < number; ++i) {
1611 CellData & cs1 = cell_info[row + i][col];
1612 cs1.multirow = CELL_PART_OF_MULTIROW;
1613 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1621 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1623 row_type const row = cellRow(cell);
1624 col_type const col = cellColumn(cell);
1626 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1633 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1635 col_type const column = cellColumn(cell);
1636 col_type row = cellRow(cell) + 1;
1637 while (row < nrows() && isPartOfMultiRow(row, column))
1640 return row - cellRow(cell);
1644 void Tabular::unsetMultiColumn(idx_type cell)
1646 if (!isMultiColumn(cell))
1649 row_type const row = cellRow(cell);
1650 col_type const col = cellColumn(cell);
1651 row_type const span = columnSpan(cell);
1652 for (col_type c = 0; c < span; ++c)
1653 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1658 void Tabular::unsetMultiRow(idx_type cell)
1660 if (!isMultiRow(cell))
1663 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1664 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1665 row_type const row = cellRow(cell);
1666 col_type const col = cellColumn(cell);
1667 row_type const span = rowSpan(cell);
1668 for (row_type r = 0; r < span; ++r)
1669 cell_info[row + r][col].multirow = CELL_NORMAL;
1674 void Tabular::setRotateCell(idx_type cell, bool flag)
1676 cellInfo(cell).rotate = flag;
1680 bool Tabular::getRotateCell(idx_type cell) const
1682 return cellInfo(cell).rotate;
1686 bool Tabular::needRotating() const
1690 for (row_type r = 0; r < nrows(); ++r)
1691 for (col_type c = 0; c < ncols(); ++c)
1692 if (cell_info[r][c].rotate)
1698 bool Tabular::isLastCell(idx_type cell) const
1700 if (cell + 1 < numberofcells)
1706 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1708 if (cellRow(cell) == 0)
1711 col_type const col = cellColumn(cell);
1712 row_type r = cellRow(cell) - 1;
1713 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1716 return cell_info[r][col].cellno;
1720 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1722 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1723 if (nextrow < nrows())
1724 return cell_info[nextrow][cellColumn(cell)].cellno;
1729 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1731 LASSERT(column != npos && column < ncols()
1732 && row != npos && row < nrows(), /**/);
1733 return cell_info[row][column].cellno;
1737 void Tabular::setUsebox(idx_type cell, BoxType type)
1739 cellInfo(cell).usebox = type;
1743 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1744 // adds line breaks, see bug 4886.
1745 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1747 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1748 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1750 if (cellInfo(cell).usebox > 1)
1751 return cellInfo(cell).usebox;
1752 return useParbox(cell);
1757 // This are functions used for the longtable support
1759 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1765 row_info[row].endfirsthead = flag;
1769 row_info[row].endhead = flag;
1774 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1777 hd.set = haveLTHead();
1778 return row_info[row].endhead;
1782 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1785 hd.set = haveLTFirstHead();
1786 return row_info[row].endfirsthead;
1790 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1796 row_info[row].endlastfoot = flag;
1800 row_info[row].endfoot = flag;
1805 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1808 fd.set = haveLTFoot();
1809 return row_info[row].endfoot;
1813 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1816 fd.set = haveLTLastFoot();
1817 return row_info[row].endlastfoot;
1821 void Tabular::setLTNewPage(row_type row, bool what)
1823 row_info[row].newpage = what;
1827 bool Tabular::getLTNewPage(row_type row) const
1829 return row_info[row].newpage;
1833 bool Tabular::haveLTHead() const
1835 for (row_type i = 0; i < nrows(); ++i)
1836 if (row_info[i].endhead)
1842 bool Tabular::haveLTFirstHead() const
1844 if (endfirsthead.empty)
1846 for (row_type r = 0; r < nrows(); ++r)
1847 if (row_info[r].endfirsthead)
1853 bool Tabular::haveLTFoot() const
1855 for (row_type r = 0; r < nrows(); ++r)
1856 if (row_info[r].endfoot)
1862 bool Tabular::haveLTLastFoot() const
1864 if (endlastfoot.empty)
1866 for (row_type r = 0; r < nrows(); ++r)
1867 if (row_info[r].endlastfoot)
1873 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1875 idx_type i = getFirstCellInRow(row);
1877 setMultiColumn(i, numberOfCellsInRow(row));
1878 setTopLine(i, false);
1879 setBottomLine(i, false);
1880 setLeftLine(i, false);
1881 setRightLine(i, false);
1883 unsetMultiColumn(i);
1884 // When unsetting a caption row, also all existing
1885 // captions in this row must be dissolved.
1887 row_info[row].caption = what;
1892 bool Tabular::ltCaption(row_type row) const
1894 return row_info[row].caption;
1898 bool Tabular::haveLTCaption() const
1900 for (row_type r = 0; r < nrows(); ++r)
1901 if (row_info[r].caption)
1907 // end longtable support functions
1909 void Tabular::setRowAscent(row_type row, int height)
1911 if (row >= nrows() || row_info[row].ascent == height)
1913 row_info[row].ascent = height;
1917 void Tabular::setRowDescent(row_type row, int height)
1919 if (row >= nrows() || row_info[row].descent == height)
1921 row_info[row].descent = height;
1925 int Tabular::rowAscent(row_type row) const
1927 LASSERT(row < nrows(), /**/);
1928 return row_info[row].ascent;
1932 int Tabular::rowDescent(row_type row) const
1934 LASSERT(row < nrows(), /**/);
1935 return row_info[row].descent;
1939 int Tabular::height() const
1942 for (row_type row = 0; row < nrows(); ++row)
1943 height += rowAscent(row) + rowDescent(row) +
1949 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1951 LASSERT(row < nrows(), /**/);
1952 LASSERT(column < ncols(), /**/);
1953 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1957 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1959 LASSERT(row < nrows(), /**/);
1960 LASSERT(column < ncols(), /**/);
1961 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1965 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1967 // we only output complete row lines and the 1st row here, the rest
1968 // is done in Tabular::TeXBottomHLine(...)
1970 // get for each column the topline (if any)
1971 vector<bool> topline;
1973 for (col_type c = 0; c < ncols(); ++c) {
1974 topline.push_back(topLine(cellIndex(row, c)));
1975 // If cell is part of a multirow and not the first cell of the
1976 // multirow, no line must be drawn.
1978 if (isMultiRow(cellIndex(row, c))
1979 && isMultiRow(cellIndex(row - 1, c)))
1985 // do nothing if empty first row, or incomplete row line after
1986 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
1989 // only output complete row lines and the 1st row's clines
1990 if (nset == ncols()) {
1992 os << (row == 0 ? "\\toprule " : "\\midrule ");
1996 } else if (row == 0) {
1997 for (col_type c = 0; c < ncols(); ++c) {
1999 col_type offset = 0;
2000 for (col_type j = 0 ; j < c; ++j)
2001 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2004 //babel makes the "-" character an active one, so we have to suppress this here
2005 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2006 if (lang == "slovak" || lang == "czech")
2007 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2008 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2010 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2012 col_type cstart = c;
2013 for ( ; c < ncols() && topline[c]; ++c) {}
2015 for (col_type j = cstart ; j < c ; ++j)
2016 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2019 os << c + offset << "} ";
2028 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2030 // we output bottomlines of row r and the toplines of row r+1
2031 // if the latter do not span the whole tabular
2033 // get the bottomlines of row r, and toplines in next row
2034 bool lastrow = row == nrows() - 1;
2035 vector<bool> bottomline, topline;
2036 bool nextrowset = true;
2037 for (col_type c = 0; c < ncols(); ++c) {
2038 bottomline.push_back(bottomLine(cellIndex(row, c)));
2039 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2040 // If cell is part of a multirow and not the last or first cell of the
2041 // multirow, no line must be drawn.
2043 if (isMultiRow(cellIndex(row, c))
2044 && isMultiRow(cellIndex(row + 1, c))) {
2045 bottomline[c] = false;
2048 nextrowset &= topline[c];
2051 // combine this row's bottom lines and next row's toplines if necessary
2053 for (col_type c = 0; c < ncols(); ++c) {
2055 bottomline[c] = bottomline[c] || topline[c];
2060 // do nothing if empty, OR incomplete row line with a topline in next row
2061 if (nset == 0 || (nextrowset && nset != ncols()))
2064 if (nset == ncols()) {
2066 os << (lastrow ? "\\bottomrule" : "\\midrule");
2070 for (col_type c = 0; c < ncols(); ++c) {
2071 if (bottomline[c]) {
2072 col_type offset = 0;
2073 for (col_type j = 0 ; j < c; ++j)
2074 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2077 //babel makes the "-" character an active one, so we have to suppress this here
2078 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2079 if (lang == "slovak" || lang == "czech")
2080 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2081 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2083 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2085 col_type cstart = c;
2086 for ( ; c < ncols() && bottomline[c]; ++c) {}
2088 for (col_type j = cstart ; j < c ; ++j)
2089 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2092 os << c + offset << "} ";
2101 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2102 bool & ismulticol, bool & ismultirow) const
2105 row_type const r = cellRow(cell);
2106 if (is_long_tabular && row_info[r].caption)
2109 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2110 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2111 // figure out how to set the lines
2112 // we always set double lines to the right of the cell
2113 col_type const c = cellColumn(cell);
2114 col_type const nextcol = c + columnSpan(cell);
2115 bool colright = columnRightLine(c);
2116 bool colleft = columnLeftLine(c);
2117 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2118 bool nextcellleft = nextcol < ncols()
2119 && leftLine(cellIndex(r, nextcol));
2120 bool coldouble = colright && nextcolleft;
2121 bool celldouble = rightLine(cell) && nextcellleft;
2123 ismulticol = isMultiColumn(cell)
2124 || (c == 0 && colleft != leftLine(cell))
2125 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2126 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2127 || (coldouble != celldouble);
2129 // we center in multicol when no decimal point
2130 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2131 && (cellInfo(cell).decimal_width == 0));
2133 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2134 int latexcolspan = columnSpan(cell);
2135 for(col_type col = c; col < c + columnSpan(cell); ++col)
2136 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2140 os << "\\multicolumn{" << latexcolspan << "}{";
2141 if (c ==0 && leftLine(cell))
2143 if (!cellInfo(cell).align_special.empty()) {
2144 os << cellInfo(cell).align_special;
2146 if (!getPWidth(cell).zero()) {
2148 case LYX_ALIGN_LEFT:
2149 os << ">{\\raggedright}";
2151 case LYX_ALIGN_RIGHT:
2152 os << ">{\\raggedleft}";
2154 case LYX_ALIGN_CENTER:
2155 os << ">{\\centering}";
2161 case LYX_VALIGN_TOP:
2164 case LYX_VALIGN_MIDDLE:
2167 case LYX_VALIGN_BOTTOM:
2172 << from_ascii(getPWidth(cell).asLatexString())
2176 case LYX_ALIGN_LEFT:
2179 case LYX_ALIGN_RIGHT:
2186 } // end if else !getPWidth
2187 } // end if else !cellinfo_of_cell
2188 if (rightLine(cell) || nextcellleft)
2191 // add extra vertical line if we want a double one
2194 } // end if ismulticol
2196 // we only need code for the first multirow cell
2197 ismultirow = isMultiRow(cell);
2199 os << "\\multirow{" << rowSpan(cell) << "}{";
2200 if (!getPWidth(cell).zero())
2201 os << from_ascii(getPWidth(cell).asLatexString());
2203 // we need to set a default value
2204 // needs to be discussed
2207 } // end if ismultirow
2209 if (getRotateCell(cell)) {
2210 os << "\\begin{sideways}\n";
2213 if (getUsebox(cell) == BOX_PARBOX) {
2216 case LYX_VALIGN_TOP:
2219 case LYX_VALIGN_MIDDLE:
2222 case LYX_VALIGN_BOTTOM:
2226 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2228 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2229 os << "\\begin{minipage}[";
2231 case LYX_VALIGN_TOP:
2234 case LYX_VALIGN_MIDDLE:
2237 case LYX_VALIGN_BOTTOM:
2241 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2249 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2250 bool ismulticol, bool ismultirow) const
2253 row_type const r = cellRow(cell);
2254 if (is_long_tabular && row_info[r].caption)
2258 if (getUsebox(cell) == BOX_PARBOX)
2260 else if (getUsebox(cell) == BOX_MINIPAGE) {
2261 os << "%\n\\end{minipage}";
2264 if (getRotateCell(cell)) {
2265 os << "%\n\\end{sideways}";
2277 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2278 OutputParams const & runparams) const
2280 if (!is_long_tabular)
2285 // the caption must be output before the headers
2286 if (haveLTCaption()) {
2287 for (row_type r = 0; r < nrows(); ++r) {
2288 if (row_info[r].caption) {
2289 ret += TeXRow(os, r, runparams);
2293 // output first header info
2294 // first header must be output before the header, otherwise the
2295 // correct caption placement becomes really weird
2296 if (haveLTFirstHead()) {
2297 if (endfirsthead.topDL) {
2301 for (row_type r = 0; r < nrows(); ++r) {
2302 if (row_info[r].endfirsthead) {
2303 ret += TeXRow(os, r, runparams);
2306 if (endfirsthead.bottomDL) {
2310 os << "\\endfirsthead\n";
2313 // output header info
2315 if (endfirsthead.empty && !haveLTFirstHead()) {
2316 os << "\\endfirsthead\n";
2319 if (endhead.topDL) {
2323 for (row_type r = 0; r < nrows(); ++r) {
2324 if (row_info[r].endhead) {
2325 ret += TeXRow(os, r, runparams);
2328 if (endhead.bottomDL) {
2332 os << "\\endhead\n";
2335 // output footer info
2337 if (endfoot.topDL) {
2341 for (row_type r = 0; r < nrows(); ++r) {
2342 if (row_info[r].endfoot) {
2343 ret += TeXRow(os, r, runparams);
2346 if (endfoot.bottomDL) {
2350 os << "\\endfoot\n";
2352 if (endlastfoot.empty && !haveLTLastFoot()) {
2353 os << "\\endlastfoot\n";
2357 // output lastfooter info
2358 if (haveLTLastFoot()) {
2359 if (endlastfoot.topDL) {
2363 for (row_type r = 0; r < nrows(); ++r) {
2364 if (row_info[r].endlastfoot) {
2365 ret += TeXRow(os, r, runparams);
2368 if (endlastfoot.bottomDL) {
2372 os << "\\endlastfoot\n";
2379 bool Tabular::isValidRow(row_type row) const
2381 if (!is_long_tabular)
2383 return !row_info[row].endhead && !row_info[row].endfirsthead
2384 && !row_info[row].endfoot && !row_info[row].endlastfoot
2385 && !row_info[row].caption;
2389 int Tabular::TeXRow(odocstream & os, row_type row,
2390 OutputParams const & runparams) const
2392 idx_type cell = cellIndex(row, 0);
2393 shared_ptr<InsetTableCell> inset = cellInset(cell);
2394 Paragraph const & par = inset->paragraphs().front();
2395 string const lang = par.getParLanguage(buffer().params())->lang();
2397 //output the top line
2398 int ret = TeXTopHLine(os, row, lang);
2400 if (row_info[row].top_space_default) {
2402 os << "\\addlinespace\n";
2404 os << "\\noalign{\\vskip\\doublerulesep}\n";
2406 } else if(!row_info[row].top_space.zero()) {
2408 os << "\\addlinespace["
2409 << from_ascii(row_info[row].top_space.asLatexString())
2412 os << "\\noalign{\\vskip"
2413 << from_ascii(row_info[row].top_space.asLatexString())
2418 bool ismulticol = false;
2419 bool ismultirow = false;
2420 for (col_type c = 0; c < ncols(); ++c) {
2421 if (isPartOfMultiColumn(row, c))
2424 if (isPartOfMultiRow(row, c) &&
2425 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2430 cell = cellIndex(row, c);
2431 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2432 shared_ptr<InsetTableCell> inset = cellInset(cell);
2434 Paragraph const & par = inset->paragraphs().front();
2435 bool rtl = par.isRTL(buffer().params())
2437 && getPWidth(cell).zero();
2441 par.getParLanguage(buffer().params())->lang();
2442 if (lang == "farsi")
2444 else if (lang == "arabic_arabi")
2446 // currently, remaning RTL languages are
2447 // arabic_arabtex and hebrew
2451 // pass to the OutputParams that we are in a cell and
2452 // which alignment we have set.
2453 // InsetNewline needs this context information.
2454 OutputParams newrp(runparams);
2455 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2456 ? OutputParams::PLAIN
2457 : OutputParams::ALIGNED;
2459 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2460 && cellInfo(cell).decimal_width != 0) {
2461 // copy cell and split in 2
2462 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2463 head.getText(0)->setMacrocontextPosition(
2464 cellInset(cell)->getText(0)->macrocontextPosition());
2465 head.setBuffer(buffer());
2466 bool hassep = false;
2467 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2468 head.latex(os, newrp);
2470 ret += tail.latex(os, newrp);
2471 } else if (!isPartOfMultiRow(row, c))
2472 ret += inset->latex(os, newrp);
2474 runparams.encoding = newrp.encoding;
2478 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2479 if (cell != getLastCellInRow(row)) { // not last cell in row
2483 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2484 // if no first header and no empty first header is used,
2485 // the caption needs to be terminated by \endfirsthead
2487 os << "\\endfirsthead";
2489 os << "\\tabularnewline";
2490 if (row_info[row].bottom_space_default) {
2492 os << "\\addlinespace";
2494 os << "[\\doublerulesep]";
2495 } else if (!row_info[row].bottom_space.zero()) {
2497 os << "\\addlinespace";
2499 << from_ascii(row_info[row].bottom_space.asLatexString())
2505 //output the bottom line
2506 ret += TeXBottomHLine(os, row, lang);
2508 if (row_info[row].interline_space_default) {
2510 os << "\\addlinespace\n";
2512 os << "\\noalign{\\vskip\\doublerulesep}\n";
2514 } else if (!row_info[row].interline_space.zero()) {
2516 os << "\\addlinespace["
2517 << from_ascii(row_info[row].interline_space.asLatexString())
2520 os << "\\noalign{\\vskip"
2521 << from_ascii(row_info[row].interline_space.asLatexString())
2529 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2533 //+---------------------------------------------------------------------
2534 //+ first the opening preamble +
2535 //+---------------------------------------------------------------------
2538 os << "\\begin{sideways}\n";
2541 if (is_long_tabular) {
2542 os << "\\begin{longtable}";
2543 switch (longtabular_alignment) {
2544 case LYX_LONGTABULAR_ALIGN_LEFT:
2547 case LYX_LONGTABULAR_ALIGN_CENTER:
2549 case LYX_LONGTABULAR_ALIGN_RIGHT:
2554 os << "\\begin{tabular}";
2555 switch (tabular_valignment) {
2556 case LYX_VALIGN_TOP:
2559 case LYX_VALIGN_MIDDLE:
2561 case LYX_VALIGN_BOTTOM:
2569 for (col_type c = 0; c < ncols(); ++c) {
2570 if (columnLeftLine(c))
2572 if (!column_info[c].align_special.empty()) {
2573 os << column_info[c].align_special;
2575 if (!column_info[c].p_width.zero()) {
2576 switch (column_info[c].alignment) {
2577 case LYX_ALIGN_LEFT:
2578 os << ">{\\raggedright}";
2580 case LYX_ALIGN_RIGHT:
2581 os << ">{\\raggedleft}";
2583 case LYX_ALIGN_CENTER:
2584 os << ">{\\centering}";
2586 case LYX_ALIGN_NONE:
2587 case LYX_ALIGN_BLOCK:
2588 case LYX_ALIGN_LAYOUT:
2589 case LYX_ALIGN_SPECIAL:
2590 case LYX_ALIGN_DECIMAL:
2594 switch (column_info[c].valignment) {
2595 case LYX_VALIGN_TOP:
2598 case LYX_VALIGN_MIDDLE:
2601 case LYX_VALIGN_BOTTOM:
2606 << from_ascii(column_info[c].p_width.asLatexString())
2609 switch (column_info[c].alignment) {
2610 case LYX_ALIGN_LEFT:
2613 case LYX_ALIGN_RIGHT:
2616 case LYX_ALIGN_DECIMAL:
2617 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2623 } // end if else !column_info[i].p_width
2624 } // end if else !column_info[i].align_special
2625 if (columnRightLine(c))
2631 ret += TeXLongtableHeaderFooter(os, runparams);
2633 //+---------------------------------------------------------------------
2634 //+ the single row and columns (cells) +
2635 //+---------------------------------------------------------------------
2637 for (row_type r = 0; r < nrows(); ++r) {
2638 if (isValidRow(r)) {
2639 ret += TeXRow(os, r, runparams);
2640 if (is_long_tabular && row_info[r].newpage) {
2641 os << "\\newpage\n";
2647 //+---------------------------------------------------------------------
2648 //+ the closing of the tabular +
2649 //+---------------------------------------------------------------------
2651 if (is_long_tabular)
2652 os << "\\end{longtable}";
2654 os << "\\end{tabular}";
2656 os << "\n\\end{sideways}";
2664 int Tabular::docbookRow(odocstream & os, row_type row,
2665 OutputParams const & runparams) const
2668 idx_type cell = getFirstCellInRow(row);
2671 for (col_type c = 0; c < ncols(); ++c) {
2672 if (isPartOfMultiColumn(row, c))
2675 os << "<entry align=\"";
2676 switch (getAlignment(cell)) {
2677 case LYX_ALIGN_LEFT:
2680 case LYX_ALIGN_RIGHT:
2688 os << "\" valign=\"";
2689 switch (getVAlignment(cell)) {
2690 case LYX_VALIGN_TOP:
2693 case LYX_VALIGN_BOTTOM:
2696 case LYX_VALIGN_MIDDLE:
2701 if (isMultiColumn(cell)) {
2702 os << " namest=\"col" << c << "\" ";
2703 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2707 ret += cellInset(cell)->docbook(os, runparams);
2716 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2720 //+---------------------------------------------------------------------
2721 //+ first the opening preamble +
2722 //+---------------------------------------------------------------------
2724 os << "<tgroup cols=\"" << ncols()
2725 << "\" colsep=\"1\" rowsep=\"1\">\n";
2727 for (col_type c = 0; c < ncols(); ++c) {
2728 os << "<colspec colname=\"col" << c << "\" align=\"";
2729 switch (column_info[c].alignment) {
2730 case LYX_ALIGN_LEFT:
2733 case LYX_ALIGN_RIGHT:
2741 if (runparams.flavor == OutputParams::XML)
2747 //+---------------------------------------------------------------------
2748 //+ Long Tabular case +
2749 //+---------------------------------------------------------------------
2751 // output caption info
2752 if (haveLTCaption()) {
2753 os << "<caption>\n";
2755 for (row_type r = 0; r < nrows(); ++r) {
2756 if (row_info[r].caption) {
2757 ret += docbookRow(os, r, runparams);
2760 os << "</caption>\n";
2763 // output header info
2764 if (haveLTHead() || haveLTFirstHead()) {
2767 for (row_type r = 0; r < nrows(); ++r) {
2768 if (row_info[r].endhead || row_info[r].endfirsthead) {
2769 ret += docbookRow(os, r, runparams);
2775 // output footer info
2776 if (haveLTFoot() || haveLTLastFoot()) {
2779 for (row_type r = 0; r < nrows(); ++r) {
2780 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2781 ret += docbookRow(os, r, runparams);
2788 //+---------------------------------------------------------------------
2789 //+ the single row and columns (cells) +
2790 //+---------------------------------------------------------------------
2794 for (row_type r = 0; r < nrows(); ++r) {
2795 if (isValidRow(r)) {
2796 ret += docbookRow(os, r, runparams);
2801 //+---------------------------------------------------------------------
2802 //+ the closing of the tabular +
2803 //+---------------------------------------------------------------------
2812 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2813 OutputParams const & runparams) const
2816 idx_type cell = getFirstCellInRow(row);
2818 xs << html::StartTag("tr");
2819 for (col_type c = 0; c < ncols(); ++c) {
2820 if (isPartOfMultiColumn(row, c))
2825 switch (getAlignment(cell)) {
2826 case LYX_ALIGN_LEFT:
2829 case LYX_ALIGN_RIGHT:
2837 attr << " valign='";
2838 switch (getVAlignment(cell)) {
2839 case LYX_VALIGN_TOP:
2842 case LYX_VALIGN_BOTTOM:
2845 case LYX_VALIGN_MIDDLE:
2850 if (isMultiColumn(cell))
2851 attr << " colspan='" << columnSpan(cell) << "'";
2853 xs << html::StartTag("td", attr.str());
2854 ret += cellInset(cell)->xhtml(xs, runparams);
2855 xs << html::EndTag("td");
2858 xs << html::EndTag("tr");
2863 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2866 // It's unclear to me if we need to mess with the long table stuff.
2867 // We can borrow that too from docbook, if so.
2869 xs << html::StartTag("tbody");
2870 for (row_type r = 0; r < nrows(); ++r) {
2871 if (isValidRow(r)) {
2872 ret += xhtmlRow(xs, r, runparams);
2875 xs << html::EndTag("tbody");
2880 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2881 vector<unsigned int> const & clen) const
2883 idx_type const fcell = getFirstCellInRow(row);
2884 idx_type const n = numberOfCellsInRow(row) + fcell;
2887 for (idx_type i = fcell; i < n; ++i) {
2897 for (idx_type i = fcell; i < n; ++i) {
2908 col_type column = cellColumn(i);
2909 int len = clen[column];
2910 while (column < ncols() - 1
2911 && isPartOfMultiColumn(row, ++column))
2912 len += clen[column] + 4;
2913 os << docstring(len, ch);
2928 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2929 vector<unsigned int> const & clen) const
2931 idx_type const fcell = getFirstCellInRow(row);
2932 idx_type const n = numberOfCellsInRow(row) + fcell;
2935 for (idx_type i = fcell; i < n; ++i) {
2936 if (bottomLine(i)) {
2945 for (idx_type i = fcell; i < n; ++i) {
2946 if (bottomLine(i)) {
2956 col_type column = cellColumn(i);
2957 int len = clen[column];
2958 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2959 len += clen[column] + 4;
2960 os << docstring(len, ch);
2961 if (bottomLine(i)) {
2975 void Tabular::plaintextPrintCell(odocstream & os,
2976 OutputParams const & runparams,
2977 idx_type cell, row_type row, col_type column,
2978 vector<unsigned int> const & clen,
2979 bool onlydata) const
2981 odocstringstream sstr;
2982 cellInset(cell)->plaintext(sstr, runparams);
2994 unsigned int len1 = sstr.str().length();
2995 unsigned int len2 = clen[column];
2996 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2997 len2 += clen[column] + 4;
3000 switch (getAlignment(cell)) {
3002 case LYX_ALIGN_LEFT:
3005 case LYX_ALIGN_RIGHT:
3009 case LYX_ALIGN_CENTER:
3015 os << docstring(len1, ' ') << sstr.str()
3016 << docstring(len2, ' ');
3018 if (rightLine(cell))
3025 void Tabular::plaintext(odocstream & os,
3026 OutputParams const & runparams, int const depth,
3027 bool onlydata, char_type delim) const
3029 // first calculate the width of the single columns
3030 vector<unsigned int> clen(ncols());
3033 // first all non multicolumn cells!
3034 for (col_type c = 0; c < ncols(); ++c) {
3036 for (row_type r = 0; r < nrows(); ++r) {
3037 idx_type cell = cellIndex(r, c);
3038 if (isMultiColumn(cell))
3040 odocstringstream sstr;
3041 cellInset(cell)->plaintext(sstr, runparams);
3042 if (clen[c] < sstr.str().length())
3043 clen[c] = sstr.str().length();
3046 // then all multicolumn cells!
3047 for (col_type c = 0; c < ncols(); ++c) {
3048 for (row_type r = 0; r < nrows(); ++r) {
3049 idx_type cell = cellIndex(r, c);
3050 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3052 odocstringstream sstr;
3053 cellInset(cell)->plaintext(sstr, runparams);
3054 int len = int(sstr.str().length());
3055 idx_type const n = columnSpan(cell);
3056 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3058 if (len > int(clen[c + n - 1]))
3059 clen[c + n - 1] = len;
3064 for (row_type r = 0; r < nrows(); ++r) {
3065 if (!onlydata && plaintextTopHLine(os, r, clen))
3066 os << docstring(depth * 2, ' ');
3067 for (col_type c = 0; c < ncols(); ++c) {
3068 if (isPartOfMultiColumn(r, c))
3070 if (onlydata && c > 0)
3071 // we don't use operator<< for single UCS4 character.
3072 // see explanation in docstream.h
3074 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3079 os << docstring(depth * 2, ' ');
3080 if (plaintextBottomHLine(os, r, clen))
3081 os << docstring(depth * 2, ' ');
3087 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3089 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3093 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3094 col_type column) const
3096 return cell_info[row][column].inset;
3100 void Tabular::setCellInset(row_type row, col_type column,
3101 shared_ptr<InsetTableCell> ins) const
3103 CellData & cd = cell_info[row][column];
3108 void Tabular::validate(LaTeXFeatures & features) const
3110 features.require("NeedTabularnewline");
3112 features.require("booktabs");
3113 if (is_long_tabular)
3114 features.require("longtable");
3116 features.require("rotating");
3117 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3118 if (isMultiRow(cell))
3119 features.require("multirow");
3120 if (getVAlignment(cell) != LYX_VALIGN_TOP
3121 || !getPWidth(cell).zero())
3122 features.require("array");
3123 cellInset(cell)->validate(features);
3128 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3130 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3131 ParagraphList::const_iterator cit = parlist.begin();
3132 ParagraphList::const_iterator end = parlist.end();
3134 for (; cit != end; ++cit)
3135 for (int i = 0; i < cit->size(); ++i)
3136 if (cit->isNewline(i))
3143 /////////////////////////////////////////////////////////////////////
3147 /////////////////////////////////////////////////////////////////////
3149 InsetTableCell::InsetTableCell(Buffer * buf)
3150 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3151 contentAlign(LYX_ALIGN_CENTER)
3155 bool InsetTableCell::forcePlainLayout(idx_type) const
3157 return !isFixedWidth;
3161 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3163 return isFixedWidth;
3167 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3168 FuncStatus & status) const
3171 switch (cmd.action()) {
3173 enabled = !forcePlainLayout();
3175 case LFUN_LAYOUT_PARAGRAPH:
3176 enabled = allowParagraphCustomization();
3179 return InsetText::getStatus(cur, cmd, status);
3181 status.setEnabled(enabled);
3185 docstring InsetTableCell::asString(bool intoInsets)
3188 if (paragraphs().empty())
3190 ParagraphList::const_iterator it = paragraphs().begin();
3191 ParagraphList::const_iterator en = paragraphs().end();
3193 for (; it != en; ++it) {
3198 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3204 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3207 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3208 return InsetText::xhtml(xs, rp);
3213 /////////////////////////////////////////////////////////////////////
3217 /////////////////////////////////////////////////////////////////////
3219 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3221 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3222 rowselect_(false), colselect_(false)
3227 InsetTabular::InsetTabular(InsetTabular const & tab)
3228 : Inset(tab), tabular(tab.tabular), scx_(0)
3233 InsetTabular::~InsetTabular()
3235 hideDialogs("tabular", this);
3239 void InsetTabular::setBuffer(Buffer & buf)
3241 tabular.setBuffer(buf);
3242 Inset::setBuffer(buf);
3246 bool InsetTabular::insetAllowed(InsetCode code) const
3251 case MATHMACRO_CODE:
3256 return tabular.is_long_tabular;
3264 void InsetTabular::write(ostream & os) const
3266 os << "Tabular" << endl;
3271 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3273 // FIXME: depending on the selection state, we could offer a different menu.
3274 return from_ascii("context-tabular");
3278 void InsetTabular::read(Lexer & lex)
3280 //bool const old_format = (lex.getString() == "\\LyXTable");
3288 string token = lex.getString();
3289 while (lex && token != "\\end_inset") {
3291 token = lex.getString();
3294 lex.printError("Missing \\end_inset at this point. ");
3298 int InsetTabular::rowFromY(Cursor & cur, int y) const
3300 // top y coordinate of tabular
3301 int h = yo(cur.bv()) - tabular.rowAscent(0);
3303 for (; r < tabular.nrows() && y > h; ++r)
3304 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3305 + tabular.interRowSpace(r);
3311 int InsetTabular::columnFromX(Cursor & cur, int x) const
3313 // left x coordinate of tabular
3314 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3316 for (; c < tabular.ncols() && x > w; ++c)
3317 w += tabular.cellWidth(c);
3322 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3324 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3325 // mi.base.textwidth << "\n";
3328 LASSERT(false, /**/);
3331 for (row_type r = 0; r < tabular.nrows(); ++r) {
3334 for (col_type c = 0; c < tabular.ncols(); ++c) {
3335 if (tabular.isPartOfMultiColumn(r, c)
3336 || tabular.isPartOfMultiRow(r, c))
3337 // multicolumn or multirow cell, but not first one
3339 idx_type const cell = tabular.cellIndex(r, c);
3342 Length const p_width = tabular.getPWidth(cell);
3343 if (!p_width.zero())
3344 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3345 tabular.cellInset(cell)->metrics(m, dim);
3346 if (!p_width.zero())
3347 dim.wid = m.base.textwidth;
3348 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3349 + tabular.interColumnSpace(cell);
3351 // FIXME(?): do we need a second metrics call?
3352 TextMetrics const & tm =
3353 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3355 // determine horiz offset because of decimal align (if necessary)
3356 int decimal_hoffset = 0;
3357 int decimal_width = 0;
3358 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3359 // make a copy which we will split in 2
3360 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3361 head.getText(0)->setMacrocontextPosition(
3362 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3363 head.setBuffer(tabular.buffer());
3364 // split in 2 and calculate width of each part
3365 bool hassep = false;
3366 InsetTableCell tail =
3367 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3369 head.metrics(m, dim1);
3370 decimal_hoffset = dim1.width();
3372 tail.metrics(m, dim1);
3373 decimal_width = dim1.width();
3376 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3377 tabular.cell_info[r][c].decimal_width = decimal_width;
3379 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3380 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3381 int const lastpardes = tm.last().second->descent()
3382 + TEXT_TO_INSET_OFFSET;
3384 switch (tabular.getVAlignment(cell)) {
3385 case Tabular::LYX_VALIGN_TOP:
3387 case Tabular::LYX_VALIGN_MIDDLE:
3388 offset = -(dim.des - lastpardes)/2;
3390 case Tabular::LYX_VALIGN_BOTTOM:
3391 offset = -(dim.des - lastpardes);
3394 tabular.cell_info[r][c].voffset = offset;
3395 maxasc = max(maxasc, dim.asc - offset);
3396 maxdes = max(maxdes, dim.des + offset);
3398 int const top_space = tabular.row_info[r].top_space_default ?
3399 default_line_space :
3400 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3401 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3402 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3403 default_line_space :
3404 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3405 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3407 tabular.updateColumnWidths();
3408 dim.asc = tabular.rowAscent(0);
3409 dim.des = tabular.height() - dim.asc;
3410 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3414 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3417 if (&cur.inset() == this && cur.selection()) {
3418 if (cur.selIsMultiCell()) {
3421 getSelection(cur, rs, re, cs, ce);
3423 idx_type const cell = tabular.cellIndex(row, col);
3424 col_type const cspan = tabular.columnSpan(cell);
3425 row_type const rspan = tabular.rowSpan(cell);
3426 if (col + cspan - 1 >= cs && col <= ce
3427 && row + rspan - 1 >= rs && row <= re)
3430 if (col == tabular.cellColumn(cur.idx())
3431 && row == tabular.cellRow(cur.idx())) {
3432 CursorSlice const & beg = cur.selBegin();
3433 CursorSlice const & end = cur.selEnd();
3435 if ((end.lastpos() > 0 || end.lastpit() > 0)
3436 && end.pos() == end.lastpos() && beg.pos() == 0
3437 && end.pit() == end.lastpit() && beg.pit() == 0)
3445 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3447 x += scx_ + ADD_TO_TABULAR_WIDTH;
3449 BufferView * bv = pi.base.bv;
3450 Cursor & cur = pi.base.bv->cursor();
3453 // FIXME: As the full background is painted in drawSelection(),
3454 // we have no choice but to do a full repaint for the Text cells.
3455 pi.full_repaint = true;
3457 bool const original_selection_state = pi.selected;
3460 first_visible_cell = Tabular::npos;
3461 for (row_type r = 0; r < tabular.nrows(); ++r) {
3463 for (col_type c = 0; c < tabular.ncols(); ++c) {
3464 if (tabular.isPartOfMultiColumn(r, c))
3467 idx = tabular.cellIndex(r, c);
3469 if (tabular.isPartOfMultiRow(r, c)) {
3470 nx += tabular.cellWidth(idx);
3474 if (first_visible_cell == Tabular::npos)
3475 first_visible_cell = idx;
3477 pi.selected |= isCellSelected(cur, r, c);
3478 int const cx = nx + tabular.textHOffset(idx);
3479 int const cy = y + tabular.textVOffset(idx);
3480 // Cache the Inset position.
3481 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3482 cell(idx)->draw(pi, cx, cy);
3483 drawCellLines(pi, nx, y, r, idx);
3484 nx += tabular.cellWidth(idx);
3485 pi.selected = original_selection_state;
3488 if (r + 1 < tabular.nrows())
3489 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3490 + tabular.interRowSpace(r + 1);
3495 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3497 Cursor & cur = pi.base.bv->cursor();
3500 x += scx_ + ADD_TO_TABULAR_WIDTH;
3502 // FIXME: it is wrong to completely paint the background
3503 // if we want to do single row painting.
3505 // Paint background of current tabular
3506 int const w = tabular.width();
3507 int const h = tabular.height();
3508 int yy = y - tabular.rowAscent(0);
3509 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3511 if (!cur.selection())
3513 if (&cur.inset() != this)
3518 bool const full_cell_selected = isCellSelected(cur,
3519 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3521 if (cur.selIsMultiCell() || full_cell_selected) {
3522 for (row_type r = 0; r < tabular.nrows(); ++r) {
3524 for (col_type c = 0; c < tabular.ncols(); ++c) {
3525 if (tabular.isPartOfMultiColumn(r, c))
3528 idx_type const cell = tabular.cellIndex(r, c);
3530 if (tabular.isPartOfMultiRow(r, c)) {
3531 xx += tabular.cellWidth(cell);
3534 int const w = tabular.cellWidth(cell);
3535 int const h = tabular.cellHeight(cell);
3536 int const yy = y - tabular.rowAscent(r);
3537 if (isCellSelected(cur, r, c))
3538 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3541 if (r + 1 < tabular.nrows())
3542 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3543 + tabular.interRowSpace(r + 1);
3547 x += cellXPos(cur.idx());
3548 x += tabular.textHOffset(cur.idx());
3549 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3554 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3555 row_type row, idx_type cell) const
3557 y -= tabular.rowAscent(row);
3558 int const w = tabular.cellWidth(cell);
3559 int const h = tabular.cellHeight(cell);
3560 Color const linecolor = pi.textColor(Color_tabularline);
3561 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3564 bool drawline = tabular.topLine(cell)
3565 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3566 pi.pain.line(x, y, x + w, y,
3567 drawline ? linecolor : gridcolor,
3568 drawline ? Painter::line_solid : Painter::line_onoffdash);
3571 drawline = tabular.bottomLine(cell);
3572 pi.pain.line(x, y + h, x + w, y + h,
3573 drawline ? linecolor : gridcolor,
3574 drawline ? Painter::line_solid : Painter::line_onoffdash);
3577 col_type const col = tabular.cellColumn(cell);
3578 drawline = tabular.leftLine(cell)
3579 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3580 pi.pain.line(x, y, x, y + h,
3581 drawline ? linecolor : gridcolor,
3582 drawline ? Painter::line_solid : Painter::line_onoffdash);
3585 x -= tabular.interColumnSpace(cell);
3586 drawline = tabular.rightLine(cell)
3587 || (col + 1 < tabular.ncols()
3588 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3589 pi.pain.line(x + w, y, x + w, y + h,
3590 drawline ? linecolor : gridcolor,
3591 drawline ? Painter::line_solid : Painter::line_onoffdash);
3595 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3597 //lyxerr << "InsetTabular::edit: " << this << endl;
3599 cur.setSelection(false);
3602 if (isRightToLeft(cur))
3603 cur.idx() = tabular.getLastCellInRow(0);
3609 if (isRightToLeft(cur))
3610 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3612 cur.idx() = tabular.numberofcells - 1;
3614 cur.pos() = cur.lastpos(); // FIXME crude guess
3616 cur.setCurrentFont();
3617 // FIXME: this accesses the position cache before it is initialized
3619 //cur.bv().fitCursor();
3623 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3625 // In a longtable, tell captions what the current float is
3626 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3627 string const saveflt = cnts.current_float();
3628 if (tabular.is_long_tabular)
3629 cnts.current_float("table");
3631 ParIterator it2 = it;
3633 size_t const end = it2.nargs();
3634 for ( ; it2.idx() < end; it2.top().forwardIdx())
3635 buffer().updateBuffer(it2, utype);
3638 if (tabular.is_long_tabular)
3639 cnts.current_float(saveflt);
3643 void InsetTabular::addToToc(DocIterator const & cpit)
3645 DocIterator dit = cpit;
3647 size_t const end = dit.nargs();
3648 for ( ; dit.idx() < end; dit.top().forwardIdx())
3649 cell(dit.idx())->addToToc(dit);
3653 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3655 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3656 << "\n cur:" << cur);
3657 CursorSlice sl = cur.top();
3658 Cursor & bvcur = cur.bv().cursor();
3660 FuncCode const act = cmd.action();
3664 case LFUN_MOUSE_PRESS: {
3665 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3667 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3668 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3669 row_type r = rowFromY(cur, cmd.y());
3670 cur.idx() = tabular.getFirstCellInRow(r);
3673 cur.idx() = tabular.getLastCellInRow(r);
3674 cur.pos() = cur.lastpos();
3675 cur.setSelection(true);
3681 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3682 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3683 || cmd.y() > y0 + tabular.height()) {
3684 col_type c = columnFromX(cur, cmd.x());
3685 cur.idx() = tabular.cellIndex(0, c);
3688 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3689 cur.pos() = cur.lastpos();
3690 cur.setSelection(true);
3695 // do not reset cursor/selection if we have selected
3696 // some cells (bug 2715).
3697 if (cmd.button() == mouse_button::button3
3698 && &bvcur.selBegin().inset() == this
3699 && bvcur.selIsMultiCell())
3702 // Let InsetTableCell do it
3703 cell(cur.idx())->dispatch(cur, cmd);
3706 case LFUN_MOUSE_MOTION:
3707 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3708 if (cmd.button() == mouse_button::button1) {
3709 // only accept motions to places not deeper nested than the real anchor
3710 if (!bvcur.realAnchor().hasPart(cur)) {
3714 // select (additional) row
3716 row_type r = rowFromY(cur, cmd.y());
3717 cur.idx() = tabular.getLastCellInRow(r);
3718 // we need to reset the cursor's pit and pos now, as the old ones
3719 // may no longer be valid.
3722 bvcur.setCursor(cur);
3723 bvcur.setSelection(true);
3726 // select (additional) column
3728 col_type c = columnFromX(cur, cmd.x());
3729 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3730 // we need to reset the cursor's pit and pos now, as the old ones
3731 // may no longer be valid.
3734 bvcur.setCursor(cur);
3735 bvcur.setSelection(true);
3738 // only update if selection changes
3739 if (bvcur.idx() == cur.idx() &&
3740 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3741 cur.noScreenUpdate();
3742 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3743 bvcur.setCursor(cur);
3744 bvcur.setSelection(true);
3745 // if this is a multicell selection, we just set the cursor to
3746 // the beginning of the cell's text.
3747 if (bvcur.selIsMultiCell()) {
3748 bvcur.pit() = bvcur.lastpit();
3749 bvcur.pos() = bvcur.lastpos();
3754 case LFUN_MOUSE_RELEASE:
3759 case LFUN_CELL_BACKWARD:
3761 cur.setSelection(false);
3764 case LFUN_CELL_FORWARD:
3766 cur.setSelection(false);
3769 case LFUN_CHAR_FORWARD_SELECT:
3770 case LFUN_CHAR_FORWARD:
3771 case LFUN_CHAR_BACKWARD_SELECT:
3772 case LFUN_CHAR_BACKWARD:
3773 case LFUN_CHAR_RIGHT_SELECT:
3774 case LFUN_CHAR_RIGHT:
3775 case LFUN_CHAR_LEFT_SELECT:
3776 case LFUN_CHAR_LEFT: {
3777 // determine whether we move to next or previous cell, where to enter
3778 // the new cell from, and which command to "finish" (i.e., exit the
3781 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3782 FuncCode finish_lfun;
3784 if (act == LFUN_CHAR_FORWARD
3785 || act == LFUN_CHAR_FORWARD_SELECT) {
3787 finish_lfun = LFUN_FINISHED_FORWARD;
3789 else if (act == LFUN_CHAR_BACKWARD
3790 || act == LFUN_CHAR_BACKWARD_SELECT) {
3792 finish_lfun = LFUN_FINISHED_BACKWARD;
3794 // LEFT or RIGHT commands --- the interpretation will depend on the
3795 // table's direction.
3797 bool const right = act == LFUN_CHAR_RIGHT
3798 || act == LFUN_CHAR_RIGHT_SELECT;
3799 next_cell = isRightToLeft(cur) != right;
3801 if (lyxrc.visual_cursor)
3802 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3804 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3807 bool const select = act == LFUN_CHAR_FORWARD_SELECT ||
3808 act == LFUN_CHAR_BACKWARD_SELECT ||
3809 act == LFUN_CHAR_RIGHT_SELECT ||
3810 act == LFUN_CHAR_LEFT_SELECT;
3812 // If we have a multicell selection or we're
3813 // not doing some LFUN_*_SELECT thing anyway...
3814 if (!cur.selIsMultiCell() || !select) {
3815 col_type const c = tabular.cellColumn(cur.idx());
3816 row_type const r = tabular.cellRow(cur.idx());
3817 // Are we trying to select the whole cell and is the whole cell
3818 // not yet selected?
3819 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3820 ((next_cell && cur.pit() == cur.lastpit()
3821 && cur.pos() == cur.lastpos())
3822 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3824 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3826 // ...try to dispatch to the cell's inset.
3827 cell(cur.idx())->dispatch(cur, cmd);
3829 // When we already have a selection we want to select the whole cell
3830 // before going to the next cell.
3831 if (select_whole && !empty_cell){
3832 getText(cur.idx())->selectAll(cur);
3837 // FIXME: When we support the selection of an empty cell, remove
3838 // the !empty_cell from this condition. For now we jump to the next
3839 // cell if the current cell is empty.
3840 if (cur.result().dispatched() && !empty_cell)
3844 // move to next/prev cell, as appropriate
3845 // note that we will always do this if we're selecting and we have
3846 // a multicell selection
3847 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3848 << " cell from: " << int(entry_from));
3850 moveNextCell(cur, entry_from);
3852 movePrevCell(cur, entry_from);
3853 // if we're exiting the table, call the appropriate FINISHED lfun
3854 if (sl == cur.top())
3855 cmd = FuncRequest(finish_lfun);
3862 case LFUN_DOWN_SELECT:
3864 if (!(cur.selection() && cur.selIsMultiCell()))
3865 cell(cur.idx())->dispatch(cur, cmd);
3867 cur.dispatched(); // override the cell's decision
3868 if (sl == cur.top()) {
3869 // if our Text didn't do anything to the cursor
3870 // then we try to put the cursor into the cell below
3871 // setting also the right targetX.
3872 cur.selHandle(act == LFUN_DOWN_SELECT);
3873 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3874 cur.idx() = tabular.cellBelow(cur.idx());
3876 TextMetrics const & tm =
3877 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3878 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3879 cur.setCurrentFont();
3882 if (sl == cur.top()) {
3883 // we trick it to go to forward after leaving the
3885 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3888 if (cur.selIsMultiCell()) {
3889 cur.pit() = cur.lastpit();
3890 cur.pos() = cur.lastpos();
3891 cur.setCurrentFont();
3896 case LFUN_UP_SELECT:
3898 if (!(cur.selection() && cur.selIsMultiCell()))
3899 cell(cur.idx())->dispatch(cur, cmd);
3900 cur.dispatched(); // override the cell's decision
3901 if (sl == cur.top()) {
3902 // if our Text didn't do anything to the cursor
3903 // then we try to put the cursor into the cell above
3904 // setting also the right targetX.
3905 cur.selHandle(act == LFUN_UP_SELECT);
3906 if (tabular.cellRow(cur.idx()) != 0) {
3907 cur.idx() = tabular.cellAbove(cur.idx());
3908 cur.pit() = cur.lastpit();
3909 Text const * text = cell(cur.idx())->getText(0);
3910 TextMetrics const & tm = cur.bv().textMetrics(text);
3911 ParagraphMetrics const & pm =
3912 tm.parMetrics(cur.lastpit());
3913 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3914 cur.setCurrentFont();
3917 if (sl == cur.top()) {
3918 cmd = FuncRequest(LFUN_UP);
3921 if (cur.selIsMultiCell()) {
3923 cur.pos() = cur.lastpos();
3924 cur.setCurrentFont();
3929 // case LFUN_SCREEN_DOWN: {
3930 // //if (hasSelection())
3931 // // cur.selection() = false;
3932 // col_type const col = tabular.cellColumn(cur.idx());
3933 // int const t = cur.bv().top_y() + cur.bv().height();
3934 // if (t < yo() + tabular.getHeightOfTabular()) {
3935 // cur.bv().scrollDocView(t, true);
3936 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3938 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3945 // case LFUN_SCREEN_UP: {
3946 // //if (hasSelection())
3947 // // cur.selection() = false;
3948 // col_type const col = tabular.cellColumn(cur.idx());
3949 // int const t = cur.bv().top_y() + cur.bv().height();
3951 // cur.bv().scrollDocView(t, true);
3955 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3959 // cur.par() = cur.lastpar();
3960 // cur.pos() = cur.lastpos();
3964 case LFUN_LAYOUT_TABULAR:
3965 cur.bv().showDialog("tabular");
3968 case LFUN_INSET_MODIFY:
3969 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3973 // insert file functions
3974 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3975 case LFUN_FILE_INSERT_PLAINTEXT:
3977 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3978 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3979 FileName(to_utf8(cmd.argument())));
3982 cur.recordUndoInset(INSERT_UNDO);
3983 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3984 // content has been replaced,
3985 // so cursor might be invalid
3986 cur.pos() = cur.lastpos();
3987 cur.pit() = cur.lastpit();
3988 bvcur.setCursor(cur);
3995 if (cur.selIsMultiCell()) {
3996 if (copySelection(cur)) {
3997 cur.recordUndoInset(DELETE_UNDO);
4001 cell(cur.idx())->dispatch(cur, cmd);
4004 case LFUN_SELF_INSERT:
4005 if (cur.selIsMultiCell()) {
4006 cur.recordUndoInset(DELETE_UNDO);
4009 cell(cur.idx())->dispatch(cur, cmd);
4012 case LFUN_CHAR_DELETE_BACKWARD:
4013 case LFUN_CHAR_DELETE_FORWARD:
4014 if (cur.selIsMultiCell()) {
4015 cur.recordUndoInset(DELETE_UNDO);
4018 cell(cur.idx())->dispatch(cur, cmd);
4022 if (!cur.selection())
4024 if (cur.selIsMultiCell()) {
4028 cell(cur.idx())->dispatch(cur, cmd);
4031 case LFUN_CLIPBOARD_PASTE:
4032 case LFUN_PRIMARY_SELECTION_PASTE: {
4033 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4034 theClipboard().getAsText() :
4035 theSelection().get();
4038 // pass to InsertPlaintextString, but
4039 // only if we have multi-cell content
4040 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4041 cur.recordUndoInset(INSERT_UNDO);
4042 if (insertPlaintextString(cur.bv(), clip, false)) {
4043 // content has been replaced,
4044 // so cursor might be invalid
4045 cur.pos() = cur.lastpos();
4046 cur.pit() = cur.lastpit();
4047 bvcur.setCursor(cur);
4051 // Let the cell handle normal text
4052 cell(cur.idx())->dispatch(cur, cmd);
4057 if (!tabularStackDirty()) {
4058 if (!cur.selIsMultiCell())
4059 cell(cur.idx())->dispatch(cur, cmd);
4062 if (theClipboard().isInternal() ||
4063 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4064 cur.recordUndoInset(INSERT_UNDO);
4065 pasteClipboard(cur);
4069 case LFUN_FONT_EMPH:
4070 case LFUN_FONT_BOLD:
4071 case LFUN_FONT_BOLDSYMBOL:
4072 case LFUN_FONT_ROMAN:
4073 case LFUN_FONT_NOUN:
4074 case LFUN_FONT_ITAL:
4075 case LFUN_FONT_FRAK:
4076 case LFUN_FONT_TYPEWRITER:
4077 case LFUN_FONT_SANS:
4078 case LFUN_TEXTSTYLE_APPLY:
4079 case LFUN_TEXTSTYLE_UPDATE:
4080 case LFUN_FONT_SIZE:
4081 case LFUN_FONT_UNDERLINE:
4082 case LFUN_FONT_STRIKEOUT:
4083 case LFUN_FONT_UULINE:
4084 case LFUN_FONT_UWAVE:
4086 case LFUN_WORD_CAPITALIZE:
4087 case LFUN_WORD_UPCASE:
4088 case LFUN_WORD_LOWCASE:
4089 case LFUN_CHARS_TRANSPOSE:
4090 if (cur.selIsMultiCell()) {
4093 getSelection(cur, rs, re, cs, ce);
4094 Cursor tmpcur = cur;
4095 for (row_type r = rs; r <= re; ++r) {
4096 for (col_type c = cs; c <= ce; ++c) {
4097 // cursor follows cell:
4098 tmpcur.idx() = tabular.cellIndex(r, c);
4099 // select this cell only:
4102 tmpcur.resetAnchor();
4103 tmpcur.pit() = tmpcur.lastpit();
4104 tmpcur.pos() = tmpcur.top().lastpos();
4105 tmpcur.setCursor(tmpcur);
4106 tmpcur.setSelection();
4107 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4112 cell(cur.idx())->dispatch(cur, cmd);
4116 case LFUN_INSET_SETTINGS:
4117 // relay this lfun to Inset, not to the cell.
4118 Inset::doDispatch(cur, cmd);
4122 // we try to handle this event in the insets dispatch function.
4123 cell(cur.idx())->dispatch(cur, cmd);
4129 // function sets an object as defined in func_status.h:
4130 // states OK, Unknown, Disabled, On, Off.
4131 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4132 FuncStatus & status) const
4134 switch (cmd.action()) {
4135 case LFUN_INSET_MODIFY: {
4136 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4139 string const s = cmd.getArg(1);
4140 // FIXME: We only check for the very first argument...
4141 int action = Tabular::LAST_ACTION;
4143 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4144 if (tabularFeature[i].feature == s) {
4145 action = tabularFeature[i].action;
4149 if (action == Tabular::LAST_ACTION) {
4151 status.setUnknown(true);
4155 string const argument = cmd.getLongArg(2);
4157 row_type sel_row_start = 0;
4158 row_type sel_row_end = 0;
4159 col_type sel_col_start = 0;
4160 col_type sel_col_end = 0;
4161 Tabular::ltType dummyltt;
4164 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4167 case Tabular::SET_PWIDTH:
4168 case Tabular::SET_MPWIDTH:
4169 case Tabular::SET_SPECIAL_COLUMN:
4170 case Tabular::SET_SPECIAL_MULTICOLUMN:
4171 case Tabular::SET_SPECIAL_MULTIROW:
4172 case Tabular::APPEND_ROW:
4173 case Tabular::APPEND_COLUMN:
4174 case Tabular::DELETE_ROW:
4175 case Tabular::DELETE_COLUMN:
4176 case Tabular::COPY_ROW:
4177 case Tabular::COPY_COLUMN:
4178 case Tabular::SET_TOP_SPACE:
4179 case Tabular::SET_BOTTOM_SPACE:
4180 case Tabular::SET_INTERLINE_SPACE:
4184 case Tabular::SET_DECIMAL_POINT:
4186 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4189 case Tabular::MULTICOLUMN:
4190 // If a row is set as longtable caption, it must not be allowed
4191 // to unset that this row is a multicolumn.
4192 status.setEnabled(sel_row_start == sel_row_end
4193 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4194 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4197 case Tabular::MULTIROW:
4198 // If a row is set as longtable caption, it must not be allowed
4199 // to unset that this row is a multirow.
4200 status.setEnabled(sel_col_start == sel_col_end
4201 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4202 status.setOnOff(tabular.isMultiRow(cur.idx()));
4205 case Tabular::SET_ALL_LINES:
4206 case Tabular::UNSET_ALL_LINES:
4207 case Tabular::SET_BORDER_LINES:
4208 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4211 case Tabular::SET_LINE_TOP:
4212 case Tabular::SET_LINE_BOTTOM:
4213 case Tabular::SET_LINE_LEFT:
4214 case Tabular::SET_LINE_RIGHT:
4215 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4218 case Tabular::TOGGLE_LINE_TOP:
4219 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4220 status.setOnOff(tabular.topLine(cur.idx()));
4223 case Tabular::TOGGLE_LINE_BOTTOM:
4224 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4225 status.setOnOff(tabular.bottomLine(cur.idx()));
4228 case Tabular::TOGGLE_LINE_LEFT:
4229 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4230 status.setOnOff(tabular.leftLine(cur.idx()));
4233 case Tabular::TOGGLE_LINE_RIGHT:
4234 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4235 status.setOnOff(tabular.rightLine(cur.idx()));
4238 case Tabular::M_ALIGN_LEFT:
4240 case Tabular::ALIGN_LEFT:
4241 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4242 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4245 case Tabular::M_ALIGN_RIGHT:
4247 case Tabular::ALIGN_RIGHT:
4248 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4249 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4252 case Tabular::M_ALIGN_CENTER:
4254 case Tabular::ALIGN_CENTER:
4255 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4256 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4259 case Tabular::ALIGN_BLOCK:
4260 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4261 && !tabular.isMultiRow(cur.idx()));
4262 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4265 case Tabular::ALIGN_DECIMAL:
4266 status.setEnabled(!tabular.isMultiRow(cur.idx())
4267 && !tabular.isMultiColumn(cur.idx()));
4268 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4271 case Tabular::M_VALIGN_TOP:
4273 case Tabular::VALIGN_TOP:
4274 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4275 && !tabular.isMultiRow(cur.idx()));
4277 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4280 case Tabular::M_VALIGN_BOTTOM:
4282 case Tabular::VALIGN_BOTTOM:
4283 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4284 && !tabular.isMultiRow(cur.idx()));
4286 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4289 case Tabular::M_VALIGN_MIDDLE:
4291 case Tabular::VALIGN_MIDDLE:
4292 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4293 && !tabular.isMultiRow(cur.idx()));
4295 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4298 case Tabular::SET_LONGTABULAR:
4299 status.setOnOff(tabular.is_long_tabular);
4302 case Tabular::UNSET_LONGTABULAR:
4303 status.setOnOff(!tabular.is_long_tabular);
4306 case Tabular::TOGGLE_ROTATE_TABULAR:
4307 case Tabular::SET_ROTATE_TABULAR:
4308 status.setOnOff(tabular.rotate);
4311 case Tabular::TABULAR_VALIGN_TOP:
4312 status.setOnOff(tabular.tabular_valignment
4313 == Tabular::LYX_VALIGN_TOP);
4315 case Tabular::TABULAR_VALIGN_MIDDLE:
4316 status.setOnOff(tabular.tabular_valignment
4317 == Tabular::LYX_VALIGN_MIDDLE);
4319 case Tabular::TABULAR_VALIGN_BOTTOM:
4320 status.setOnOff(tabular.tabular_valignment
4321 == Tabular::LYX_VALIGN_BOTTOM);
4324 case Tabular::LONGTABULAR_ALIGN_LEFT:
4325 status.setOnOff(tabular.longtabular_alignment
4326 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4328 case Tabular::LONGTABULAR_ALIGN_CENTER:
4329 status.setOnOff(tabular.longtabular_alignment
4330 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4332 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4333 status.setOnOff(tabular.longtabular_alignment
4334 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4337 case Tabular::UNSET_ROTATE_TABULAR:
4338 status.setOnOff(!tabular.rotate);
4341 case Tabular::TOGGLE_ROTATE_CELL:
4342 case Tabular::SET_ROTATE_CELL:
4343 status.setOnOff(!oneCellHasRotationState(false,
4344 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4347 case Tabular::UNSET_ROTATE_CELL:
4348 status.setOnOff(!oneCellHasRotationState(true,
4349 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4352 case Tabular::SET_USEBOX:
4353 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4356 // every row can only be one thing:
4357 // either a footer or header or caption
4358 case Tabular::SET_LTFIRSTHEAD:
4359 status.setEnabled(sel_row_start == sel_row_end
4360 && !tabular.ltCaption(sel_row_start));
4361 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4364 case Tabular::UNSET_LTFIRSTHEAD:
4365 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4368 case Tabular::SET_LTHEAD:
4369 status.setEnabled(sel_row_start == sel_row_end
4370 && !tabular.ltCaption(sel_row_start));
4371 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4374 case Tabular::UNSET_LTHEAD:
4375 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4378 case Tabular::SET_LTFOOT:
4379 status.setEnabled(sel_row_start == sel_row_end
4380 && !tabular.ltCaption(sel_row_start));
4381 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4384 case Tabular::UNSET_LTFOOT:
4385 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4388 case Tabular::SET_LTLASTFOOT:
4389 status.setEnabled(sel_row_start == sel_row_end
4390 && !tabular.ltCaption(sel_row_start));
4391 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4394 case Tabular::UNSET_LTLASTFOOT:
4395 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4398 case Tabular::SET_LTNEWPAGE:
4399 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4402 // only one row can be the caption
4403 // and a multirow cannot be set as caption
4404 case Tabular::TOGGLE_LTCAPTION:
4405 status.setEnabled(sel_row_start == sel_row_end
4406 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4407 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4408 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4409 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4410 && (!tabular.haveLTCaption()
4411 || tabular.ltCaption(sel_row_start))
4412 && !tabular.isMultiRow(sel_row_start));
4413 status.setOnOff(tabular.ltCaption(sel_row_start));
4416 case Tabular::SET_BOOKTABS:
4417 status.setOnOff(tabular.use_booktabs);
4420 case Tabular::UNSET_BOOKTABS:
4421 status.setOnOff(!tabular.use_booktabs);
4426 status.setEnabled(false);
4432 // These are only enabled inside tabular
4433 case LFUN_CELL_BACKWARD:
4434 case LFUN_CELL_FORWARD:
4435 status.setEnabled(true);
4438 // disable these with multiple cells selected
4439 case LFUN_INSET_INSERT:
4440 case LFUN_TABULAR_INSERT:
4441 case LFUN_FLEX_INSERT:
4442 case LFUN_FLOAT_INSERT:
4443 case LFUN_FLOAT_WIDE_INSERT:
4444 case LFUN_FOOTNOTE_INSERT:
4445 case LFUN_MARGINALNOTE_INSERT:
4446 case LFUN_MATH_INSERT:
4447 case LFUN_MATH_MODE:
4448 case LFUN_MATH_MUTATE:
4449 case LFUN_MATH_DISPLAY:
4450 case LFUN_NOTE_INSERT:
4451 case LFUN_ARGUMENT_INSERT:
4452 case LFUN_BOX_INSERT:
4453 case LFUN_BRANCH_INSERT:
4454 case LFUN_PHANTOM_INSERT:
4455 case LFUN_WRAP_INSERT:
4456 case LFUN_PREVIEW_INSERT:
4457 case LFUN_ERT_INSERT: {
4458 if (cur.selIsMultiCell()) {
4459 status.setEnabled(false);
4462 return cell(cur.idx())->getStatus(cur, cmd, status);
4465 // disable in non-fixed-width cells
4466 case LFUN_NEWLINE_INSERT:
4467 case LFUN_BREAK_PARAGRAPH: {
4468 if (tabular.getPWidth(cur.idx()).zero()) {
4469 status.setEnabled(false);
4472 return cell(cur.idx())->getStatus(cur, cmd, status);
4475 case LFUN_NEWPAGE_INSERT:
4476 status.setEnabled(false);
4480 if (tabularStackDirty() && theClipboard().isInternal()) {
4481 if (cur.selIsMultiCell()) {
4484 getSelection(cur, rs, re, cs, ce);
4485 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4486 && paste_tabular->nrows() == re - rs + 1)
4487 status.setEnabled(true);
4489 status.setEnabled(false);
4490 status.message(_("Selection size should match clipboard content."));
4493 status.setEnabled(true);
4496 return cell(cur.idx())->getStatus(cur, cmd, status);
4498 case LFUN_INSET_SETTINGS:
4499 // relay this lfun to Inset, not to the cell.
4500 return Inset::getStatus(cur, cmd, status);
4503 // we try to handle this event in the insets dispatch function.
4504 return cell(cur.idx())->getStatus(cur, cmd, status);
4510 Inset::DisplayType InsetTabular::display() const
4512 if (tabular.is_long_tabular) {
4513 switch (tabular.longtabular_alignment) {
4514 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4516 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4518 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4528 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4530 return tabular.latex(os, runparams);
4534 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4536 os << '\n'; // output table on a new line
4537 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4538 tabular.plaintext(os, runparams, dp, false, 0);
4539 return PLAINTEXT_NEWLINE;
4543 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4548 // FIXME: Why not pass a proper DocIterator here?
4550 // if the table is inside a float it doesn't need the informaltable
4551 // wrapper. Search for it.
4552 for (master = owner(); master; master = master->owner())
4553 if (master->lyxCode() == FLOAT_CODE)
4558 os << "<informaltable>";
4561 ret += tabular.docbook(os, runparams);
4563 os << "</informaltable>";
4570 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4573 // It'd be better to be able to get this from an InsetLayout, but at present
4574 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4575 xs << html::StartTag("table");
4576 docstring ret = tabular.xhtml(xs, rp);
4577 xs << html::EndTag("table");
4582 void InsetTabular::validate(LaTeXFeatures & features) const
4584 tabular.validate(features);
4586 // It'd be better to be able to get this from an InsetLayout, but at present
4587 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4588 if (features.runparams().flavor == OutputParams::HTML)
4589 features.addPreambleSnippet("<style type=\"text/css\">\n"
4590 "table { border: 1px solid black; display: inline-block; }\n"
4591 "td { border: 1px solid black; padding: 0.5ex; }\n"
4596 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4598 return tabular.cellInset(idx);
4602 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4604 return tabular.cellInset(idx);
4608 void InsetTabular::cursorPos(BufferView const & bv,
4609 CursorSlice const & sl, bool boundary, int & x, int & y) const
4611 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4613 int const row = tabular.cellRow(sl.idx());
4614 int const col = tabular.cellColumn(sl.idx());
4616 // y offset correction
4617 for (int r = 0; r < row; ++r)
4618 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4619 + tabular.interRowSpace(r + 1);
4621 y += tabular.textVOffset(sl.idx());
4623 // x offset correction
4624 for (int c = 0; c < col; ++c)
4625 x += tabular.column_info[c].width;
4627 x += tabular.textHOffset(sl.idx());
4628 x += ADD_TO_TABULAR_WIDTH;
4633 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4637 Inset const & inset = *tabular.cellInset(cell);
4638 Point o = bv.coordCache().getInsets().xy(&inset);
4639 int const xbeg = o.x_ - tabular.textHOffset(cell);
4640 int const xend = xbeg + tabular.cellWidth(cell);
4641 row_type const row = tabular.cellRow(cell);
4642 int const ybeg = o.y_ - tabular.rowAscent(row)
4643 - tabular.interRowSpace(row);
4644 int const yend = ybeg + tabular.cellHeight(cell);
4656 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4657 // << " ybeg=" << ybeg << " yend=" << yend
4658 // << " xx=" << xx << " yy=" << yy
4659 // << " dist=" << xx + yy << endl;
4664 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4666 //lyxerr << "InsetTabular::editXY: " << this << endl;
4667 cur.setSelection(false);
4669 cur.idx() = getNearestCell(cur.bv(), x, y);
4671 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4675 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4677 cur.idx() = getNearestCell(cur.bv(), x, y);
4678 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4682 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4684 idx_type idx_min = 0;
4685 int dist_min = numeric_limits<int>::max();
4686 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4687 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4688 int const d = dist(bv, i, x, y);
4699 int InsetTabular::cellXPos(idx_type const cell) const
4701 col_type col = tabular.cellColumn(cell);
4703 for (col_type c = 0; c < col; ++c)
4704 lx += tabular.column_info[c].width;
4710 void InsetTabular::resetPos(Cursor & cur) const
4712 BufferView & bv = cur.bv();
4713 int const maxwidth = bv.workWidth();
4715 int const scx_old = scx_;
4716 int const i = cur.find(this);
4721 int const X2 = maxwidth;
4722 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4723 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4724 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4727 scx_ = X1 + 20 - x1;
4729 scx_ = X2 - 20 - x2;
4734 // only update if offset changed
4735 if (scx_ != scx_old)
4736 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4740 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4742 row_type const row = tabular.cellRow(cur.idx());
4743 col_type const col = tabular.cellColumn(cur.idx());
4745 if (isRightToLeft(cur)) {
4746 if (tabular.cellColumn(cur.idx()) == 0) {
4747 if (row == tabular.nrows() - 1)
4749 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4754 cur.idx() = tabular.getLastCellInRow(row - 1);
4756 cur.idx() = tabular.cellIndex(row, col - 1);
4759 if (tabular.isLastCell(cur.idx()))
4761 if (cur.idx() == tabular.getLastCellInRow(row))
4762 cur.idx() = tabular.cellIndex(row + 1, 0);
4764 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4765 cur.idx() = tabular.cellIndex(row, colnextcell);
4769 cur.boundary(false);
4771 if (cur.selIsMultiCell()) {
4772 cur.pit() = cur.lastpit();
4773 cur.pos() = cur.lastpos();
4781 // in visual mode, place cursor at extreme left or right
4783 switch(entry_from) {
4785 case ENTRY_DIRECTION_RIGHT:
4786 cur.posVisToRowExtremity(false /* !left */);
4788 case ENTRY_DIRECTION_LEFT:
4789 cur.posVisToRowExtremity(true /* left */);
4791 case ENTRY_DIRECTION_IGNORE:
4792 // nothing to do in this case
4796 cur.setCurrentFont();
4801 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4803 row_type const row = tabular.cellRow(cur.idx());
4804 col_type const col = tabular.cellColumn(cur.idx());
4806 if (isRightToLeft(cur)) {
4807 if (cur.idx() == tabular.getLastCellInRow(row)) {
4810 cur.idx() = tabular.getFirstCellInRow(row);
4811 cur.idx() = tabular.cellAbove(cur.idx());
4813 if (tabular.isLastCell(cur.idx()))
4815 if (cur.idx() == tabular.getLastCellInRow(row))
4816 cur.idx() = tabular.cellIndex(row + 1, 0);
4818 cur.idx() = tabular.cellIndex(row, col + 1);
4821 if (cur.idx() == 0) // first cell
4824 cur.idx() = tabular.getLastCellInRow(row - 1);
4826 cur.idx() = tabular.cellIndex(row, col - 1);
4829 if (cur.selIsMultiCell()) {
4830 cur.pit() = cur.lastpit();
4831 cur.pos() = cur.lastpos();
4836 cur.pit() = cur.lastpit();
4837 cur.pos() = cur.lastpos();
4839 // in visual mode, place cursor at extreme left or right
4841 switch(entry_from) {
4843 case ENTRY_DIRECTION_RIGHT:
4844 cur.posVisToRowExtremity(false /* !left */);
4846 case ENTRY_DIRECTION_LEFT:
4847 cur.posVisToRowExtremity(true /* left */);
4849 case ENTRY_DIRECTION_IGNORE:
4850 // nothing to do in this case
4854 cur.setCurrentFont();
4859 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4861 istringstream is(argument);
4864 if (insetCode(s) != TABULAR_CODE)
4868 size_t safe_guard = 0;
4873 if (safe_guard > 1000) {
4874 LYXERR0("parameter max count reached!");
4878 Tabular::Feature action = Tabular::LAST_ACTION;
4881 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4882 if (s != tabularFeature[i].feature)
4885 action = tabularFeature[i].action;
4888 if (action == Tabular::LAST_ACTION) {
4889 LYXERR0("Feature not found " << s);
4893 if (tabularFeature[i].need_value)
4895 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4896 tabularFeatures(cur, action, val);
4902 static void checkLongtableSpecial(Tabular::ltType & ltt,
4903 string const & special, bool & flag)
4905 if (special == "dl_above") {
4908 } else if (special == "dl_below") {
4909 ltt.bottomDL = flag;
4911 } else if (special == "empty") {
4921 bool InsetTabular::oneCellHasRotationState(bool rotated,
4922 row_type row_start, row_type row_end,
4923 col_type col_start, col_type col_end) const
4925 for (row_type r = row_start; r <= row_end; ++r)
4926 for (col_type c = col_start; c <= col_end; ++c)
4927 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4933 void InsetTabular::tabularFeatures(Cursor & cur,
4934 Tabular::Feature feature, string const & value)
4936 col_type sel_col_start;
4937 col_type sel_col_end;
4938 row_type sel_row_start;
4939 row_type sel_row_end;
4940 bool setLines = false;
4941 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4942 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4946 case Tabular::M_ALIGN_LEFT:
4947 case Tabular::ALIGN_LEFT:
4948 setAlign = LYX_ALIGN_LEFT;
4951 case Tabular::M_ALIGN_RIGHT:
4952 case Tabular::ALIGN_RIGHT:
4953 setAlign = LYX_ALIGN_RIGHT;
4956 case Tabular::M_ALIGN_CENTER:
4957 case Tabular::ALIGN_CENTER:
4958 setAlign = LYX_ALIGN_CENTER;
4961 case Tabular::ALIGN_BLOCK:
4962 setAlign = LYX_ALIGN_BLOCK;
4965 case Tabular::ALIGN_DECIMAL:
4966 setAlign = LYX_ALIGN_DECIMAL;
4969 case Tabular::M_VALIGN_TOP:
4970 case Tabular::VALIGN_TOP:
4971 setVAlign = Tabular::LYX_VALIGN_TOP;
4974 case Tabular::M_VALIGN_BOTTOM:
4975 case Tabular::VALIGN_BOTTOM:
4976 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4979 case Tabular::M_VALIGN_MIDDLE:
4980 case Tabular::VALIGN_MIDDLE:
4981 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4988 cur.recordUndoInset(ATOMIC_UNDO);
4990 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4991 row_type const row = tabular.cellRow(cur.idx());
4992 col_type const column = tabular.cellColumn(cur.idx());
4994 Tabular::ltType ltt;
4998 case Tabular::SET_PWIDTH: {
4999 Length const len(value);
5000 tabular.setColumnPWidth(cur, cur.idx(), len);
5002 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5003 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5007 case Tabular::SET_MPWIDTH:
5008 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5011 case Tabular::SET_SPECIAL_COLUMN:
5012 case Tabular::SET_SPECIAL_MULTICOLUMN:
5013 if (value == "none")
5014 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5016 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5019 case Tabular::SET_SPECIAL_MULTIROW:
5023 case Tabular::APPEND_ROW:
5024 // append the row into the tabular
5025 tabular.appendRow(cur.idx());
5028 case Tabular::APPEND_COLUMN:
5029 // append the column into the tabular
5030 tabular.appendColumn(cur.idx());
5031 cur.idx() = tabular.cellIndex(row, column);
5034 case Tabular::DELETE_ROW:
5035 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5036 tabular.deleteRow(sel_row_start);
5037 if (sel_row_start >= tabular.nrows())
5039 cur.idx() = tabular.cellIndex(sel_row_start, column);
5042 cur.setSelection(false);
5045 case Tabular::DELETE_COLUMN:
5046 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5047 tabular.deleteColumn(sel_col_start);
5048 if (sel_col_start >= tabular.ncols())
5050 cur.idx() = tabular.cellIndex(row, sel_col_start);
5053 cur.setSelection(false);
5056 case Tabular::COPY_ROW:
5057 tabular.copyRow(row);
5060 case Tabular::COPY_COLUMN:
5061 tabular.copyColumn(column);
5062 cur.idx() = tabular.cellIndex(row, column);
5065 case Tabular::SET_LINE_TOP:
5066 case Tabular::TOGGLE_LINE_TOP: {
5067 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5068 ? (value == "true") : !tabular.topLine(cur.idx());
5069 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5070 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5071 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5075 case Tabular::SET_LINE_BOTTOM:
5076 case Tabular::TOGGLE_LINE_BOTTOM: {
5077 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5078 ? (value == "true") : !tabular.bottomLine(cur.idx());
5079 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5080 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5081 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5085 case Tabular::SET_LINE_LEFT:
5086 case Tabular::TOGGLE_LINE_LEFT: {
5087 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5088 ? (value == "true") : !tabular.leftLine(cur.idx());
5089 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5090 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5091 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5095 case Tabular::SET_LINE_RIGHT:
5096 case Tabular::TOGGLE_LINE_RIGHT: {
5097 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5098 ? (value == "true") : !tabular.rightLine(cur.idx());
5099 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5100 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5101 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5105 case Tabular::M_ALIGN_LEFT:
5106 case Tabular::M_ALIGN_RIGHT:
5107 case Tabular::M_ALIGN_CENTER:
5109 case Tabular::ALIGN_LEFT:
5110 case Tabular::ALIGN_RIGHT:
5111 case Tabular::ALIGN_CENTER:
5112 case Tabular::ALIGN_BLOCK:
5113 case Tabular::ALIGN_DECIMAL:
5114 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5115 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5116 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5119 case Tabular::M_VALIGN_TOP:
5120 case Tabular::M_VALIGN_BOTTOM:
5121 case Tabular::M_VALIGN_MIDDLE:
5123 case Tabular::VALIGN_TOP:
5124 case Tabular::VALIGN_BOTTOM:
5125 case Tabular::VALIGN_MIDDLE:
5126 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5127 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5128 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5131 case Tabular::MULTICOLUMN: {
5132 if (!cur.selection()) {
5133 // just multicol for one single cell
5134 // check whether we are completely in a multicol
5135 if (tabular.isMultiColumn(cur.idx()))
5136 tabular.unsetMultiColumn(cur.idx());
5138 tabular.setMultiColumn(cur.idx(), 1);
5141 // we have a selection so this means we just add all this
5142 // cells to form a multicolumn cell
5143 idx_type const s_start = cur.selBegin().idx();
5144 row_type const col_start = tabular.cellColumn(s_start);
5145 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5146 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5149 cur.setSelection(false);
5153 case Tabular::MULTIROW: {
5154 if (!cur.selection()) {
5155 // just multirow for one single cell
5156 // check whether we are completely in a multirow
5157 if (tabular.isMultiRow(cur.idx()))
5158 tabular.unsetMultiRow(cur.idx());
5160 tabular.setMultiRow(cur.idx(), 1);
5163 // we have a selection so this means we just add all this
5164 // cells to form a multirow cell
5165 idx_type const s_start = cur.selBegin().idx();
5166 row_type const row_start = tabular.cellRow(s_start);
5167 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5168 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5171 cur.setSelection(false);
5175 case Tabular::SET_ALL_LINES:
5177 case Tabular::UNSET_ALL_LINES:
5178 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5179 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5180 idx_type const cell = tabular.cellIndex(r, c);
5181 tabular.setTopLine(cell, setLines);
5182 tabular.setBottomLine(cell, setLines);
5183 tabular.setRightLine(cell, setLines);
5184 tabular.setLeftLine(cell, setLines);
5188 case Tabular::SET_BORDER_LINES:
5189 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5190 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5191 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5193 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5194 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5195 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5199 case Tabular::SET_LONGTABULAR:
5200 tabular.is_long_tabular = true;
5203 case Tabular::UNSET_LONGTABULAR:
5204 for (row_type r = 0; r < tabular.nrows(); ++r) {
5205 if (tabular.ltCaption(r)) {
5206 cur.idx() = tabular.cellIndex(r, 0);
5209 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5212 tabular.is_long_tabular = false;
5215 case Tabular::SET_ROTATE_TABULAR:
5216 tabular.rotate = true;
5219 case Tabular::UNSET_ROTATE_TABULAR:
5220 tabular.rotate = false;
5223 case Tabular::TOGGLE_ROTATE_TABULAR:
5224 tabular.rotate = !tabular.rotate;
5227 case Tabular::TABULAR_VALIGN_TOP:
5228 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5231 case Tabular::TABULAR_VALIGN_MIDDLE:
5232 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5235 case Tabular::TABULAR_VALIGN_BOTTOM:
5236 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5239 case Tabular::LONGTABULAR_ALIGN_LEFT:
5240 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5243 case Tabular::LONGTABULAR_ALIGN_CENTER:
5244 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5247 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5248 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5253 case Tabular::SET_ROTATE_CELL:
5254 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5255 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5256 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5259 case Tabular::UNSET_ROTATE_CELL:
5260 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5261 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5262 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5265 case Tabular::TOGGLE_ROTATE_CELL:
5267 bool oneNotRotated = oneCellHasRotationState(false,
5268 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5270 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5271 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5272 tabular.setRotateCell(tabular.cellIndex(r, c),
5277 case Tabular::SET_USEBOX: {
5278 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5279 if (val == tabular.getUsebox(cur.idx()))
5280 val = Tabular::BOX_NONE;
5281 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5282 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5283 tabular.setUsebox(tabular.cellIndex(r, c), val);
5287 case Tabular::UNSET_LTFIRSTHEAD:
5289 case Tabular::SET_LTFIRSTHEAD:
5290 tabular.getRowOfLTFirstHead(row, ltt);
5291 checkLongtableSpecial(ltt, value, flag);
5292 tabular.setLTHead(row, flag, ltt, true);
5295 case Tabular::UNSET_LTHEAD:
5297 case Tabular::SET_LTHEAD:
5298 tabular.getRowOfLTHead(row, ltt);
5299 checkLongtableSpecial(ltt, value, flag);
5300 tabular.setLTHead(row, flag, ltt, false);
5303 case Tabular::UNSET_LTFOOT:
5305 case Tabular::SET_LTFOOT:
5306 tabular.getRowOfLTFoot(row, ltt);
5307 checkLongtableSpecial(ltt, value, flag);
5308 tabular.setLTFoot(row, flag, ltt, false);
5311 case Tabular::UNSET_LTLASTFOOT:
5313 case Tabular::SET_LTLASTFOOT:
5314 tabular.getRowOfLTLastFoot(row, ltt);
5315 checkLongtableSpecial(ltt, value, flag);
5316 tabular.setLTFoot(row, flag, ltt, true);
5319 case Tabular::SET_LTNEWPAGE:
5320 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5323 case Tabular::TOGGLE_LTCAPTION: {
5324 bool const set = !tabular.ltCaption(row);
5325 cur.idx() = tabular.setLTCaption(row, set);
5328 cur.setSelection(false);
5331 // When a row is set as caption, then also insert
5332 // a caption. Otherwise the LaTeX output is broken.
5333 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5334 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5336 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5337 if (lyx::getStatus(fr).enabled())
5343 case Tabular::SET_BOOKTABS:
5344 tabular.use_booktabs = true;
5347 case Tabular::UNSET_BOOKTABS:
5348 tabular.use_booktabs = false;
5351 case Tabular::SET_TOP_SPACE: {
5353 if (value == "default")
5354 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5355 tabular.row_info[r].top_space_default = true;
5356 else if (value == "none")
5357 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5358 tabular.row_info[r].top_space_default = false;
5359 tabular.row_info[r].top_space = len;
5361 else if (isValidLength(value, &len))
5362 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5363 tabular.row_info[r].top_space_default = false;
5364 tabular.row_info[r].top_space = len;
5369 case Tabular::SET_BOTTOM_SPACE: {
5371 if (value == "default")
5372 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5373 tabular.row_info[r].bottom_space_default = true;
5374 else if (value == "none")
5375 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5376 tabular.row_info[r].bottom_space_default = false;
5377 tabular.row_info[r].bottom_space = len;
5379 else if (isValidLength(value, &len))
5380 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5381 tabular.row_info[r].bottom_space_default = false;
5382 tabular.row_info[r].bottom_space = len;
5387 case Tabular::SET_INTERLINE_SPACE: {
5389 if (value == "default")
5390 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5391 tabular.row_info[r].interline_space_default = true;
5392 else if (value == "none")
5393 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5394 tabular.row_info[r].interline_space_default = false;
5395 tabular.row_info[r].interline_space = len;
5397 else if (isValidLength(value, &len))
5398 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5399 tabular.row_info[r].interline_space_default = false;
5400 tabular.row_info[r].interline_space = len;
5405 case Tabular::SET_DECIMAL_POINT:
5406 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5407 tabular.column_info[c].decimal_point = from_utf8(value);
5410 // dummy stuff just to avoid warnings
5411 case Tabular::LAST_ACTION:
5417 bool InsetTabular::copySelection(Cursor & cur)
5419 if (!cur.selection())
5424 getSelection(cur, rs, re, cs, ce);
5426 paste_tabular.reset(new Tabular(tabular));
5428 for (row_type r = 0; r < rs; ++r)
5429 paste_tabular->deleteRow(0);
5431 row_type const rows = re - rs + 1;
5432 while (paste_tabular->nrows() > rows)
5433 paste_tabular->deleteRow(rows);
5435 for (col_type c = 0; c < cs; ++c)
5436 paste_tabular->deleteColumn(0);
5438 col_type const columns = ce - cs + 1;
5439 while (paste_tabular->ncols() > columns)
5440 paste_tabular->deleteColumn(columns);
5442 paste_tabular->setBuffer(tabular.buffer());
5444 odocstringstream os;
5445 OutputParams const runparams(0);
5446 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5447 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5448 cap::copySelection(cur, os.str());
5450 // mark tabular stack dirty
5451 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5452 // when we (hopefully) have a one-for-all paste mechanism.
5453 // This must be called after cap::copySelection.
5454 dirtyTabularStack(true);
5460 bool InsetTabular::pasteClipboard(Cursor & cur)
5464 col_type actcol = tabular.cellColumn(cur.idx());
5465 row_type actrow = tabular.cellRow(cur.idx());
5467 if (cur.selIsMultiCell()) {
5470 getSelection(cur, actrow, re, actcol, ce);
5473 for (row_type r1 = 0, r2 = actrow;
5474 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5476 for (col_type c1 = 0, c2 = actcol;
5477 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5479 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5480 tabular.isPartOfMultiColumn(r2, c2))
5482 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5486 if (tabular.isPartOfMultiColumn(r2, c2)) {
5490 shared_ptr<InsetTableCell> inset(
5491 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5492 tabular.setCellInset(r2, c2, inset);
5493 // FIXME?: why do we need to do this explicitly? (EL)
5494 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5496 // FIXME: change tracking (MG)
5497 inset->setChange(Change(buffer().params().trackChanges ?
5498 Change::INSERTED : Change::UNCHANGED));
5506 void InsetTabular::cutSelection(Cursor & cur)
5508 if (!cur.selection())
5513 getSelection(cur, rs, re, cs, ce);
5514 for (row_type r = rs; r <= re; ++r) {
5515 for (col_type c = cs; c <= ce; ++c) {
5516 shared_ptr<InsetTableCell> t
5517 = cell(tabular.cellIndex(r, c));
5518 if (buffer().params().trackChanges)
5519 // FIXME: Change tracking (MG)
5520 t->setChange(Change(Change::DELETED));
5526 // cursor position might be invalid now
5527 if (cur.pit() > cur.lastpit())
5528 cur.pit() = cur.lastpit();
5529 if (cur.pos() > cur.lastpos())
5530 cur.pos() = cur.lastpos();
5531 cur.clearSelection();
5535 bool InsetTabular::isRightToLeft(Cursor & cur) const
5537 LASSERT(cur.depth() > 1, /**/);
5538 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5539 pos_type const parentpos = cur[cur.depth() - 2].pos();
5540 return parentpar.getFontSettings(buffer().params(),
5541 parentpos).language()->rightToLeft();
5545 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5548 LASSERT(stidx <= enidx, return docstring());
5550 col_type const col1 = tabular.cellColumn(stidx);
5551 col_type const col2 = tabular.cellColumn(enidx);
5552 row_type const row1 = tabular.cellRow(stidx);
5553 row_type const row2 = tabular.cellRow(enidx);
5555 for (col_type col = col1; col <= col2; col++)
5556 for (row_type row = row1; row <= row2; row++) {
5561 retval += tabular.cellInset(row, col)->asString(intoInsets);
5567 void InsetTabular::getSelection(Cursor & cur,
5568 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5570 CursorSlice const & beg = cur.selBegin();
5571 CursorSlice const & end = cur.selEnd();
5572 cs = tabular.cellColumn(beg.idx());
5573 ce = tabular.cellColumn(end.idx());
5577 rs = tabular.cellRow(beg.idx());
5578 re = tabular.cellRow(end.idx());
5584 Text * InsetTabular::getText(int idx) const
5586 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5590 void InsetTabular::setChange(Change const & change)
5592 for (idx_type idx = 0; idx < nargs(); ++idx)
5593 cell(idx)->setChange(change);
5597 void InsetTabular::acceptChanges()
5599 for (idx_type idx = 0; idx < nargs(); ++idx)
5600 cell(idx)->acceptChanges();
5604 void InsetTabular::rejectChanges()
5606 for (idx_type idx = 0; idx < nargs(); ++idx)
5607 cell(idx)->rejectChanges();
5611 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5613 return tabular.getPWidth(cell).zero();
5617 bool InsetTabular::forcePlainLayout(idx_type cell) const
5619 return !tabular.getPWidth(cell).zero();
5623 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5626 if (buf.length() <= 0)
5631 col_type maxCols = 1;
5632 size_t const len = buf.length();
5636 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5644 maxCols = max(cols, maxCols);
5650 maxCols = max(cols, maxCols);
5656 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5657 loctab = paste_tabular.get();
5659 dirtyTabularStack(true);
5662 cell = bv.cursor().idx();
5663 ocol = tabular.cellColumn(cell);
5664 row = tabular.cellRow(cell);
5668 idx_type const cells = loctab->numberofcells;
5671 rows = loctab->nrows();
5672 col_type const columns = loctab->ncols();
5674 while (cell < cells && p < len && row < rows &&
5675 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5681 // we can only set this if we are not too far right
5682 if (cols < columns) {
5683 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5684 Font const font = bv.textMetrics(&inset->text()).
5686 inset->setText(buf.substr(op, p - op), font,
5687 buffer().params().trackChanges);
5693 // we can only set this if we are not too far right
5694 if (cols < columns) {
5695 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5696 Font const font = bv.textMetrics(&inset->text()).
5698 inset->setText(buf.substr(op, p - op), font,
5699 buffer().params().trackChanges);
5704 cell = loctab->cellIndex(row, cols);
5710 // check for the last cell if there is no trailing '\n'
5711 if (cell < cells && op < len) {
5712 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5713 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5714 inset->setText(buf.substr(op, len - op), font,
5715 buffer().params().trackChanges);
5721 void InsetTabular::addPreview(DocIterator const & inset_pos,
5722 PreviewLoader & loader) const
5724 DocIterator cell_pos = inset_pos;
5726 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5727 for (row_type r = 0; r < tabular.nrows(); ++r) {
5728 for (col_type c = 0; c < tabular.ncols(); ++c) {
5729 cell_pos.top().idx() = tabular.cellIndex(r, c);
5730 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5736 bool InsetTabular::completionSupported(Cursor const & cur) const
5738 Cursor const & bvCur = cur.bv().cursor();
5739 if (&bvCur.inset() != this)
5741 return cur.text()->completionSupported(cur);
5745 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5747 return completionSupported(cur);
5751 bool InsetTabular::automaticInlineCompletion() const
5753 return lyxrc.completion_inline_text;
5757 bool InsetTabular::automaticPopupCompletion() const
5759 return lyxrc.completion_popup_text;
5763 bool InsetTabular::showCompletionCursor() const
5765 return lyxrc.completion_cursor_text;
5769 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5771 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5775 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5777 if (!completionSupported(cur))
5779 return cur.text()->completionPrefix(cur);
5783 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5785 if (!completionSupported(cur))
5788 return cur.text()->insertCompletion(cur, s, finished);
5792 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5793 Dimension & dim) const
5795 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5796 tm.completionPosAndDim(cur, x, y, dim);
5800 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5802 istringstream data(in);
5804 lex.setStream(data);
5811 if (!lex || token != "tabular") {
5812 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5816 // This is part of the inset proper that is usually swallowed
5817 // by Buffer::readInset
5819 if (!lex || token != "Tabular") {
5820 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5828 string InsetTabular::params2string(InsetTabular const & inset)
5831 data << "tabular" << ' ';
5833 data << "\\end_inset\n";