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);
529 tail.paragraphs().front().eraseChars(0,
530 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::columnWidth(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::setCellWidth(idx_type cell, int new_width)
1012 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
1013 + interColumnSpace(cell);
1017 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1020 col_type const col = cellColumn(cell);
1021 if (onlycolumn || !isMultiColumn(cell)) {
1022 column_info[col].alignment = align;
1023 docstring & dpoint = column_info[col].decimal_point;
1024 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1025 dpoint = from_utf8(lyxrc.default_decimal_point);
1027 cellInfo(cell).alignment = align;
1028 cellInset(cell).get()->setContentAlignment(align);
1033 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1036 if (!isMultiColumn(cell) || onlycolumn)
1037 column_info[cellColumn(cell)].valignment = align;
1039 cellInfo(cell).valignment = align;
1046 * Allow line and paragraph breaks for fixed width cells or disallow them,
1047 * merge cell paragraphs and reset layout to standard for variable width
1050 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1052 inset->setAutoBreakRows(fixedWidth);
1053 inset->toggleFixedWidth(fixedWidth);
1057 // merge all paragraphs to one
1058 BufferParams const & bp = cur.bv().buffer().params();
1059 while (inset->paragraphs().size() > 1)
1060 mergeParagraph(bp, inset->paragraphs(), 0);
1064 // undo information has already been recorded
1065 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1066 bp.documentClass().plainLayoutName());
1073 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1074 Length const & width)
1076 col_type const c = cellColumn(cell);
1078 column_info[c].p_width = width;
1079 // reset the vertical alignment to top if the fixed with
1080 // is removed or zero because only fixed width columns can
1081 // have a vertical alignment
1082 if (column_info[c].p_width.zero())
1083 column_info[c].valignment = LYX_VALIGN_TOP;
1084 for (row_type r = 0; r < nrows(); ++r) {
1085 idx_type const cell = cellIndex(r, c);
1086 // because of multicolumns
1087 toggleFixedWidth(cur, cellInset(cell).get(),
1088 !getPWidth(cell).zero());
1090 // cur paragraph can become invalid after paragraphs were merged
1091 if (cur.pit() > cur.lastpit())
1092 cur.pit() = cur.lastpit();
1093 // cur position can become invalid after newlines were removed
1094 if (cur.pos() > cur.lastpos())
1095 cur.pos() = cur.lastpos();
1099 bool Tabular::setFixedWidth(row_type r, col_type c)
1101 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1102 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1103 || (multicol && !cell_info[r][c].p_width.zero());
1104 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1109 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1110 Length const & width)
1112 if (!isMultiColumn(cell))
1115 cellInfo(cell).p_width = width;
1116 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1117 // cur paragraph can become invalid after paragraphs were merged
1118 if (cur.pit() > cur.lastpit())
1119 cur.pit() = cur.lastpit();
1120 // cur position can become invalid after newlines were removed
1121 if (cur.pos() > cur.lastpos())
1122 cur.pos() = cur.lastpos();
1127 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1128 Tabular::Feature what)
1130 if (what == SET_SPECIAL_MULTICOLUMN)
1131 cellInfo(cell).align_special = special;
1133 column_info[cellColumn(cell)].align_special = special;
1137 void Tabular::setTopLine(idx_type i, bool line)
1139 cellInfo(i).top_line = line;
1143 void Tabular::setBottomLine(idx_type i, bool line)
1145 cellInfo(i).bottom_line = line;
1149 void Tabular::setLeftLine(idx_type cell, bool line)
1151 cellInfo(cell).left_line = line;
1155 void Tabular::setRightLine(idx_type cell, bool line)
1157 cellInfo(cell).right_line = line;
1160 bool Tabular::rowTopLine(row_type r) const
1162 bool all_rows_set = true;
1163 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1164 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1165 return all_rows_set;
1169 bool Tabular::rowBottomLine(row_type r) const
1171 bool all_rows_set = true;
1172 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1173 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1174 return all_rows_set;
1178 bool Tabular::columnLeftLine(col_type c) const
1185 for (row_type r = 0; r < nrows(); ++r) {
1186 idx_type const i = cellIndex(r, c);
1187 if (c == cellColumn(i)) {
1189 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1190 if (cellInfo(i).left_line || right)
1194 return 2 * nrows_left >= total;
1198 bool Tabular::columnRightLine(col_type c) const
1203 int nrows_right = 0;
1205 for (row_type r = 0; r < nrows(); ++r) {
1206 idx_type i = cellIndex(r, c);
1207 if (c == cellColumn(i) + columnSpan(i) - 1) {
1209 bool left = (c + 1 < ncols()
1210 && cellInfo(cellIndex(r, c + 1)).left_line)
1211 || c + 1 == ncols();
1212 if (cellInfo(i).right_line && left)
1216 return 2 * nrows_right >= total;
1220 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1222 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1223 return cellInfo(cell).alignment;
1225 return column_info[cellColumn(cell)].alignment;
1230 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1232 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1233 return cellInfo(cell).valignment;
1234 return column_info[cellColumn(cell)].valignment;
1238 Length const Tabular::getPWidth(idx_type cell) const
1240 if (isMultiColumn(cell))
1241 return cellInfo(cell).p_width;
1242 return column_info[cellColumn(cell)].p_width;
1246 int Tabular::cellWidth(idx_type cell) const
1248 return cellInfo(cell).width;
1252 int Tabular::textHOffset(idx_type cell) const
1254 // the LaTeX Way :-(
1255 int x = WIDTH_OF_LINE;
1257 switch (getAlignment(cell)) {
1258 case LYX_ALIGN_CENTER:
1259 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1261 case LYX_ALIGN_RIGHT:
1262 x += columnWidth(cell) - cellWidth(cell);
1263 // + interColumnSpace(cell);
1265 case LYX_ALIGN_DECIMAL: {
1266 // we center when no decimal point
1267 if (cellInfo(cell).decimal_width == 0) {
1268 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1271 col_type const c = cellColumn(cell);
1272 int max_dhoffset = 0;
1273 for(row_type r = 0; r < row_info.size() ; ++r) {
1274 idx_type const i = cellIndex(r, c);
1275 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1276 && cellInfo(i).decimal_width != 0)
1277 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1279 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1282 // LYX_ALIGN_LEFT: nothing :-)
1290 int Tabular::textVOffset(idx_type cell) const
1292 int voffset = cellInfo(cell).voffset;
1293 if (isMultiRow(cell)) {
1294 row_type const row = cellRow(cell);
1295 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1301 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1304 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1306 return cell_info[row][c].cellno;
1310 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1312 col_type c = ncols() - 1;
1313 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1314 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1316 return cell_info[row][c].cellno;
1320 Tabular::row_type Tabular::cellRow(idx_type cell) const
1322 if (cell >= numberofcells)
1326 return rowofcell[cell];
1330 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1332 if (cell >= numberofcells)
1336 return columnofcell[cell];
1340 void Tabular::write(ostream & os) const
1344 << write_attribute("version", 3)
1345 << write_attribute("rows", nrows())
1346 << write_attribute("columns", ncols())
1348 // global longtable options
1350 << write_attribute("rotate", rotate)
1351 << write_attribute("booktabs", use_booktabs)
1352 << write_attribute("islongtable", is_long_tabular)
1353 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1354 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1355 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1356 << write_attribute("headTopDL", endhead.topDL)
1357 << write_attribute("headBottomDL", endhead.bottomDL)
1358 << write_attribute("footTopDL", endfoot.topDL)
1359 << write_attribute("footBottomDL", endfoot.bottomDL)
1360 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1361 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1362 << write_attribute("lastFootEmpty", endlastfoot.empty);
1363 // longtables cannot be aligned vertically
1364 if (!is_long_tabular)
1365 os << write_attribute("tabularvalignment", tabular_valignment);
1366 if (is_long_tabular)
1367 os << write_attribute("longtabularalignment",
1368 longtabular_alignment);
1370 for (col_type c = 0; c < ncols(); ++c) {
1372 << write_attribute("alignment", column_info[c].alignment);
1373 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1374 os << write_attribute("decimal_point", column_info[c].decimal_point);
1375 os << write_attribute("valignment", column_info[c].valignment)
1376 << write_attribute("width", column_info[c].p_width.asString())
1377 << write_attribute("special", column_info[c].align_special)
1380 for (row_type r = 0; r < nrows(); ++r) {
1381 static const string def("default");
1383 if (row_info[r].top_space_default)
1384 os << write_attribute("topspace", def);
1386 os << write_attribute("topspace", row_info[r].top_space);
1387 if (row_info[r].bottom_space_default)
1388 os << write_attribute("bottomspace", def);
1390 os << write_attribute("bottomspace", row_info[r].bottom_space);
1391 if (row_info[r].interline_space_default)
1392 os << write_attribute("interlinespace", def);
1394 os << write_attribute("interlinespace", row_info[r].interline_space);
1395 os << write_attribute("endhead", row_info[r].endhead)
1396 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1397 << write_attribute("endfoot", row_info[r].endfoot)
1398 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1399 << write_attribute("newpage", row_info[r].newpage)
1400 << write_attribute("caption", row_info[r].caption)
1402 for (col_type c = 0; c < ncols(); ++c) {
1404 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1405 << write_attribute("multirow", cell_info[r][c].multirow)
1406 << write_attribute("alignment", cell_info[r][c].alignment)
1407 << write_attribute("valignment", cell_info[r][c].valignment)
1408 << write_attribute("topline", cell_info[r][c].top_line)
1409 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1410 << write_attribute("leftline", cell_info[r][c].left_line)
1411 << write_attribute("rightline", cell_info[r][c].right_line)
1412 << write_attribute("rotate", cell_info[r][c].rotate)
1413 << write_attribute("usebox", cell_info[r][c].usebox)
1414 << write_attribute("width", cell_info[r][c].p_width)
1415 << write_attribute("special", cell_info[r][c].align_special)
1417 os << "\\begin_inset ";
1418 cell_info[r][c].inset->write(os);
1419 os << "\n\\end_inset\n"
1424 os << "</lyxtabular>\n";
1428 void Tabular::read(Lexer & lex)
1431 istream & is = lex.getStream();
1433 l_getline(is, line);
1434 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1435 LASSERT(false, /**/);
1440 if (!getTokenValue(line, "version", version))
1442 LASSERT(version >= 2, /**/);
1445 if (!getTokenValue(line, "rows", rows_arg))
1448 if (!getTokenValue(line, "columns", columns_arg))
1450 init(buffer_, rows_arg, columns_arg);
1451 l_getline(is, line);
1452 if (!prefixIs(line, "<features")) {
1453 lyxerr << "Wrong tabular format (expected <features ...> got"
1454 << line << ')' << endl;
1457 getTokenValue(line, "rotate", rotate);
1458 getTokenValue(line, "booktabs", use_booktabs);
1459 getTokenValue(line, "islongtable", is_long_tabular);
1460 getTokenValue(line, "tabularvalignment", tabular_valignment);
1461 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1462 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1463 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1464 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1465 getTokenValue(line, "headTopDL", endhead.topDL);
1466 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1467 getTokenValue(line, "footTopDL", endfoot.topDL);
1468 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1469 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1470 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1471 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1473 for (col_type c = 0; c < ncols(); ++c) {
1475 if (!prefixIs(line,"<column")) {
1476 lyxerr << "Wrong tabular format (expected <column ...> got"
1477 << line << ')' << endl;
1480 getTokenValue(line, "alignment", column_info[c].alignment);
1481 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1482 getTokenValue(line, "valignment", column_info[c].valignment);
1483 getTokenValue(line, "width", column_info[c].p_width);
1484 getTokenValue(line, "special", column_info[c].align_special);
1487 for (row_type i = 0; i < nrows(); ++i) {
1488 l_getline(is, line);
1489 if (!prefixIs(line, "<row")) {
1490 lyxerr << "Wrong tabular format (expected <row ...> got"
1491 << line << ')' << endl;
1494 getTokenValue(line, "topspace", row_info[i].top_space,
1495 row_info[i].top_space_default);
1496 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1497 row_info[i].bottom_space_default);
1498 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1499 row_info[i].interline_space_default);
1500 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1501 getTokenValue(line, "endhead", row_info[i].endhead);
1502 getTokenValue(line, "endfoot", row_info[i].endfoot);
1503 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1504 getTokenValue(line, "newpage", row_info[i].newpage);
1505 getTokenValue(line, "caption", row_info[i].caption);
1506 for (col_type j = 0; j < ncols(); ++j) {
1507 l_getline(is, line);
1508 if (!prefixIs(line, "<cell")) {
1509 lyxerr << "Wrong tabular format (expected <cell ...> got"
1510 << line << ')' << endl;
1513 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1514 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1515 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1516 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1517 getTokenValue(line, "topline", cell_info[i][j].top_line);
1518 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1519 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1520 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1521 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1522 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1523 getTokenValue(line, "width", cell_info[i][j].p_width);
1525 getTokenValue(line, "special", cell_info[i][j].align_special);
1526 l_getline(is, line);
1527 if (prefixIs(line, "\\begin_inset")) {
1528 cell_info[i][j].inset->setBuffer(*buffer_);
1529 cell_info[i][j].inset->read(lex);
1530 l_getline(is, line);
1532 if (!prefixIs(line, "</cell>")) {
1533 lyxerr << "Wrong tabular format (expected </cell> got"
1534 << line << ')' << endl;
1538 l_getline(is, line);
1539 if (!prefixIs(line, "</row>")) {
1540 lyxerr << "Wrong tabular format (expected </row> got"
1541 << line << ')' << endl;
1545 while (!prefixIs(line, "</lyxtabular>")) {
1546 l_getline(is, line);
1552 bool Tabular::isMultiColumn(idx_type cell) const
1554 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1555 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1559 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1561 return cell_info[cellRow(cell)][cellColumn(cell)];
1565 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1567 idx_type const col = cellColumn(cell);
1568 idx_type const row = cellRow(cell);
1569 for (idx_type i = 0; i < number; ++i)
1570 unsetMultiRow(cellIndex(row, col + i));
1572 // unsetting of multirow may have invalidated cell index
1573 cell = cellIndex(row, col);
1574 CellData & cs = cellInfo(cell);
1575 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1576 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1577 cs.alignment = column_info[col].alignment;
1579 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1581 for (idx_type i = 1; i < number; ++i) {
1582 CellData & cs1 = cellInfo(cell + i);
1583 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1584 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1592 bool Tabular::isMultiRow(idx_type cell) const
1594 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1595 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1599 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1601 idx_type const col = cellColumn(cell);
1602 idx_type const row = cellRow(cell);
1603 for (idx_type i = 0; i < number; ++i)
1604 unsetMultiColumn(cellIndex(row + i, col));
1606 // unsetting of multicol may have invalidated cell index
1607 cell = cellIndex(row, col);
1608 CellData & cs = cellInfo(cell);
1609 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1610 cs.valignment = LYX_VALIGN_MIDDLE;
1611 // FIXME: the horizontal alignment of multirow cells can only
1612 // be changed for the whole table,
1613 // support for this needs to be implemented but us a fileformat
1614 // change (assigning this to uwestoehr)
1615 // until LyX supports this, use the deault alignment of multirow
1617 cs.alignment = LYX_ALIGN_CENTER;
1619 // set the bottom row of the last selected cell
1620 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1622 for (idx_type i = 1; i < number; ++i) {
1623 CellData & cs1 = cell_info[row + i][col];
1624 cs1.multirow = CELL_PART_OF_MULTIROW;
1625 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1633 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1635 row_type const row = cellRow(cell);
1636 col_type const col = cellColumn(cell);
1638 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1645 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1647 col_type const column = cellColumn(cell);
1648 col_type row = cellRow(cell) + 1;
1649 while (row < nrows() && isPartOfMultiRow(row, column))
1652 return row - cellRow(cell);
1656 void Tabular::unsetMultiColumn(idx_type cell)
1658 if (!isMultiColumn(cell))
1661 row_type const row = cellRow(cell);
1662 col_type const col = cellColumn(cell);
1663 row_type const span = columnSpan(cell);
1664 for (col_type c = 0; c < span; ++c)
1665 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1670 void Tabular::unsetMultiRow(idx_type cell)
1672 if (!isMultiRow(cell))
1675 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1676 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1677 row_type const row = cellRow(cell);
1678 col_type const col = cellColumn(cell);
1679 row_type const span = rowSpan(cell);
1680 for (row_type r = 0; r < span; ++r)
1681 cell_info[row + r][col].multirow = CELL_NORMAL;
1686 void Tabular::setRotateCell(idx_type cell, bool flag)
1688 cellInfo(cell).rotate = flag;
1692 bool Tabular::getRotateCell(idx_type cell) const
1694 return cellInfo(cell).rotate;
1698 bool Tabular::needRotating() const
1702 for (row_type r = 0; r < nrows(); ++r)
1703 for (col_type c = 0; c < ncols(); ++c)
1704 if (cell_info[r][c].rotate)
1710 bool Tabular::isLastCell(idx_type cell) const
1712 if (cell + 1 < numberofcells)
1718 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1720 if (cellRow(cell) == 0)
1723 col_type const col = cellColumn(cell);
1724 row_type r = cellRow(cell) - 1;
1725 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1728 return cell_info[r][col].cellno;
1732 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1734 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1735 if (nextrow < nrows())
1736 return cell_info[nextrow][cellColumn(cell)].cellno;
1741 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1743 LASSERT(column != npos && column < ncols()
1744 && row != npos && row < nrows(), /**/);
1745 return cell_info[row][column].cellno;
1749 void Tabular::setUsebox(idx_type cell, BoxType type)
1751 cellInfo(cell).usebox = type;
1755 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1756 // adds line breaks, see bug 4886.
1757 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1759 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1760 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1762 if (cellInfo(cell).usebox > 1)
1763 return cellInfo(cell).usebox;
1764 return useParbox(cell);
1769 // This are functions used for the longtable support
1771 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1777 row_info[row].endfirsthead = flag;
1781 row_info[row].endhead = flag;
1786 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1789 hd.set = haveLTHead();
1790 return row_info[row].endhead;
1794 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1797 hd.set = haveLTFirstHead();
1798 return row_info[row].endfirsthead;
1802 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1808 row_info[row].endlastfoot = flag;
1812 row_info[row].endfoot = flag;
1817 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1820 fd.set = haveLTFoot();
1821 return row_info[row].endfoot;
1825 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1828 fd.set = haveLTLastFoot();
1829 return row_info[row].endlastfoot;
1833 void Tabular::setLTNewPage(row_type row, bool what)
1835 row_info[row].newpage = what;
1839 bool Tabular::getLTNewPage(row_type row) const
1841 return row_info[row].newpage;
1845 bool Tabular::haveLTHead() const
1847 for (row_type i = 0; i < nrows(); ++i)
1848 if (row_info[i].endhead)
1854 bool Tabular::haveLTFirstHead() const
1856 if (endfirsthead.empty)
1858 for (row_type r = 0; r < nrows(); ++r)
1859 if (row_info[r].endfirsthead)
1865 bool Tabular::haveLTFoot() const
1867 for (row_type r = 0; r < nrows(); ++r)
1868 if (row_info[r].endfoot)
1874 bool Tabular::haveLTLastFoot() const
1876 if (endlastfoot.empty)
1878 for (row_type r = 0; r < nrows(); ++r)
1879 if (row_info[r].endlastfoot)
1885 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1887 idx_type i = getFirstCellInRow(row);
1889 setMultiColumn(i, numberOfCellsInRow(row));
1890 setTopLine(i, false);
1891 setBottomLine(i, false);
1892 setLeftLine(i, false);
1893 setRightLine(i, false);
1895 unsetMultiColumn(i);
1896 // When unsetting a caption row, also all existing
1897 // captions in this row must be dissolved.
1899 row_info[row].caption = what;
1904 bool Tabular::ltCaption(row_type row) const
1906 return row_info[row].caption;
1910 bool Tabular::haveLTCaption() const
1912 for (row_type r = 0; r < nrows(); ++r)
1913 if (row_info[r].caption)
1919 // end longtable support functions
1921 void Tabular::setRowAscent(row_type row, int height)
1923 if (row >= nrows() || row_info[row].ascent == height)
1925 row_info[row].ascent = height;
1929 void Tabular::setRowDescent(row_type row, int height)
1931 if (row >= nrows() || row_info[row].descent == height)
1933 row_info[row].descent = height;
1937 int Tabular::rowAscent(row_type row) const
1939 LASSERT(row < nrows(), /**/);
1940 return row_info[row].ascent;
1944 int Tabular::rowDescent(row_type row) const
1946 LASSERT(row < nrows(), /**/);
1947 return row_info[row].descent;
1951 int Tabular::height() const
1954 for (row_type row = 0; row < nrows(); ++row)
1955 height += rowAscent(row) + rowDescent(row) +
1961 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1963 LASSERT(row < nrows(), /**/);
1964 LASSERT(column < ncols(), /**/);
1965 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1969 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
1971 LASSERT(row < nrows(), /**/);
1972 LASSERT(column < ncols(), /**/);
1973 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
1977 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1979 // we only output complete row lines and the 1st row here, the rest
1980 // is done in Tabular::TeXBottomHLine(...)
1982 // get for each column the topline (if any)
1983 vector<bool> topline;
1985 for (col_type c = 0; c < ncols(); ++c) {
1986 topline.push_back(topLine(cellIndex(row, c)));
1987 // If cell is part of a multirow and not the first cell of the
1988 // multirow, no line must be drawn.
1990 if (isMultiRow(cellIndex(row, c))
1991 && isMultiRow(cellIndex(row - 1, c)))
1997 // do nothing if empty first row, or incomplete row line after
1998 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2001 // only output complete row lines and the 1st row's clines
2002 if (nset == ncols()) {
2004 os << (row == 0 ? "\\toprule " : "\\midrule ");
2008 } else if (row == 0) {
2009 for (col_type c = 0; c < ncols(); ++c) {
2010 for ( ; c < ncols() && !topline[c]; ++c) {}
2012 col_type offset = 0;
2013 for (col_type j = 0 ; j < c; ++j)
2014 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2017 //babel makes the "-" character an active one, so we have to suppress this here
2018 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2019 if (lang == "slovak" || lang == "czech")
2020 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2021 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2023 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2025 col_type cstart = c;
2026 for ( ; c < ncols() && topline[c]; ++c) {}
2028 for (col_type j = cstart ; j < c ; ++j)
2029 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2032 os << c + offset << "} ";
2040 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2042 // we output bottomlines of row r and the toplines of row r+1
2043 // if the latter do not span the whole tabular
2045 // get the bottomlines of row r, and toplines in next row
2046 bool lastrow = row == nrows() - 1;
2047 vector<bool> bottomline, topline;
2048 bool nextrowset = true;
2049 for (col_type c = 0; c < ncols(); ++c) {
2050 bottomline.push_back(bottomLine(cellIndex(row, c)));
2051 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2052 // If cell is part of a multirow and not the last or first cell of the
2053 // multirow, no line must be drawn.
2055 if (isMultiRow(cellIndex(row, c))
2056 && isMultiRow(cellIndex(row + 1, c))) {
2057 bottomline[c] = false;
2060 nextrowset &= topline[c];
2063 // combine this row's bottom lines and next row's toplines if necessary
2065 for (col_type c = 0; c < ncols(); ++c) {
2067 bottomline[c] = bottomline[c] || topline[c];
2072 // do nothing if empty, OR incomplete row line with a topline in next row
2073 if (nset == 0 || (nextrowset && nset != ncols()))
2076 if (nset == ncols()) {
2078 os << (lastrow ? "\\bottomrule" : "\\midrule");
2082 for (col_type c = 0; c < ncols(); ++c) {
2083 for ( ; c < ncols() && !bottomline[c]; ++c) {}
2085 col_type offset = 0;
2086 for (col_type j = 0 ; j < c; ++j)
2087 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2090 //babel makes the "-" character an active one, so we have to suppress this here
2091 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2092 if (lang == "slovak" || lang == "czech")
2093 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2094 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2096 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2098 col_type cstart = c;
2099 for ( ; c < ncols() && bottomline[c]; ++c) {}
2101 for (col_type j = cstart ; j < c ; ++j)
2102 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2105 os << c + offset << "} ";
2113 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2114 bool & ismulticol, bool & ismultirow) const
2117 row_type const r = cellRow(cell);
2118 if (is_long_tabular && row_info[r].caption)
2121 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2122 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2123 // figure out how to set the lines
2124 // we always set double lines to the right of the cell
2125 col_type const c = cellColumn(cell);
2126 col_type const nextcol = c + columnSpan(cell);
2127 bool colright = columnRightLine(c);
2128 bool colleft = columnLeftLine(c);
2129 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2130 bool nextcellleft = nextcol < ncols()
2131 && leftLine(cellIndex(r, nextcol));
2132 bool coldouble = colright && nextcolleft;
2133 bool celldouble = rightLine(cell) && nextcellleft;
2135 ismulticol = isMultiColumn(cell)
2136 || (c == 0 && colleft != leftLine(cell))
2137 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2138 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2139 || (coldouble != celldouble);
2141 // we center in multicol when no decimal point
2142 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2143 && (cellInfo(cell).decimal_width == 0));
2145 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2146 int latexcolspan = columnSpan(cell);
2147 for(col_type col = c; col < c + columnSpan(cell); ++col)
2148 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2152 os << "\\multicolumn{" << latexcolspan << "}{";
2153 if (c ==0 && leftLine(cell))
2155 if (!cellInfo(cell).align_special.empty()) {
2156 os << cellInfo(cell).align_special;
2158 if (!getPWidth(cell).zero()) {
2160 case LYX_ALIGN_LEFT:
2161 os << ">{\\raggedright}";
2163 case LYX_ALIGN_RIGHT:
2164 os << ">{\\raggedleft}";
2166 case LYX_ALIGN_CENTER:
2167 os << ">{\\centering}";
2173 case LYX_VALIGN_TOP:
2176 case LYX_VALIGN_MIDDLE:
2179 case LYX_VALIGN_BOTTOM:
2184 << from_ascii(getPWidth(cell).asLatexString())
2188 case LYX_ALIGN_LEFT:
2191 case LYX_ALIGN_RIGHT:
2198 } // end if else !getPWidth
2199 } // end if else !cellinfo_of_cell
2200 if (rightLine(cell) || nextcellleft)
2203 // add extra vertical line if we want a double one
2206 } // end if ismulticol
2208 // we only need code for the first multirow cell
2209 ismultirow = isMultiRow(cell);
2211 os << "\\multirow{" << rowSpan(cell) << "}{";
2212 if (!getPWidth(cell).zero())
2213 os << from_ascii(getPWidth(cell).asLatexString());
2215 // we need to set a default value
2216 // needs to be discussed
2219 } // end if ismultirow
2221 if (getRotateCell(cell)) {
2222 os << "\\begin{sideways}\n";
2225 if (getUsebox(cell) == BOX_PARBOX) {
2228 case LYX_VALIGN_TOP:
2231 case LYX_VALIGN_MIDDLE:
2234 case LYX_VALIGN_BOTTOM:
2238 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2240 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2241 os << "\\begin{minipage}[";
2243 case LYX_VALIGN_TOP:
2246 case LYX_VALIGN_MIDDLE:
2249 case LYX_VALIGN_BOTTOM:
2253 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2261 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2262 bool ismulticol, bool ismultirow) const
2265 row_type const r = cellRow(cell);
2266 if (is_long_tabular && row_info[r].caption)
2270 if (getUsebox(cell) == BOX_PARBOX)
2272 else if (getUsebox(cell) == BOX_MINIPAGE) {
2273 os << "%\n\\end{minipage}";
2276 if (getRotateCell(cell)) {
2277 os << "%\n\\end{sideways}";
2289 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2290 OutputParams const & runparams) const
2292 if (!is_long_tabular)
2297 // the caption must be output before the headers
2298 if (haveLTCaption()) {
2299 for (row_type r = 0; r < nrows(); ++r) {
2300 if (row_info[r].caption) {
2301 ret += TeXRow(os, r, runparams);
2305 // output first header info
2306 // first header must be output before the header, otherwise the
2307 // correct caption placement becomes really weird
2308 if (haveLTFirstHead()) {
2309 if (endfirsthead.topDL) {
2313 for (row_type r = 0; r < nrows(); ++r) {
2314 if (row_info[r].endfirsthead) {
2315 ret += TeXRow(os, r, runparams);
2318 if (endfirsthead.bottomDL) {
2322 os << "\\endfirsthead\n";
2325 // output header info
2327 if (endfirsthead.empty && !haveLTFirstHead()) {
2328 os << "\\endfirsthead\n";
2331 if (endhead.topDL) {
2335 for (row_type r = 0; r < nrows(); ++r) {
2336 if (row_info[r].endhead) {
2337 ret += TeXRow(os, r, runparams);
2340 if (endhead.bottomDL) {
2344 os << "\\endhead\n";
2347 // output footer info
2349 if (endfoot.topDL) {
2353 for (row_type r = 0; r < nrows(); ++r) {
2354 if (row_info[r].endfoot) {
2355 ret += TeXRow(os, r, runparams);
2358 if (endfoot.bottomDL) {
2362 os << "\\endfoot\n";
2364 if (endlastfoot.empty && !haveLTLastFoot()) {
2365 os << "\\endlastfoot\n";
2369 // output lastfooter info
2370 if (haveLTLastFoot()) {
2371 if (endlastfoot.topDL) {
2375 for (row_type r = 0; r < nrows(); ++r) {
2376 if (row_info[r].endlastfoot) {
2377 ret += TeXRow(os, r, runparams);
2380 if (endlastfoot.bottomDL) {
2384 os << "\\endlastfoot\n";
2391 bool Tabular::isValidRow(row_type row) const
2393 if (!is_long_tabular)
2395 return !row_info[row].endhead && !row_info[row].endfirsthead
2396 && !row_info[row].endfoot && !row_info[row].endlastfoot
2397 && !row_info[row].caption;
2401 int Tabular::TeXRow(odocstream & os, row_type row,
2402 OutputParams const & runparams) const
2404 idx_type cell = cellIndex(row, 0);
2405 shared_ptr<InsetTableCell> inset = cellInset(cell);
2406 Paragraph const & par = inset->paragraphs().front();
2407 string const lang = par.getParLanguage(buffer().params())->lang();
2409 //output the top line
2410 int ret = TeXTopHLine(os, row, lang);
2412 if (row_info[row].top_space_default) {
2414 os << "\\addlinespace\n";
2416 os << "\\noalign{\\vskip\\doublerulesep}\n";
2418 } else if(!row_info[row].top_space.zero()) {
2420 os << "\\addlinespace["
2421 << from_ascii(row_info[row].top_space.asLatexString())
2424 os << "\\noalign{\\vskip"
2425 << from_ascii(row_info[row].top_space.asLatexString())
2430 bool ismulticol = false;
2431 bool ismultirow = false;
2432 for (col_type c = 0; c < ncols(); ++c) {
2433 if (isPartOfMultiColumn(row, c))
2436 if (isPartOfMultiRow(row, c) &&
2437 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2442 cell = cellIndex(row, c);
2443 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2444 shared_ptr<InsetTableCell> inset = cellInset(cell);
2446 Paragraph const & par = inset->paragraphs().front();
2447 bool rtl = par.isRTL(buffer().params())
2449 && getPWidth(cell).zero();
2453 par.getParLanguage(buffer().params())->lang();
2454 if (lang == "farsi")
2456 else if (lang == "arabic_arabi")
2458 // currently, remaning RTL languages are
2459 // arabic_arabtex and hebrew
2463 // pass to the OutputParams that we are in a cell and
2464 // which alignment we have set.
2465 // InsetNewline needs this context information.
2466 OutputParams newrp(runparams);
2467 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2468 ? OutputParams::PLAIN
2469 : OutputParams::ALIGNED;
2471 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2472 && cellInfo(cell).decimal_width != 0) {
2473 // copy cell and split in 2
2474 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2475 head.getText(0)->setMacrocontextPosition(
2476 cellInset(cell)->getText(0)->macrocontextPosition());
2477 head.setBuffer(buffer());
2478 bool hassep = false;
2479 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2480 head.latex(os, newrp);
2482 ret += tail.latex(os, newrp);
2483 } else if (!isPartOfMultiRow(row, c))
2484 ret += inset->latex(os, newrp);
2486 runparams.encoding = newrp.encoding;
2490 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2491 if (cell != getLastCellInRow(row)) { // not last cell in row
2495 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2496 // if no first header and no empty first header is used,
2497 // the caption needs to be terminated by \endfirsthead
2499 os << "\\endfirsthead";
2501 os << "\\tabularnewline";
2502 if (row_info[row].bottom_space_default) {
2504 os << "\\addlinespace";
2506 os << "[\\doublerulesep]";
2507 } else if (!row_info[row].bottom_space.zero()) {
2509 os << "\\addlinespace";
2511 << from_ascii(row_info[row].bottom_space.asLatexString())
2517 //output the bottom line
2518 ret += TeXBottomHLine(os, row, lang);
2520 if (row_info[row].interline_space_default) {
2522 os << "\\addlinespace\n";
2524 os << "\\noalign{\\vskip\\doublerulesep}\n";
2526 } else if (!row_info[row].interline_space.zero()) {
2528 os << "\\addlinespace["
2529 << from_ascii(row_info[row].interline_space.asLatexString())
2532 os << "\\noalign{\\vskip"
2533 << from_ascii(row_info[row].interline_space.asLatexString())
2541 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2545 //+---------------------------------------------------------------------
2546 //+ first the opening preamble +
2547 //+---------------------------------------------------------------------
2550 os << "\\begin{sideways}\n";
2553 if (is_long_tabular) {
2554 os << "\\begin{longtable}";
2555 switch (longtabular_alignment) {
2556 case LYX_LONGTABULAR_ALIGN_LEFT:
2559 case LYX_LONGTABULAR_ALIGN_CENTER:
2561 case LYX_LONGTABULAR_ALIGN_RIGHT:
2566 os << "\\begin{tabular}";
2567 switch (tabular_valignment) {
2568 case LYX_VALIGN_TOP:
2571 case LYX_VALIGN_MIDDLE:
2573 case LYX_VALIGN_BOTTOM:
2581 for (col_type c = 0; c < ncols(); ++c) {
2582 if (columnLeftLine(c))
2584 if (!column_info[c].align_special.empty()) {
2585 os << column_info[c].align_special;
2587 if (!column_info[c].p_width.zero()) {
2588 switch (column_info[c].alignment) {
2589 case LYX_ALIGN_LEFT:
2590 os << ">{\\raggedright}";
2592 case LYX_ALIGN_RIGHT:
2593 os << ">{\\raggedleft}";
2595 case LYX_ALIGN_CENTER:
2596 os << ">{\\centering}";
2598 case LYX_ALIGN_NONE:
2599 case LYX_ALIGN_BLOCK:
2600 case LYX_ALIGN_LAYOUT:
2601 case LYX_ALIGN_SPECIAL:
2602 case LYX_ALIGN_DECIMAL:
2606 switch (column_info[c].valignment) {
2607 case LYX_VALIGN_TOP:
2610 case LYX_VALIGN_MIDDLE:
2613 case LYX_VALIGN_BOTTOM:
2618 << from_ascii(column_info[c].p_width.asLatexString())
2621 switch (column_info[c].alignment) {
2622 case LYX_ALIGN_LEFT:
2625 case LYX_ALIGN_RIGHT:
2628 case LYX_ALIGN_DECIMAL:
2629 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2635 } // end if else !column_info[i].p_width
2636 } // end if else !column_info[i].align_special
2637 if (columnRightLine(c))
2643 ret += TeXLongtableHeaderFooter(os, runparams);
2645 //+---------------------------------------------------------------------
2646 //+ the single row and columns (cells) +
2647 //+---------------------------------------------------------------------
2649 for (row_type r = 0; r < nrows(); ++r) {
2650 if (isValidRow(r)) {
2651 ret += TeXRow(os, r, runparams);
2652 if (is_long_tabular && row_info[r].newpage) {
2653 os << "\\newpage\n";
2659 //+---------------------------------------------------------------------
2660 //+ the closing of the tabular +
2661 //+---------------------------------------------------------------------
2663 if (is_long_tabular)
2664 os << "\\end{longtable}";
2666 os << "\\end{tabular}";
2668 os << "\n\\end{sideways}";
2676 int Tabular::docbookRow(odocstream & os, row_type row,
2677 OutputParams const & runparams) const
2680 idx_type cell = getFirstCellInRow(row);
2683 for (col_type c = 0; c < ncols(); ++c) {
2684 if (isPartOfMultiColumn(row, c))
2687 os << "<entry align=\"";
2688 switch (getAlignment(cell)) {
2689 case LYX_ALIGN_LEFT:
2692 case LYX_ALIGN_RIGHT:
2700 os << "\" valign=\"";
2701 switch (getVAlignment(cell)) {
2702 case LYX_VALIGN_TOP:
2705 case LYX_VALIGN_BOTTOM:
2708 case LYX_VALIGN_MIDDLE:
2713 if (isMultiColumn(cell)) {
2714 os << " namest=\"col" << c << "\" ";
2715 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2719 ret += cellInset(cell)->docbook(os, runparams);
2728 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2732 //+---------------------------------------------------------------------
2733 //+ first the opening preamble +
2734 //+---------------------------------------------------------------------
2736 os << "<tgroup cols=\"" << ncols()
2737 << "\" colsep=\"1\" rowsep=\"1\">\n";
2739 for (col_type c = 0; c < ncols(); ++c) {
2740 os << "<colspec colname=\"col" << c << "\" align=\"";
2741 switch (column_info[c].alignment) {
2742 case LYX_ALIGN_LEFT:
2745 case LYX_ALIGN_RIGHT:
2753 if (runparams.flavor == OutputParams::XML)
2759 //+---------------------------------------------------------------------
2760 //+ Long Tabular case +
2761 //+---------------------------------------------------------------------
2763 // output caption info
2764 if (haveLTCaption()) {
2765 os << "<caption>\n";
2767 for (row_type r = 0; r < nrows(); ++r) {
2768 if (row_info[r].caption) {
2769 ret += docbookRow(os, r, runparams);
2772 os << "</caption>\n";
2775 // output header info
2776 if (haveLTHead() || haveLTFirstHead()) {
2779 for (row_type r = 0; r < nrows(); ++r) {
2780 if (row_info[r].endhead || row_info[r].endfirsthead) {
2781 ret += docbookRow(os, r, runparams);
2787 // output footer info
2788 if (haveLTFoot() || haveLTLastFoot()) {
2791 for (row_type r = 0; r < nrows(); ++r) {
2792 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2793 ret += docbookRow(os, r, runparams);
2800 //+---------------------------------------------------------------------
2801 //+ the single row and columns (cells) +
2802 //+---------------------------------------------------------------------
2806 for (row_type r = 0; r < nrows(); ++r) {
2807 if (isValidRow(r)) {
2808 ret += docbookRow(os, r, runparams);
2813 //+---------------------------------------------------------------------
2814 //+ the closing of the tabular +
2815 //+---------------------------------------------------------------------
2824 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2825 OutputParams const & runparams) const
2828 idx_type cell = getFirstCellInRow(row);
2830 xs << html::StartTag("tr");
2831 for (col_type c = 0; c < ncols(); ++c) {
2832 if (isPartOfMultiColumn(row, c))
2837 switch (getAlignment(cell)) {
2838 case LYX_ALIGN_LEFT:
2841 case LYX_ALIGN_RIGHT:
2849 attr << " valign='";
2850 switch (getVAlignment(cell)) {
2851 case LYX_VALIGN_TOP:
2854 case LYX_VALIGN_BOTTOM:
2857 case LYX_VALIGN_MIDDLE:
2862 if (isMultiColumn(cell))
2863 attr << " colspan='" << columnSpan(cell) << "'";
2865 xs << html::StartTag("td", attr.str());
2866 ret += cellInset(cell)->xhtml(xs, runparams);
2867 xs << html::EndTag("td");
2870 xs << html::EndTag("tr");
2875 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2878 // It's unclear to me if we need to mess with the long table stuff.
2879 // We can borrow that too from docbook, if so.
2881 xs << html::StartTag("tbody");
2882 for (row_type r = 0; r < nrows(); ++r) {
2883 if (isValidRow(r)) {
2884 ret += xhtmlRow(xs, r, runparams);
2887 xs << html::EndTag("tbody");
2892 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2893 vector<unsigned int> const & clen) const
2895 idx_type const fcell = getFirstCellInRow(row);
2896 idx_type const n = numberOfCellsInRow(row) + fcell;
2899 for (idx_type i = fcell; i < n; ++i) {
2909 for (idx_type i = fcell; i < n; ++i) {
2920 col_type column = cellColumn(i);
2921 int len = clen[column];
2922 while (column < ncols() - 1
2923 && isPartOfMultiColumn(row, ++column))
2924 len += clen[column] + 4;
2925 os << docstring(len, ch);
2940 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2941 vector<unsigned int> const & clen) const
2943 idx_type const fcell = getFirstCellInRow(row);
2944 idx_type const n = numberOfCellsInRow(row) + fcell;
2947 for (idx_type i = fcell; i < n; ++i) {
2948 if (bottomLine(i)) {
2957 for (idx_type i = fcell; i < n; ++i) {
2958 if (bottomLine(i)) {
2968 col_type column = cellColumn(i);
2969 int len = clen[column];
2970 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
2971 len += clen[column] + 4;
2972 os << docstring(len, ch);
2973 if (bottomLine(i)) {
2987 void Tabular::plaintextPrintCell(odocstream & os,
2988 OutputParams const & runparams,
2989 idx_type cell, row_type row, col_type column,
2990 vector<unsigned int> const & clen,
2991 bool onlydata) const
2993 odocstringstream sstr;
2994 cellInset(cell)->plaintext(sstr, runparams);
3006 unsigned int len1 = sstr.str().length();
3007 unsigned int len2 = clen[column];
3008 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3009 len2 += clen[column] + 4;
3012 switch (getAlignment(cell)) {
3014 case LYX_ALIGN_LEFT:
3017 case LYX_ALIGN_RIGHT:
3021 case LYX_ALIGN_CENTER:
3027 os << docstring(len1, ' ') << sstr.str()
3028 << docstring(len2, ' ');
3030 if (rightLine(cell))
3037 void Tabular::plaintext(odocstream & os,
3038 OutputParams const & runparams, int const depth,
3039 bool onlydata, char_type delim) const
3041 // first calculate the width of the single columns
3042 vector<unsigned int> clen(ncols());
3045 // first all non multicolumn cells!
3046 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 (isMultiColumn(cell))
3052 odocstringstream sstr;
3053 cellInset(cell)->plaintext(sstr, runparams);
3054 if (clen[c] < sstr.str().length())
3055 clen[c] = sstr.str().length();
3058 // then all multicolumn cells!
3059 for (col_type c = 0; c < ncols(); ++c) {
3060 for (row_type r = 0; r < nrows(); ++r) {
3061 idx_type cell = cellIndex(r, c);
3062 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3064 odocstringstream sstr;
3065 cellInset(cell)->plaintext(sstr, runparams);
3066 int len = int(sstr.str().length());
3067 idx_type const n = columnSpan(cell);
3068 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3070 if (len > int(clen[c + n - 1]))
3071 clen[c + n - 1] = len;
3076 for (row_type r = 0; r < nrows(); ++r) {
3077 if (!onlydata && plaintextTopHLine(os, r, clen))
3078 os << docstring(depth * 2, ' ');
3079 for (col_type c = 0; c < ncols(); ++c) {
3080 if (isPartOfMultiColumn(r, c))
3082 if (onlydata && c > 0)
3083 // we don't use operator<< for single UCS4 character.
3084 // see explanation in docstream.h
3086 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3091 os << docstring(depth * 2, ' ');
3092 if (plaintextBottomHLine(os, r, clen))
3093 os << docstring(depth * 2, ' ');
3099 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3101 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3105 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3106 col_type column) const
3108 return cell_info[row][column].inset;
3112 void Tabular::setCellInset(row_type row, col_type column,
3113 shared_ptr<InsetTableCell> ins) const
3115 CellData & cd = cell_info[row][column];
3120 void Tabular::validate(LaTeXFeatures & features) const
3122 features.require("NeedTabularnewline");
3124 features.require("booktabs");
3125 if (is_long_tabular)
3126 features.require("longtable");
3128 features.require("rotating");
3129 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3130 if (isMultiRow(cell))
3131 features.require("multirow");
3132 if (getVAlignment(cell) != LYX_VALIGN_TOP
3133 || !getPWidth(cell).zero())
3134 features.require("array");
3135 cellInset(cell)->validate(features);
3140 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3142 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3143 ParagraphList::const_iterator cit = parlist.begin();
3144 ParagraphList::const_iterator end = parlist.end();
3146 for (; cit != end; ++cit)
3147 for (int i = 0; i < cit->size(); ++i)
3148 if (cit->isNewline(i))
3155 /////////////////////////////////////////////////////////////////////
3159 /////////////////////////////////////////////////////////////////////
3161 InsetTableCell::InsetTableCell(Buffer * buf)
3162 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3163 contentAlign(LYX_ALIGN_CENTER)
3167 bool InsetTableCell::forcePlainLayout(idx_type) const
3169 return !isFixedWidth;
3173 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3175 return isFixedWidth;
3179 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3180 FuncStatus & status) const
3183 switch (cmd.action()) {
3185 enabled = !forcePlainLayout();
3187 case LFUN_LAYOUT_PARAGRAPH:
3188 enabled = allowParagraphCustomization();
3191 return InsetText::getStatus(cur, cmd, status);
3193 status.setEnabled(enabled);
3197 docstring InsetTableCell::asString(bool intoInsets)
3200 if (paragraphs().empty())
3202 ParagraphList::const_iterator it = paragraphs().begin();
3203 ParagraphList::const_iterator en = paragraphs().end();
3205 for (; it != en; ++it) {
3210 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3216 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3219 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3220 return InsetText::xhtml(xs, rp);
3225 /////////////////////////////////////////////////////////////////////
3229 /////////////////////////////////////////////////////////////////////
3231 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3233 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3234 rowselect_(false), colselect_(false)
3239 InsetTabular::InsetTabular(InsetTabular const & tab)
3240 : Inset(tab), tabular(tab.tabular), scx_(0)
3245 InsetTabular::~InsetTabular()
3247 hideDialogs("tabular", this);
3251 void InsetTabular::setBuffer(Buffer & buf)
3253 tabular.setBuffer(buf);
3254 Inset::setBuffer(buf);
3258 bool InsetTabular::insetAllowed(InsetCode code) const
3263 case MATHMACRO_CODE:
3267 return tabular.is_long_tabular;
3275 void InsetTabular::write(ostream & os) const
3277 os << "Tabular" << endl;
3282 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3284 // FIXME: depending on the selection state, we could offer a different menu.
3285 return from_ascii("context-tabular");
3289 void InsetTabular::read(Lexer & lex)
3291 //bool const old_format = (lex.getString() == "\\LyXTable");
3299 string token = lex.getString();
3300 while (lex && token != "\\end_inset") {
3302 token = lex.getString();
3305 lex.printError("Missing \\end_inset at this point. ");
3309 int InsetTabular::rowFromY(Cursor & cur, int y) const
3311 // top y coordinate of tabular
3312 int h = yo(cur.bv()) - tabular.rowAscent(0);
3314 for (; r < tabular.nrows() && y > h; ++r)
3315 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3316 + tabular.interRowSpace(r);
3322 int InsetTabular::columnFromX(Cursor & cur, int x) const
3324 // left x coordinate of tabular
3325 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3327 for (; c < tabular.ncols() && x > w; ++c)
3328 w += tabular.columnWidth(c);
3333 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3335 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3336 // mi.base.textwidth << "\n";
3339 LASSERT(false, /**/);
3342 for (row_type r = 0; r < tabular.nrows(); ++r) {
3345 for (col_type c = 0; c < tabular.ncols(); ++c) {
3346 if (tabular.isPartOfMultiColumn(r, c)
3347 || tabular.isPartOfMultiRow(r, c))
3348 // multicolumn or multirow cell, but not first one
3350 idx_type const cell = tabular.cellIndex(r, c);
3353 Length const p_width = tabular.getPWidth(cell);
3354 if (!p_width.zero())
3355 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3356 tabular.cellInset(cell)->metrics(m, dim);
3357 if (!p_width.zero())
3358 dim.wid = m.base.textwidth;
3359 tabular.setCellWidth(cell, dim.wid);
3361 // FIXME(?): do we need a second metrics call?
3362 TextMetrics const & tm =
3363 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3365 // determine horiz offset because of decimal align (if necessary)
3366 int decimal_hoffset = 0;
3367 int decimal_width = 0;
3368 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3369 // make a copy which we will split in 2
3370 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3371 head.getText(0)->setMacrocontextPosition(
3372 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3373 head.setBuffer(tabular.buffer());
3374 // split in 2 and calculate width of each part
3375 bool hassep = false;
3376 InsetTableCell tail =
3377 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3379 head.metrics(m, dim1);
3380 decimal_hoffset = dim1.width();
3382 tail.metrics(m, dim1);
3383 decimal_width = dim1.width();
3386 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3387 tabular.cell_info[r][c].decimal_width = decimal_width;
3389 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3390 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3391 int const lastpardes = tm.last().second->descent()
3392 + TEXT_TO_INSET_OFFSET;
3394 switch (tabular.getVAlignment(cell)) {
3395 case Tabular::LYX_VALIGN_TOP:
3397 case Tabular::LYX_VALIGN_MIDDLE:
3398 offset = -(dim.des - lastpardes)/2;
3400 case Tabular::LYX_VALIGN_BOTTOM:
3401 offset = -(dim.des - lastpardes);
3404 tabular.cell_info[r][c].voffset = offset;
3405 maxasc = max(maxasc, dim.asc - offset);
3406 maxdes = max(maxdes, dim.des + offset);
3408 int const top_space = tabular.row_info[r].top_space_default ?
3409 default_line_space :
3410 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3411 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3412 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3413 default_line_space :
3414 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3415 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3417 tabular.updateColumnWidths();
3418 dim.asc = tabular.rowAscent(0);
3419 dim.des = tabular.height() - dim.asc;
3420 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3424 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3427 if (&cur.inset() == this && cur.selection()) {
3428 if (cur.selIsMultiCell()) {
3431 getSelection(cur, rs, re, cs, ce);
3433 idx_type const cell = tabular.cellIndex(row, col);
3434 col_type const cspan = tabular.columnSpan(cell);
3435 row_type const rspan = tabular.rowSpan(cell);
3436 if (col + cspan - 1 >= cs && col <= ce
3437 && row + rspan - 1 >= rs && row <= re)
3440 if (col == tabular.cellColumn(cur.idx())
3441 && row == tabular.cellRow(cur.idx())) {
3442 CursorSlice const & beg = cur.selBegin();
3443 CursorSlice const & end = cur.selEnd();
3445 if ((end.lastpos() > 0 || end.lastpit() > 0)
3446 && end.pos() == end.lastpos() && beg.pos() == 0
3447 && end.pit() == end.lastpit() && beg.pit() == 0)
3455 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3457 x += scx_ + ADD_TO_TABULAR_WIDTH;
3459 BufferView * bv = pi.base.bv;
3460 Cursor & cur = pi.base.bv->cursor();
3463 // FIXME: As the full background is painted in drawSelection(),
3464 // we have no choice but to do a full repaint for the Text cells.
3465 pi.full_repaint = true;
3467 bool const original_selection_state = pi.selected;
3470 first_visible_cell = Tabular::npos;
3471 for (row_type r = 0; r < tabular.nrows(); ++r) {
3473 for (col_type c = 0; c < tabular.ncols(); ++c) {
3474 if (tabular.isPartOfMultiColumn(r, c))
3477 idx = tabular.cellIndex(r, c);
3479 if (tabular.isPartOfMultiRow(r, c)) {
3480 nx += tabular.columnWidth(idx);
3484 if (first_visible_cell == Tabular::npos)
3485 first_visible_cell = idx;
3487 pi.selected |= isCellSelected(cur, r, c);
3488 int const cx = nx + tabular.textHOffset(idx);
3489 int const cy = y + tabular.textVOffset(idx);
3490 // Cache the Inset position.
3491 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3492 cell(idx)->draw(pi, cx, cy);
3493 drawCellLines(pi.pain, nx, y, r, idx, pi.change_);
3494 nx += tabular.columnWidth(idx);
3495 pi.selected = original_selection_state;
3498 if (r + 1 < tabular.nrows())
3499 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3500 + tabular.interRowSpace(r + 1);
3505 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3507 Cursor & cur = pi.base.bv->cursor();
3510 x += scx_ + ADD_TO_TABULAR_WIDTH;
3512 // FIXME: it is wrong to completely paint the background
3513 // if we want to do single row painting.
3515 // Paint background of current tabular
3516 int const w = tabular.width();
3517 int const h = tabular.height();
3518 int yy = y - tabular.rowAscent(0);
3519 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3521 if (!cur.selection())
3523 if (&cur.inset() != this)
3528 bool const full_cell_selected = isCellSelected(cur,
3529 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3531 if (cur.selIsMultiCell() || full_cell_selected) {
3532 for (row_type r = 0; r < tabular.nrows(); ++r) {
3534 for (col_type c = 0; c < tabular.ncols(); ++c) {
3535 if (tabular.isPartOfMultiColumn(r, c))
3538 idx_type const cell = tabular.cellIndex(r, c);
3540 if (tabular.isPartOfMultiRow(r, c)) {
3541 xx += tabular.columnWidth(cell);
3544 int const w = tabular.columnWidth(cell);
3545 int const h = tabular.cellHeight(cell);
3546 int const yy = y - tabular.rowAscent(r);
3547 if (isCellSelected(cur, r, c))
3548 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3551 if (r + 1 < tabular.nrows())
3552 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3553 + tabular.interRowSpace(r + 1);
3557 x += cellXPos(cur.idx());
3558 x += tabular.textHOffset(cur.idx());
3559 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3564 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3565 row_type row, idx_type cell, Change const & change) const
3567 y -= tabular.rowAscent(row);
3568 int const w = tabular.columnWidth(cell);
3569 int const h = tabular.cellHeight(cell);
3570 Color linecolor = change.changed() ? change.color() : Color_tabularline;
3571 Color gridcolor = change.changed() ? change.color() : Color_tabularonoffline;
3574 bool drawline = tabular.topLine(cell)
3575 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3576 pain.line(x, y, x + w, y,
3577 drawline ? linecolor : gridcolor,
3578 drawline ? Painter::line_solid : Painter::line_onoffdash);
3581 drawline = tabular.bottomLine(cell);
3582 pain.line(x, y + h, x + w, y + h,
3583 drawline ? linecolor : gridcolor,
3584 drawline ? Painter::line_solid : Painter::line_onoffdash);
3587 col_type const col = tabular.cellColumn(cell);
3588 drawline = tabular.leftLine(cell)
3589 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3590 pain.line(x, y, x, y + h,
3591 drawline ? linecolor : gridcolor,
3592 drawline ? Painter::line_solid : Painter::line_onoffdash);
3595 x -= tabular.interColumnSpace(cell);
3596 drawline = tabular.rightLine(cell)
3597 || (col + 1 < tabular.ncols()
3598 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3599 pain.line(x + w, y, x + w, y + h,
3600 drawline ? linecolor : gridcolor,
3601 drawline ? Painter::line_solid : Painter::line_onoffdash);
3605 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3607 //lyxerr << "InsetTabular::edit: " << this << endl;
3609 cur.setSelection(false);
3612 if (isRightToLeft(cur))
3613 cur.idx() = tabular.getLastCellInRow(0);
3619 if (isRightToLeft(cur))
3620 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3622 cur.idx() = tabular.numberofcells - 1;
3624 cur.pos() = cur.lastpos(); // FIXME crude guess
3626 cur.setCurrentFont();
3627 // FIXME: this accesses the position cache before it is initialized
3629 //cur.bv().fitCursor();
3633 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3635 // In a longtable, tell captions what the current float is
3636 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3637 string const saveflt = cnts.current_float();
3638 if (tabular.is_long_tabular)
3639 cnts.current_float("table");
3641 ParIterator it2 = it;
3643 size_t const end = it2.nargs();
3644 for ( ; it2.idx() < end; it2.top().forwardIdx())
3645 buffer().updateBuffer(it2, utype);
3648 if (tabular.is_long_tabular)
3649 cnts.current_float(saveflt);
3653 void InsetTabular::addToToc(DocIterator const & cpit)
3655 DocIterator dit = cpit;
3657 size_t const end = dit.nargs();
3658 for ( ; dit.idx() < end; dit.top().forwardIdx())
3659 cell(dit.idx())->addToToc(dit);
3663 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3665 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3666 << "\n cur:" << cur);
3667 CursorSlice sl = cur.top();
3668 Cursor & bvcur = cur.bv().cursor();
3670 FuncCode const act = cmd.action();
3674 case LFUN_MOUSE_PRESS: {
3675 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3677 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3678 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3679 row_type r = rowFromY(cur, cmd.y());
3680 cur.idx() = tabular.getFirstCellInRow(r);
3683 cur.idx() = tabular.getLastCellInRow(r);
3684 cur.pos() = cur.lastpos();
3685 cur.setSelection(true);
3691 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3692 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3693 || cmd.y() > y0 + tabular.height()) {
3694 col_type c = columnFromX(cur, cmd.x());
3695 cur.idx() = tabular.cellIndex(0, c);
3698 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3699 cur.pos() = cur.lastpos();
3700 cur.setSelection(true);
3705 // do not reset cursor/selection if we have selected
3706 // some cells (bug 2715).
3707 if (cmd.button() == mouse_button::button3
3708 && &bvcur.selBegin().inset() == this
3709 && bvcur.selIsMultiCell())
3712 // Let InsetTableCell do it
3713 cell(cur.idx())->dispatch(cur, cmd);
3716 case LFUN_MOUSE_MOTION:
3717 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3718 if (cmd.button() == mouse_button::button1) {
3719 // only accept motions to places not deeper nested than the real anchor
3720 if (!bvcur.realAnchor().hasPart(cur)) {
3724 // select (additional) row
3726 row_type r = rowFromY(cur, cmd.y());
3727 cur.idx() = tabular.getLastCellInRow(r);
3728 // we need to reset the cursor's pit and pos now, as the old ones
3729 // may no longer be valid.
3732 bvcur.setCursor(cur);
3733 bvcur.setSelection(true);
3736 // select (additional) column
3738 col_type c = columnFromX(cur, cmd.x());
3739 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3740 // we need to reset the cursor's pit and pos now, as the old ones
3741 // may no longer be valid.
3744 bvcur.setCursor(cur);
3745 bvcur.setSelection(true);
3748 // only update if selection changes
3749 if (bvcur.idx() == cur.idx() &&
3750 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3752 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3753 bvcur.setCursor(cur);
3754 bvcur.setSelection(true);
3755 // if this is a multicell selection, we just set the cursor to
3756 // the beginning of the cell's text.
3757 if (bvcur.selIsMultiCell()) {
3758 bvcur.pit() = bvcur.lastpit();
3759 bvcur.pos() = bvcur.lastpos();
3764 case LFUN_MOUSE_RELEASE:
3769 case LFUN_CELL_BACKWARD:
3771 cur.setSelection(false);
3774 case LFUN_CELL_FORWARD:
3776 cur.setSelection(false);
3779 case LFUN_CHAR_FORWARD_SELECT:
3780 case LFUN_CHAR_FORWARD:
3781 case LFUN_CHAR_BACKWARD_SELECT:
3782 case LFUN_CHAR_BACKWARD:
3783 case LFUN_CHAR_RIGHT_SELECT:
3784 case LFUN_CHAR_RIGHT:
3785 case LFUN_CHAR_LEFT_SELECT:
3786 case LFUN_CHAR_LEFT: {
3787 // determine whether we move to next or previous cell, where to enter
3788 // the new cell from, and which command to "finish" (i.e., exit the
3791 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3792 FuncCode finish_lfun;
3794 if (act == LFUN_CHAR_FORWARD
3795 || act == LFUN_CHAR_FORWARD_SELECT) {
3797 finish_lfun = LFUN_FINISHED_FORWARD;
3799 else if (act == LFUN_CHAR_BACKWARD
3800 || act == LFUN_CHAR_BACKWARD_SELECT) {
3802 finish_lfun = LFUN_FINISHED_BACKWARD;
3804 // LEFT or RIGHT commands --- the interpretation will depend on the
3805 // table's direction.
3807 bool const right = act == LFUN_CHAR_RIGHT
3808 || act == LFUN_CHAR_RIGHT_SELECT;
3809 next_cell = isRightToLeft(cur) != right;
3811 if (lyxrc.visual_cursor)
3812 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3814 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3817 bool const select = act == LFUN_CHAR_FORWARD_SELECT ||
3818 act == LFUN_CHAR_BACKWARD_SELECT ||
3819 act == LFUN_CHAR_RIGHT_SELECT ||
3820 act == LFUN_CHAR_LEFT_SELECT;
3822 // If we have a multicell selection or we're
3823 // not doing some LFUN_*_SELECT thing anyway...
3824 if (!cur.selIsMultiCell() || !select) {
3825 col_type const c = tabular.cellColumn(cur.idx());
3826 row_type const r = tabular.cellRow(cur.idx());
3827 // Are we trying to select the whole cell and is the whole cell
3828 // not yet selected?
3829 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3830 ((next_cell && cur.pit() == cur.lastpit()
3831 && cur.pos() == cur.lastpos())
3832 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3834 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3836 // ...try to dispatch to the cell's inset.
3837 cell(cur.idx())->dispatch(cur, cmd);
3839 // When we already have a selection we want to select the whole cell
3840 // before going to the next cell.
3841 if (select_whole && !empty_cell){
3842 getText(cur.idx())->selectAll(cur);
3847 // FIXME: When we support the selection of an empty cell, remove
3848 // the !empty_cell from this condition. For now we jump to the next
3849 // cell if the current cell is empty.
3850 if (cur.result().dispatched() && !empty_cell)
3854 // move to next/prev cell, as appropriate
3855 // note that we will always do this if we're selecting and we have
3856 // a multicell selection
3857 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3858 << " cell from: " << int(entry_from));
3860 moveNextCell(cur, entry_from);
3862 movePrevCell(cur, entry_from);
3863 // if we're exiting the table, call the appropriate FINISHED lfun
3864 if (sl == cur.top())
3865 cmd = FuncRequest(finish_lfun);
3872 case LFUN_DOWN_SELECT:
3874 if (!(cur.selection() && cur.selIsMultiCell()))
3875 cell(cur.idx())->dispatch(cur, cmd);
3877 cur.dispatched(); // override the cell's decision
3878 if (sl == cur.top()) {
3879 // if our Text didn't do anything to the cursor
3880 // then we try to put the cursor into the cell below
3881 // setting also the right targetX.
3882 cur.selHandle(act == LFUN_DOWN_SELECT);
3883 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3884 cur.idx() = tabular.cellBelow(cur.idx());
3886 TextMetrics const & tm =
3887 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3888 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3889 cur.setCurrentFont();
3892 if (sl == cur.top()) {
3893 // we trick it to go to forward after leaving the
3895 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3898 if (cur.selIsMultiCell()) {
3899 cur.pit() = cur.lastpit();
3900 cur.pos() = cur.lastpos();
3901 cur.setCurrentFont();
3906 case LFUN_UP_SELECT:
3908 if (!(cur.selection() && cur.selIsMultiCell()))
3909 cell(cur.idx())->dispatch(cur, cmd);
3910 cur.dispatched(); // override the cell's decision
3911 if (sl == cur.top()) {
3912 // if our Text didn't do anything to the cursor
3913 // then we try to put the cursor into the cell above
3914 // setting also the right targetX.
3915 cur.selHandle(act == LFUN_UP_SELECT);
3916 if (tabular.cellRow(cur.idx()) != 0) {
3917 cur.idx() = tabular.cellAbove(cur.idx());
3918 cur.pit() = cur.lastpit();
3919 Text const * text = cell(cur.idx())->getText(0);
3920 TextMetrics const & tm = cur.bv().textMetrics(text);
3921 ParagraphMetrics const & pm =
3922 tm.parMetrics(cur.lastpit());
3923 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3924 cur.setCurrentFont();
3927 if (sl == cur.top()) {
3928 cmd = FuncRequest(LFUN_UP);
3931 if (cur.selIsMultiCell()) {
3933 cur.pos() = cur.lastpos();
3934 cur.setCurrentFont();
3939 // case LFUN_SCREEN_DOWN: {
3940 // //if (hasSelection())
3941 // // cur.selection() = false;
3942 // col_type const col = tabular.cellColumn(cur.idx());
3943 // int const t = cur.bv().top_y() + cur.bv().height();
3944 // if (t < yo() + tabular.getHeightOfTabular()) {
3945 // cur.bv().scrollDocView(t);
3946 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3948 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3955 // case LFUN_SCREEN_UP: {
3956 // //if (hasSelection())
3957 // // cur.selection() = false;
3958 // col_type const col = tabular.cellColumn(cur.idx());
3959 // int const t = cur.bv().top_y() + cur.bv().height();
3961 // cur.bv().scrollDocView(t);
3965 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3969 // cur.par() = cur.lastpar();
3970 // cur.pos() = cur.lastpos();
3974 case LFUN_LAYOUT_TABULAR:
3975 cur.bv().showDialog("tabular");
3978 case LFUN_INSET_MODIFY:
3979 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3983 // insert file functions
3984 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3985 case LFUN_FILE_INSERT_PLAINTEXT:
3987 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3988 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3989 FileName(to_utf8(cmd.argument())));
3992 cur.recordUndoInset(INSERT_UNDO);
3993 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3994 // content has been replaced,
3995 // so cursor might be invalid
3996 cur.pos() = cur.lastpos();
3997 cur.pit() = cur.lastpit();
3998 bvcur.setCursor(cur);
4005 if (cur.selIsMultiCell()) {
4006 if (copySelection(cur)) {
4007 cur.recordUndoInset(DELETE_UNDO);
4011 cell(cur.idx())->dispatch(cur, cmd);
4014 case LFUN_SELF_INSERT:
4015 if (cur.selIsMultiCell()) {
4016 cur.recordUndoInset(DELETE_UNDO);
4019 cell(cur.idx())->dispatch(cur, cmd);
4022 case LFUN_CHAR_DELETE_BACKWARD:
4023 case LFUN_CHAR_DELETE_FORWARD:
4024 if (cur.selIsMultiCell()) {
4025 cur.recordUndoInset(DELETE_UNDO);
4028 cell(cur.idx())->dispatch(cur, cmd);
4032 if (!cur.selection())
4034 if (cur.selIsMultiCell()) {
4038 cell(cur.idx())->dispatch(cur, cmd);
4041 case LFUN_CLIPBOARD_PASTE:
4042 case LFUN_PRIMARY_SELECTION_PASTE: {
4043 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4044 theClipboard().getAsText() :
4045 theSelection().get();
4048 // pass to InsertPlaintextString, but
4049 // only if we have multi-cell content
4050 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4051 cur.recordUndoInset(INSERT_UNDO);
4052 if (insertPlaintextString(cur.bv(), clip, false)) {
4053 // content has been replaced,
4054 // so cursor might be invalid
4055 cur.pos() = cur.lastpos();
4056 cur.pit() = cur.lastpit();
4057 bvcur.setCursor(cur);
4061 // Let the cell handle normal text
4062 cell(cur.idx())->dispatch(cur, cmd);
4067 if (!tabularStackDirty()) {
4068 if (!cur.selIsMultiCell())
4069 cell(cur.idx())->dispatch(cur, cmd);
4072 if (theClipboard().isInternal() ||
4073 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4074 cur.recordUndoInset(INSERT_UNDO);
4075 pasteClipboard(cur);
4079 case LFUN_FONT_EMPH:
4080 case LFUN_FONT_BOLD:
4081 case LFUN_FONT_BOLDSYMBOL:
4082 case LFUN_FONT_ROMAN:
4083 case LFUN_FONT_NOUN:
4084 case LFUN_FONT_ITAL:
4085 case LFUN_FONT_FRAK:
4086 case LFUN_FONT_TYPEWRITER:
4087 case LFUN_FONT_SANS:
4088 case LFUN_TEXTSTYLE_APPLY:
4089 case LFUN_TEXTSTYLE_UPDATE:
4090 case LFUN_FONT_SIZE:
4091 case LFUN_FONT_UNDERLINE:
4092 case LFUN_FONT_STRIKEOUT:
4093 case LFUN_FONT_UULINE:
4094 case LFUN_FONT_UWAVE:
4096 case LFUN_WORD_CAPITALIZE:
4097 case LFUN_WORD_UPCASE:
4098 case LFUN_WORD_LOWCASE:
4099 case LFUN_CHARS_TRANSPOSE:
4100 if (cur.selIsMultiCell()) {
4103 getSelection(cur, rs, re, cs, ce);
4104 Cursor tmpcur = cur;
4105 for (row_type r = rs; r <= re; ++r) {
4106 for (col_type c = cs; c <= ce; ++c) {
4107 // cursor follows cell:
4108 tmpcur.idx() = tabular.cellIndex(r, c);
4109 // select this cell only:
4112 tmpcur.resetAnchor();
4113 tmpcur.pit() = tmpcur.lastpit();
4114 tmpcur.pos() = tmpcur.top().lastpos();
4115 tmpcur.setCursor(tmpcur);
4116 tmpcur.setSelection();
4117 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4122 cell(cur.idx())->dispatch(cur, cmd);
4126 case LFUN_INSET_SETTINGS:
4127 // relay this lfun to Inset, not to the cell.
4128 Inset::doDispatch(cur, cmd);
4132 // we try to handle this event in the insets dispatch function.
4133 cell(cur.idx())->dispatch(cur, cmd);
4139 // function sets an object as defined in func_status.h:
4140 // states OK, Unknown, Disabled, On, Off.
4141 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4142 FuncStatus & status) const
4144 switch (cmd.action()) {
4145 case LFUN_INSET_MODIFY: {
4146 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4149 string const s = cmd.getArg(1);
4150 // FIXME: We only check for the very first argument...
4151 int action = Tabular::LAST_ACTION;
4153 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4154 if (tabularFeature[i].feature == s) {
4155 action = tabularFeature[i].action;
4159 if (action == Tabular::LAST_ACTION) {
4161 status.setUnknown(true);
4165 string const argument = cmd.getLongArg(2);
4167 row_type sel_row_start = 0;
4168 row_type sel_row_end = 0;
4169 col_type sel_col_start = 0;
4170 col_type sel_col_end = 0;
4171 Tabular::ltType dummyltt;
4174 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4177 case Tabular::SET_PWIDTH:
4178 case Tabular::SET_MPWIDTH:
4179 case Tabular::SET_SPECIAL_COLUMN:
4180 case Tabular::SET_SPECIAL_MULTICOLUMN:
4181 case Tabular::SET_SPECIAL_MULTIROW:
4182 case Tabular::APPEND_ROW:
4183 case Tabular::APPEND_COLUMN:
4184 case Tabular::DELETE_ROW:
4185 case Tabular::DELETE_COLUMN:
4186 case Tabular::COPY_ROW:
4187 case Tabular::COPY_COLUMN:
4188 case Tabular::SET_TOP_SPACE:
4189 case Tabular::SET_BOTTOM_SPACE:
4190 case Tabular::SET_INTERLINE_SPACE:
4194 case Tabular::SET_DECIMAL_POINT:
4196 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4199 case Tabular::MULTICOLUMN:
4200 // If a row is set as longtable caption, it must not be allowed
4201 // to unset that this row is a multicolumn.
4202 status.setEnabled(sel_row_start == sel_row_end
4203 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4204 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4207 case Tabular::MULTIROW:
4208 // If a row is set as longtable caption, it must not be allowed
4209 // to unset that this row is a multirow.
4210 status.setEnabled(sel_col_start == sel_col_end
4211 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4212 status.setOnOff(tabular.isMultiRow(cur.idx()));
4215 case Tabular::SET_ALL_LINES:
4216 case Tabular::UNSET_ALL_LINES:
4217 case Tabular::SET_BORDER_LINES:
4218 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4221 case Tabular::SET_LINE_TOP:
4222 case Tabular::SET_LINE_BOTTOM:
4223 case Tabular::SET_LINE_LEFT:
4224 case Tabular::SET_LINE_RIGHT:
4225 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4228 case Tabular::TOGGLE_LINE_TOP:
4229 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4230 status.setOnOff(tabular.topLine(cur.idx()));
4233 case Tabular::TOGGLE_LINE_BOTTOM:
4234 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4235 status.setOnOff(tabular.bottomLine(cur.idx()));
4238 case Tabular::TOGGLE_LINE_LEFT:
4239 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4240 status.setOnOff(tabular.leftLine(cur.idx()));
4243 case Tabular::TOGGLE_LINE_RIGHT:
4244 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4245 status.setOnOff(tabular.rightLine(cur.idx()));
4248 case Tabular::M_ALIGN_LEFT:
4250 case Tabular::ALIGN_LEFT:
4251 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4252 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4255 case Tabular::M_ALIGN_RIGHT:
4257 case Tabular::ALIGN_RIGHT:
4258 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4259 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4262 case Tabular::M_ALIGN_CENTER:
4264 case Tabular::ALIGN_CENTER:
4265 status.setEnabled(!tabular.isMultiRow(cur.idx()));
4266 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4269 case Tabular::ALIGN_BLOCK:
4270 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4271 && !tabular.isMultiRow(cur.idx()));
4272 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4275 case Tabular::ALIGN_DECIMAL:
4276 status.setEnabled(!tabular.isMultiRow(cur.idx())
4277 && !tabular.isMultiColumn(cur.idx()));
4278 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4281 case Tabular::M_VALIGN_TOP:
4283 case Tabular::VALIGN_TOP:
4284 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4285 && !tabular.isMultiRow(cur.idx()));
4287 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4290 case Tabular::M_VALIGN_BOTTOM:
4292 case Tabular::VALIGN_BOTTOM:
4293 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4294 && !tabular.isMultiRow(cur.idx()));
4296 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4299 case Tabular::M_VALIGN_MIDDLE:
4301 case Tabular::VALIGN_MIDDLE:
4302 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4303 && !tabular.isMultiRow(cur.idx()));
4305 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4308 case Tabular::SET_LONGTABULAR:
4309 status.setOnOff(tabular.is_long_tabular);
4312 case Tabular::UNSET_LONGTABULAR:
4313 status.setOnOff(!tabular.is_long_tabular);
4316 case Tabular::TOGGLE_ROTATE_TABULAR:
4317 case Tabular::SET_ROTATE_TABULAR:
4318 status.setOnOff(tabular.rotate);
4321 case Tabular::TABULAR_VALIGN_TOP:
4322 status.setOnOff(tabular.tabular_valignment
4323 == Tabular::LYX_VALIGN_TOP);
4325 case Tabular::TABULAR_VALIGN_MIDDLE:
4326 status.setOnOff(tabular.tabular_valignment
4327 == Tabular::LYX_VALIGN_MIDDLE);
4329 case Tabular::TABULAR_VALIGN_BOTTOM:
4330 status.setOnOff(tabular.tabular_valignment
4331 == Tabular::LYX_VALIGN_BOTTOM);
4334 case Tabular::LONGTABULAR_ALIGN_LEFT:
4335 status.setOnOff(tabular.longtabular_alignment
4336 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4338 case Tabular::LONGTABULAR_ALIGN_CENTER:
4339 status.setOnOff(tabular.longtabular_alignment
4340 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4342 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4343 status.setOnOff(tabular.longtabular_alignment
4344 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4347 case Tabular::UNSET_ROTATE_TABULAR:
4348 status.setOnOff(!tabular.rotate);
4351 case Tabular::TOGGLE_ROTATE_CELL:
4352 case Tabular::SET_ROTATE_CELL:
4353 status.setOnOff(!oneCellHasRotationState(false,
4354 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4357 case Tabular::UNSET_ROTATE_CELL:
4358 status.setOnOff(!oneCellHasRotationState(true,
4359 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4362 case Tabular::SET_USEBOX:
4363 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4366 // every row can only be one thing:
4367 // either a footer or header or caption
4368 case Tabular::SET_LTFIRSTHEAD:
4369 status.setEnabled(sel_row_start == sel_row_end
4370 && !tabular.ltCaption(sel_row_start));
4371 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4374 case Tabular::UNSET_LTFIRSTHEAD:
4375 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4378 case Tabular::SET_LTHEAD:
4379 status.setEnabled(sel_row_start == sel_row_end
4380 && !tabular.ltCaption(sel_row_start));
4381 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4384 case Tabular::UNSET_LTHEAD:
4385 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4388 case Tabular::SET_LTFOOT:
4389 status.setEnabled(sel_row_start == sel_row_end
4390 && !tabular.ltCaption(sel_row_start));
4391 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4394 case Tabular::UNSET_LTFOOT:
4395 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4398 case Tabular::SET_LTLASTFOOT:
4399 status.setEnabled(sel_row_start == sel_row_end
4400 && !tabular.ltCaption(sel_row_start));
4401 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4404 case Tabular::UNSET_LTLASTFOOT:
4405 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4408 case Tabular::SET_LTNEWPAGE:
4409 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4412 // only one row can be the caption
4413 // and a multirow cannot be set as caption
4414 case Tabular::TOGGLE_LTCAPTION:
4415 status.setEnabled(sel_row_start == sel_row_end
4416 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4417 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4418 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4419 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4420 && (!tabular.haveLTCaption()
4421 || tabular.ltCaption(sel_row_start))
4422 && !tabular.isMultiRow(sel_row_start));
4423 status.setOnOff(tabular.ltCaption(sel_row_start));
4426 case Tabular::SET_BOOKTABS:
4427 status.setOnOff(tabular.use_booktabs);
4430 case Tabular::UNSET_BOOKTABS:
4431 status.setOnOff(!tabular.use_booktabs);
4436 status.setEnabled(false);
4442 // These are only enabled inside tabular
4443 case LFUN_CELL_BACKWARD:
4444 case LFUN_CELL_FORWARD:
4445 status.setEnabled(true);
4448 // disable these with multiple cells selected
4449 case LFUN_INSET_INSERT:
4450 case LFUN_TABULAR_INSERT:
4451 case LFUN_FLEX_INSERT:
4452 case LFUN_FLOAT_INSERT:
4453 case LFUN_FLOAT_WIDE_INSERT:
4454 case LFUN_FOOTNOTE_INSERT:
4455 case LFUN_MARGINALNOTE_INSERT:
4456 case LFUN_MATH_INSERT:
4457 case LFUN_MATH_MODE:
4458 case LFUN_MATH_MUTATE:
4459 case LFUN_MATH_DISPLAY:
4460 case LFUN_NOTE_INSERT:
4461 case LFUN_ARGUMENT_INSERT:
4462 case LFUN_BOX_INSERT:
4463 case LFUN_BRANCH_INSERT:
4464 case LFUN_PHANTOM_INSERT:
4465 case LFUN_WRAP_INSERT:
4466 case LFUN_PREVIEW_INSERT:
4467 case LFUN_ERT_INSERT: {
4468 if (cur.selIsMultiCell()) {
4469 status.setEnabled(false);
4472 return cell(cur.idx())->getStatus(cur, cmd, status);
4475 // disable in non-fixed-width cells
4476 case LFUN_NEWLINE_INSERT:
4477 case LFUN_BREAK_PARAGRAPH: {
4478 if (tabular.getPWidth(cur.idx()).zero()) {
4479 status.setEnabled(false);
4482 return cell(cur.idx())->getStatus(cur, cmd, status);
4485 case LFUN_NEWPAGE_INSERT:
4486 status.setEnabled(false);
4490 if (tabularStackDirty() && theClipboard().isInternal()) {
4491 if (cur.selIsMultiCell()) {
4494 getSelection(cur, rs, re, cs, ce);
4495 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4496 && paste_tabular->nrows() == re - rs + 1)
4497 status.setEnabled(true);
4499 status.setEnabled(false);
4500 status.message(_("Selection size should match clipboard content."));
4503 status.setEnabled(true);
4506 return cell(cur.idx())->getStatus(cur, cmd, status);
4508 case LFUN_INSET_SETTINGS:
4509 // relay this lfun to Inset, not to the cell.
4510 return Inset::getStatus(cur, cmd, status);
4513 // we try to handle this event in the insets dispatch function.
4514 return cell(cur.idx())->getStatus(cur, cmd, status);
4520 Inset::DisplayType InsetTabular::display() const
4522 if (tabular.is_long_tabular) {
4523 switch (tabular.longtabular_alignment) {
4524 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4526 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4528 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4538 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4540 return tabular.latex(os, runparams);
4544 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4546 os << '\n'; // output table on a new line
4547 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4548 tabular.plaintext(os, runparams, dp, false, 0);
4549 return PLAINTEXT_NEWLINE;
4553 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4558 // FIXME: Why not pass a proper DocIterator here?
4560 // if the table is inside a float it doesn't need the informaltable
4561 // wrapper. Search for it.
4562 for (master = owner(); master; master = master->owner())
4563 if (master->lyxCode() == FLOAT_CODE)
4568 os << "<informaltable>";
4571 ret += tabular.docbook(os, runparams);
4573 os << "</informaltable>";
4580 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4583 // It'd be better to be able to get this from an InsetLayout, but at present
4584 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4585 xs << html::StartTag("table");
4586 docstring ret = tabular.xhtml(xs, rp);
4587 xs << html::EndTag("table");
4592 void InsetTabular::validate(LaTeXFeatures & features) const
4594 tabular.validate(features);
4596 // It'd be better to be able to get this from an InsetLayout, but at present
4597 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4598 if (features.runparams().flavor == OutputParams::HTML)
4599 features.addPreambleSnippet("<style type=\"text/css\">\n"
4600 "table { border: 1px solid black; display: inline-block; }\n"
4601 "td { border: 1px solid black; padding: 0.5ex; }\n"
4606 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4608 return tabular.cellInset(idx);
4612 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4614 return tabular.cellInset(idx);
4618 void InsetTabular::cursorPos(BufferView const & bv,
4619 CursorSlice const & sl, bool boundary, int & x, int & y) const
4621 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4623 int const row = tabular.cellRow(sl.idx());
4624 int const col = tabular.cellColumn(sl.idx());
4626 // y offset correction
4627 for (int r = 0; r < row; ++r)
4628 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4629 + tabular.interRowSpace(r + 1);
4631 y += tabular.textVOffset(sl.idx());
4633 // x offset correction
4634 for (int c = 0; c < col; ++c)
4635 x += tabular.column_info[c].width;
4637 x += tabular.textHOffset(sl.idx());
4638 x += ADD_TO_TABULAR_WIDTH;
4643 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4647 Inset const & inset = *tabular.cellInset(cell);
4648 Point o = bv.coordCache().getInsets().xy(&inset);
4649 int const xbeg = o.x_ - tabular.textHOffset(cell);
4650 int const xend = xbeg + tabular.columnWidth(cell);
4651 row_type const row = tabular.cellRow(cell);
4652 int const ybeg = o.y_ - tabular.rowAscent(row)
4653 - tabular.interRowSpace(row);
4654 int const yend = ybeg + tabular.cellHeight(cell);
4666 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4667 // << " ybeg=" << ybeg << " yend=" << yend
4668 // << " xx=" << xx << " yy=" << yy
4669 // << " dist=" << xx + yy << endl;
4674 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4676 //lyxerr << "InsetTabular::editXY: " << this << endl;
4677 cur.setSelection(false);
4679 cur.idx() = getNearestCell(cur.bv(), x, y);
4681 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4685 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4687 cur.idx() = getNearestCell(cur.bv(), x, y);
4688 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4692 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4694 idx_type idx_min = 0;
4695 int dist_min = numeric_limits<int>::max();
4696 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4697 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4698 int const d = dist(bv, i, x, y);
4709 int InsetTabular::cellXPos(idx_type const cell) const
4711 col_type col = tabular.cellColumn(cell);
4713 for (col_type c = 0; c < col; ++c)
4714 lx += tabular.columnWidth(c);
4720 void InsetTabular::resetPos(Cursor & cur) const
4722 BufferView & bv = cur.bv();
4723 int const maxwidth = bv.workWidth();
4725 int const scx_old = scx_;
4726 int const i = cur.find(this);
4731 int const X2 = maxwidth;
4732 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4733 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4734 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4737 scx_ = X1 + 20 - x1;
4739 scx_ = X2 - 20 - x2;
4744 // only update if offset changed
4745 if (scx_ != scx_old)
4746 cur.updateFlags(Update::Force | Update::FitCursor);
4750 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4752 row_type const row = tabular.cellRow(cur.idx());
4753 col_type const col = tabular.cellColumn(cur.idx());
4755 if (isRightToLeft(cur)) {
4756 if (tabular.cellColumn(cur.idx()) == 0) {
4757 if (row == tabular.nrows() - 1)
4759 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4764 cur.idx() = tabular.getLastCellInRow(row - 1);
4766 cur.idx() = tabular.cellIndex(row, col - 1);
4769 if (tabular.isLastCell(cur.idx()))
4771 if (cur.idx() == tabular.getLastCellInRow(row))
4772 cur.idx() = tabular.cellIndex(row + 1, 0);
4774 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4775 cur.idx() = tabular.cellIndex(row, colnextcell);
4779 cur.boundary(false);
4781 if (cur.selIsMultiCell()) {
4782 cur.pit() = cur.lastpit();
4783 cur.pos() = cur.lastpos();
4791 // in visual mode, place cursor at extreme left or right
4793 switch(entry_from) {
4795 case ENTRY_DIRECTION_RIGHT:
4796 cur.posVisToRowExtremity(false /* !left */);
4798 case ENTRY_DIRECTION_LEFT:
4799 cur.posVisToRowExtremity(true /* left */);
4801 case ENTRY_DIRECTION_IGNORE:
4802 // nothing to do in this case
4806 cur.setCurrentFont();
4811 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4813 row_type const row = tabular.cellRow(cur.idx());
4814 col_type const col = tabular.cellColumn(cur.idx());
4816 if (isRightToLeft(cur)) {
4817 if (cur.idx() == tabular.getLastCellInRow(row)) {
4820 cur.idx() = tabular.getFirstCellInRow(row);
4821 cur.idx() = tabular.cellAbove(cur.idx());
4823 if (tabular.isLastCell(cur.idx()))
4825 if (cur.idx() == tabular.getLastCellInRow(row))
4826 cur.idx() = tabular.cellIndex(row + 1, 0);
4828 cur.idx() = tabular.cellIndex(row, col + 1);
4831 if (cur.idx() == 0) // first cell
4834 cur.idx() = tabular.getLastCellInRow(row - 1);
4836 cur.idx() = tabular.cellIndex(row, col - 1);
4839 if (cur.selIsMultiCell()) {
4840 cur.pit() = cur.lastpit();
4841 cur.pos() = cur.lastpos();
4846 cur.pit() = cur.lastpit();
4847 cur.pos() = cur.lastpos();
4849 // in visual mode, place cursor at extreme left or right
4851 switch(entry_from) {
4853 case ENTRY_DIRECTION_RIGHT:
4854 cur.posVisToRowExtremity(false /* !left */);
4856 case ENTRY_DIRECTION_LEFT:
4857 cur.posVisToRowExtremity(true /* left */);
4859 case ENTRY_DIRECTION_IGNORE:
4860 // nothing to do in this case
4864 cur.setCurrentFont();
4869 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4871 istringstream is(argument);
4874 if (insetCode(s) != TABULAR_CODE)
4878 size_t safe_guard = 0;
4883 if (safe_guard > 1000) {
4884 LYXERR0("parameter max count reached!");
4888 Tabular::Feature action = Tabular::LAST_ACTION;
4891 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4892 if (s != tabularFeature[i].feature)
4895 action = tabularFeature[i].action;
4898 if (action == Tabular::LAST_ACTION) {
4899 LYXERR0("Feature not found " << s);
4903 if (tabularFeature[i].need_value)
4905 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
4906 tabularFeatures(cur, action, val);
4912 static void checkLongtableSpecial(Tabular::ltType & ltt,
4913 string const & special, bool & flag)
4915 if (special == "dl_above") {
4918 } else if (special == "dl_below") {
4919 ltt.bottomDL = flag;
4921 } else if (special == "empty") {
4931 bool InsetTabular::oneCellHasRotationState(bool rotated,
4932 row_type row_start, row_type row_end,
4933 col_type col_start, col_type col_end) const
4935 for (row_type r = row_start; r <= row_end; ++r)
4936 for (col_type c = col_start; c <= col_end; ++c)
4937 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
4943 void InsetTabular::tabularFeatures(Cursor & cur,
4944 Tabular::Feature feature, string const & value)
4946 col_type sel_col_start;
4947 col_type sel_col_end;
4948 row_type sel_row_start;
4949 row_type sel_row_end;
4950 bool setLines = false;
4951 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4952 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4956 case Tabular::M_ALIGN_LEFT:
4957 case Tabular::ALIGN_LEFT:
4958 setAlign = LYX_ALIGN_LEFT;
4961 case Tabular::M_ALIGN_RIGHT:
4962 case Tabular::ALIGN_RIGHT:
4963 setAlign = LYX_ALIGN_RIGHT;
4966 case Tabular::M_ALIGN_CENTER:
4967 case Tabular::ALIGN_CENTER:
4968 setAlign = LYX_ALIGN_CENTER;
4971 case Tabular::ALIGN_BLOCK:
4972 setAlign = LYX_ALIGN_BLOCK;
4975 case Tabular::ALIGN_DECIMAL:
4976 setAlign = LYX_ALIGN_DECIMAL;
4979 case Tabular::M_VALIGN_TOP:
4980 case Tabular::VALIGN_TOP:
4981 setVAlign = Tabular::LYX_VALIGN_TOP;
4984 case Tabular::M_VALIGN_BOTTOM:
4985 case Tabular::VALIGN_BOTTOM:
4986 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4989 case Tabular::M_VALIGN_MIDDLE:
4990 case Tabular::VALIGN_MIDDLE:
4991 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4998 cur.recordUndoInset(ATOMIC_UNDO);
5000 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5001 row_type const row = tabular.cellRow(cur.idx());
5002 col_type const column = tabular.cellColumn(cur.idx());
5004 Tabular::ltType ltt;
5008 case Tabular::SET_PWIDTH: {
5009 Length const len(value);
5010 tabular.setColumnPWidth(cur, cur.idx(), len);
5012 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5013 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5017 case Tabular::SET_MPWIDTH:
5018 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5021 case Tabular::SET_SPECIAL_COLUMN:
5022 case Tabular::SET_SPECIAL_MULTICOLUMN:
5023 if (value == "none")
5024 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5026 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5029 case Tabular::SET_SPECIAL_MULTIROW:
5033 case Tabular::APPEND_ROW:
5034 // append the row into the tabular
5035 tabular.appendRow(cur.idx());
5038 case Tabular::APPEND_COLUMN:
5039 // append the column into the tabular
5040 tabular.appendColumn(cur.idx());
5041 cur.idx() = tabular.cellIndex(row, column);
5044 case Tabular::DELETE_ROW:
5045 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5046 tabular.deleteRow(sel_row_start);
5047 if (sel_row_start >= tabular.nrows())
5049 cur.idx() = tabular.cellIndex(sel_row_start, column);
5052 cur.setSelection(false);
5055 case Tabular::DELETE_COLUMN:
5056 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5057 tabular.deleteColumn(sel_col_start);
5058 if (sel_col_start >= tabular.ncols())
5060 cur.idx() = tabular.cellIndex(row, sel_col_start);
5063 cur.setSelection(false);
5066 case Tabular::COPY_ROW:
5067 tabular.copyRow(row);
5070 case Tabular::COPY_COLUMN:
5071 tabular.copyColumn(column);
5072 cur.idx() = tabular.cellIndex(row, column);
5075 case Tabular::SET_LINE_TOP:
5076 case Tabular::TOGGLE_LINE_TOP: {
5077 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5078 ? (value == "true") : !tabular.topLine(cur.idx());
5079 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5080 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5081 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5085 case Tabular::SET_LINE_BOTTOM:
5086 case Tabular::TOGGLE_LINE_BOTTOM: {
5087 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5088 ? (value == "true") : !tabular.bottomLine(cur.idx());
5089 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5090 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5091 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5095 case Tabular::SET_LINE_LEFT:
5096 case Tabular::TOGGLE_LINE_LEFT: {
5097 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5098 ? (value == "true") : !tabular.leftLine(cur.idx());
5099 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5100 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5101 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5105 case Tabular::SET_LINE_RIGHT:
5106 case Tabular::TOGGLE_LINE_RIGHT: {
5107 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5108 ? (value == "true") : !tabular.rightLine(cur.idx());
5109 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5110 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5111 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5115 case Tabular::M_ALIGN_LEFT:
5116 case Tabular::M_ALIGN_RIGHT:
5117 case Tabular::M_ALIGN_CENTER:
5119 case Tabular::ALIGN_LEFT:
5120 case Tabular::ALIGN_RIGHT:
5121 case Tabular::ALIGN_CENTER:
5122 case Tabular::ALIGN_BLOCK:
5123 case Tabular::ALIGN_DECIMAL:
5124 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5125 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5126 tabular.setAlignment(tabular.cellIndex(r, c), setAlign, flag);
5129 case Tabular::M_VALIGN_TOP:
5130 case Tabular::M_VALIGN_BOTTOM:
5131 case Tabular::M_VALIGN_MIDDLE:
5133 case Tabular::VALIGN_TOP:
5134 case Tabular::VALIGN_BOTTOM:
5135 case Tabular::VALIGN_MIDDLE:
5136 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5137 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5138 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5141 case Tabular::MULTICOLUMN: {
5142 if (!cur.selection()) {
5143 // just multicol for one single cell
5144 // check whether we are completely in a multicol
5145 if (tabular.isMultiColumn(cur.idx()))
5146 tabular.unsetMultiColumn(cur.idx());
5148 tabular.setMultiColumn(cur.idx(), 1);
5151 // we have a selection so this means we just add all this
5152 // cells to form a multicolumn cell
5153 idx_type const s_start = cur.selBegin().idx();
5154 row_type const col_start = tabular.cellColumn(s_start);
5155 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5156 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5159 cur.setSelection(false);
5163 case Tabular::MULTIROW: {
5164 if (!cur.selection()) {
5165 // just multirow for one single cell
5166 // check whether we are completely in a multirow
5167 if (tabular.isMultiRow(cur.idx()))
5168 tabular.unsetMultiRow(cur.idx());
5170 tabular.setMultiRow(cur.idx(), 1);
5173 // we have a selection so this means we just add all this
5174 // cells to form a multirow cell
5175 idx_type const s_start = cur.selBegin().idx();
5176 row_type const row_start = tabular.cellRow(s_start);
5177 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5178 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5181 cur.setSelection(false);
5185 case Tabular::SET_ALL_LINES:
5187 case Tabular::UNSET_ALL_LINES:
5188 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5189 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5190 idx_type const cell = tabular.cellIndex(r, c);
5191 tabular.setTopLine(cell, setLines);
5192 tabular.setBottomLine(cell, setLines);
5193 tabular.setRightLine(cell, setLines);
5194 tabular.setLeftLine(cell, setLines);
5198 case Tabular::SET_BORDER_LINES:
5199 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5200 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5201 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5203 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5204 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5205 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5209 case Tabular::SET_LONGTABULAR:
5210 tabular.is_long_tabular = true;
5213 case Tabular::UNSET_LONGTABULAR:
5214 for (row_type r = 0; r < tabular.nrows(); ++r) {
5215 if (tabular.ltCaption(r)) {
5216 cur.idx() = tabular.cellIndex(r, 0);
5219 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5222 tabular.is_long_tabular = false;
5225 case Tabular::SET_ROTATE_TABULAR:
5226 tabular.rotate = true;
5229 case Tabular::UNSET_ROTATE_TABULAR:
5230 tabular.rotate = false;
5233 case Tabular::TOGGLE_ROTATE_TABULAR:
5234 tabular.rotate = !tabular.rotate;
5237 case Tabular::TABULAR_VALIGN_TOP:
5238 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5241 case Tabular::TABULAR_VALIGN_MIDDLE:
5242 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5245 case Tabular::TABULAR_VALIGN_BOTTOM:
5246 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5249 case Tabular::LONGTABULAR_ALIGN_LEFT:
5250 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5253 case Tabular::LONGTABULAR_ALIGN_CENTER:
5254 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5257 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5258 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5263 case Tabular::SET_ROTATE_CELL:
5264 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5265 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5266 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5269 case Tabular::UNSET_ROTATE_CELL:
5270 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5271 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5272 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5275 case Tabular::TOGGLE_ROTATE_CELL:
5277 bool oneNotRotated = oneCellHasRotationState(false,
5278 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5280 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5281 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5282 tabular.setRotateCell(tabular.cellIndex(r, c),
5287 case Tabular::SET_USEBOX: {
5288 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5289 if (val == tabular.getUsebox(cur.idx()))
5290 val = Tabular::BOX_NONE;
5291 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5292 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5293 tabular.setUsebox(tabular.cellIndex(r, c), val);
5297 case Tabular::UNSET_LTFIRSTHEAD:
5299 case Tabular::SET_LTFIRSTHEAD:
5300 tabular.getRowOfLTFirstHead(row, ltt);
5301 checkLongtableSpecial(ltt, value, flag);
5302 tabular.setLTHead(row, flag, ltt, true);
5305 case Tabular::UNSET_LTHEAD:
5307 case Tabular::SET_LTHEAD:
5308 tabular.getRowOfLTHead(row, ltt);
5309 checkLongtableSpecial(ltt, value, flag);
5310 tabular.setLTHead(row, flag, ltt, false);
5313 case Tabular::UNSET_LTFOOT:
5315 case Tabular::SET_LTFOOT:
5316 tabular.getRowOfLTFoot(row, ltt);
5317 checkLongtableSpecial(ltt, value, flag);
5318 tabular.setLTFoot(row, flag, ltt, false);
5321 case Tabular::UNSET_LTLASTFOOT:
5323 case Tabular::SET_LTLASTFOOT:
5324 tabular.getRowOfLTLastFoot(row, ltt);
5325 checkLongtableSpecial(ltt, value, flag);
5326 tabular.setLTFoot(row, flag, ltt, true);
5329 case Tabular::SET_LTNEWPAGE:
5330 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5333 case Tabular::TOGGLE_LTCAPTION: {
5334 bool const set = !tabular.ltCaption(row);
5335 cur.idx() = tabular.setLTCaption(row, set);
5338 cur.setSelection(false);
5341 // When a row is set as caption, then also insert
5342 // a caption. Otherwise the LaTeX output is broken.
5343 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5344 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5346 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5347 if (lyx::getStatus(fr).enabled())
5353 case Tabular::SET_BOOKTABS:
5354 tabular.use_booktabs = true;
5357 case Tabular::UNSET_BOOKTABS:
5358 tabular.use_booktabs = false;
5361 case Tabular::SET_TOP_SPACE: {
5363 if (value == "default")
5364 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5365 tabular.row_info[r].top_space_default = true;
5366 else if (value == "none")
5367 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5368 tabular.row_info[r].top_space_default = false;
5369 tabular.row_info[r].top_space = len;
5371 else if (isValidLength(value, &len))
5372 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5373 tabular.row_info[r].top_space_default = false;
5374 tabular.row_info[r].top_space = len;
5379 case Tabular::SET_BOTTOM_SPACE: {
5381 if (value == "default")
5382 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5383 tabular.row_info[r].bottom_space_default = true;
5384 else if (value == "none")
5385 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5386 tabular.row_info[r].bottom_space_default = false;
5387 tabular.row_info[r].bottom_space = len;
5389 else if (isValidLength(value, &len))
5390 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5391 tabular.row_info[r].bottom_space_default = false;
5392 tabular.row_info[r].bottom_space = len;
5397 case Tabular::SET_INTERLINE_SPACE: {
5399 if (value == "default")
5400 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5401 tabular.row_info[r].interline_space_default = true;
5402 else if (value == "none")
5403 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5404 tabular.row_info[r].interline_space_default = false;
5405 tabular.row_info[r].interline_space = len;
5407 else if (isValidLength(value, &len))
5408 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5409 tabular.row_info[r].interline_space_default = false;
5410 tabular.row_info[r].interline_space = len;
5415 case Tabular::SET_DECIMAL_POINT:
5416 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5417 tabular.column_info[c].decimal_point = from_utf8(value);
5420 // dummy stuff just to avoid warnings
5421 case Tabular::LAST_ACTION:
5427 bool InsetTabular::copySelection(Cursor & cur)
5429 if (!cur.selection())
5434 getSelection(cur, rs, re, cs, ce);
5436 paste_tabular.reset(new Tabular(tabular));
5438 for (row_type r = 0; r < rs; ++r)
5439 paste_tabular->deleteRow(0);
5441 row_type const rows = re - rs + 1;
5442 while (paste_tabular->nrows() > rows)
5443 paste_tabular->deleteRow(rows);
5445 for (col_type c = 0; c < cs; ++c)
5446 paste_tabular->deleteColumn(0);
5448 col_type const columns = ce - cs + 1;
5449 while (paste_tabular->ncols() > columns)
5450 paste_tabular->deleteColumn(columns);
5452 paste_tabular->setBuffer(tabular.buffer());
5454 odocstringstream os;
5455 OutputParams const runparams(0);
5456 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5457 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5458 cap::copySelection(cur, os.str());
5460 // mark tabular stack dirty
5461 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5462 // when we (hopefully) have a one-for-all paste mechanism.
5463 // This must be called after cap::copySelection.
5464 dirtyTabularStack(true);
5470 bool InsetTabular::pasteClipboard(Cursor & cur)
5474 col_type actcol = tabular.cellColumn(cur.idx());
5475 row_type actrow = tabular.cellRow(cur.idx());
5477 if (cur.selIsMultiCell()) {
5480 getSelection(cur, actrow, re, actcol, ce);
5483 for (row_type r1 = 0, r2 = actrow;
5484 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5486 for (col_type c1 = 0, c2 = actcol;
5487 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5489 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5490 tabular.isPartOfMultiColumn(r2, c2))
5492 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5496 if (tabular.isPartOfMultiColumn(r2, c2)) {
5500 shared_ptr<InsetTableCell> inset(
5501 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5502 tabular.setCellInset(r2, c2, inset);
5503 // FIXME?: why do we need to do this explicitly? (EL)
5504 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5506 // FIXME: change tracking (MG)
5507 inset->setChange(Change(buffer().params().trackChanges ?
5508 Change::INSERTED : Change::UNCHANGED));
5516 void InsetTabular::cutSelection(Cursor & cur)
5518 if (!cur.selection())
5523 getSelection(cur, rs, re, cs, ce);
5524 for (row_type r = rs; r <= re; ++r) {
5525 for (col_type c = cs; c <= ce; ++c) {
5526 shared_ptr<InsetTableCell> t
5527 = cell(tabular.cellIndex(r, c));
5528 if (buffer().params().trackChanges)
5529 // FIXME: Change tracking (MG)
5530 t->setChange(Change(Change::DELETED));
5536 // cursor position might be invalid now
5537 if (cur.pit() > cur.lastpit())
5538 cur.pit() = cur.lastpit();
5539 if (cur.pos() > cur.lastpos())
5540 cur.pos() = cur.lastpos();
5541 cur.clearSelection();
5545 bool InsetTabular::isRightToLeft(Cursor & cur) const
5547 LASSERT(cur.depth() > 1, /**/);
5548 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5549 pos_type const parentpos = cur[cur.depth() - 2].pos();
5550 return parentpar.getFontSettings(buffer().params(),
5551 parentpos).language()->rightToLeft();
5555 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5558 LASSERT(stidx <= enidx, return docstring());
5560 col_type const col1 = tabular.cellColumn(stidx);
5561 col_type const col2 = tabular.cellColumn(enidx);
5562 row_type const row1 = tabular.cellRow(stidx);
5563 row_type const row2 = tabular.cellRow(enidx);
5565 for (col_type col = col1; col <= col2; col++)
5566 for (row_type row = row1; row <= row2; row++) {
5571 retval += tabular.cellInset(row, col)->asString(intoInsets);
5577 void InsetTabular::getSelection(Cursor & cur,
5578 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5580 CursorSlice const & beg = cur.selBegin();
5581 CursorSlice const & end = cur.selEnd();
5582 cs = tabular.cellColumn(beg.idx());
5583 ce = tabular.cellColumn(end.idx());
5587 rs = tabular.cellRow(beg.idx());
5588 re = tabular.cellRow(end.idx());
5594 Text * InsetTabular::getText(int idx) const
5596 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5600 void InsetTabular::setChange(Change const & change)
5602 for (idx_type idx = 0; idx < nargs(); ++idx)
5603 cell(idx)->setChange(change);
5607 void InsetTabular::acceptChanges()
5609 for (idx_type idx = 0; idx < nargs(); ++idx)
5610 cell(idx)->acceptChanges();
5614 void InsetTabular::rejectChanges()
5616 for (idx_type idx = 0; idx < nargs(); ++idx)
5617 cell(idx)->rejectChanges();
5621 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5623 return tabular.getPWidth(cell).zero();
5627 bool InsetTabular::forcePlainLayout(idx_type cell) const
5629 return !tabular.getPWidth(cell).zero();
5633 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5636 if (buf.length() <= 0)
5641 col_type maxCols = 1;
5642 size_t const len = buf.length();
5646 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5654 maxCols = max(cols, maxCols);
5660 maxCols = max(cols, maxCols);
5666 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5667 loctab = paste_tabular.get();
5669 dirtyTabularStack(true);
5672 cell = bv.cursor().idx();
5673 ocol = tabular.cellColumn(cell);
5674 row = tabular.cellRow(cell);
5678 idx_type const cells = loctab->numberofcells;
5681 rows = loctab->nrows();
5682 col_type const columns = loctab->ncols();
5684 while (cell < cells && p < len && row < rows &&
5685 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5691 // we can only set this if we are not too far right
5692 if (cols < columns) {
5693 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5694 Font const font = bv.textMetrics(&inset->text()).
5696 inset->setText(buf.substr(op, p - op), font,
5697 buffer().params().trackChanges);
5703 // we can only set this if we are not too far right
5704 if (cols < columns) {
5705 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5706 Font const font = bv.textMetrics(&inset->text()).
5708 inset->setText(buf.substr(op, p - op), font,
5709 buffer().params().trackChanges);
5714 cell = loctab->cellIndex(row, cols);
5720 // check for the last cell if there is no trailing '\n'
5721 if (cell < cells && op < len) {
5722 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5723 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5724 inset->setText(buf.substr(op, len - op), font,
5725 buffer().params().trackChanges);
5731 void InsetTabular::addPreview(DocIterator const & inset_pos,
5732 PreviewLoader & loader) const
5734 DocIterator cell_pos = inset_pos;
5736 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5737 for (row_type r = 0; r < tabular.nrows(); ++r) {
5738 for (col_type c = 0; c < tabular.ncols(); ++c) {
5739 cell_pos.top().idx() = tabular.cellIndex(r, c);
5740 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5746 bool InsetTabular::completionSupported(Cursor const & cur) const
5748 Cursor const & bvCur = cur.bv().cursor();
5749 if (&bvCur.inset() != this)
5751 return cur.text()->completionSupported(cur);
5755 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5757 return completionSupported(cur);
5761 bool InsetTabular::automaticInlineCompletion() const
5763 return lyxrc.completion_inline_text;
5767 bool InsetTabular::automaticPopupCompletion() const
5769 return lyxrc.completion_popup_text;
5773 bool InsetTabular::showCompletionCursor() const
5775 return lyxrc.completion_cursor_text;
5779 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5781 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5785 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5787 if (!completionSupported(cur))
5789 return cur.text()->completionPrefix(cur);
5793 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5795 if (!completionSupported(cur))
5798 return cur.text()->insertCompletion(cur, s, finished);
5802 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5803 Dimension & dim) const
5805 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5806 tm.completionPosAndDim(cur, x, y, dim);
5810 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5812 istringstream data(in);
5814 lex.setStream(data);
5821 if (!lex || token != "tabular") {
5822 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5826 // This is part of the inset proper that is usually swallowed
5827 // by Buffer::readInset
5829 if (!lex || token != "Tabular") {
5830 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5838 string InsetTabular::params2string(InsetTabular const & inset)
5841 data << "tabular" << ' ';
5843 data << "\\end_inset\n";