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; // horizontal 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 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
169 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
170 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
172 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
173 { Tabular::SET_BOOKTABS, "set-booktabs", false },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
175 { Tabular::SET_TOP_SPACE, "set-top-space", true },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
178 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
179 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
180 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
181 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
182 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
183 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
184 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
185 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
186 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
187 { Tabular::LAST_ACTION, "", false }
191 string const tostr(LyXAlignment const & num)
196 case LYX_ALIGN_BLOCK:
200 case LYX_ALIGN_CENTER:
202 case LYX_ALIGN_RIGHT:
204 case LYX_ALIGN_LAYOUT:
206 case LYX_ALIGN_SPECIAL:
208 case LYX_ALIGN_DECIMAL:
215 string const tostr(Tabular::HAlignment const & num)
218 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
220 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
222 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
229 string const tostr(Tabular::VAlignment const & num)
232 case Tabular::LYX_VALIGN_TOP:
234 case Tabular::LYX_VALIGN_MIDDLE:
236 case Tabular::LYX_VALIGN_BOTTOM:
243 string const tostr(Tabular::BoxType const & num)
246 case Tabular::BOX_NONE:
248 case Tabular::BOX_PARBOX:
250 case Tabular::BOX_MINIPAGE:
257 // I would have liked a fromstr template a lot better. (Lgb)
258 bool string2type(string const str, LyXAlignment & num)
261 num = LYX_ALIGN_NONE;
262 else if (str == "block")
263 num = LYX_ALIGN_BLOCK;
264 else if (str == "left")
265 num = LYX_ALIGN_LEFT;
266 else if (str == "center")
267 num = LYX_ALIGN_CENTER;
268 else if (str == "right")
269 num = LYX_ALIGN_RIGHT;
270 else if (str == "decimal")
271 num = LYX_ALIGN_DECIMAL;
278 bool string2type(string const str, Tabular::HAlignment & num)
281 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
282 else if (str == "center" )
283 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
284 else if (str == "right")
285 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
292 bool string2type(string const str, Tabular::VAlignment & num)
295 num = Tabular::LYX_VALIGN_TOP;
296 else if (str == "middle" )
297 num = Tabular::LYX_VALIGN_MIDDLE;
298 else if (str == "bottom")
299 num = Tabular::LYX_VALIGN_BOTTOM;
306 bool string2type(string const str, Tabular::BoxType & num)
309 num = Tabular::BOX_NONE;
310 else if (str == "parbox")
311 num = Tabular::BOX_PARBOX;
312 else if (str == "minipage")
313 num = Tabular::BOX_MINIPAGE;
320 bool string2type(string const str, bool & num)
324 else if (str == "false")
332 bool getTokenValue(string const & str, char const * token, string & ret)
335 size_t token_length = strlen(token);
336 size_t pos = str.find(token);
338 if (pos == string::npos || pos + token_length + 1 >= str.length()
339 || str[pos + token_length] != '=')
341 pos += token_length + 1;
343 if (ch != '"' && ch != '\'') { // only read till next space
347 while (pos < str.length() - 1 && str[++pos] != ch)
354 bool getTokenValue(string const & str, char const * token, docstring & ret)
357 bool const success = getTokenValue(str, token, tmp);
358 ret = from_utf8(tmp);
363 bool getTokenValue(string const & str, char const * token, int & num)
367 if (!getTokenValue(str, token, tmp))
369 num = convert<int>(tmp);
374 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
377 return getTokenValue(str, token, tmp) && string2type(tmp, num);
381 bool getTokenValue(string const & str, char const * token,
382 Tabular::HAlignment & num)
385 return getTokenValue(str, token, tmp) && string2type(tmp, num);
389 bool getTokenValue(string const & str, char const * token,
390 Tabular::VAlignment & num)
393 return getTokenValue(str, token, tmp) && string2type(tmp, num);
397 bool getTokenValue(string const & str, char const * token,
398 Tabular::BoxType & num)
401 return getTokenValue(str, token, tmp) && string2type(tmp, num);
405 bool getTokenValue(string const & str, char const * token, bool & flag)
407 // set the flag always to false as this should be the default for bools
408 // not in the file-format.
411 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
415 bool getTokenValue(string const & str, char const * token, Length & len)
417 // set the length to be zero() as default as this it should be if not
418 // in the file format.
421 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
425 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
430 if (!getTokenValue(str, token, tmp))
432 if (tmp == "default") {
436 return isValidLength(tmp, &len);
440 void l_getline(istream & is, string & str)
443 while (str.empty()) {
445 if (!str.empty() && str[str.length() - 1] == '\r')
446 str.erase(str.length() - 1);
451 string const write_attribute(string const & name, T const & t)
453 string const s = tostr(t);
454 return s.empty() ? s : " " + name + "=\"" + s + "\"";
458 string const write_attribute(string const & name, string const & t)
460 return t.empty() ? t : " " + name + "=\"" + t + "\"";
465 string const write_attribute(string const & name, docstring const & t)
467 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
472 string const write_attribute(string const & name, bool const & b)
474 // we write only true attribute values so we remove a bit of the
475 // file format bloat for tabulars.
476 return b ? write_attribute(name, convert<string>(b)) : string();
481 string const write_attribute(string const & name, int const & i)
483 // we write only true attribute values so we remove a bit of the
484 // file format bloat for tabulars.
485 return i ? write_attribute(name, convert<string>(i)) : string();
490 string const write_attribute(string const & name, Tabular::idx_type const & i)
492 // we write only true attribute values so we remove a bit of the
493 // file format bloat for tabulars.
494 return i ? write_attribute(name, convert<string>(i)) : string();
499 string const write_attribute(string const & name, Length const & value)
501 // we write only the value if we really have one same reson as above.
502 return value.zero() ? string() : write_attribute(name, value.asString());
508 string const featureAsString(Tabular::Feature action)
510 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
511 if (tabularFeature[i].action == action)
512 return tabularFeature[i].feature;
518 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
520 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
521 for (; dit; dit.forwardChar())
522 if (dit.inTexted() && dit.depth() == 1
523 && dit.paragraph().find(align_d, false, false, dit.pos()))
530 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
532 InsetTableCell tail = InsetTableCell(head);
533 DocIterator const dit = separatorPos(&head, align_d);
536 pit_type const psize = head.paragraphs().front().size();
537 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
538 tail.paragraphs().front().eraseChars(0,
539 dit.pos() < psize ? dit.pos() + 1 : psize, false);
546 /////////////////////////////////////////////////////////////////////
550 /////////////////////////////////////////////////////////////////////
553 Tabular::CellData::CellData(Buffer * buf)
556 multicolumn(Tabular::CELL_NORMAL),
557 multirow(Tabular::CELL_NORMAL),
558 alignment(LYX_ALIGN_CENTER),
559 valignment(LYX_VALIGN_TOP),
569 inset(new InsetTableCell(buf))
571 inset->setBuffer(*buf);
575 Tabular::CellData::CellData(CellData const & cs)
578 multicolumn(cs.multicolumn),
579 multirow(cs.multirow),
580 mroffset(cs.mroffset),
581 alignment(cs.alignment),
582 valignment(cs.valignment),
583 decimal_hoffset(cs.decimal_hoffset),
584 decimal_width(cs.decimal_width),
586 top_line(cs.top_line),
587 bottom_line(cs.bottom_line),
588 left_line(cs.left_line),
589 right_line(cs.right_line),
592 align_special(cs.align_special),
594 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
598 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
604 void Tabular::CellData::swap(CellData & rhs)
606 std::swap(cellno, rhs.cellno);
607 std::swap(width, rhs.width);
608 std::swap(multicolumn, rhs.multicolumn);
609 std::swap(multirow, rhs.multirow);
610 std::swap(mroffset, rhs.mroffset);
611 std::swap(alignment, rhs.alignment);
612 std::swap(valignment, rhs.valignment);
613 std::swap(decimal_hoffset, rhs.decimal_hoffset);
614 std::swap(decimal_width, rhs.decimal_width);
615 std::swap(voffset, rhs.voffset);
616 std::swap(top_line, rhs.top_line);
617 std::swap(bottom_line, rhs.bottom_line);
618 std::swap(left_line, rhs.left_line);
619 std::swap(right_line, rhs.right_line);
620 std::swap(usebox, rhs.usebox);
621 std::swap(rotate, rhs.rotate);
622 std::swap(align_special, rhs.align_special);
623 p_width.swap(rhs.p_width);
624 inset.swap(rhs.inset);
628 Tabular::RowData::RowData()
631 top_space_default(false),
632 bottom_space_default(false),
633 interline_space_default(false),
643 Tabular::ColumnData::ColumnData()
644 : alignment(LYX_ALIGN_CENTER),
645 valignment(LYX_VALIGN_TOP),
651 Tabular::ltType::ltType()
658 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
660 init(buffer, rows_arg, columns_arg);
664 void Tabular::setBuffer(Buffer & buffer)
667 for (row_type i = 0; i < nrows(); ++i)
668 for (col_type j = 0; j < ncols(); ++j)
669 cell_info[i][j].inset->setBuffer(*buffer_);
673 // activates all lines and sets all widths to 0
674 void Tabular::init(Buffer * buf, row_type rows_arg,
675 col_type columns_arg)
678 row_info = row_vector(rows_arg);
679 column_info = column_vector(columns_arg);
680 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
681 row_info.reserve(10);
682 column_info.reserve(10);
683 cell_info.reserve(100);
685 is_long_tabular = false;
686 tabular_valignment = LYX_VALIGN_MIDDLE;
687 tabular_width = Length();
688 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
690 use_booktabs = false;
691 // set silly default lines
692 for (row_type r = 0; r < nrows(); ++r)
693 for (col_type c = 0; c < ncols(); ++c) {
694 cell_info[r][c].inset->setBuffer(*buffer_);
695 cell_info[r][c].top_line = true;
696 cell_info[r][c].left_line = true;
697 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
698 cell_info[r][c].right_line = c == ncols() - 1;
703 void Tabular::appendRow(idx_type const cell)
705 row_type const row = cellRow(cell);
707 row_info.insert(row_info.begin() + row + 1, RowData());
708 row_info[row + 1] = row_info[row];
710 cell_info.insert(cell_info.begin() + row + 1,
711 cell_vector(ncols(), CellData(buffer_)));
712 for (col_type c = 0; c < ncols(); ++c) {
713 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
714 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
716 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
720 for (col_type c = 0; c < ncols(); ++c) {
721 if (isPartOfMultiRow(row, c))
723 // inherit line settings
724 idx_type const i = cellIndex(row + 1, c);
725 idx_type const j = cellIndex(row, c);
726 setLeftLine(i, leftLine(j));
727 setRightLine(i, rightLine(j));
728 setTopLine(i, topLine(j));
729 if (topLine(j) && bottomLine(j)) {
730 setBottomLine(i, true);
731 setBottomLine(j, false);
733 // mark track changes
734 if (buffer().params().trackChanges)
735 cellInfo(i).inset->setChange(Change(Change::INSERTED));
740 void Tabular::deleteRow(row_type const row)
742 // Not allowed to delete last row
746 for (col_type c = 0; c < ncols(); ++c) {
747 // Care about multirow cells
748 if (row + 1 < nrows() &&
749 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
750 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
751 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
754 row_info.erase(row_info.begin() + row);
755 cell_info.erase(cell_info.begin() + row);
760 void Tabular::copyRow(row_type const row)
762 row_info.insert(row_info.begin() + row, row_info[row]);
763 cell_info.insert(cell_info.begin() + row, cell_info[row]);
765 if (buffer().params().trackChanges)
766 for (col_type c = 0; c < ncols(); ++c)
767 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
773 void Tabular::appendColumn(idx_type const cell)
775 col_type const c = cellColumn(cell);
777 column_info.insert(column_info.begin() + c + 1, ColumnData());
778 column_info[c + 1] = column_info[c];
780 for (row_type r = 0; r < nrows(); ++r) {
781 cell_info[r].insert(cell_info[r].begin() + c + 1,
783 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
784 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
786 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
789 for (row_type r = 0; r < nrows(); ++r) {
790 // inherit line settings
791 idx_type const i = cellIndex(r, c + 1);
792 idx_type const j = cellIndex(r, c);
793 setBottomLine(i, bottomLine(j));
794 setTopLine(i, topLine(j));
795 setLeftLine(i, leftLine(j));
796 if (rightLine(j) && rightLine(j)) {
797 setRightLine(i, true);
798 setRightLine(j, false);
800 if (buffer().params().trackChanges)
801 cellInfo(i).inset->setChange(Change(Change::INSERTED));
806 void Tabular::deleteColumn(col_type const col)
808 // Not allowed to delete last column
812 for (row_type r = 0; r < nrows(); ++r) {
813 // Care about multicolumn cells
814 if (col + 1 < ncols() &&
815 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
816 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
817 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
819 cell_info[r].erase(cell_info[r].begin() + col);
821 column_info.erase(column_info.begin() + col);
826 void Tabular::copyColumn(col_type const col)
828 BufferParams const & bp = buffer().params();
829 column_info.insert(column_info.begin() + col, column_info[col]);
831 for (row_type r = 0; r < nrows(); ++r) {
832 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
834 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
840 void Tabular::updateIndexes()
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))
850 if (isPartOfMultiRow(row, column))
851 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
853 cell_info[row][column].cellno = numberofcells - 1;
856 rowofcell.resize(numberofcells);
857 columnofcell.resize(numberofcells);
859 // reset column and row of cells and update their width and alignment
860 for (row_type row = 0; row < nrows(); ++row)
861 for (col_type column = 0; column < ncols(); ++column) {
862 if (isPartOfMultiColumn(row, column))
864 // columnofcell needs to be called before setting width and aligment
865 // multirow cells inherit the width from the column width
866 if (!isPartOfMultiRow(row, column)) {
867 columnofcell[i] = column;
870 setFixedWidth(row, column);
871 if (isPartOfMultiRow(row, column))
873 cell_info[row][column].inset->setContentAlignment(
874 getAlignment(cellIndex(row, column)));
880 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
883 for (col_type c = 0; c < ncols(); ++c)
884 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
890 bool Tabular::topLine(idx_type const cell) const
892 return cellInfo(cell).top_line;
896 bool Tabular::bottomLine(idx_type const cell) const
898 return cellInfo(cell).bottom_line;
902 bool Tabular::leftLine(idx_type cell) const
906 return cellInfo(cell).left_line;
910 bool Tabular::rightLine(idx_type cell) const
914 return cellInfo(cell).right_line;
918 int Tabular::interRowSpace(row_type row) const
920 if (!row || row >= nrows())
923 int const interline_space = row_info[row - 1].interline_space_default ?
925 row_info[row - 1].interline_space.inPixels(width());
926 if (rowTopLine(row) && rowBottomLine(row - 1))
927 return interline_space + WIDTH_OF_LINE;
928 return interline_space;
932 int Tabular::interColumnSpace(idx_type cell) const
934 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
935 if (rightLine(cell) && nextcol < ncols()
936 && leftLine(cellIndex(cellRow(cell), nextcol)))
937 return WIDTH_OF_LINE;
942 int Tabular::cellWidth(idx_type cell) const
945 col_type const span = columnSpan(cell);
946 col_type const col = cellColumn(cell);
947 for(col_type c = col; c < col + span ; ++c)
948 w += column_info[c].width;
953 int Tabular::cellHeight(idx_type cell) const
955 row_type const span = rowSpan(cell);
956 row_type const row = cellRow(cell);
958 for(row_type r = row; r < row + span ; ++r) {
959 h += rowAscent(r) + rowDescent(r);
960 if (r != row + span - 1)
961 h += interRowSpace(r + 1);
968 bool Tabular::updateColumnWidths()
970 vector<int> max_dwidth(ncols(), 0);
971 for(col_type c = 0; c < ncols(); ++c)
972 for(row_type r = 0; r < nrows(); ++r) {
973 idx_type const i = cellIndex(r, c);
974 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
975 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
979 // for each col get max of single col cells
980 for(col_type c = 0; c < ncols(); ++c) {
982 for(row_type r = 0; r < nrows(); ++r) {
983 idx_type const i = cellIndex(r, c);
984 if (columnSpan(i) == 1) {
985 if (getAlignment(i) == LYX_ALIGN_DECIMAL
986 && cell_info[r][c].decimal_width!=0)
987 new_width = max(new_width, cellInfo(i).width
988 + max_dwidth[c] - cellInfo(i).decimal_width);
990 new_width = max(new_width, cellInfo(i).width);
994 if (column_info[c].width != new_width) {
995 column_info[c].width = new_width;
999 // update col widths to fit merged cells
1000 for(col_type c = 0; c < ncols(); ++c)
1001 for(row_type r = 0; r < nrows(); ++r) {
1002 idx_type const i = cellIndex(r, c);
1003 int const span = columnSpan(i);
1004 if (span == 1 || c > cellColumn(i))
1008 for(col_type j = c; j < c + span ; ++j)
1009 old_width += column_info[j].width;
1011 if (cellInfo(i).width > old_width) {
1012 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1021 int Tabular::width() const
1024 for (col_type c = 0; c < ncols(); ++c)
1025 width += column_info[c].width;
1030 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1033 col_type const col = cellColumn(cell);
1034 // set alignment for the whole row if we are not in a multicolumn cell,
1035 // exclude possible multicolumn cells in the row
1036 if (!isMultiColumn(cell)) {
1037 for (row_type r = 0; r < nrows(); ++r) {
1038 // only if the column has no width the multirow inherits the
1039 // alignment of the column, otherwise it is left aligned
1040 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1041 && !isMultiColumn(cellIndex(r, col))) {
1042 cell_info[r][col].alignment = align;
1043 cell_info[r][col].inset->setContentAlignment(align);
1045 if ((isMultiRow(cellIndex(r, col)) && has_width)
1046 && !isMultiColumn(cellIndex(r, col))) {
1047 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1048 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1051 column_info[col].alignment = align;
1052 docstring & dpoint = column_info[col].decimal_point;
1053 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1054 dpoint = from_utf8(lyxrc.default_decimal_point);
1056 cellInfo(cell).alignment = align;
1057 cellInset(cell).get()->setContentAlignment(align);
1062 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1065 if (!isMultiColumn(cell) || onlycolumn)
1066 column_info[cellColumn(cell)].valignment = align;
1068 cellInfo(cell).valignment = align;
1075 * Allow line and paragraph breaks for fixed width cells or disallow them,
1076 * merge cell paragraphs and reset layout to standard for variable width
1079 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1081 inset->setAutoBreakRows(fixedWidth);
1082 inset->toggleFixedWidth(fixedWidth);
1086 // merge all paragraphs to one
1087 BufferParams const & bp = cur.bv().buffer().params();
1088 while (inset->paragraphs().size() > 1)
1089 mergeParagraph(bp, inset->paragraphs(), 0);
1093 // undo information has already been recorded
1094 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1095 bp.documentClass().plainLayoutName());
1102 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1103 Length const & width)
1105 col_type const c = cellColumn(cell);
1107 column_info[c].p_width = width;
1108 // reset the vertical alignment to top if the fixed with
1109 // is removed or zero because only fixed width columns can
1110 // have a vertical alignment
1111 if (column_info[c].p_width.zero())
1112 column_info[c].valignment = LYX_VALIGN_TOP;
1113 for (row_type r = 0; r < nrows(); ++r) {
1114 idx_type const cell = cellIndex(r, c);
1115 // because of multicolumns
1116 toggleFixedWidth(cur, cellInset(cell).get(),
1117 !getPWidth(cell).zero());
1119 // cur paragraph can become invalid after paragraphs were merged
1120 if (cur.pit() > cur.lastpit())
1121 cur.pit() = cur.lastpit();
1122 // cur position can become invalid after newlines were removed
1123 if (cur.pos() > cur.lastpos())
1124 cur.pos() = cur.lastpos();
1128 bool Tabular::setFixedWidth(row_type r, col_type c)
1130 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1131 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1132 || (multicol && !cell_info[r][c].p_width.zero());
1133 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1138 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1139 Length const & width)
1141 if (!isMultiColumn(cell))
1144 cellInfo(cell).p_width = width;
1145 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1146 // cur paragraph can become invalid after paragraphs were merged
1147 if (cur.pit() > cur.lastpit())
1148 cur.pit() = cur.lastpit();
1149 // cur position can become invalid after newlines were removed
1150 if (cur.pos() > cur.lastpos())
1151 cur.pos() = cur.lastpos();
1156 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1158 cellInfo(cell).mroffset = mroffset;
1163 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1164 Tabular::Feature what)
1166 if (what == SET_SPECIAL_MULTICOLUMN)
1167 cellInfo(cell).align_special = special;
1169 column_info[cellColumn(cell)].align_special = special;
1173 void Tabular::setTopLine(idx_type i, bool line)
1175 cellInfo(i).top_line = line;
1179 void Tabular::setBottomLine(idx_type i, bool line)
1181 cellInfo(i).bottom_line = line;
1185 void Tabular::setLeftLine(idx_type cell, bool line)
1187 cellInfo(cell).left_line = line;
1191 void Tabular::setRightLine(idx_type cell, bool line)
1193 cellInfo(cell).right_line = line;
1196 bool Tabular::rowTopLine(row_type r) const
1198 bool all_rows_set = true;
1199 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1200 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1201 return all_rows_set;
1205 bool Tabular::rowBottomLine(row_type r) const
1207 bool all_rows_set = true;
1208 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1209 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1210 return all_rows_set;
1214 bool Tabular::columnLeftLine(col_type c) const
1221 for (row_type r = 0; r < nrows(); ++r) {
1222 idx_type const i = cellIndex(r, c);
1223 if (c == cellColumn(i)) {
1225 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1226 if (cellInfo(i).left_line || right)
1230 return 2 * nrows_left >= total;
1234 bool Tabular::columnRightLine(col_type c) const
1239 int nrows_right = 0;
1241 for (row_type r = 0; r < nrows(); ++r) {
1242 idx_type i = cellIndex(r, c);
1243 if (c == cellColumn(i) + columnSpan(i) - 1) {
1245 bool left = (c + 1 < ncols()
1246 && cellInfo(cellIndex(r, c + 1)).left_line)
1247 || c + 1 == ncols();
1248 if (cellInfo(i).right_line && left)
1252 return 2 * nrows_right >= total;
1256 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1258 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1259 return cellInfo(cell).alignment;
1261 return column_info[cellColumn(cell)].alignment;
1266 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1268 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1269 return cellInfo(cell).valignment;
1270 return column_info[cellColumn(cell)].valignment;
1274 Length const Tabular::getPWidth(idx_type cell) const
1276 if (isMultiColumn(cell))
1277 return cellInfo(cell).p_width;
1278 return column_info[cellColumn(cell)].p_width;
1282 Length const Tabular::getMROffset(idx_type cell) const
1284 return cellInfo(cell).mroffset;
1288 int Tabular::textHOffset(idx_type cell) const
1290 // the LaTeX Way :-(
1291 int x = WIDTH_OF_LINE;
1293 int const w = cellWidth(cell) - cellInfo(cell).width;
1295 switch (getAlignment(cell)) {
1296 case LYX_ALIGN_CENTER:
1299 case LYX_ALIGN_RIGHT:
1302 case LYX_ALIGN_DECIMAL: {
1303 // we center when no decimal point
1304 if (cellInfo(cell).decimal_width == 0) {
1308 col_type const c = cellColumn(cell);
1309 int max_dhoffset = 0;
1310 for(row_type r = 0; r < row_info.size() ; ++r) {
1311 idx_type const i = cellIndex(r, c);
1312 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1313 && cellInfo(i).decimal_width != 0)
1314 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1316 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1319 // LYX_ALIGN_LEFT: nothing :-)
1327 int Tabular::textVOffset(idx_type cell) const
1329 int voffset = cellInfo(cell).voffset;
1330 if (isMultiRow(cell)) {
1331 row_type const row = cellRow(cell);
1332 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1338 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1341 idx_type const numcells = numberOfCellsInRow(row);
1342 // we check against numcells to make sure we do not crash if all the
1343 // cells are multirow (bug #7535), but in that case our return value
1344 // is really invalid, i.e., it is NOT the first cell in the row. but
1345 // i do not know what to do here. (rgh)
1346 while (c < numcells - 1
1347 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1349 return cell_info[row][c].cellno;
1353 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1355 col_type c = ncols() - 1;
1356 // of course we check against 0 so we don't crash. but we have the same
1357 // problem as in the previous routine: if all the cells are part of a
1358 // multirow or part of a multi column, then our return value is invalid.
1360 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1361 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1363 return cell_info[row][c].cellno;
1367 Tabular::row_type Tabular::cellRow(idx_type cell) const
1369 if (cell >= numberofcells)
1373 return rowofcell[cell];
1377 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1379 if (cell >= numberofcells)
1383 return columnofcell[cell];
1387 void Tabular::write(ostream & os) const
1391 << write_attribute("version", 3)
1392 << write_attribute("rows", nrows())
1393 << write_attribute("columns", ncols())
1395 // global longtable options
1397 << write_attribute("rotate", rotate)
1398 << write_attribute("booktabs", use_booktabs)
1399 << write_attribute("islongtable", is_long_tabular)
1400 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1401 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1402 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1403 << write_attribute("headTopDL", endhead.topDL)
1404 << write_attribute("headBottomDL", endhead.bottomDL)
1405 << write_attribute("footTopDL", endfoot.topDL)
1406 << write_attribute("footBottomDL", endfoot.bottomDL)
1407 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1408 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1409 << write_attribute("lastFootEmpty", endlastfoot.empty);
1410 // longtables cannot be aligned vertically
1411 if (!is_long_tabular) {
1412 os << write_attribute("tabularvalignment", tabular_valignment);
1413 os << write_attribute("tabularwidth", tabular_width);
1415 if (is_long_tabular)
1416 os << write_attribute("longtabularalignment",
1417 longtabular_alignment);
1419 for (col_type c = 0; c < ncols(); ++c) {
1421 << write_attribute("alignment", column_info[c].alignment);
1422 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1423 os << write_attribute("decimal_point", column_info[c].decimal_point);
1424 os << write_attribute("valignment", column_info[c].valignment)
1425 << write_attribute("width", column_info[c].p_width.asString())
1426 << write_attribute("special", column_info[c].align_special)
1429 for (row_type r = 0; r < nrows(); ++r) {
1430 static const string def("default");
1432 if (row_info[r].top_space_default)
1433 os << write_attribute("topspace", def);
1435 os << write_attribute("topspace", row_info[r].top_space);
1436 if (row_info[r].bottom_space_default)
1437 os << write_attribute("bottomspace", def);
1439 os << write_attribute("bottomspace", row_info[r].bottom_space);
1440 if (row_info[r].interline_space_default)
1441 os << write_attribute("interlinespace", def);
1443 os << write_attribute("interlinespace", row_info[r].interline_space);
1444 os << write_attribute("endhead", row_info[r].endhead)
1445 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1446 << write_attribute("endfoot", row_info[r].endfoot)
1447 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1448 << write_attribute("newpage", row_info[r].newpage)
1449 << write_attribute("caption", row_info[r].caption)
1451 for (col_type c = 0; c < ncols(); ++c) {
1453 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1454 << write_attribute("multirow", cell_info[r][c].multirow)
1455 << write_attribute("mroffset", cell_info[r][c].mroffset)
1456 << write_attribute("alignment", cell_info[r][c].alignment)
1457 << write_attribute("valignment", cell_info[r][c].valignment)
1458 << write_attribute("topline", cell_info[r][c].top_line)
1459 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1460 << write_attribute("leftline", cell_info[r][c].left_line)
1461 << write_attribute("rightline", cell_info[r][c].right_line)
1462 << write_attribute("rotate", cell_info[r][c].rotate)
1463 << write_attribute("usebox", cell_info[r][c].usebox)
1464 << write_attribute("width", cell_info[r][c].p_width)
1465 << write_attribute("special", cell_info[r][c].align_special)
1467 os << "\\begin_inset ";
1468 cell_info[r][c].inset->write(os);
1469 os << "\n\\end_inset\n"
1474 os << "</lyxtabular>\n";
1478 void Tabular::read(Lexer & lex)
1481 istream & is = lex.getStream();
1483 l_getline(is, line);
1484 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1485 LASSERT(false, /**/);
1490 if (!getTokenValue(line, "version", version))
1492 LASSERT(version >= 2, /**/);
1495 if (!getTokenValue(line, "rows", rows_arg))
1498 if (!getTokenValue(line, "columns", columns_arg))
1500 init(buffer_, rows_arg, columns_arg);
1501 l_getline(is, line);
1502 if (!prefixIs(line, "<features")) {
1503 lyxerr << "Wrong tabular format (expected <features ...> got"
1504 << line << ')' << endl;
1507 getTokenValue(line, "rotate", rotate);
1508 getTokenValue(line, "booktabs", use_booktabs);
1509 getTokenValue(line, "islongtable", is_long_tabular);
1510 getTokenValue(line, "tabularvalignment", tabular_valignment);
1511 getTokenValue(line, "tabularwidth", tabular_width);
1512 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1513 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1514 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1515 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1516 getTokenValue(line, "headTopDL", endhead.topDL);
1517 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1518 getTokenValue(line, "footTopDL", endfoot.topDL);
1519 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1520 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1521 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1522 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1524 for (col_type c = 0; c < ncols(); ++c) {
1526 if (!prefixIs(line,"<column")) {
1527 lyxerr << "Wrong tabular format (expected <column ...> got"
1528 << line << ')' << endl;
1531 getTokenValue(line, "alignment", column_info[c].alignment);
1532 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1533 getTokenValue(line, "valignment", column_info[c].valignment);
1534 getTokenValue(line, "width", column_info[c].p_width);
1535 getTokenValue(line, "special", column_info[c].align_special);
1538 for (row_type i = 0; i < nrows(); ++i) {
1539 l_getline(is, line);
1540 if (!prefixIs(line, "<row")) {
1541 lyxerr << "Wrong tabular format (expected <row ...> got"
1542 << line << ')' << endl;
1545 getTokenValue(line, "topspace", row_info[i].top_space,
1546 row_info[i].top_space_default);
1547 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1548 row_info[i].bottom_space_default);
1549 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1550 row_info[i].interline_space_default);
1551 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1552 getTokenValue(line, "endhead", row_info[i].endhead);
1553 getTokenValue(line, "endfoot", row_info[i].endfoot);
1554 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1555 getTokenValue(line, "newpage", row_info[i].newpage);
1556 getTokenValue(line, "caption", row_info[i].caption);
1557 for (col_type j = 0; j < ncols(); ++j) {
1558 l_getline(is, line);
1559 if (!prefixIs(line, "<cell")) {
1560 lyxerr << "Wrong tabular format (expected <cell ...> got"
1561 << line << ')' << endl;
1564 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1565 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1566 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1567 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1568 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1569 getTokenValue(line, "topline", cell_info[i][j].top_line);
1570 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1571 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1572 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1573 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1574 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1575 getTokenValue(line, "width", cell_info[i][j].p_width);
1577 getTokenValue(line, "special", cell_info[i][j].align_special);
1578 l_getline(is, line);
1579 if (prefixIs(line, "\\begin_inset")) {
1580 cell_info[i][j].inset->setBuffer(*buffer_);
1581 cell_info[i][j].inset->read(lex);
1582 l_getline(is, line);
1584 if (!prefixIs(line, "</cell>")) {
1585 lyxerr << "Wrong tabular format (expected </cell> got"
1586 << line << ')' << endl;
1590 l_getline(is, line);
1591 if (!prefixIs(line, "</row>")) {
1592 lyxerr << "Wrong tabular format (expected </row> got"
1593 << line << ')' << endl;
1597 while (!prefixIs(line, "</lyxtabular>")) {
1598 l_getline(is, line);
1604 bool Tabular::isMultiColumn(idx_type cell) const
1606 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1607 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1611 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1613 return cell_info[cellRow(cell)][cellColumn(cell)];
1617 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1618 bool const right_border)
1620 idx_type const col = cellColumn(cell);
1621 idx_type const row = cellRow(cell);
1622 for (idx_type i = 0; i < number; ++i)
1623 unsetMultiRow(cellIndex(row, col + i));
1625 // unsetting of multirow may have invalidated cell index
1626 cell = cellIndex(row, col);
1627 CellData & cs = cellInfo(cell);
1628 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1629 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1630 cs.alignment = column_info[col].alignment;
1632 setRightLine(cell, right_border);
1634 for (idx_type i = 1; i < number; ++i) {
1635 CellData & cs1 = cellInfo(cell + i);
1636 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1637 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1645 bool Tabular::isMultiRow(idx_type cell) const
1647 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1648 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1652 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1653 bool const bottom_border)
1655 idx_type const col = cellColumn(cell);
1656 idx_type const row = cellRow(cell);
1657 for (idx_type i = 0; i < number; ++i)
1658 unsetMultiColumn(cellIndex(row + i, col));
1660 // unsetting of multirow may have invalidated cell index
1661 cell = cellIndex(row, col);
1662 CellData & cs = cellInfo(cell);
1663 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1664 cs.valignment = LYX_VALIGN_MIDDLE;
1665 // the horizontal alignment of multirow cells can only
1666 // be changed for the whole table row,
1667 // support changing this only for the multirow cell can be done via
1669 // this feature would be a fileformat change
1670 // until LyX supports this, use the deault alignment of multirow
1672 cs.alignment = LYX_ALIGN_LEFT;
1674 // set the bottom line of the last selected cell
1675 setBottomLine(cell, bottom_border);
1677 for (idx_type i = 1; i < number; ++i) {
1678 CellData & cs1 = cell_info[row + i][col];
1679 cs1.multirow = CELL_PART_OF_MULTIROW;
1680 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1688 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1690 row_type const row = cellRow(cell);
1691 col_type const col = cellColumn(cell);
1693 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1700 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1702 col_type const column = cellColumn(cell);
1703 col_type row = cellRow(cell) + 1;
1704 while (row < nrows() && isPartOfMultiRow(row, column))
1707 return row - cellRow(cell);
1711 void Tabular::unsetMultiColumn(idx_type cell)
1713 if (!isMultiColumn(cell))
1716 row_type const row = cellRow(cell);
1717 col_type const col = cellColumn(cell);
1718 row_type const span = columnSpan(cell);
1719 for (col_type c = 0; c < span; ++c) {
1720 // in the table dialog the lines are set in every case
1721 // when unsetting a multicolumn this leads to an additional right
1722 // line for every cell that was part of the former multicolumn cell,
1723 // except if the cell is in the last column
1724 // therefore remove this line
1725 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1726 && (col + c) < (col + span - 1))
1727 cell_info[row][col + c].right_line = false;
1728 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1734 void Tabular::unsetMultiRow(idx_type cell)
1736 if (!isMultiRow(cell))
1739 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1740 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1741 row_type const row = cellRow(cell);
1742 col_type const col = cellColumn(cell);
1743 row_type const span = rowSpan(cell);
1744 for (row_type r = 0; r < span; ++r)
1745 cell_info[row + r][col].multirow = CELL_NORMAL;
1750 void Tabular::setRotateCell(idx_type cell, bool flag)
1752 cellInfo(cell).rotate = flag;
1756 bool Tabular::getRotateCell(idx_type cell) const
1758 return cellInfo(cell).rotate;
1762 bool Tabular::needRotating() const
1766 for (row_type r = 0; r < nrows(); ++r)
1767 for (col_type c = 0; c < ncols(); ++c)
1768 if (cell_info[r][c].rotate)
1774 bool Tabular::isLastCell(idx_type cell) const
1776 if (cell + 1 < numberofcells)
1782 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1784 if (cellRow(cell) == 0)
1787 col_type const col = cellColumn(cell);
1788 row_type r = cellRow(cell) - 1;
1789 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1792 return cell_info[r][col].cellno;
1796 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1798 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1799 if (nextrow < nrows())
1800 return cell_info[nextrow][cellColumn(cell)].cellno;
1805 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1807 LASSERT(column != npos && column < ncols()
1808 && row != npos && row < nrows(), /**/);
1809 return cell_info[row][column].cellno;
1813 void Tabular::setUsebox(idx_type cell, BoxType type)
1815 cellInfo(cell).usebox = type;
1819 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1820 // adds line breaks, see bug 4886.
1821 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1823 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1824 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1826 if (cellInfo(cell).usebox > 1)
1827 return cellInfo(cell).usebox;
1828 return useParbox(cell);
1833 // This are functions used for the longtable support
1835 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1841 row_info[row].endfirsthead = flag;
1845 row_info[row].endhead = flag;
1850 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1853 hd.set = haveLTHead();
1854 return row_info[row].endhead;
1858 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1861 hd.set = haveLTFirstHead();
1862 return row_info[row].endfirsthead;
1866 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1872 row_info[row].endlastfoot = flag;
1876 row_info[row].endfoot = flag;
1881 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1884 fd.set = haveLTFoot();
1885 return row_info[row].endfoot;
1889 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1892 fd.set = haveLTLastFoot();
1893 return row_info[row].endlastfoot;
1897 void Tabular::setLTNewPage(row_type row, bool what)
1899 row_info[row].newpage = what;
1903 bool Tabular::getLTNewPage(row_type row) const
1905 return row_info[row].newpage;
1909 bool Tabular::haveLTHead(bool withcaptions) const
1911 if (!is_long_tabular)
1913 for (row_type i = 0; i < nrows(); ++i)
1914 if (row_info[i].endhead &&
1915 (withcaptions || !row_info[i].caption))
1921 bool Tabular::haveLTFirstHead(bool withcaptions) const
1923 if (!is_long_tabular || endfirsthead.empty)
1925 for (row_type r = 0; r < nrows(); ++r)
1926 if (row_info[r].endfirsthead &&
1927 (withcaptions || !row_info[r].caption))
1933 bool Tabular::haveLTFoot(bool withcaptions) const
1935 if (!is_long_tabular)
1937 for (row_type r = 0; r < nrows(); ++r)
1938 if (row_info[r].endfoot &&
1939 (withcaptions || !row_info[r].caption))
1945 bool Tabular::haveLTLastFoot(bool withcaptions) const
1947 if (!is_long_tabular || endlastfoot.empty)
1949 for (row_type r = 0; r < nrows(); ++r)
1950 if (row_info[r].endlastfoot &&
1951 (withcaptions || !row_info[r].caption))
1957 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1959 idx_type i = getFirstCellInRow(row);
1961 setMultiColumn(i, numberOfCellsInRow(row), false);
1962 setTopLine(i, false);
1963 setBottomLine(i, false);
1964 setLeftLine(i, false);
1965 setRightLine(i, false);
1966 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
1967 !row_info[row].endfoot && !row_info[row].endlastfoot) {
1968 setLTHead(row, true, endfirsthead, true);
1969 row_info[row].endfirsthead = true;
1972 unsetMultiColumn(i);
1973 // When unsetting a caption row, also all existing
1974 // captions in this row must be dissolved.
1976 row_info[row].caption = what;
1981 bool Tabular::ltCaption(row_type row) const
1983 return row_info[row].caption;
1987 bool Tabular::haveLTCaption(CaptionType captiontype) const
1989 if (!is_long_tabular)
1991 for (row_type r = 0; r < nrows(); ++r) {
1992 if (row_info[r].caption) {
1993 switch (captiontype) {
1994 case CAPTION_FIRSTHEAD:
1995 if (row_info[r].endfirsthead)
1999 if (row_info[r].endhead)
2003 if (row_info[r].endfoot)
2006 case CAPTION_LASTFOOT:
2007 if (row_info[r].endlastfoot)
2019 // end longtable support functions
2021 void Tabular::setRowAscent(row_type row, int height)
2023 if (row >= nrows() || row_info[row].ascent == height)
2025 row_info[row].ascent = height;
2029 void Tabular::setRowDescent(row_type row, int height)
2031 if (row >= nrows() || row_info[row].descent == height)
2033 row_info[row].descent = height;
2037 int Tabular::rowAscent(row_type row) const
2039 LASSERT(row < nrows(), /**/);
2040 return row_info[row].ascent;
2044 int Tabular::rowDescent(row_type row) const
2046 LASSERT(row < nrows(), /**/);
2047 return row_info[row].descent;
2051 int Tabular::height() const
2054 for (row_type row = 0; row < nrows(); ++row)
2055 height += rowAscent(row) + rowDescent(row) +
2061 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2063 LASSERT(row < nrows(), /**/);
2064 LASSERT(column < ncols(), /**/);
2065 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2069 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2071 LASSERT(row < nrows(), /**/);
2072 LASSERT(column < ncols(), /**/);
2073 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2077 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2079 // we only output complete row lines and the 1st row here, the rest
2080 // is done in Tabular::TeXBottomHLine(...)
2082 // get for each column the topline (if any)
2083 vector<bool> topline;
2085 for (col_type c = 0; c < ncols(); ++c) {
2086 topline.push_back(topLine(cellIndex(row, c)));
2087 // If cell is part of a multirow and not the first cell of the
2088 // multirow, no line must be drawn.
2090 if (isMultiRow(cellIndex(row, c))
2091 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2097 // do nothing if empty first row, or incomplete row line after
2098 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2101 // only output complete row lines and the 1st row's clines
2102 if (nset == ncols()) {
2104 os << (row == 0 ? "\\toprule " : "\\midrule ");
2108 } else if (row == 0) {
2109 for (col_type c = 0; c < ncols(); ++c) {
2111 col_type offset = 0;
2112 for (col_type j = 0 ; j < c; ++j)
2113 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2116 //babel makes the "-" character an active one, so we have to suppress this here
2117 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2118 if (lang == "slovak" || lang == "czech")
2119 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2120 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2122 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2124 col_type cstart = c;
2125 for ( ; c < ncols() && topline[c]; ++c) {}
2127 for (col_type j = cstart ; j < c ; ++j)
2128 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2131 os << c + offset << "} ";
2139 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2141 // we output bottomlines of row r and the toplines of row r+1
2142 // if the latter do not span the whole tabular
2144 // get the bottomlines of row r, and toplines in next row
2145 bool lastrow = row == nrows() - 1;
2146 vector<bool> bottomline, topline;
2147 bool nextrowset = true;
2148 for (col_type c = 0; c < ncols(); ++c) {
2149 bottomline.push_back(bottomLine(cellIndex(row, c)));
2150 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2151 // If cell is part of a multirow and not the last cell of the
2152 // multirow, no line must be drawn.
2154 if (isMultiRow(cellIndex(row, c))
2155 && isMultiRow(cellIndex(row + 1, c))
2156 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2157 bottomline[c] = false;
2160 nextrowset &= topline[c];
2163 // combine this row's bottom lines and next row's toplines if necessary
2165 for (col_type c = 0; c < ncols(); ++c) {
2167 bottomline[c] = bottomline[c] || topline[c];
2172 // do nothing if empty, OR incomplete row line with a topline in next row
2173 if (nset == 0 || (nextrowset && nset != ncols()))
2176 if (nset == ncols()) {
2178 os << (lastrow ? "\\bottomrule" : "\\midrule");
2182 for (col_type c = 0; c < ncols(); ++c) {
2183 if (bottomline[c]) {
2184 col_type offset = 0;
2185 for (col_type j = 0 ; j < c; ++j)
2186 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2189 //babel makes the "-" character an active one, so we have to suppress this here
2190 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2191 if (lang == "slovak" || lang == "czech")
2192 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2193 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2195 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2197 col_type cstart = c;
2198 for ( ; c < ncols() && bottomline[c]; ++c) {}
2200 for (col_type j = cstart ; j < c ; ++j)
2201 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2204 os << c + offset << "} ";
2212 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2213 bool & ismulticol, bool & ismultirow) const
2215 row_type const r = cellRow(cell);
2216 if (is_long_tabular && row_info[r].caption)
2219 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2220 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2221 // figure out how to set the lines
2222 // we always set double lines to the right of the cell
2223 col_type const c = cellColumn(cell);
2224 col_type const nextcol = c + columnSpan(cell);
2225 bool colright = columnRightLine(c);
2226 bool colleft = columnLeftLine(c);
2227 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2228 bool nextcellleft = nextcol < ncols()
2229 && leftLine(cellIndex(r, nextcol));
2230 bool coldouble = colright && nextcolleft;
2231 bool celldouble = rightLine(cell) && nextcellleft;
2233 ismulticol = isMultiColumn(cell)
2234 || (c == 0 && colleft != leftLine(cell))
2235 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2236 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2237 || (coldouble != celldouble);
2239 // we center in multicol when no decimal point
2240 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2241 docstring const align_d = column_info[c].decimal_point;
2242 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2246 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2247 int latexcolspan = columnSpan(cell);
2248 for(col_type col = c; col < c + columnSpan(cell); ++col)
2249 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2253 os << "\\multicolumn{" << latexcolspan << "}{";
2254 if (c ==0 && leftLine(cell))
2256 if (!cellInfo(cell).align_special.empty()) {
2257 os << cellInfo(cell).align_special;
2259 if (!getPWidth(cell).zero()) {
2261 case LYX_ALIGN_LEFT:
2262 os << ">{\\raggedright}";
2264 case LYX_ALIGN_RIGHT:
2265 os << ">{\\raggedleft}";
2267 case LYX_ALIGN_CENTER:
2268 os << ">{\\centering}";
2274 case LYX_VALIGN_TOP:
2277 case LYX_VALIGN_MIDDLE:
2280 case LYX_VALIGN_BOTTOM:
2285 << from_ascii(getPWidth(cell).asLatexString())
2289 case LYX_ALIGN_LEFT:
2292 case LYX_ALIGN_RIGHT:
2299 } // end if else !getPWidth
2300 } // end if else !cellinfo_of_cell
2301 if (rightLine(cell) || nextcellleft)
2304 // add extra vertical line if we want a double one
2307 } // end if ismulticol
2309 // we only need code for the first multirow cell
2310 ismultirow = isMultiRow(cell);
2312 os << "\\multirow{" << rowSpan(cell) << "}{";
2313 if (!getPWidth(cell).zero())
2314 os << from_ascii(getPWidth(cell).asLatexString());
2316 // we need to set a default value
2319 if (!getMROffset(cell).zero())
2320 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2322 } // end if ismultirow
2324 if (getRotateCell(cell)) {
2325 os << "\\begin{sideways}\n";
2327 if (getUsebox(cell) == BOX_PARBOX) {
2330 case LYX_VALIGN_TOP:
2333 case LYX_VALIGN_MIDDLE:
2336 case LYX_VALIGN_BOTTOM:
2340 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2342 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2343 os << "\\begin{minipage}[";
2345 case LYX_VALIGN_TOP:
2348 case LYX_VALIGN_MIDDLE:
2351 case LYX_VALIGN_BOTTOM:
2355 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2361 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2362 bool ismulticol, bool ismultirow) const
2364 row_type const r = cellRow(cell);
2365 if (is_long_tabular && row_info[r].caption)
2369 if (getUsebox(cell) == BOX_PARBOX)
2371 else if (getUsebox(cell) == BOX_MINIPAGE)
2372 os << breakln << "\\end{minipage}";
2373 if (getRotateCell(cell))
2374 os << breakln << "\\end{sideways}";
2382 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2383 OutputParams const & runparams) const
2385 if (!is_long_tabular)
2388 // output first header info
2389 if (haveLTFirstHead()) {
2390 if (endfirsthead.topDL)
2392 for (row_type r = 0; r < nrows(); ++r) {
2393 if (row_info[r].endfirsthead)
2394 TeXRow(os, r, runparams);
2396 if (endfirsthead.bottomDL)
2398 os << "\\endfirsthead\n";
2400 // output header info
2402 if (endfirsthead.empty && !haveLTFirstHead())
2403 os << "\\endfirsthead\n";
2406 for (row_type r = 0; r < nrows(); ++r) {
2407 if (row_info[r].endhead)
2408 TeXRow(os, r, runparams);
2410 if (endhead.bottomDL)
2412 os << "\\endhead\n";
2414 // output footer info
2418 for (row_type r = 0; r < nrows(); ++r) {
2419 if (row_info[r].endfoot)
2420 TeXRow(os, r, runparams);
2422 if (endfoot.bottomDL)
2424 os << "\\endfoot\n";
2425 if (endlastfoot.empty && !haveLTLastFoot())
2426 os << "\\endlastfoot\n";
2428 // output lastfooter info
2429 if (haveLTLastFoot()) {
2430 if (endlastfoot.topDL)
2432 for (row_type r = 0; r < nrows(); ++r) {
2433 if (row_info[r].endlastfoot)
2434 TeXRow(os, r, runparams);
2436 if (endlastfoot.bottomDL)
2438 os << "\\endlastfoot\n";
2443 bool Tabular::isValidRow(row_type row) const
2445 if (!is_long_tabular)
2447 return !row_info[row].endhead && !row_info[row].endfirsthead
2448 && !row_info[row].endfoot && !row_info[row].endlastfoot
2449 && !row_info[row].caption;
2453 void Tabular::TeXRow(otexstream & os, row_type row,
2454 OutputParams const & runparams) const
2456 idx_type cell = cellIndex(row, 0);
2457 shared_ptr<InsetTableCell> inset = cellInset(cell);
2458 Paragraph const & par = inset->paragraphs().front();
2459 string const lang = par.getParLanguage(buffer().params())->lang();
2461 //output the top line
2462 TeXTopHLine(os, row, lang);
2464 if (row_info[row].top_space_default) {
2466 os << "\\addlinespace\n";
2468 os << "\\noalign{\\vskip\\doublerulesep}\n";
2469 } else if(!row_info[row].top_space.zero()) {
2471 os << "\\addlinespace["
2472 << from_ascii(row_info[row].top_space.asLatexString())
2475 os << "\\noalign{\\vskip"
2476 << from_ascii(row_info[row].top_space.asLatexString())
2480 bool ismulticol = false;
2481 bool ismultirow = false;
2482 for (col_type c = 0; c < ncols(); ++c) {
2483 if (isPartOfMultiColumn(row, c))
2486 cell = cellIndex(row, c);
2488 if (isPartOfMultiRow(row, c)
2489 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2490 if (cell != getLastCellInRow(row))
2495 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2496 shared_ptr<InsetTableCell> inset = cellInset(cell);
2498 Paragraph const & par = inset->paragraphs().front();
2499 bool rtl = par.isRTL(buffer().params())
2501 && getPWidth(cell).zero();
2505 par.getParLanguage(buffer().params())->lang();
2506 if (lang == "farsi")
2508 else if (lang == "arabic_arabi")
2510 // currently, remaining RTL languages are
2511 // arabic_arabtex and hebrew
2515 // pass to the OutputParams that we are in a cell and
2516 // which alignment we have set.
2517 // InsetNewline needs this context information.
2518 OutputParams newrp(runparams);
2519 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2520 ? OutputParams::PLAIN
2521 : OutputParams::ALIGNED;
2523 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2524 // copy cell and split in 2
2525 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2526 head.setBuffer(buffer());
2527 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2529 dit.push_back(CursorSlice(head));
2530 head.setMacrocontextPositionRecursive(dit);
2531 bool hassep = false;
2532 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2533 head.latex(os, newrp);
2536 tail.setBuffer(head.buffer());
2538 dit.push_back(CursorSlice(tail));
2539 tail.setMacrocontextPositionRecursive(dit);
2540 tail.latex(os, newrp);
2542 } else if (!isPartOfMultiRow(row, c)) {
2543 if (!runparams.nice)
2544 os.texrow().start(par.id(), 0);
2545 inset->latex(os, newrp);
2548 runparams.encoding = newrp.encoding;
2552 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2553 if (cell != getLastCellInRow(row)) { // not last cell in row
2560 os << "\\tabularnewline";
2561 if (row_info[row].bottom_space_default) {
2563 os << "\\addlinespace";
2565 os << "[\\doublerulesep]";
2566 } else if (!row_info[row].bottom_space.zero()) {
2568 os << "\\addlinespace";
2570 << from_ascii(row_info[row].bottom_space.asLatexString())
2575 //output the bottom line
2576 TeXBottomHLine(os, row, lang);
2578 if (row_info[row].interline_space_default) {
2580 os << "\\addlinespace\n";
2582 os << "\\noalign{\\vskip\\doublerulesep}\n";
2583 } else if (!row_info[row].interline_space.zero()) {
2585 os << "\\addlinespace["
2586 << from_ascii(row_info[row].interline_space.asLatexString())
2589 os << "\\noalign{\\vskip"
2590 << from_ascii(row_info[row].interline_space.asLatexString())
2596 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2598 bool const is_tabular_star = !tabular_width.zero();
2600 //+---------------------------------------------------------------------
2601 //+ first the opening preamble +
2602 //+---------------------------------------------------------------------
2605 if (runparams.lastid != -1)
2606 os.texrow().start(runparams.lastid, runparams.lastpos);
2609 os << "\\begin{sideways}\n";
2611 if (is_long_tabular) {
2612 os << "\\begin{longtable}";
2613 switch (longtabular_alignment) {
2614 case LYX_LONGTABULAR_ALIGN_LEFT:
2617 case LYX_LONGTABULAR_ALIGN_CENTER:
2619 case LYX_LONGTABULAR_ALIGN_RIGHT:
2624 if (is_tabular_star)
2625 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2627 os << "\\begin{tabular}";
2628 switch (tabular_valignment) {
2629 case LYX_VALIGN_TOP:
2632 case LYX_VALIGN_MIDDLE:
2634 case LYX_VALIGN_BOTTOM:
2642 if (is_tabular_star)
2643 os << "@{\\extracolsep{\\fill}}";
2645 for (col_type c = 0; c < ncols(); ++c) {
2646 if (columnLeftLine(c))
2648 if (!column_info[c].align_special.empty()) {
2649 os << column_info[c].align_special;
2651 if (!column_info[c].p_width.zero()) {
2652 switch (column_info[c].alignment) {
2653 case LYX_ALIGN_LEFT:
2654 os << ">{\\raggedright}";
2656 case LYX_ALIGN_RIGHT:
2657 os << ">{\\raggedleft}";
2659 case LYX_ALIGN_CENTER:
2660 os << ">{\\centering}";
2662 case LYX_ALIGN_NONE:
2663 case LYX_ALIGN_BLOCK:
2664 case LYX_ALIGN_LAYOUT:
2665 case LYX_ALIGN_SPECIAL:
2666 case LYX_ALIGN_DECIMAL:
2670 switch (column_info[c].valignment) {
2671 case LYX_VALIGN_TOP:
2674 case LYX_VALIGN_MIDDLE:
2677 case LYX_VALIGN_BOTTOM:
2682 << from_ascii(column_info[c].p_width.asLatexString())
2685 switch (column_info[c].alignment) {
2686 case LYX_ALIGN_LEFT:
2689 case LYX_ALIGN_RIGHT:
2692 case LYX_ALIGN_DECIMAL:
2693 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2699 } // end if else !column_info[i].p_width
2700 } // end if else !column_info[i].align_special
2701 if (columnRightLine(c))
2706 TeXLongtableHeaderFooter(os, runparams);
2708 //+---------------------------------------------------------------------
2709 //+ the single row and columns (cells) +
2710 //+---------------------------------------------------------------------
2712 for (row_type r = 0; r < nrows(); ++r) {
2713 if (isValidRow(r)) {
2714 TeXRow(os, r, runparams);
2715 if (is_long_tabular && row_info[r].newpage)
2716 os << "\\newpage\n";
2720 //+---------------------------------------------------------------------
2721 //+ the closing of the tabular +
2722 //+---------------------------------------------------------------------
2724 if (is_long_tabular)
2725 os << "\\end{longtable}";
2727 if (is_tabular_star)
2728 os << "\\end{tabular*}";
2730 os << "\\end{tabular}";
2734 os << breakln << "\\end{sideways}";
2738 int Tabular::docbookRow(odocstream & os, row_type row,
2739 OutputParams const & runparams) const
2742 idx_type cell = getFirstCellInRow(row);
2745 for (col_type c = 0; c < ncols(); ++c) {
2746 if (isPartOfMultiColumn(row, c))
2749 os << "<entry align=\"";
2750 switch (getAlignment(cell)) {
2751 case LYX_ALIGN_LEFT:
2754 case LYX_ALIGN_RIGHT:
2762 os << "\" valign=\"";
2763 switch (getVAlignment(cell)) {
2764 case LYX_VALIGN_TOP:
2767 case LYX_VALIGN_BOTTOM:
2770 case LYX_VALIGN_MIDDLE:
2775 if (isMultiColumn(cell)) {
2776 os << " namest=\"col" << c << "\" ";
2777 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2781 ret += cellInset(cell)->docbook(os, runparams);
2790 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2794 //+---------------------------------------------------------------------
2795 //+ first the opening preamble +
2796 //+---------------------------------------------------------------------
2798 os << "<tgroup cols=\"" << ncols()
2799 << "\" colsep=\"1\" rowsep=\"1\">\n";
2801 for (col_type c = 0; c < ncols(); ++c) {
2802 os << "<colspec colname=\"col" << c << "\" align=\"";
2803 switch (column_info[c].alignment) {
2804 case LYX_ALIGN_LEFT:
2807 case LYX_ALIGN_RIGHT:
2815 if (runparams.flavor == OutputParams::XML)
2821 //+---------------------------------------------------------------------
2822 //+ Long Tabular case +
2823 //+---------------------------------------------------------------------
2825 // output caption info
2826 // The caption flag wins over head/foot
2827 if (haveLTCaption()) {
2828 os << "<caption>\n";
2830 for (row_type r = 0; r < nrows(); ++r) {
2831 if (row_info[r].caption) {
2832 ret += docbookRow(os, r, runparams);
2835 os << "</caption>\n";
2838 // output header info
2839 if (haveLTHead(false) || haveLTFirstHead(false)) {
2842 for (row_type r = 0; r < nrows(); ++r) {
2843 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2844 !row_info[r].caption) {
2845 ret += docbookRow(os, r, runparams);
2851 // output footer info
2852 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2855 for (row_type r = 0; r < nrows(); ++r) {
2856 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2857 !row_info[r].caption) {
2858 ret += docbookRow(os, r, runparams);
2865 //+---------------------------------------------------------------------
2866 //+ the single row and columns (cells) +
2867 //+---------------------------------------------------------------------
2871 for (row_type r = 0; r < nrows(); ++r) {
2872 if (isValidRow(r)) {
2873 ret += docbookRow(os, r, runparams);
2878 //+---------------------------------------------------------------------
2879 //+ the closing of the tabular +
2880 //+---------------------------------------------------------------------
2889 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2890 OutputParams const & runparams, bool header) const
2893 string const celltag = header ? "th" : "td";
2894 idx_type cell = getFirstCellInRow(row);
2896 xs << html::StartTag("tr");
2897 for (col_type c = 0; c < ncols(); ++c) {
2898 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2903 switch (getAlignment(cell)) {
2904 case LYX_ALIGN_LEFT:
2907 case LYX_ALIGN_RIGHT:
2915 attr << " valign='";
2916 switch (getVAlignment(cell)) {
2917 case LYX_VALIGN_TOP:
2920 case LYX_VALIGN_BOTTOM:
2923 case LYX_VALIGN_MIDDLE:
2928 if (isMultiColumn(cell))
2929 attr << " colspan='" << columnSpan(cell) << "'";
2930 else if (isMultiRow(cell))
2931 attr << " rowspan='" << rowSpan(cell) << "'";
2933 xs << html::StartTag(celltag, attr.str()) << html::CR();
2934 ret += cellInset(cell)->xhtml(xs, runparams);
2935 xs << html::EndTag(celltag) << html::CR();
2938 xs << html::EndTag("tr");
2943 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2947 if (is_long_tabular) {
2948 // we'll wrap it in a div, so as to deal with alignment
2950 switch (longtabular_alignment) {
2951 case LYX_LONGTABULAR_ALIGN_LEFT:
2954 case LYX_LONGTABULAR_ALIGN_CENTER:
2957 case LYX_LONGTABULAR_ALIGN_RIGHT:
2961 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2963 // The caption flag wins over head/foot
2964 if (haveLTCaption()) {
2965 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2967 for (row_type r = 0; r < nrows(); ++r)
2968 if (row_info[r].caption)
2969 ret += xhtmlRow(xs, r, runparams);
2970 xs << html::EndTag("div") << html::CR();
2974 xs << html::StartTag("table") << html::CR();
2976 // output header info
2977 bool const havefirsthead = haveLTFirstHead(false);
2978 // if we have a first head, then we are going to ignore the
2979 // headers for the additional pages, since there aren't any
2980 // in XHTML. this test accomplishes that.
2981 bool const havehead = !havefirsthead && haveLTHead(false);
2982 if (havehead || havefirsthead) {
2983 xs << html::StartTag("thead") << html::CR();
2984 for (row_type r = 0; r < nrows(); ++r) {
2985 if (((havefirsthead && row_info[r].endfirsthead) ||
2986 (havehead && row_info[r].endhead)) &&
2987 !row_info[r].caption) {
2988 ret += xhtmlRow(xs, r, runparams, true);
2991 xs << html::EndTag("thead") << html::CR();
2993 // output footer info
2994 bool const havelastfoot = haveLTLastFoot(false);
2996 bool const havefoot = !havelastfoot && haveLTFoot(false);
2997 if (havefoot || havelastfoot) {
2998 xs << html::StartTag("tfoot") << html::CR();
2999 for (row_type r = 0; r < nrows(); ++r) {
3000 if (((havelastfoot && row_info[r].endlastfoot) ||
3001 (havefoot && row_info[r].endfoot)) &&
3002 !row_info[r].caption) {
3003 ret += xhtmlRow(xs, r, runparams);
3006 xs << html::EndTag("tfoot") << html::CR();
3009 xs << html::StartTag("tbody") << html::CR();
3010 for (row_type r = 0; r < nrows(); ++r) {
3011 if (isValidRow(r)) {
3012 ret += xhtmlRow(xs, r, runparams);
3015 xs << html::EndTag("tbody")
3017 << html::EndTag("table")
3019 if (is_long_tabular)
3020 xs << html::EndTag("div") << html::CR();
3025 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3026 vector<unsigned int> const & clen) const
3028 idx_type const fcell = getFirstCellInRow(row);
3029 idx_type const n = numberOfCellsInRow(row) + fcell;
3032 for (idx_type i = fcell; i < n; ++i) {
3042 for (idx_type i = fcell; i < n; ++i) {
3053 col_type column = cellColumn(i);
3054 int len = clen[column];
3055 while (column < ncols() - 1
3056 && isPartOfMultiColumn(row, ++column))
3057 len += clen[column] + 4;
3058 os << docstring(len, ch);
3073 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3074 vector<unsigned int> const & clen) const
3076 idx_type const fcell = getFirstCellInRow(row);
3077 idx_type const n = numberOfCellsInRow(row) + fcell;
3080 for (idx_type i = fcell; i < n; ++i) {
3081 if (bottomLine(i)) {
3090 for (idx_type i = fcell; i < n; ++i) {
3091 if (bottomLine(i)) {
3101 col_type column = cellColumn(i);
3102 int len = clen[column];
3103 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3104 len += clen[column] + 4;
3105 os << docstring(len, ch);
3106 if (bottomLine(i)) {
3120 void Tabular::plaintextPrintCell(odocstream & os,
3121 OutputParams const & runparams,
3122 idx_type cell, row_type row, col_type column,
3123 vector<unsigned int> const & clen,
3124 bool onlydata) const
3126 odocstringstream sstr;
3127 cellInset(cell)->plaintext(sstr, runparams);
3139 unsigned int len1 = sstr.str().length();
3140 unsigned int len2 = clen[column];
3141 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3142 len2 += clen[column] + 4;
3145 switch (getAlignment(cell)) {
3147 case LYX_ALIGN_LEFT:
3150 case LYX_ALIGN_RIGHT:
3154 case LYX_ALIGN_CENTER:
3160 os << docstring(len1, ' ') << sstr.str()
3161 << docstring(len2, ' ');
3163 if (rightLine(cell))
3170 void Tabular::plaintext(odocstream & os,
3171 OutputParams const & runparams, int const depth,
3172 bool onlydata, char_type delim) const
3174 // first calculate the width of the single columns
3175 vector<unsigned int> clen(ncols());
3178 // first all non multicolumn cells!
3179 for (col_type c = 0; c < ncols(); ++c) {
3181 for (row_type r = 0; r < nrows(); ++r) {
3182 idx_type cell = cellIndex(r, c);
3183 if (isMultiColumn(cell))
3185 odocstringstream sstr;
3186 cellInset(cell)->plaintext(sstr, runparams);
3187 if (clen[c] < sstr.str().length())
3188 clen[c] = sstr.str().length();
3191 // then all multicolumn cells!
3192 for (col_type c = 0; c < ncols(); ++c) {
3193 for (row_type r = 0; r < nrows(); ++r) {
3194 idx_type cell = cellIndex(r, c);
3195 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3197 odocstringstream sstr;
3198 cellInset(cell)->plaintext(sstr, runparams);
3199 int len = int(sstr.str().length());
3200 idx_type const n = columnSpan(cell);
3201 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3203 if (len > int(clen[c + n - 1]))
3204 clen[c + n - 1] = len;
3209 for (row_type r = 0; r < nrows(); ++r) {
3210 if (!onlydata && plaintextTopHLine(os, r, clen))
3211 os << docstring(depth * 2, ' ');
3212 for (col_type c = 0; c < ncols(); ++c) {
3213 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3215 if (onlydata && c > 0)
3216 // we don't use operator<< for single UCS4 character.
3217 // see explanation in docstream.h
3219 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3224 os << docstring(depth * 2, ' ');
3225 if (plaintextBottomHLine(os, r, clen))
3226 os << docstring(depth * 2, ' ');
3232 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3234 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3238 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3239 col_type column) const
3241 return cell_info[row][column].inset;
3245 void Tabular::setCellInset(row_type row, col_type column,
3246 shared_ptr<InsetTableCell> ins) const
3248 CellData & cd = cell_info[row][column];
3253 void Tabular::validate(LaTeXFeatures & features) const
3255 features.require("NeedTabularnewline");
3257 features.require("booktabs");
3258 if (is_long_tabular)
3259 features.require("longtable");
3261 features.require("rotating");
3262 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3263 if (isMultiRow(cell))
3264 features.require("multirow");
3265 if (getVAlignment(cell) != LYX_VALIGN_TOP
3266 || !getPWidth(cell).zero())
3267 features.require("array");
3268 cellInset(cell)->validate(features);
3273 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3275 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3276 ParagraphList::const_iterator cit = parlist.begin();
3277 ParagraphList::const_iterator end = parlist.end();
3279 for (; cit != end; ++cit)
3280 for (int i = 0; i < cit->size(); ++i)
3281 if (cit->isNewline(i))
3288 /////////////////////////////////////////////////////////////////////
3292 /////////////////////////////////////////////////////////////////////
3294 InsetTableCell::InsetTableCell(Buffer * buf)
3295 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3296 contentAlign(LYX_ALIGN_CENTER)
3300 bool InsetTableCell::forcePlainLayout(idx_type) const
3302 return !isFixedWidth;
3306 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3308 return isFixedWidth;
3312 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3313 FuncStatus & status) const
3315 bool enabled = true;
3316 switch (cmd.action()) {
3318 enabled = !forcePlainLayout();
3320 case LFUN_LAYOUT_PARAGRAPH:
3321 enabled = allowParagraphCustomization();
3324 case LFUN_MATH_DISPLAY:
3325 if (!hasFixedWidth()) {
3330 return InsetText::getStatus(cur, cmd, status);
3332 status.setEnabled(enabled);
3336 docstring InsetTableCell::asString(bool intoInsets)
3339 if (paragraphs().empty())
3341 ParagraphList::const_iterator it = paragraphs().begin();
3342 ParagraphList::const_iterator en = paragraphs().end();
3344 for (; it != en; ++it) {
3349 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3355 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3358 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3359 return InsetText::xhtml(xs, rp);
3364 /////////////////////////////////////////////////////////////////////
3368 /////////////////////////////////////////////////////////////////////
3370 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3372 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3373 rowselect_(false), colselect_(false)
3378 InsetTabular::InsetTabular(InsetTabular const & tab)
3379 : Inset(tab), tabular(tab.tabular), scx_(0)
3384 InsetTabular::~InsetTabular()
3386 hideDialogs("tabular", this);
3390 void InsetTabular::setBuffer(Buffer & buf)
3392 tabular.setBuffer(buf);
3393 Inset::setBuffer(buf);
3397 bool InsetTabular::insetAllowed(InsetCode code) const
3402 case MATHMACRO_CODE:
3407 return tabular.is_long_tabular;
3415 void InsetTabular::write(ostream & os) const
3417 os << "Tabular" << endl;
3422 string InsetTabular::contextMenu(BufferView const &, int, int) const
3424 // FIXME: depending on the selection state,
3425 // we could offer a different menu.
3426 return cell(0)->contextMenuName() + ";" + contextMenuName();
3430 string InsetTabular::contextMenuName() const
3432 return "context-tabular";
3436 void InsetTabular::read(Lexer & lex)
3438 //bool const old_format = (lex.getString() == "\\LyXTable");
3446 string token = lex.getString();
3447 while (lex && token != "\\end_inset") {
3449 token = lex.getString();
3452 lex.printError("Missing \\end_inset at this point. ");
3456 int InsetTabular::rowFromY(Cursor & cur, int y) const
3458 // top y coordinate of tabular
3459 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3461 for (; r < tabular.nrows() && y > h; ++r)
3462 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3463 + tabular.interRowSpace(r);
3469 int InsetTabular::columnFromX(Cursor & cur, int x) const
3471 // left x coordinate of tabular
3472 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3474 for (; c < tabular.ncols() && x > w; ++c)
3475 w += tabular.cellWidth(c);
3480 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3482 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3483 // mi.base.textwidth << "\n";
3486 LASSERT(false, /**/);
3489 for (row_type r = 0; r < tabular.nrows(); ++r) {
3492 for (col_type c = 0; c < tabular.ncols(); ++c) {
3493 if (tabular.isPartOfMultiColumn(r, c)
3494 || tabular.isPartOfMultiRow(r, c))
3495 // multicolumn or multirow cell, but not first one
3497 idx_type const cell = tabular.cellIndex(r, c);
3500 Length const p_width = tabular.getPWidth(cell);
3501 if (!p_width.zero())
3502 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3503 tabular.cellInset(cell)->metrics(m, dim);
3504 if (!p_width.zero())
3505 dim.wid = m.base.textwidth;
3506 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3507 + tabular.interColumnSpace(cell);
3509 // FIXME(?): do we need a second metrics call?
3510 TextMetrics const & tm =
3511 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3513 // determine horizontal offset because of decimal align (if necessary)
3514 int decimal_width = 0;
3515 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3516 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3517 tail.setBuffer(tabular.buffer());
3518 // we need to set macrocontext position everywhere
3519 // otherwise we crash with nested insets (e.g. footnotes)
3520 // after decimal point
3521 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3523 dit.push_back(CursorSlice(tail));
3524 tail.setMacrocontextPositionRecursive(dit);
3526 // remove text leading decimal point
3527 docstring const align_d = tabular.column_info[c].decimal_point;
3528 dit = separatorPos(&tail, align_d);
3530 pit_type const psize = tail.paragraphs().front().size();
3532 tail.paragraphs().front().eraseChars(0,
3533 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3535 tail.metrics(m, dim1);
3536 decimal_width = dim1.width();
3539 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3540 tabular.cell_info[r][c].decimal_width = decimal_width;
3542 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3543 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3544 int const lastpardes = tm.last().second->descent()
3545 + TEXT_TO_INSET_OFFSET;
3547 switch (tabular.getVAlignment(cell)) {
3548 case Tabular::LYX_VALIGN_TOP:
3550 case Tabular::LYX_VALIGN_MIDDLE:
3551 offset = -(dim.des - lastpardes)/2;
3553 case Tabular::LYX_VALIGN_BOTTOM:
3554 offset = -(dim.des - lastpardes);
3557 tabular.cell_info[r][c].voffset = offset;
3558 maxasc = max(maxasc, dim.asc - offset);
3559 maxdes = max(maxdes, dim.des + offset);
3561 int const top_space = tabular.row_info[r].top_space_default ?
3562 default_line_space :
3563 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3564 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3565 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3566 default_line_space :
3567 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3568 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3571 // for top-alignment the first horizontal table line must be exactly at
3572 // the position of the base line of the surrounding text line
3573 // for bottom alignment, the same is for the last table line
3574 switch (tabular.tabular_valignment) {
3575 case Tabular::LYX_VALIGN_BOTTOM:
3576 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3578 case Tabular::LYX_VALIGN_MIDDLE:
3579 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3581 case Tabular::LYX_VALIGN_TOP:
3582 offset_valign_ = tabular.rowAscent(0);
3586 tabular.updateColumnWidths();
3587 dim.asc = tabular.rowAscent(0) - offset_valign_;
3588 dim.des = tabular.height() - dim.asc;
3589 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3593 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3596 if (&cur.inset() == this && cur.selection()) {
3597 if (cur.selIsMultiCell()) {
3600 getSelection(cur, rs, re, cs, ce);
3602 idx_type const cell = tabular.cellIndex(row, col);
3603 col_type const cspan = tabular.columnSpan(cell);
3604 row_type const rspan = tabular.rowSpan(cell);
3605 if (col + cspan - 1 >= cs && col <= ce
3606 && row + rspan - 1 >= rs && row <= re)
3609 if (col == tabular.cellColumn(cur.idx())
3610 && row == tabular.cellRow(cur.idx())) {
3611 CursorSlice const & beg = cur.selBegin();
3612 CursorSlice const & end = cur.selEnd();
3614 if ((end.lastpos() > 0 || end.lastpit() > 0)
3615 && end.pos() == end.lastpos() && beg.pos() == 0
3616 && end.pit() == end.lastpit() && beg.pit() == 0)
3624 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3626 x += scx_ + ADD_TO_TABULAR_WIDTH;
3628 BufferView * bv = pi.base.bv;
3629 Cursor & cur = pi.base.bv->cursor();
3632 // FIXME: As the full background is painted in drawBackground(),
3633 // we have no choice but to do a full repaint for the Text cells.
3634 pi.full_repaint = true;
3636 bool const original_selection_state = pi.selected;
3639 first_visible_cell = Tabular::npos;
3641 int yy = y + offset_valign_;
3642 for (row_type r = 0; r < tabular.nrows(); ++r) {
3644 for (col_type c = 0; c < tabular.ncols(); ++c) {
3645 if (tabular.isPartOfMultiColumn(r, c))
3648 idx = tabular.cellIndex(r, c);
3650 if (tabular.isPartOfMultiRow(r, c)) {
3651 nx += tabular.cellWidth(idx);
3655 if (first_visible_cell == Tabular::npos)
3656 first_visible_cell = idx;
3658 pi.selected |= isCellSelected(cur, r, c);
3659 int const cx = nx + tabular.textHOffset(idx);
3660 int const cy = yy + tabular.textVOffset(idx);
3661 // Cache the Inset position.
3662 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3663 cell(idx)->draw(pi, cx, cy);
3664 drawCellLines(pi, nx, yy, r, idx);
3665 nx += tabular.cellWidth(idx);
3666 pi.selected = original_selection_state;
3669 if (r + 1 < tabular.nrows())
3670 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3671 + tabular.interRowSpace(r + 1);
3676 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3678 x += scx_ + ADD_TO_TABULAR_WIDTH;
3679 y += offset_valign_ - tabular.rowAscent(0);
3680 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3681 pi.backgroundColor(this));
3685 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3687 Cursor & cur = pi.base.bv->cursor();
3690 x += scx_ + ADD_TO_TABULAR_WIDTH;
3692 if (!cur.selection())
3694 if (&cur.inset() != this)
3699 bool const full_cell_selected = isCellSelected(cur,
3700 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3702 if (cur.selIsMultiCell() || full_cell_selected) {
3703 for (row_type r = 0; r < tabular.nrows(); ++r) {
3705 for (col_type c = 0; c < tabular.ncols(); ++c) {
3706 if (tabular.isPartOfMultiColumn(r, c))
3709 idx_type const cell = tabular.cellIndex(r, c);
3711 if (tabular.isPartOfMultiRow(r, c)) {
3712 xx += tabular.cellWidth(cell);
3715 int const w = tabular.cellWidth(cell);
3716 int const h = tabular.cellHeight(cell);
3717 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3718 if (isCellSelected(cur, r, c))
3719 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3722 if (r + 1 < tabular.nrows())
3723 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3724 + tabular.interRowSpace(r + 1);
3728 // FIXME: This code has no effect because InsetTableCell does not handle
3729 // drawSelection other than the trivial implementation in Inset.
3731 // x += cellXPos(cur.idx());
3732 // x += tabular.textHOffset(cur.idx());
3733 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3738 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3739 row_type row, idx_type cell) const
3741 y -= tabular.rowAscent(row);
3742 int const w = tabular.cellWidth(cell);
3743 int const h = tabular.cellHeight(cell);
3744 Color const linecolor = pi.textColor(Color_tabularline);
3745 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3748 bool drawline = tabular.topLine(cell)
3749 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3750 pi.pain.line(x, y, x + w, y,
3751 drawline ? linecolor : gridcolor,
3752 drawline ? Painter::line_solid : Painter::line_onoffdash);
3755 drawline = tabular.bottomLine(cell);
3756 pi.pain.line(x, y + h, x + w, y + h,
3757 drawline ? linecolor : gridcolor,
3758 drawline ? Painter::line_solid : Painter::line_onoffdash);
3761 col_type const col = tabular.cellColumn(cell);
3762 drawline = tabular.leftLine(cell)
3763 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3764 pi.pain.line(x, y, x, y + h,
3765 drawline ? linecolor : gridcolor,
3766 drawline ? Painter::line_solid : Painter::line_onoffdash);
3769 x -= tabular.interColumnSpace(cell);
3770 drawline = tabular.rightLine(cell)
3771 || (col + 1 < tabular.ncols()
3772 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3773 pi.pain.line(x + w, y, x + w, y + h,
3774 drawline ? linecolor : gridcolor,
3775 drawline ? Painter::line_solid : Painter::line_onoffdash);
3779 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3781 //lyxerr << "InsetTabular::edit: " << this << endl;
3783 cur.setSelection(false);
3786 if (isRightToLeft(cur))
3787 cur.idx() = tabular.getLastCellInRow(0);
3793 if (isRightToLeft(cur))
3794 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3796 cur.idx() = tabular.numberofcells - 1;
3798 cur.pos() = cur.lastpos(); // FIXME crude guess
3800 cur.setCurrentFont();
3801 // FIXME: this accesses the position cache before it is initialized
3803 //cur.bv().fitCursor();
3807 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3809 // In a longtable, tell captions what the current float is
3810 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3811 string const saveflt = cnts.current_float();
3812 if (tabular.is_long_tabular)
3813 cnts.current_float("table");
3815 ParIterator it2 = it;
3817 size_t const end = it2.nargs();
3818 for ( ; it2.idx() < end; it2.top().forwardIdx())
3819 buffer().updateBuffer(it2, utype);
3822 if (tabular.is_long_tabular)
3823 cnts.current_float(saveflt);
3827 void InsetTabular::addToToc(DocIterator const & cpit) const
3829 DocIterator dit = cpit;
3831 size_t const end = dit.nargs();
3832 for ( ; dit.idx() < end; dit.top().forwardIdx())
3833 cell(dit.idx())->addToToc(dit);
3837 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3839 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3840 << "\n cur:" << cur);
3841 CursorSlice sl = cur.top();
3842 Cursor & bvcur = cur.bv().cursor();
3844 FuncCode const act = cmd.action();
3848 case LFUN_MOUSE_PRESS: {
3849 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3851 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3852 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3853 row_type r = rowFromY(cur, cmd.y());
3854 cur.idx() = tabular.getFirstCellInRow(r);
3857 cur.idx() = tabular.getLastCellInRow(r);
3858 cur.pos() = cur.lastpos();
3859 cur.setSelection(true);
3865 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3866 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3867 || cmd.y() > y0 + tabular.height()) {
3868 col_type c = columnFromX(cur, cmd.x());
3869 cur.idx() = tabular.cellIndex(0, c);
3872 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3873 cur.pos() = cur.lastpos();
3874 cur.setSelection(true);
3879 // do not reset cursor/selection if we have selected
3880 // some cells (bug 2715).
3881 if (cmd.button() == mouse_button::button3
3882 && &bvcur.selBegin().inset() == this
3883 && bvcur.selIsMultiCell())
3886 // Let InsetTableCell do it
3887 cell(cur.idx())->dispatch(cur, cmd);
3890 case LFUN_MOUSE_MOTION:
3891 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3892 if (cmd.button() == mouse_button::button1) {
3893 // only accept motions to places not deeper nested than the real anchor
3894 if (!bvcur.realAnchor().hasPart(cur)) {
3898 // select (additional) row
3900 row_type r = rowFromY(cur, cmd.y());
3901 cur.idx() = tabular.getLastCellInRow(r);
3902 // we need to reset the cursor's pit and pos now, as the old ones
3903 // may no longer be valid.
3906 bvcur.setCursor(cur);
3907 bvcur.setSelection(true);
3910 // select (additional) column
3912 col_type c = columnFromX(cur, cmd.x());
3913 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3914 // we need to reset the cursor's pit and pos now, as the old ones
3915 // may no longer be valid.
3918 bvcur.setCursor(cur);
3919 bvcur.setSelection(true);
3922 // only update if selection changes
3923 if (bvcur.idx() == cur.idx() &&
3924 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3925 cur.noScreenUpdate();
3926 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3927 bvcur.setCursor(cur);
3928 bvcur.setSelection(true);
3929 // if this is a multicell selection, we just set the cursor to
3930 // the beginning of the cell's text.
3931 if (bvcur.selIsMultiCell()) {
3932 bvcur.pit() = bvcur.lastpit();
3933 bvcur.pos() = bvcur.lastpos();
3938 case LFUN_MOUSE_RELEASE:
3943 case LFUN_CELL_BACKWARD:
3945 cur.setSelection(false);
3948 case LFUN_CELL_FORWARD:
3950 cur.setSelection(false);
3953 case LFUN_CHAR_FORWARD_SELECT:
3954 case LFUN_CHAR_FORWARD:
3955 case LFUN_CHAR_BACKWARD_SELECT:
3956 case LFUN_CHAR_BACKWARD:
3957 case LFUN_CHAR_RIGHT_SELECT:
3958 case LFUN_CHAR_RIGHT:
3959 case LFUN_CHAR_LEFT_SELECT:
3960 case LFUN_CHAR_LEFT:
3961 case LFUN_WORD_FORWARD:
3962 case LFUN_WORD_FORWARD_SELECT:
3963 case LFUN_WORD_BACKWARD:
3964 case LFUN_WORD_BACKWARD_SELECT:
3965 case LFUN_WORD_RIGHT:
3966 case LFUN_WORD_RIGHT_SELECT:
3967 case LFUN_WORD_LEFT:
3968 case LFUN_WORD_LEFT_SELECT: {
3969 // determine whether we move to next or previous cell, where to enter
3970 // the new cell from, and which command to "finish" (i.e., exit the
3973 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3974 FuncCode finish_lfun;
3976 if (act == LFUN_CHAR_FORWARD
3977 || act == LFUN_CHAR_FORWARD_SELECT
3978 || act == LFUN_WORD_FORWARD
3979 || act == LFUN_WORD_FORWARD_SELECT) {
3981 finish_lfun = LFUN_FINISHED_FORWARD;
3983 else if (act == LFUN_CHAR_BACKWARD
3984 || act == LFUN_CHAR_BACKWARD_SELECT
3985 || act == LFUN_WORD_BACKWARD
3986 || act == LFUN_WORD_BACKWARD_SELECT) {
3988 finish_lfun = LFUN_FINISHED_BACKWARD;
3990 // LEFT or RIGHT commands --- the interpretation will depend on the
3991 // table's direction.
3993 bool const right = act == LFUN_CHAR_RIGHT
3994 || act == LFUN_CHAR_RIGHT_SELECT
3995 || act == LFUN_WORD_RIGHT
3996 || act == LFUN_WORD_RIGHT_SELECT;
3997 next_cell = isRightToLeft(cur) != right;
3999 if (lyxrc.visual_cursor)
4000 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
4002 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4005 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4006 || act == LFUN_CHAR_BACKWARD_SELECT
4007 || act == LFUN_CHAR_RIGHT_SELECT
4008 || act == LFUN_CHAR_LEFT_SELECT
4009 || act == LFUN_WORD_FORWARD_SELECT
4010 || act == LFUN_WORD_RIGHT_SELECT
4011 || act == LFUN_WORD_BACKWARD_SELECT
4012 || act == LFUN_WORD_LEFT_SELECT;
4014 // If we have a multicell selection or we're
4015 // not doing some LFUN_*_SELECT thing anyway...
4016 if (!cur.selIsMultiCell() || !select) {
4017 col_type const c = tabular.cellColumn(cur.idx());
4018 row_type const r = tabular.cellRow(cur.idx());
4019 // Are we trying to select the whole cell and is the whole cell
4020 // not yet selected?
4021 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4022 ((next_cell && cur.pit() == cur.lastpit()
4023 && cur.pos() == cur.lastpos())
4024 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4026 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4028 // ...try to dispatch to the cell's inset.
4029 cell(cur.idx())->dispatch(cur, cmd);
4031 // When we already have a selection we want to select the whole cell
4032 // before going to the next cell.
4033 if (select_whole && !empty_cell){
4034 getText(cur.idx())->selectAll(cur);
4039 // FIXME: When we support the selection of an empty cell, remove
4040 // the !empty_cell from this condition. For now we jump to the next
4041 // cell if the current cell is empty.
4042 if (cur.result().dispatched() && !empty_cell)
4046 // move to next/prev cell, as appropriate
4047 // note that we will always do this if we're selecting and we have
4048 // a multicell selection
4049 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4050 << " cell from: " << int(entry_from));
4052 moveNextCell(cur, entry_from);
4054 movePrevCell(cur, entry_from);
4055 // if we're exiting the table, call the appropriate FINISHED lfun
4056 if (sl == cur.top())
4057 cmd = FuncRequest(finish_lfun);
4061 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4066 case LFUN_DOWN_SELECT:
4068 if (!(cur.selection() && cur.selIsMultiCell()))
4069 cell(cur.idx())->dispatch(cur, cmd);
4071 cur.dispatched(); // override the cell's decision
4072 if (sl == cur.top()) {
4073 // if our Text didn't do anything to the cursor
4074 // then we try to put the cursor into the cell below
4075 // setting also the right targetX.
4076 cur.selHandle(act == LFUN_DOWN_SELECT);
4077 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4078 int const xtarget = cur.targetX();
4079 // WARNING: Once cur.idx() has been reset, the cursor is in
4080 // an inconsistent state until pos() has been set. Be careful
4081 // what you do with it!
4082 cur.idx() = tabular.cellBelow(cur.idx());
4084 TextMetrics const & tm =
4085 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4086 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4087 cur.setCurrentFont();
4090 if (sl == cur.top()) {
4091 // we trick it to go to forward after leaving the
4093 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4096 if (cur.selIsMultiCell()) {
4097 cur.pit() = cur.lastpit();
4098 cur.pos() = cur.lastpos();
4099 cur.setCurrentFont();
4102 cur.screenUpdateFlags(Update::FitCursor);
4105 case LFUN_UP_SELECT:
4107 if (!(cur.selection() && cur.selIsMultiCell()))
4108 cell(cur.idx())->dispatch(cur, cmd);
4109 cur.dispatched(); // override the cell's decision
4110 if (sl == cur.top()) {
4111 // if our Text didn't do anything to the cursor
4112 // then we try to put the cursor into the cell above
4113 // setting also the right targetX.
4114 cur.selHandle(act == LFUN_UP_SELECT);
4115 if (tabular.cellRow(cur.idx()) != 0) {
4116 int const xtarget = cur.targetX();
4117 // WARNING: Once cur.idx() has been reset, the cursor is in
4118 // an inconsistent state until pos() has been set. Be careful
4119 // what you do with it!
4120 cur.idx() = tabular.cellAbove(cur.idx());
4121 cur.pit() = cur.lastpit();
4122 Text const * text = cell(cur.idx())->getText(0);
4123 TextMetrics const & tm = cur.bv().textMetrics(text);
4124 ParagraphMetrics const & pm =
4125 tm.parMetrics(cur.lastpit());
4126 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4127 cur.setCurrentFont();
4130 if (sl == cur.top()) {
4131 cmd = FuncRequest(LFUN_UP);
4134 if (cur.selIsMultiCell()) {
4136 cur.pos() = cur.lastpos();
4137 cur.setCurrentFont();
4140 cur.screenUpdateFlags(Update::FitCursor);
4143 // case LFUN_SCREEN_DOWN: {
4144 // //if (hasSelection())
4145 // // cur.selection() = false;
4146 // col_type const col = tabular.cellColumn(cur.idx());
4147 // int const t = cur.bv().top_y() + cur.bv().height();
4148 // if (t < yo() + tabular.getHeightOfTabular()) {
4149 // cur.bv().scrollDocView(t, true);
4150 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4152 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4159 // case LFUN_SCREEN_UP: {
4160 // //if (hasSelection())
4161 // // cur.selection() = false;
4162 // col_type const col = tabular.cellColumn(cur.idx());
4163 // int const t = cur.bv().top_y() + cur.bv().height();
4165 // cur.bv().scrollDocView(t, true);
4169 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4173 // cur.par() = cur.lastpar();
4174 // cur.pos() = cur.lastpos();
4178 case LFUN_LAYOUT_TABULAR:
4179 cur.bv().showDialog("tabular");
4182 case LFUN_INSET_MODIFY: {
4184 if (cmd.getArg(1) == "from-dialog")
4185 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4187 arg = to_utf8(cmd.argument());
4188 if (!tabularFeatures(cur, arg))
4193 // insert file functions
4194 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4195 case LFUN_FILE_INSERT_PLAINTEXT:
4197 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4198 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4199 FileName(to_utf8(cmd.argument())));
4202 cur.recordUndoInset(INSERT_UNDO);
4203 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4204 // content has been replaced,
4205 // so cursor might be invalid
4206 cur.pos() = cur.lastpos();
4207 cur.pit() = cur.lastpit();
4208 bvcur.setCursor(cur);
4215 if (cur.selIsMultiCell()) {
4216 if (copySelection(cur)) {
4217 cur.recordUndoInset(DELETE_UNDO);
4221 cell(cur.idx())->dispatch(cur, cmd);
4224 case LFUN_SELF_INSERT:
4225 if (cur.selIsMultiCell()) {
4226 cur.recordUndoInset(DELETE_UNDO);
4229 cell(cur.idx())->dispatch(cur, cmd);
4232 case LFUN_CHAR_DELETE_BACKWARD:
4233 case LFUN_CHAR_DELETE_FORWARD:
4234 if (cur.selIsMultiCell()) {
4235 cur.recordUndoInset(DELETE_UNDO);
4238 cell(cur.idx())->dispatch(cur, cmd);
4242 if (!cur.selection())
4244 if (cur.selIsMultiCell()) {
4248 cell(cur.idx())->dispatch(cur, cmd);
4251 case LFUN_CLIPBOARD_PASTE:
4252 case LFUN_PRIMARY_SELECTION_PASTE: {
4253 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4254 theClipboard().getAsText() :
4255 theSelection().get();
4258 // pass to InsertPlaintextString, but
4259 // only if we have multi-cell content
4260 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4261 cur.recordUndoInset(INSERT_UNDO);
4262 if (insertPlaintextString(cur.bv(), clip, false)) {
4263 // content has been replaced,
4264 // so cursor might be invalid
4265 cur.pos() = cur.lastpos();
4266 cur.pit() = cur.lastpit();
4267 bvcur.setCursor(cur);
4271 // Let the cell handle normal text
4272 cell(cur.idx())->dispatch(cur, cmd);
4277 if (!tabularStackDirty()) {
4278 if (!cur.selIsMultiCell())
4279 cell(cur.idx())->dispatch(cur, cmd);
4282 if (theClipboard().isInternal()) {
4283 cur.recordUndoInset(INSERT_UNDO);
4284 pasteClipboard(cur);
4288 case LFUN_FONT_EMPH:
4289 case LFUN_FONT_BOLD:
4290 case LFUN_FONT_BOLDSYMBOL:
4291 case LFUN_FONT_ROMAN:
4292 case LFUN_FONT_NOUN:
4293 case LFUN_FONT_ITAL:
4294 case LFUN_FONT_FRAK:
4295 case LFUN_FONT_TYPEWRITER:
4296 case LFUN_FONT_SANS:
4297 case LFUN_TEXTSTYLE_APPLY:
4298 case LFUN_TEXTSTYLE_UPDATE:
4299 case LFUN_FONT_SIZE:
4300 case LFUN_FONT_UNDERLINE:
4301 case LFUN_FONT_STRIKEOUT:
4302 case LFUN_FONT_UULINE:
4303 case LFUN_FONT_UWAVE:
4305 case LFUN_WORD_CAPITALIZE:
4306 case LFUN_WORD_UPCASE:
4307 case LFUN_WORD_LOWCASE:
4308 case LFUN_CHARS_TRANSPOSE:
4309 if (cur.selIsMultiCell()) {
4312 getSelection(cur, rs, re, cs, ce);
4313 Cursor tmpcur = cur;
4314 for (row_type r = rs; r <= re; ++r) {
4315 for (col_type c = cs; c <= ce; ++c) {
4316 // cursor follows cell:
4317 tmpcur.idx() = tabular.cellIndex(r, c);
4318 // select this cell only:
4321 tmpcur.resetAnchor();
4322 tmpcur.pit() = tmpcur.lastpit();
4323 tmpcur.pos() = tmpcur.top().lastpos();
4324 tmpcur.setCursor(tmpcur);
4325 tmpcur.setSelection();
4326 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4331 cell(cur.idx())->dispatch(cur, cmd);
4335 case LFUN_INSET_SETTINGS:
4336 // relay this lfun to Inset, not to the cell.
4337 Inset::doDispatch(cur, cmd);
4341 // we try to handle this event in the insets dispatch function.
4342 cell(cur.idx())->dispatch(cur, cmd);
4348 // function sets an object as defined in func_status.h:
4349 // states OK, Unknown, Disabled, On, Off.
4350 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4351 FuncStatus & status) const
4353 switch (cmd.action()) {
4354 case LFUN_INSET_MODIFY: {
4355 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4358 // FIXME: We only check for the very first argument...
4359 string const s = cmd.getArg(1);
4360 // We always enable the lfun if it is coming from the dialog
4361 // because the dialog makes sure all the settings are valid,
4362 // even though the first argument might not be valid now.
4363 if (s == "from-dialog") {
4364 status.setEnabled(true);
4368 int action = Tabular::LAST_ACTION;
4370 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4371 if (tabularFeature[i].feature == s) {
4372 action = tabularFeature[i].action;
4376 if (action == Tabular::LAST_ACTION) {
4378 status.setUnknown(true);
4382 string const argument = cmd.getLongArg(2);
4384 row_type sel_row_start = 0;
4385 row_type sel_row_end = 0;
4386 col_type sel_col_start = 0;
4387 col_type sel_col_end = 0;
4388 Tabular::ltType dummyltt;
4391 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4394 case Tabular::SET_PWIDTH:
4395 case Tabular::SET_MPWIDTH:
4396 case Tabular::SET_SPECIAL_COLUMN:
4397 case Tabular::SET_SPECIAL_MULTICOLUMN:
4398 case Tabular::APPEND_ROW:
4399 case Tabular::APPEND_COLUMN:
4400 case Tabular::DELETE_ROW:
4401 case Tabular::DELETE_COLUMN:
4402 case Tabular::COPY_ROW:
4403 case Tabular::COPY_COLUMN:
4404 case Tabular::SET_TOP_SPACE:
4405 case Tabular::SET_BOTTOM_SPACE:
4406 case Tabular::SET_INTERLINE_SPACE:
4410 case Tabular::SET_TABULAR_WIDTH:
4411 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4412 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4415 case Tabular::SET_DECIMAL_POINT:
4417 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4420 case Tabular::SET_MULTICOLUMN:
4421 case Tabular::UNSET_MULTICOLUMN:
4422 case Tabular::MULTICOLUMN:
4423 // If a row is set as longtable caption, it must not be allowed
4424 // to unset that this row is a multicolumn.
4425 status.setEnabled(sel_row_start == sel_row_end
4426 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4427 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4430 case Tabular::SET_MULTIROW:
4431 case Tabular::UNSET_MULTIROW:
4432 case Tabular::MULTIROW:
4433 // If a row is set as longtable caption, it must not be allowed
4434 // to unset that this row is a multirow.
4435 status.setEnabled(sel_col_start == sel_col_end
4436 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4437 status.setOnOff(tabular.isMultiRow(cur.idx()));
4440 case Tabular::SET_ALL_LINES:
4441 case Tabular::UNSET_ALL_LINES:
4442 case Tabular::SET_BORDER_LINES:
4443 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4446 case Tabular::SET_LINE_TOP:
4447 case Tabular::SET_LINE_BOTTOM:
4448 case Tabular::SET_LINE_LEFT:
4449 case Tabular::SET_LINE_RIGHT:
4450 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4453 case Tabular::TOGGLE_LINE_TOP:
4454 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4455 status.setOnOff(tabular.topLine(cur.idx()));
4458 case Tabular::TOGGLE_LINE_BOTTOM:
4459 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4460 status.setOnOff(tabular.bottomLine(cur.idx()));
4463 case Tabular::TOGGLE_LINE_LEFT:
4464 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4465 status.setOnOff(tabular.leftLine(cur.idx()));
4468 case Tabular::TOGGLE_LINE_RIGHT:
4469 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4470 status.setOnOff(tabular.rightLine(cur.idx()));
4473 // multirow cells only inherit the alignment of the column if the column has
4474 // no width, otherwise they are left-aligned
4475 // therefore allow always left but right and center only if there is no width
4476 case Tabular::M_ALIGN_LEFT:
4478 case Tabular::ALIGN_LEFT:
4479 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4482 case Tabular::M_ALIGN_RIGHT:
4484 case Tabular::ALIGN_RIGHT:
4485 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4486 && !tabular.getPWidth(cur.idx()).zero()));
4487 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4490 case Tabular::M_ALIGN_CENTER:
4492 case Tabular::ALIGN_CENTER:
4493 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4494 && !tabular.getPWidth(cur.idx()).zero()));
4495 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4498 case Tabular::ALIGN_BLOCK:
4499 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4500 && !tabular.isMultiRow(cur.idx()));
4501 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4504 case Tabular::ALIGN_DECIMAL:
4505 status.setEnabled(!tabular.isMultiRow(cur.idx())
4506 && !tabular.isMultiColumn(cur.idx()));
4507 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4510 case Tabular::M_VALIGN_TOP:
4512 case Tabular::VALIGN_TOP:
4513 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4514 && !tabular.isMultiRow(cur.idx()));
4516 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4519 case Tabular::M_VALIGN_BOTTOM:
4521 case Tabular::VALIGN_BOTTOM:
4522 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4523 && !tabular.isMultiRow(cur.idx()));
4525 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4528 case Tabular::M_VALIGN_MIDDLE:
4530 case Tabular::VALIGN_MIDDLE:
4531 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4532 && !tabular.isMultiRow(cur.idx()));
4534 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4537 case Tabular::SET_LONGTABULAR:
4538 // setting as longtable is not allowed when table is inside a float
4539 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4540 || cur.innerInsetOfType(WRAP_CODE) != 0)
4541 status.setEnabled(false);
4543 status.setEnabled(true);
4544 status.setOnOff(tabular.is_long_tabular);
4547 case Tabular::UNSET_LONGTABULAR:
4548 status.setOnOff(!tabular.is_long_tabular);
4551 case Tabular::TOGGLE_ROTATE_TABULAR:
4552 case Tabular::SET_ROTATE_TABULAR:
4553 status.setEnabled(tabular.tabular_width.zero());
4554 status.setOnOff(tabular.rotate);
4557 case Tabular::TABULAR_VALIGN_TOP:
4558 status.setEnabled(tabular.tabular_width.zero());
4559 status.setOnOff(tabular.tabular_valignment
4560 == Tabular::LYX_VALIGN_TOP);
4562 case Tabular::TABULAR_VALIGN_MIDDLE:
4563 status.setEnabled(tabular.tabular_width.zero());
4564 status.setOnOff(tabular.tabular_valignment
4565 == Tabular::LYX_VALIGN_MIDDLE);
4567 case Tabular::TABULAR_VALIGN_BOTTOM:
4568 status.setEnabled(tabular.tabular_width.zero());
4569 status.setOnOff(tabular.tabular_valignment
4570 == Tabular::LYX_VALIGN_BOTTOM);
4573 case Tabular::LONGTABULAR_ALIGN_LEFT:
4574 status.setOnOff(tabular.longtabular_alignment
4575 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4577 case Tabular::LONGTABULAR_ALIGN_CENTER:
4578 status.setOnOff(tabular.longtabular_alignment
4579 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4581 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4582 status.setOnOff(tabular.longtabular_alignment
4583 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4586 case Tabular::UNSET_ROTATE_TABULAR:
4587 status.setOnOff(!tabular.rotate);
4590 case Tabular::TOGGLE_ROTATE_CELL:
4591 case Tabular::SET_ROTATE_CELL:
4592 status.setOnOff(!oneCellHasRotationState(false,
4593 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4596 case Tabular::UNSET_ROTATE_CELL:
4597 status.setOnOff(!oneCellHasRotationState(true,
4598 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4601 case Tabular::SET_USEBOX:
4602 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4605 // every row can only be one thing:
4606 // either a footer or header
4607 case Tabular::SET_LTFIRSTHEAD:
4608 status.setEnabled(sel_row_start == sel_row_end);
4609 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4612 case Tabular::UNSET_LTFIRSTHEAD:
4613 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4614 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4617 case Tabular::SET_LTHEAD:
4618 status.setEnabled(sel_row_start == sel_row_end);
4619 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4622 case Tabular::UNSET_LTHEAD:
4623 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4624 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4627 case Tabular::SET_LTFOOT:
4628 status.setEnabled(sel_row_start == sel_row_end);
4629 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4632 case Tabular::UNSET_LTFOOT:
4633 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4634 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4637 case Tabular::SET_LTLASTFOOT:
4638 status.setEnabled(sel_row_start == sel_row_end);
4639 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4642 case Tabular::UNSET_LTLASTFOOT:
4643 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4644 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4647 case Tabular::SET_LTNEWPAGE:
4648 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4651 // only one row in head/firsthead/foot/lasthead can be the caption
4652 // and a multirow cannot be set as caption
4653 case Tabular::SET_LTCAPTION:
4654 status.setEnabled(sel_row_start == sel_row_end
4655 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4656 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4657 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4658 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4659 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4660 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4661 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4662 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4663 && !tabular.isMultiRow(sel_row_start));
4664 status.setOnOff(tabular.ltCaption(sel_row_start));
4667 case Tabular::UNSET_LTCAPTION:
4668 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4671 case Tabular::TOGGLE_LTCAPTION:
4672 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4673 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4674 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4675 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4676 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4677 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4678 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4679 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4680 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4681 status.setOnOff(tabular.ltCaption(sel_row_start));
4684 case Tabular::SET_BOOKTABS:
4685 status.setOnOff(tabular.use_booktabs);
4688 case Tabular::UNSET_BOOKTABS:
4689 status.setOnOff(!tabular.use_booktabs);
4694 status.setEnabled(false);
4700 // These are only enabled inside tabular
4701 case LFUN_CELL_BACKWARD:
4702 case LFUN_CELL_FORWARD:
4703 status.setEnabled(true);
4706 // disable these with multiple cells selected
4707 case LFUN_INSET_INSERT:
4708 case LFUN_TABULAR_INSERT:
4709 case LFUN_FLEX_INSERT:
4710 case LFUN_FLOAT_INSERT:
4711 case LFUN_FLOAT_WIDE_INSERT:
4712 case LFUN_FOOTNOTE_INSERT:
4713 case LFUN_MARGINALNOTE_INSERT:
4714 case LFUN_MATH_INSERT:
4715 case LFUN_MATH_MODE:
4716 case LFUN_MATH_MUTATE:
4717 case LFUN_MATH_DISPLAY:
4718 case LFUN_NOTE_INSERT:
4719 case LFUN_ARGUMENT_INSERT:
4720 case LFUN_BOX_INSERT:
4721 case LFUN_BRANCH_INSERT:
4722 case LFUN_PHANTOM_INSERT:
4723 case LFUN_WRAP_INSERT:
4724 case LFUN_PREVIEW_INSERT:
4725 case LFUN_ERT_INSERT: {
4726 if (cur.selIsMultiCell()) {
4727 status.setEnabled(false);
4730 return cell(cur.idx())->getStatus(cur, cmd, status);
4733 // disable in non-fixed-width cells
4734 case LFUN_BREAK_PARAGRAPH:
4735 // multirow does not allow paragraph breaks
4736 if (tabular.isMultiRow(cur.idx())) {
4737 status.setEnabled(false);
4740 case LFUN_NEWLINE_INSERT: {
4741 if (tabular.getPWidth(cur.idx()).zero()) {
4742 status.setEnabled(false);
4745 return cell(cur.idx())->getStatus(cur, cmd, status);
4748 case LFUN_NEWPAGE_INSERT:
4749 status.setEnabled(false);
4753 if (tabularStackDirty() && theClipboard().isInternal()) {
4754 if (cur.selIsMultiCell()) {
4757 getSelection(cur, rs, re, cs, ce);
4758 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4759 && paste_tabular->nrows() == re - rs + 1)
4760 status.setEnabled(true);
4762 status.setEnabled(false);
4763 status.message(_("Selection size should match clipboard content."));
4766 status.setEnabled(true);
4769 return cell(cur.idx())->getStatus(cur, cmd, status);
4771 case LFUN_INSET_SETTINGS:
4772 // relay this lfun to Inset, not to the cell.
4773 return Inset::getStatus(cur, cmd, status);
4776 // we try to handle this event in the insets dispatch function.
4777 return cell(cur.idx())->getStatus(cur, cmd, status);
4783 Inset::DisplayType InsetTabular::display() const
4785 if (tabular.is_long_tabular) {
4786 switch (tabular.longtabular_alignment) {
4787 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4789 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4791 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4801 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4803 tabular.latex(os, runparams);
4807 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4809 os << '\n'; // output table on a new line
4810 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4811 tabular.plaintext(os, runparams, dp, false, 0);
4812 return PLAINTEXT_NEWLINE;
4816 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4821 // FIXME: Why not pass a proper DocIterator here?
4823 // if the table is inside a float it doesn't need the informaltable
4824 // wrapper. Search for it.
4825 for (master = owner(); master; master = master->owner())
4826 if (master->lyxCode() == FLOAT_CODE)
4831 os << "<informaltable>";
4834 ret += tabular.docbook(os, runparams);
4836 os << "</informaltable>";
4843 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4845 return tabular.xhtml(xs, rp);
4849 void InsetTabular::validate(LaTeXFeatures & features) const
4851 tabular.validate(features);
4853 // It'd be better to be able to get this from an InsetLayout, but at present
4854 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4855 if (features.runparams().flavor == OutputParams::HTML)
4856 features.addCSSSnippet(
4857 "table { border: 1px solid black; display: inline-block; }\n"
4858 "td { border: 1px solid black; padding: 0.5ex; }");
4862 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4864 return tabular.cellInset(idx);
4868 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4870 return tabular.cellInset(idx);
4874 void InsetTabular::cursorPos(BufferView const & bv,
4875 CursorSlice const & sl, bool boundary, int & x, int & y) const
4877 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4879 // y offset correction
4880 y += cellYPos(sl.idx());
4881 y += tabular.textVOffset(sl.idx());
4882 y += offset_valign_;
4884 // x offset correction
4885 x += cellXPos(sl.idx());
4886 x += tabular.textHOffset(sl.idx());
4887 x += ADD_TO_TABULAR_WIDTH;
4892 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4896 Inset const & inset = *tabular.cellInset(cell);
4897 Point o = bv.coordCache().getInsets().xy(&inset);
4898 int const xbeg = o.x_ - tabular.textHOffset(cell);
4899 int const xend = xbeg + tabular.cellWidth(cell);
4900 row_type const row = tabular.cellRow(cell);
4901 int const ybeg = o.y_ - tabular.rowAscent(row)
4902 - tabular.interRowSpace(row);
4903 int const yend = ybeg + tabular.cellHeight(cell);
4915 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4916 // << " ybeg=" << ybeg << " yend=" << yend
4917 // << " xx=" << xx << " yy=" << yy
4918 // << " dist=" << xx + yy << endl;
4923 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4925 //lyxerr << "InsetTabular::editXY: " << this << endl;
4926 cur.setSelection(false);
4928 cur.idx() = getNearestCell(cur.bv(), x, y);
4930 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4934 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4936 cur.idx() = getNearestCell(cur.bv(), x, y);
4937 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4941 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4943 idx_type idx_min = 0;
4944 int dist_min = numeric_limits<int>::max();
4945 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4946 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4947 int const d = dist(bv, i, x, y);
4958 int InsetTabular::cellYPos(idx_type const cell) const
4960 row_type row = tabular.cellRow(cell);
4962 for (row_type r = 0; r < row; ++r)
4963 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4964 + tabular.interRowSpace(r + 1);
4969 int InsetTabular::cellXPos(idx_type const cell) const
4971 col_type col = tabular.cellColumn(cell);
4973 for (col_type c = 0; c < col; ++c)
4974 lx += tabular.column_info[c].width;
4979 void InsetTabular::resetPos(Cursor & cur) const
4981 BufferView & bv = cur.bv();
4982 int const maxwidth = bv.workWidth();
4984 int const scx_old = scx_;
4985 int const i = cur.find(this);
4990 int const X2 = maxwidth;
4991 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4992 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4993 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4996 scx_ = X1 + 20 - x1;
4998 scx_ = X2 - 20 - x2;
5003 // only update if offset changed
5004 if (scx_ != scx_old)
5005 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5009 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5011 row_type const row = tabular.cellRow(cur.idx());
5012 col_type const col = tabular.cellColumn(cur.idx());
5014 if (isRightToLeft(cur)) {
5015 if (tabular.cellColumn(cur.idx()) == 0) {
5016 if (row == tabular.nrows() - 1)
5018 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5023 cur.idx() = tabular.getLastCellInRow(row - 1);
5025 cur.idx() = tabular.cellIndex(row, col - 1);
5028 if (tabular.isLastCell(cur.idx()))
5030 if (cur.idx() == tabular.getLastCellInRow(row))
5031 cur.idx() = tabular.cellIndex(row + 1, 0);
5033 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5034 cur.idx() = tabular.cellIndex(row, colnextcell);
5038 cur.boundary(false);
5040 if (cur.selIsMultiCell()) {
5041 cur.pit() = cur.lastpit();
5042 cur.pos() = cur.lastpos();
5050 // in visual mode, place cursor at extreme left or right
5052 switch(entry_from) {
5054 case ENTRY_DIRECTION_RIGHT:
5055 cur.posVisToRowExtremity(false /* !left */);
5057 case ENTRY_DIRECTION_LEFT:
5058 cur.posVisToRowExtremity(true /* left */);
5060 case ENTRY_DIRECTION_IGNORE:
5061 // nothing to do in this case
5065 cur.setCurrentFont();
5070 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5072 row_type const row = tabular.cellRow(cur.idx());
5073 col_type const col = tabular.cellColumn(cur.idx());
5075 if (isRightToLeft(cur)) {
5076 if (cur.idx() == tabular.getLastCellInRow(row)) {
5079 cur.idx() = tabular.getFirstCellInRow(row);
5080 cur.idx() = tabular.cellAbove(cur.idx());
5082 if (tabular.isLastCell(cur.idx()))
5084 if (cur.idx() == tabular.getLastCellInRow(row))
5085 cur.idx() = tabular.cellIndex(row + 1, 0);
5087 cur.idx() = tabular.cellIndex(row, col + 1);
5090 if (cur.idx() == 0) // first cell
5093 cur.idx() = tabular.getLastCellInRow(row - 1);
5095 cur.idx() = tabular.cellIndex(row, col - 1);
5098 if (cur.selIsMultiCell()) {
5099 cur.pit() = cur.lastpit();
5100 cur.pos() = cur.lastpos();
5105 cur.pit() = cur.lastpit();
5106 cur.pos() = cur.lastpos();
5108 // in visual mode, place cursor at extreme left or right
5110 switch(entry_from) {
5112 case ENTRY_DIRECTION_RIGHT:
5113 cur.posVisToRowExtremity(false /* !left */);
5115 case ENTRY_DIRECTION_LEFT:
5116 cur.posVisToRowExtremity(true /* left */);
5118 case ENTRY_DIRECTION_IGNORE:
5119 // nothing to do in this case
5123 cur.setCurrentFont();
5128 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5130 istringstream is(argument);
5133 if (insetCode(s) != TABULAR_CODE)
5137 size_t safe_guard = 0;
5142 if (safe_guard > 1000) {
5143 LYXERR0("parameter max count reached!");
5147 Tabular::Feature action = Tabular::LAST_ACTION;
5150 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5151 if (s != tabularFeature[i].feature)
5154 action = tabularFeature[i].action;
5157 if (action == Tabular::LAST_ACTION) {
5158 LYXERR0("Feature not found " << s);
5162 if (tabularFeature[i].need_value)
5164 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5165 tabularFeatures(cur, action, val);
5171 static void checkLongtableSpecial(Tabular::ltType & ltt,
5172 string const & special, bool & flag)
5174 if (special == "dl_above") {
5177 } else if (special == "dl_below") {
5178 ltt.bottomDL = flag;
5180 } else if (special == "empty") {
5190 bool InsetTabular::oneCellHasRotationState(bool rotated,
5191 row_type row_start, row_type row_end,
5192 col_type col_start, col_type col_end) const
5194 for (row_type r = row_start; r <= row_end; ++r)
5195 for (col_type c = col_start; c <= col_end; ++c)
5196 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5202 void InsetTabular::tabularFeatures(Cursor & cur,
5203 Tabular::Feature feature, string const & value)
5205 col_type sel_col_start;
5206 col_type sel_col_end;
5207 row_type sel_row_start;
5208 row_type sel_row_end;
5209 bool setLines = false;
5210 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5211 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5215 case Tabular::M_ALIGN_LEFT:
5216 case Tabular::ALIGN_LEFT:
5217 setAlign = LYX_ALIGN_LEFT;
5220 case Tabular::M_ALIGN_RIGHT:
5221 case Tabular::ALIGN_RIGHT:
5222 setAlign = LYX_ALIGN_RIGHT;
5225 case Tabular::M_ALIGN_CENTER:
5226 case Tabular::ALIGN_CENTER:
5227 setAlign = LYX_ALIGN_CENTER;
5230 case Tabular::ALIGN_BLOCK:
5231 setAlign = LYX_ALIGN_BLOCK;
5234 case Tabular::ALIGN_DECIMAL:
5235 setAlign = LYX_ALIGN_DECIMAL;
5238 case Tabular::M_VALIGN_TOP:
5239 case Tabular::VALIGN_TOP:
5240 setVAlign = Tabular::LYX_VALIGN_TOP;
5243 case Tabular::M_VALIGN_BOTTOM:
5244 case Tabular::VALIGN_BOTTOM:
5245 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5248 case Tabular::M_VALIGN_MIDDLE:
5249 case Tabular::VALIGN_MIDDLE:
5250 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5257 cur.recordUndoInset(ATOMIC_UNDO, this);
5259 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5260 row_type const row = tabular.cellRow(cur.idx());
5261 col_type const column = tabular.cellColumn(cur.idx());
5263 Tabular::ltType ltt;
5267 case Tabular::SET_TABULAR_WIDTH:
5268 tabular.setTabularWidth(Length(value));
5271 case Tabular::SET_PWIDTH: {
5272 Length const len(value);
5273 tabular.setColumnPWidth(cur, cur.idx(), len);
5275 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5276 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5280 case Tabular::SET_MPWIDTH:
5281 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5284 case Tabular::SET_MROFFSET:
5285 tabular.setMROffset(cur, cur.idx(), Length(value));
5288 case Tabular::SET_SPECIAL_COLUMN:
5289 case Tabular::SET_SPECIAL_MULTICOLUMN:
5290 if (value == "none")
5291 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5293 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5296 case Tabular::APPEND_ROW:
5297 // append the row into the tabular
5298 tabular.appendRow(cur.idx());
5301 case Tabular::APPEND_COLUMN:
5302 // append the column into the tabular
5303 tabular.appendColumn(cur.idx());
5304 cur.idx() = tabular.cellIndex(row, column);
5307 case Tabular::DELETE_ROW:
5308 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5309 tabular.deleteRow(sel_row_start);
5310 if (sel_row_start >= tabular.nrows())
5312 cur.idx() = tabular.cellIndex(sel_row_start, column);
5315 cur.setSelection(false);
5318 case Tabular::DELETE_COLUMN:
5319 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5320 tabular.deleteColumn(sel_col_start);
5321 if (sel_col_start >= tabular.ncols())
5323 cur.idx() = tabular.cellIndex(row, sel_col_start);
5326 cur.setSelection(false);
5329 case Tabular::COPY_ROW:
5330 tabular.copyRow(row);
5333 case Tabular::COPY_COLUMN:
5334 tabular.copyColumn(column);
5335 cur.idx() = tabular.cellIndex(row, column);
5338 case Tabular::SET_LINE_TOP:
5339 case Tabular::TOGGLE_LINE_TOP: {
5340 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5341 ? (value == "true") : !tabular.topLine(cur.idx());
5342 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5343 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5344 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5348 case Tabular::SET_LINE_BOTTOM:
5349 case Tabular::TOGGLE_LINE_BOTTOM: {
5350 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5351 ? (value == "true") : !tabular.bottomLine(cur.idx());
5352 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5353 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5354 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5358 case Tabular::SET_LINE_LEFT:
5359 case Tabular::TOGGLE_LINE_LEFT: {
5360 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5361 ? (value == "true") : !tabular.leftLine(cur.idx());
5362 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5363 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5364 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5368 case Tabular::SET_LINE_RIGHT:
5369 case Tabular::TOGGLE_LINE_RIGHT: {
5370 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5371 ? (value == "true") : !tabular.rightLine(cur.idx());
5372 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5373 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5374 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5378 case Tabular::M_ALIGN_LEFT:
5379 case Tabular::M_ALIGN_RIGHT:
5380 case Tabular::M_ALIGN_CENTER:
5381 case Tabular::ALIGN_LEFT:
5382 case Tabular::ALIGN_RIGHT:
5383 case Tabular::ALIGN_CENTER:
5384 case Tabular::ALIGN_BLOCK:
5385 case Tabular::ALIGN_DECIMAL:
5386 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5387 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5388 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5389 !tabular.getPWidth(c).zero());
5392 case Tabular::M_VALIGN_TOP:
5393 case Tabular::M_VALIGN_BOTTOM:
5394 case Tabular::M_VALIGN_MIDDLE:
5396 case Tabular::VALIGN_TOP:
5397 case Tabular::VALIGN_BOTTOM:
5398 case Tabular::VALIGN_MIDDLE:
5399 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5400 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5401 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5404 case Tabular::SET_MULTICOLUMN: {
5405 if (!cur.selection()) {
5406 // just multicol for one single cell
5407 // check whether we are completely in a multicol
5408 if (!tabular.isMultiColumn(cur.idx()))
5409 tabular.setMultiColumn(cur.idx(), 1,
5410 tabular.rightLine(cur.idx()));
5413 // we have a selection so this means we just add all this
5414 // cells to form a multicolumn cell
5415 idx_type const s_start = cur.selBegin().idx();
5416 row_type const col_start = tabular.cellColumn(s_start);
5417 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5418 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5419 tabular.rightLine(cur.selEnd().idx()));
5422 cur.setSelection(false);
5426 case Tabular::UNSET_MULTICOLUMN: {
5427 if (!cur.selection()) {
5428 if (tabular.isMultiColumn(cur.idx()))
5429 tabular.unsetMultiColumn(cur.idx());
5434 case Tabular::MULTICOLUMN: {
5435 if (!cur.selection()) {
5436 if (tabular.isMultiColumn(cur.idx()))
5437 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5439 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5443 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5444 row_type const r = sel_row_start;
5445 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5446 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5449 // If the selection contains at least one singlecol cell
5450 // or multiple multicol cells,
5451 // we assume the user will merge is to a single multicol
5453 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5455 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5459 case Tabular::SET_MULTIROW: {
5460 if (!cur.selection()) {
5461 // just multirow for one single cell
5462 // check whether we are completely in a multirow
5463 if (!tabular.isMultiRow(cur.idx()))
5464 tabular.setMultiRow(cur.idx(), 1,
5465 tabular.bottomLine(cur.idx()));
5468 // we have a selection so this means we just add all this
5469 // cells to form a multirow cell
5470 idx_type const s_start = cur.selBegin().idx();
5471 row_type const row_start = tabular.cellRow(s_start);
5472 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5473 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5474 tabular.bottomLine(cur.selEnd().idx()));
5477 cur.setSelection(false);
5481 case Tabular::UNSET_MULTIROW: {
5482 if (!cur.selection()) {
5483 if (tabular.isMultiRow(cur.idx()))
5484 tabular.unsetMultiRow(cur.idx());
5489 case Tabular::MULTIROW: {
5490 if (!cur.selection()) {
5491 if (tabular.isMultiRow(cur.idx()))
5492 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5494 tabularFeatures(cur, Tabular::SET_MULTIROW);
5498 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5499 col_type const c = sel_col_start;
5500 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5501 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5504 // If the selection contains at least one singlerow cell
5505 // or multiple multirow cells,
5506 // we assume the user will merge is to a single multirow
5508 tabularFeatures(cur, Tabular::SET_MULTIROW);
5510 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5514 case Tabular::SET_ALL_LINES:
5516 case Tabular::UNSET_ALL_LINES:
5517 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5518 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5519 idx_type const cell = tabular.cellIndex(r, c);
5520 tabular.setTopLine(cell, setLines);
5521 tabular.setBottomLine(cell, setLines);
5522 tabular.setRightLine(cell, setLines);
5523 tabular.setLeftLine(cell, setLines);
5527 case Tabular::SET_BORDER_LINES:
5528 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5529 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5530 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5532 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5533 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5534 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5538 case Tabular::SET_LONGTABULAR:
5539 tabular.is_long_tabular = true;
5542 case Tabular::UNSET_LONGTABULAR:
5543 for (row_type r = 0; r < tabular.nrows(); ++r) {
5544 if (tabular.ltCaption(r)) {
5545 cur.idx() = tabular.cellIndex(r, 0);
5548 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5551 tabular.is_long_tabular = false;
5554 case Tabular::SET_ROTATE_TABULAR:
5555 tabular.rotate = true;
5558 case Tabular::UNSET_ROTATE_TABULAR:
5559 tabular.rotate = false;
5562 case Tabular::TOGGLE_ROTATE_TABULAR:
5563 tabular.rotate = !tabular.rotate;
5566 case Tabular::TABULAR_VALIGN_TOP:
5567 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5570 case Tabular::TABULAR_VALIGN_MIDDLE:
5571 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5574 case Tabular::TABULAR_VALIGN_BOTTOM:
5575 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5578 case Tabular::LONGTABULAR_ALIGN_LEFT:
5579 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5582 case Tabular::LONGTABULAR_ALIGN_CENTER:
5583 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5586 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5587 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5592 case Tabular::SET_ROTATE_CELL:
5593 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5594 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5595 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5598 case Tabular::UNSET_ROTATE_CELL:
5599 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5600 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5601 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5604 case Tabular::TOGGLE_ROTATE_CELL:
5606 bool oneNotRotated = oneCellHasRotationState(false,
5607 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5609 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5610 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5611 tabular.setRotateCell(tabular.cellIndex(r, c),
5616 case Tabular::SET_USEBOX: {
5617 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5618 if (val == tabular.getUsebox(cur.idx()))
5619 val = Tabular::BOX_NONE;
5620 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5621 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5622 tabular.setUsebox(tabular.cellIndex(r, c), val);
5626 case Tabular::UNSET_LTFIRSTHEAD:
5628 case Tabular::SET_LTFIRSTHEAD:
5629 tabular.getRowOfLTFirstHead(row, ltt);
5630 checkLongtableSpecial(ltt, value, flag);
5631 tabular.setLTHead(row, flag, ltt, true);
5634 case Tabular::UNSET_LTHEAD:
5636 case Tabular::SET_LTHEAD:
5637 tabular.getRowOfLTHead(row, ltt);
5638 checkLongtableSpecial(ltt, value, flag);
5639 tabular.setLTHead(row, flag, ltt, false);
5642 case Tabular::UNSET_LTFOOT:
5644 case Tabular::SET_LTFOOT:
5645 tabular.getRowOfLTFoot(row, ltt);
5646 checkLongtableSpecial(ltt, value, flag);
5647 tabular.setLTFoot(row, flag, ltt, false);
5650 case Tabular::UNSET_LTLASTFOOT:
5652 case Tabular::SET_LTLASTFOOT:
5653 tabular.getRowOfLTLastFoot(row, ltt);
5654 checkLongtableSpecial(ltt, value, flag);
5655 tabular.setLTFoot(row, flag, ltt, true);
5658 case Tabular::SET_LTNEWPAGE:
5659 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5662 case Tabular::SET_LTCAPTION: {
5663 if (tabular.ltCaption(row))
5665 cur.idx() = tabular.setLTCaption(row, true);
5668 cur.setSelection(false);
5669 // If a row is set as caption, then also insert
5670 // a caption. Otherwise the LaTeX output is broken.
5671 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5672 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5676 case Tabular::UNSET_LTCAPTION: {
5677 if (!tabular.ltCaption(row))
5679 cur.idx() = tabular.setLTCaption(row, false);
5682 cur.setSelection(false);
5683 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5684 if (lyx::getStatus(fr).enabled())
5689 case Tabular::TOGGLE_LTCAPTION: {
5690 if (tabular.ltCaption(row))
5691 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5693 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5697 case Tabular::SET_BOOKTABS:
5698 tabular.use_booktabs = true;
5701 case Tabular::UNSET_BOOKTABS:
5702 tabular.use_booktabs = false;
5705 case Tabular::SET_TOP_SPACE: {
5707 if (value == "default")
5708 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5709 tabular.row_info[r].top_space_default = true;
5710 else if (value == "none")
5711 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5712 tabular.row_info[r].top_space_default = false;
5713 tabular.row_info[r].top_space = len;
5715 else if (isValidLength(value, &len))
5716 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5717 tabular.row_info[r].top_space_default = false;
5718 tabular.row_info[r].top_space = len;
5723 case Tabular::SET_BOTTOM_SPACE: {
5725 if (value == "default")
5726 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5727 tabular.row_info[r].bottom_space_default = true;
5728 else if (value == "none")
5729 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5730 tabular.row_info[r].bottom_space_default = false;
5731 tabular.row_info[r].bottom_space = len;
5733 else if (isValidLength(value, &len))
5734 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5735 tabular.row_info[r].bottom_space_default = false;
5736 tabular.row_info[r].bottom_space = len;
5741 case Tabular::SET_INTERLINE_SPACE: {
5743 if (value == "default")
5744 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5745 tabular.row_info[r].interline_space_default = true;
5746 else if (value == "none")
5747 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5748 tabular.row_info[r].interline_space_default = false;
5749 tabular.row_info[r].interline_space = len;
5751 else if (isValidLength(value, &len))
5752 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5753 tabular.row_info[r].interline_space_default = false;
5754 tabular.row_info[r].interline_space = len;
5759 case Tabular::SET_DECIMAL_POINT:
5760 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5761 tabular.column_info[c].decimal_point = from_utf8(value);
5764 // dummy stuff just to avoid warnings
5765 case Tabular::LAST_ACTION:
5771 bool InsetTabular::copySelection(Cursor & cur)
5773 if (!cur.selection())
5778 getSelection(cur, rs, re, cs, ce);
5780 paste_tabular.reset(new Tabular(tabular));
5782 for (row_type r = 0; r < rs; ++r)
5783 paste_tabular->deleteRow(0);
5785 row_type const rows = re - rs + 1;
5786 while (paste_tabular->nrows() > rows)
5787 paste_tabular->deleteRow(rows);
5789 for (col_type c = 0; c < cs; ++c)
5790 paste_tabular->deleteColumn(0);
5792 col_type const columns = ce - cs + 1;
5793 while (paste_tabular->ncols() > columns)
5794 paste_tabular->deleteColumn(columns);
5796 paste_tabular->setBuffer(tabular.buffer());
5798 odocstringstream os;
5799 OutputParams const runparams(0);
5800 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5801 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5802 cap::copySelection(cur, os.str());
5804 // mark tabular stack dirty
5805 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5806 // when we (hopefully) have a one-for-all paste mechanism.
5807 // This must be called after cap::copySelection.
5808 dirtyTabularStack(true);
5814 bool InsetTabular::pasteClipboard(Cursor & cur)
5818 col_type actcol = tabular.cellColumn(cur.idx());
5819 row_type actrow = tabular.cellRow(cur.idx());
5821 if (cur.selIsMultiCell()) {
5824 getSelection(cur, actrow, re, actcol, ce);
5827 for (row_type r1 = 0, r2 = actrow;
5828 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5830 for (col_type c1 = 0, c2 = actcol;
5831 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5833 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5834 tabular.isPartOfMultiColumn(r2, c2))
5836 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5840 if (tabular.isPartOfMultiColumn(r2, c2)) {
5844 shared_ptr<InsetTableCell> inset(
5845 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5846 tabular.setCellInset(r2, c2, inset);
5847 // FIXME?: why do we need to do this explicitly? (EL)
5848 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5850 // FIXME: change tracking (MG)
5851 inset->setChange(Change(buffer().params().trackChanges ?
5852 Change::INSERTED : Change::UNCHANGED));
5860 void InsetTabular::cutSelection(Cursor & cur)
5862 if (!cur.selection())
5867 getSelection(cur, rs, re, cs, ce);
5868 for (row_type r = rs; r <= re; ++r) {
5869 for (col_type c = cs; c <= ce; ++c) {
5870 shared_ptr<InsetTableCell> t
5871 = cell(tabular.cellIndex(r, c));
5872 if (buffer().params().trackChanges)
5873 // FIXME: Change tracking (MG)
5874 t->setChange(Change(Change::DELETED));
5880 // cursor position might be invalid now
5881 if (cur.pit() > cur.lastpit())
5882 cur.pit() = cur.lastpit();
5883 if (cur.pos() > cur.lastpos())
5884 cur.pos() = cur.lastpos();
5885 cur.clearSelection();
5889 bool InsetTabular::isRightToLeft(Cursor & cur) const
5891 LASSERT(cur.depth() > 1, /**/);
5892 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5893 pos_type const parentpos = cur[cur.depth() - 2].pos();
5894 return parentpar.getFontSettings(buffer().params(),
5895 parentpos).language()->rightToLeft();
5899 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5902 LASSERT(stidx <= enidx, return docstring());
5904 col_type const col1 = tabular.cellColumn(stidx);
5905 col_type const col2 = tabular.cellColumn(enidx);
5906 row_type const row1 = tabular.cellRow(stidx);
5907 row_type const row2 = tabular.cellRow(enidx);
5909 for (col_type col = col1; col <= col2; col++)
5910 for (row_type row = row1; row <= row2; row++) {
5915 retval += tabular.cellInset(row, col)->asString(intoInsets);
5921 void InsetTabular::getSelection(Cursor & cur,
5922 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5924 CursorSlice const & beg = cur.selBegin();
5925 CursorSlice const & end = cur.selEnd();
5926 cs = tabular.cellColumn(beg.idx());
5927 ce = tabular.cellColumn(end.idx());
5931 rs = tabular.cellRow(beg.idx());
5932 re = tabular.cellRow(end.idx());
5938 Text * InsetTabular::getText(int idx) const
5940 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5944 void InsetTabular::setChange(Change const & change)
5946 for (idx_type idx = 0; idx < nargs(); ++idx)
5947 cell(idx)->setChange(change);
5951 void InsetTabular::acceptChanges()
5953 for (idx_type idx = 0; idx < nargs(); ++idx)
5954 cell(idx)->acceptChanges();
5958 void InsetTabular::rejectChanges()
5960 for (idx_type idx = 0; idx < nargs(); ++idx)
5961 cell(idx)->rejectChanges();
5965 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5967 return tabular.getPWidth(cell).zero();
5971 bool InsetTabular::forcePlainLayout(idx_type cell) const
5973 return !tabular.getPWidth(cell).zero();
5977 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5980 if (buf.length() <= 0)
5985 col_type maxCols = 1;
5986 size_t const len = buf.length();
5990 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5998 maxCols = max(cols, maxCols);
6004 maxCols = max(cols, maxCols);
6010 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6011 loctab = paste_tabular.get();
6013 dirtyTabularStack(true);
6016 cell = bv.cursor().idx();
6017 ocol = tabular.cellColumn(cell);
6018 row = tabular.cellRow(cell);
6022 idx_type const cells = loctab->numberofcells;
6025 rows = loctab->nrows();
6026 col_type const columns = loctab->ncols();
6028 while (cell < cells && p < len && row < rows &&
6029 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6035 // we can only set this if we are not too far right
6036 if (cols < columns) {
6037 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6038 Font const font = bv.textMetrics(&inset->text()).
6040 inset->setText(buf.substr(op, p - op), font,
6041 buffer().params().trackChanges);
6047 // we can only set this if we are not too far right
6048 if (cols < columns) {
6049 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6050 Font const font = bv.textMetrics(&inset->text()).
6052 inset->setText(buf.substr(op, p - op), font,
6053 buffer().params().trackChanges);
6058 cell = loctab->cellIndex(row, cols);
6064 // check for the last cell if there is no trailing '\n'
6065 if (cell < cells && op < len) {
6066 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6067 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6068 inset->setText(buf.substr(op, len - op), font,
6069 buffer().params().trackChanges);
6075 void InsetTabular::addPreview(DocIterator const & inset_pos,
6076 PreviewLoader & loader) const
6078 DocIterator cell_pos = inset_pos;
6080 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6081 for (row_type r = 0; r < tabular.nrows(); ++r) {
6082 for (col_type c = 0; c < tabular.ncols(); ++c) {
6083 cell_pos.top().idx() = tabular.cellIndex(r, c);
6084 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6090 bool InsetTabular::completionSupported(Cursor const & cur) const
6092 Cursor const & bvCur = cur.bv().cursor();
6093 if (&bvCur.inset() != this)
6095 return cur.text()->completionSupported(cur);
6099 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6101 return completionSupported(cur);
6105 bool InsetTabular::automaticInlineCompletion() const
6107 return lyxrc.completion_inline_text;
6111 bool InsetTabular::automaticPopupCompletion() const
6113 return lyxrc.completion_popup_text;
6117 bool InsetTabular::showCompletionCursor() const
6119 return lyxrc.completion_cursor_text;
6123 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6125 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6129 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6131 if (!completionSupported(cur))
6133 return cur.text()->completionPrefix(cur);
6137 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6139 if (!completionSupported(cur))
6142 return cur.text()->insertCompletion(cur, s, finished);
6146 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6147 Dimension & dim) const
6149 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6150 tm.completionPosAndDim(cur, x, y, dim);
6154 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6156 istringstream data(in);
6158 lex.setStream(data);
6165 if (!lex || token != "tabular") {
6166 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6170 // This is part of the inset proper that is usually swallowed
6171 // by Buffer::readInset
6173 if (!lex || token != "Tabular") {
6174 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6182 string InsetTabular::params2string(InsetTabular const & inset)
6185 data << "tabular" << ' ';
6187 data << "\\end_inset\n";