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
2206 } // end if ismultirow
2208 if (getRotateCell(cell)) {
2209 os << "\\begin{sideways}\n";
2212 if (getUsebox(cell) == BOX_PARBOX) {
2215 case LYX_VALIGN_TOP:
2218 case LYX_VALIGN_MIDDLE:
2221 case LYX_VALIGN_BOTTOM:
2225 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2227 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2228 os << "\\begin{minipage}[";
2230 case LYX_VALIGN_TOP:
2233 case LYX_VALIGN_MIDDLE:
2236 case LYX_VALIGN_BOTTOM:
2240 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2248 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2249 bool ismulticol, bool ismultirow) const
2252 row_type const r = cellRow(cell);
2253 if (is_long_tabular && row_info[r].caption)
2257 if (getUsebox(cell) == BOX_PARBOX)
2259 else if (getUsebox(cell) == BOX_MINIPAGE) {
2260 os << "%\n\\end{minipage}";
2263 if (getRotateCell(cell)) {
2264 os << "%\n\\end{sideways}";
2276 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2277 OutputParams const & runparams) const
2279 if (!is_long_tabular)
2284 // the caption must be output before the headers
2285 if (haveLTCaption()) {
2286 for (row_type r = 0; r < nrows(); ++r) {
2287 if (row_info[r].caption) {
2288 ret += TeXRow(os, r, runparams);
2292 // output first header info
2293 // first header must be output before the header, otherwise the
2294 // correct caption placement becomes really weird
2295 if (haveLTFirstHead()) {
2296 if (endfirsthead.topDL) {
2300 for (row_type r = 0; r < nrows(); ++r) {
2301 if (row_info[r].endfirsthead) {
2302 ret += TeXRow(os, r, runparams);
2305 if (endfirsthead.bottomDL) {
2309 os << "\\endfirsthead\n";
2312 // output header info
2314 if (endfirsthead.empty && !haveLTFirstHead()) {
2315 os << "\\endfirsthead\n";
2318 if (endhead.topDL) {
2322 for (row_type r = 0; r < nrows(); ++r) {
2323 if (row_info[r].endhead) {
2324 ret += TeXRow(os, r, runparams);
2327 if (endhead.bottomDL) {
2331 os << "\\endhead\n";
2334 // output footer info
2336 if (endfoot.topDL) {
2340 for (row_type r = 0; r < nrows(); ++r) {
2341 if (row_info[r].endfoot) {
2342 ret += TeXRow(os, r, runparams);
2345 if (endfoot.bottomDL) {
2349 os << "\\endfoot\n";
2351 if (endlastfoot.empty && !haveLTLastFoot()) {
2352 os << "\\endlastfoot\n";
2356 // output lastfooter info
2357 if (haveLTLastFoot()) {
2358 if (endlastfoot.topDL) {
2362 for (row_type r = 0; r < nrows(); ++r) {
2363 if (row_info[r].endlastfoot) {
2364 ret += TeXRow(os, r, runparams);
2367 if (endlastfoot.bottomDL) {
2371 os << "\\endlastfoot\n";
2378 bool Tabular::isValidRow(row_type row) const
2380 if (!is_long_tabular)
2382 return !row_info[row].endhead && !row_info[row].endfirsthead
2383 && !row_info[row].endfoot && !row_info[row].endlastfoot
2384 && !row_info[row].caption;
2388 int Tabular::TeXRow(odocstream & os, row_type row,
2389 OutputParams const & runparams) const
2391 idx_type cell = cellIndex(row, 0);
2392 shared_ptr<InsetTableCell> inset = cellInset(cell);
2393 Paragraph const & par = inset->paragraphs().front();
2394 string const lang = par.getParLanguage(buffer().params())->lang();
2396 //output the top line
2397 int ret = TeXTopHLine(os, row, lang);
2399 if (row_info[row].top_space_default) {
2401 os << "\\addlinespace\n";
2403 os << "\\noalign{\\vskip\\doublerulesep}\n";
2405 } else if(!row_info[row].top_space.zero()) {
2407 os << "\\addlinespace["
2408 << from_ascii(row_info[row].top_space.asLatexString())
2411 os << "\\noalign{\\vskip"
2412 << from_ascii(row_info[row].top_space.asLatexString())
2417 bool ismulticol = false;
2418 bool ismultirow = false;
2419 for (col_type c = 0; c < ncols(); ++c) {
2420 if (isPartOfMultiColumn(row, c))
2423 if (isPartOfMultiRow(row, c) &&
2424 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2429 cell = cellIndex(row, c);
2430 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2431 shared_ptr<InsetTableCell> inset = cellInset(cell);
2433 Paragraph const & par = inset->paragraphs().front();
2434 bool rtl = par.isRTL(buffer().params())
2436 && getPWidth(cell).zero();
2440 par.getParLanguage(buffer().params())->lang();
2441 if (lang == "farsi")
2443 else if (lang == "arabic_arabi")
2445 // currently, remaning RTL languages are
2446 // arabic_arabtex and hebrew
2450 // pass to the OutputParams that we are in a cell and
2451 // which alignment we have set.
2452 // InsetNewline needs this context information.
2453 OutputParams newrp(runparams);
2454 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2455 ? OutputParams::PLAIN
2456 : OutputParams::ALIGNED;
2458 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2459 && cellInfo(cell).decimal_width != 0) {
2460 // copy cell and split in 2
2461 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2462 head.getText(0)->setMacrocontextPosition(
2463 cellInset(cell)->getText(0)->macrocontextPosition());
2464 head.setBuffer(buffer());
2465 bool hassep = false;
2466 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2467 head.latex(os, newrp);
2469 ret += tail.latex(os, newrp);
2470 } else if (!isPartOfMultiRow(row, c))
2471 ret += inset->latex(os, newrp);
2473 runparams.encoding = newrp.encoding;
2477 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2478 if (cell != getLastCellInRow(row)) { // not last cell in row
2482 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2483 // if no first header and no empty first header is used,
2484 // the caption needs to be terminated by \endfirsthead
2486 os << "\\endfirsthead";
2488 os << "\\tabularnewline";
2489 if (row_info[row].bottom_space_default) {
2491 os << "\\addlinespace";
2493 os << "[\\doublerulesep]";
2494 } else if (!row_info[row].bottom_space.zero()) {
2496 os << "\\addlinespace";
2498 << from_ascii(row_info[row].bottom_space.asLatexString())
2504 //output the bottom line
2505 ret += TeXBottomHLine(os, row, lang);
2507 if (row_info[row].interline_space_default) {
2509 os << "\\addlinespace\n";
2511 os << "\\noalign{\\vskip\\doublerulesep}\n";
2513 } else if (!row_info[row].interline_space.zero()) {
2515 os << "\\addlinespace["
2516 << from_ascii(row_info[row].interline_space.asLatexString())
2519 os << "\\noalign{\\vskip"
2520 << from_ascii(row_info[row].interline_space.asLatexString())
2528 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2532 //+---------------------------------------------------------------------
2533 //+ first the opening preamble +
2534 //+---------------------------------------------------------------------
2537 os << "\\begin{sideways}\n";
2540 if (is_long_tabular) {
2541 os << "\\begin{longtable}";
2542 switch (longtabular_alignment) {
2543 case LYX_LONGTABULAR_ALIGN_LEFT:
2546 case LYX_LONGTABULAR_ALIGN_CENTER:
2548 case LYX_LONGTABULAR_ALIGN_RIGHT:
2553 os << "\\begin{tabular}";
2554 switch (tabular_valignment) {
2555 case LYX_VALIGN_TOP:
2558 case LYX_VALIGN_MIDDLE:
2560 case LYX_VALIGN_BOTTOM:
2568 for (col_type c = 0; c < ncols(); ++c) {
2569 if (columnLeftLine(c))
2571 if (!column_info[c].align_special.empty()) {
2572 os << column_info[c].align_special;
2574 if (!column_info[c].p_width.zero()) {
2575 switch (column_info[c].alignment) {
2576 case LYX_ALIGN_LEFT:
2577 os << ">{\\raggedright}";
2579 case LYX_ALIGN_RIGHT:
2580 os << ">{\\raggedleft}";
2582 case LYX_ALIGN_CENTER:
2583 os << ">{\\centering}";
2585 case LYX_ALIGN_NONE:
2586 case LYX_ALIGN_BLOCK:
2587 case LYX_ALIGN_LAYOUT:
2588 case LYX_ALIGN_SPECIAL:
2589 case LYX_ALIGN_DECIMAL:
2593 switch (column_info[c].valignment) {
2594 case LYX_VALIGN_TOP:
2597 case LYX_VALIGN_MIDDLE:
2600 case LYX_VALIGN_BOTTOM:
2605 << from_ascii(column_info[c].p_width.asLatexString())
2608 switch (column_info[c].alignment) {
2609 case LYX_ALIGN_LEFT:
2612 case LYX_ALIGN_RIGHT:
2615 case LYX_ALIGN_DECIMAL:
2616 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2622 } // end if else !column_info[i].p_width
2623 } // end if else !column_info[i].align_special
2624 if (columnRightLine(c))
2630 ret += TeXLongtableHeaderFooter(os, runparams);
2632 //+---------------------------------------------------------------------
2633 //+ the single row and columns (cells) +
2634 //+---------------------------------------------------------------------
2636 for (row_type r = 0; r < nrows(); ++r) {
2637 if (isValidRow(r)) {
2638 ret += TeXRow(os, r, runparams);
2639 if (is_long_tabular && row_info[r].newpage) {
2640 os << "\\newpage\n";
2646 //+---------------------------------------------------------------------
2647 //+ the closing of the tabular +
2648 //+---------------------------------------------------------------------
2650 if (is_long_tabular)
2651 os << "\\end{longtable}";
2653 os << "\\end{tabular}";
2655 os << "\n\\end{sideways}";
2663 int Tabular::docbookRow(odocstream & os, row_type row,
2664 OutputParams const & runparams) const
2667 idx_type cell = getFirstCellInRow(row);
2670 for (col_type c = 0; c < ncols(); ++c) {
2671 if (isPartOfMultiColumn(row, c))
2674 os << "<entry align=\"";
2675 switch (getAlignment(cell)) {
2676 case LYX_ALIGN_LEFT:
2679 case LYX_ALIGN_RIGHT:
2687 os << "\" valign=\"";
2688 switch (getVAlignment(cell)) {
2689 case LYX_VALIGN_TOP:
2692 case LYX_VALIGN_BOTTOM:
2695 case LYX_VALIGN_MIDDLE:
2700 if (isMultiColumn(cell)) {
2701 os << " namest=\"col" << c << "\" ";
2702 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2706 ret += cellInset(cell)->docbook(os, runparams);
2715 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2719 //+---------------------------------------------------------------------
2720 //+ first the opening preamble +
2721 //+---------------------------------------------------------------------
2723 os << "<tgroup cols=\"" << ncols()
2724 << "\" colsep=\"1\" rowsep=\"1\">\n";
2726 for (col_type c = 0; c < ncols(); ++c) {
2727 os << "<colspec colname=\"col" << c << "\" align=\"";
2728 switch (column_info[c].alignment) {
2729 case LYX_ALIGN_LEFT:
2732 case LYX_ALIGN_RIGHT:
2740 if (runparams.flavor == OutputParams::XML)
2746 //+---------------------------------------------------------------------
2747 //+ Long Tabular case +
2748 //+---------------------------------------------------------------------
2750 // output caption info
2751 if (haveLTCaption()) {
2752 os << "<caption>\n";
2754 for (row_type r = 0; r < nrows(); ++r) {
2755 if (row_info[r].caption) {
2756 ret += docbookRow(os, r, runparams);
2759 os << "</caption>\n";
2762 // output header info
2763 if (haveLTHead() || haveLTFirstHead()) {
2766 for (row_type r = 0; r < nrows(); ++r) {
2767 if (row_info[r].endhead || row_info[r].endfirsthead) {
2768 ret += docbookRow(os, r, runparams);
2774 // output footer info
2775 if (haveLTFoot() || haveLTLastFoot()) {
2778 for (row_type r = 0; r < nrows(); ++r) {
2779 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2780 ret += docbookRow(os, r, runparams);
2787 //+---------------------------------------------------------------------
2788 //+ the single row and columns (cells) +
2789 //+---------------------------------------------------------------------
2793 for (row_type r = 0; r < nrows(); ++r) {
2794 if (isValidRow(r)) {
2795 ret += docbookRow(os, r, runparams);
2800 //+---------------------------------------------------------------------
2801 //+ the closing of the tabular +
2802 //+---------------------------------------------------------------------
2811 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2812 OutputParams const & runparams) const
2815 idx_type cell = getFirstCellInRow(row);
2817 xs << html::StartTag("tr");
2818 for (col_type c = 0; c < ncols(); ++c) {
2819 if (isPartOfMultiColumn(row, c))
2824 switch (getAlignment(cell)) {
2825 case LYX_ALIGN_LEFT:
2828 case LYX_ALIGN_RIGHT:
2836 attr << " valign='";
2837 switch (getVAlignment(cell)) {
2838 case LYX_VALIGN_TOP:
2841 case LYX_VALIGN_BOTTOM:
2844 case LYX_VALIGN_MIDDLE:
2849 if (isMultiColumn(cell))
2850 attr << " colspan='" << columnSpan(cell) << "'";
2852 xs << html::StartTag("td", attr.str());
2853 ret += cellInset(cell)->xhtml(xs, runparams);
2854 xs << html::EndTag("td");
2857 xs << html::EndTag("tr");
2862 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2865 // It's unclear to me if we need to mess with the long table stuff.
2866 // We can borrow that too from docbook, if so.
2868 xs << html::StartTag("tbody");
2869 for (row_type r = 0; r < nrows(); ++r) {
2870 if (isValidRow(r)) {
2871 ret += xhtmlRow(xs, r, runparams);
2874 xs << html::EndTag("tbody");
2879 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2880 vector<unsigned int> const & clen) const
2882 idx_type const fcell = getFirstCellInRow(row);
2883 idx_type const n = numberOfCellsInRow(row) + fcell;
2886 for (idx_type i = fcell; i < n; ++i) {
2896 for (idx_type i = fcell; i < n; ++i) {
2907 col_type column = cellColumn(i);
2908 int len = clen[column];
2909 while (column < ncols() - 1
2910 && isPartOfMultiColumn(row, ++column))
2911 len += clen[column] + 4;
2912 os << docstring(len, ch);
2927 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2928 vector<unsigned int> const & clen) const
2930 idx_type const fcell = getFirstCellInRow(row);
2931 idx_type const n = numberOfCellsInRow(row) + fcell;
2934 for (idx_type i = fcell; i < n; ++i) {
2935 if (bottomLine(i)) {
2944 for (idx_type i = fcell; i < n; ++i) {
2945 if (bottomLine(i)) {
2955 col_type column = cellColumn(i);
2956 int len = clen[column];
2957 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2958 len += clen[column] + 4;
2959 os << docstring(len, ch);
2960 if (bottomLine(i)) {
2974 void Tabular::plaintextPrintCell(odocstream & os,
2975 OutputParams const & runparams,
2976 idx_type cell, row_type row, col_type column,
2977 vector<unsigned int> const & clen,
2978 bool onlydata) const
2980 odocstringstream sstr;
2981 cellInset(cell)->plaintext(sstr, runparams);
2993 unsigned int len1 = sstr.str().length();
2994 unsigned int len2 = clen[column];
2995 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2996 len2 += clen[column] + 4;
2999 switch (getAlignment(cell)) {
3001 case LYX_ALIGN_LEFT:
3004 case LYX_ALIGN_RIGHT:
3008 case LYX_ALIGN_CENTER:
3014 os << docstring(len1, ' ') << sstr.str()
3015 << docstring(len2, ' ');
3017 if (rightLine(cell))
3024 void Tabular::plaintext(odocstream & os,
3025 OutputParams const & runparams, int const depth,
3026 bool onlydata, char_type delim) const
3028 // first calculate the width of the single columns
3029 vector<unsigned int> clen(ncols());
3032 // first all non multicolumn cells!
3033 for (col_type c = 0; c < ncols(); ++c) {
3035 for (row_type r = 0; r < nrows(); ++r) {
3036 idx_type cell = cellIndex(r, c);
3037 if (isMultiColumn(cell))
3039 odocstringstream sstr;
3040 cellInset(cell)->plaintext(sstr, runparams);
3041 if (clen[c] < sstr.str().length())
3042 clen[c] = sstr.str().length();
3045 // then all multicolumn cells!
3046 for (col_type c = 0; c < ncols(); ++c) {
3047 for (row_type r = 0; r < nrows(); ++r) {
3048 idx_type cell = cellIndex(r, c);
3049 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3051 odocstringstream sstr;
3052 cellInset(cell)->plaintext(sstr, runparams);
3053 int len = int(sstr.str().length());
3054 idx_type const n = columnSpan(cell);
3055 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3057 if (len > int(clen[c + n - 1]))
3058 clen[c + n - 1] = len;
3063 for (row_type r = 0; r < nrows(); ++r) {
3064 if (!onlydata && plaintextTopHLine(os, r, clen))
3065 os << docstring(depth * 2, ' ');
3066 for (col_type c = 0; c < ncols(); ++c) {
3067 if (isPartOfMultiColumn(r, c))
3069 if (onlydata && c > 0)
3070 // we don't use operator<< for single UCS4 character.
3071 // see explanation in docstream.h
3073 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3078 os << docstring(depth * 2, ' ');
3079 if (plaintextBottomHLine(os, r, clen))
3080 os << docstring(depth * 2, ' ');
3086 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3088 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3092 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3093 col_type column) const
3095 return cell_info[row][column].inset;
3099 void Tabular::setCellInset(row_type row, col_type column,
3100 shared_ptr<InsetTableCell> ins) const
3102 CellData & cd = cell_info[row][column];
3107 void Tabular::validate(LaTeXFeatures & features) const
3109 features.require("NeedTabularnewline");
3111 features.require("booktabs");
3112 if (is_long_tabular)
3113 features.require("longtable");
3115 features.require("rotating");
3116 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3117 if (isMultiRow(cell))
3118 features.require("multirow");
3119 if (getVAlignment(cell) != LYX_VALIGN_TOP
3120 || !getPWidth(cell).zero())
3121 features.require("array");
3122 cellInset(cell)->validate(features);
3127 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3129 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3130 ParagraphList::const_iterator cit = parlist.begin();
3131 ParagraphList::const_iterator end = parlist.end();
3133 for (; cit != end; ++cit)
3134 for (int i = 0; i < cit->size(); ++i)
3135 if (cit->isNewline(i))
3142 /////////////////////////////////////////////////////////////////////
3146 /////////////////////////////////////////////////////////////////////
3148 InsetTableCell::InsetTableCell(Buffer * buf)
3149 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3150 contentAlign(LYX_ALIGN_CENTER)
3154 bool InsetTableCell::forcePlainLayout(idx_type) const
3156 return !isFixedWidth;
3160 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3162 return isFixedWidth;
3166 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3167 FuncStatus & status) const
3170 switch (cmd.action()) {
3172 enabled = !forcePlainLayout();
3174 case LFUN_LAYOUT_PARAGRAPH:
3175 enabled = allowParagraphCustomization();
3178 return InsetText::getStatus(cur, cmd, status);
3180 status.setEnabled(enabled);
3184 docstring InsetTableCell::asString(bool intoInsets)
3187 if (paragraphs().empty())
3189 ParagraphList::const_iterator it = paragraphs().begin();
3190 ParagraphList::const_iterator en = paragraphs().end();
3192 for (; it != en; ++it) {
3197 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3203 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3206 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3207 return InsetText::xhtml(xs, rp);
3212 /////////////////////////////////////////////////////////////////////
3216 /////////////////////////////////////////////////////////////////////
3218 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3220 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3221 rowselect_(false), colselect_(false)
3226 InsetTabular::InsetTabular(InsetTabular const & tab)
3227 : Inset(tab), tabular(tab.tabular), scx_(0)
3232 InsetTabular::~InsetTabular()
3234 hideDialogs("tabular", this);
3238 void InsetTabular::setBuffer(Buffer & buf)
3240 tabular.setBuffer(buf);
3241 Inset::setBuffer(buf);
3245 bool InsetTabular::insetAllowed(InsetCode code) const
3250 case MATHMACRO_CODE:
3255 return tabular.is_long_tabular;
3263 void InsetTabular::write(ostream & os) const
3265 os << "Tabular" << endl;
3270 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3272 // FIXME: depending on the selection state, we could offer a different menu.
3273 return from_ascii("context-tabular");
3277 void InsetTabular::read(Lexer & lex)
3279 //bool const old_format = (lex.getString() == "\\LyXTable");
3287 string token = lex.getString();
3288 while (lex && token != "\\end_inset") {
3290 token = lex.getString();
3293 lex.printError("Missing \\end_inset at this point. ");
3297 int InsetTabular::rowFromY(Cursor & cur, int y) const
3299 // top y coordinate of tabular
3300 int h = yo(cur.bv()) - tabular.rowAscent(0);
3302 for (; r < tabular.nrows() && y > h; ++r)
3303 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3304 + tabular.interRowSpace(r);
3310 int InsetTabular::columnFromX(Cursor & cur, int x) const
3312 // left x coordinate of tabular
3313 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3315 for (; c < tabular.ncols() && x > w; ++c)
3316 w += tabular.cellWidth(c);
3321 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3323 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3324 // mi.base.textwidth << "\n";
3327 LASSERT(false, /**/);
3330 for (row_type r = 0; r < tabular.nrows(); ++r) {
3333 for (col_type c = 0; c < tabular.ncols(); ++c) {
3334 if (tabular.isPartOfMultiColumn(r, c)
3335 || tabular.isPartOfMultiRow(r, c))
3336 // multicolumn or multirow cell, but not first one
3338 idx_type const cell = tabular.cellIndex(r, c);
3341 Length const p_width = tabular.getPWidth(cell);
3342 if (!p_width.zero())
3343 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3344 tabular.cellInset(cell)->metrics(m, dim);
3345 if (!p_width.zero())
3346 dim.wid = m.base.textwidth;
3347 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3348 + tabular.interColumnSpace(cell);
3350 // FIXME(?): do we need a second metrics call?
3351 TextMetrics const & tm =
3352 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3354 // determine horiz offset because of decimal align (if necessary)
3355 int decimal_hoffset = 0;
3356 int decimal_width = 0;
3357 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3358 // make a copy which we will split in 2
3359 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3360 head.getText(0)->setMacrocontextPosition(
3361 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3362 head.setBuffer(tabular.buffer());
3363 // split in 2 and calculate width of each part
3364 bool hassep = false;
3365 InsetTableCell tail =
3366 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3368 head.metrics(m, dim1);
3369 decimal_hoffset = dim1.width();
3371 tail.metrics(m, dim1);
3372 decimal_width = dim1.width();
3375 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3376 tabular.cell_info[r][c].decimal_width = decimal_width;
3378 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3379 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3380 int const lastpardes = tm.last().second->descent()
3381 + TEXT_TO_INSET_OFFSET;
3383 switch (tabular.getVAlignment(cell)) {
3384 case Tabular::LYX_VALIGN_TOP:
3386 case Tabular::LYX_VALIGN_MIDDLE:
3387 offset = -(dim.des - lastpardes)/2;
3389 case Tabular::LYX_VALIGN_BOTTOM:
3390 offset = -(dim.des - lastpardes);
3393 tabular.cell_info[r][c].voffset = offset;
3394 maxasc = max(maxasc, dim.asc - offset);
3395 maxdes = max(maxdes, dim.des + offset);
3397 int const top_space = tabular.row_info[r].top_space_default ?
3398 default_line_space :
3399 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3400 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3401 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3402 default_line_space :
3403 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3404 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3406 tabular.updateColumnWidths();
3407 dim.asc = tabular.rowAscent(0);
3408 dim.des = tabular.height() - dim.asc;
3409 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3413 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3416 if (&cur.inset() == this && cur.selection()) {
3417 if (cur.selIsMultiCell()) {
3420 getSelection(cur, rs, re, cs, ce);
3422 idx_type const cell = tabular.cellIndex(row, col);
3423 col_type const cspan = tabular.columnSpan(cell);
3424 row_type const rspan = tabular.rowSpan(cell);
3425 if (col + cspan - 1 >= cs && col <= ce
3426 && row + rspan - 1 >= rs && row <= re)
3429 if (col == tabular.cellColumn(cur.idx())
3430 && row == tabular.cellRow(cur.idx())) {
3431 CursorSlice const & beg = cur.selBegin();
3432 CursorSlice const & end = cur.selEnd();
3434 if ((end.lastpos() > 0 || end.lastpit() > 0)
3435 && end.pos() == end.lastpos() && beg.pos() == 0
3436 && end.pit() == end.lastpit() && beg.pit() == 0)
3444 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3446 x += scx_ + ADD_TO_TABULAR_WIDTH;
3448 BufferView * bv = pi.base.bv;
3449 Cursor & cur = pi.base.bv->cursor();
3452 // FIXME: As the full background is painted in drawSelection(),
3453 // we have no choice but to do a full repaint for the Text cells.
3454 pi.full_repaint = true;
3456 bool const original_selection_state = pi.selected;
3459 first_visible_cell = Tabular::npos;
3460 for (row_type r = 0; r < tabular.nrows(); ++r) {
3462 for (col_type c = 0; c < tabular.ncols(); ++c) {
3463 if (tabular.isPartOfMultiColumn(r, c))
3466 idx = tabular.cellIndex(r, c);
3468 if (tabular.isPartOfMultiRow(r, c)) {
3469 nx += tabular.cellWidth(idx);
3473 if (first_visible_cell == Tabular::npos)
3474 first_visible_cell = idx;
3476 pi.selected |= isCellSelected(cur, r, c);
3477 int const cx = nx + tabular.textHOffset(idx);
3478 int const cy = y + tabular.textVOffset(idx);
3479 // Cache the Inset position.
3480 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3481 cell(idx)->draw(pi, cx, cy);
3482 drawCellLines(pi, nx, y, r, idx);
3483 nx += tabular.cellWidth(idx);
3484 pi.selected = original_selection_state;
3487 if (r + 1 < tabular.nrows())
3488 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3489 + tabular.interRowSpace(r + 1);
3494 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3496 Cursor & cur = pi.base.bv->cursor();
3499 x += scx_ + ADD_TO_TABULAR_WIDTH;
3501 // FIXME: it is wrong to completely paint the background
3502 // if we want to do single row painting.
3504 // Paint background of current tabular
3505 int const w = tabular.width();
3506 int const h = tabular.height();
3507 int yy = y - tabular.rowAscent(0);
3508 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3510 if (!cur.selection())
3512 if (&cur.inset() != this)
3517 bool const full_cell_selected = isCellSelected(cur,
3518 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3520 if (cur.selIsMultiCell() || full_cell_selected) {
3521 for (row_type r = 0; r < tabular.nrows(); ++r) {
3523 for (col_type c = 0; c < tabular.ncols(); ++c) {
3524 if (tabular.isPartOfMultiColumn(r, c))
3527 idx_type const cell = tabular.cellIndex(r, c);
3529 if (tabular.isPartOfMultiRow(r, c)) {
3530 xx += tabular.cellWidth(cell);
3533 int const w = tabular.cellWidth(cell);
3534 int const h = tabular.cellHeight(cell);
3535 int const yy = y - tabular.rowAscent(r);
3536 if (isCellSelected(cur, r, c))
3537 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3540 if (r + 1 < tabular.nrows())
3541 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3542 + tabular.interRowSpace(r + 1);
3546 x += cellXPos(cur.idx());
3547 x += tabular.textHOffset(cur.idx());
3548 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3553 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3554 row_type row, idx_type cell) const
3556 y -= tabular.rowAscent(row);
3557 int const w = tabular.cellWidth(cell);
3558 int const h = tabular.cellHeight(cell);
3559 Color const linecolor = pi.textColor(Color_tabularline);
3560 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3563 bool drawline = tabular.topLine(cell)
3564 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3565 pi.pain.line(x, y, x + w, y,
3566 drawline ? linecolor : gridcolor,
3567 drawline ? Painter::line_solid : Painter::line_onoffdash);
3570 drawline = tabular.bottomLine(cell);
3571 pi.pain.line(x, y + h, x + w, y + h,
3572 drawline ? linecolor : gridcolor,
3573 drawline ? Painter::line_solid : Painter::line_onoffdash);
3576 col_type const col = tabular.cellColumn(cell);
3577 drawline = tabular.leftLine(cell)
3578 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3579 pi.pain.line(x, y, x, y + h,
3580 drawline ? linecolor : gridcolor,
3581 drawline ? Painter::line_solid : Painter::line_onoffdash);
3584 x -= tabular.interColumnSpace(cell);
3585 drawline = tabular.rightLine(cell)
3586 || (col + 1 < tabular.ncols()
3587 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3588 pi.pain.line(x + w, y, x + w, y + h,
3589 drawline ? linecolor : gridcolor,
3590 drawline ? Painter::line_solid : Painter::line_onoffdash);
3594 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3596 //lyxerr << "InsetTabular::edit: " << this << endl;
3598 cur.setSelection(false);
3601 if (isRightToLeft(cur))
3602 cur.idx() = tabular.getLastCellInRow(0);
3608 if (isRightToLeft(cur))
3609 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3611 cur.idx() = tabular.numberofcells - 1;
3613 cur.pos() = cur.lastpos(); // FIXME crude guess
3615 cur.setCurrentFont();
3616 // FIXME: this accesses the position cache before it is initialized
3618 //cur.bv().fitCursor();
3622 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3624 // In a longtable, tell captions what the current float is
3625 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3626 string const saveflt = cnts.current_float();
3627 if (tabular.is_long_tabular)
3628 cnts.current_float("table");
3630 ParIterator it2 = it;
3632 size_t const end = it2.nargs();
3633 for ( ; it2.idx() < end; it2.top().forwardIdx())
3634 buffer().updateBuffer(it2, utype);
3637 if (tabular.is_long_tabular)
3638 cnts.current_float(saveflt);
3642 void InsetTabular::addToToc(DocIterator const & cpit)
3644 DocIterator dit = cpit;
3646 size_t const end = dit.nargs();
3647 for ( ; dit.idx() < end; dit.top().forwardIdx())
3648 cell(dit.idx())->addToToc(dit);
3652 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3654 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3655 << "\n cur:" << cur);
3656 CursorSlice sl = cur.top();
3657 Cursor & bvcur = cur.bv().cursor();
3659 FuncCode const act = cmd.action();
3663 case LFUN_MOUSE_PRESS: {
3664 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3666 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3667 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3668 row_type r = rowFromY(cur, cmd.y());
3669 cur.idx() = tabular.getFirstCellInRow(r);
3672 cur.idx() = tabular.getLastCellInRow(r);
3673 cur.pos() = cur.lastpos();
3674 cur.setSelection(true);
3680 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3681 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3682 || cmd.y() > y0 + tabular.height()) {
3683 col_type c = columnFromX(cur, cmd.x());
3684 cur.idx() = tabular.cellIndex(0, c);
3687 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3688 cur.pos() = cur.lastpos();
3689 cur.setSelection(true);
3694 // do not reset cursor/selection if we have selected
3695 // some cells (bug 2715).
3696 if (cmd.button() == mouse_button::button3
3697 && &bvcur.selBegin().inset() == this
3698 && bvcur.selIsMultiCell())
3701 // Let InsetTableCell do it
3702 cell(cur.idx())->dispatch(cur, cmd);
3705 case LFUN_MOUSE_MOTION:
3706 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3707 if (cmd.button() == mouse_button::button1) {
3708 // only accept motions to places not deeper nested than the real anchor
3709 if (!bvcur.realAnchor().hasPart(cur)) {
3713 // select (additional) row
3715 row_type r = rowFromY(cur, cmd.y());
3716 cur.idx() = tabular.getLastCellInRow(r);
3717 // we need to reset the cursor's pit and pos now, as the old ones
3718 // may no longer be valid.
3721 bvcur.setCursor(cur);
3722 bvcur.setSelection(true);
3725 // select (additional) column
3727 col_type c = columnFromX(cur, cmd.x());
3728 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3729 // we need to reset the cursor's pit and pos now, as the old ones
3730 // may no longer be valid.
3733 bvcur.setCursor(cur);
3734 bvcur.setSelection(true);
3737 // only update if selection changes
3738 if (bvcur.idx() == cur.idx() &&
3739 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3740 cur.noScreenUpdate();
3741 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3742 bvcur.setCursor(cur);
3743 bvcur.setSelection(true);
3744 // if this is a multicell selection, we just set the cursor to
3745 // the beginning of the cell's text.
3746 if (bvcur.selIsMultiCell()) {
3747 bvcur.pit() = bvcur.lastpit();
3748 bvcur.pos() = bvcur.lastpos();
3753 case LFUN_MOUSE_RELEASE:
3758 case LFUN_CELL_BACKWARD:
3760 cur.setSelection(false);
3763 case LFUN_CELL_FORWARD:
3765 cur.setSelection(false);
3768 case LFUN_CHAR_FORWARD_SELECT:
3769 case LFUN_CHAR_FORWARD:
3770 case LFUN_CHAR_BACKWARD_SELECT:
3771 case LFUN_CHAR_BACKWARD:
3772 case LFUN_CHAR_RIGHT_SELECT:
3773 case LFUN_CHAR_RIGHT:
3774 case LFUN_CHAR_LEFT_SELECT:
3775 case LFUN_CHAR_LEFT:
3776 case LFUN_WORD_FORWARD:
3777 case LFUN_WORD_FORWARD_SELECT:
3778 case LFUN_WORD_BACKWARD:
3779 case LFUN_WORD_BACKWARD_SELECT:
3780 case LFUN_WORD_RIGHT:
3781 case LFUN_WORD_RIGHT_SELECT:
3782 case LFUN_WORD_LEFT:
3783 case LFUN_WORD_LEFT_SELECT: {
3784 // determine whether we move to next or previous cell, where to enter
3785 // the new cell from, and which command to "finish" (i.e., exit the
3788 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3789 FuncCode finish_lfun;
3791 if (act == LFUN_CHAR_FORWARD
3792 || act == LFUN_CHAR_FORWARD_SELECT
3793 || act == LFUN_WORD_FORWARD
3794 || act == LFUN_WORD_FORWARD_SELECT) {
3796 finish_lfun = LFUN_FINISHED_FORWARD;
3798 else if (act == LFUN_CHAR_BACKWARD
3799 || act == LFUN_CHAR_BACKWARD_SELECT
3800 || act == LFUN_WORD_BACKWARD
3801 || act == LFUN_WORD_BACKWARD_SELECT) {
3803 finish_lfun = LFUN_FINISHED_BACKWARD;
3805 // LEFT or RIGHT commands --- the interpretation will depend on the
3806 // table's direction.
3808 bool const right = act == LFUN_CHAR_RIGHT
3809 || act == LFUN_CHAR_RIGHT_SELECT
3810 || act == LFUN_WORD_RIGHT
3811 || act == LFUN_WORD_RIGHT_SELECT;
3812 next_cell = isRightToLeft(cur) != right;
3814 if (lyxrc.visual_cursor)
3815 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3817 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3820 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3821 || act == LFUN_CHAR_BACKWARD_SELECT
3822 || act == LFUN_CHAR_RIGHT_SELECT
3823 || act == LFUN_CHAR_LEFT_SELECT
3824 || act == LFUN_WORD_FORWARD_SELECT
3825 || act == LFUN_WORD_RIGHT_SELECT
3826 || act == LFUN_WORD_BACKWARD_SELECT
3827 || act == LFUN_WORD_LEFT_SELECT;
3829 // If we have a multicell selection or we're
3830 // not doing some LFUN_*_SELECT thing anyway...
3831 if (!cur.selIsMultiCell() || !select) {
3832 col_type const c = tabular.cellColumn(cur.idx());
3833 row_type const r = tabular.cellRow(cur.idx());
3834 // Are we trying to select the whole cell and is the whole cell
3835 // not yet selected?
3836 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3837 ((next_cell && cur.pit() == cur.lastpit()
3838 && cur.pos() == cur.lastpos())
3839 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3841 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3843 // ...try to dispatch to the cell's inset.
3844 cell(cur.idx())->dispatch(cur, cmd);
3846 // When we already have a selection we want to select the whole cell
3847 // before going to the next cell.
3848 if (select_whole && !empty_cell){
3849 getText(cur.idx())->selectAll(cur);
3854 // FIXME: When we support the selection of an empty cell, remove
3855 // the !empty_cell from this condition. For now we jump to the next
3856 // cell if the current cell is empty.
3857 if (cur.result().dispatched() && !empty_cell)
3861 // move to next/prev cell, as appropriate
3862 // note that we will always do this if we're selecting and we have
3863 // a multicell selection
3864 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3865 << " cell from: " << int(entry_from));
3867 moveNextCell(cur, entry_from);
3869 movePrevCell(cur, entry_from);
3870 // if we're exiting the table, call the appropriate FINISHED lfun
3871 if (sl == cur.top())
3872 cmd = FuncRequest(finish_lfun);
3879 case LFUN_DOWN_SELECT:
3881 if (!(cur.selection() && cur.selIsMultiCell()))
3882 cell(cur.idx())->dispatch(cur, cmd);
3884 cur.dispatched(); // override the cell's decision
3885 if (sl == cur.top()) {
3886 // if our Text didn't do anything to the cursor
3887 // then we try to put the cursor into the cell below
3888 // setting also the right targetX.
3889 cur.selHandle(act == LFUN_DOWN_SELECT);
3890 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3891 cur.idx() = tabular.cellBelow(cur.idx());
3893 TextMetrics const & tm =
3894 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3895 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3896 cur.setCurrentFont();
3899 if (sl == cur.top()) {
3900 // we trick it to go to forward after leaving the
3902 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3905 if (cur.selIsMultiCell()) {
3906 cur.pit() = cur.lastpit();
3907 cur.pos() = cur.lastpos();
3908 cur.setCurrentFont();
3913 case LFUN_UP_SELECT:
3915 if (!(cur.selection() && cur.selIsMultiCell()))
3916 cell(cur.idx())->dispatch(cur, cmd);
3917 cur.dispatched(); // override the cell's decision
3918 if (sl == cur.top()) {
3919 // if our Text didn't do anything to the cursor
3920 // then we try to put the cursor into the cell above
3921 // setting also the right targetX.
3922 cur.selHandle(act == LFUN_UP_SELECT);
3923 if (tabular.cellRow(cur.idx()) != 0) {
3924 cur.idx() = tabular.cellAbove(cur.idx());
3925 cur.pit() = cur.lastpit();
3926 Text const * text = cell(cur.idx())->getText(0);
3927 TextMetrics const & tm = cur.bv().textMetrics(text);
3928 ParagraphMetrics const & pm =
3929 tm.parMetrics(cur.lastpit());
3930 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3931 cur.setCurrentFont();
3934 if (sl == cur.top()) {
3935 cmd = FuncRequest(LFUN_UP);
3938 if (cur.selIsMultiCell()) {
3940 cur.pos() = cur.lastpos();
3941 cur.setCurrentFont();
3946 // case LFUN_SCREEN_DOWN: {
3947 // //if (hasSelection())
3948 // // cur.selection() = false;
3949 // col_type const col = tabular.cellColumn(cur.idx());
3950 // int const t = cur.bv().top_y() + cur.bv().height();
3951 // if (t < yo() + tabular.getHeightOfTabular()) {
3952 // cur.bv().scrollDocView(t, true);
3953 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3955 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3962 // case LFUN_SCREEN_UP: {
3963 // //if (hasSelection())
3964 // // cur.selection() = false;
3965 // col_type const col = tabular.cellColumn(cur.idx());
3966 // int const t = cur.bv().top_y() + cur.bv().height();
3968 // cur.bv().scrollDocView(t, true);
3972 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3976 // cur.par() = cur.lastpar();
3977 // cur.pos() = cur.lastpos();
3981 case LFUN_LAYOUT_TABULAR:
3982 cur.bv().showDialog("tabular");
3985 case LFUN_INSET_MODIFY:
3986 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3990 // insert file functions
3991 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3992 case LFUN_FILE_INSERT_PLAINTEXT:
3994 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3995 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3996 FileName(to_utf8(cmd.argument())));
3999 cur.recordUndoInset(INSERT_UNDO);
4000 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4001 // content has been replaced,
4002 // so cursor might be invalid
4003 cur.pos() = cur.lastpos();
4004 cur.pit() = cur.lastpit();
4005 bvcur.setCursor(cur);
4012 if (cur.selIsMultiCell()) {
4013 if (copySelection(cur)) {
4014 cur.recordUndoInset(DELETE_UNDO);
4018 cell(cur.idx())->dispatch(cur, cmd);
4021 case LFUN_SELF_INSERT:
4022 if (cur.selIsMultiCell()) {
4023 cur.recordUndoInset(DELETE_UNDO);
4026 cell(cur.idx())->dispatch(cur, cmd);
4029 case LFUN_CHAR_DELETE_BACKWARD:
4030 case LFUN_CHAR_DELETE_FORWARD:
4031 if (cur.selIsMultiCell()) {
4032 cur.recordUndoInset(DELETE_UNDO);
4035 cell(cur.idx())->dispatch(cur, cmd);
4039 if (!cur.selection())
4041 if (cur.selIsMultiCell()) {
4045 cell(cur.idx())->dispatch(cur, cmd);
4048 case LFUN_CLIPBOARD_PASTE:
4049 case LFUN_PRIMARY_SELECTION_PASTE: {
4050 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4051 theClipboard().getAsText() :
4052 theSelection().get();
4055 // pass to InsertPlaintextString, but
4056 // only if we have multi-cell content
4057 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4058 cur.recordUndoInset(INSERT_UNDO);
4059 if (insertPlaintextString(cur.bv(), clip, false)) {
4060 // content has been replaced,
4061 // so cursor might be invalid
4062 cur.pos() = cur.lastpos();
4063 cur.pit() = cur.lastpit();
4064 bvcur.setCursor(cur);
4068 // Let the cell handle normal text
4069 cell(cur.idx())->dispatch(cur, cmd);
4074 if (!tabularStackDirty()) {
4075 if (!cur.selIsMultiCell())
4076 cell(cur.idx())->dispatch(cur, cmd);
4079 if (theClipboard().isInternal() ||
4080 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4081 cur.recordUndoInset(INSERT_UNDO);
4082 pasteClipboard(cur);
4086 case LFUN_FONT_EMPH:
4087 case LFUN_FONT_BOLD:
4088 case LFUN_FONT_BOLDSYMBOL:
4089 case LFUN_FONT_ROMAN:
4090 case LFUN_FONT_NOUN:
4091 case LFUN_FONT_ITAL:
4092 case LFUN_FONT_FRAK:
4093 case LFUN_FONT_TYPEWRITER:
4094 case LFUN_FONT_SANS:
4095 case LFUN_TEXTSTYLE_APPLY:
4096 case LFUN_TEXTSTYLE_UPDATE:
4097 case LFUN_FONT_SIZE:
4098 case LFUN_FONT_UNDERLINE:
4099 case LFUN_FONT_STRIKEOUT:
4100 case LFUN_FONT_UULINE:
4101 case LFUN_FONT_UWAVE:
4103 case LFUN_WORD_CAPITALIZE:
4104 case LFUN_WORD_UPCASE:
4105 case LFUN_WORD_LOWCASE:
4106 case LFUN_CHARS_TRANSPOSE:
4107 if (cur.selIsMultiCell()) {
4110 getSelection(cur, rs, re, cs, ce);
4111 Cursor tmpcur = cur;
4112 for (row_type r = rs; r <= re; ++r) {
4113 for (col_type c = cs; c <= ce; ++c) {
4114 // cursor follows cell:
4115 tmpcur.idx() = tabular.cellIndex(r, c);
4116 // select this cell only:
4119 tmpcur.resetAnchor();
4120 tmpcur.pit() = tmpcur.lastpit();
4121 tmpcur.pos() = tmpcur.top().lastpos();
4122 tmpcur.setCursor(tmpcur);
4123 tmpcur.setSelection();
4124 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4129 cell(cur.idx())->dispatch(cur, cmd);
4133 case LFUN_INSET_SETTINGS:
4134 // relay this lfun to Inset, not to the cell.
4135 Inset::doDispatch(cur, cmd);
4139 // we try to handle this event in the insets dispatch function.
4140 cell(cur.idx())->dispatch(cur, cmd);
4146 // function sets an object as defined in func_status.h:
4147 // states OK, Unknown, Disabled, On, Off.
4148 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4149 FuncStatus & status) const
4151 switch (cmd.action()) {
4152 case LFUN_INSET_MODIFY: {
4153 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4156 string const s = cmd.getArg(1);
4157 // FIXME: We only check for the very first argument...
4158 int action = Tabular::LAST_ACTION;
4160 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4161 if (tabularFeature[i].feature == s) {
4162 action = tabularFeature[i].action;
4166 if (action == Tabular::LAST_ACTION) {
4168 status.setUnknown(true);
4172 string const argument = cmd.getLongArg(2);
4174 row_type sel_row_start = 0;
4175 row_type sel_row_end = 0;
4176 col_type sel_col_start = 0;
4177 col_type sel_col_end = 0;
4178 Tabular::ltType dummyltt;
4181 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4184 case Tabular::SET_PWIDTH:
4185 case Tabular::SET_MPWIDTH:
4186 case Tabular::SET_SPECIAL_COLUMN:
4187 case Tabular::SET_SPECIAL_MULTICOLUMN:
4188 case Tabular::SET_SPECIAL_MULTIROW:
4189 case Tabular::APPEND_ROW:
4190 case Tabular::APPEND_COLUMN:
4191 case Tabular::DELETE_ROW:
4192 case Tabular::DELETE_COLUMN:
4193 case Tabular::COPY_ROW:
4194 case Tabular::COPY_COLUMN:
4195 case Tabular::SET_TOP_SPACE:
4196 case Tabular::SET_BOTTOM_SPACE:
4197 case Tabular::SET_INTERLINE_SPACE:
4201 case Tabular::SET_DECIMAL_POINT:
4203 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4206 case Tabular::MULTICOLUMN:
4207 // If a row is set as longtable caption, it must not be allowed
4208 // to unset that this row is a multicolumn.
4209 status.setEnabled(sel_row_start == sel_row_end
4210 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4211 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4214 case Tabular::MULTIROW:
4215 // If a row is set as longtable caption, it must not be allowed
4216 // to unset that this row is a multirow.
4217 status.setEnabled(sel_col_start == sel_col_end
4218 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4219 status.setOnOff(tabular.isMultiRow(cur.idx()));
4222 case Tabular::SET_ALL_LINES:
4223 case Tabular::UNSET_ALL_LINES:
4224 case Tabular::SET_BORDER_LINES:
4225 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4228 case Tabular::SET_LINE_TOP:
4229 case Tabular::SET_LINE_BOTTOM:
4230 case Tabular::SET_LINE_LEFT:
4231 case Tabular::SET_LINE_RIGHT:
4232 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4235 case Tabular::TOGGLE_LINE_TOP:
4236 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4237 status.setOnOff(tabular.topLine(cur.idx()));
4240 case Tabular::TOGGLE_LINE_BOTTOM:
4241 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4242 status.setOnOff(tabular.bottomLine(cur.idx()));
4245 case Tabular::TOGGLE_LINE_LEFT:
4246 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4247 status.setOnOff(tabular.leftLine(cur.idx()));
4250 case Tabular::TOGGLE_LINE_RIGHT:
4251 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4252 status.setOnOff(tabular.rightLine(cur.idx()));
4255 case Tabular::M_ALIGN_LEFT:
4257 case Tabular::ALIGN_LEFT:
4258 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4259 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4262 case Tabular::M_ALIGN_RIGHT:
4264 case Tabular::ALIGN_RIGHT:
4265 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4266 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4269 case Tabular::M_ALIGN_CENTER:
4271 case Tabular::ALIGN_CENTER:
4272 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4273 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4276 case Tabular::ALIGN_BLOCK:
4277 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4278 && !tabular.isMultiRow(cur.idx()));
4279 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4282 case Tabular::ALIGN_DECIMAL:
4283 status.setEnabled(!tabular.isMultiRow(cur.idx())
4284 && !tabular.isMultiColumn(cur.idx()));
4285 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4288 case Tabular::M_VALIGN_TOP:
4290 case Tabular::VALIGN_TOP:
4291 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4292 && !tabular.isMultiRow(cur.idx()));
4294 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4297 case Tabular::M_VALIGN_BOTTOM:
4299 case Tabular::VALIGN_BOTTOM:
4300 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4301 && !tabular.isMultiRow(cur.idx()));
4303 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4306 case Tabular::M_VALIGN_MIDDLE:
4308 case Tabular::VALIGN_MIDDLE:
4309 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4310 && !tabular.isMultiRow(cur.idx()));
4312 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4315 case Tabular::SET_LONGTABULAR:
4316 status.setOnOff(tabular.is_long_tabular);
4319 case Tabular::UNSET_LONGTABULAR:
4320 status.setOnOff(!tabular.is_long_tabular);
4323 case Tabular::TOGGLE_ROTATE_TABULAR:
4324 case Tabular::SET_ROTATE_TABULAR:
4325 status.setOnOff(tabular.rotate);
4328 case Tabular::TABULAR_VALIGN_TOP:
4329 status.setOnOff(tabular.tabular_valignment
4330 == Tabular::LYX_VALIGN_TOP);
4332 case Tabular::TABULAR_VALIGN_MIDDLE:
4333 status.setOnOff(tabular.tabular_valignment
4334 == Tabular::LYX_VALIGN_MIDDLE);
4336 case Tabular::TABULAR_VALIGN_BOTTOM:
4337 status.setOnOff(tabular.tabular_valignment
4338 == Tabular::LYX_VALIGN_BOTTOM);
4341 case Tabular::LONGTABULAR_ALIGN_LEFT:
4342 status.setOnOff(tabular.longtabular_alignment
4343 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4345 case Tabular::LONGTABULAR_ALIGN_CENTER:
4346 status.setOnOff(tabular.longtabular_alignment
4347 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4349 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4350 status.setOnOff(tabular.longtabular_alignment
4351 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4354 case Tabular::UNSET_ROTATE_TABULAR:
4355 status.setOnOff(!tabular.rotate);
4358 case Tabular::TOGGLE_ROTATE_CELL:
4359 case Tabular::SET_ROTATE_CELL:
4360 status.setOnOff(!oneCellHasRotationState(false,
4361 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4364 case Tabular::UNSET_ROTATE_CELL:
4365 status.setOnOff(!oneCellHasRotationState(true,
4366 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4369 case Tabular::SET_USEBOX:
4370 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4373 // every row can only be one thing:
4374 // either a footer or header or caption
4375 case Tabular::SET_LTFIRSTHEAD:
4376 status.setEnabled(sel_row_start == sel_row_end
4377 && !tabular.ltCaption(sel_row_start));
4378 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4381 case Tabular::UNSET_LTFIRSTHEAD:
4382 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4385 case Tabular::SET_LTHEAD:
4386 status.setEnabled(sel_row_start == sel_row_end
4387 && !tabular.ltCaption(sel_row_start));
4388 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4391 case Tabular::UNSET_LTHEAD:
4392 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4395 case Tabular::SET_LTFOOT:
4396 status.setEnabled(sel_row_start == sel_row_end
4397 && !tabular.ltCaption(sel_row_start));
4398 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4401 case Tabular::UNSET_LTFOOT:
4402 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4405 case Tabular::SET_LTLASTFOOT:
4406 status.setEnabled(sel_row_start == sel_row_end
4407 && !tabular.ltCaption(sel_row_start));
4408 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4411 case Tabular::UNSET_LTLASTFOOT:
4412 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4415 case Tabular::SET_LTNEWPAGE:
4416 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4419 // only one row can be the caption
4420 // and a multirow cannot be set as caption
4421 case Tabular::TOGGLE_LTCAPTION:
4422 status.setEnabled(sel_row_start == sel_row_end
4423 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4424 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4425 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4426 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4427 && (!tabular.haveLTCaption()
4428 || tabular.ltCaption(sel_row_start))
4429 && !tabular.isMultiRow(sel_row_start));
4430 status.setOnOff(tabular.ltCaption(sel_row_start));
4433 case Tabular::SET_BOOKTABS:
4434 status.setOnOff(tabular.use_booktabs);
4437 case Tabular::UNSET_BOOKTABS:
4438 status.setOnOff(!tabular.use_booktabs);
4443 status.setEnabled(false);
4449 // These are only enabled inside tabular
4450 case LFUN_CELL_BACKWARD:
4451 case LFUN_CELL_FORWARD:
4452 status.setEnabled(true);
4455 // disable these with multiple cells selected
4456 case LFUN_INSET_INSERT:
4457 case LFUN_TABULAR_INSERT:
4458 case LFUN_FLEX_INSERT:
4459 case LFUN_FLOAT_INSERT:
4460 case LFUN_FLOAT_WIDE_INSERT:
4461 case LFUN_FOOTNOTE_INSERT:
4462 case LFUN_MARGINALNOTE_INSERT:
4463 case LFUN_MATH_INSERT:
4464 case LFUN_MATH_MODE:
4465 case LFUN_MATH_MUTATE:
4466 case LFUN_MATH_DISPLAY:
4467 case LFUN_NOTE_INSERT:
4468 case LFUN_ARGUMENT_INSERT:
4469 case LFUN_BOX_INSERT:
4470 case LFUN_BRANCH_INSERT:
4471 case LFUN_PHANTOM_INSERT:
4472 case LFUN_WRAP_INSERT:
4473 case LFUN_PREVIEW_INSERT:
4474 case LFUN_ERT_INSERT: {
4475 if (cur.selIsMultiCell()) {
4476 status.setEnabled(false);
4479 return cell(cur.idx())->getStatus(cur, cmd, status);
4482 // disable in non-fixed-width cells
4483 case LFUN_NEWLINE_INSERT:
4484 case LFUN_BREAK_PARAGRAPH: {
4485 if (tabular.getPWidth(cur.idx()).zero()) {
4486 status.setEnabled(false);
4489 return cell(cur.idx())->getStatus(cur, cmd, status);
4492 case LFUN_NEWPAGE_INSERT:
4493 status.setEnabled(false);
4497 if (tabularStackDirty() && theClipboard().isInternal()) {
4498 if (cur.selIsMultiCell()) {
4501 getSelection(cur, rs, re, cs, ce);
4502 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4503 && paste_tabular->nrows() == re - rs + 1)
4504 status.setEnabled(true);
4506 status.setEnabled(false);
4507 status.message(_("Selection size should match clipboard content."));
4510 status.setEnabled(true);
4513 return cell(cur.idx())->getStatus(cur, cmd, status);
4515 case LFUN_INSET_SETTINGS:
4516 // relay this lfun to Inset, not to the cell.
4517 return Inset::getStatus(cur, cmd, status);
4520 // we try to handle this event in the insets dispatch function.
4521 return cell(cur.idx())->getStatus(cur, cmd, status);
4527 Inset::DisplayType InsetTabular::display() const
4529 if (tabular.is_long_tabular) {
4530 switch (tabular.longtabular_alignment) {
4531 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4533 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4535 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4545 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4547 return tabular.latex(os, runparams);
4551 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4553 os << '\n'; // output table on a new line
4554 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4555 tabular.plaintext(os, runparams, dp, false, 0);
4556 return PLAINTEXT_NEWLINE;
4560 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4565 // FIXME: Why not pass a proper DocIterator here?
4567 // if the table is inside a float it doesn't need the informaltable
4568 // wrapper. Search for it.
4569 for (master = owner(); master; master = master->owner())
4570 if (master->lyxCode() == FLOAT_CODE)
4575 os << "<informaltable>";
4578 ret += tabular.docbook(os, runparams);
4580 os << "</informaltable>";
4587 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4590 // It'd be better to be able to get this from an InsetLayout, but at present
4591 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4592 xs << html::StartTag("table");
4593 docstring ret = tabular.xhtml(xs, rp);
4594 xs << html::EndTag("table");
4599 void InsetTabular::validate(LaTeXFeatures & features) const
4601 tabular.validate(features);
4603 // It'd be better to be able to get this from an InsetLayout, but at present
4604 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4605 if (features.runparams().flavor == OutputParams::HTML)
4606 features.addPreambleSnippet("<style type=\"text/css\">\n"
4607 "table { border: 1px solid black; display: inline-block; }\n"
4608 "td { border: 1px solid black; padding: 0.5ex; }\n"
4613 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4615 return tabular.cellInset(idx);
4619 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4621 return tabular.cellInset(idx);
4625 void InsetTabular::cursorPos(BufferView const & bv,
4626 CursorSlice const & sl, bool boundary, int & x, int & y) const
4628 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4630 int const row = tabular.cellRow(sl.idx());
4631 int const col = tabular.cellColumn(sl.idx());
4633 // y offset correction
4634 for (int r = 0; r < row; ++r)
4635 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4636 + tabular.interRowSpace(r + 1);
4638 y += tabular.textVOffset(sl.idx());
4640 // x offset correction
4641 for (int c = 0; c < col; ++c)
4642 x += tabular.column_info[c].width;
4644 x += tabular.textHOffset(sl.idx());
4645 x += ADD_TO_TABULAR_WIDTH;
4650 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4654 Inset const & inset = *tabular.cellInset(cell);
4655 Point o = bv.coordCache().getInsets().xy(&inset);
4656 int const xbeg = o.x_ - tabular.textHOffset(cell);
4657 int const xend = xbeg + tabular.cellWidth(cell);
4658 row_type const row = tabular.cellRow(cell);
4659 int const ybeg = o.y_ - tabular.rowAscent(row)
4660 - tabular.interRowSpace(row);
4661 int const yend = ybeg + tabular.cellHeight(cell);
4673 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4674 // << " ybeg=" << ybeg << " yend=" << yend
4675 // << " xx=" << xx << " yy=" << yy
4676 // << " dist=" << xx + yy << endl;
4681 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4683 //lyxerr << "InsetTabular::editXY: " << this << endl;
4684 cur.setSelection(false);
4686 cur.idx() = getNearestCell(cur.bv(), x, y);
4688 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4692 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4694 cur.idx() = getNearestCell(cur.bv(), x, y);
4695 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4699 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4701 idx_type idx_min = 0;
4702 int dist_min = numeric_limits<int>::max();
4703 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4704 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4705 int const d = dist(bv, i, x, y);
4716 int InsetTabular::cellXPos(idx_type const cell) const
4718 col_type col = tabular.cellColumn(cell);
4720 for (col_type c = 0; c < col; ++c)
4721 lx += tabular.column_info[c].width;
4727 void InsetTabular::resetPos(Cursor & cur) const
4729 BufferView & bv = cur.bv();
4730 int const maxwidth = bv.workWidth();
4732 int const scx_old = scx_;
4733 int const i = cur.find(this);
4738 int const X2 = maxwidth;
4739 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4740 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4741 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4744 scx_ = X1 + 20 - x1;
4746 scx_ = X2 - 20 - x2;
4751 // only update if offset changed
4752 if (scx_ != scx_old)
4753 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4757 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4759 row_type const row = tabular.cellRow(cur.idx());
4760 col_type const col = tabular.cellColumn(cur.idx());
4762 if (isRightToLeft(cur)) {
4763 if (tabular.cellColumn(cur.idx()) == 0) {
4764 if (row == tabular.nrows() - 1)
4766 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4771 cur.idx() = tabular.getLastCellInRow(row - 1);
4773 cur.idx() = tabular.cellIndex(row, col - 1);
4776 if (tabular.isLastCell(cur.idx()))
4778 if (cur.idx() == tabular.getLastCellInRow(row))
4779 cur.idx() = tabular.cellIndex(row + 1, 0);
4781 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4782 cur.idx() = tabular.cellIndex(row, colnextcell);
4786 cur.boundary(false);
4788 if (cur.selIsMultiCell()) {
4789 cur.pit() = cur.lastpit();
4790 cur.pos() = cur.lastpos();
4798 // in visual mode, place cursor at extreme left or right
4800 switch(entry_from) {
4802 case ENTRY_DIRECTION_RIGHT:
4803 cur.posVisToRowExtremity(false /* !left */);
4805 case ENTRY_DIRECTION_LEFT:
4806 cur.posVisToRowExtremity(true /* left */);
4808 case ENTRY_DIRECTION_IGNORE:
4809 // nothing to do in this case
4813 cur.setCurrentFont();
4818 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4820 row_type const row = tabular.cellRow(cur.idx());
4821 col_type const col = tabular.cellColumn(cur.idx());
4823 if (isRightToLeft(cur)) {
4824 if (cur.idx() == tabular.getLastCellInRow(row)) {
4827 cur.idx() = tabular.getFirstCellInRow(row);
4828 cur.idx() = tabular.cellAbove(cur.idx());
4830 if (tabular.isLastCell(cur.idx()))
4832 if (cur.idx() == tabular.getLastCellInRow(row))
4833 cur.idx() = tabular.cellIndex(row + 1, 0);
4835 cur.idx() = tabular.cellIndex(row, col + 1);
4838 if (cur.idx() == 0) // first cell
4841 cur.idx() = tabular.getLastCellInRow(row - 1);
4843 cur.idx() = tabular.cellIndex(row, col - 1);
4846 if (cur.selIsMultiCell()) {
4847 cur.pit() = cur.lastpit();
4848 cur.pos() = cur.lastpos();
4853 cur.pit() = cur.lastpit();
4854 cur.pos() = cur.lastpos();
4856 // in visual mode, place cursor at extreme left or right
4858 switch(entry_from) {
4860 case ENTRY_DIRECTION_RIGHT:
4861 cur.posVisToRowExtremity(false /* !left */);
4863 case ENTRY_DIRECTION_LEFT:
4864 cur.posVisToRowExtremity(true /* left */);
4866 case ENTRY_DIRECTION_IGNORE:
4867 // nothing to do in this case
4871 cur.setCurrentFont();
4876 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4878 istringstream is(argument);
4881 if (insetCode(s) != TABULAR_CODE)
4885 size_t safe_guard = 0;
4890 if (safe_guard > 1000) {
4891 LYXERR0("parameter max count reached!");
4895 Tabular::Feature action = Tabular::LAST_ACTION;
4898 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4899 if (s != tabularFeature[i].feature)
4902 action = tabularFeature[i].action;
4905 if (action == Tabular::LAST_ACTION) {
4906 LYXERR0("Feature not found " << s);
4910 if (tabularFeature[i].need_value)
4912 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4913 tabularFeatures(cur, action, val);
4919 static void checkLongtableSpecial(Tabular::ltType & ltt,
4920 string const & special, bool & flag)
4922 if (special == "dl_above") {
4925 } else if (special == "dl_below") {
4926 ltt.bottomDL = flag;
4928 } else if (special == "empty") {
4938 bool InsetTabular::oneCellHasRotationState(bool rotated,
4939 row_type row_start, row_type row_end,
4940 col_type col_start, col_type col_end) const
4942 for (row_type r = row_start; r <= row_end; ++r)
4943 for (col_type c = col_start; c <= col_end; ++c)
4944 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4950 void InsetTabular::tabularFeatures(Cursor & cur,
4951 Tabular::Feature feature, string const & value)
4953 col_type sel_col_start;
4954 col_type sel_col_end;
4955 row_type sel_row_start;
4956 row_type sel_row_end;
4957 bool setLines = false;
4958 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4959 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4963 case Tabular::M_ALIGN_LEFT:
4964 case Tabular::ALIGN_LEFT:
4965 setAlign = LYX_ALIGN_LEFT;
4968 case Tabular::M_ALIGN_RIGHT:
4969 case Tabular::ALIGN_RIGHT:
4970 setAlign = LYX_ALIGN_RIGHT;
4973 case Tabular::M_ALIGN_CENTER:
4974 case Tabular::ALIGN_CENTER:
4975 setAlign = LYX_ALIGN_CENTER;
4978 case Tabular::ALIGN_BLOCK:
4979 setAlign = LYX_ALIGN_BLOCK;
4982 case Tabular::ALIGN_DECIMAL:
4983 setAlign = LYX_ALIGN_DECIMAL;
4986 case Tabular::M_VALIGN_TOP:
4987 case Tabular::VALIGN_TOP:
4988 setVAlign = Tabular::LYX_VALIGN_TOP;
4991 case Tabular::M_VALIGN_BOTTOM:
4992 case Tabular::VALIGN_BOTTOM:
4993 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4996 case Tabular::M_VALIGN_MIDDLE:
4997 case Tabular::VALIGN_MIDDLE:
4998 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5005 cur.recordUndoInset(ATOMIC_UNDO);
5007 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5008 row_type const row = tabular.cellRow(cur.idx());
5009 col_type const column = tabular.cellColumn(cur.idx());
5011 Tabular::ltType ltt;
5015 case Tabular::SET_PWIDTH: {
5016 Length const len(value);
5017 tabular.setColumnPWidth(cur, cur.idx(), len);
5019 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5020 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5024 case Tabular::SET_MPWIDTH:
5025 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5028 case Tabular::SET_SPECIAL_COLUMN:
5029 case Tabular::SET_SPECIAL_MULTICOLUMN:
5030 if (value == "none")
5031 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5033 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5036 case Tabular::SET_SPECIAL_MULTIROW:
5040 case Tabular::APPEND_ROW:
5041 // append the row into the tabular
5042 tabular.appendRow(cur.idx());
5045 case Tabular::APPEND_COLUMN:
5046 // append the column into the tabular
5047 tabular.appendColumn(cur.idx());
5048 cur.idx() = tabular.cellIndex(row, column);
5051 case Tabular::DELETE_ROW:
5052 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5053 tabular.deleteRow(sel_row_start);
5054 if (sel_row_start >= tabular.nrows())
5056 cur.idx() = tabular.cellIndex(sel_row_start, column);
5059 cur.setSelection(false);
5062 case Tabular::DELETE_COLUMN:
5063 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5064 tabular.deleteColumn(sel_col_start);
5065 if (sel_col_start >= tabular.ncols())
5067 cur.idx() = tabular.cellIndex(row, sel_col_start);
5070 cur.setSelection(false);
5073 case Tabular::COPY_ROW:
5074 tabular.copyRow(row);
5077 case Tabular::COPY_COLUMN:
5078 tabular.copyColumn(column);
5079 cur.idx() = tabular.cellIndex(row, column);
5082 case Tabular::SET_LINE_TOP:
5083 case Tabular::TOGGLE_LINE_TOP: {
5084 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5085 ? (value == "true") : !tabular.topLine(cur.idx());
5086 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5087 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5088 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5092 case Tabular::SET_LINE_BOTTOM:
5093 case Tabular::TOGGLE_LINE_BOTTOM: {
5094 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5095 ? (value == "true") : !tabular.bottomLine(cur.idx());
5096 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5097 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5098 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5102 case Tabular::SET_LINE_LEFT:
5103 case Tabular::TOGGLE_LINE_LEFT: {
5104 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5105 ? (value == "true") : !tabular.leftLine(cur.idx());
5106 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5107 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5108 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5112 case Tabular::SET_LINE_RIGHT:
5113 case Tabular::TOGGLE_LINE_RIGHT: {
5114 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5115 ? (value == "true") : !tabular.rightLine(cur.idx());
5116 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5117 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5118 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5122 case Tabular::M_ALIGN_LEFT:
5123 case Tabular::M_ALIGN_RIGHT:
5124 case Tabular::M_ALIGN_CENTER:
5126 case Tabular::ALIGN_LEFT:
5127 case Tabular::ALIGN_RIGHT:
5128 case Tabular::ALIGN_CENTER:
5129 case Tabular::ALIGN_BLOCK:
5130 case Tabular::ALIGN_DECIMAL:
5131 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5132 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5133 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5136 case Tabular::M_VALIGN_TOP:
5137 case Tabular::M_VALIGN_BOTTOM:
5138 case Tabular::M_VALIGN_MIDDLE:
5140 case Tabular::VALIGN_TOP:
5141 case Tabular::VALIGN_BOTTOM:
5142 case Tabular::VALIGN_MIDDLE:
5143 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5144 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5145 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5148 case Tabular::MULTICOLUMN: {
5149 if (!cur.selection()) {
5150 // just multicol for one single cell
5151 // check whether we are completely in a multicol
5152 if (tabular.isMultiColumn(cur.idx()))
5153 tabular.unsetMultiColumn(cur.idx());
5155 tabular.setMultiColumn(cur.idx(), 1);
5158 // we have a selection so this means we just add all this
5159 // cells to form a multicolumn cell
5160 idx_type const s_start = cur.selBegin().idx();
5161 row_type const col_start = tabular.cellColumn(s_start);
5162 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5163 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5166 cur.setSelection(false);
5170 case Tabular::MULTIROW: {
5171 if (!cur.selection()) {
5172 // just multirow for one single cell
5173 // check whether we are completely in a multirow
5174 if (tabular.isMultiRow(cur.idx()))
5175 tabular.unsetMultiRow(cur.idx());
5177 tabular.setMultiRow(cur.idx(), 1);
5180 // we have a selection so this means we just add all this
5181 // cells to form a multirow cell
5182 idx_type const s_start = cur.selBegin().idx();
5183 row_type const row_start = tabular.cellRow(s_start);
5184 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5185 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5188 cur.setSelection(false);
5192 case Tabular::SET_ALL_LINES:
5194 case Tabular::UNSET_ALL_LINES:
5195 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5196 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5197 idx_type const cell = tabular.cellIndex(r, c);
5198 tabular.setTopLine(cell, setLines);
5199 tabular.setBottomLine(cell, setLines);
5200 tabular.setRightLine(cell, setLines);
5201 tabular.setLeftLine(cell, setLines);
5205 case Tabular::SET_BORDER_LINES:
5206 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5207 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5208 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5210 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5211 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5212 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5216 case Tabular::SET_LONGTABULAR:
5217 tabular.is_long_tabular = true;
5220 case Tabular::UNSET_LONGTABULAR:
5221 for (row_type r = 0; r < tabular.nrows(); ++r) {
5222 if (tabular.ltCaption(r)) {
5223 cur.idx() = tabular.cellIndex(r, 0);
5226 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5229 tabular.is_long_tabular = false;
5232 case Tabular::SET_ROTATE_TABULAR:
5233 tabular.rotate = true;
5236 case Tabular::UNSET_ROTATE_TABULAR:
5237 tabular.rotate = false;
5240 case Tabular::TOGGLE_ROTATE_TABULAR:
5241 tabular.rotate = !tabular.rotate;
5244 case Tabular::TABULAR_VALIGN_TOP:
5245 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5248 case Tabular::TABULAR_VALIGN_MIDDLE:
5249 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5252 case Tabular::TABULAR_VALIGN_BOTTOM:
5253 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5256 case Tabular::LONGTABULAR_ALIGN_LEFT:
5257 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5260 case Tabular::LONGTABULAR_ALIGN_CENTER:
5261 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5264 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5265 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5270 case Tabular::SET_ROTATE_CELL:
5271 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5272 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5273 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5276 case Tabular::UNSET_ROTATE_CELL:
5277 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5278 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5279 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5282 case Tabular::TOGGLE_ROTATE_CELL:
5284 bool oneNotRotated = oneCellHasRotationState(false,
5285 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5287 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5288 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5289 tabular.setRotateCell(tabular.cellIndex(r, c),
5294 case Tabular::SET_USEBOX: {
5295 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5296 if (val == tabular.getUsebox(cur.idx()))
5297 val = Tabular::BOX_NONE;
5298 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5299 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5300 tabular.setUsebox(tabular.cellIndex(r, c), val);
5304 case Tabular::UNSET_LTFIRSTHEAD:
5306 case Tabular::SET_LTFIRSTHEAD:
5307 tabular.getRowOfLTFirstHead(row, ltt);
5308 checkLongtableSpecial(ltt, value, flag);
5309 tabular.setLTHead(row, flag, ltt, true);
5312 case Tabular::UNSET_LTHEAD:
5314 case Tabular::SET_LTHEAD:
5315 tabular.getRowOfLTHead(row, ltt);
5316 checkLongtableSpecial(ltt, value, flag);
5317 tabular.setLTHead(row, flag, ltt, false);
5320 case Tabular::UNSET_LTFOOT:
5322 case Tabular::SET_LTFOOT:
5323 tabular.getRowOfLTFoot(row, ltt);
5324 checkLongtableSpecial(ltt, value, flag);
5325 tabular.setLTFoot(row, flag, ltt, false);
5328 case Tabular::UNSET_LTLASTFOOT:
5330 case Tabular::SET_LTLASTFOOT:
5331 tabular.getRowOfLTLastFoot(row, ltt);
5332 checkLongtableSpecial(ltt, value, flag);
5333 tabular.setLTFoot(row, flag, ltt, true);
5336 case Tabular::SET_LTNEWPAGE:
5337 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5340 case Tabular::TOGGLE_LTCAPTION: {
5341 bool const set = !tabular.ltCaption(row);
5342 cur.idx() = tabular.setLTCaption(row, set);
5345 cur.setSelection(false);
5348 // When a row is set as caption, then also insert
5349 // a caption. Otherwise the LaTeX output is broken.
5350 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5351 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5353 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5354 if (lyx::getStatus(fr).enabled())
5360 case Tabular::SET_BOOKTABS:
5361 tabular.use_booktabs = true;
5364 case Tabular::UNSET_BOOKTABS:
5365 tabular.use_booktabs = false;
5368 case Tabular::SET_TOP_SPACE: {
5370 if (value == "default")
5371 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5372 tabular.row_info[r].top_space_default = true;
5373 else if (value == "none")
5374 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5375 tabular.row_info[r].top_space_default = false;
5376 tabular.row_info[r].top_space = len;
5378 else if (isValidLength(value, &len))
5379 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5380 tabular.row_info[r].top_space_default = false;
5381 tabular.row_info[r].top_space = len;
5386 case Tabular::SET_BOTTOM_SPACE: {
5388 if (value == "default")
5389 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5390 tabular.row_info[r].bottom_space_default = true;
5391 else if (value == "none")
5392 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5393 tabular.row_info[r].bottom_space_default = false;
5394 tabular.row_info[r].bottom_space = len;
5396 else if (isValidLength(value, &len))
5397 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5398 tabular.row_info[r].bottom_space_default = false;
5399 tabular.row_info[r].bottom_space = len;
5404 case Tabular::SET_INTERLINE_SPACE: {
5406 if (value == "default")
5407 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5408 tabular.row_info[r].interline_space_default = true;
5409 else if (value == "none")
5410 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5411 tabular.row_info[r].interline_space_default = false;
5412 tabular.row_info[r].interline_space = len;
5414 else if (isValidLength(value, &len))
5415 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5416 tabular.row_info[r].interline_space_default = false;
5417 tabular.row_info[r].interline_space = len;
5422 case Tabular::SET_DECIMAL_POINT:
5423 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5424 tabular.column_info[c].decimal_point = from_utf8(value);
5427 // dummy stuff just to avoid warnings
5428 case Tabular::LAST_ACTION:
5434 bool InsetTabular::copySelection(Cursor & cur)
5436 if (!cur.selection())
5441 getSelection(cur, rs, re, cs, ce);
5443 paste_tabular.reset(new Tabular(tabular));
5445 for (row_type r = 0; r < rs; ++r)
5446 paste_tabular->deleteRow(0);
5448 row_type const rows = re - rs + 1;
5449 while (paste_tabular->nrows() > rows)
5450 paste_tabular->deleteRow(rows);
5452 for (col_type c = 0; c < cs; ++c)
5453 paste_tabular->deleteColumn(0);
5455 col_type const columns = ce - cs + 1;
5456 while (paste_tabular->ncols() > columns)
5457 paste_tabular->deleteColumn(columns);
5459 paste_tabular->setBuffer(tabular.buffer());
5461 odocstringstream os;
5462 OutputParams const runparams(0);
5463 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5464 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5465 cap::copySelection(cur, os.str());
5467 // mark tabular stack dirty
5468 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5469 // when we (hopefully) have a one-for-all paste mechanism.
5470 // This must be called after cap::copySelection.
5471 dirtyTabularStack(true);
5477 bool InsetTabular::pasteClipboard(Cursor & cur)
5481 col_type actcol = tabular.cellColumn(cur.idx());
5482 row_type actrow = tabular.cellRow(cur.idx());
5484 if (cur.selIsMultiCell()) {
5487 getSelection(cur, actrow, re, actcol, ce);
5490 for (row_type r1 = 0, r2 = actrow;
5491 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5493 for (col_type c1 = 0, c2 = actcol;
5494 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5496 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5497 tabular.isPartOfMultiColumn(r2, c2))
5499 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5503 if (tabular.isPartOfMultiColumn(r2, c2)) {
5507 shared_ptr<InsetTableCell> inset(
5508 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5509 tabular.setCellInset(r2, c2, inset);
5510 // FIXME?: why do we need to do this explicitly? (EL)
5511 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5513 // FIXME: change tracking (MG)
5514 inset->setChange(Change(buffer().params().trackChanges ?
5515 Change::INSERTED : Change::UNCHANGED));
5523 void InsetTabular::cutSelection(Cursor & cur)
5525 if (!cur.selection())
5530 getSelection(cur, rs, re, cs, ce);
5531 for (row_type r = rs; r <= re; ++r) {
5532 for (col_type c = cs; c <= ce; ++c) {
5533 shared_ptr<InsetTableCell> t
5534 = cell(tabular.cellIndex(r, c));
5535 if (buffer().params().trackChanges)
5536 // FIXME: Change tracking (MG)
5537 t->setChange(Change(Change::DELETED));
5543 // cursor position might be invalid now
5544 if (cur.pit() > cur.lastpit())
5545 cur.pit() = cur.lastpit();
5546 if (cur.pos() > cur.lastpos())
5547 cur.pos() = cur.lastpos();
5548 cur.clearSelection();
5552 bool InsetTabular::isRightToLeft(Cursor & cur) const
5554 LASSERT(cur.depth() > 1, /**/);
5555 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5556 pos_type const parentpos = cur[cur.depth() - 2].pos();
5557 return parentpar.getFontSettings(buffer().params(),
5558 parentpos).language()->rightToLeft();
5562 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5565 LASSERT(stidx <= enidx, return docstring());
5567 col_type const col1 = tabular.cellColumn(stidx);
5568 col_type const col2 = tabular.cellColumn(enidx);
5569 row_type const row1 = tabular.cellRow(stidx);
5570 row_type const row2 = tabular.cellRow(enidx);
5572 for (col_type col = col1; col <= col2; col++)
5573 for (row_type row = row1; row <= row2; row++) {
5578 retval += tabular.cellInset(row, col)->asString(intoInsets);
5584 void InsetTabular::getSelection(Cursor & cur,
5585 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5587 CursorSlice const & beg = cur.selBegin();
5588 CursorSlice const & end = cur.selEnd();
5589 cs = tabular.cellColumn(beg.idx());
5590 ce = tabular.cellColumn(end.idx());
5594 rs = tabular.cellRow(beg.idx());
5595 re = tabular.cellRow(end.idx());
5601 Text * InsetTabular::getText(int idx) const
5603 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5607 void InsetTabular::setChange(Change const & change)
5609 for (idx_type idx = 0; idx < nargs(); ++idx)
5610 cell(idx)->setChange(change);
5614 void InsetTabular::acceptChanges()
5616 for (idx_type idx = 0; idx < nargs(); ++idx)
5617 cell(idx)->acceptChanges();
5621 void InsetTabular::rejectChanges()
5623 for (idx_type idx = 0; idx < nargs(); ++idx)
5624 cell(idx)->rejectChanges();
5628 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5630 return tabular.getPWidth(cell).zero();
5634 bool InsetTabular::forcePlainLayout(idx_type cell) const
5636 return !tabular.getPWidth(cell).zero();
5640 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5643 if (buf.length() <= 0)
5648 col_type maxCols = 1;
5649 size_t const len = buf.length();
5653 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5661 maxCols = max(cols, maxCols);
5667 maxCols = max(cols, maxCols);
5673 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5674 loctab = paste_tabular.get();
5676 dirtyTabularStack(true);
5679 cell = bv.cursor().idx();
5680 ocol = tabular.cellColumn(cell);
5681 row = tabular.cellRow(cell);
5685 idx_type const cells = loctab->numberofcells;
5688 rows = loctab->nrows();
5689 col_type const columns = loctab->ncols();
5691 while (cell < cells && p < len && row < rows &&
5692 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5698 // we can only set this if we are not too far right
5699 if (cols < columns) {
5700 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5701 Font const font = bv.textMetrics(&inset->text()).
5703 inset->setText(buf.substr(op, p - op), font,
5704 buffer().params().trackChanges);
5710 // we can only set this if we are not too far right
5711 if (cols < columns) {
5712 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5713 Font const font = bv.textMetrics(&inset->text()).
5715 inset->setText(buf.substr(op, p - op), font,
5716 buffer().params().trackChanges);
5721 cell = loctab->cellIndex(row, cols);
5727 // check for the last cell if there is no trailing '\n'
5728 if (cell < cells && op < len) {
5729 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5730 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5731 inset->setText(buf.substr(op, len - op), font,
5732 buffer().params().trackChanges);
5738 void InsetTabular::addPreview(DocIterator const & inset_pos,
5739 PreviewLoader & loader) const
5741 DocIterator cell_pos = inset_pos;
5743 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5744 for (row_type r = 0; r < tabular.nrows(); ++r) {
5745 for (col_type c = 0; c < tabular.ncols(); ++c) {
5746 cell_pos.top().idx() = tabular.cellIndex(r, c);
5747 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5753 bool InsetTabular::completionSupported(Cursor const & cur) const
5755 Cursor const & bvCur = cur.bv().cursor();
5756 if (&bvCur.inset() != this)
5758 return cur.text()->completionSupported(cur);
5762 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5764 return completionSupported(cur);
5768 bool InsetTabular::automaticInlineCompletion() const
5770 return lyxrc.completion_inline_text;
5774 bool InsetTabular::automaticPopupCompletion() const
5776 return lyxrc.completion_popup_text;
5780 bool InsetTabular::showCompletionCursor() const
5782 return lyxrc.completion_cursor_text;
5786 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5788 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5792 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5794 if (!completionSupported(cur))
5796 return cur.text()->completionPrefix(cur);
5800 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5802 if (!completionSupported(cur))
5805 return cur.text()->insertCompletion(cur, s, finished);
5809 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5810 Dimension & dim) const
5812 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5813 tm.completionPosAndDim(cur, x, y, dim);
5817 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5819 istringstream data(in);
5821 lex.setStream(data);
5828 if (!lex || token != "tabular") {
5829 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5833 // This is part of the inset proper that is usually swallowed
5834 // by Buffer::readInset
5836 if (!lex || token != "Tabular") {
5837 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5845 string InsetTabular::params2string(InsetTabular const & inset)
5848 data << "tabular" << ' ';
5850 data << "\\end_inset\n";