2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 { Tabular::APPEND_ROW, "append-row", false },
110 { Tabular::APPEND_COLUMN, "append-column", false },
111 { Tabular::DELETE_ROW, "delete-row", false },
112 { Tabular::DELETE_COLUMN, "delete-column", false },
113 { Tabular::COPY_ROW, "copy-row", false },
114 { Tabular::COPY_COLUMN, "copy-column", false },
115 { Tabular::SET_LINE_TOP, "set-line-top", true },
116 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
117 { Tabular::SET_LINE_LEFT, "set-line-left", true },
118 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
119 //FIXME: get rid of those 4 TOGGLE actions in favor of the 4 above.
120 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
121 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
122 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
123 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
124 { Tabular::ALIGN_LEFT, "align-left", false },
125 { Tabular::ALIGN_RIGHT, "align-right", false },
126 { Tabular::ALIGN_CENTER, "align-center", false },
127 { Tabular::ALIGN_BLOCK, "align-block", false },
128 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
129 { Tabular::VALIGN_TOP, "valign-top", false },
130 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
131 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
132 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
133 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
134 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
135 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
136 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
137 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
138 { Tabular::MULTICOLUMN, "multicolumn", false },
139 { Tabular::MULTIROW, "multirow", false },
140 { Tabular::SET_ALL_LINES, "set-all-lines", false },
141 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
142 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
143 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
144 { Tabular::SET_PWIDTH, "set-pwidth", true },
145 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
146 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
147 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
148 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
149 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
150 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
151 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
152 { Tabular::SET_USEBOX, "set-usebox", true },
153 { Tabular::SET_LTHEAD, "set-lthead", true },
154 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
155 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
156 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
157 { Tabular::SET_LTFOOT, "set-ltfoot", true },
158 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
159 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
160 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
161 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
162 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
163 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
164 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
165 { Tabular::SET_SPECIAL_MULTIROW, "set-special-multirow", false },
166 { Tabular::SET_BOOKTABS, "set-booktabs", false },
167 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
168 { Tabular::SET_TOP_SPACE, "set-top-space", true },
169 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
170 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
171 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
172 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
173 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
174 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
175 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
176 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
177 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
178 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
179 { Tabular::LAST_ACTION, "", false }
184 string const write_attribute(string const & name, T const & t)
186 string const s = tostr(t);
187 return s.empty() ? s : " " + name + "=\"" + s + "\"";
191 string const write_attribute(string const & name, string const & t)
193 return t.empty() ? t : " " + name + "=\"" + t + "\"";
198 string const write_attribute(string const & name, docstring const & t)
200 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
205 string const write_attribute(string const & name, bool const & b)
207 // we write only true attribute values so we remove a bit of the
208 // file format bloat for tabulars.
209 return b ? write_attribute(name, convert<string>(b)) : string();
214 string const write_attribute(string const & name, int const & i)
216 // we write only true attribute values so we remove a bit of the
217 // file format bloat for tabulars.
218 return i ? write_attribute(name, convert<string>(i)) : string();
223 string const write_attribute(string const & name, Tabular::idx_type const & i)
225 // we write only true attribute values so we remove a bit of the
226 // file format bloat for tabulars.
227 return i ? write_attribute(name, convert<string>(i)) : string();
232 string const write_attribute(string const & name, Length const & value)
234 // we write only the value if we really have one same reson as above.
235 return value.zero() ? string() : write_attribute(name, value.asString());
239 string const tostr(LyXAlignment const & num)
244 case LYX_ALIGN_BLOCK:
248 case LYX_ALIGN_CENTER:
250 case LYX_ALIGN_RIGHT:
252 case LYX_ALIGN_LAYOUT:
254 case LYX_ALIGN_SPECIAL:
256 case LYX_ALIGN_DECIMAL:
263 string const tostr(Tabular::HAlignment const & num)
266 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
268 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
270 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
277 string const tostr(Tabular::VAlignment const & num)
280 case Tabular::LYX_VALIGN_TOP:
282 case Tabular::LYX_VALIGN_MIDDLE:
284 case Tabular::LYX_VALIGN_BOTTOM:
291 string const tostr(Tabular::BoxType const & num)
294 case Tabular::BOX_NONE:
296 case Tabular::BOX_PARBOX:
298 case Tabular::BOX_MINIPAGE:
305 // I would have liked a fromstr template a lot better. (Lgb)
306 bool string2type(string const str, LyXAlignment & num)
309 num = LYX_ALIGN_NONE;
310 else if (str == "block")
311 num = LYX_ALIGN_BLOCK;
312 else if (str == "left")
313 num = LYX_ALIGN_LEFT;
314 else if (str == "center")
315 num = LYX_ALIGN_CENTER;
316 else if (str == "right")
317 num = LYX_ALIGN_RIGHT;
318 else if (str == "decimal")
319 num = LYX_ALIGN_DECIMAL;
326 bool string2type(string const str, Tabular::HAlignment & num)
329 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
330 else if (str == "center" )
331 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
332 else if (str == "right")
333 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
340 bool string2type(string const str, Tabular::VAlignment & num)
343 num = Tabular::LYX_VALIGN_TOP;
344 else if (str == "middle" )
345 num = Tabular::LYX_VALIGN_MIDDLE;
346 else if (str == "bottom")
347 num = Tabular::LYX_VALIGN_BOTTOM;
354 bool string2type(string const str, Tabular::BoxType & num)
357 num = Tabular::BOX_NONE;
358 else if (str == "parbox")
359 num = Tabular::BOX_PARBOX;
360 else if (str == "minipage")
361 num = Tabular::BOX_MINIPAGE;
368 bool string2type(string const str, bool & num)
372 else if (str == "false")
380 bool getTokenValue(string const & str, char const * token, string & ret)
383 size_t token_length = strlen(token);
384 size_t pos = str.find(token);
386 if (pos == string::npos || pos + token_length + 1 >= str.length()
387 || str[pos + token_length] != '=')
389 pos += token_length + 1;
391 if (ch != '"' && ch != '\'') { // only read till next space
395 while (pos < str.length() - 1 && str[++pos] != ch)
402 bool getTokenValue(string const & str, char const * token, docstring & ret)
405 bool const success = getTokenValue(str, token, tmp);
406 ret = from_utf8(tmp);
411 bool getTokenValue(string const & str, char const * token, int & num)
415 if (!getTokenValue(str, token, tmp))
417 num = convert<int>(tmp);
422 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
425 return getTokenValue(str, token, tmp) && string2type(tmp, num);
429 bool getTokenValue(string const & str, char const * token,
430 Tabular::HAlignment & num)
433 return getTokenValue(str, token, tmp) && string2type(tmp, num);
437 bool getTokenValue(string const & str, char const * token,
438 Tabular::VAlignment & num)
441 return getTokenValue(str, token, tmp) && string2type(tmp, num);
445 bool getTokenValue(string const & str, char const * token,
446 Tabular::BoxType & num)
449 return getTokenValue(str, token, tmp) && string2type(tmp, num);
453 bool getTokenValue(string const & str, char const * token, bool & flag)
455 // set the flag always to false as this should be the default for bools
456 // not in the file-format.
459 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
463 bool getTokenValue(string const & str, char const * token, Length & len)
465 // set the length to be zero() as default as this it should be if not
466 // in the file format.
469 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
473 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
478 if (!getTokenValue(str, token, tmp))
480 if (tmp == "default") {
484 return isValidLength(tmp, &len);
488 void l_getline(istream & is, string & str)
491 while (str.empty()) {
493 if (!str.empty() && str[str.length() - 1] == '\r')
494 str.erase(str.length() - 1);
501 string const featureAsString(Tabular::Feature action)
503 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
504 if (tabularFeature[i].action == action)
505 return tabularFeature[i].feature;
511 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
513 InsetTableCell tail = InsetTableCell(head);
514 tail.getText(0)->setMacrocontextPosition(
515 head.getText(0)->macrocontextPosition());
516 tail.setBuffer(head.buffer());
518 DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
519 for (; dit; dit.forwardChar())
520 if (dit.inTexted() && dit.depth()==1
521 && dit.paragraph().find(align_d, false, false, dit.pos()))
524 pit_type const psize = head.paragraphs().front().size();
527 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
528 tail.paragraphs().front().eraseChars(0,
529 dit.pos() < psize ? dit.pos() + 1 : psize, false);
536 /////////////////////////////////////////////////////////////////////
540 /////////////////////////////////////////////////////////////////////
543 Tabular::CellData::CellData(Buffer * buf)
546 multicolumn(Tabular::CELL_NORMAL),
547 multirow(Tabular::CELL_NORMAL),
548 alignment(LYX_ALIGN_CENTER),
549 valignment(LYX_VALIGN_TOP),
559 inset(new InsetTableCell(buf))
561 inset->setBuffer(*buf);
565 Tabular::CellData::CellData(CellData const & cs)
568 multicolumn(cs.multicolumn),
569 multirow(cs.multirow),
570 alignment(cs.alignment),
571 valignment(cs.valignment),
572 decimal_hoffset(cs.decimal_hoffset),
573 decimal_width(cs.decimal_width),
575 top_line(cs.top_line),
576 bottom_line(cs.bottom_line),
577 left_line(cs.left_line),
578 right_line(cs.right_line),
581 align_special(cs.align_special),
583 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
587 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
593 void Tabular::CellData::swap(CellData & rhs)
595 std::swap(cellno, rhs.cellno);
596 std::swap(width, rhs.width);
597 std::swap(multicolumn, rhs.multicolumn);
598 std::swap(multirow, rhs.multirow);
599 std::swap(alignment, rhs.alignment);
600 std::swap(valignment, rhs.valignment);
601 std::swap(decimal_hoffset, rhs.decimal_hoffset);
602 std::swap(decimal_width, rhs.decimal_width);
603 std::swap(voffset, rhs.voffset);
604 std::swap(top_line, rhs.top_line);
605 std::swap(bottom_line, rhs.bottom_line);
606 std::swap(left_line, rhs.left_line);
607 std::swap(right_line, rhs.right_line);
608 std::swap(usebox, rhs.usebox);
609 std::swap(rotate, rhs.rotate);
610 std::swap(align_special, rhs.align_special);
611 p_width.swap(rhs.p_width);
612 inset.swap(rhs.inset);
616 Tabular::RowData::RowData()
619 top_space_default(false),
620 bottom_space_default(false),
621 interline_space_default(false),
631 Tabular::ColumnData::ColumnData()
632 : alignment(LYX_ALIGN_CENTER),
633 valignment(LYX_VALIGN_TOP),
639 Tabular::ltType::ltType()
646 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
648 init(buffer, rows_arg, columns_arg);
652 void Tabular::setBuffer(Buffer & buffer)
655 for (row_type i = 0; i < nrows(); ++i)
656 for (col_type j = 0; j < ncols(); ++j)
657 cell_info[i][j].inset->setBuffer(*buffer_);
661 // activates all lines and sets all widths to 0
662 void Tabular::init(Buffer * buf, row_type rows_arg,
663 col_type columns_arg)
666 row_info = row_vector(rows_arg);
667 column_info = column_vector(columns_arg);
668 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
669 row_info.reserve(10);
670 column_info.reserve(10);
671 cell_info.reserve(100);
673 is_long_tabular = false;
674 tabular_valignment = LYX_VALIGN_MIDDLE;
675 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
677 use_booktabs = false;
678 // set silly default lines
679 for (row_type r = 0; r < nrows(); ++r)
680 for (col_type c = 0; c < ncols(); ++c) {
681 cell_info[r][c].inset->setBuffer(*buffer_);
682 cell_info[r][c].top_line = true;
683 cell_info[r][c].left_line = true;
684 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
685 cell_info[r][c].right_line = c == ncols() - 1;
690 void Tabular::appendRow(idx_type const cell)
692 row_type const row = cellRow(cell);
694 row_info.insert(row_info.begin() + row + 1, RowData());
695 row_info[row + 1] = row_info[row];
697 cell_info.insert(cell_info.begin() + row + 1,
698 cell_vector(ncols(), CellData(buffer_)));
699 for (col_type c = 0; c < ncols(); ++c) {
700 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
701 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
703 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
707 for (col_type c = 0; c < ncols(); ++c) {
708 if (isPartOfMultiRow(row, c))
710 // inherit line settings
711 idx_type const i = cellIndex(row + 1, c);
712 idx_type const j = cellIndex(row, c);
713 setLeftLine(i, leftLine(j));
714 setRightLine(i, rightLine(j));
715 setTopLine(i, topLine(j));
716 if (topLine(j) && bottomLine(j)) {
717 setBottomLine(i, true);
718 setBottomLine(j, false);
720 // mark track changes
721 if (buffer().params().trackChanges)
722 cellInfo(i).inset->setChange(Change(Change::INSERTED));
727 void Tabular::deleteRow(row_type const row)
729 // Not allowed to delete last row
733 for (col_type c = 0; c < ncols(); ++c) {
734 // Care about multirow cells
735 if (row + 1 < nrows() &&
736 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
737 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
738 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
741 row_info.erase(row_info.begin() + row);
742 cell_info.erase(cell_info.begin() + row);
747 void Tabular::copyRow(row_type const row)
749 row_info.insert(row_info.begin() + row, row_info[row]);
750 cell_info.insert(cell_info.begin() + row, cell_info[row]);
752 if (buffer().params().trackChanges)
753 for (col_type c = 0; c < ncols(); ++c)
754 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
760 void Tabular::appendColumn(idx_type const cell)
762 col_type const c = cellColumn(cell);
764 column_info.insert(column_info.begin() + c + 1, ColumnData());
765 column_info[c + 1] = column_info[c];
767 for (row_type r = 0; r < nrows(); ++r) {
768 cell_info[r].insert(cell_info[r].begin() + c + 1,
770 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
771 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
773 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
776 for (row_type r = 0; r < nrows(); ++r) {
777 // inherit line settings
778 idx_type const i = cellIndex(r, c + 1);
779 idx_type const j = cellIndex(r, c);
780 setBottomLine(i, bottomLine(j));
781 setTopLine(i, topLine(j));
782 setLeftLine(i, leftLine(j));
783 if (rightLine(j) && rightLine(j)) {
784 setRightLine(i, true);
785 setRightLine(j, false);
787 if (buffer().params().trackChanges)
788 cellInfo(i).inset->setChange(Change(Change::INSERTED));
793 void Tabular::deleteColumn(col_type const col)
795 // Not allowed to delete last column
799 for (row_type r = 0; r < nrows(); ++r) {
800 // Care about multicolumn cells
801 if (col + 1 < ncols() &&
802 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
803 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
804 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
806 cell_info[r].erase(cell_info[r].begin() + col);
808 column_info.erase(column_info.begin() + col);
813 void Tabular::copyColumn(col_type const col)
815 BufferParams const & bp = buffer().params();
816 column_info.insert(column_info.begin() + col, column_info[col]);
818 for (row_type r = 0; r < nrows(); ++r) {
819 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
821 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
827 void Tabular::updateIndexes()
831 for (row_type row = 0; row < nrows(); ++row)
832 for (col_type column = 0; column < ncols(); ++column) {
833 if (!isPartOfMultiColumn(row, column)
834 && !isPartOfMultiRow(row, column))
836 if (isPartOfMultiRow(row, column))
837 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
839 cell_info[row][column].cellno = numberofcells - 1;
842 rowofcell.resize(numberofcells);
843 columnofcell.resize(numberofcells);
845 for (row_type row = 0; row < nrows(); ++row)
846 for (col_type column = 0; column < ncols(); ++column) {
847 if (isPartOfMultiColumn(row, column)
848 || isPartOfMultiRow(row, column))
851 columnofcell[i] = column;
852 setFixedWidth(row, column);
853 cell_info[row][column].inset->setContentAlignment(
854 getAlignment(cellIndex(row, column)));
860 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
863 for (col_type c = 0; c < ncols(); ++c)
864 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
870 bool Tabular::topLine(idx_type const cell) const
872 return cellInfo(cell).top_line;
876 bool Tabular::bottomLine(idx_type const cell) const
878 return cellInfo(cell).bottom_line;
882 bool Tabular::leftLine(idx_type cell) const
886 return cellInfo(cell).left_line;
890 bool Tabular::rightLine(idx_type cell) const
894 return cellInfo(cell).right_line;
898 int Tabular::interRowSpace(row_type row) const
900 if (!row || row >= nrows())
903 int const interline_space = row_info[row - 1].interline_space_default ?
905 row_info[row - 1].interline_space.inPixels(width());
906 if (rowTopLine(row) && rowBottomLine(row - 1))
907 return interline_space + WIDTH_OF_LINE;
908 return interline_space;
912 int Tabular::interColumnSpace(idx_type cell) const
914 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
915 if (rightLine(cell) && nextcol < ncols()
916 && leftLine(cellIndex(cellRow(cell), nextcol)))
917 return WIDTH_OF_LINE;
922 int Tabular::cellWidth(idx_type cell) const
925 col_type const span = columnSpan(cell);
926 col_type const col = cellColumn(cell);
927 for(col_type c = col; c < col + span ; ++c)
928 w += column_info[c].width;
933 int Tabular::cellHeight(idx_type cell) const
935 row_type const span = rowSpan(cell);
936 row_type const row = cellRow(cell);
938 for(row_type r = row; r < row + span ; ++r) {
939 h += rowAscent(r) + rowDescent(r);
940 if (r != row + span - 1)
941 h += interRowSpace(r + 1);
948 bool Tabular::updateColumnWidths()
950 vector<int> max_dwidth(ncols(), 0);
951 for(col_type c = 0; c < ncols(); ++c)
952 for(row_type r = 0; r < nrows(); ++r) {
953 idx_type const i = cellIndex(r, c);
954 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
955 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
959 // for each col get max of single col cells
960 for(col_type c = 0; c < ncols(); ++c) {
962 for(row_type r = 0; r < nrows(); ++r) {
963 idx_type const i = cellIndex(r, c);
964 if (columnSpan(i) == 1) {
965 if (getAlignment(i) == LYX_ALIGN_DECIMAL
966 && cell_info[r][c].decimal_width!=0)
967 new_width = max(new_width, cellInfo(i).width
968 + max_dwidth[c] - cellInfo(i).decimal_width);
970 new_width = max(new_width, cellInfo(i).width);
974 if (column_info[c].width != new_width) {
975 column_info[c].width = new_width;
979 // update col widths to fit merged cells
980 for(col_type c = 0; c < ncols(); ++c)
981 for(row_type r = 0; r < nrows(); ++r) {
982 idx_type const i = cellIndex(r, c);
983 int const span = columnSpan(i);
984 if (span == 1 || c > cellColumn(i))
988 for(col_type j = c; j < c + span ; ++j)
989 old_width += column_info[j].width;
991 if (cellInfo(i).width > old_width) {
992 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1001 int Tabular::width() const
1004 for (col_type c = 0; c < ncols(); ++c)
1005 width += column_info[c].width;
1010 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1013 col_type const col = cellColumn(cell);
1014 if (onlycolumn || !isMultiColumn(cell)) {
1015 column_info[col].alignment = align;
1016 docstring & dpoint = column_info[col].decimal_point;
1017 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1018 dpoint = from_utf8(lyxrc.default_decimal_point);
1020 cellInfo(cell).alignment = align;
1021 cellInset(cell).get()->setContentAlignment(align);
1026 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1029 if (!isMultiColumn(cell) || onlycolumn)
1030 column_info[cellColumn(cell)].valignment = align;
1032 cellInfo(cell).valignment = align;
1039 * Allow line and paragraph breaks for fixed width cells or disallow them,
1040 * merge cell paragraphs and reset layout to standard for variable width
1043 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1045 inset->setAutoBreakRows(fixedWidth);
1046 inset->toggleFixedWidth(fixedWidth);
1050 // merge all paragraphs to one
1051 BufferParams const & bp = cur.bv().buffer().params();
1052 while (inset->paragraphs().size() > 1)
1053 mergeParagraph(bp, inset->paragraphs(), 0);
1057 // undo information has already been recorded
1058 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1059 bp.documentClass().plainLayoutName());
1066 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1067 Length const & width)
1069 col_type const c = cellColumn(cell);
1071 column_info[c].p_width = width;
1072 // reset the vertical alignment to top if the fixed with
1073 // is removed or zero because only fixed width columns can
1074 // have a vertical alignment
1075 if (column_info[c].p_width.zero())
1076 column_info[c].valignment = LYX_VALIGN_TOP;
1077 for (row_type r = 0; r < nrows(); ++r) {
1078 idx_type const cell = cellIndex(r, c);
1079 // because of multicolumns
1080 toggleFixedWidth(cur, cellInset(cell).get(),
1081 !getPWidth(cell).zero());
1083 // cur paragraph can become invalid after paragraphs were merged
1084 if (cur.pit() > cur.lastpit())
1085 cur.pit() = cur.lastpit();
1086 // cur position can become invalid after newlines were removed
1087 if (cur.pos() > cur.lastpos())
1088 cur.pos() = cur.lastpos();
1092 bool Tabular::setFixedWidth(row_type r, col_type c)
1094 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1095 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1096 || (multicol && !cell_info[r][c].p_width.zero());
1097 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1102 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1103 Length const & width)
1105 if (!isMultiColumn(cell))
1108 cellInfo(cell).p_width = width;
1109 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1110 // cur paragraph can become invalid after paragraphs were merged
1111 if (cur.pit() > cur.lastpit())
1112 cur.pit() = cur.lastpit();
1113 // cur position can become invalid after newlines were removed
1114 if (cur.pos() > cur.lastpos())
1115 cur.pos() = cur.lastpos();
1120 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1121 Tabular::Feature what)
1123 if (what == SET_SPECIAL_MULTICOLUMN)
1124 cellInfo(cell).align_special = special;
1126 column_info[cellColumn(cell)].align_special = special;
1130 void Tabular::setTopLine(idx_type i, bool line)
1132 cellInfo(i).top_line = line;
1136 void Tabular::setBottomLine(idx_type i, bool line)
1138 cellInfo(i).bottom_line = line;
1142 void Tabular::setLeftLine(idx_type cell, bool line)
1144 cellInfo(cell).left_line = line;
1148 void Tabular::setRightLine(idx_type cell, bool line)
1150 cellInfo(cell).right_line = line;
1153 bool Tabular::rowTopLine(row_type r) const
1155 bool all_rows_set = true;
1156 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1157 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1158 return all_rows_set;
1162 bool Tabular::rowBottomLine(row_type r) const
1164 bool all_rows_set = true;
1165 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1166 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1167 return all_rows_set;
1171 bool Tabular::columnLeftLine(col_type c) const
1178 for (row_type r = 0; r < nrows(); ++r) {
1179 idx_type const i = cellIndex(r, c);
1180 if (c == cellColumn(i)) {
1182 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1183 if (cellInfo(i).left_line || right)
1187 return 2 * nrows_left >= total;
1191 bool Tabular::columnRightLine(col_type c) const
1196 int nrows_right = 0;
1198 for (row_type r = 0; r < nrows(); ++r) {
1199 idx_type i = cellIndex(r, c);
1200 if (c == cellColumn(i) + columnSpan(i) - 1) {
1202 bool left = (c + 1 < ncols()
1203 && cellInfo(cellIndex(r, c + 1)).left_line)
1204 || c + 1 == ncols();
1205 if (cellInfo(i).right_line && left)
1209 return 2 * nrows_right >= total;
1213 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1215 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1216 return cellInfo(cell).alignment;
1218 return column_info[cellColumn(cell)].alignment;
1223 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1225 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1226 return cellInfo(cell).valignment;
1227 return column_info[cellColumn(cell)].valignment;
1231 Length const Tabular::getPWidth(idx_type cell) const
1233 if (isMultiColumn(cell))
1234 return cellInfo(cell).p_width;
1235 return column_info[cellColumn(cell)].p_width;
1239 int Tabular::textHOffset(idx_type cell) const
1241 // the LaTeX Way :-(
1242 int x = WIDTH_OF_LINE;
1244 int const w = cellWidth(cell) - cellInfo(cell).width;
1246 switch (getAlignment(cell)) {
1247 case LYX_ALIGN_CENTER:
1250 case LYX_ALIGN_RIGHT:
1253 case LYX_ALIGN_DECIMAL: {
1254 // we center when no decimal point
1255 if (cellInfo(cell).decimal_width == 0) {
1259 col_type const c = cellColumn(cell);
1260 int max_dhoffset = 0;
1261 for(row_type r = 0; r < row_info.size() ; ++r) {
1262 idx_type const i = cellIndex(r, c);
1263 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1264 && cellInfo(i).decimal_width != 0)
1265 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1267 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1270 // LYX_ALIGN_LEFT: nothing :-)
1278 int Tabular::textVOffset(idx_type cell) const
1280 int voffset = cellInfo(cell).voffset;
1281 if (isMultiRow(cell)) {
1282 row_type const row = cellRow(cell);
1283 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1289 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1292 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1294 return cell_info[row][c].cellno;
1298 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1300 col_type c = ncols() - 1;
1301 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1302 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1304 return cell_info[row][c].cellno;
1308 Tabular::row_type Tabular::cellRow(idx_type cell) const
1310 if (cell >= numberofcells)
1314 return rowofcell[cell];
1318 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1320 if (cell >= numberofcells)
1324 return columnofcell[cell];
1328 void Tabular::write(ostream & os) const
1332 << write_attribute("version", 3)
1333 << write_attribute("rows", nrows())
1334 << write_attribute("columns", ncols())
1336 // global longtable options
1338 << write_attribute("rotate", rotate)
1339 << write_attribute("booktabs", use_booktabs)
1340 << write_attribute("islongtable", is_long_tabular)
1341 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1342 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1343 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1344 << write_attribute("headTopDL", endhead.topDL)
1345 << write_attribute("headBottomDL", endhead.bottomDL)
1346 << write_attribute("footTopDL", endfoot.topDL)
1347 << write_attribute("footBottomDL", endfoot.bottomDL)
1348 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1349 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1350 << write_attribute("lastFootEmpty", endlastfoot.empty);
1351 // longtables cannot be aligned vertically
1352 if (!is_long_tabular)
1353 os << write_attribute("tabularvalignment", tabular_valignment);
1354 if (is_long_tabular)
1355 os << write_attribute("longtabularalignment",
1356 longtabular_alignment);
1358 for (col_type c = 0; c < ncols(); ++c) {
1360 << write_attribute("alignment", column_info[c].alignment);
1361 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1362 os << write_attribute("decimal_point", column_info[c].decimal_point);
1363 os << write_attribute("valignment", column_info[c].valignment)
1364 << write_attribute("width", column_info[c].p_width.asString())
1365 << write_attribute("special", column_info[c].align_special)
1368 for (row_type r = 0; r < nrows(); ++r) {
1369 static const string def("default");
1371 if (row_info[r].top_space_default)
1372 os << write_attribute("topspace", def);
1374 os << write_attribute("topspace", row_info[r].top_space);
1375 if (row_info[r].bottom_space_default)
1376 os << write_attribute("bottomspace", def);
1378 os << write_attribute("bottomspace", row_info[r].bottom_space);
1379 if (row_info[r].interline_space_default)
1380 os << write_attribute("interlinespace", def);
1382 os << write_attribute("interlinespace", row_info[r].interline_space);
1383 os << write_attribute("endhead", row_info[r].endhead)
1384 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1385 << write_attribute("endfoot", row_info[r].endfoot)
1386 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1387 << write_attribute("newpage", row_info[r].newpage)
1388 << write_attribute("caption", row_info[r].caption)
1390 for (col_type c = 0; c < ncols(); ++c) {
1392 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1393 << write_attribute("multirow", cell_info[r][c].multirow)
1394 << write_attribute("alignment", cell_info[r][c].alignment)
1395 << write_attribute("valignment", cell_info[r][c].valignment)
1396 << write_attribute("topline", cell_info[r][c].top_line)
1397 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1398 << write_attribute("leftline", cell_info[r][c].left_line)
1399 << write_attribute("rightline", cell_info[r][c].right_line)
1400 << write_attribute("rotate", cell_info[r][c].rotate)
1401 << write_attribute("usebox", cell_info[r][c].usebox)
1402 << write_attribute("width", cell_info[r][c].p_width)
1403 << write_attribute("special", cell_info[r][c].align_special)
1405 os << "\\begin_inset ";
1406 cell_info[r][c].inset->write(os);
1407 os << "\n\\end_inset\n"
1412 os << "</lyxtabular>\n";
1416 void Tabular::read(Lexer & lex)
1419 istream & is = lex.getStream();
1421 l_getline(is, line);
1422 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1423 LASSERT(false, /**/);
1428 if (!getTokenValue(line, "version", version))
1430 LASSERT(version >= 2, /**/);
1433 if (!getTokenValue(line, "rows", rows_arg))
1436 if (!getTokenValue(line, "columns", columns_arg))
1438 init(buffer_, rows_arg, columns_arg);
1439 l_getline(is, line);
1440 if (!prefixIs(line, "<features")) {
1441 lyxerr << "Wrong tabular format (expected <features ...> got"
1442 << line << ')' << endl;
1445 getTokenValue(line, "rotate", rotate);
1446 getTokenValue(line, "booktabs", use_booktabs);
1447 getTokenValue(line, "islongtable", is_long_tabular);
1448 getTokenValue(line, "tabularvalignment", tabular_valignment);
1449 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1450 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1451 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1452 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1453 getTokenValue(line, "headTopDL", endhead.topDL);
1454 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1455 getTokenValue(line, "footTopDL", endfoot.topDL);
1456 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1457 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1458 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1459 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1461 for (col_type c = 0; c < ncols(); ++c) {
1463 if (!prefixIs(line,"<column")) {
1464 lyxerr << "Wrong tabular format (expected <column ...> got"
1465 << line << ')' << endl;
1468 getTokenValue(line, "alignment", column_info[c].alignment);
1469 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1470 getTokenValue(line, "valignment", column_info[c].valignment);
1471 getTokenValue(line, "width", column_info[c].p_width);
1472 getTokenValue(line, "special", column_info[c].align_special);
1475 for (row_type i = 0; i < nrows(); ++i) {
1476 l_getline(is, line);
1477 if (!prefixIs(line, "<row")) {
1478 lyxerr << "Wrong tabular format (expected <row ...> got"
1479 << line << ')' << endl;
1482 getTokenValue(line, "topspace", row_info[i].top_space,
1483 row_info[i].top_space_default);
1484 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1485 row_info[i].bottom_space_default);
1486 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1487 row_info[i].interline_space_default);
1488 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1489 getTokenValue(line, "endhead", row_info[i].endhead);
1490 getTokenValue(line, "endfoot", row_info[i].endfoot);
1491 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1492 getTokenValue(line, "newpage", row_info[i].newpage);
1493 getTokenValue(line, "caption", row_info[i].caption);
1494 for (col_type j = 0; j < ncols(); ++j) {
1495 l_getline(is, line);
1496 if (!prefixIs(line, "<cell")) {
1497 lyxerr << "Wrong tabular format (expected <cell ...> got"
1498 << line << ')' << endl;
1501 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1502 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1503 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1504 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1505 getTokenValue(line, "topline", cell_info[i][j].top_line);
1506 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1507 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1508 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1509 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1510 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1511 getTokenValue(line, "width", cell_info[i][j].p_width);
1513 getTokenValue(line, "special", cell_info[i][j].align_special);
1514 l_getline(is, line);
1515 if (prefixIs(line, "\\begin_inset")) {
1516 cell_info[i][j].inset->setBuffer(*buffer_);
1517 cell_info[i][j].inset->read(lex);
1518 l_getline(is, line);
1520 if (!prefixIs(line, "</cell>")) {
1521 lyxerr << "Wrong tabular format (expected </cell> got"
1522 << line << ')' << endl;
1526 l_getline(is, line);
1527 if (!prefixIs(line, "</row>")) {
1528 lyxerr << "Wrong tabular format (expected </row> got"
1529 << line << ')' << endl;
1533 while (!prefixIs(line, "</lyxtabular>")) {
1534 l_getline(is, line);
1540 bool Tabular::isMultiColumn(idx_type cell) const
1542 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1543 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1547 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1549 return cell_info[cellRow(cell)][cellColumn(cell)];
1553 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1555 idx_type const col = cellColumn(cell);
1556 idx_type const row = cellRow(cell);
1557 for (idx_type i = 0; i < number; ++i)
1558 unsetMultiRow(cellIndex(row, col + i));
1560 // unsetting of multirow may have invalidated cell index
1561 cell = cellIndex(row, col);
1562 CellData & cs = cellInfo(cell);
1563 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1564 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1565 cs.alignment = column_info[col].alignment;
1567 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1569 for (idx_type i = 1; i < number; ++i) {
1570 CellData & cs1 = cellInfo(cell + i);
1571 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1572 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1580 bool Tabular::isMultiRow(idx_type cell) const
1582 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1583 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1587 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1589 idx_type const col = cellColumn(cell);
1590 idx_type const row = cellRow(cell);
1591 for (idx_type i = 0; i < number; ++i)
1592 unsetMultiColumn(cellIndex(row + i, col));
1594 // unsetting of multicol may have invalidated cell index
1595 cell = cellIndex(row, col);
1596 CellData & cs = cellInfo(cell);
1597 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1598 cs.valignment = LYX_VALIGN_MIDDLE;
1599 // FIXME: the horizontal alignment of multirow cells can only
1600 // be changed for the whole table,
1601 // support for this needs to be implemented but us a fileformat
1602 // change (assigning this to uwestoehr)
1603 // until LyX supports this, use the deault alignment of multirow
1605 cs.alignment = LYX_ALIGN_CENTER;
1607 // set the bottom row of the last selected cell
1608 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1610 for (idx_type i = 1; i < number; ++i) {
1611 CellData & cs1 = cell_info[row + i][col];
1612 cs1.multirow = CELL_PART_OF_MULTIROW;
1613 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1621 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1623 row_type const row = cellRow(cell);
1624 col_type const col = cellColumn(cell);
1626 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1633 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1635 col_type const column = cellColumn(cell);
1636 col_type row = cellRow(cell) + 1;
1637 while (row < nrows() && isPartOfMultiRow(row, column))
1640 return row - cellRow(cell);
1644 void Tabular::unsetMultiColumn(idx_type cell)
1646 if (!isMultiColumn(cell))
1649 row_type const row = cellRow(cell);
1650 col_type const col = cellColumn(cell);
1651 row_type const span = columnSpan(cell);
1652 for (col_type c = 0; c < span; ++c)
1653 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1658 void Tabular::unsetMultiRow(idx_type cell)
1660 if (!isMultiRow(cell))
1663 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1664 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1665 row_type const row = cellRow(cell);
1666 col_type const col = cellColumn(cell);
1667 row_type const span = rowSpan(cell);
1668 for (row_type r = 0; r < span; ++r)
1669 cell_info[row + r][col].multirow = CELL_NORMAL;
1674 void Tabular::setRotateCell(idx_type cell, bool flag)
1676 cellInfo(cell).rotate = flag;
1680 bool Tabular::getRotateCell(idx_type cell) const
1682 return cellInfo(cell).rotate;
1686 bool Tabular::needRotating() const
1690 for (row_type r = 0; r < nrows(); ++r)
1691 for (col_type c = 0; c < ncols(); ++c)
1692 if (cell_info[r][c].rotate)
1698 bool Tabular::isLastCell(idx_type cell) const
1700 if (cell + 1 < numberofcells)
1706 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1708 if (cellRow(cell) == 0)
1711 col_type const col = cellColumn(cell);
1712 row_type r = cellRow(cell) - 1;
1713 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1716 return cell_info[r][col].cellno;
1720 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1722 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1723 if (nextrow < nrows())
1724 return cell_info[nextrow][cellColumn(cell)].cellno;
1729 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1731 LASSERT(column != npos && column < ncols()
1732 && row != npos && row < nrows(), /**/);
1733 return cell_info[row][column].cellno;
1737 void Tabular::setUsebox(idx_type cell, BoxType type)
1739 cellInfo(cell).usebox = type;
1743 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1744 // adds line breaks, see bug 4886.
1745 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1747 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1748 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1750 if (cellInfo(cell).usebox > 1)
1751 return cellInfo(cell).usebox;
1752 return useParbox(cell);
1757 // This are functions used for the longtable support
1759 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1765 row_info[row].endfirsthead = flag;
1769 row_info[row].endhead = flag;
1774 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1777 hd.set = haveLTHead();
1778 return row_info[row].endhead;
1782 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1785 hd.set = haveLTFirstHead();
1786 return row_info[row].endfirsthead;
1790 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1796 row_info[row].endlastfoot = flag;
1800 row_info[row].endfoot = flag;
1805 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1808 fd.set = haveLTFoot();
1809 return row_info[row].endfoot;
1813 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1816 fd.set = haveLTLastFoot();
1817 return row_info[row].endlastfoot;
1821 void Tabular::setLTNewPage(row_type row, bool what)
1823 row_info[row].newpage = what;
1827 bool Tabular::getLTNewPage(row_type row) const
1829 return row_info[row].newpage;
1833 bool Tabular::haveLTHead() const
1835 for (row_type i = 0; i < nrows(); ++i)
1836 if (row_info[i].endhead)
1842 bool Tabular::haveLTFirstHead() const
1844 if (endfirsthead.empty)
1846 for (row_type r = 0; r < nrows(); ++r)
1847 if (row_info[r].endfirsthead)
1853 bool Tabular::haveLTFoot() const
1855 for (row_type r = 0; r < nrows(); ++r)
1856 if (row_info[r].endfoot)
1862 bool Tabular::haveLTLastFoot() const
1864 if (endlastfoot.empty)
1866 for (row_type r = 0; r < nrows(); ++r)
1867 if (row_info[r].endlastfoot)
1873 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1875 idx_type i = getFirstCellInRow(row);
1877 setMultiColumn(i, numberOfCellsInRow(row));
1878 setTopLine(i, false);
1879 setBottomLine(i, false);
1880 setLeftLine(i, false);
1881 setRightLine(i, false);
1883 unsetMultiColumn(i);
1884 // When unsetting a caption row, also all existing
1885 // captions in this row must be dissolved.
1887 row_info[row].caption = what;
1892 bool Tabular::ltCaption(row_type row) const
1894 return row_info[row].caption;
1898 bool Tabular::haveLTCaption() const
1900 for (row_type r = 0; r < nrows(); ++r)
1901 if (row_info[r].caption)
1907 // end longtable support functions
1909 void Tabular::setRowAscent(row_type row, int height)
1911 if (row >= nrows() || row_info[row].ascent == height)
1913 row_info[row].ascent = height;
1917 void Tabular::setRowDescent(row_type row, int height)
1919 if (row >= nrows() || row_info[row].descent == height)
1921 row_info[row].descent = height;
1925 int Tabular::rowAscent(row_type row) const
1927 LASSERT(row < nrows(), /**/);
1928 return row_info[row].ascent;
1932 int Tabular::rowDescent(row_type row) const
1934 LASSERT(row < nrows(), /**/);
1935 return row_info[row].descent;
1939 int Tabular::height() const
1942 for (row_type row = 0; row < nrows(); ++row)
1943 height += rowAscent(row) + rowDescent(row) +
1949 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1951 LASSERT(row < nrows(), /**/);
1952 LASSERT(column < ncols(), /**/);
1953 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1957 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1959 LASSERT(row < nrows(), /**/);
1960 LASSERT(column < ncols(), /**/);
1961 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1965 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1967 // we only output complete row lines and the 1st row here, the rest
1968 // is done in Tabular::TeXBottomHLine(...)
1970 // get for each column the topline (if any)
1971 vector<bool> topline;
1973 for (col_type c = 0; c < ncols(); ++c) {
1974 topline.push_back(topLine(cellIndex(row, c)));
1975 // If cell is part of a multirow and not the first cell of the
1976 // multirow, no line must be drawn.
1978 if (isMultiRow(cellIndex(row, c))
1979 && isMultiRow(cellIndex(row - 1, c)))
1985 // do nothing if empty first row, or incomplete row line after
1986 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
1989 // only output complete row lines and the 1st row's clines
1990 if (nset == ncols()) {
1992 os << (row == 0 ? "\\toprule " : "\\midrule ");
1996 } else if (row == 0) {
1997 for (col_type c = 0; c < ncols(); ++c) {
1999 col_type offset = 0;
2000 for (col_type j = 0 ; j < c; ++j)
2001 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2004 //babel makes the "-" character an active one, so we have to suppress this here
2005 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2006 if (lang == "slovak" || lang == "czech")
2007 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2008 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2010 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2012 col_type cstart = c;
2013 for ( ; c < ncols() && topline[c]; ++c) {}
2015 for (col_type j = cstart ; j < c ; ++j)
2016 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2019 os << c + offset << "} ";
2028 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2030 // we output bottomlines of row r and the toplines of row r+1
2031 // if the latter do not span the whole tabular
2033 // get the bottomlines of row r, and toplines in next row
2034 bool lastrow = row == nrows() - 1;
2035 vector<bool> bottomline, topline;
2036 bool nextrowset = true;
2037 for (col_type c = 0; c < ncols(); ++c) {
2038 bottomline.push_back(bottomLine(cellIndex(row, c)));
2039 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2040 // If cell is part of a multirow and not the last or first cell of the
2041 // multirow, no line must be drawn.
2043 if (isMultiRow(cellIndex(row, c))
2044 && isMultiRow(cellIndex(row + 1, c))) {
2045 bottomline[c] = false;
2048 nextrowset &= topline[c];
2051 // combine this row's bottom lines and next row's toplines if necessary
2053 for (col_type c = 0; c < ncols(); ++c) {
2055 bottomline[c] = bottomline[c] || topline[c];
2060 // do nothing if empty, OR incomplete row line with a topline in next row
2061 if (nset == 0 || (nextrowset && nset != ncols()))
2064 if (nset == ncols()) {
2066 os << (lastrow ? "\\bottomrule" : "\\midrule");
2070 for (col_type c = 0; c < ncols(); ++c) {
2071 if (bottomline[c]) {
2072 col_type offset = 0;
2073 for (col_type j = 0 ; j < c; ++j)
2074 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2077 //babel makes the "-" character an active one, so we have to suppress this here
2078 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2079 if (lang == "slovak" || lang == "czech")
2080 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2081 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2083 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2085 col_type cstart = c;
2086 for ( ; c < ncols() && bottomline[c]; ++c) {}
2088 for (col_type j = cstart ; j < c ; ++j)
2089 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2092 os << c + offset << "} ";
2101 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2102 bool & ismulticol, bool & ismultirow) const
2105 row_type const r = cellRow(cell);
2106 if (is_long_tabular && row_info[r].caption)
2109 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2110 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2111 // figure out how to set the lines
2112 // we always set double lines to the right of the cell
2113 col_type const c = cellColumn(cell);
2114 col_type const nextcol = c + columnSpan(cell);
2115 bool colright = columnRightLine(c);
2116 bool colleft = columnLeftLine(c);
2117 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2118 bool nextcellleft = nextcol < ncols()
2119 && leftLine(cellIndex(r, nextcol));
2120 bool coldouble = colright && nextcolleft;
2121 bool celldouble = rightLine(cell) && nextcellleft;
2123 ismulticol = isMultiColumn(cell)
2124 || (c == 0 && colleft != leftLine(cell))
2125 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2126 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2127 || (coldouble != celldouble);
2129 // we center in multicol when no decimal point
2130 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2131 && (cellInfo(cell).decimal_width == 0));
2133 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2134 int latexcolspan = columnSpan(cell);
2135 for(col_type col = c; col < c + columnSpan(cell); ++col)
2136 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2140 os << "\\multicolumn{" << latexcolspan << "}{";
2141 if (c ==0 && leftLine(cell))
2143 if (!cellInfo(cell).align_special.empty()) {
2144 os << cellInfo(cell).align_special;
2146 if (!getPWidth(cell).zero()) {
2148 case LYX_ALIGN_LEFT:
2149 os << ">{\\raggedright}";
2151 case LYX_ALIGN_RIGHT:
2152 os << ">{\\raggedleft}";
2154 case LYX_ALIGN_CENTER:
2155 os << ">{\\centering}";
2161 case LYX_VALIGN_TOP:
2164 case LYX_VALIGN_MIDDLE:
2167 case LYX_VALIGN_BOTTOM:
2172 << from_ascii(getPWidth(cell).asLatexString())
2176 case LYX_ALIGN_LEFT:
2179 case LYX_ALIGN_RIGHT:
2186 } // end if else !getPWidth
2187 } // end if else !cellinfo_of_cell
2188 if (rightLine(cell) || nextcellleft)
2191 // add extra vertical line if we want a double one
2194 } // end if ismulticol
2196 // we only need code for the first multirow cell
2197 ismultirow = isMultiRow(cell);
2199 os << "\\multirow{" << rowSpan(cell) << "}{";
2200 if (!getPWidth(cell).zero())
2201 os << from_ascii(getPWidth(cell).asLatexString());
2203 // we need to set a default value
2204 // needs to be discussed
2207 } // end if ismultirow
2209 if (getRotateCell(cell)) {
2210 os << "\\begin{sideways}\n";
2213 if (getUsebox(cell) == BOX_PARBOX) {
2216 case LYX_VALIGN_TOP:
2219 case LYX_VALIGN_MIDDLE:
2222 case LYX_VALIGN_BOTTOM:
2226 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2228 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2229 os << "\\begin{minipage}[";
2231 case LYX_VALIGN_TOP:
2234 case LYX_VALIGN_MIDDLE:
2237 case LYX_VALIGN_BOTTOM:
2241 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2249 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2250 bool ismulticol, bool ismultirow) const
2253 row_type const r = cellRow(cell);
2254 if (is_long_tabular && row_info[r].caption)
2258 if (getUsebox(cell) == BOX_PARBOX)
2260 else if (getUsebox(cell) == BOX_MINIPAGE) {
2261 os << "%\n\\end{minipage}";
2264 if (getRotateCell(cell)) {
2265 os << "%\n\\end{sideways}";
2277 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2278 OutputParams const & runparams) const
2280 if (!is_long_tabular)
2285 // the caption must be output before the headers
2286 if (haveLTCaption()) {
2287 for (row_type r = 0; r < nrows(); ++r) {
2288 if (row_info[r].caption) {
2289 ret += TeXRow(os, r, runparams);
2293 // output first header info
2294 // first header must be output before the header, otherwise the
2295 // correct caption placement becomes really weird
2296 if (haveLTFirstHead()) {
2297 if (endfirsthead.topDL) {
2301 for (row_type r = 0; r < nrows(); ++r) {
2302 if (row_info[r].endfirsthead) {
2303 ret += TeXRow(os, r, runparams);
2306 if (endfirsthead.bottomDL) {
2310 os << "\\endfirsthead\n";
2313 // output header info
2315 if (endfirsthead.empty && !haveLTFirstHead()) {
2316 os << "\\endfirsthead\n";
2319 if (endhead.topDL) {
2323 for (row_type r = 0; r < nrows(); ++r) {
2324 if (row_info[r].endhead) {
2325 ret += TeXRow(os, r, runparams);
2328 if (endhead.bottomDL) {
2332 os << "\\endhead\n";
2335 // output footer info
2337 if (endfoot.topDL) {
2341 for (row_type r = 0; r < nrows(); ++r) {
2342 if (row_info[r].endfoot) {
2343 ret += TeXRow(os, r, runparams);
2346 if (endfoot.bottomDL) {
2350 os << "\\endfoot\n";
2352 if (endlastfoot.empty && !haveLTLastFoot()) {
2353 os << "\\endlastfoot\n";
2357 // output lastfooter info
2358 if (haveLTLastFoot()) {
2359 if (endlastfoot.topDL) {
2363 for (row_type r = 0; r < nrows(); ++r) {
2364 if (row_info[r].endlastfoot) {
2365 ret += TeXRow(os, r, runparams);
2368 if (endlastfoot.bottomDL) {
2372 os << "\\endlastfoot\n";
2379 bool Tabular::isValidRow(row_type row) const
2381 if (!is_long_tabular)
2383 return !row_info[row].endhead && !row_info[row].endfirsthead
2384 && !row_info[row].endfoot && !row_info[row].endlastfoot
2385 && !row_info[row].caption;
2389 int Tabular::TeXRow(odocstream & os, row_type row,
2390 OutputParams const & runparams) const
2392 idx_type cell = cellIndex(row, 0);
2393 shared_ptr<InsetTableCell> inset = cellInset(cell);
2394 Paragraph const & par = inset->paragraphs().front();
2395 string const lang = par.getParLanguage(buffer().params())->lang();
2397 //output the top line
2398 int ret = TeXTopHLine(os, row, lang);
2400 if (row_info[row].top_space_default) {
2402 os << "\\addlinespace\n";
2404 os << "\\noalign{\\vskip\\doublerulesep}\n";
2406 } else if(!row_info[row].top_space.zero()) {
2408 os << "\\addlinespace["
2409 << from_ascii(row_info[row].top_space.asLatexString())
2412 os << "\\noalign{\\vskip"
2413 << from_ascii(row_info[row].top_space.asLatexString())
2418 bool ismulticol = false;
2419 bool ismultirow = false;
2420 for (col_type c = 0; c < ncols(); ++c) {
2421 if (isPartOfMultiColumn(row, c))
2424 if (isPartOfMultiRow(row, c) &&
2425 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2430 cell = cellIndex(row, c);
2431 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2432 shared_ptr<InsetTableCell> inset = cellInset(cell);
2434 Paragraph const & par = inset->paragraphs().front();
2435 bool rtl = par.isRTL(buffer().params())
2437 && getPWidth(cell).zero();
2441 par.getParLanguage(buffer().params())->lang();
2442 if (lang == "farsi")
2444 else if (lang == "arabic_arabi")
2446 // currently, remaning RTL languages are
2447 // arabic_arabtex and hebrew
2451 // pass to the OutputParams that we are in a cell and
2452 // which alignment we have set.
2453 // InsetNewline needs this context information.
2454 OutputParams newrp(runparams);
2455 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2456 ? OutputParams::PLAIN
2457 : OutputParams::ALIGNED;
2459 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2460 && cellInfo(cell).decimal_width != 0) {
2461 // copy cell and split in 2
2462 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2463 head.getText(0)->setMacrocontextPosition(
2464 cellInset(cell)->getText(0)->macrocontextPosition());
2465 head.setBuffer(buffer());
2466 bool hassep = false;
2467 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2468 head.latex(os, newrp);
2470 ret += tail.latex(os, newrp);
2471 } else if (!isPartOfMultiRow(row, c))
2472 ret += inset->latex(os, newrp);
2474 runparams.encoding = newrp.encoding;
2478 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2479 if (cell != getLastCellInRow(row)) { // not last cell in row
2483 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2484 // if no first header and no empty first header is used,
2485 // the caption needs to be terminated by \endfirsthead
2487 os << "\\endfirsthead";
2489 os << "\\tabularnewline";
2490 if (row_info[row].bottom_space_default) {
2492 os << "\\addlinespace";
2494 os << "[\\doublerulesep]";
2495 } else if (!row_info[row].bottom_space.zero()) {
2497 os << "\\addlinespace";
2499 << from_ascii(row_info[row].bottom_space.asLatexString())
2505 //output the bottom line
2506 ret += TeXBottomHLine(os, row, lang);
2508 if (row_info[row].interline_space_default) {
2510 os << "\\addlinespace\n";
2512 os << "\\noalign{\\vskip\\doublerulesep}\n";
2514 } else if (!row_info[row].interline_space.zero()) {
2516 os << "\\addlinespace["
2517 << from_ascii(row_info[row].interline_space.asLatexString())
2520 os << "\\noalign{\\vskip"
2521 << from_ascii(row_info[row].interline_space.asLatexString())
2529 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2533 //+---------------------------------------------------------------------
2534 //+ first the opening preamble +
2535 //+---------------------------------------------------------------------
2538 os << "\\begin{sideways}\n";
2541 if (is_long_tabular) {
2542 os << "\\begin{longtable}";
2543 switch (longtabular_alignment) {
2544 case LYX_LONGTABULAR_ALIGN_LEFT:
2547 case LYX_LONGTABULAR_ALIGN_CENTER:
2549 case LYX_LONGTABULAR_ALIGN_RIGHT:
2554 os << "\\begin{tabular}";
2555 switch (tabular_valignment) {
2556 case LYX_VALIGN_TOP:
2559 case LYX_VALIGN_MIDDLE:
2561 case LYX_VALIGN_BOTTOM:
2569 for (col_type c = 0; c < ncols(); ++c) {
2570 if (columnLeftLine(c))
2572 if (!column_info[c].align_special.empty()) {
2573 os << column_info[c].align_special;
2575 if (!column_info[c].p_width.zero()) {
2576 switch (column_info[c].alignment) {
2577 case LYX_ALIGN_LEFT:
2578 os << ">{\\raggedright}";
2580 case LYX_ALIGN_RIGHT:
2581 os << ">{\\raggedleft}";
2583 case LYX_ALIGN_CENTER:
2584 os << ">{\\centering}";
2586 case LYX_ALIGN_NONE:
2587 case LYX_ALIGN_BLOCK:
2588 case LYX_ALIGN_LAYOUT:
2589 case LYX_ALIGN_SPECIAL:
2590 case LYX_ALIGN_DECIMAL:
2594 switch (column_info[c].valignment) {
2595 case LYX_VALIGN_TOP:
2598 case LYX_VALIGN_MIDDLE:
2601 case LYX_VALIGN_BOTTOM:
2606 << from_ascii(column_info[c].p_width.asLatexString())
2609 switch (column_info[c].alignment) {
2610 case LYX_ALIGN_LEFT:
2613 case LYX_ALIGN_RIGHT:
2616 case LYX_ALIGN_DECIMAL:
2617 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2623 } // end if else !column_info[i].p_width
2624 } // end if else !column_info[i].align_special
2625 if (columnRightLine(c))
2631 ret += TeXLongtableHeaderFooter(os, runparams);
2633 //+---------------------------------------------------------------------
2634 //+ the single row and columns (cells) +
2635 //+---------------------------------------------------------------------
2637 for (row_type r = 0; r < nrows(); ++r) {
2638 if (isValidRow(r)) {
2639 ret += TeXRow(os, r, runparams);
2640 if (is_long_tabular && row_info[r].newpage) {
2641 os << "\\newpage\n";
2647 //+---------------------------------------------------------------------
2648 //+ the closing of the tabular +
2649 //+---------------------------------------------------------------------
2651 if (is_long_tabular)
2652 os << "\\end{longtable}";
2654 os << "\\end{tabular}";
2656 os << "\n\\end{sideways}";
2664 int Tabular::docbookRow(odocstream & os, row_type row,
2665 OutputParams const & runparams) const
2668 idx_type cell = getFirstCellInRow(row);
2671 for (col_type c = 0; c < ncols(); ++c) {
2672 if (isPartOfMultiColumn(row, c))
2675 os << "<entry align=\"";
2676 switch (getAlignment(cell)) {
2677 case LYX_ALIGN_LEFT:
2680 case LYX_ALIGN_RIGHT:
2688 os << "\" valign=\"";
2689 switch (getVAlignment(cell)) {
2690 case LYX_VALIGN_TOP:
2693 case LYX_VALIGN_BOTTOM:
2696 case LYX_VALIGN_MIDDLE:
2701 if (isMultiColumn(cell)) {
2702 os << " namest=\"col" << c << "\" ";
2703 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2707 ret += cellInset(cell)->docbook(os, runparams);
2716 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2720 //+---------------------------------------------------------------------
2721 //+ first the opening preamble +
2722 //+---------------------------------------------------------------------
2724 os << "<tgroup cols=\"" << ncols()
2725 << "\" colsep=\"1\" rowsep=\"1\">\n";
2727 for (col_type c = 0; c < ncols(); ++c) {
2728 os << "<colspec colname=\"col" << c << "\" align=\"";
2729 switch (column_info[c].alignment) {
2730 case LYX_ALIGN_LEFT:
2733 case LYX_ALIGN_RIGHT:
2741 if (runparams.flavor == OutputParams::XML)
2747 //+---------------------------------------------------------------------
2748 //+ Long Tabular case +
2749 //+---------------------------------------------------------------------
2751 // output caption info
2752 if (haveLTCaption()) {
2753 os << "<caption>\n";
2755 for (row_type r = 0; r < nrows(); ++r) {
2756 if (row_info[r].caption) {
2757 ret += docbookRow(os, r, runparams);
2760 os << "</caption>\n";
2763 // output header info
2764 if (haveLTHead() || haveLTFirstHead()) {
2767 for (row_type r = 0; r < nrows(); ++r) {
2768 if (row_info[r].endhead || row_info[r].endfirsthead) {
2769 ret += docbookRow(os, r, runparams);
2775 // output footer info
2776 if (haveLTFoot() || haveLTLastFoot()) {
2779 for (row_type r = 0; r < nrows(); ++r) {
2780 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2781 ret += docbookRow(os, r, runparams);
2788 //+---------------------------------------------------------------------
2789 //+ the single row and columns (cells) +
2790 //+---------------------------------------------------------------------
2794 for (row_type r = 0; r < nrows(); ++r) {
2795 if (isValidRow(r)) {
2796 ret += docbookRow(os, r, runparams);
2801 //+---------------------------------------------------------------------
2802 //+ the closing of the tabular +
2803 //+---------------------------------------------------------------------
2812 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2813 OutputParams const & runparams) const
2816 idx_type cell = getFirstCellInRow(row);
2818 xs << html::StartTag("tr");
2819 for (col_type c = 0; c < ncols(); ++c) {
2820 if (isPartOfMultiColumn(row, c))
2825 switch (getAlignment(cell)) {
2826 case LYX_ALIGN_LEFT:
2829 case LYX_ALIGN_RIGHT:
2837 attr << " valign='";
2838 switch (getVAlignment(cell)) {
2839 case LYX_VALIGN_TOP:
2842 case LYX_VALIGN_BOTTOM:
2845 case LYX_VALIGN_MIDDLE:
2850 if (isMultiColumn(cell))
2851 attr << " colspan='" << columnSpan(cell) << "'";
2853 xs << html::StartTag("td", attr.str());
2854 ret += cellInset(cell)->xhtml(xs, runparams);
2855 xs << html::EndTag("td");
2858 xs << html::EndTag("tr");
2863 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2866 // It's unclear to me if we need to mess with the long table stuff.
2867 // We can borrow that too from docbook, if so.
2869 xs << html::StartTag("tbody");
2870 for (row_type r = 0; r < nrows(); ++r) {
2871 if (isValidRow(r)) {
2872 ret += xhtmlRow(xs, r, runparams);
2875 xs << html::EndTag("tbody");
2880 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2881 vector<unsigned int> const & clen) const
2883 idx_type const fcell = getFirstCellInRow(row);
2884 idx_type const n = numberOfCellsInRow(row) + fcell;
2887 for (idx_type i = fcell; i < n; ++i) {
2897 for (idx_type i = fcell; i < n; ++i) {
2908 col_type column = cellColumn(i);
2909 int len = clen[column];
2910 while (column < ncols() - 1
2911 && isPartOfMultiColumn(row, ++column))
2912 len += clen[column] + 4;
2913 os << docstring(len, ch);
2928 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2929 vector<unsigned int> const & clen) const
2931 idx_type const fcell = getFirstCellInRow(row);
2932 idx_type const n = numberOfCellsInRow(row) + fcell;
2935 for (idx_type i = fcell; i < n; ++i) {
2936 if (bottomLine(i)) {
2945 for (idx_type i = fcell; i < n; ++i) {
2946 if (bottomLine(i)) {
2956 col_type column = cellColumn(i);
2957 int len = clen[column];
2958 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2959 len += clen[column] + 4;
2960 os << docstring(len, ch);
2961 if (bottomLine(i)) {
2975 void Tabular::plaintextPrintCell(odocstream & os,
2976 OutputParams const & runparams,
2977 idx_type cell, row_type row, col_type column,
2978 vector<unsigned int> const & clen,
2979 bool onlydata) const
2981 odocstringstream sstr;
2982 cellInset(cell)->plaintext(sstr, runparams);
2994 unsigned int len1 = sstr.str().length();
2995 unsigned int len2 = clen[column];
2996 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2997 len2 += clen[column] + 4;
3000 switch (getAlignment(cell)) {
3002 case LYX_ALIGN_LEFT:
3005 case LYX_ALIGN_RIGHT:
3009 case LYX_ALIGN_CENTER:
3015 os << docstring(len1, ' ') << sstr.str()
3016 << docstring(len2, ' ');
3018 if (rightLine(cell))
3025 void Tabular::plaintext(odocstream & os,
3026 OutputParams const & runparams, int const depth,
3027 bool onlydata, char_type delim) const
3029 // first calculate the width of the single columns
3030 vector<unsigned int> clen(ncols());
3033 // first all non multicolumn cells!
3034 for (col_type c = 0; c < ncols(); ++c) {
3036 for (row_type r = 0; r < nrows(); ++r) {
3037 idx_type cell = cellIndex(r, c);
3038 if (isMultiColumn(cell))
3040 odocstringstream sstr;
3041 cellInset(cell)->plaintext(sstr, runparams);
3042 if (clen[c] < sstr.str().length())
3043 clen[c] = sstr.str().length();
3046 // then all multicolumn cells!
3047 for (col_type c = 0; c < ncols(); ++c) {
3048 for (row_type r = 0; r < nrows(); ++r) {
3049 idx_type cell = cellIndex(r, c);
3050 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3052 odocstringstream sstr;
3053 cellInset(cell)->plaintext(sstr, runparams);
3054 int len = int(sstr.str().length());
3055 idx_type const n = columnSpan(cell);
3056 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3058 if (len > int(clen[c + n - 1]))
3059 clen[c + n - 1] = len;
3064 for (row_type r = 0; r < nrows(); ++r) {
3065 if (!onlydata && plaintextTopHLine(os, r, clen))
3066 os << docstring(depth * 2, ' ');
3067 for (col_type c = 0; c < ncols(); ++c) {
3068 if (isPartOfMultiColumn(r, c))
3070 if (onlydata && c > 0)
3071 // we don't use operator<< for single UCS4 character.
3072 // see explanation in docstream.h
3074 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3079 os << docstring(depth * 2, ' ');
3080 if (plaintextBottomHLine(os, r, clen))
3081 os << docstring(depth * 2, ' ');
3087 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3089 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3093 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3094 col_type column) const
3096 return cell_info[row][column].inset;
3100 void Tabular::setCellInset(row_type row, col_type column,
3101 shared_ptr<InsetTableCell> ins) const
3103 CellData & cd = cell_info[row][column];
3108 void Tabular::validate(LaTeXFeatures & features) const
3110 features.require("NeedTabularnewline");
3112 features.require("booktabs");
3113 if (is_long_tabular)
3114 features.require("longtable");
3116 features.require("rotating");
3117 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3118 if (isMultiRow(cell))
3119 features.require("multirow");
3120 if (getVAlignment(cell) != LYX_VALIGN_TOP
3121 || !getPWidth(cell).zero())
3122 features.require("array");
3123 cellInset(cell)->validate(features);
3128 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3130 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3131 ParagraphList::const_iterator cit = parlist.begin();
3132 ParagraphList::const_iterator end = parlist.end();
3134 for (; cit != end; ++cit)
3135 for (int i = 0; i < cit->size(); ++i)
3136 if (cit->isNewline(i))
3143 /////////////////////////////////////////////////////////////////////
3147 /////////////////////////////////////////////////////////////////////
3149 InsetTableCell::InsetTableCell(Buffer * buf)
3150 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3151 contentAlign(LYX_ALIGN_CENTER)
3155 bool InsetTableCell::forcePlainLayout(idx_type) const
3157 return !isFixedWidth;
3161 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3163 return isFixedWidth;
3167 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3168 FuncStatus & status) const
3171 switch (cmd.action()) {
3173 enabled = !forcePlainLayout();
3175 case LFUN_LAYOUT_PARAGRAPH:
3176 enabled = allowParagraphCustomization();
3179 return InsetText::getStatus(cur, cmd, status);
3181 status.setEnabled(enabled);
3185 docstring InsetTableCell::asString(bool intoInsets)
3188 if (paragraphs().empty())
3190 ParagraphList::const_iterator it = paragraphs().begin();
3191 ParagraphList::const_iterator en = paragraphs().end();
3193 for (; it != en; ++it) {
3198 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3204 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3207 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3208 return InsetText::xhtml(xs, rp);
3213 /////////////////////////////////////////////////////////////////////
3217 /////////////////////////////////////////////////////////////////////
3219 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3221 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3222 rowselect_(false), colselect_(false)
3227 InsetTabular::InsetTabular(InsetTabular const & tab)
3228 : Inset(tab), tabular(tab.tabular), scx_(0)
3233 InsetTabular::~InsetTabular()
3235 hideDialogs("tabular", this);
3239 void InsetTabular::setBuffer(Buffer & buf)
3241 tabular.setBuffer(buf);
3242 Inset::setBuffer(buf);
3246 bool InsetTabular::insetAllowed(InsetCode code) const
3251 case MATHMACRO_CODE:
3256 return tabular.is_long_tabular;
3264 void InsetTabular::write(ostream & os) const
3266 os << "Tabular" << endl;
3271 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3273 // FIXME: depending on the selection state, we could offer a different menu.
3274 return from_ascii("context-tabular");
3278 void InsetTabular::read(Lexer & lex)
3280 //bool const old_format = (lex.getString() == "\\LyXTable");
3288 string token = lex.getString();
3289 while (lex && token != "\\end_inset") {
3291 token = lex.getString();
3294 lex.printError("Missing \\end_inset at this point. ");
3298 int InsetTabular::rowFromY(Cursor & cur, int y) const
3300 // top y coordinate of tabular
3301 int h = yo(cur.bv()) - tabular.rowAscent(0);
3303 for (; r < tabular.nrows() && y > h; ++r)
3304 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3305 + tabular.interRowSpace(r);
3311 int InsetTabular::columnFromX(Cursor & cur, int x) const
3313 // left x coordinate of tabular
3314 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3316 for (; c < tabular.ncols() && x > w; ++c)
3317 w += tabular.cellWidth(c);
3322 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3324 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3325 // mi.base.textwidth << "\n";
3328 LASSERT(false, /**/);
3331 for (row_type r = 0; r < tabular.nrows(); ++r) {
3334 for (col_type c = 0; c < tabular.ncols(); ++c) {
3335 if (tabular.isPartOfMultiColumn(r, c)
3336 || tabular.isPartOfMultiRow(r, c))
3337 // multicolumn or multirow cell, but not first one
3339 idx_type const cell = tabular.cellIndex(r, c);
3342 Length const p_width = tabular.getPWidth(cell);
3343 if (!p_width.zero())
3344 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3345 tabular.cellInset(cell)->metrics(m, dim);
3346 if (!p_width.zero())
3347 dim.wid = m.base.textwidth;
3348 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3349 + tabular.interColumnSpace(cell);
3351 // FIXME(?): do we need a second metrics call?
3352 TextMetrics const & tm =
3353 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3355 // determine horiz offset because of decimal align (if necessary)
3356 int decimal_hoffset = 0;
3357 int decimal_width = 0;
3358 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3359 // make a copy which we will split in 2
3360 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3361 head.getText(0)->setMacrocontextPosition(
3362 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3363 head.setBuffer(tabular.buffer());
3364 // split in 2 and calculate width of each part
3365 bool hassep = false;
3366 InsetTableCell tail =
3367 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3369 head.metrics(m, dim1);
3370 decimal_hoffset = dim1.width();
3372 tail.metrics(m, dim1);
3373 decimal_width = dim1.width();
3376 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3377 tabular.cell_info[r][c].decimal_width = decimal_width;
3379 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3380 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3381 int const lastpardes = tm.last().second->descent()
3382 + TEXT_TO_INSET_OFFSET;
3384 switch (tabular.getVAlignment(cell)) {
3385 case Tabular::LYX_VALIGN_TOP:
3387 case Tabular::LYX_VALIGN_MIDDLE:
3388 offset = -(dim.des - lastpardes)/2;
3390 case Tabular::LYX_VALIGN_BOTTOM:
3391 offset = -(dim.des - lastpardes);
3394 tabular.cell_info[r][c].voffset = offset;
3395 maxasc = max(maxasc, dim.asc - offset);
3396 maxdes = max(maxdes, dim.des + offset);
3398 int const top_space = tabular.row_info[r].top_space_default ?
3399 default_line_space :
3400 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3401 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3402 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3403 default_line_space :
3404 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3405 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3407 tabular.updateColumnWidths();
3408 dim.asc = tabular.rowAscent(0);
3409 dim.des = tabular.height() - dim.asc;
3410 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3414 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3417 if (&cur.inset() == this && cur.selection()) {
3418 if (cur.selIsMultiCell()) {
3421 getSelection(cur, rs, re, cs, ce);
3423 idx_type const cell = tabular.cellIndex(row, col);
3424 col_type const cspan = tabular.columnSpan(cell);
3425 row_type const rspan = tabular.rowSpan(cell);
3426 if (col + cspan - 1 >= cs && col <= ce
3427 && row + rspan - 1 >= rs && row <= re)
3430 if (col == tabular.cellColumn(cur.idx())
3431 && row == tabular.cellRow(cur.idx())) {
3432 CursorSlice const & beg = cur.selBegin();
3433 CursorSlice const & end = cur.selEnd();
3435 if ((end.lastpos() > 0 || end.lastpit() > 0)
3436 && end.pos() == end.lastpos() && beg.pos() == 0
3437 && end.pit() == end.lastpit() && beg.pit() == 0)
3445 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3447 x += scx_ + ADD_TO_TABULAR_WIDTH;
3449 BufferView * bv = pi.base.bv;
3450 Cursor & cur = pi.base.bv->cursor();
3453 // FIXME: As the full background is painted in drawSelection(),
3454 // we have no choice but to do a full repaint for the Text cells.
3455 pi.full_repaint = true;
3457 bool const original_selection_state = pi.selected;
3460 first_visible_cell = Tabular::npos;
3461 for (row_type r = 0; r < tabular.nrows(); ++r) {
3463 for (col_type c = 0; c < tabular.ncols(); ++c) {
3464 if (tabular.isPartOfMultiColumn(r, c))
3467 idx = tabular.cellIndex(r, c);
3469 if (tabular.isPartOfMultiRow(r, c)) {
3470 nx += tabular.cellWidth(idx);
3474 if (first_visible_cell == Tabular::npos)
3475 first_visible_cell = idx;
3477 pi.selected |= isCellSelected(cur, r, c);
3478 int const cx = nx + tabular.textHOffset(idx);
3479 int const cy = y + tabular.textVOffset(idx);
3480 // Cache the Inset position.
3481 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3482 cell(idx)->draw(pi, cx, cy);
3483 drawCellLines(pi, nx, y, r, idx);
3484 nx += tabular.cellWidth(idx);
3485 pi.selected = original_selection_state;
3488 if (r + 1 < tabular.nrows())
3489 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3490 + tabular.interRowSpace(r + 1);
3495 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3497 Cursor & cur = pi.base.bv->cursor();
3500 x += scx_ + ADD_TO_TABULAR_WIDTH;
3502 // FIXME: it is wrong to completely paint the background
3503 // if we want to do single row painting.
3505 // Paint background of current tabular
3506 int const w = tabular.width();
3507 int const h = tabular.height();
3508 int yy = y - tabular.rowAscent(0);
3509 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3511 if (!cur.selection())
3513 if (&cur.inset() != this)
3518 bool const full_cell_selected = isCellSelected(cur,
3519 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3521 if (cur.selIsMultiCell() || full_cell_selected) {
3522 for (row_type r = 0; r < tabular.nrows(); ++r) {
3524 for (col_type c = 0; c < tabular.ncols(); ++c) {
3525 if (tabular.isPartOfMultiColumn(r, c))
3528 idx_type const cell = tabular.cellIndex(r, c);
3530 if (tabular.isPartOfMultiRow(r, c)) {
3531 xx += tabular.cellWidth(cell);
3534 int const w = tabular.cellWidth(cell);
3535 int const h = tabular.cellHeight(cell);
3536 int const yy = y - tabular.rowAscent(r);
3537 if (isCellSelected(cur, r, c))
3538 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3541 if (r + 1 < tabular.nrows())
3542 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3543 + tabular.interRowSpace(r + 1);
3547 x += cellXPos(cur.idx());
3548 x += tabular.textHOffset(cur.idx());
3549 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3554 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3555 row_type row, idx_type cell) const
3557 y -= tabular.rowAscent(row);
3558 int const w = tabular.cellWidth(cell);
3559 int const h = tabular.cellHeight(cell);
3560 Color const linecolor = pi.textColor(Color_tabularline);
3561 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3564 bool drawline = tabular.topLine(cell)
3565 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3566 pi.pain.line(x, y, x + w, y,
3567 drawline ? linecolor : gridcolor,
3568 drawline ? Painter::line_solid : Painter::line_onoffdash);
3571 drawline = tabular.bottomLine(cell);
3572 pi.pain.line(x, y + h, x + w, y + h,
3573 drawline ? linecolor : gridcolor,
3574 drawline ? Painter::line_solid : Painter::line_onoffdash);
3577 col_type const col = tabular.cellColumn(cell);
3578 drawline = tabular.leftLine(cell)
3579 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3580 pi.pain.line(x, y, x, y + h,
3581 drawline ? linecolor : gridcolor,
3582 drawline ? Painter::line_solid : Painter::line_onoffdash);
3585 x -= tabular.interColumnSpace(cell);
3586 drawline = tabular.rightLine(cell)
3587 || (col + 1 < tabular.ncols()
3588 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3589 pi.pain.line(x + w, y, x + w, y + h,
3590 drawline ? linecolor : gridcolor,
3591 drawline ? Painter::line_solid : Painter::line_onoffdash);
3595 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3597 //lyxerr << "InsetTabular::edit: " << this << endl;
3599 cur.setSelection(false);
3602 if (isRightToLeft(cur))
3603 cur.idx() = tabular.getLastCellInRow(0);
3609 if (isRightToLeft(cur))
3610 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3612 cur.idx() = tabular.numberofcells - 1;
3614 cur.pos() = cur.lastpos(); // FIXME crude guess
3616 cur.setCurrentFont();
3617 // FIXME: this accesses the position cache before it is initialized
3619 //cur.bv().fitCursor();
3623 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3625 // In a longtable, tell captions what the current float is
3626 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3627 string const saveflt = cnts.current_float();
3628 if (tabular.is_long_tabular)
3629 cnts.current_float("table");
3631 ParIterator it2 = it;
3633 size_t const end = it2.nargs();
3634 for ( ; it2.idx() < end; it2.top().forwardIdx())
3635 buffer().updateBuffer(it2, utype);
3638 if (tabular.is_long_tabular)
3639 cnts.current_float(saveflt);
3643 void InsetTabular::addToToc(DocIterator const & cpit)
3645 DocIterator dit = cpit;
3647 size_t const end = dit.nargs();
3648 for ( ; dit.idx() < end; dit.top().forwardIdx())
3649 cell(dit.idx())->addToToc(dit);
3653 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3655 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3656 << "\n cur:" << cur);
3657 CursorSlice sl = cur.top();
3658 Cursor & bvcur = cur.bv().cursor();
3660 FuncCode const act = cmd.action();
3664 case LFUN_MOUSE_PRESS: {
3665 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3667 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3668 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3669 row_type r = rowFromY(cur, cmd.y());
3670 cur.idx() = tabular.getFirstCellInRow(r);
3673 cur.idx() = tabular.getLastCellInRow(r);
3674 cur.pos() = cur.lastpos();
3675 cur.setSelection(true);
3681 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3682 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3683 || cmd.y() > y0 + tabular.height()) {
3684 col_type c = columnFromX(cur, cmd.x());
3685 cur.idx() = tabular.cellIndex(0, c);
3688 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3689 cur.pos() = cur.lastpos();
3690 cur.setSelection(true);
3695 // do not reset cursor/selection if we have selected
3696 // some cells (bug 2715).
3697 if (cmd.button() == mouse_button::button3
3698 && &bvcur.selBegin().inset() == this
3699 && bvcur.selIsMultiCell())
3702 // Let InsetTableCell do it
3703 cell(cur.idx())->dispatch(cur, cmd);
3706 case LFUN_MOUSE_MOTION:
3707 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3708 if (cmd.button() == mouse_button::button1) {
3709 // only accept motions to places not deeper nested than the real anchor
3710 if (!bvcur.realAnchor().hasPart(cur)) {
3714 // select (additional) row
3716 row_type r = rowFromY(cur, cmd.y());
3717 cur.idx() = tabular.getLastCellInRow(r);
3718 // we need to reset the cursor's pit and pos now, as the old ones
3719 // may no longer be valid.
3722 bvcur.setCursor(cur);
3723 bvcur.setSelection(true);
3726 // select (additional) column
3728 col_type c = columnFromX(cur, cmd.x());
3729 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3730 // we need to reset the cursor's pit and pos now, as the old ones
3731 // may no longer be valid.
3734 bvcur.setCursor(cur);
3735 bvcur.setSelection(true);
3738 // only update if selection changes
3739 if (bvcur.idx() == cur.idx() &&
3740 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3741 cur.noScreenUpdate();
3742 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3743 bvcur.setCursor(cur);
3744 bvcur.setSelection(true);
3745 // if this is a multicell selection, we just set the cursor to
3746 // the beginning of the cell's text.
3747 if (bvcur.selIsMultiCell()) {
3748 bvcur.pit() = bvcur.lastpit();
3749 bvcur.pos() = bvcur.lastpos();
3754 case LFUN_MOUSE_RELEASE:
3759 case LFUN_CELL_BACKWARD:
3761 cur.setSelection(false);
3764 case LFUN_CELL_FORWARD:
3766 cur.setSelection(false);
3769 case LFUN_CHAR_FORWARD_SELECT:
3770 case LFUN_CHAR_FORWARD:
3771 case LFUN_CHAR_BACKWARD_SELECT:
3772 case LFUN_CHAR_BACKWARD:
3773 case LFUN_CHAR_RIGHT_SELECT:
3774 case LFUN_CHAR_RIGHT:
3775 case LFUN_CHAR_LEFT_SELECT:
3776 case LFUN_CHAR_LEFT:
3777 case LFUN_WORD_FORWARD:
3778 case LFUN_WORD_FORWARD_SELECT:
3779 case LFUN_WORD_BACKWARD:
3780 case LFUN_WORD_BACKWARD_SELECT:
3781 case LFUN_WORD_RIGHT:
3782 case LFUN_WORD_RIGHT_SELECT:
3783 case LFUN_WORD_LEFT:
3784 case LFUN_WORD_LEFT_SELECT: {
3785 // determine whether we move to next or previous cell, where to enter
3786 // the new cell from, and which command to "finish" (i.e., exit the
3789 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3790 FuncCode finish_lfun;
3792 if (act == LFUN_CHAR_FORWARD
3793 || act == LFUN_CHAR_FORWARD_SELECT
3794 || act == LFUN_WORD_FORWARD
3795 || act == LFUN_WORD_FORWARD_SELECT) {
3797 finish_lfun = LFUN_FINISHED_FORWARD;
3799 else if (act == LFUN_CHAR_BACKWARD
3800 || act == LFUN_CHAR_BACKWARD_SELECT
3801 || act == LFUN_WORD_BACKWARD
3802 || act == LFUN_WORD_BACKWARD_SELECT) {
3804 finish_lfun = LFUN_FINISHED_BACKWARD;
3806 // LEFT or RIGHT commands --- the interpretation will depend on the
3807 // table's direction.
3809 bool const right = act == LFUN_CHAR_RIGHT
3810 || act == LFUN_CHAR_RIGHT_SELECT
3811 || act == LFUN_WORD_RIGHT
3812 || act == LFUN_WORD_RIGHT_SELECT;
3813 next_cell = isRightToLeft(cur) != right;
3815 if (lyxrc.visual_cursor)
3816 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3818 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3821 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3822 || act == LFUN_CHAR_BACKWARD_SELECT
3823 || act == LFUN_CHAR_RIGHT_SELECT
3824 || act == LFUN_CHAR_LEFT_SELECT
3825 || act == LFUN_WORD_FORWARD_SELECT
3826 || act == LFUN_WORD_RIGHT_SELECT
3827 || act == LFUN_WORD_BACKWARD_SELECT
3828 || act == LFUN_WORD_LEFT_SELECT;
3830 // If we have a multicell selection or we're
3831 // not doing some LFUN_*_SELECT thing anyway...
3832 if (!cur.selIsMultiCell() || !select) {
3833 col_type const c = tabular.cellColumn(cur.idx());
3834 row_type const r = tabular.cellRow(cur.idx());
3835 // Are we trying to select the whole cell and is the whole cell
3836 // not yet selected?
3837 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3838 ((next_cell && cur.pit() == cur.lastpit()
3839 && cur.pos() == cur.lastpos())
3840 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3842 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3844 // ...try to dispatch to the cell's inset.
3845 cell(cur.idx())->dispatch(cur, cmd);
3847 // When we already have a selection we want to select the whole cell
3848 // before going to the next cell.
3849 if (select_whole && !empty_cell){
3850 getText(cur.idx())->selectAll(cur);
3855 // FIXME: When we support the selection of an empty cell, remove
3856 // the !empty_cell from this condition. For now we jump to the next
3857 // cell if the current cell is empty.
3858 if (cur.result().dispatched() && !empty_cell)
3862 // move to next/prev cell, as appropriate
3863 // note that we will always do this if we're selecting and we have
3864 // a multicell selection
3865 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3866 << " cell from: " << int(entry_from));
3868 moveNextCell(cur, entry_from);
3870 movePrevCell(cur, entry_from);
3871 // if we're exiting the table, call the appropriate FINISHED lfun
3872 if (sl == cur.top())
3873 cmd = FuncRequest(finish_lfun);
3880 case LFUN_DOWN_SELECT:
3882 if (!(cur.selection() && cur.selIsMultiCell()))
3883 cell(cur.idx())->dispatch(cur, cmd);
3885 cur.dispatched(); // override the cell's decision
3886 if (sl == cur.top()) {
3887 // if our Text didn't do anything to the cursor
3888 // then we try to put the cursor into the cell below
3889 // setting also the right targetX.
3890 cur.selHandle(act == LFUN_DOWN_SELECT);
3891 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3892 cur.idx() = tabular.cellBelow(cur.idx());
3894 TextMetrics const & tm =
3895 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3896 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3897 cur.setCurrentFont();
3900 if (sl == cur.top()) {
3901 // we trick it to go to forward after leaving the
3903 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3906 if (cur.selIsMultiCell()) {
3907 cur.pit() = cur.lastpit();
3908 cur.pos() = cur.lastpos();
3909 cur.setCurrentFont();
3914 case LFUN_UP_SELECT:
3916 if (!(cur.selection() && cur.selIsMultiCell()))
3917 cell(cur.idx())->dispatch(cur, cmd);
3918 cur.dispatched(); // override the cell's decision
3919 if (sl == cur.top()) {
3920 // if our Text didn't do anything to the cursor
3921 // then we try to put the cursor into the cell above
3922 // setting also the right targetX.
3923 cur.selHandle(act == LFUN_UP_SELECT);
3924 if (tabular.cellRow(cur.idx()) != 0) {
3925 cur.idx() = tabular.cellAbove(cur.idx());
3926 cur.pit() = cur.lastpit();
3927 Text const * text = cell(cur.idx())->getText(0);
3928 TextMetrics const & tm = cur.bv().textMetrics(text);
3929 ParagraphMetrics const & pm =
3930 tm.parMetrics(cur.lastpit());
3931 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3932 cur.setCurrentFont();
3935 if (sl == cur.top()) {
3936 cmd = FuncRequest(LFUN_UP);
3939 if (cur.selIsMultiCell()) {
3941 cur.pos() = cur.lastpos();
3942 cur.setCurrentFont();
3947 // case LFUN_SCREEN_DOWN: {
3948 // //if (hasSelection())
3949 // // cur.selection() = false;
3950 // col_type const col = tabular.cellColumn(cur.idx());
3951 // int const t = cur.bv().top_y() + cur.bv().height();
3952 // if (t < yo() + tabular.getHeightOfTabular()) {
3953 // cur.bv().scrollDocView(t, true);
3954 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3956 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3963 // case LFUN_SCREEN_UP: {
3964 // //if (hasSelection())
3965 // // cur.selection() = false;
3966 // col_type const col = tabular.cellColumn(cur.idx());
3967 // int const t = cur.bv().top_y() + cur.bv().height();
3969 // cur.bv().scrollDocView(t, true);
3973 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3977 // cur.par() = cur.lastpar();
3978 // cur.pos() = cur.lastpos();
3982 case LFUN_LAYOUT_TABULAR:
3983 cur.bv().showDialog("tabular");
3986 case LFUN_INSET_MODIFY:
3987 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3991 // insert file functions
3992 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3993 case LFUN_FILE_INSERT_PLAINTEXT:
3995 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3996 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3997 FileName(to_utf8(cmd.argument())));
4000 cur.recordUndoInset(INSERT_UNDO);
4001 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4002 // content has been replaced,
4003 // so cursor might be invalid
4004 cur.pos() = cur.lastpos();
4005 cur.pit() = cur.lastpit();
4006 bvcur.setCursor(cur);
4013 if (cur.selIsMultiCell()) {
4014 if (copySelection(cur)) {
4015 cur.recordUndoInset(DELETE_UNDO);
4019 cell(cur.idx())->dispatch(cur, cmd);
4022 case LFUN_SELF_INSERT:
4023 if (cur.selIsMultiCell()) {
4024 cur.recordUndoInset(DELETE_UNDO);
4027 cell(cur.idx())->dispatch(cur, cmd);
4030 case LFUN_CHAR_DELETE_BACKWARD:
4031 case LFUN_CHAR_DELETE_FORWARD:
4032 if (cur.selIsMultiCell()) {
4033 cur.recordUndoInset(DELETE_UNDO);
4036 cell(cur.idx())->dispatch(cur, cmd);
4040 if (!cur.selection())
4042 if (cur.selIsMultiCell()) {
4046 cell(cur.idx())->dispatch(cur, cmd);
4049 case LFUN_CLIPBOARD_PASTE:
4050 case LFUN_PRIMARY_SELECTION_PASTE: {
4051 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4052 theClipboard().getAsText() :
4053 theSelection().get();
4056 // pass to InsertPlaintextString, but
4057 // only if we have multi-cell content
4058 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4059 cur.recordUndoInset(INSERT_UNDO);
4060 if (insertPlaintextString(cur.bv(), clip, false)) {
4061 // content has been replaced,
4062 // so cursor might be invalid
4063 cur.pos() = cur.lastpos();
4064 cur.pit() = cur.lastpit();
4065 bvcur.setCursor(cur);
4069 // Let the cell handle normal text
4070 cell(cur.idx())->dispatch(cur, cmd);
4075 if (!tabularStackDirty()) {
4076 if (!cur.selIsMultiCell())
4077 cell(cur.idx())->dispatch(cur, cmd);
4080 if (theClipboard().isInternal() ||
4081 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4082 cur.recordUndoInset(INSERT_UNDO);
4083 pasteClipboard(cur);
4087 case LFUN_FONT_EMPH:
4088 case LFUN_FONT_BOLD:
4089 case LFUN_FONT_BOLDSYMBOL:
4090 case LFUN_FONT_ROMAN:
4091 case LFUN_FONT_NOUN:
4092 case LFUN_FONT_ITAL:
4093 case LFUN_FONT_FRAK:
4094 case LFUN_FONT_TYPEWRITER:
4095 case LFUN_FONT_SANS:
4096 case LFUN_TEXTSTYLE_APPLY:
4097 case LFUN_TEXTSTYLE_UPDATE:
4098 case LFUN_FONT_SIZE:
4099 case LFUN_FONT_UNDERLINE:
4100 case LFUN_FONT_STRIKEOUT:
4101 case LFUN_FONT_UULINE:
4102 case LFUN_FONT_UWAVE:
4104 case LFUN_WORD_CAPITALIZE:
4105 case LFUN_WORD_UPCASE:
4106 case LFUN_WORD_LOWCASE:
4107 case LFUN_CHARS_TRANSPOSE:
4108 if (cur.selIsMultiCell()) {
4111 getSelection(cur, rs, re, cs, ce);
4112 Cursor tmpcur = cur;
4113 for (row_type r = rs; r <= re; ++r) {
4114 for (col_type c = cs; c <= ce; ++c) {
4115 // cursor follows cell:
4116 tmpcur.idx() = tabular.cellIndex(r, c);
4117 // select this cell only:
4120 tmpcur.resetAnchor();
4121 tmpcur.pit() = tmpcur.lastpit();
4122 tmpcur.pos() = tmpcur.top().lastpos();
4123 tmpcur.setCursor(tmpcur);
4124 tmpcur.setSelection();
4125 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4130 cell(cur.idx())->dispatch(cur, cmd);
4134 case LFUN_INSET_SETTINGS:
4135 // relay this lfun to Inset, not to the cell.
4136 Inset::doDispatch(cur, cmd);
4140 // we try to handle this event in the insets dispatch function.
4141 cell(cur.idx())->dispatch(cur, cmd);
4147 // function sets an object as defined in func_status.h:
4148 // states OK, Unknown, Disabled, On, Off.
4149 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4150 FuncStatus & status) const
4152 switch (cmd.action()) {
4153 case LFUN_INSET_MODIFY: {
4154 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4157 string const s = cmd.getArg(1);
4158 // FIXME: We only check for the very first argument...
4159 int action = Tabular::LAST_ACTION;
4161 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4162 if (tabularFeature[i].feature == s) {
4163 action = tabularFeature[i].action;
4167 if (action == Tabular::LAST_ACTION) {
4169 status.setUnknown(true);
4173 string const argument = cmd.getLongArg(2);
4175 row_type sel_row_start = 0;
4176 row_type sel_row_end = 0;
4177 col_type sel_col_start = 0;
4178 col_type sel_col_end = 0;
4179 Tabular::ltType dummyltt;
4182 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4185 case Tabular::SET_PWIDTH:
4186 case Tabular::SET_MPWIDTH:
4187 case Tabular::SET_SPECIAL_COLUMN:
4188 case Tabular::SET_SPECIAL_MULTICOLUMN:
4189 case Tabular::SET_SPECIAL_MULTIROW:
4190 case Tabular::APPEND_ROW:
4191 case Tabular::APPEND_COLUMN:
4192 case Tabular::DELETE_ROW:
4193 case Tabular::DELETE_COLUMN:
4194 case Tabular::COPY_ROW:
4195 case Tabular::COPY_COLUMN:
4196 case Tabular::SET_TOP_SPACE:
4197 case Tabular::SET_BOTTOM_SPACE:
4198 case Tabular::SET_INTERLINE_SPACE:
4202 case Tabular::SET_DECIMAL_POINT:
4204 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4207 case Tabular::MULTICOLUMN:
4208 // If a row is set as longtable caption, it must not be allowed
4209 // to unset that this row is a multicolumn.
4210 status.setEnabled(sel_row_start == sel_row_end
4211 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4212 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4215 case Tabular::MULTIROW:
4216 // If a row is set as longtable caption, it must not be allowed
4217 // to unset that this row is a multirow.
4218 status.setEnabled(sel_col_start == sel_col_end
4219 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4220 status.setOnOff(tabular.isMultiRow(cur.idx()));
4223 case Tabular::SET_ALL_LINES:
4224 case Tabular::UNSET_ALL_LINES:
4225 case Tabular::SET_BORDER_LINES:
4226 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4229 case Tabular::SET_LINE_TOP:
4230 case Tabular::SET_LINE_BOTTOM:
4231 case Tabular::SET_LINE_LEFT:
4232 case Tabular::SET_LINE_RIGHT:
4233 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4236 case Tabular::TOGGLE_LINE_TOP:
4237 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4238 status.setOnOff(tabular.topLine(cur.idx()));
4241 case Tabular::TOGGLE_LINE_BOTTOM:
4242 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4243 status.setOnOff(tabular.bottomLine(cur.idx()));
4246 case Tabular::TOGGLE_LINE_LEFT:
4247 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4248 status.setOnOff(tabular.leftLine(cur.idx()));
4251 case Tabular::TOGGLE_LINE_RIGHT:
4252 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4253 status.setOnOff(tabular.rightLine(cur.idx()));
4256 case Tabular::M_ALIGN_LEFT:
4258 case Tabular::ALIGN_LEFT:
4259 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4260 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4263 case Tabular::M_ALIGN_RIGHT:
4265 case Tabular::ALIGN_RIGHT:
4266 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4267 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4270 case Tabular::M_ALIGN_CENTER:
4272 case Tabular::ALIGN_CENTER:
4273 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4274 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4277 case Tabular::ALIGN_BLOCK:
4278 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4279 && !tabular.isMultiRow(cur.idx()));
4280 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4283 case Tabular::ALIGN_DECIMAL:
4284 status.setEnabled(!tabular.isMultiRow(cur.idx())
4285 && !tabular.isMultiColumn(cur.idx()));
4286 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4289 case Tabular::M_VALIGN_TOP:
4291 case Tabular::VALIGN_TOP:
4292 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4293 && !tabular.isMultiRow(cur.idx()));
4295 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4298 case Tabular::M_VALIGN_BOTTOM:
4300 case Tabular::VALIGN_BOTTOM:
4301 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4302 && !tabular.isMultiRow(cur.idx()));
4304 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4307 case Tabular::M_VALIGN_MIDDLE:
4309 case Tabular::VALIGN_MIDDLE:
4310 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4311 && !tabular.isMultiRow(cur.idx()));
4313 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4316 case Tabular::SET_LONGTABULAR:
4317 status.setOnOff(tabular.is_long_tabular);
4320 case Tabular::UNSET_LONGTABULAR:
4321 status.setOnOff(!tabular.is_long_tabular);
4324 case Tabular::TOGGLE_ROTATE_TABULAR:
4325 case Tabular::SET_ROTATE_TABULAR:
4326 status.setOnOff(tabular.rotate);
4329 case Tabular::TABULAR_VALIGN_TOP:
4330 status.setOnOff(tabular.tabular_valignment
4331 == Tabular::LYX_VALIGN_TOP);
4333 case Tabular::TABULAR_VALIGN_MIDDLE:
4334 status.setOnOff(tabular.tabular_valignment
4335 == Tabular::LYX_VALIGN_MIDDLE);
4337 case Tabular::TABULAR_VALIGN_BOTTOM:
4338 status.setOnOff(tabular.tabular_valignment
4339 == Tabular::LYX_VALIGN_BOTTOM);
4342 case Tabular::LONGTABULAR_ALIGN_LEFT:
4343 status.setOnOff(tabular.longtabular_alignment
4344 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4346 case Tabular::LONGTABULAR_ALIGN_CENTER:
4347 status.setOnOff(tabular.longtabular_alignment
4348 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4350 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4351 status.setOnOff(tabular.longtabular_alignment
4352 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4355 case Tabular::UNSET_ROTATE_TABULAR:
4356 status.setOnOff(!tabular.rotate);
4359 case Tabular::TOGGLE_ROTATE_CELL:
4360 case Tabular::SET_ROTATE_CELL:
4361 status.setOnOff(!oneCellHasRotationState(false,
4362 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4365 case Tabular::UNSET_ROTATE_CELL:
4366 status.setOnOff(!oneCellHasRotationState(true,
4367 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4370 case Tabular::SET_USEBOX:
4371 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4374 // every row can only be one thing:
4375 // either a footer or header or caption
4376 case Tabular::SET_LTFIRSTHEAD:
4377 status.setEnabled(sel_row_start == sel_row_end
4378 && !tabular.ltCaption(sel_row_start));
4379 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4382 case Tabular::UNSET_LTFIRSTHEAD:
4383 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4386 case Tabular::SET_LTHEAD:
4387 status.setEnabled(sel_row_start == sel_row_end
4388 && !tabular.ltCaption(sel_row_start));
4389 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4392 case Tabular::UNSET_LTHEAD:
4393 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4396 case Tabular::SET_LTFOOT:
4397 status.setEnabled(sel_row_start == sel_row_end
4398 && !tabular.ltCaption(sel_row_start));
4399 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4402 case Tabular::UNSET_LTFOOT:
4403 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4406 case Tabular::SET_LTLASTFOOT:
4407 status.setEnabled(sel_row_start == sel_row_end
4408 && !tabular.ltCaption(sel_row_start));
4409 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4412 case Tabular::UNSET_LTLASTFOOT:
4413 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4416 case Tabular::SET_LTNEWPAGE:
4417 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4420 // only one row can be the caption
4421 // and a multirow cannot be set as caption
4422 case Tabular::TOGGLE_LTCAPTION:
4423 status.setEnabled(sel_row_start == sel_row_end
4424 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4425 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4426 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4427 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4428 && (!tabular.haveLTCaption()
4429 || tabular.ltCaption(sel_row_start))
4430 && !tabular.isMultiRow(sel_row_start));
4431 status.setOnOff(tabular.ltCaption(sel_row_start));
4434 case Tabular::SET_BOOKTABS:
4435 status.setOnOff(tabular.use_booktabs);
4438 case Tabular::UNSET_BOOKTABS:
4439 status.setOnOff(!tabular.use_booktabs);
4444 status.setEnabled(false);
4450 // These are only enabled inside tabular
4451 case LFUN_CELL_BACKWARD:
4452 case LFUN_CELL_FORWARD:
4453 status.setEnabled(true);
4456 // disable these with multiple cells selected
4457 case LFUN_INSET_INSERT:
4458 case LFUN_TABULAR_INSERT:
4459 case LFUN_FLEX_INSERT:
4460 case LFUN_FLOAT_INSERT:
4461 case LFUN_FLOAT_WIDE_INSERT:
4462 case LFUN_FOOTNOTE_INSERT:
4463 case LFUN_MARGINALNOTE_INSERT:
4464 case LFUN_MATH_INSERT:
4465 case LFUN_MATH_MODE:
4466 case LFUN_MATH_MUTATE:
4467 case LFUN_MATH_DISPLAY:
4468 case LFUN_NOTE_INSERT:
4469 case LFUN_ARGUMENT_INSERT:
4470 case LFUN_BOX_INSERT:
4471 case LFUN_BRANCH_INSERT:
4472 case LFUN_PHANTOM_INSERT:
4473 case LFUN_WRAP_INSERT:
4474 case LFUN_PREVIEW_INSERT:
4475 case LFUN_ERT_INSERT: {
4476 if (cur.selIsMultiCell()) {
4477 status.setEnabled(false);
4480 return cell(cur.idx())->getStatus(cur, cmd, status);
4483 // disable in non-fixed-width cells
4484 case LFUN_NEWLINE_INSERT:
4485 case LFUN_BREAK_PARAGRAPH: {
4486 if (tabular.getPWidth(cur.idx()).zero()) {
4487 status.setEnabled(false);
4490 return cell(cur.idx())->getStatus(cur, cmd, status);
4493 case LFUN_NEWPAGE_INSERT:
4494 status.setEnabled(false);
4498 if (tabularStackDirty() && theClipboard().isInternal()) {
4499 if (cur.selIsMultiCell()) {
4502 getSelection(cur, rs, re, cs, ce);
4503 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4504 && paste_tabular->nrows() == re - rs + 1)
4505 status.setEnabled(true);
4507 status.setEnabled(false);
4508 status.message(_("Selection size should match clipboard content."));
4511 status.setEnabled(true);
4514 return cell(cur.idx())->getStatus(cur, cmd, status);
4516 case LFUN_INSET_SETTINGS:
4517 // relay this lfun to Inset, not to the cell.
4518 return Inset::getStatus(cur, cmd, status);
4521 // we try to handle this event in the insets dispatch function.
4522 return cell(cur.idx())->getStatus(cur, cmd, status);
4528 Inset::DisplayType InsetTabular::display() const
4530 if (tabular.is_long_tabular) {
4531 switch (tabular.longtabular_alignment) {
4532 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4534 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4536 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4546 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4548 return tabular.latex(os, runparams);
4552 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4554 os << '\n'; // output table on a new line
4555 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4556 tabular.plaintext(os, runparams, dp, false, 0);
4557 return PLAINTEXT_NEWLINE;
4561 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4566 // FIXME: Why not pass a proper DocIterator here?
4568 // if the table is inside a float it doesn't need the informaltable
4569 // wrapper. Search for it.
4570 for (master = owner(); master; master = master->owner())
4571 if (master->lyxCode() == FLOAT_CODE)
4576 os << "<informaltable>";
4579 ret += tabular.docbook(os, runparams);
4581 os << "</informaltable>";
4588 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4591 // It'd be better to be able to get this from an InsetLayout, but at present
4592 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4593 xs << html::StartTag("table");
4594 docstring ret = tabular.xhtml(xs, rp);
4595 xs << html::EndTag("table");
4600 void InsetTabular::validate(LaTeXFeatures & features) const
4602 tabular.validate(features);
4604 // It'd be better to be able to get this from an InsetLayout, but at present
4605 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4606 if (features.runparams().flavor == OutputParams::HTML)
4607 features.addPreambleSnippet("<style type=\"text/css\">\n"
4608 "table { border: 1px solid black; display: inline-block; }\n"
4609 "td { border: 1px solid black; padding: 0.5ex; }\n"
4614 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4616 return tabular.cellInset(idx);
4620 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4622 return tabular.cellInset(idx);
4626 void InsetTabular::cursorPos(BufferView const & bv,
4627 CursorSlice const & sl, bool boundary, int & x, int & y) const
4629 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4631 int const row = tabular.cellRow(sl.idx());
4632 int const col = tabular.cellColumn(sl.idx());
4634 // y offset correction
4635 for (int r = 0; r < row; ++r)
4636 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4637 + tabular.interRowSpace(r + 1);
4639 y += tabular.textVOffset(sl.idx());
4641 // x offset correction
4642 for (int c = 0; c < col; ++c)
4643 x += tabular.column_info[c].width;
4645 x += tabular.textHOffset(sl.idx());
4646 x += ADD_TO_TABULAR_WIDTH;
4651 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4655 Inset const & inset = *tabular.cellInset(cell);
4656 Point o = bv.coordCache().getInsets().xy(&inset);
4657 int const xbeg = o.x_ - tabular.textHOffset(cell);
4658 int const xend = xbeg + tabular.cellWidth(cell);
4659 row_type const row = tabular.cellRow(cell);
4660 int const ybeg = o.y_ - tabular.rowAscent(row)
4661 - tabular.interRowSpace(row);
4662 int const yend = ybeg + tabular.cellHeight(cell);
4674 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4675 // << " ybeg=" << ybeg << " yend=" << yend
4676 // << " xx=" << xx << " yy=" << yy
4677 // << " dist=" << xx + yy << endl;
4682 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4684 //lyxerr << "InsetTabular::editXY: " << this << endl;
4685 cur.setSelection(false);
4687 cur.idx() = getNearestCell(cur.bv(), x, y);
4689 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4693 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4695 cur.idx() = getNearestCell(cur.bv(), x, y);
4696 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4700 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4702 idx_type idx_min = 0;
4703 int dist_min = numeric_limits<int>::max();
4704 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4705 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4706 int const d = dist(bv, i, x, y);
4717 int InsetTabular::cellXPos(idx_type const cell) const
4719 col_type col = tabular.cellColumn(cell);
4721 for (col_type c = 0; c < col; ++c)
4722 lx += tabular.column_info[c].width;
4728 void InsetTabular::resetPos(Cursor & cur) const
4730 BufferView & bv = cur.bv();
4731 int const maxwidth = bv.workWidth();
4733 int const scx_old = scx_;
4734 int const i = cur.find(this);
4739 int const X2 = maxwidth;
4740 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4741 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4742 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4745 scx_ = X1 + 20 - x1;
4747 scx_ = X2 - 20 - x2;
4752 // only update if offset changed
4753 if (scx_ != scx_old)
4754 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4758 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4760 row_type const row = tabular.cellRow(cur.idx());
4761 col_type const col = tabular.cellColumn(cur.idx());
4763 if (isRightToLeft(cur)) {
4764 if (tabular.cellColumn(cur.idx()) == 0) {
4765 if (row == tabular.nrows() - 1)
4767 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4772 cur.idx() = tabular.getLastCellInRow(row - 1);
4774 cur.idx() = tabular.cellIndex(row, col - 1);
4777 if (tabular.isLastCell(cur.idx()))
4779 if (cur.idx() == tabular.getLastCellInRow(row))
4780 cur.idx() = tabular.cellIndex(row + 1, 0);
4782 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4783 cur.idx() = tabular.cellIndex(row, colnextcell);
4787 cur.boundary(false);
4789 if (cur.selIsMultiCell()) {
4790 cur.pit() = cur.lastpit();
4791 cur.pos() = cur.lastpos();
4799 // in visual mode, place cursor at extreme left or right
4801 switch(entry_from) {
4803 case ENTRY_DIRECTION_RIGHT:
4804 cur.posVisToRowExtremity(false /* !left */);
4806 case ENTRY_DIRECTION_LEFT:
4807 cur.posVisToRowExtremity(true /* left */);
4809 case ENTRY_DIRECTION_IGNORE:
4810 // nothing to do in this case
4814 cur.setCurrentFont();
4819 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4821 row_type const row = tabular.cellRow(cur.idx());
4822 col_type const col = tabular.cellColumn(cur.idx());
4824 if (isRightToLeft(cur)) {
4825 if (cur.idx() == tabular.getLastCellInRow(row)) {
4828 cur.idx() = tabular.getFirstCellInRow(row);
4829 cur.idx() = tabular.cellAbove(cur.idx());
4831 if (tabular.isLastCell(cur.idx()))
4833 if (cur.idx() == tabular.getLastCellInRow(row))
4834 cur.idx() = tabular.cellIndex(row + 1, 0);
4836 cur.idx() = tabular.cellIndex(row, col + 1);
4839 if (cur.idx() == 0) // first cell
4842 cur.idx() = tabular.getLastCellInRow(row - 1);
4844 cur.idx() = tabular.cellIndex(row, col - 1);
4847 if (cur.selIsMultiCell()) {
4848 cur.pit() = cur.lastpit();
4849 cur.pos() = cur.lastpos();
4854 cur.pit() = cur.lastpit();
4855 cur.pos() = cur.lastpos();
4857 // in visual mode, place cursor at extreme left or right
4859 switch(entry_from) {
4861 case ENTRY_DIRECTION_RIGHT:
4862 cur.posVisToRowExtremity(false /* !left */);
4864 case ENTRY_DIRECTION_LEFT:
4865 cur.posVisToRowExtremity(true /* left */);
4867 case ENTRY_DIRECTION_IGNORE:
4868 // nothing to do in this case
4872 cur.setCurrentFont();
4877 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4879 istringstream is(argument);
4882 if (insetCode(s) != TABULAR_CODE)
4886 size_t safe_guard = 0;
4891 if (safe_guard > 1000) {
4892 LYXERR0("parameter max count reached!");
4896 Tabular::Feature action = Tabular::LAST_ACTION;
4899 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4900 if (s != tabularFeature[i].feature)
4903 action = tabularFeature[i].action;
4906 if (action == Tabular::LAST_ACTION) {
4907 LYXERR0("Feature not found " << s);
4911 if (tabularFeature[i].need_value)
4913 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4914 tabularFeatures(cur, action, val);
4920 static void checkLongtableSpecial(Tabular::ltType & ltt,
4921 string const & special, bool & flag)
4923 if (special == "dl_above") {
4926 } else if (special == "dl_below") {
4927 ltt.bottomDL = flag;
4929 } else if (special == "empty") {
4939 bool InsetTabular::oneCellHasRotationState(bool rotated,
4940 row_type row_start, row_type row_end,
4941 col_type col_start, col_type col_end) const
4943 for (row_type r = row_start; r <= row_end; ++r)
4944 for (col_type c = col_start; c <= col_end; ++c)
4945 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4951 void InsetTabular::tabularFeatures(Cursor & cur,
4952 Tabular::Feature feature, string const & value)
4954 col_type sel_col_start;
4955 col_type sel_col_end;
4956 row_type sel_row_start;
4957 row_type sel_row_end;
4958 bool setLines = false;
4959 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4960 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4964 case Tabular::M_ALIGN_LEFT:
4965 case Tabular::ALIGN_LEFT:
4966 setAlign = LYX_ALIGN_LEFT;
4969 case Tabular::M_ALIGN_RIGHT:
4970 case Tabular::ALIGN_RIGHT:
4971 setAlign = LYX_ALIGN_RIGHT;
4974 case Tabular::M_ALIGN_CENTER:
4975 case Tabular::ALIGN_CENTER:
4976 setAlign = LYX_ALIGN_CENTER;
4979 case Tabular::ALIGN_BLOCK:
4980 setAlign = LYX_ALIGN_BLOCK;
4983 case Tabular::ALIGN_DECIMAL:
4984 setAlign = LYX_ALIGN_DECIMAL;
4987 case Tabular::M_VALIGN_TOP:
4988 case Tabular::VALIGN_TOP:
4989 setVAlign = Tabular::LYX_VALIGN_TOP;
4992 case Tabular::M_VALIGN_BOTTOM:
4993 case Tabular::VALIGN_BOTTOM:
4994 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4997 case Tabular::M_VALIGN_MIDDLE:
4998 case Tabular::VALIGN_MIDDLE:
4999 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5006 cur.recordUndoInset(ATOMIC_UNDO);
5008 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5009 row_type const row = tabular.cellRow(cur.idx());
5010 col_type const column = tabular.cellColumn(cur.idx());
5012 Tabular::ltType ltt;
5016 case Tabular::SET_PWIDTH: {
5017 Length const len(value);
5018 tabular.setColumnPWidth(cur, cur.idx(), len);
5020 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5021 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5025 case Tabular::SET_MPWIDTH:
5026 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5029 case Tabular::SET_SPECIAL_COLUMN:
5030 case Tabular::SET_SPECIAL_MULTICOLUMN:
5031 if (value == "none")
5032 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5034 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5037 case Tabular::SET_SPECIAL_MULTIROW:
5041 case Tabular::APPEND_ROW:
5042 // append the row into the tabular
5043 tabular.appendRow(cur.idx());
5046 case Tabular::APPEND_COLUMN:
5047 // append the column into the tabular
5048 tabular.appendColumn(cur.idx());
5049 cur.idx() = tabular.cellIndex(row, column);
5052 case Tabular::DELETE_ROW:
5053 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5054 tabular.deleteRow(sel_row_start);
5055 if (sel_row_start >= tabular.nrows())
5057 cur.idx() = tabular.cellIndex(sel_row_start, column);
5060 cur.setSelection(false);
5063 case Tabular::DELETE_COLUMN:
5064 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5065 tabular.deleteColumn(sel_col_start);
5066 if (sel_col_start >= tabular.ncols())
5068 cur.idx() = tabular.cellIndex(row, sel_col_start);
5071 cur.setSelection(false);
5074 case Tabular::COPY_ROW:
5075 tabular.copyRow(row);
5078 case Tabular::COPY_COLUMN:
5079 tabular.copyColumn(column);
5080 cur.idx() = tabular.cellIndex(row, column);
5083 case Tabular::SET_LINE_TOP:
5084 case Tabular::TOGGLE_LINE_TOP: {
5085 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5086 ? (value == "true") : !tabular.topLine(cur.idx());
5087 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5088 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5089 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5093 case Tabular::SET_LINE_BOTTOM:
5094 case Tabular::TOGGLE_LINE_BOTTOM: {
5095 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5096 ? (value == "true") : !tabular.bottomLine(cur.idx());
5097 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5098 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5099 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5103 case Tabular::SET_LINE_LEFT:
5104 case Tabular::TOGGLE_LINE_LEFT: {
5105 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5106 ? (value == "true") : !tabular.leftLine(cur.idx());
5107 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5108 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5109 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5113 case Tabular::SET_LINE_RIGHT:
5114 case Tabular::TOGGLE_LINE_RIGHT: {
5115 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5116 ? (value == "true") : !tabular.rightLine(cur.idx());
5117 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5118 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5119 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5123 case Tabular::M_ALIGN_LEFT:
5124 case Tabular::M_ALIGN_RIGHT:
5125 case Tabular::M_ALIGN_CENTER:
5127 case Tabular::ALIGN_LEFT:
5128 case Tabular::ALIGN_RIGHT:
5129 case Tabular::ALIGN_CENTER:
5130 case Tabular::ALIGN_BLOCK:
5131 case Tabular::ALIGN_DECIMAL:
5132 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5133 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5134 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5137 case Tabular::M_VALIGN_TOP:
5138 case Tabular::M_VALIGN_BOTTOM:
5139 case Tabular::M_VALIGN_MIDDLE:
5141 case Tabular::VALIGN_TOP:
5142 case Tabular::VALIGN_BOTTOM:
5143 case Tabular::VALIGN_MIDDLE:
5144 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5145 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5146 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5149 case Tabular::MULTICOLUMN: {
5150 if (!cur.selection()) {
5151 // just multicol for one single cell
5152 // check whether we are completely in a multicol
5153 if (tabular.isMultiColumn(cur.idx()))
5154 tabular.unsetMultiColumn(cur.idx());
5156 tabular.setMultiColumn(cur.idx(), 1);
5159 // we have a selection so this means we just add all this
5160 // cells to form a multicolumn cell
5161 idx_type const s_start = cur.selBegin().idx();
5162 row_type const col_start = tabular.cellColumn(s_start);
5163 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5164 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5167 cur.setSelection(false);
5171 case Tabular::MULTIROW: {
5172 if (!cur.selection()) {
5173 // just multirow for one single cell
5174 // check whether we are completely in a multirow
5175 if (tabular.isMultiRow(cur.idx()))
5176 tabular.unsetMultiRow(cur.idx());
5178 tabular.setMultiRow(cur.idx(), 1);
5181 // we have a selection so this means we just add all this
5182 // cells to form a multirow cell
5183 idx_type const s_start = cur.selBegin().idx();
5184 row_type const row_start = tabular.cellRow(s_start);
5185 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5186 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5189 cur.setSelection(false);
5193 case Tabular::SET_ALL_LINES:
5195 case Tabular::UNSET_ALL_LINES:
5196 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5197 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5198 idx_type const cell = tabular.cellIndex(r, c);
5199 tabular.setTopLine(cell, setLines);
5200 tabular.setBottomLine(cell, setLines);
5201 tabular.setRightLine(cell, setLines);
5202 tabular.setLeftLine(cell, setLines);
5206 case Tabular::SET_BORDER_LINES:
5207 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5208 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5209 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5211 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5212 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5213 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5217 case Tabular::SET_LONGTABULAR:
5218 tabular.is_long_tabular = true;
5221 case Tabular::UNSET_LONGTABULAR:
5222 for (row_type r = 0; r < tabular.nrows(); ++r) {
5223 if (tabular.ltCaption(r)) {
5224 cur.idx() = tabular.cellIndex(r, 0);
5227 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5230 tabular.is_long_tabular = false;
5233 case Tabular::SET_ROTATE_TABULAR:
5234 tabular.rotate = true;
5237 case Tabular::UNSET_ROTATE_TABULAR:
5238 tabular.rotate = false;
5241 case Tabular::TOGGLE_ROTATE_TABULAR:
5242 tabular.rotate = !tabular.rotate;
5245 case Tabular::TABULAR_VALIGN_TOP:
5246 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5249 case Tabular::TABULAR_VALIGN_MIDDLE:
5250 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5253 case Tabular::TABULAR_VALIGN_BOTTOM:
5254 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5257 case Tabular::LONGTABULAR_ALIGN_LEFT:
5258 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5261 case Tabular::LONGTABULAR_ALIGN_CENTER:
5262 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5265 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5266 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5271 case Tabular::SET_ROTATE_CELL:
5272 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5273 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5274 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5277 case Tabular::UNSET_ROTATE_CELL:
5278 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5279 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5280 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5283 case Tabular::TOGGLE_ROTATE_CELL:
5285 bool oneNotRotated = oneCellHasRotationState(false,
5286 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5288 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5289 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5290 tabular.setRotateCell(tabular.cellIndex(r, c),
5295 case Tabular::SET_USEBOX: {
5296 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5297 if (val == tabular.getUsebox(cur.idx()))
5298 val = Tabular::BOX_NONE;
5299 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5300 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5301 tabular.setUsebox(tabular.cellIndex(r, c), val);
5305 case Tabular::UNSET_LTFIRSTHEAD:
5307 case Tabular::SET_LTFIRSTHEAD:
5308 tabular.getRowOfLTFirstHead(row, ltt);
5309 checkLongtableSpecial(ltt, value, flag);
5310 tabular.setLTHead(row, flag, ltt, true);
5313 case Tabular::UNSET_LTHEAD:
5315 case Tabular::SET_LTHEAD:
5316 tabular.getRowOfLTHead(row, ltt);
5317 checkLongtableSpecial(ltt, value, flag);
5318 tabular.setLTHead(row, flag, ltt, false);
5321 case Tabular::UNSET_LTFOOT:
5323 case Tabular::SET_LTFOOT:
5324 tabular.getRowOfLTFoot(row, ltt);
5325 checkLongtableSpecial(ltt, value, flag);
5326 tabular.setLTFoot(row, flag, ltt, false);
5329 case Tabular::UNSET_LTLASTFOOT:
5331 case Tabular::SET_LTLASTFOOT:
5332 tabular.getRowOfLTLastFoot(row, ltt);
5333 checkLongtableSpecial(ltt, value, flag);
5334 tabular.setLTFoot(row, flag, ltt, true);
5337 case Tabular::SET_LTNEWPAGE:
5338 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5341 case Tabular::TOGGLE_LTCAPTION: {
5342 bool const set = !tabular.ltCaption(row);
5343 cur.idx() = tabular.setLTCaption(row, set);
5346 cur.setSelection(false);
5349 // When a row is set as caption, then also insert
5350 // a caption. Otherwise the LaTeX output is broken.
5351 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5352 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5354 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5355 if (lyx::getStatus(fr).enabled())
5361 case Tabular::SET_BOOKTABS:
5362 tabular.use_booktabs = true;
5365 case Tabular::UNSET_BOOKTABS:
5366 tabular.use_booktabs = false;
5369 case Tabular::SET_TOP_SPACE: {
5371 if (value == "default")
5372 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5373 tabular.row_info[r].top_space_default = true;
5374 else if (value == "none")
5375 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5376 tabular.row_info[r].top_space_default = false;
5377 tabular.row_info[r].top_space = len;
5379 else if (isValidLength(value, &len))
5380 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5381 tabular.row_info[r].top_space_default = false;
5382 tabular.row_info[r].top_space = len;
5387 case Tabular::SET_BOTTOM_SPACE: {
5389 if (value == "default")
5390 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5391 tabular.row_info[r].bottom_space_default = true;
5392 else if (value == "none")
5393 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5394 tabular.row_info[r].bottom_space_default = false;
5395 tabular.row_info[r].bottom_space = len;
5397 else if (isValidLength(value, &len))
5398 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5399 tabular.row_info[r].bottom_space_default = false;
5400 tabular.row_info[r].bottom_space = len;
5405 case Tabular::SET_INTERLINE_SPACE: {
5407 if (value == "default")
5408 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5409 tabular.row_info[r].interline_space_default = true;
5410 else if (value == "none")
5411 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5412 tabular.row_info[r].interline_space_default = false;
5413 tabular.row_info[r].interline_space = len;
5415 else if (isValidLength(value, &len))
5416 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5417 tabular.row_info[r].interline_space_default = false;
5418 tabular.row_info[r].interline_space = len;
5423 case Tabular::SET_DECIMAL_POINT:
5424 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5425 tabular.column_info[c].decimal_point = from_utf8(value);
5428 // dummy stuff just to avoid warnings
5429 case Tabular::LAST_ACTION:
5435 bool InsetTabular::copySelection(Cursor & cur)
5437 if (!cur.selection())
5442 getSelection(cur, rs, re, cs, ce);
5444 paste_tabular.reset(new Tabular(tabular));
5446 for (row_type r = 0; r < rs; ++r)
5447 paste_tabular->deleteRow(0);
5449 row_type const rows = re - rs + 1;
5450 while (paste_tabular->nrows() > rows)
5451 paste_tabular->deleteRow(rows);
5453 for (col_type c = 0; c < cs; ++c)
5454 paste_tabular->deleteColumn(0);
5456 col_type const columns = ce - cs + 1;
5457 while (paste_tabular->ncols() > columns)
5458 paste_tabular->deleteColumn(columns);
5460 paste_tabular->setBuffer(tabular.buffer());
5462 odocstringstream os;
5463 OutputParams const runparams(0);
5464 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5465 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5466 cap::copySelection(cur, os.str());
5468 // mark tabular stack dirty
5469 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5470 // when we (hopefully) have a one-for-all paste mechanism.
5471 // This must be called after cap::copySelection.
5472 dirtyTabularStack(true);
5478 bool InsetTabular::pasteClipboard(Cursor & cur)
5482 col_type actcol = tabular.cellColumn(cur.idx());
5483 row_type actrow = tabular.cellRow(cur.idx());
5485 if (cur.selIsMultiCell()) {
5488 getSelection(cur, actrow, re, actcol, ce);
5491 for (row_type r1 = 0, r2 = actrow;
5492 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5494 for (col_type c1 = 0, c2 = actcol;
5495 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5497 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5498 tabular.isPartOfMultiColumn(r2, c2))
5500 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5504 if (tabular.isPartOfMultiColumn(r2, c2)) {
5508 shared_ptr<InsetTableCell> inset(
5509 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5510 tabular.setCellInset(r2, c2, inset);
5511 // FIXME?: why do we need to do this explicitly? (EL)
5512 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5514 // FIXME: change tracking (MG)
5515 inset->setChange(Change(buffer().params().trackChanges ?
5516 Change::INSERTED : Change::UNCHANGED));
5524 void InsetTabular::cutSelection(Cursor & cur)
5526 if (!cur.selection())
5531 getSelection(cur, rs, re, cs, ce);
5532 for (row_type r = rs; r <= re; ++r) {
5533 for (col_type c = cs; c <= ce; ++c) {
5534 shared_ptr<InsetTableCell> t
5535 = cell(tabular.cellIndex(r, c));
5536 if (buffer().params().trackChanges)
5537 // FIXME: Change tracking (MG)
5538 t->setChange(Change(Change::DELETED));
5544 // cursor position might be invalid now
5545 if (cur.pit() > cur.lastpit())
5546 cur.pit() = cur.lastpit();
5547 if (cur.pos() > cur.lastpos())
5548 cur.pos() = cur.lastpos();
5549 cur.clearSelection();
5553 bool InsetTabular::isRightToLeft(Cursor & cur) const
5555 LASSERT(cur.depth() > 1, /**/);
5556 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5557 pos_type const parentpos = cur[cur.depth() - 2].pos();
5558 return parentpar.getFontSettings(buffer().params(),
5559 parentpos).language()->rightToLeft();
5563 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5566 LASSERT(stidx <= enidx, return docstring());
5568 col_type const col1 = tabular.cellColumn(stidx);
5569 col_type const col2 = tabular.cellColumn(enidx);
5570 row_type const row1 = tabular.cellRow(stidx);
5571 row_type const row2 = tabular.cellRow(enidx);
5573 for (col_type col = col1; col <= col2; col++)
5574 for (row_type row = row1; row <= row2; row++) {
5579 retval += tabular.cellInset(row, col)->asString(intoInsets);
5585 void InsetTabular::getSelection(Cursor & cur,
5586 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5588 CursorSlice const & beg = cur.selBegin();
5589 CursorSlice const & end = cur.selEnd();
5590 cs = tabular.cellColumn(beg.idx());
5591 ce = tabular.cellColumn(end.idx());
5595 rs = tabular.cellRow(beg.idx());
5596 re = tabular.cellRow(end.idx());
5602 Text * InsetTabular::getText(int idx) const
5604 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5608 void InsetTabular::setChange(Change const & change)
5610 for (idx_type idx = 0; idx < nargs(); ++idx)
5611 cell(idx)->setChange(change);
5615 void InsetTabular::acceptChanges()
5617 for (idx_type idx = 0; idx < nargs(); ++idx)
5618 cell(idx)->acceptChanges();
5622 void InsetTabular::rejectChanges()
5624 for (idx_type idx = 0; idx < nargs(); ++idx)
5625 cell(idx)->rejectChanges();
5629 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5631 return tabular.getPWidth(cell).zero();
5635 bool InsetTabular::forcePlainLayout(idx_type cell) const
5637 return !tabular.getPWidth(cell).zero();
5641 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5644 if (buf.length() <= 0)
5649 col_type maxCols = 1;
5650 size_t const len = buf.length();
5654 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5662 maxCols = max(cols, maxCols);
5668 maxCols = max(cols, maxCols);
5674 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5675 loctab = paste_tabular.get();
5677 dirtyTabularStack(true);
5680 cell = bv.cursor().idx();
5681 ocol = tabular.cellColumn(cell);
5682 row = tabular.cellRow(cell);
5686 idx_type const cells = loctab->numberofcells;
5689 rows = loctab->nrows();
5690 col_type const columns = loctab->ncols();
5692 while (cell < cells && p < len && row < rows &&
5693 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5699 // we can only set this if we are not too far right
5700 if (cols < columns) {
5701 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5702 Font const font = bv.textMetrics(&inset->text()).
5704 inset->setText(buf.substr(op, p - op), font,
5705 buffer().params().trackChanges);
5711 // we can only set this if we are not too far right
5712 if (cols < columns) {
5713 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5714 Font const font = bv.textMetrics(&inset->text()).
5716 inset->setText(buf.substr(op, p - op), font,
5717 buffer().params().trackChanges);
5722 cell = loctab->cellIndex(row, cols);
5728 // check for the last cell if there is no trailing '\n'
5729 if (cell < cells && op < len) {
5730 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5731 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5732 inset->setText(buf.substr(op, len - op), font,
5733 buffer().params().trackChanges);
5739 void InsetTabular::addPreview(DocIterator const & inset_pos,
5740 PreviewLoader & loader) const
5742 DocIterator cell_pos = inset_pos;
5744 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5745 for (row_type r = 0; r < tabular.nrows(); ++r) {
5746 for (col_type c = 0; c < tabular.ncols(); ++c) {
5747 cell_pos.top().idx() = tabular.cellIndex(r, c);
5748 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5754 bool InsetTabular::completionSupported(Cursor const & cur) const
5756 Cursor const & bvCur = cur.bv().cursor();
5757 if (&bvCur.inset() != this)
5759 return cur.text()->completionSupported(cur);
5763 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5765 return completionSupported(cur);
5769 bool InsetTabular::automaticInlineCompletion() const
5771 return lyxrc.completion_inline_text;
5775 bool InsetTabular::automaticPopupCompletion() const
5777 return lyxrc.completion_popup_text;
5781 bool InsetTabular::showCompletionCursor() const
5783 return lyxrc.completion_cursor_text;
5787 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5789 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5793 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5795 if (!completionSupported(cur))
5797 return cur.text()->completionPrefix(cur);
5801 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5803 if (!completionSupported(cur))
5806 return cur.text()->insertCompletion(cur, s, finished);
5810 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5811 Dimension & dim) const
5813 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5814 tm.completionPosAndDim(cur, x, y, dim);
5818 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5820 istringstream data(in);
5822 lex.setStream(data);
5829 if (!lex || token != "tabular") {
5830 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5834 // This is part of the inset proper that is usually swallowed
5835 // by Buffer::readInset
5837 if (!lex || token != "Tabular") {
5838 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5846 string InsetTabular::params2string(InsetTabular const & inset)
5849 data << "tabular" << ' ';
5851 data << "\\end_inset\n";