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"
35 #include "InsetList.h"
37 #include "LaTeXFeatures.h"
41 #include "MetricsInfo.h"
42 #include "OutputParams.h"
43 #include "output_xhtml.h"
44 #include "Paragraph.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
47 #include "TextClass.h"
48 #include "TextMetrics.h"
50 #include "frontends/Application.h"
51 #include "frontends/alert.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
56 #include "support/convert.h"
57 #include "support/debug.h"
58 #include "support/docstream.h"
59 #include "support/FileName.h"
60 #include "support/gettext.h"
61 #include "support/lassert.h"
62 #include "support/lstrings.h"
64 #include <boost/scoped_ptr.hpp>
72 using namespace lyx::support;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
108 TabularFeature tabularFeature[] =
110 // the SET/UNSET actions are used by the table dialog,
111 // the TOGGLE actions by the table toolbar buttons
112 { Tabular::APPEND_ROW, "append-row", false },
113 { Tabular::APPEND_COLUMN, "append-column", false },
114 { Tabular::DELETE_ROW, "delete-row", false },
115 { Tabular::DELETE_COLUMN, "delete-column", false },
116 { Tabular::COPY_ROW, "copy-row", false },
117 { Tabular::COPY_COLUMN, "copy-column", false },
118 { Tabular::SET_LINE_TOP, "set-line-top", true },
119 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
120 { Tabular::SET_LINE_LEFT, "set-line-left", true },
121 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
122 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
123 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
124 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
125 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
126 { Tabular::ALIGN_LEFT, "align-left", false },
127 { Tabular::ALIGN_RIGHT, "align-right", false },
128 { Tabular::ALIGN_CENTER, "align-center", false },
129 { Tabular::ALIGN_BLOCK, "align-block", false },
130 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
131 { Tabular::VALIGN_TOP, "valign-top", false },
132 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
133 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
134 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
135 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
136 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
137 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
138 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
139 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
140 { Tabular::MULTICOLUMN, "multicolumn", false },
141 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
142 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
143 { Tabular::MULTIROW, "multirow", false },
144 { Tabular::SET_MULTIROW, "set-multirow", false },
145 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
146 { Tabular::SET_MROFFSET, "set-mroffset", true },
147 { Tabular::SET_ALL_LINES, "set-all-lines", false },
148 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
149 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
150 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
151 { Tabular::SET_PWIDTH, "set-pwidth", true },
152 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
153 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
154 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
155 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
156 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
157 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
158 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
159 { Tabular::SET_USEBOX, "set-usebox", true },
160 { Tabular::SET_LTHEAD, "set-lthead", true },
161 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
162 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
163 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
164 { Tabular::SET_LTFOOT, "set-ltfoot", true },
165 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
166 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
167 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
168 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
169 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
170 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
171 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
172 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
173 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
174 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
175 { Tabular::SET_BOOKTABS, "set-booktabs", false },
176 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
177 { Tabular::SET_TOP_SPACE, "set-top-space", true },
178 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
179 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
180 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
181 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
182 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
183 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
184 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
185 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
186 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
187 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
188 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
189 { Tabular::LAST_ACTION, "", false }
193 string const tostr(LyXAlignment const & num)
198 case LYX_ALIGN_BLOCK:
202 case LYX_ALIGN_CENTER:
204 case LYX_ALIGN_RIGHT:
206 case LYX_ALIGN_LAYOUT:
208 case LYX_ALIGN_SPECIAL:
210 case LYX_ALIGN_DECIMAL:
217 string const tostr(Tabular::HAlignment const & num)
220 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
222 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
224 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
231 string const tostr(Tabular::VAlignment const & num)
234 case Tabular::LYX_VALIGN_TOP:
236 case Tabular::LYX_VALIGN_MIDDLE:
238 case Tabular::LYX_VALIGN_BOTTOM:
245 string const tostr(Tabular::BoxType const & num)
248 case Tabular::BOX_NONE:
250 case Tabular::BOX_PARBOX:
252 case Tabular::BOX_MINIPAGE:
259 // I would have liked a fromstr template a lot better. (Lgb)
260 bool string2type(string const str, LyXAlignment & num)
263 num = LYX_ALIGN_NONE;
264 else if (str == "block")
265 num = LYX_ALIGN_BLOCK;
266 else if (str == "left")
267 num = LYX_ALIGN_LEFT;
268 else if (str == "center")
269 num = LYX_ALIGN_CENTER;
270 else if (str == "right")
271 num = LYX_ALIGN_RIGHT;
272 else if (str == "decimal")
273 num = LYX_ALIGN_DECIMAL;
280 bool string2type(string const str, Tabular::HAlignment & num)
283 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
284 else if (str == "center" )
285 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
286 else if (str == "right")
287 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
294 bool string2type(string const str, Tabular::VAlignment & num)
297 num = Tabular::LYX_VALIGN_TOP;
298 else if (str == "middle" )
299 num = Tabular::LYX_VALIGN_MIDDLE;
300 else if (str == "bottom")
301 num = Tabular::LYX_VALIGN_BOTTOM;
308 bool string2type(string const str, Tabular::BoxType & num)
311 num = Tabular::BOX_NONE;
312 else if (str == "parbox")
313 num = Tabular::BOX_PARBOX;
314 else if (str == "minipage")
315 num = Tabular::BOX_MINIPAGE;
322 bool string2type(string const str, bool & num)
326 else if (str == "false")
334 bool getTokenValue(string const & str, char const * token, string & ret)
337 size_t token_length = strlen(token);
338 size_t pos = str.find(token);
340 if (pos == string::npos || pos + token_length + 1 >= str.length()
341 || str[pos + token_length] != '=')
343 pos += token_length + 1;
345 if (ch != '"' && ch != '\'') { // only read till next space
349 while (pos < str.length() - 1 && str[++pos] != ch)
356 bool getTokenValue(string const & str, char const * token, docstring & ret)
359 bool const success = getTokenValue(str, token, tmp);
360 ret = from_utf8(tmp);
365 bool getTokenValue(string const & str, char const * token, int & num)
369 if (!getTokenValue(str, token, tmp))
371 num = convert<int>(tmp);
376 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
379 return getTokenValue(str, token, tmp) && string2type(tmp, num);
383 bool getTokenValue(string const & str, char const * token,
384 Tabular::HAlignment & num)
387 return getTokenValue(str, token, tmp) && string2type(tmp, num);
391 bool getTokenValue(string const & str, char const * token,
392 Tabular::VAlignment & num)
395 return getTokenValue(str, token, tmp) && string2type(tmp, num);
399 bool getTokenValue(string const & str, char const * token,
400 Tabular::BoxType & num)
403 return getTokenValue(str, token, tmp) && string2type(tmp, num);
407 bool getTokenValue(string const & str, char const * token, bool & flag)
409 // set the flag always to false as this should be the default for bools
410 // not in the file-format.
413 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
417 bool getTokenValue(string const & str, char const * token, Length & len)
419 // set the length to be zero() as default as this it should be if not
420 // in the file format.
423 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
427 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
432 if (!getTokenValue(str, token, tmp))
434 if (tmp == "default") {
438 return isValidLength(tmp, &len);
442 void l_getline(istream & is, string & str)
445 while (str.empty()) {
447 if (!str.empty() && str[str.length() - 1] == '\r')
448 str.erase(str.length() - 1);
453 string const write_attribute(string const & name, T const & t)
455 string const s = tostr(t);
456 return s.empty() ? s : " " + name + "=\"" + s + "\"";
460 string const write_attribute(string const & name, string const & t)
462 return t.empty() ? t : " " + name + "=\"" + t + "\"";
467 string const write_attribute(string const & name, docstring const & t)
469 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
474 string const write_attribute(string const & name, bool const & b)
476 // we write only true attribute values so we remove a bit of the
477 // file format bloat for tabulars.
478 return b ? write_attribute(name, convert<string>(b)) : string();
483 string const write_attribute(string const & name, int const & i)
485 // we write only true attribute values so we remove a bit of the
486 // file format bloat for tabulars.
487 return i ? write_attribute(name, convert<string>(i)) : string();
492 string const write_attribute(string const & name, Tabular::idx_type const & i)
494 // we write only true attribute values so we remove a bit of the
495 // file format bloat for tabulars.
496 return i ? write_attribute(name, convert<string>(i)) : string();
501 string const write_attribute(string const & name, Length const & value)
503 // we write only the value if we really have one same reson as above.
504 return value.zero() ? string() : write_attribute(name, value.asString());
510 string const featureAsString(Tabular::Feature action)
512 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
513 if (tabularFeature[i].action == action)
514 return tabularFeature[i].feature;
520 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
522 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
523 for (; dit; dit.forwardChar())
524 if (dit.inTexted() && dit.depth() == 1
525 && dit.paragraph().find(align_d, false, false, dit.pos()))
532 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
534 InsetTableCell tail = InsetTableCell(head);
535 DocIterator const dit = separatorPos(&head, align_d);
538 pit_type const psize = head.paragraphs().front().size();
539 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
540 tail.paragraphs().front().eraseChars(0,
541 dit.pos() < psize ? dit.pos() + 1 : psize, false);
548 /////////////////////////////////////////////////////////////////////
552 /////////////////////////////////////////////////////////////////////
555 Tabular::CellData::CellData(Buffer * buf)
558 multicolumn(Tabular::CELL_NORMAL),
559 multirow(Tabular::CELL_NORMAL),
560 alignment(LYX_ALIGN_CENTER),
561 valignment(LYX_VALIGN_TOP),
571 inset(new InsetTableCell(buf))
573 inset->setBuffer(*buf);
577 Tabular::CellData::CellData(CellData const & cs)
580 multicolumn(cs.multicolumn),
581 multirow(cs.multirow),
582 mroffset(cs.mroffset),
583 alignment(cs.alignment),
584 valignment(cs.valignment),
585 decimal_hoffset(cs.decimal_hoffset),
586 decimal_width(cs.decimal_width),
588 top_line(cs.top_line),
589 bottom_line(cs.bottom_line),
590 left_line(cs.left_line),
591 right_line(cs.right_line),
594 align_special(cs.align_special),
596 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
600 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
606 void Tabular::CellData::swap(CellData & rhs)
608 std::swap(cellno, rhs.cellno);
609 std::swap(width, rhs.width);
610 std::swap(multicolumn, rhs.multicolumn);
611 std::swap(multirow, rhs.multirow);
612 std::swap(mroffset, rhs.mroffset);
613 std::swap(alignment, rhs.alignment);
614 std::swap(valignment, rhs.valignment);
615 std::swap(decimal_hoffset, rhs.decimal_hoffset);
616 std::swap(decimal_width, rhs.decimal_width);
617 std::swap(voffset, rhs.voffset);
618 std::swap(top_line, rhs.top_line);
619 std::swap(bottom_line, rhs.bottom_line);
620 std::swap(left_line, rhs.left_line);
621 std::swap(right_line, rhs.right_line);
622 std::swap(usebox, rhs.usebox);
623 std::swap(rotate, rhs.rotate);
624 std::swap(align_special, rhs.align_special);
625 p_width.swap(rhs.p_width);
626 inset.swap(rhs.inset);
630 Tabular::RowData::RowData()
633 top_space_default(false),
634 bottom_space_default(false),
635 interline_space_default(false),
645 Tabular::ColumnData::ColumnData()
646 : alignment(LYX_ALIGN_CENTER),
647 valignment(LYX_VALIGN_TOP),
653 Tabular::ltType::ltType()
660 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
662 init(buffer, rows_arg, columns_arg);
666 void Tabular::setBuffer(Buffer & buffer)
669 for (row_type i = 0; i < nrows(); ++i)
670 for (col_type j = 0; j < ncols(); ++j)
671 cell_info[i][j].inset->setBuffer(*buffer_);
675 // activates all lines and sets all widths to 0
676 void Tabular::init(Buffer * buf, row_type rows_arg,
677 col_type columns_arg)
680 row_info = row_vector(rows_arg);
681 column_info = column_vector(columns_arg);
682 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
683 row_info.reserve(10);
684 column_info.reserve(10);
685 cell_info.reserve(100);
687 is_long_tabular = false;
688 tabular_valignment = LYX_VALIGN_MIDDLE;
689 tabular_width = Length();
690 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
692 use_booktabs = false;
693 // set silly default lines
694 for (row_type r = 0; r < nrows(); ++r)
695 for (col_type c = 0; c < ncols(); ++c) {
696 cell_info[r][c].inset->setBuffer(*buffer_);
697 cell_info[r][c].top_line = true;
698 cell_info[r][c].left_line = true;
699 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
700 cell_info[r][c].right_line = c == ncols() - 1;
705 void Tabular::deleteRow(row_type const row)
707 // Not allowed to delete last row
711 for (col_type c = 0; c < ncols(); ++c) {
712 // Care about multirow cells
713 if (row + 1 < nrows() &&
714 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
715 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
716 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
719 row_info.erase(row_info.begin() + row);
720 cell_info.erase(cell_info.begin() + row);
725 void Tabular::copyRow(row_type const row)
727 insertRow(row, true);
731 void Tabular::appendRow(row_type row)
733 insertRow(row, false);
737 void Tabular::insertRow(row_type const row, bool copy)
739 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
740 cell_info.insert(cell_info.begin() + row + 1,
741 cell_vector(0, CellData(buffer_)));
743 for (col_type c = 0; c < ncols(); ++c) {
744 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
745 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
746 if (buffer().params().trackChanges)
747 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
748 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
749 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
753 for (col_type c = 0; c < ncols(); ++c) {
754 if (isPartOfMultiRow(row, c))
756 // inherit line settings
757 idx_type const i = cellIndex(row + 1, c);
758 idx_type const j = cellIndex(row, c);
759 setLeftLine(i, leftLine(j));
760 setRightLine(i, rightLine(j));
761 setTopLine(i, topLine(j));
762 if (topLine(j) && bottomLine(j)) {
763 setBottomLine(i, true);
764 setBottomLine(j, false);
766 // mark track changes
767 if (buffer().params().trackChanges)
768 cellInfo(i).inset->setChange(Change(Change::INSERTED));
773 void Tabular::deleteColumn(col_type const col)
775 // Not allowed to delete last column
779 for (row_type r = 0; r < nrows(); ++r) {
780 // Care about multicolumn cells
781 if (col + 1 < ncols() &&
782 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
783 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
784 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
786 cell_info[r].erase(cell_info[r].begin() + col);
788 column_info.erase(column_info.begin() + col);
793 void Tabular::copyColumn(col_type const col)
795 insertColumn(col, true);
799 void Tabular::appendColumn(col_type col)
801 insertColumn(col, false);
805 void Tabular::insertColumn(col_type const col, bool copy)
807 BufferParams const & bp = buffer().params();
808 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
810 for (row_type r = 0; r < nrows(); ++r) {
811 cell_info[r].insert(cell_info[r].begin() + col + 1,
812 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
814 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
815 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
816 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
819 for (row_type r = 0; r < nrows(); ++r) {
820 // inherit line settings
821 idx_type const i = cellIndex(r, col + 1);
822 idx_type const j = cellIndex(r, col);
823 setBottomLine(i, bottomLine(j));
824 setTopLine(i, topLine(j));
825 setLeftLine(i, leftLine(j));
826 if (rightLine(j) && rightLine(j)) {
827 setRightLine(i, true);
828 setRightLine(j, false);
830 if (buffer().params().trackChanges)
831 cellInfo(i).inset->setChange(Change(Change::INSERTED));
836 void Tabular::updateIndexes()
841 for (row_type row = 0; row < nrows(); ++row)
842 for (col_type column = 0; column < ncols(); ++column) {
843 if (!isPartOfMultiColumn(row, column)
844 && !isPartOfMultiRow(row, column))
846 if (isPartOfMultiRow(row, column))
847 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
849 cell_info[row][column].cellno = numberofcells - 1;
852 rowofcell.resize(numberofcells);
853 columnofcell.resize(numberofcells);
855 // reset column and row of cells and update their width and alignment
856 for (row_type row = 0; row < nrows(); ++row)
857 for (col_type column = 0; column < ncols(); ++column) {
858 if (isPartOfMultiColumn(row, column))
860 // columnofcell needs to be called before setting width and aligment
861 // multirow cells inherit the width from the column width
862 if (!isPartOfMultiRow(row, column)) {
863 columnofcell[i] = column;
866 setFixedWidth(row, column);
867 if (isPartOfMultiRow(row, column))
869 cell_info[row][column].inset->setContentAlignment(
870 getAlignment(cellIndex(row, column)));
876 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
879 for (col_type c = 0; c < ncols(); ++c)
880 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
886 bool Tabular::topLine(idx_type const cell) const
888 return cellInfo(cell).top_line;
892 bool Tabular::bottomLine(idx_type const cell) const
894 return cellInfo(cell).bottom_line;
898 bool Tabular::leftLine(idx_type cell) const
902 return cellInfo(cell).left_line;
906 bool Tabular::rightLine(idx_type cell) const
910 return cellInfo(cell).right_line;
914 int Tabular::interRowSpace(row_type row) const
916 if (!row || row >= nrows())
919 int const interline_space = row_info[row - 1].interline_space_default ?
921 row_info[row - 1].interline_space.inPixels(width());
922 if (rowTopLine(row) && rowBottomLine(row - 1))
923 return interline_space + WIDTH_OF_LINE;
924 return interline_space;
928 int Tabular::interColumnSpace(idx_type cell) const
930 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
931 if (rightLine(cell) && nextcol < ncols()
932 && leftLine(cellIndex(cellRow(cell), nextcol)))
933 return WIDTH_OF_LINE;
938 int Tabular::cellWidth(idx_type cell) const
941 col_type const span = columnSpan(cell);
942 col_type const col = cellColumn(cell);
943 for(col_type c = col; c < col + span ; ++c)
944 w += column_info[c].width;
949 int Tabular::cellHeight(idx_type cell) const
951 row_type const span = rowSpan(cell);
952 row_type const row = cellRow(cell);
954 for(row_type r = row; r < row + span ; ++r) {
955 h += rowAscent(r) + rowDescent(r);
956 if (r != row + span - 1)
957 h += interRowSpace(r + 1);
964 bool Tabular::updateColumnWidths()
966 vector<int> max_dwidth(ncols(), 0);
967 for(col_type c = 0; c < ncols(); ++c)
968 for(row_type r = 0; r < nrows(); ++r) {
969 idx_type const i = cellIndex(r, c);
970 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
971 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
975 // for each col get max of single col cells
976 for(col_type c = 0; c < ncols(); ++c) {
978 for(row_type r = 0; r < nrows(); ++r) {
979 idx_type const i = cellIndex(r, c);
980 if (columnSpan(i) == 1) {
981 if (getAlignment(i) == LYX_ALIGN_DECIMAL
982 && cell_info[r][c].decimal_width!=0)
983 new_width = max(new_width, cellInfo(i).width
984 + max_dwidth[c] - cellInfo(i).decimal_width);
986 new_width = max(new_width, cellInfo(i).width);
990 if (column_info[c].width != new_width) {
991 column_info[c].width = new_width;
995 // update col widths to fit merged cells
996 for(col_type c = 0; c < ncols(); ++c)
997 for(row_type r = 0; r < nrows(); ++r) {
998 idx_type const i = cellIndex(r, c);
999 int const span = columnSpan(i);
1000 if (span == 1 || c > cellColumn(i))
1004 for(col_type j = c; j < c + span ; ++j)
1005 old_width += column_info[j].width;
1007 if (cellInfo(i).width > old_width) {
1008 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1017 int Tabular::width() const
1020 for (col_type c = 0; c < ncols(); ++c)
1021 width += column_info[c].width;
1026 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1029 col_type const col = cellColumn(cell);
1030 // set alignment for the whole row if we are not in a multicolumn cell,
1031 // exclude possible multicolumn cells in the row
1032 if (!isMultiColumn(cell)) {
1033 for (row_type r = 0; r < nrows(); ++r) {
1034 // only if the column has no width the multirow inherits the
1035 // alignment of the column, otherwise it is left aligned
1036 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1037 && !isMultiColumn(cellIndex(r, col))) {
1038 cell_info[r][col].alignment = align;
1039 cell_info[r][col].inset->setContentAlignment(align);
1041 if ((isMultiRow(cellIndex(r, col)) && has_width)
1042 && !isMultiColumn(cellIndex(r, col))) {
1043 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1044 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1047 column_info[col].alignment = align;
1048 docstring & dpoint = column_info[col].decimal_point;
1049 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1050 dpoint = from_utf8(lyxrc.default_decimal_point);
1052 cellInfo(cell).alignment = align;
1053 cellInset(cell).get()->setContentAlignment(align);
1058 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1061 if (!isMultiColumn(cell) || onlycolumn)
1062 column_info[cellColumn(cell)].valignment = align;
1064 cellInfo(cell).valignment = align;
1071 * Allow line and paragraph breaks for fixed width cells or disallow them,
1072 * merge cell paragraphs and reset layout to standard for variable width
1075 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1077 inset->setAutoBreakRows(fixedWidth);
1078 inset->toggleFixedWidth(fixedWidth);
1082 // merge all paragraphs to one
1083 BufferParams const & bp = cur.bv().buffer().params();
1084 while (inset->paragraphs().size() > 1)
1085 mergeParagraph(bp, inset->paragraphs(), 0);
1089 // undo information has already been recorded
1090 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1091 bp.documentClass().plainLayoutName());
1098 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1099 Length const & width)
1101 col_type const c = cellColumn(cell);
1103 column_info[c].p_width = width;
1104 // reset the vertical alignment to top if the fixed with
1105 // is removed or zero because only fixed width columns can
1106 // have a vertical alignment
1107 if (column_info[c].p_width.zero())
1108 column_info[c].valignment = LYX_VALIGN_TOP;
1109 for (row_type r = 0; r < nrows(); ++r) {
1110 idx_type const cell = cellIndex(r, c);
1111 // because of multicolumns
1112 toggleFixedWidth(cur, cellInset(cell).get(),
1113 !getPWidth(cell).zero());
1115 // cur paragraph can become invalid after paragraphs were merged
1116 if (cur.pit() > cur.lastpit())
1117 cur.pit() = cur.lastpit();
1118 // cur position can become invalid after newlines were removed
1119 if (cur.pos() > cur.lastpos())
1120 cur.pos() = cur.lastpos();
1124 bool Tabular::setFixedWidth(row_type r, col_type c)
1126 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1127 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1128 || (multicol && !cell_info[r][c].p_width.zero());
1129 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1134 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1135 Length const & width)
1137 if (!isMultiColumn(cell))
1140 cellInfo(cell).p_width = width;
1141 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1142 // cur paragraph can become invalid after paragraphs were merged
1143 if (cur.pit() > cur.lastpit())
1144 cur.pit() = cur.lastpit();
1145 // cur position can become invalid after newlines were removed
1146 if (cur.pos() > cur.lastpos())
1147 cur.pos() = cur.lastpos();
1152 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1154 cellInfo(cell).mroffset = mroffset;
1159 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1160 Tabular::Feature what)
1162 if (what == SET_SPECIAL_MULTICOLUMN)
1163 cellInfo(cell).align_special = special;
1165 column_info[cellColumn(cell)].align_special = special;
1169 void Tabular::setTopLine(idx_type i, bool line)
1171 cellInfo(i).top_line = line;
1175 void Tabular::setBottomLine(idx_type i, bool line)
1177 cellInfo(i).bottom_line = line;
1181 void Tabular::setLeftLine(idx_type cell, bool line)
1183 cellInfo(cell).left_line = line;
1187 void Tabular::setRightLine(idx_type cell, bool line)
1189 cellInfo(cell).right_line = line;
1192 bool Tabular::rowTopLine(row_type r) const
1194 bool all_rows_set = true;
1195 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1196 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1197 return all_rows_set;
1201 bool Tabular::rowBottomLine(row_type r) const
1203 bool all_rows_set = true;
1204 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1205 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1206 return all_rows_set;
1210 bool Tabular::columnLeftLine(col_type c) const
1217 for (row_type r = 0; r < nrows(); ++r) {
1218 idx_type const i = cellIndex(r, c);
1219 if (c == cellColumn(i)) {
1221 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1222 if (cellInfo(i).left_line || right)
1226 return 2 * nrows_left >= total;
1230 bool Tabular::columnRightLine(col_type c) const
1235 int nrows_right = 0;
1237 for (row_type r = 0; r < nrows(); ++r) {
1238 idx_type i = cellIndex(r, c);
1239 if (c == cellColumn(i) + columnSpan(i) - 1) {
1241 bool left = (c + 1 < ncols()
1242 && cellInfo(cellIndex(r, c + 1)).left_line)
1243 || c + 1 == ncols();
1244 if (cellInfo(i).right_line && left)
1248 return 2 * nrows_right >= total;
1252 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1254 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1255 return cellInfo(cell).alignment;
1257 return column_info[cellColumn(cell)].alignment;
1262 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1264 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1265 return cellInfo(cell).valignment;
1266 return column_info[cellColumn(cell)].valignment;
1270 Length const Tabular::getPWidth(idx_type cell) const
1272 if (isMultiColumn(cell))
1273 return cellInfo(cell).p_width;
1274 return column_info[cellColumn(cell)].p_width;
1278 Length const Tabular::getMROffset(idx_type cell) const
1280 return cellInfo(cell).mroffset;
1284 int Tabular::textHOffset(idx_type cell) const
1286 // the LaTeX Way :-(
1287 int x = WIDTH_OF_LINE;
1289 int const w = cellWidth(cell) - cellInfo(cell).width;
1291 switch (getAlignment(cell)) {
1292 case LYX_ALIGN_CENTER:
1295 case LYX_ALIGN_RIGHT:
1298 case LYX_ALIGN_DECIMAL: {
1299 // we center when no decimal point
1300 if (cellInfo(cell).decimal_width == 0) {
1304 col_type const c = cellColumn(cell);
1305 int max_dhoffset = 0;
1306 for(row_type r = 0; r < row_info.size() ; ++r) {
1307 idx_type const i = cellIndex(r, c);
1308 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1309 && cellInfo(i).decimal_width != 0)
1310 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1312 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1315 // LYX_ALIGN_LEFT: nothing :-)
1323 int Tabular::textVOffset(idx_type cell) const
1325 int voffset = cellInfo(cell).voffset;
1326 if (isMultiRow(cell)) {
1327 row_type const row = cellRow(cell);
1328 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1334 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1337 idx_type const numcells = numberOfCellsInRow(row);
1338 // we check against numcells to make sure we do not crash if all the
1339 // cells are multirow (bug #7535), but in that case our return value
1340 // is really invalid, i.e., it is NOT the first cell in the row. but
1341 // i do not know what to do here. (rgh)
1342 while (c < numcells - 1
1343 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1345 return cell_info[row][c].cellno;
1349 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1351 col_type c = ncols() - 1;
1352 // of course we check against 0 so we don't crash. but we have the same
1353 // problem as in the previous routine: if all the cells are part of a
1354 // multirow or part of a multi column, then our return value is invalid.
1356 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1357 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1359 return cell_info[row][c].cellno;
1363 Tabular::row_type Tabular::cellRow(idx_type cell) const
1365 if (cell >= numberofcells)
1369 return rowofcell[cell];
1373 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1375 if (cell >= numberofcells)
1379 return columnofcell[cell];
1383 void Tabular::write(ostream & os) const
1387 << write_attribute("version", 3)
1388 << write_attribute("rows", nrows())
1389 << write_attribute("columns", ncols())
1391 // global longtable options
1393 << write_attribute("rotate", convert<string>(rotate))
1394 << write_attribute("booktabs", use_booktabs)
1395 << write_attribute("islongtable", is_long_tabular)
1396 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1397 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1398 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1399 << write_attribute("headTopDL", endhead.topDL)
1400 << write_attribute("headBottomDL", endhead.bottomDL)
1401 << write_attribute("footTopDL", endfoot.topDL)
1402 << write_attribute("footBottomDL", endfoot.bottomDL)
1403 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1404 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1405 << write_attribute("lastFootEmpty", endlastfoot.empty);
1406 // longtables cannot be aligned vertically
1407 if (!is_long_tabular) {
1408 os << write_attribute("tabularvalignment", tabular_valignment);
1409 os << write_attribute("tabularwidth", tabular_width);
1411 if (is_long_tabular)
1412 os << write_attribute("longtabularalignment",
1413 longtabular_alignment);
1415 for (col_type c = 0; c < ncols(); ++c) {
1417 << write_attribute("alignment", column_info[c].alignment);
1418 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1419 os << write_attribute("decimal_point", column_info[c].decimal_point);
1420 os << write_attribute("valignment", column_info[c].valignment)
1421 << write_attribute("width", column_info[c].p_width.asString())
1422 << write_attribute("special", column_info[c].align_special)
1425 for (row_type r = 0; r < nrows(); ++r) {
1426 static const string def("default");
1428 if (row_info[r].top_space_default)
1429 os << write_attribute("topspace", def);
1431 os << write_attribute("topspace", row_info[r].top_space);
1432 if (row_info[r].bottom_space_default)
1433 os << write_attribute("bottomspace", def);
1435 os << write_attribute("bottomspace", row_info[r].bottom_space);
1436 if (row_info[r].interline_space_default)
1437 os << write_attribute("interlinespace", def);
1439 os << write_attribute("interlinespace", row_info[r].interline_space);
1440 os << write_attribute("endhead", row_info[r].endhead)
1441 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1442 << write_attribute("endfoot", row_info[r].endfoot)
1443 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1444 << write_attribute("newpage", row_info[r].newpage)
1445 << write_attribute("caption", row_info[r].caption)
1447 for (col_type c = 0; c < ncols(); ++c) {
1449 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1450 << write_attribute("multirow", cell_info[r][c].multirow)
1451 << write_attribute("mroffset", cell_info[r][c].mroffset)
1452 << write_attribute("alignment", cell_info[r][c].alignment)
1453 << write_attribute("valignment", cell_info[r][c].valignment)
1454 << write_attribute("topline", cell_info[r][c].top_line)
1455 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1456 << write_attribute("leftline", cell_info[r][c].left_line)
1457 << write_attribute("rightline", cell_info[r][c].right_line)
1458 << write_attribute("rotate", cell_info[r][c].rotate)
1459 << write_attribute("usebox", cell_info[r][c].usebox)
1460 << write_attribute("width", cell_info[r][c].p_width)
1461 << write_attribute("special", cell_info[r][c].align_special)
1463 os << "\\begin_inset ";
1464 cell_info[r][c].inset->write(os);
1465 os << "\n\\end_inset\n"
1470 os << "</lyxtabular>\n";
1474 void Tabular::read(Lexer & lex)
1477 istream & is = lex.getStream();
1479 l_getline(is, line);
1480 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1481 LASSERT(false, /**/);
1486 if (!getTokenValue(line, "version", version))
1488 LASSERT(version >= 2, /**/);
1491 if (!getTokenValue(line, "rows", rows_arg))
1494 if (!getTokenValue(line, "columns", columns_arg))
1496 init(buffer_, rows_arg, columns_arg);
1497 l_getline(is, line);
1498 if (!prefixIs(line, "<features")) {
1499 lyxerr << "Wrong tabular format (expected <features ...> got"
1500 << line << ')' << endl;
1503 getTokenValue(line, "rotate", rotate);
1504 getTokenValue(line, "booktabs", use_booktabs);
1505 getTokenValue(line, "islongtable", is_long_tabular);
1506 getTokenValue(line, "tabularvalignment", tabular_valignment);
1507 getTokenValue(line, "tabularwidth", tabular_width);
1508 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1509 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1510 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1511 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1512 getTokenValue(line, "headTopDL", endhead.topDL);
1513 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1514 getTokenValue(line, "footTopDL", endfoot.topDL);
1515 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1516 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1517 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1518 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1520 for (col_type c = 0; c < ncols(); ++c) {
1522 if (!prefixIs(line,"<column")) {
1523 lyxerr << "Wrong tabular format (expected <column ...> got"
1524 << line << ')' << endl;
1527 getTokenValue(line, "alignment", column_info[c].alignment);
1528 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1529 getTokenValue(line, "valignment", column_info[c].valignment);
1530 getTokenValue(line, "width", column_info[c].p_width);
1531 getTokenValue(line, "special", column_info[c].align_special);
1534 for (row_type i = 0; i < nrows(); ++i) {
1535 l_getline(is, line);
1536 if (!prefixIs(line, "<row")) {
1537 lyxerr << "Wrong tabular format (expected <row ...> got"
1538 << line << ')' << endl;
1541 getTokenValue(line, "topspace", row_info[i].top_space,
1542 row_info[i].top_space_default);
1543 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1544 row_info[i].bottom_space_default);
1545 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1546 row_info[i].interline_space_default);
1547 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1548 getTokenValue(line, "endhead", row_info[i].endhead);
1549 getTokenValue(line, "endfoot", row_info[i].endfoot);
1550 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1551 getTokenValue(line, "newpage", row_info[i].newpage);
1552 getTokenValue(line, "caption", row_info[i].caption);
1553 for (col_type j = 0; j < ncols(); ++j) {
1554 l_getline(is, line);
1555 if (!prefixIs(line, "<cell")) {
1556 lyxerr << "Wrong tabular format (expected <cell ...> got"
1557 << line << ')' << endl;
1560 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1561 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1562 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1563 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1564 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1565 getTokenValue(line, "topline", cell_info[i][j].top_line);
1566 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1567 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1568 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1569 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1570 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1571 getTokenValue(line, "width", cell_info[i][j].p_width);
1573 getTokenValue(line, "special", cell_info[i][j].align_special);
1574 l_getline(is, line);
1575 if (prefixIs(line, "\\begin_inset")) {
1576 cell_info[i][j].inset->setBuffer(*buffer_);
1577 cell_info[i][j].inset->read(lex);
1578 l_getline(is, line);
1580 if (!prefixIs(line, "</cell>")) {
1581 lyxerr << "Wrong tabular format (expected </cell> got"
1582 << line << ')' << endl;
1586 l_getline(is, line);
1587 if (!prefixIs(line, "</row>")) {
1588 lyxerr << "Wrong tabular format (expected </row> got"
1589 << line << ')' << endl;
1593 while (!prefixIs(line, "</lyxtabular>")) {
1594 l_getline(is, line);
1600 bool Tabular::isMultiColumn(idx_type cell) const
1602 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1603 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1607 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1609 return cell_info[cellRow(cell)][cellColumn(cell)];
1613 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1614 bool const right_border)
1616 idx_type const col = cellColumn(cell);
1617 idx_type const row = cellRow(cell);
1618 for (idx_type i = 0; i < number; ++i)
1619 unsetMultiRow(cellIndex(row, col + i));
1621 // unsetting of multirow may have invalidated cell index
1622 cell = cellIndex(row, col);
1623 CellData & cs = cellInfo(cell);
1624 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1625 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1626 cs.alignment = column_info[col].alignment;
1628 setRightLine(cell, right_border);
1630 for (idx_type i = 1; i < number; ++i) {
1631 CellData & cs1 = cellInfo(cell + i);
1632 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1633 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1641 bool Tabular::isMultiRow(idx_type cell) const
1643 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1644 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1648 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1649 bool const bottom_border)
1651 idx_type const col = cellColumn(cell);
1652 idx_type const row = cellRow(cell);
1653 for (idx_type i = 0; i < number; ++i)
1654 unsetMultiColumn(cellIndex(row + i, col));
1656 // unsetting of multirow may have invalidated cell index
1657 cell = cellIndex(row, col);
1658 CellData & cs = cellInfo(cell);
1659 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1660 cs.valignment = LYX_VALIGN_MIDDLE;
1661 // the horizontal alignment of multirow cells can only
1662 // be changed for the whole table row,
1663 // support changing this only for the multirow cell can be done via
1665 // this feature would be a fileformat change
1666 // until LyX supports this, use the deault alignment of multirow
1668 cs.alignment = LYX_ALIGN_LEFT;
1670 // set the bottom line of the last selected cell
1671 setBottomLine(cell, bottom_border);
1673 for (idx_type i = 1; i < number; ++i) {
1674 CellData & cs1 = cell_info[row + i][col];
1675 cs1.multirow = CELL_PART_OF_MULTIROW;
1676 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1684 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1686 row_type const row = cellRow(cell);
1687 col_type const col = cellColumn(cell);
1689 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1696 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1698 col_type const column = cellColumn(cell);
1699 col_type row = cellRow(cell) + 1;
1700 while (row < nrows() && isPartOfMultiRow(row, column))
1703 return row - cellRow(cell);
1707 void Tabular::unsetMultiColumn(idx_type cell)
1709 if (!isMultiColumn(cell))
1712 row_type const row = cellRow(cell);
1713 col_type const col = cellColumn(cell);
1714 row_type const span = columnSpan(cell);
1715 for (col_type c = 0; c < span; ++c) {
1716 // in the table dialog the lines are set in every case
1717 // when unsetting a multicolumn this leads to an additional right
1718 // line for every cell that was part of the former multicolumn cell,
1719 // except if the cell is in the last column
1720 // therefore remove this line
1721 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1722 && (col + c) < (col + span - 1))
1723 cell_info[row][col + c].right_line = false;
1724 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1730 void Tabular::unsetMultiRow(idx_type cell)
1732 if (!isMultiRow(cell))
1735 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1736 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1737 row_type const row = cellRow(cell);
1738 col_type const col = cellColumn(cell);
1739 row_type const span = rowSpan(cell);
1740 for (row_type r = 0; r < span; ++r)
1741 cell_info[row + r][col].multirow = CELL_NORMAL;
1746 void Tabular::setRotateCell(idx_type cell, int value)
1748 cellInfo(cell).rotate = value;
1752 int Tabular::getRotateCell(idx_type cell) const
1754 return cellInfo(cell).rotate;
1758 bool Tabular::needRotating() const
1762 for (row_type r = 0; r < nrows(); ++r)
1763 for (col_type c = 0; c < ncols(); ++c)
1764 if (cell_info[r][c].rotate != 0)
1770 bool Tabular::isLastCell(idx_type cell) const
1772 if (cell + 1 < numberofcells)
1778 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1780 if (cellRow(cell) == 0)
1783 col_type const col = cellColumn(cell);
1784 row_type r = cellRow(cell) - 1;
1785 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1788 return cell_info[r][col].cellno;
1792 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1794 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1795 if (nextrow < nrows())
1796 return cell_info[nextrow][cellColumn(cell)].cellno;
1801 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1803 LASSERT(column != npos && column < ncols()
1804 && row != npos && row < nrows(), /**/);
1805 return cell_info[row][column].cellno;
1809 void Tabular::setUsebox(idx_type cell, BoxType type)
1811 cellInfo(cell).usebox = type;
1815 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1816 // adds line breaks, see bug 4886.
1817 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1819 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1820 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1822 if (cellInfo(cell).usebox > 1)
1823 return cellInfo(cell).usebox;
1824 return useParbox(cell);
1829 // This are functions used for the longtable support
1831 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1837 row_info[row].endfirsthead = flag;
1841 row_info[row].endhead = flag;
1846 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1849 hd.set = haveLTHead();
1850 return row_info[row].endhead;
1854 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1857 hd.set = haveLTFirstHead();
1858 return row_info[row].endfirsthead;
1862 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1868 row_info[row].endlastfoot = flag;
1872 row_info[row].endfoot = flag;
1877 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1880 fd.set = haveLTFoot();
1881 return row_info[row].endfoot;
1885 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1888 fd.set = haveLTLastFoot();
1889 return row_info[row].endlastfoot;
1893 void Tabular::setLTNewPage(row_type row, bool what)
1895 row_info[row].newpage = what;
1899 bool Tabular::getLTNewPage(row_type row) const
1901 return row_info[row].newpage;
1905 bool Tabular::haveLTHead(bool withcaptions) const
1907 if (!is_long_tabular)
1909 for (row_type i = 0; i < nrows(); ++i)
1910 if (row_info[i].endhead &&
1911 (withcaptions || !row_info[i].caption))
1917 bool Tabular::haveLTFirstHead(bool withcaptions) const
1919 if (!is_long_tabular || endfirsthead.empty)
1921 for (row_type r = 0; r < nrows(); ++r)
1922 if (row_info[r].endfirsthead &&
1923 (withcaptions || !row_info[r].caption))
1929 bool Tabular::haveLTFoot(bool withcaptions) const
1931 if (!is_long_tabular)
1933 for (row_type r = 0; r < nrows(); ++r)
1934 if (row_info[r].endfoot &&
1935 (withcaptions || !row_info[r].caption))
1941 bool Tabular::haveLTLastFoot(bool withcaptions) const
1943 if (!is_long_tabular || endlastfoot.empty)
1945 for (row_type r = 0; r < nrows(); ++r)
1946 if (row_info[r].endlastfoot &&
1947 (withcaptions || !row_info[r].caption))
1953 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1955 idx_type i = getFirstCellInRow(row);
1957 setMultiColumn(i, numberOfCellsInRow(row), false);
1958 setTopLine(i, false);
1959 setBottomLine(i, false);
1960 setLeftLine(i, false);
1961 setRightLine(i, false);
1962 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
1963 !row_info[row].endfoot && !row_info[row].endlastfoot) {
1964 setLTHead(row, true, endfirsthead, true);
1965 row_info[row].endfirsthead = true;
1968 unsetMultiColumn(i);
1969 // When unsetting a caption row, also all existing
1970 // captions in this row must be dissolved.
1972 row_info[row].caption = what;
1977 bool Tabular::ltCaption(row_type row) const
1979 return row_info[row].caption;
1983 bool Tabular::haveLTCaption(CaptionType captiontype) const
1985 if (!is_long_tabular)
1987 for (row_type r = 0; r < nrows(); ++r) {
1988 if (row_info[r].caption) {
1989 switch (captiontype) {
1990 case CAPTION_FIRSTHEAD:
1991 if (row_info[r].endfirsthead)
1995 if (row_info[r].endhead)
1999 if (row_info[r].endfoot)
2002 case CAPTION_LASTFOOT:
2003 if (row_info[r].endlastfoot)
2015 // end longtable support functions
2017 void Tabular::setRowAscent(row_type row, int height)
2019 if (row >= nrows() || row_info[row].ascent == height)
2021 row_info[row].ascent = height;
2025 void Tabular::setRowDescent(row_type row, int height)
2027 if (row >= nrows() || row_info[row].descent == height)
2029 row_info[row].descent = height;
2033 int Tabular::rowAscent(row_type row) const
2035 LASSERT(row < nrows(), /**/);
2036 return row_info[row].ascent;
2040 int Tabular::rowDescent(row_type row) const
2042 LASSERT(row < nrows(), /**/);
2043 return row_info[row].descent;
2047 int Tabular::height() const
2050 for (row_type row = 0; row < nrows(); ++row)
2051 height += rowAscent(row) + rowDescent(row) +
2057 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2059 LASSERT(row < nrows(), /**/);
2060 LASSERT(column < ncols(), /**/);
2061 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2065 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2067 LASSERT(row < nrows(), /**/);
2068 LASSERT(column < ncols(), /**/);
2069 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2073 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2075 // we only output complete row lines and the 1st row here, the rest
2076 // is done in Tabular::TeXBottomHLine(...)
2078 // get for each column the topline (if any)
2079 vector<bool> topline;
2081 for (col_type c = 0; c < ncols(); ++c) {
2082 topline.push_back(topLine(cellIndex(row, c)));
2083 // If cell is part of a multirow and not the first cell of the
2084 // multirow, no line must be drawn.
2086 if (isMultiRow(cellIndex(row, c))
2087 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2093 // do nothing if empty first row, or incomplete row line after
2094 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2097 // only output complete row lines and the 1st row's clines
2098 if (nset == ncols()) {
2100 os << (row == 0 ? "\\toprule " : "\\midrule ");
2104 } else if (row == 0) {
2105 for (col_type c = 0; c < ncols(); ++c) {
2107 col_type offset = 0;
2108 for (col_type j = 0 ; j < c; ++j)
2109 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2112 //babel makes the "-" character an active one, so we have to suppress this here
2113 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2114 if (lang == "slovak" || lang == "czech")
2115 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2116 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2118 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2120 col_type cstart = c;
2121 for ( ; c < ncols() && topline[c]; ++c) {}
2123 for (col_type j = cstart ; j < c ; ++j)
2124 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2127 os << c + offset << "} ";
2135 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2137 // we output bottomlines of row r and the toplines of row r+1
2138 // if the latter do not span the whole tabular
2140 // get the bottomlines of row r, and toplines in next row
2141 bool lastrow = row == nrows() - 1;
2142 vector<bool> bottomline, topline;
2143 bool nextrowset = true;
2144 for (col_type c = 0; c < ncols(); ++c) {
2145 bottomline.push_back(bottomLine(cellIndex(row, c)));
2146 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2147 // If cell is part of a multirow and not the last cell of the
2148 // multirow, no line must be drawn.
2150 if (isMultiRow(cellIndex(row, c))
2151 && isMultiRow(cellIndex(row + 1, c))
2152 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2153 bottomline[c] = false;
2156 nextrowset &= topline[c];
2159 // combine this row's bottom lines and next row's toplines if necessary
2161 for (col_type c = 0; c < ncols(); ++c) {
2163 bottomline[c] = bottomline[c] || topline[c];
2168 // do nothing if empty, OR incomplete row line with a topline in next row
2169 if (nset == 0 || (nextrowset && nset != ncols()))
2172 if (nset == ncols()) {
2174 os << (lastrow ? "\\bottomrule" : "\\midrule");
2178 for (col_type c = 0; c < ncols(); ++c) {
2179 if (bottomline[c]) {
2180 col_type offset = 0;
2181 for (col_type j = 0 ; j < c; ++j)
2182 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2185 //babel makes the "-" character an active one, so we have to suppress this here
2186 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2187 if (lang == "slovak" || lang == "czech")
2188 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2189 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2191 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2193 col_type cstart = c;
2194 for ( ; c < ncols() && bottomline[c]; ++c) {}
2196 for (col_type j = cstart ; j < c ; ++j)
2197 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2200 os << c + offset << "} ";
2208 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2209 bool & ismulticol, bool & ismultirow) const
2211 row_type const r = cellRow(cell);
2212 if (is_long_tabular && row_info[r].caption)
2215 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2216 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2217 // figure out how to set the lines
2218 // we always set double lines to the right of the cell
2219 col_type const c = cellColumn(cell);
2220 col_type const nextcol = c + columnSpan(cell);
2221 bool colright = columnRightLine(c);
2222 bool colleft = columnLeftLine(c);
2223 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2224 bool nextcellleft = nextcol < ncols()
2225 && leftLine(cellIndex(r, nextcol));
2226 bool coldouble = colright && nextcolleft;
2227 bool celldouble = rightLine(cell) && nextcellleft;
2229 ismulticol = isMultiColumn(cell)
2230 || (c == 0 && colleft != leftLine(cell))
2231 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2232 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2233 || (coldouble != celldouble);
2235 // we center in multicol when no decimal point
2236 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2237 docstring const align_d = column_info[c].decimal_point;
2238 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2242 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2243 int latexcolspan = columnSpan(cell);
2244 for(col_type col = c; col < c + columnSpan(cell); ++col)
2245 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2249 os << "\\multicolumn{" << latexcolspan << "}{";
2250 if (c ==0 && leftLine(cell))
2252 if (!cellInfo(cell).align_special.empty()) {
2253 os << cellInfo(cell).align_special;
2255 if (!getPWidth(cell).zero()) {
2257 case LYX_ALIGN_LEFT:
2258 os << ">{\\raggedright}";
2260 case LYX_ALIGN_RIGHT:
2261 os << ">{\\raggedleft}";
2263 case LYX_ALIGN_CENTER:
2264 os << ">{\\centering}";
2270 case LYX_VALIGN_TOP:
2273 case LYX_VALIGN_MIDDLE:
2276 case LYX_VALIGN_BOTTOM:
2281 << from_ascii(getPWidth(cell).asLatexString())
2285 case LYX_ALIGN_LEFT:
2288 case LYX_ALIGN_RIGHT:
2295 } // end if else !getPWidth
2296 } // end if else !cellinfo_of_cell
2297 if (rightLine(cell) || nextcellleft)
2300 // add extra vertical line if we want a double one
2303 } // end if ismulticol
2305 // we only need code for the first multirow cell
2306 ismultirow = isMultiRow(cell);
2308 os << "\\multirow{" << rowSpan(cell) << "}{";
2309 if (!getPWidth(cell).zero())
2310 os << from_ascii(getPWidth(cell).asLatexString());
2312 // we need to set a default value
2315 if (!getMROffset(cell).zero())
2316 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2318 } // end if ismultirow
2320 if (getRotateCell(cell) != 0)
2321 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2323 if (getUsebox(cell) == BOX_PARBOX) {
2326 case LYX_VALIGN_TOP:
2329 case LYX_VALIGN_MIDDLE:
2332 case LYX_VALIGN_BOTTOM:
2336 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2338 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2339 os << "\\begin{minipage}[";
2341 case LYX_VALIGN_TOP:
2344 case LYX_VALIGN_MIDDLE:
2347 case LYX_VALIGN_BOTTOM:
2351 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2357 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2358 bool ismulticol, bool ismultirow) const
2360 row_type const r = cellRow(cell);
2361 if (is_long_tabular && row_info[r].caption)
2365 if (getUsebox(cell) == BOX_PARBOX)
2367 else if (getUsebox(cell) == BOX_MINIPAGE)
2368 os << breakln << "\\end{minipage}";
2369 if (getRotateCell(cell) != 0)
2370 os << breakln << "\\end{turn}";
2378 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2379 OutputParams const & runparams) const
2381 if (!is_long_tabular)
2384 // output first header info
2385 if (haveLTFirstHead()) {
2386 if (endfirsthead.topDL)
2388 for (row_type r = 0; r < nrows(); ++r) {
2389 if (row_info[r].endfirsthead)
2390 TeXRow(os, r, runparams);
2392 if (endfirsthead.bottomDL)
2394 os << "\\endfirsthead\n";
2396 // output header info
2398 if (endfirsthead.empty && !haveLTFirstHead())
2399 os << "\\endfirsthead\n";
2402 for (row_type r = 0; r < nrows(); ++r) {
2403 if (row_info[r].endhead)
2404 TeXRow(os, r, runparams);
2406 if (endhead.bottomDL)
2408 os << "\\endhead\n";
2410 // output footer info
2414 for (row_type r = 0; r < nrows(); ++r) {
2415 if (row_info[r].endfoot)
2416 TeXRow(os, r, runparams);
2418 if (endfoot.bottomDL)
2420 os << "\\endfoot\n";
2421 if (endlastfoot.empty && !haveLTLastFoot())
2422 os << "\\endlastfoot\n";
2424 // output lastfooter info
2425 if (haveLTLastFoot()) {
2426 if (endlastfoot.topDL)
2428 for (row_type r = 0; r < nrows(); ++r) {
2429 if (row_info[r].endlastfoot)
2430 TeXRow(os, r, runparams);
2432 if (endlastfoot.bottomDL)
2434 os << "\\endlastfoot\n";
2439 bool Tabular::isValidRow(row_type row) const
2441 if (!is_long_tabular)
2443 return !row_info[row].endhead && !row_info[row].endfirsthead
2444 && !row_info[row].endfoot && !row_info[row].endlastfoot
2445 && !row_info[row].caption;
2449 void Tabular::TeXRow(otexstream & os, row_type row,
2450 OutputParams const & runparams) const
2452 idx_type cell = cellIndex(row, 0);
2453 shared_ptr<InsetTableCell> inset = cellInset(cell);
2454 Paragraph const & par = inset->paragraphs().front();
2455 string const lang = par.getParLanguage(buffer().params())->lang();
2457 //output the top line
2458 TeXTopHLine(os, row, lang);
2460 if (row_info[row].top_space_default) {
2462 os << "\\addlinespace\n";
2464 os << "\\noalign{\\vskip\\doublerulesep}\n";
2465 } else if(!row_info[row].top_space.zero()) {
2467 os << "\\addlinespace["
2468 << from_ascii(row_info[row].top_space.asLatexString())
2471 os << "\\noalign{\\vskip"
2472 << from_ascii(row_info[row].top_space.asLatexString())
2476 bool ismulticol = false;
2477 bool ismultirow = false;
2478 for (col_type c = 0; c < ncols(); ++c) {
2479 if (isPartOfMultiColumn(row, c))
2482 cell = cellIndex(row, c);
2484 if (isPartOfMultiRow(row, c)
2485 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2486 if (cell != getLastCellInRow(row))
2491 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2492 shared_ptr<InsetTableCell> inset = cellInset(cell);
2494 Paragraph const & par = inset->paragraphs().front();
2495 bool rtl = par.isRTL(buffer().params())
2497 && getPWidth(cell).zero()
2498 && !runparams.use_polyglossia;
2502 par.getParLanguage(buffer().params())->lang();
2503 if (lang == "farsi")
2505 else if (lang == "arabic_arabi")
2507 // currently, remaining RTL languages are
2508 // arabic_arabtex and hebrew
2512 // pass to the OutputParams that we are in a cell and
2513 // which alignment we have set.
2514 // InsetNewline needs this context information.
2515 OutputParams newrp(runparams);
2516 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2517 ? OutputParams::PLAIN
2518 : OutputParams::ALIGNED;
2520 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2521 // copy cell and split in 2
2522 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2523 head.setBuffer(buffer());
2524 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2526 dit.push_back(CursorSlice(head));
2527 head.setMacrocontextPositionRecursive(dit);
2528 bool hassep = false;
2529 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2530 head.latex(os, newrp);
2533 tail.setBuffer(head.buffer());
2535 dit.push_back(CursorSlice(tail));
2536 tail.setMacrocontextPositionRecursive(dit);
2537 tail.latex(os, newrp);
2539 } else if (!isPartOfMultiRow(row, c)) {
2540 if (!runparams.nice)
2541 os.texrow().start(par.id(), 0);
2542 inset->latex(os, newrp);
2545 runparams.encoding = newrp.encoding;
2549 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2550 if (cell != getLastCellInRow(row)) { // not last cell in row
2557 os << "\\tabularnewline";
2558 if (row_info[row].bottom_space_default) {
2560 os << "\\addlinespace";
2562 os << "[\\doublerulesep]";
2563 } else if (!row_info[row].bottom_space.zero()) {
2565 os << "\\addlinespace";
2567 << from_ascii(row_info[row].bottom_space.asLatexString())
2572 //output the bottom line
2573 TeXBottomHLine(os, row, lang);
2575 if (row_info[row].interline_space_default) {
2577 os << "\\addlinespace\n";
2579 os << "\\noalign{\\vskip\\doublerulesep}\n";
2580 } else if (!row_info[row].interline_space.zero()) {
2582 os << "\\addlinespace["
2583 << from_ascii(row_info[row].interline_space.asLatexString())
2586 os << "\\noalign{\\vskip"
2587 << from_ascii(row_info[row].interline_space.asLatexString())
2593 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2595 bool const is_tabular_star = !tabular_width.zero();
2597 //+---------------------------------------------------------------------
2598 //+ first the opening preamble +
2599 //+---------------------------------------------------------------------
2602 if (runparams.lastid != -1)
2603 os.texrow().start(runparams.lastid, runparams.lastpos);
2606 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2608 if (is_long_tabular) {
2609 os << "\\begin{longtable}";
2610 switch (longtabular_alignment) {
2611 case LYX_LONGTABULAR_ALIGN_LEFT:
2614 case LYX_LONGTABULAR_ALIGN_CENTER:
2616 case LYX_LONGTABULAR_ALIGN_RIGHT:
2621 if (is_tabular_star)
2622 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2624 os << "\\begin{tabular}";
2625 switch (tabular_valignment) {
2626 case LYX_VALIGN_TOP:
2629 case LYX_VALIGN_MIDDLE:
2631 case LYX_VALIGN_BOTTOM:
2639 if (is_tabular_star)
2640 os << "@{\\extracolsep{\\fill}}";
2642 for (col_type c = 0; c < ncols(); ++c) {
2643 if (columnLeftLine(c))
2645 if (!column_info[c].align_special.empty()) {
2646 os << column_info[c].align_special;
2648 if (!column_info[c].p_width.zero()) {
2649 switch (column_info[c].alignment) {
2650 case LYX_ALIGN_LEFT:
2651 os << ">{\\raggedright}";
2653 case LYX_ALIGN_RIGHT:
2654 os << ">{\\raggedleft}";
2656 case LYX_ALIGN_CENTER:
2657 os << ">{\\centering}";
2659 case LYX_ALIGN_NONE:
2660 case LYX_ALIGN_BLOCK:
2661 case LYX_ALIGN_LAYOUT:
2662 case LYX_ALIGN_SPECIAL:
2663 case LYX_ALIGN_DECIMAL:
2667 switch (column_info[c].valignment) {
2668 case LYX_VALIGN_TOP:
2671 case LYX_VALIGN_MIDDLE:
2674 case LYX_VALIGN_BOTTOM:
2679 << from_ascii(column_info[c].p_width.asLatexString())
2682 switch (column_info[c].alignment) {
2683 case LYX_ALIGN_LEFT:
2686 case LYX_ALIGN_RIGHT:
2689 case LYX_ALIGN_DECIMAL:
2690 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2696 } // end if else !column_info[i].p_width
2697 } // end if else !column_info[i].align_special
2698 if (columnRightLine(c))
2703 TeXLongtableHeaderFooter(os, runparams);
2705 //+---------------------------------------------------------------------
2706 //+ the single row and columns (cells) +
2707 //+---------------------------------------------------------------------
2709 for (row_type r = 0; r < nrows(); ++r) {
2710 if (isValidRow(r)) {
2711 TeXRow(os, r, runparams);
2712 if (is_long_tabular && row_info[r].newpage)
2713 os << "\\newpage\n";
2717 //+---------------------------------------------------------------------
2718 //+ the closing of the tabular +
2719 //+---------------------------------------------------------------------
2721 if (is_long_tabular)
2722 os << "\\end{longtable}";
2724 if (is_tabular_star)
2725 os << "\\end{tabular*}";
2727 os << "\\end{tabular}";
2731 os << breakln << "\\end{turn}";
2735 int Tabular::docbookRow(odocstream & os, row_type row,
2736 OutputParams const & runparams) const
2739 idx_type cell = getFirstCellInRow(row);
2742 for (col_type c = 0; c < ncols(); ++c) {
2743 if (isPartOfMultiColumn(row, c))
2746 os << "<entry align=\"";
2747 switch (getAlignment(cell)) {
2748 case LYX_ALIGN_LEFT:
2751 case LYX_ALIGN_RIGHT:
2759 os << "\" valign=\"";
2760 switch (getVAlignment(cell)) {
2761 case LYX_VALIGN_TOP:
2764 case LYX_VALIGN_BOTTOM:
2767 case LYX_VALIGN_MIDDLE:
2772 if (isMultiColumn(cell)) {
2773 os << " namest=\"col" << c << "\" ";
2774 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2778 ret += cellInset(cell)->docbook(os, runparams);
2787 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2791 //+---------------------------------------------------------------------
2792 //+ first the opening preamble +
2793 //+---------------------------------------------------------------------
2795 os << "<tgroup cols=\"" << ncols()
2796 << "\" colsep=\"1\" rowsep=\"1\">\n";
2798 for (col_type c = 0; c < ncols(); ++c) {
2799 os << "<colspec colname=\"col" << c << "\" align=\"";
2800 switch (column_info[c].alignment) {
2801 case LYX_ALIGN_LEFT:
2804 case LYX_ALIGN_RIGHT:
2812 if (runparams.flavor == OutputParams::XML)
2818 //+---------------------------------------------------------------------
2819 //+ Long Tabular case +
2820 //+---------------------------------------------------------------------
2822 // output caption info
2823 // The caption flag wins over head/foot
2824 if (haveLTCaption()) {
2825 os << "<caption>\n";
2827 for (row_type r = 0; r < nrows(); ++r) {
2828 if (row_info[r].caption) {
2829 ret += docbookRow(os, r, runparams);
2832 os << "</caption>\n";
2835 // output header info
2836 if (haveLTHead(false) || haveLTFirstHead(false)) {
2839 for (row_type r = 0; r < nrows(); ++r) {
2840 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2841 !row_info[r].caption) {
2842 ret += docbookRow(os, r, runparams);
2848 // output footer info
2849 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2852 for (row_type r = 0; r < nrows(); ++r) {
2853 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2854 !row_info[r].caption) {
2855 ret += docbookRow(os, r, runparams);
2862 //+---------------------------------------------------------------------
2863 //+ the single row and columns (cells) +
2864 //+---------------------------------------------------------------------
2868 for (row_type r = 0; r < nrows(); ++r) {
2869 if (isValidRow(r)) {
2870 ret += docbookRow(os, r, runparams);
2875 //+---------------------------------------------------------------------
2876 //+ the closing of the tabular +
2877 //+---------------------------------------------------------------------
2886 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2887 OutputParams const & runparams, bool header) const
2890 string const celltag = header ? "th" : "td";
2891 idx_type cell = getFirstCellInRow(row);
2893 xs << html::StartTag("tr");
2894 for (col_type c = 0; c < ncols(); ++c) {
2895 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2900 switch (getAlignment(cell)) {
2901 case LYX_ALIGN_LEFT:
2904 case LYX_ALIGN_RIGHT:
2912 attr << " valign='";
2913 switch (getVAlignment(cell)) {
2914 case LYX_VALIGN_TOP:
2917 case LYX_VALIGN_BOTTOM:
2920 case LYX_VALIGN_MIDDLE:
2925 if (isMultiColumn(cell))
2926 attr << " colspan='" << columnSpan(cell) << "'";
2927 else if (isMultiRow(cell))
2928 attr << " rowspan='" << rowSpan(cell) << "'";
2930 xs << html::StartTag(celltag, attr.str()) << html::CR();
2931 ret += cellInset(cell)->xhtml(xs, runparams);
2932 xs << html::EndTag(celltag) << html::CR();
2935 xs << html::EndTag("tr");
2940 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2944 if (is_long_tabular) {
2945 // we'll wrap it in a div, so as to deal with alignment
2947 switch (longtabular_alignment) {
2948 case LYX_LONGTABULAR_ALIGN_LEFT:
2951 case LYX_LONGTABULAR_ALIGN_CENTER:
2954 case LYX_LONGTABULAR_ALIGN_RIGHT:
2958 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2960 // The caption flag wins over head/foot
2961 if (haveLTCaption()) {
2962 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2964 for (row_type r = 0; r < nrows(); ++r)
2965 if (row_info[r].caption)
2966 ret += xhtmlRow(xs, r, runparams);
2967 xs << html::EndTag("div") << html::CR();
2971 xs << html::StartTag("table") << html::CR();
2973 // output header info
2974 bool const havefirsthead = haveLTFirstHead(false);
2975 // if we have a first head, then we are going to ignore the
2976 // headers for the additional pages, since there aren't any
2977 // in XHTML. this test accomplishes that.
2978 bool const havehead = !havefirsthead && haveLTHead(false);
2979 if (havehead || havefirsthead) {
2980 xs << html::StartTag("thead") << html::CR();
2981 for (row_type r = 0; r < nrows(); ++r) {
2982 if (((havefirsthead && row_info[r].endfirsthead) ||
2983 (havehead && row_info[r].endhead)) &&
2984 !row_info[r].caption) {
2985 ret += xhtmlRow(xs, r, runparams, true);
2988 xs << html::EndTag("thead") << html::CR();
2990 // output footer info
2991 bool const havelastfoot = haveLTLastFoot(false);
2993 bool const havefoot = !havelastfoot && haveLTFoot(false);
2994 if (havefoot || havelastfoot) {
2995 xs << html::StartTag("tfoot") << html::CR();
2996 for (row_type r = 0; r < nrows(); ++r) {
2997 if (((havelastfoot && row_info[r].endlastfoot) ||
2998 (havefoot && row_info[r].endfoot)) &&
2999 !row_info[r].caption) {
3000 ret += xhtmlRow(xs, r, runparams);
3003 xs << html::EndTag("tfoot") << html::CR();
3006 xs << html::StartTag("tbody") << html::CR();
3007 for (row_type r = 0; r < nrows(); ++r) {
3008 if (isValidRow(r)) {
3009 ret += xhtmlRow(xs, r, runparams);
3012 xs << html::EndTag("tbody")
3014 << html::EndTag("table")
3016 if (is_long_tabular)
3017 xs << html::EndTag("div") << html::CR();
3022 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3023 vector<unsigned int> const & clen) const
3025 idx_type const fcell = getFirstCellInRow(row);
3026 idx_type const n = numberOfCellsInRow(row) + fcell;
3029 for (idx_type i = fcell; i < n; ++i) {
3039 for (idx_type i = fcell; i < n; ++i) {
3050 col_type column = cellColumn(i);
3051 int len = clen[column];
3052 while (column < ncols() - 1
3053 && isPartOfMultiColumn(row, ++column))
3054 len += clen[column] + 4;
3055 os << docstring(len, ch);
3070 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3071 vector<unsigned int> const & clen) const
3073 idx_type const fcell = getFirstCellInRow(row);
3074 idx_type const n = numberOfCellsInRow(row) + fcell;
3077 for (idx_type i = fcell; i < n; ++i) {
3078 if (bottomLine(i)) {
3087 for (idx_type i = fcell; i < n; ++i) {
3088 if (bottomLine(i)) {
3098 col_type column = cellColumn(i);
3099 int len = clen[column];
3100 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3101 len += clen[column] + 4;
3102 os << docstring(len, ch);
3103 if (bottomLine(i)) {
3117 void Tabular::plaintextPrintCell(odocstream & os,
3118 OutputParams const & runparams,
3119 idx_type cell, row_type row, col_type column,
3120 vector<unsigned int> const & clen,
3121 bool onlydata) const
3123 odocstringstream sstr;
3124 cellInset(cell)->plaintext(sstr, runparams);
3136 unsigned int len1 = sstr.str().length();
3137 unsigned int len2 = clen[column];
3138 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3139 len2 += clen[column] + 4;
3142 switch (getAlignment(cell)) {
3144 case LYX_ALIGN_LEFT:
3147 case LYX_ALIGN_RIGHT:
3151 case LYX_ALIGN_CENTER:
3157 os << docstring(len1, ' ') << sstr.str()
3158 << docstring(len2, ' ');
3160 if (rightLine(cell))
3167 void Tabular::plaintext(odocstream & os,
3168 OutputParams const & runparams, int const depth,
3169 bool onlydata, char_type delim) const
3171 // first calculate the width of the single columns
3172 vector<unsigned int> clen(ncols());
3175 // first all non multicolumn cells!
3176 for (col_type c = 0; c < ncols(); ++c) {
3178 for (row_type r = 0; r < nrows(); ++r) {
3179 idx_type cell = cellIndex(r, c);
3180 if (isMultiColumn(cell))
3182 odocstringstream sstr;
3183 cellInset(cell)->plaintext(sstr, runparams);
3184 if (clen[c] < sstr.str().length())
3185 clen[c] = sstr.str().length();
3188 // then all multicolumn cells!
3189 for (col_type c = 0; c < ncols(); ++c) {
3190 for (row_type r = 0; r < nrows(); ++r) {
3191 idx_type cell = cellIndex(r, c);
3192 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3194 odocstringstream sstr;
3195 cellInset(cell)->plaintext(sstr, runparams);
3196 int len = int(sstr.str().length());
3197 idx_type const n = columnSpan(cell);
3198 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3200 if (len > int(clen[c + n - 1]))
3201 clen[c + n - 1] = len;
3206 for (row_type r = 0; r < nrows(); ++r) {
3207 if (!onlydata && plaintextTopHLine(os, r, clen))
3208 os << docstring(depth * 2, ' ');
3209 for (col_type c = 0; c < ncols(); ++c) {
3210 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3212 if (onlydata && c > 0)
3213 // we don't use operator<< for single UCS4 character.
3214 // see explanation in docstream.h
3216 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3221 os << docstring(depth * 2, ' ');
3222 if (plaintextBottomHLine(os, r, clen))
3223 os << docstring(depth * 2, ' ');
3229 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3231 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3235 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3236 col_type column) const
3238 return cell_info[row][column].inset;
3242 void Tabular::setCellInset(row_type row, col_type column,
3243 shared_ptr<InsetTableCell> ins) const
3245 CellData & cd = cell_info[row][column];
3250 void Tabular::validate(LaTeXFeatures & features) const
3252 features.require("NeedTabularnewline");
3254 features.require("booktabs");
3255 if (is_long_tabular)
3256 features.require("longtable");
3258 features.require("rotating");
3259 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3260 if (isMultiRow(cell))
3261 features.require("multirow");
3262 if (getVAlignment(cell) != LYX_VALIGN_TOP
3263 || !getPWidth(cell).zero())
3264 features.require("array");
3265 cellInset(cell)->validate(features);
3270 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3272 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3273 ParagraphList::const_iterator cit = parlist.begin();
3274 ParagraphList::const_iterator end = parlist.end();
3276 for (; cit != end; ++cit)
3277 for (int i = 0; i < cit->size(); ++i)
3278 if (cit->isNewline(i))
3285 /////////////////////////////////////////////////////////////////////
3289 /////////////////////////////////////////////////////////////////////
3291 InsetTableCell::InsetTableCell(Buffer * buf)
3292 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3293 contentAlign(LYX_ALIGN_CENTER)
3297 bool InsetTableCell::forcePlainLayout(idx_type) const
3299 return !isFixedWidth;
3303 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3305 return isFixedWidth;
3309 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3310 FuncStatus & status) const
3312 bool enabled = true;
3313 switch (cmd.action()) {
3315 enabled = !forcePlainLayout();
3317 case LFUN_LAYOUT_PARAGRAPH:
3318 enabled = allowParagraphCustomization();
3321 case LFUN_MATH_DISPLAY:
3322 if (!hasFixedWidth()) {
3327 return InsetText::getStatus(cur, cmd, status);
3329 status.setEnabled(enabled);
3333 docstring InsetTableCell::asString(bool intoInsets)
3336 if (paragraphs().empty())
3338 ParagraphList::const_iterator it = paragraphs().begin();
3339 ParagraphList::const_iterator en = paragraphs().end();
3341 for (; it != en; ++it) {
3346 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3352 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3355 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3356 return InsetText::xhtml(xs, rp);
3361 /////////////////////////////////////////////////////////////////////
3365 /////////////////////////////////////////////////////////////////////
3367 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3369 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3370 rowselect_(false), colselect_(false)
3375 InsetTabular::InsetTabular(InsetTabular const & tab)
3376 : Inset(tab), tabular(tab.tabular), scx_(0)
3381 InsetTabular::~InsetTabular()
3383 hideDialogs("tabular", this);
3387 void InsetTabular::setBuffer(Buffer & buf)
3389 tabular.setBuffer(buf);
3390 Inset::setBuffer(buf);
3394 bool InsetTabular::insetAllowed(InsetCode code) const
3399 case MATHMACRO_CODE:
3404 return tabular.is_long_tabular;
3412 void InsetTabular::write(ostream & os) const
3414 os << "Tabular" << endl;
3419 string InsetTabular::contextMenu(BufferView const &, int, int) const
3421 // FIXME: depending on the selection state,
3422 // we could offer a different menu.
3423 return cell(0)->contextMenuName() + ";" + contextMenuName();
3427 string InsetTabular::contextMenuName() const
3429 return "context-tabular";
3433 void InsetTabular::read(Lexer & lex)
3435 //bool const old_format = (lex.getString() == "\\LyXTable");
3443 string token = lex.getString();
3444 while (lex && token != "\\end_inset") {
3446 token = lex.getString();
3449 lex.printError("Missing \\end_inset at this point. ");
3453 int InsetTabular::rowFromY(Cursor & cur, int y) const
3455 // top y coordinate of tabular
3456 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3458 for (; r < tabular.nrows() && y > h; ++r)
3459 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3460 + tabular.interRowSpace(r);
3466 int InsetTabular::columnFromX(Cursor & cur, int x) const
3468 // left x coordinate of tabular
3469 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3471 for (; c < tabular.ncols() && x > w; ++c)
3472 w += tabular.cellWidth(c);
3477 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3479 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3480 // mi.base.textwidth << "\n";
3483 LASSERT(false, /**/);
3486 for (row_type r = 0; r < tabular.nrows(); ++r) {
3489 for (col_type c = 0; c < tabular.ncols(); ++c) {
3490 if (tabular.isPartOfMultiColumn(r, c)
3491 || tabular.isPartOfMultiRow(r, c))
3492 // multicolumn or multirow cell, but not first one
3494 idx_type const cell = tabular.cellIndex(r, c);
3497 Length const p_width = tabular.getPWidth(cell);
3498 if (!p_width.zero())
3499 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3500 tabular.cellInset(cell)->metrics(m, dim);
3501 if (!p_width.zero())
3502 dim.wid = m.base.textwidth;
3503 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3504 + tabular.interColumnSpace(cell);
3506 // FIXME(?): do we need a second metrics call?
3507 TextMetrics const & tm =
3508 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3510 // determine horizontal offset because of decimal align (if necessary)
3511 int decimal_width = 0;
3512 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3513 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3514 tail.setBuffer(tabular.buffer());
3515 // we need to set macrocontext position everywhere
3516 // otherwise we crash with nested insets (e.g. footnotes)
3517 // after decimal point
3518 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3520 dit.push_back(CursorSlice(tail));
3521 tail.setMacrocontextPositionRecursive(dit);
3523 // remove text leading decimal point
3524 docstring const align_d = tabular.column_info[c].decimal_point;
3525 dit = separatorPos(&tail, align_d);
3527 pit_type const psize = tail.paragraphs().front().size();
3529 tail.paragraphs().front().eraseChars(0,
3530 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3532 tail.metrics(m, dim1);
3533 decimal_width = dim1.width();
3536 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3537 tabular.cell_info[r][c].decimal_width = decimal_width;
3539 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3540 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3541 int const lastpardes = tm.last().second->descent()
3542 + TEXT_TO_INSET_OFFSET;
3544 switch (tabular.getVAlignment(cell)) {
3545 case Tabular::LYX_VALIGN_TOP:
3547 case Tabular::LYX_VALIGN_MIDDLE:
3548 offset = -(dim.des - lastpardes)/2;
3550 case Tabular::LYX_VALIGN_BOTTOM:
3551 offset = -(dim.des - lastpardes);
3554 tabular.cell_info[r][c].voffset = offset;
3555 maxasc = max(maxasc, dim.asc - offset);
3556 maxdes = max(maxdes, dim.des + offset);
3558 int const top_space = tabular.row_info[r].top_space_default ?
3559 default_line_space :
3560 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3561 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3562 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3563 default_line_space :
3564 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3565 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3568 // for top-alignment the first horizontal table line must be exactly at
3569 // the position of the base line of the surrounding text line
3570 // for bottom alignment, the same is for the last table line
3571 switch (tabular.tabular_valignment) {
3572 case Tabular::LYX_VALIGN_BOTTOM:
3573 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3575 case Tabular::LYX_VALIGN_MIDDLE:
3576 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3578 case Tabular::LYX_VALIGN_TOP:
3579 offset_valign_ = tabular.rowAscent(0);
3583 tabular.updateColumnWidths();
3584 dim.asc = tabular.rowAscent(0) - offset_valign_;
3585 dim.des = tabular.height() - dim.asc;
3586 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3590 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3593 if (&cur.inset() == this && cur.selection()) {
3594 if (cur.selIsMultiCell()) {
3597 getSelection(cur, rs, re, cs, ce);
3599 idx_type const cell = tabular.cellIndex(row, col);
3600 col_type const cspan = tabular.columnSpan(cell);
3601 row_type const rspan = tabular.rowSpan(cell);
3602 if (col + cspan - 1 >= cs && col <= ce
3603 && row + rspan - 1 >= rs && row <= re)
3606 if (col == tabular.cellColumn(cur.idx())
3607 && row == tabular.cellRow(cur.idx())) {
3608 CursorSlice const & beg = cur.selBegin();
3609 CursorSlice const & end = cur.selEnd();
3611 if ((end.lastpos() > 0 || end.lastpit() > 0)
3612 && end.pos() == end.lastpos() && beg.pos() == 0
3613 && end.pit() == end.lastpit() && beg.pit() == 0)
3621 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3623 x += scx_ + ADD_TO_TABULAR_WIDTH;
3625 BufferView * bv = pi.base.bv;
3626 Cursor & cur = pi.base.bv->cursor();
3629 // FIXME: As the full background is painted in drawBackground(),
3630 // we have no choice but to do a full repaint for the Text cells.
3631 pi.full_repaint = true;
3633 bool const original_selection_state = pi.selected;
3636 first_visible_cell = Tabular::npos;
3638 int yy = y + offset_valign_;
3639 for (row_type r = 0; r < tabular.nrows(); ++r) {
3641 for (col_type c = 0; c < tabular.ncols(); ++c) {
3642 if (tabular.isPartOfMultiColumn(r, c))
3645 idx = tabular.cellIndex(r, c);
3647 if (tabular.isPartOfMultiRow(r, c)) {
3648 nx += tabular.cellWidth(idx);
3652 if (first_visible_cell == Tabular::npos)
3653 first_visible_cell = idx;
3655 pi.selected |= isCellSelected(cur, r, c);
3656 int const cx = nx + tabular.textHOffset(idx);
3657 int const cy = yy + tabular.textVOffset(idx);
3658 // Cache the Inset position.
3659 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3660 cell(idx)->draw(pi, cx, cy);
3661 drawCellLines(pi, nx, yy, r, idx);
3662 nx += tabular.cellWidth(idx);
3663 pi.selected = original_selection_state;
3666 if (r + 1 < tabular.nrows())
3667 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3668 + tabular.interRowSpace(r + 1);
3673 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3675 x += scx_ + ADD_TO_TABULAR_WIDTH;
3676 y += offset_valign_ - tabular.rowAscent(0);
3677 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3678 pi.backgroundColor(this));
3682 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3684 Cursor & cur = pi.base.bv->cursor();
3687 x += scx_ + ADD_TO_TABULAR_WIDTH;
3689 if (!cur.selection())
3691 if (&cur.inset() != this)
3696 bool const full_cell_selected = isCellSelected(cur,
3697 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3699 if (cur.selIsMultiCell() || full_cell_selected) {
3700 for (row_type r = 0; r < tabular.nrows(); ++r) {
3702 for (col_type c = 0; c < tabular.ncols(); ++c) {
3703 if (tabular.isPartOfMultiColumn(r, c))
3706 idx_type const cell = tabular.cellIndex(r, c);
3708 if (tabular.isPartOfMultiRow(r, c)) {
3709 xx += tabular.cellWidth(cell);
3712 int const w = tabular.cellWidth(cell);
3713 int const h = tabular.cellHeight(cell);
3714 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3715 if (isCellSelected(cur, r, c))
3716 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3719 if (r + 1 < tabular.nrows())
3720 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3721 + tabular.interRowSpace(r + 1);
3725 // FIXME: This code has no effect because InsetTableCell does not handle
3726 // drawSelection other than the trivial implementation in Inset.
3728 // x += cellXPos(cur.idx());
3729 // x += tabular.textHOffset(cur.idx());
3730 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3735 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3736 row_type row, idx_type cell) const
3738 y -= tabular.rowAscent(row);
3739 int const w = tabular.cellWidth(cell);
3740 int const h = tabular.cellHeight(cell);
3741 Color const linecolor = pi.textColor(Color_tabularline);
3742 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3745 bool drawline = tabular.topLine(cell)
3746 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3747 pi.pain.line(x, y, x + w, y,
3748 drawline ? linecolor : gridcolor,
3749 drawline ? Painter::line_solid : Painter::line_onoffdash);
3752 drawline = tabular.bottomLine(cell);
3753 pi.pain.line(x, y + h, x + w, y + h,
3754 drawline ? linecolor : gridcolor,
3755 drawline ? Painter::line_solid : Painter::line_onoffdash);
3758 col_type const col = tabular.cellColumn(cell);
3759 drawline = tabular.leftLine(cell)
3760 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3761 pi.pain.line(x, y, x, y + h,
3762 drawline ? linecolor : gridcolor,
3763 drawline ? Painter::line_solid : Painter::line_onoffdash);
3766 x -= tabular.interColumnSpace(cell);
3767 drawline = tabular.rightLine(cell)
3768 || (col + 1 < tabular.ncols()
3769 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3770 pi.pain.line(x + w, y, x + w, y + h,
3771 drawline ? linecolor : gridcolor,
3772 drawline ? Painter::line_solid : Painter::line_onoffdash);
3776 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3778 //lyxerr << "InsetTabular::edit: " << this << endl;
3780 cur.setSelection(false);
3783 if (isRightToLeft(cur))
3784 cur.idx() = tabular.getLastCellInRow(0);
3790 if (isRightToLeft(cur))
3791 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3793 cur.idx() = tabular.numberofcells - 1;
3795 cur.pos() = cur.lastpos(); // FIXME crude guess
3797 cur.setCurrentFont();
3798 // FIXME: this accesses the position cache before it is initialized
3800 //cur.bv().fitCursor();
3804 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3806 // In a longtable, tell captions what the current float is
3807 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3808 string const saveflt = cnts.current_float();
3809 if (tabular.is_long_tabular)
3810 cnts.current_float("table");
3812 ParIterator it2 = it;
3814 size_t const end = it2.nargs();
3815 for ( ; it2.idx() < end; it2.top().forwardIdx())
3816 buffer().updateBuffer(it2, utype);
3819 if (tabular.is_long_tabular)
3820 cnts.current_float(saveflt);
3824 void InsetTabular::addToToc(DocIterator const & cpit) const
3826 DocIterator dit = cpit;
3828 size_t const end = dit.nargs();
3829 for ( ; dit.idx() < end; dit.top().forwardIdx())
3830 cell(dit.idx())->addToToc(dit);
3834 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3836 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3837 << "\n cur:" << cur);
3838 CursorSlice sl = cur.top();
3839 Cursor & bvcur = cur.bv().cursor();
3841 FuncCode const act = cmd.action();
3845 case LFUN_MOUSE_PRESS: {
3846 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3848 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3849 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3850 row_type r = rowFromY(cur, cmd.y());
3851 cur.idx() = tabular.getFirstCellInRow(r);
3854 cur.idx() = tabular.getLastCellInRow(r);
3855 cur.pos() = cur.lastpos();
3856 cur.setSelection(true);
3862 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3863 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3864 || cmd.y() > y0 + tabular.height()) {
3865 col_type c = columnFromX(cur, cmd.x());
3866 cur.idx() = tabular.cellIndex(0, c);
3869 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3870 cur.pos() = cur.lastpos();
3871 cur.setSelection(true);
3876 // do not reset cursor/selection if we have selected
3877 // some cells (bug 2715).
3878 if (cmd.button() == mouse_button::button3
3879 && &bvcur.selBegin().inset() == this
3880 && bvcur.selIsMultiCell())
3883 // Let InsetTableCell do it
3884 cell(cur.idx())->dispatch(cur, cmd);
3887 case LFUN_MOUSE_MOTION:
3888 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3889 if (cmd.button() == mouse_button::button1) {
3890 // only accept motions to places not deeper nested than the real anchor
3891 if (!bvcur.realAnchor().hasPart(cur)) {
3895 // select (additional) row
3897 row_type r = rowFromY(cur, cmd.y());
3898 cur.idx() = tabular.getLastCellInRow(r);
3899 // we need to reset the cursor's pit and pos now, as the old ones
3900 // may no longer be valid.
3903 bvcur.setCursor(cur);
3904 bvcur.setSelection(true);
3907 // select (additional) column
3909 col_type c = columnFromX(cur, cmd.x());
3910 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3911 // we need to reset the cursor's pit and pos now, as the old ones
3912 // may no longer be valid.
3915 bvcur.setCursor(cur);
3916 bvcur.setSelection(true);
3919 // only update if selection changes
3920 if (bvcur.idx() == cur.idx() &&
3921 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3922 cur.noScreenUpdate();
3923 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3924 bvcur.setCursor(cur);
3925 bvcur.setSelection(true);
3926 // if this is a multicell selection, we just set the cursor to
3927 // the beginning of the cell's text.
3928 if (bvcur.selIsMultiCell()) {
3929 bvcur.pit() = bvcur.lastpit();
3930 bvcur.pos() = bvcur.lastpos();
3935 case LFUN_MOUSE_RELEASE:
3940 case LFUN_CELL_BACKWARD:
3942 cur.setSelection(false);
3945 case LFUN_CELL_FORWARD:
3947 cur.setSelection(false);
3950 case LFUN_CHAR_FORWARD_SELECT:
3951 case LFUN_CHAR_FORWARD:
3952 case LFUN_CHAR_BACKWARD_SELECT:
3953 case LFUN_CHAR_BACKWARD:
3954 case LFUN_CHAR_RIGHT_SELECT:
3955 case LFUN_CHAR_RIGHT:
3956 case LFUN_CHAR_LEFT_SELECT:
3957 case LFUN_CHAR_LEFT:
3958 case LFUN_WORD_FORWARD:
3959 case LFUN_WORD_FORWARD_SELECT:
3960 case LFUN_WORD_BACKWARD:
3961 case LFUN_WORD_BACKWARD_SELECT:
3962 case LFUN_WORD_RIGHT:
3963 case LFUN_WORD_RIGHT_SELECT:
3964 case LFUN_WORD_LEFT:
3965 case LFUN_WORD_LEFT_SELECT: {
3966 // determine whether we move to next or previous cell, where to enter
3967 // the new cell from, and which command to "finish" (i.e., exit the
3970 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3971 FuncCode finish_lfun;
3973 if (act == LFUN_CHAR_FORWARD
3974 || act == LFUN_CHAR_FORWARD_SELECT
3975 || act == LFUN_WORD_FORWARD
3976 || act == LFUN_WORD_FORWARD_SELECT) {
3978 finish_lfun = LFUN_FINISHED_FORWARD;
3980 else if (act == LFUN_CHAR_BACKWARD
3981 || act == LFUN_CHAR_BACKWARD_SELECT
3982 || act == LFUN_WORD_BACKWARD
3983 || act == LFUN_WORD_BACKWARD_SELECT) {
3985 finish_lfun = LFUN_FINISHED_BACKWARD;
3987 // LEFT or RIGHT commands --- the interpretation will depend on the
3988 // table's direction.
3990 bool const right = act == LFUN_CHAR_RIGHT
3991 || act == LFUN_CHAR_RIGHT_SELECT
3992 || act == LFUN_WORD_RIGHT
3993 || act == LFUN_WORD_RIGHT_SELECT;
3994 next_cell = isRightToLeft(cur) != right;
3996 if (lyxrc.visual_cursor)
3997 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3999 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4002 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4003 || act == LFUN_CHAR_BACKWARD_SELECT
4004 || act == LFUN_CHAR_RIGHT_SELECT
4005 || act == LFUN_CHAR_LEFT_SELECT
4006 || act == LFUN_WORD_FORWARD_SELECT
4007 || act == LFUN_WORD_RIGHT_SELECT
4008 || act == LFUN_WORD_BACKWARD_SELECT
4009 || act == LFUN_WORD_LEFT_SELECT;
4011 // If we have a multicell selection or we're
4012 // not doing some LFUN_*_SELECT thing anyway...
4013 if (!cur.selIsMultiCell() || !select) {
4014 col_type const c = tabular.cellColumn(cur.idx());
4015 row_type const r = tabular.cellRow(cur.idx());
4016 // Are we trying to select the whole cell and is the whole cell
4017 // not yet selected?
4018 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4019 ((next_cell && cur.pit() == cur.lastpit()
4020 && cur.pos() == cur.lastpos())
4021 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4023 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4025 // ...try to dispatch to the cell's inset.
4026 cell(cur.idx())->dispatch(cur, cmd);
4028 // When we already have a selection we want to select the whole cell
4029 // before going to the next cell.
4030 if (select_whole && !empty_cell){
4031 getText(cur.idx())->selectAll(cur);
4033 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4037 // FIXME: When we support the selection of an empty cell, remove
4038 // the !empty_cell from this condition. For now we jump to the next
4039 // cell if the current cell is empty.
4040 if (cur.result().dispatched() && !empty_cell)
4044 // move to next/prev cell, as appropriate
4045 // note that we will always do this if we're selecting and we have
4046 // a multicell selection
4047 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4048 << " cell from: " << int(entry_from));
4050 moveNextCell(cur, entry_from);
4052 movePrevCell(cur, entry_from);
4053 // if we're exiting the table, call the appropriate FINISHED lfun
4054 if (sl == cur.top())
4055 cmd = FuncRequest(finish_lfun);
4059 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4064 case LFUN_DOWN_SELECT:
4066 if (!(cur.selection() && cur.selIsMultiCell()))
4067 cell(cur.idx())->dispatch(cur, cmd);
4069 cur.dispatched(); // override the cell's decision
4070 if (sl == cur.top()) {
4071 // if our Text didn't do anything to the cursor
4072 // then we try to put the cursor into the cell below
4073 // setting also the right targetX.
4074 cur.selHandle(act == LFUN_DOWN_SELECT);
4075 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4076 int const xtarget = cur.targetX();
4077 // WARNING: Once cur.idx() has been reset, the cursor is in
4078 // an inconsistent state until pos() has been set. Be careful
4079 // what you do with it!
4080 cur.idx() = tabular.cellBelow(cur.idx());
4082 TextMetrics const & tm =
4083 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4084 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4085 cur.setCurrentFont();
4088 if (sl == cur.top()) {
4089 // we trick it to go to forward after leaving the
4091 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4094 if (cur.selIsMultiCell()) {
4095 cur.pit() = cur.lastpit();
4096 cur.pos() = cur.lastpos();
4097 cur.setCurrentFont();
4098 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4101 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4104 case LFUN_UP_SELECT:
4106 if (!(cur.selection() && cur.selIsMultiCell()))
4107 cell(cur.idx())->dispatch(cur, cmd);
4108 cur.dispatched(); // override the cell's decision
4109 if (sl == cur.top()) {
4110 // if our Text didn't do anything to the cursor
4111 // then we try to put the cursor into the cell above
4112 // setting also the right targetX.
4113 cur.selHandle(act == LFUN_UP_SELECT);
4114 if (tabular.cellRow(cur.idx()) != 0) {
4115 int const xtarget = cur.targetX();
4116 // WARNING: Once cur.idx() has been reset, the cursor is in
4117 // an inconsistent state until pos() has been set. Be careful
4118 // what you do with it!
4119 cur.idx() = tabular.cellAbove(cur.idx());
4120 cur.pit() = cur.lastpit();
4121 Text const * text = cell(cur.idx())->getText(0);
4122 TextMetrics const & tm = cur.bv().textMetrics(text);
4123 ParagraphMetrics const & pm =
4124 tm.parMetrics(cur.lastpit());
4125 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4126 cur.setCurrentFont();
4129 if (sl == cur.top()) {
4130 cmd = FuncRequest(LFUN_UP);
4133 if (cur.selIsMultiCell()) {
4135 cur.pos() = cur.lastpos();
4136 cur.setCurrentFont();
4137 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4140 cur.screenUpdateFlags(Update::Force | 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.setOnOff(tabular.rotate != 0);
4556 case Tabular::TABULAR_VALIGN_TOP:
4557 status.setEnabled(tabular.tabular_width.zero());
4558 status.setOnOff(tabular.tabular_valignment
4559 == Tabular::LYX_VALIGN_TOP);
4561 case Tabular::TABULAR_VALIGN_MIDDLE:
4562 status.setEnabled(tabular.tabular_width.zero());
4563 status.setOnOff(tabular.tabular_valignment
4564 == Tabular::LYX_VALIGN_MIDDLE);
4566 case Tabular::TABULAR_VALIGN_BOTTOM:
4567 status.setEnabled(tabular.tabular_width.zero());
4568 status.setOnOff(tabular.tabular_valignment
4569 == Tabular::LYX_VALIGN_BOTTOM);
4572 case Tabular::LONGTABULAR_ALIGN_LEFT:
4573 status.setOnOff(tabular.longtabular_alignment
4574 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4576 case Tabular::LONGTABULAR_ALIGN_CENTER:
4577 status.setOnOff(tabular.longtabular_alignment
4578 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4580 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4581 status.setOnOff(tabular.longtabular_alignment
4582 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4585 case Tabular::UNSET_ROTATE_TABULAR:
4586 status.setOnOff(tabular.rotate == 0);
4589 case Tabular::TOGGLE_ROTATE_CELL:
4590 case Tabular::SET_ROTATE_CELL:
4591 status.setOnOff(!oneCellHasRotationState(false,
4592 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4595 case Tabular::UNSET_ROTATE_CELL:
4596 status.setOnOff(!oneCellHasRotationState(true,
4597 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4600 case Tabular::SET_USEBOX:
4601 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4604 // every row can only be one thing:
4605 // either a footer or header
4606 case Tabular::SET_LTFIRSTHEAD:
4607 status.setEnabled(sel_row_start == sel_row_end);
4608 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4611 case Tabular::UNSET_LTFIRSTHEAD:
4612 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4613 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4616 case Tabular::SET_LTHEAD:
4617 status.setEnabled(sel_row_start == sel_row_end);
4618 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4621 case Tabular::UNSET_LTHEAD:
4622 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4623 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4626 case Tabular::SET_LTFOOT:
4627 status.setEnabled(sel_row_start == sel_row_end);
4628 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4631 case Tabular::UNSET_LTFOOT:
4632 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4633 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4636 case Tabular::SET_LTLASTFOOT:
4637 status.setEnabled(sel_row_start == sel_row_end);
4638 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4641 case Tabular::UNSET_LTLASTFOOT:
4642 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4643 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4646 case Tabular::SET_LTNEWPAGE:
4647 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4649 case Tabular::UNSET_LTNEWPAGE:
4650 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4653 // only one row in head/firsthead/foot/lasthead can be the caption
4654 // and a multirow cannot be set as caption
4655 case Tabular::SET_LTCAPTION:
4656 status.setEnabled(sel_row_start == sel_row_end
4657 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4658 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4659 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4660 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4661 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4662 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4663 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4664 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4665 && !tabular.isMultiRow(sel_row_start));
4666 status.setOnOff(tabular.ltCaption(sel_row_start));
4669 case Tabular::UNSET_LTCAPTION:
4670 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4673 case Tabular::TOGGLE_LTCAPTION:
4674 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4675 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4676 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4677 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4678 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4679 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4680 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4681 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4682 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4683 status.setOnOff(tabular.ltCaption(sel_row_start));
4686 case Tabular::SET_BOOKTABS:
4687 status.setOnOff(tabular.use_booktabs);
4690 case Tabular::UNSET_BOOKTABS:
4691 status.setOnOff(!tabular.use_booktabs);
4696 status.setEnabled(false);
4702 case LFUN_CAPTION_INSERT: {
4703 // caption is only allowed in caption cell of longtable
4704 if (!tabular.ltCaption(tabular.cellRow(cur.idx()))) {
4705 status.setEnabled(false);
4708 // check if there is already a caption
4709 bool have_caption = false;
4710 InsetTableCell itc = InsetTableCell(*tabular.cellInset(cur.idx()).get());
4711 ParagraphList::const_iterator pit = itc.paragraphs().begin();
4712 ParagraphList::const_iterator pend = itc.paragraphs().end();
4713 for (; pit != pend; ++pit) {
4714 InsetList::const_iterator it = pit->insetList().begin();
4715 InsetList::const_iterator end = pit->insetList().end();
4716 for (; it != end; ++it) {
4717 if (it->inset->lyxCode() == CAPTION_CODE) {
4718 have_caption = true;
4723 status.setEnabled(!have_caption);
4727 // These are only enabled inside tabular
4728 case LFUN_CELL_BACKWARD:
4729 case LFUN_CELL_FORWARD:
4730 status.setEnabled(true);
4733 // disable these with multiple cells selected
4734 case LFUN_INSET_INSERT:
4735 case LFUN_TABULAR_INSERT:
4736 case LFUN_FLEX_INSERT:
4737 case LFUN_FLOAT_INSERT:
4738 case LFUN_FLOAT_WIDE_INSERT:
4739 case LFUN_FOOTNOTE_INSERT:
4740 case LFUN_MARGINALNOTE_INSERT:
4741 case LFUN_MATH_INSERT:
4742 case LFUN_MATH_MODE:
4743 case LFUN_MATH_MUTATE:
4744 case LFUN_MATH_DISPLAY:
4745 case LFUN_NOTE_INSERT:
4746 case LFUN_ARGUMENT_INSERT:
4747 case LFUN_BOX_INSERT:
4748 case LFUN_BRANCH_INSERT:
4749 case LFUN_PHANTOM_INSERT:
4750 case LFUN_WRAP_INSERT:
4751 case LFUN_PREVIEW_INSERT:
4752 case LFUN_ERT_INSERT: {
4753 if (cur.selIsMultiCell()) {
4754 status.setEnabled(false);
4757 return cell(cur.idx())->getStatus(cur, cmd, status);
4760 // disable in non-fixed-width cells
4761 case LFUN_PARAGRAPH_BREAK:
4762 // multirow does not allow paragraph breaks
4763 if (tabular.isMultiRow(cur.idx())) {
4764 status.setEnabled(false);
4767 case LFUN_NEWLINE_INSERT: {
4768 if (tabular.getPWidth(cur.idx()).zero()) {
4769 status.setEnabled(false);
4772 return cell(cur.idx())->getStatus(cur, cmd, status);
4775 case LFUN_NEWPAGE_INSERT:
4776 status.setEnabled(false);
4780 if (tabularStackDirty() && theClipboard().isInternal()) {
4781 if (cur.selIsMultiCell()) {
4784 getSelection(cur, rs, re, cs, ce);
4785 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4786 && paste_tabular->nrows() == re - rs + 1)
4787 status.setEnabled(true);
4789 status.setEnabled(false);
4790 status.message(_("Selection size should match clipboard content."));
4793 status.setEnabled(true);
4796 return cell(cur.idx())->getStatus(cur, cmd, status);
4798 case LFUN_INSET_SETTINGS:
4799 // relay this lfun to Inset, not to the cell.
4800 return Inset::getStatus(cur, cmd, status);
4803 // we try to handle this event in the insets dispatch function.
4804 return cell(cur.idx())->getStatus(cur, cmd, status);
4810 Inset::DisplayType InsetTabular::display() const
4812 if (tabular.is_long_tabular) {
4813 switch (tabular.longtabular_alignment) {
4814 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4816 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4818 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4828 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4830 tabular.latex(os, runparams);
4834 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4836 os << '\n'; // output table on a new line
4837 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4838 tabular.plaintext(os, runparams, dp, false, 0);
4839 return PLAINTEXT_NEWLINE;
4843 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4848 // FIXME: Why not pass a proper DocIterator here?
4850 // if the table is inside a float it doesn't need the informaltable
4851 // wrapper. Search for it.
4852 for (master = owner(); master; master = master->owner())
4853 if (master->lyxCode() == FLOAT_CODE)
4858 os << "<informaltable>";
4861 ret += tabular.docbook(os, runparams);
4863 os << "</informaltable>";
4870 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4872 return tabular.xhtml(xs, rp);
4876 void InsetTabular::validate(LaTeXFeatures & features) const
4878 tabular.validate(features);
4880 // It'd be better to be able to get this from an InsetLayout, but at present
4881 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4882 if (features.runparams().flavor == OutputParams::HTML)
4883 features.addCSSSnippet(
4884 "table { border: 1px solid black; display: inline-block; }\n"
4885 "td { border: 1px solid black; padding: 0.5ex; }");
4889 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4891 return tabular.cellInset(idx);
4895 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4897 return tabular.cellInset(idx);
4901 void InsetTabular::cursorPos(BufferView const & bv,
4902 CursorSlice const & sl, bool boundary, int & x, int & y) const
4904 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4906 // y offset correction
4907 y += cellYPos(sl.idx());
4908 y += tabular.textVOffset(sl.idx());
4909 y += offset_valign_;
4911 // x offset correction
4912 x += cellXPos(sl.idx());
4913 x += tabular.textHOffset(sl.idx());
4914 x += ADD_TO_TABULAR_WIDTH;
4919 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4923 Inset const & inset = *tabular.cellInset(cell);
4924 Point o = bv.coordCache().getInsets().xy(&inset);
4925 int const xbeg = o.x_ - tabular.textHOffset(cell);
4926 int const xend = xbeg + tabular.cellWidth(cell);
4927 row_type const row = tabular.cellRow(cell);
4928 int const ybeg = o.y_ - tabular.rowAscent(row)
4929 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
4930 int const yend = ybeg + tabular.cellHeight(cell);
4942 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4943 // << " ybeg=" << ybeg << " yend=" << yend
4944 // << " xx=" << xx << " yy=" << yy
4945 // << " dist=" << xx + yy << endl;
4950 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4952 //lyxerr << "InsetTabular::editXY: " << this << endl;
4953 cur.setSelection(false);
4955 cur.idx() = getNearestCell(cur.bv(), x, y);
4957 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4961 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4963 cur.idx() = getNearestCell(cur.bv(), x, y);
4964 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4968 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4970 idx_type idx_min = 0;
4971 int dist_min = numeric_limits<int>::max();
4972 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4973 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4974 int const d = dist(bv, i, x, y);
4985 int InsetTabular::cellYPos(idx_type const cell) const
4987 row_type row = tabular.cellRow(cell);
4989 for (row_type r = 0; r < row; ++r)
4990 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4991 + tabular.interRowSpace(r + 1);
4996 int InsetTabular::cellXPos(idx_type const cell) const
4998 col_type col = tabular.cellColumn(cell);
5000 for (col_type c = 0; c < col; ++c)
5001 lx += tabular.column_info[c].width;
5006 void InsetTabular::resetPos(Cursor & cur) const
5008 BufferView & bv = cur.bv();
5009 int const maxwidth = bv.workWidth();
5011 int const scx_old = scx_;
5012 int const i = cur.find(this);
5017 int const X2 = maxwidth;
5018 int const offset = ADD_TO_TABULAR_WIDTH + 2;
5019 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
5020 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
5023 scx_ = X1 + 20 - x1;
5025 scx_ = X2 - 20 - x2;
5030 // only update if offset changed
5031 if (scx_ != scx_old)
5032 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5036 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5038 row_type const row = tabular.cellRow(cur.idx());
5039 col_type const col = tabular.cellColumn(cur.idx());
5041 if (isRightToLeft(cur)) {
5042 if (tabular.cellColumn(cur.idx()) == 0) {
5043 if (row == tabular.nrows() - 1)
5045 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5050 cur.idx() = tabular.getLastCellInRow(row - 1);
5052 cur.idx() = tabular.cellIndex(row, col - 1);
5055 if (tabular.isLastCell(cur.idx()))
5057 if (cur.idx() == tabular.getLastCellInRow(row))
5058 cur.idx() = tabular.cellIndex(row + 1, 0);
5060 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5061 cur.idx() = tabular.cellIndex(row, colnextcell);
5065 cur.boundary(false);
5067 if (cur.selIsMultiCell()) {
5068 cur.pit() = cur.lastpit();
5069 cur.pos() = cur.lastpos();
5077 // in visual mode, place cursor at extreme left or right
5079 switch(entry_from) {
5081 case ENTRY_DIRECTION_RIGHT:
5082 cur.posVisToRowExtremity(false /* !left */);
5084 case ENTRY_DIRECTION_LEFT:
5085 cur.posVisToRowExtremity(true /* left */);
5087 case ENTRY_DIRECTION_IGNORE:
5088 // nothing to do in this case
5092 cur.setCurrentFont();
5097 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5099 row_type const row = tabular.cellRow(cur.idx());
5100 col_type const col = tabular.cellColumn(cur.idx());
5102 if (isRightToLeft(cur)) {
5103 if (cur.idx() == tabular.getLastCellInRow(row)) {
5106 cur.idx() = tabular.getFirstCellInRow(row);
5107 cur.idx() = tabular.cellAbove(cur.idx());
5109 if (tabular.isLastCell(cur.idx()))
5111 if (cur.idx() == tabular.getLastCellInRow(row))
5112 cur.idx() = tabular.cellIndex(row + 1, 0);
5114 cur.idx() = tabular.cellIndex(row, col + 1);
5117 if (cur.idx() == 0) // first cell
5120 cur.idx() = tabular.getLastCellInRow(row - 1);
5122 cur.idx() = tabular.cellIndex(row, col - 1);
5125 if (cur.selIsMultiCell()) {
5126 cur.pit() = cur.lastpit();
5127 cur.pos() = cur.lastpos();
5132 cur.pit() = cur.lastpit();
5133 cur.pos() = cur.lastpos();
5135 // in visual mode, place cursor at extreme left or right
5137 switch(entry_from) {
5139 case ENTRY_DIRECTION_RIGHT:
5140 cur.posVisToRowExtremity(false /* !left */);
5142 case ENTRY_DIRECTION_LEFT:
5143 cur.posVisToRowExtremity(true /* left */);
5145 case ENTRY_DIRECTION_IGNORE:
5146 // nothing to do in this case
5150 cur.setCurrentFont();
5155 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5157 istringstream is(argument);
5160 if (insetCode(s) != TABULAR_CODE)
5164 size_t safe_guard = 0;
5169 if (safe_guard > 1000) {
5170 LYXERR0("parameter max count reached!");
5174 Tabular::Feature action = Tabular::LAST_ACTION;
5177 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5178 if (s != tabularFeature[i].feature)
5180 action = tabularFeature[i].action;
5183 if (action == Tabular::LAST_ACTION) {
5184 LYXERR0("Feature not found " << s);
5188 if (tabularFeature[i].need_value)
5190 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5191 tabularFeatures(cur, action, val);
5197 static void checkLongtableSpecial(Tabular::ltType & ltt,
5198 string const & special, bool & flag)
5200 if (special == "dl_above") {
5203 } else if (special == "dl_below") {
5204 ltt.bottomDL = flag;
5206 } else if (special == "empty") {
5216 bool InsetTabular::oneCellHasRotationState(bool rotated,
5217 row_type row_start, row_type row_end,
5218 col_type col_start, col_type col_end) const
5220 for (row_type r = row_start; r <= row_end; ++r)
5221 for (col_type c = col_start; c <= col_end; ++c)
5223 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5226 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5233 void InsetTabular::tabularFeatures(Cursor & cur,
5234 Tabular::Feature feature, string const & value)
5236 col_type sel_col_start;
5237 col_type sel_col_end;
5238 row_type sel_row_start;
5239 row_type sel_row_end;
5240 bool setLines = false;
5241 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5242 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5246 case Tabular::M_ALIGN_LEFT:
5247 case Tabular::ALIGN_LEFT:
5248 setAlign = LYX_ALIGN_LEFT;
5251 case Tabular::M_ALIGN_RIGHT:
5252 case Tabular::ALIGN_RIGHT:
5253 setAlign = LYX_ALIGN_RIGHT;
5256 case Tabular::M_ALIGN_CENTER:
5257 case Tabular::ALIGN_CENTER:
5258 setAlign = LYX_ALIGN_CENTER;
5261 case Tabular::ALIGN_BLOCK:
5262 setAlign = LYX_ALIGN_BLOCK;
5265 case Tabular::ALIGN_DECIMAL:
5266 if (tabular.column_info[tabular.cellColumn(cur.idx())].alignment == LYX_ALIGN_DECIMAL)
5267 setAlign = LYX_ALIGN_CENTER;
5269 setAlign = LYX_ALIGN_DECIMAL;
5272 case Tabular::M_VALIGN_TOP:
5273 case Tabular::VALIGN_TOP:
5274 setVAlign = Tabular::LYX_VALIGN_TOP;
5277 case Tabular::M_VALIGN_BOTTOM:
5278 case Tabular::VALIGN_BOTTOM:
5279 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5282 case Tabular::M_VALIGN_MIDDLE:
5283 case Tabular::VALIGN_MIDDLE:
5284 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5291 cur.recordUndoInset(ATOMIC_UNDO, this);
5293 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5294 row_type const row = tabular.cellRow(cur.idx());
5295 col_type const column = tabular.cellColumn(cur.idx());
5297 Tabular::ltType ltt;
5301 case Tabular::SET_TABULAR_WIDTH:
5302 tabular.setTabularWidth(Length(value));
5305 case Tabular::SET_PWIDTH: {
5306 Length const len(value);
5307 tabular.setColumnPWidth(cur, cur.idx(), len);
5309 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5310 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5314 case Tabular::SET_MPWIDTH:
5315 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5318 case Tabular::SET_MROFFSET:
5319 tabular.setMROffset(cur, cur.idx(), Length(value));
5322 case Tabular::SET_SPECIAL_COLUMN:
5323 case Tabular::SET_SPECIAL_MULTICOLUMN:
5324 if (value == "none")
5325 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5327 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5330 case Tabular::APPEND_ROW:
5331 // append the row into the tabular
5332 tabular.appendRow(row);
5335 case Tabular::APPEND_COLUMN:
5336 // append the column into the tabular
5337 tabular.appendColumn(column);
5338 cur.idx() = tabular.cellIndex(row, column);
5341 case Tabular::DELETE_ROW:
5342 if (sel_row_end == tabular.nrows() - 1 && sel_row_start != 0) {
5343 for (col_type c = 0; c < tabular.ncols(); c++)
5344 tabular.setBottomLine(tabular.cellIndex(sel_row_start - 1, c),
5345 tabular.bottomLine(tabular.cellIndex(sel_row_end, c)));
5348 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5349 tabular.deleteRow(sel_row_start);
5350 if (sel_row_start >= tabular.nrows())
5352 cur.idx() = tabular.cellIndex(sel_row_start, column);
5355 cur.setSelection(false);
5358 case Tabular::DELETE_COLUMN:
5359 if (sel_col_end == tabular.ncols() - 1 && sel_col_start != 0) {
5360 for (row_type r = 0; r < tabular.nrows(); r++)
5361 tabular.setRightLine(tabular.cellIndex(r, sel_col_start - 1),
5362 tabular.rightLine(tabular.cellIndex(r, sel_col_end)));
5365 if (sel_col_start == 0 && sel_col_end != tabular.ncols() - 1) {
5366 for (row_type r = 0; r < tabular.nrows(); r++)
5367 tabular.setLeftLine(tabular.cellIndex(r, sel_col_end + 1),
5368 tabular.leftLine(tabular.cellIndex(r, 0)));
5371 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5372 tabular.deleteColumn(sel_col_start);
5373 if (sel_col_start >= tabular.ncols())
5375 cur.idx() = tabular.cellIndex(row, sel_col_start);
5378 cur.setSelection(false);
5381 case Tabular::COPY_ROW:
5382 tabular.copyRow(row);
5385 case Tabular::COPY_COLUMN:
5386 tabular.copyColumn(column);
5387 cur.idx() = tabular.cellIndex(row, column);
5390 case Tabular::SET_LINE_TOP:
5391 case Tabular::TOGGLE_LINE_TOP: {
5392 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5393 ? (value == "true") : !tabular.topLine(cur.idx());
5394 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5395 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5396 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5400 case Tabular::SET_LINE_BOTTOM:
5401 case Tabular::TOGGLE_LINE_BOTTOM: {
5402 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5403 ? (value == "true") : !tabular.bottomLine(cur.idx());
5404 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5405 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5406 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5410 case Tabular::SET_LINE_LEFT:
5411 case Tabular::TOGGLE_LINE_LEFT: {
5412 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5413 ? (value == "true") : !tabular.leftLine(cur.idx());
5414 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5415 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5416 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5420 case Tabular::SET_LINE_RIGHT:
5421 case Tabular::TOGGLE_LINE_RIGHT: {
5422 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5423 ? (value == "true") : !tabular.rightLine(cur.idx());
5424 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5425 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5426 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5430 case Tabular::M_ALIGN_LEFT:
5431 case Tabular::M_ALIGN_RIGHT:
5432 case Tabular::M_ALIGN_CENTER:
5433 case Tabular::ALIGN_LEFT:
5434 case Tabular::ALIGN_RIGHT:
5435 case Tabular::ALIGN_CENTER:
5436 case Tabular::ALIGN_BLOCK:
5437 case Tabular::ALIGN_DECIMAL:
5438 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5439 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5440 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5441 !tabular.getPWidth(c).zero());
5444 case Tabular::M_VALIGN_TOP:
5445 case Tabular::M_VALIGN_BOTTOM:
5446 case Tabular::M_VALIGN_MIDDLE:
5448 case Tabular::VALIGN_TOP:
5449 case Tabular::VALIGN_BOTTOM:
5450 case Tabular::VALIGN_MIDDLE:
5451 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5452 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5453 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5456 case Tabular::SET_MULTICOLUMN: {
5457 if (!cur.selection()) {
5458 // just multicol for one single cell
5459 // check whether we are completely in a multicol
5460 if (!tabular.isMultiColumn(cur.idx()))
5461 tabular.setMultiColumn(cur.idx(), 1,
5462 tabular.rightLine(cur.idx()));
5465 // we have a selection so this means we just add all this
5466 // cells to form a multicolumn cell
5467 idx_type const s_start = cur.selBegin().idx();
5468 row_type const col_start = tabular.cellColumn(s_start);
5469 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5470 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5471 tabular.rightLine(cur.selEnd().idx()));
5474 cur.setSelection(false);
5478 case Tabular::UNSET_MULTICOLUMN: {
5479 if (!cur.selection()) {
5480 if (tabular.isMultiColumn(cur.idx()))
5481 tabular.unsetMultiColumn(cur.idx());
5486 case Tabular::MULTICOLUMN: {
5487 if (!cur.selection()) {
5488 if (tabular.isMultiColumn(cur.idx()))
5489 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5491 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5495 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5496 row_type const r = sel_row_start;
5497 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5498 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5501 // If the selection contains at least one singlecol cell
5502 // or multiple multicol cells,
5503 // we assume the user will merge is to a single multicol
5505 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5507 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5511 case Tabular::SET_MULTIROW: {
5512 if (!cur.selection()) {
5513 // just multirow for one single cell
5514 // check whether we are completely in a multirow
5515 if (!tabular.isMultiRow(cur.idx()))
5516 tabular.setMultiRow(cur.idx(), 1,
5517 tabular.bottomLine(cur.idx()));
5520 // we have a selection so this means we just add all this
5521 // cells to form a multirow cell
5522 idx_type const s_start = cur.selBegin().idx();
5523 row_type const row_start = tabular.cellRow(s_start);
5524 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5525 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5526 tabular.bottomLine(cur.selEnd().idx()));
5529 cur.setSelection(false);
5533 case Tabular::UNSET_MULTIROW: {
5534 if (!cur.selection()) {
5535 if (tabular.isMultiRow(cur.idx()))
5536 tabular.unsetMultiRow(cur.idx());
5541 case Tabular::MULTIROW: {
5542 if (!cur.selection()) {
5543 if (tabular.isMultiRow(cur.idx()))
5544 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5546 tabularFeatures(cur, Tabular::SET_MULTIROW);
5550 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5551 col_type const c = sel_col_start;
5552 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5553 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5556 // If the selection contains at least one singlerow cell
5557 // or multiple multirow cells,
5558 // we assume the user will merge is to a single multirow
5560 tabularFeatures(cur, Tabular::SET_MULTIROW);
5562 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5566 case Tabular::SET_ALL_LINES:
5568 case Tabular::UNSET_ALL_LINES:
5569 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5570 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5571 idx_type const cell = tabular.cellIndex(r, c);
5572 tabular.setTopLine(cell, setLines);
5573 tabular.setBottomLine(cell, setLines);
5574 tabular.setRightLine(cell, setLines);
5575 tabular.setLeftLine(cell, setLines);
5579 case Tabular::SET_BORDER_LINES:
5580 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5581 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5582 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5584 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5585 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5586 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5590 case Tabular::SET_LONGTABULAR:
5591 tabular.is_long_tabular = true;
5594 case Tabular::UNSET_LONGTABULAR:
5595 for (row_type r = 0; r < tabular.nrows(); ++r) {
5596 if (tabular.ltCaption(r)) {
5597 cur.idx() = tabular.cellIndex(r, 0);
5600 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5603 tabular.is_long_tabular = false;
5606 case Tabular::SET_ROTATE_TABULAR:
5607 tabular.rotate = convert<int>(value);
5610 case Tabular::UNSET_ROTATE_TABULAR:
5614 case Tabular::TOGGLE_ROTATE_TABULAR:
5615 // when pressing the rotate button we default to 90° rotation
5616 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5619 case Tabular::TABULAR_VALIGN_TOP:
5620 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5623 case Tabular::TABULAR_VALIGN_MIDDLE:
5624 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5627 case Tabular::TABULAR_VALIGN_BOTTOM:
5628 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5631 case Tabular::LONGTABULAR_ALIGN_LEFT:
5632 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5635 case Tabular::LONGTABULAR_ALIGN_CENTER:
5636 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5639 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5640 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5645 case Tabular::SET_ROTATE_CELL:
5646 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5647 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5648 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5651 case Tabular::UNSET_ROTATE_CELL:
5652 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5653 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5654 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5657 case Tabular::TOGGLE_ROTATE_CELL:
5659 bool oneNotRotated = oneCellHasRotationState(false,
5660 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5662 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5663 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5664 // when pressing the rotate cell button we default to 90° rotation
5666 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5668 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5673 case Tabular::SET_USEBOX: {
5674 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5675 if (val == tabular.getUsebox(cur.idx()))
5676 val = Tabular::BOX_NONE;
5677 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5678 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5679 tabular.setUsebox(tabular.cellIndex(r, c), val);
5683 case Tabular::UNSET_LTFIRSTHEAD:
5685 case Tabular::SET_LTFIRSTHEAD:
5686 tabular.getRowOfLTFirstHead(row, ltt);
5687 checkLongtableSpecial(ltt, value, flag);
5688 tabular.setLTHead(row, flag, ltt, true);
5691 case Tabular::UNSET_LTHEAD:
5693 case Tabular::SET_LTHEAD:
5694 tabular.getRowOfLTHead(row, ltt);
5695 checkLongtableSpecial(ltt, value, flag);
5696 tabular.setLTHead(row, flag, ltt, false);
5699 case Tabular::UNSET_LTFOOT:
5701 case Tabular::SET_LTFOOT:
5702 tabular.getRowOfLTFoot(row, ltt);
5703 checkLongtableSpecial(ltt, value, flag);
5704 tabular.setLTFoot(row, flag, ltt, false);
5707 case Tabular::UNSET_LTLASTFOOT:
5709 case Tabular::SET_LTLASTFOOT:
5710 tabular.getRowOfLTLastFoot(row, ltt);
5711 checkLongtableSpecial(ltt, value, flag);
5712 tabular.setLTFoot(row, flag, ltt, true);
5715 case Tabular::UNSET_LTNEWPAGE:
5717 case Tabular::SET_LTNEWPAGE:
5718 tabular.setLTNewPage(row, flag);
5721 case Tabular::SET_LTCAPTION: {
5722 if (tabular.ltCaption(row))
5724 cur.idx() = tabular.setLTCaption(row, true);
5727 cur.setSelection(false);
5728 // If a row is set as caption, then also insert
5729 // a caption. Otherwise the LaTeX output is broken.
5730 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5731 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5735 case Tabular::UNSET_LTCAPTION: {
5736 if (!tabular.ltCaption(row))
5738 cur.idx() = tabular.setLTCaption(row, false);
5741 cur.setSelection(false);
5742 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5743 if (lyx::getStatus(fr).enabled())
5748 case Tabular::TOGGLE_LTCAPTION: {
5749 if (tabular.ltCaption(row))
5750 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5752 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5756 case Tabular::SET_BOOKTABS:
5757 tabular.use_booktabs = true;
5760 case Tabular::UNSET_BOOKTABS:
5761 tabular.use_booktabs = false;
5764 case Tabular::SET_TOP_SPACE: {
5766 if (value == "default")
5767 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5768 tabular.row_info[r].top_space_default = true;
5769 else if (value == "none")
5770 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5771 tabular.row_info[r].top_space_default = false;
5772 tabular.row_info[r].top_space = len;
5774 else if (isValidLength(value, &len))
5775 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5776 tabular.row_info[r].top_space_default = false;
5777 tabular.row_info[r].top_space = len;
5782 case Tabular::SET_BOTTOM_SPACE: {
5784 if (value == "default")
5785 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5786 tabular.row_info[r].bottom_space_default = true;
5787 else if (value == "none")
5788 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5789 tabular.row_info[r].bottom_space_default = false;
5790 tabular.row_info[r].bottom_space = len;
5792 else if (isValidLength(value, &len))
5793 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5794 tabular.row_info[r].bottom_space_default = false;
5795 tabular.row_info[r].bottom_space = len;
5800 case Tabular::SET_INTERLINE_SPACE: {
5802 if (value == "default")
5803 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5804 tabular.row_info[r].interline_space_default = true;
5805 else if (value == "none")
5806 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5807 tabular.row_info[r].interline_space_default = false;
5808 tabular.row_info[r].interline_space = len;
5810 else if (isValidLength(value, &len))
5811 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5812 tabular.row_info[r].interline_space_default = false;
5813 tabular.row_info[r].interline_space = len;
5818 case Tabular::SET_DECIMAL_POINT:
5819 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5820 tabular.column_info[c].decimal_point = from_utf8(value);
5823 // dummy stuff just to avoid warnings
5824 case Tabular::LAST_ACTION:
5830 bool InsetTabular::copySelection(Cursor & cur)
5832 if (!cur.selection())
5837 getSelection(cur, rs, re, cs, ce);
5839 paste_tabular.reset(new Tabular(tabular));
5841 for (row_type r = 0; r < rs; ++r)
5842 paste_tabular->deleteRow(0);
5844 row_type const rows = re - rs + 1;
5845 while (paste_tabular->nrows() > rows)
5846 paste_tabular->deleteRow(rows);
5848 for (col_type c = 0; c < cs; ++c)
5849 paste_tabular->deleteColumn(0);
5851 col_type const columns = ce - cs + 1;
5852 while (paste_tabular->ncols() > columns)
5853 paste_tabular->deleteColumn(columns);
5855 paste_tabular->setBuffer(tabular.buffer());
5857 odocstringstream os;
5858 OutputParams const runparams(0);
5859 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5860 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5861 cap::copySelection(cur, os.str());
5863 // mark tabular stack dirty
5864 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5865 // when we (hopefully) have a one-for-all paste mechanism.
5866 // This must be called after cap::copySelection.
5867 dirtyTabularStack(true);
5873 bool InsetTabular::pasteClipboard(Cursor & cur)
5877 col_type actcol = tabular.cellColumn(cur.idx());
5878 row_type actrow = tabular.cellRow(cur.idx());
5880 if (cur.selIsMultiCell()) {
5883 getSelection(cur, actrow, re, actcol, ce);
5886 for (row_type r1 = 0, r2 = actrow;
5887 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5889 for (col_type c1 = 0, c2 = actcol;
5890 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5892 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5893 tabular.isPartOfMultiColumn(r2, c2))
5895 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5899 if (tabular.isPartOfMultiColumn(r2, c2)) {
5903 shared_ptr<InsetTableCell> inset(
5904 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5905 tabular.setCellInset(r2, c2, inset);
5906 // FIXME?: why do we need to do this explicitly? (EL)
5907 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5909 // FIXME: change tracking (MG)
5910 inset->setChange(Change(buffer().params().trackChanges ?
5911 Change::INSERTED : Change::UNCHANGED));
5919 void InsetTabular::cutSelection(Cursor & cur)
5921 if (!cur.selection())
5926 getSelection(cur, rs, re, cs, ce);
5927 for (row_type r = rs; r <= re; ++r) {
5928 for (col_type c = cs; c <= ce; ++c) {
5929 shared_ptr<InsetTableCell> t
5930 = cell(tabular.cellIndex(r, c));
5931 if (buffer().params().trackChanges)
5932 // FIXME: Change tracking (MG)
5933 t->setChange(Change(Change::DELETED));
5939 // cursor position might be invalid now
5940 if (cur.pit() > cur.lastpit())
5941 cur.pit() = cur.lastpit();
5942 if (cur.pos() > cur.lastpos())
5943 cur.pos() = cur.lastpos();
5944 cur.clearSelection();
5948 bool InsetTabular::isRightToLeft(Cursor & cur) const
5950 LASSERT(cur.depth() > 1, /**/);
5951 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5952 pos_type const parentpos = cur[cur.depth() - 2].pos();
5953 return parentpar.getFontSettings(buffer().params(),
5954 parentpos).language()->rightToLeft();
5958 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5961 LASSERT(stidx <= enidx, return docstring());
5963 col_type const col1 = tabular.cellColumn(stidx);
5964 col_type const col2 = tabular.cellColumn(enidx);
5965 row_type const row1 = tabular.cellRow(stidx);
5966 row_type const row2 = tabular.cellRow(enidx);
5968 for (col_type col = col1; col <= col2; col++)
5969 for (row_type row = row1; row <= row2; row++) {
5974 retval += tabular.cellInset(row, col)->asString(intoInsets);
5980 void InsetTabular::getSelection(Cursor & cur,
5981 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5983 CursorSlice const & beg = cur.selBegin();
5984 CursorSlice const & end = cur.selEnd();
5985 cs = tabular.cellColumn(beg.idx());
5986 ce = tabular.cellColumn(end.idx());
5990 rs = tabular.cellRow(beg.idx());
5991 re = tabular.cellRow(end.idx());
5997 Text * InsetTabular::getText(int idx) const
5999 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
6003 void InsetTabular::setChange(Change const & change)
6005 for (idx_type idx = 0; idx < nargs(); ++idx)
6006 cell(idx)->setChange(change);
6010 void InsetTabular::acceptChanges()
6012 for (idx_type idx = 0; idx < nargs(); ++idx)
6013 cell(idx)->acceptChanges();
6017 void InsetTabular::rejectChanges()
6019 for (idx_type idx = 0; idx < nargs(); ++idx)
6020 cell(idx)->rejectChanges();
6024 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
6026 return tabular.getPWidth(cell).zero();
6030 bool InsetTabular::forcePlainLayout(idx_type cell) const
6032 return !tabular.getPWidth(cell).zero();
6036 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
6039 if (buf.length() <= 0)
6044 col_type maxCols = 1;
6045 size_t const len = buf.length();
6049 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6057 maxCols = max(cols, maxCols);
6063 maxCols = max(cols, maxCols);
6069 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6070 loctab = paste_tabular.get();
6072 dirtyTabularStack(true);
6075 cell = bv.cursor().idx();
6076 ocol = tabular.cellColumn(cell);
6077 row = tabular.cellRow(cell);
6081 idx_type const cells = loctab->numberofcells;
6084 rows = loctab->nrows();
6085 col_type const columns = loctab->ncols();
6087 while (cell < cells && p < len && row < rows &&
6088 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6094 // we can only set this if we are not too far right
6095 if (cols < columns) {
6096 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6097 Font const font = bv.textMetrics(&inset->text()).
6099 inset->setText(buf.substr(op, p - op), font,
6100 buffer().params().trackChanges);
6106 // we can only set this if we are not too far right
6107 if (cols < columns) {
6108 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6109 Font const font = bv.textMetrics(&inset->text()).
6111 inset->setText(buf.substr(op, p - op), font,
6112 buffer().params().trackChanges);
6117 cell = loctab->cellIndex(row, cols);
6123 // check for the last cell if there is no trailing '\n'
6124 if (cell < cells && op < len) {
6125 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6126 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6127 inset->setText(buf.substr(op, len - op), font,
6128 buffer().params().trackChanges);
6134 void InsetTabular::addPreview(DocIterator const & inset_pos,
6135 PreviewLoader & loader) const
6137 DocIterator cell_pos = inset_pos;
6139 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6140 for (row_type r = 0; r < tabular.nrows(); ++r) {
6141 for (col_type c = 0; c < tabular.ncols(); ++c) {
6142 cell_pos.top().idx() = tabular.cellIndex(r, c);
6143 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6149 bool InsetTabular::completionSupported(Cursor const & cur) const
6151 Cursor const & bvCur = cur.bv().cursor();
6152 if (&bvCur.inset() != this)
6154 return cur.text()->completionSupported(cur);
6158 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6160 return completionSupported(cur);
6164 bool InsetTabular::automaticInlineCompletion() const
6166 return lyxrc.completion_inline_text;
6170 bool InsetTabular::automaticPopupCompletion() const
6172 return lyxrc.completion_popup_text;
6176 bool InsetTabular::showCompletionCursor() const
6178 return lyxrc.completion_cursor_text;
6182 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6184 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6188 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6190 if (!completionSupported(cur))
6192 return cur.text()->completionPrefix(cur);
6196 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6198 if (!completionSupported(cur))
6201 return cur.text()->insertCompletion(cur, s, finished);
6205 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6206 Dimension & dim) const
6208 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6209 tm.completionPosAndDim(cur, x, y, dim);
6213 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6215 istringstream data(in);
6217 lex.setStream(data);
6224 if (!lex || token != "tabular") {
6225 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6229 // This is part of the inset proper that is usually swallowed
6230 // by Buffer::readInset
6232 if (!lex || token != "Tabular") {
6233 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6241 string InsetTabular::params2string(InsetTabular const & inset)
6244 data << "tabular" << ' ';
6246 data << "\\end_inset\n";