2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
169 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
170 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
171 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
172 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
173 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
174 { Tabular::SET_BOOKTABS, "set-booktabs", false },
175 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
176 { Tabular::SET_TOP_SPACE, "set-top-space", true },
177 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
178 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
179 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
180 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
181 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
182 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
183 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
184 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
185 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
186 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
187 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
188 { Tabular::LAST_ACTION, "", false }
192 string const tostr(LyXAlignment const & num)
197 case LYX_ALIGN_BLOCK:
201 case LYX_ALIGN_CENTER:
203 case LYX_ALIGN_RIGHT:
205 case LYX_ALIGN_LAYOUT:
207 case LYX_ALIGN_SPECIAL:
209 case LYX_ALIGN_DECIMAL:
216 string const tostr(Tabular::HAlignment const & num)
219 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
221 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
223 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
230 string const tostr(Tabular::VAlignment const & num)
233 case Tabular::LYX_VALIGN_TOP:
235 case Tabular::LYX_VALIGN_MIDDLE:
237 case Tabular::LYX_VALIGN_BOTTOM:
244 string const tostr(Tabular::BoxType const & num)
247 case Tabular::BOX_NONE:
249 case Tabular::BOX_PARBOX:
251 case Tabular::BOX_MINIPAGE:
258 // I would have liked a fromstr template a lot better. (Lgb)
259 bool string2type(string const str, LyXAlignment & num)
262 num = LYX_ALIGN_NONE;
263 else if (str == "block")
264 num = LYX_ALIGN_BLOCK;
265 else if (str == "left")
266 num = LYX_ALIGN_LEFT;
267 else if (str == "center")
268 num = LYX_ALIGN_CENTER;
269 else if (str == "right")
270 num = LYX_ALIGN_RIGHT;
271 else if (str == "decimal")
272 num = LYX_ALIGN_DECIMAL;
279 bool string2type(string const str, Tabular::HAlignment & num)
282 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
283 else if (str == "center" )
284 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
285 else if (str == "right")
286 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
293 bool string2type(string const str, Tabular::VAlignment & num)
296 num = Tabular::LYX_VALIGN_TOP;
297 else if (str == "middle" )
298 num = Tabular::LYX_VALIGN_MIDDLE;
299 else if (str == "bottom")
300 num = Tabular::LYX_VALIGN_BOTTOM;
307 bool string2type(string const str, Tabular::BoxType & num)
310 num = Tabular::BOX_NONE;
311 else if (str == "parbox")
312 num = Tabular::BOX_PARBOX;
313 else if (str == "minipage")
314 num = Tabular::BOX_MINIPAGE;
321 bool string2type(string const str, bool & num)
325 else if (str == "false")
333 bool getTokenValue(string const & str, char const * token, string & ret)
336 size_t token_length = strlen(token);
337 size_t pos = str.find(token);
339 if (pos == string::npos || pos + token_length + 1 >= str.length()
340 || str[pos + token_length] != '=')
342 pos += token_length + 1;
344 if (ch != '"' && ch != '\'') { // only read till next space
348 while (pos < str.length() - 1 && str[++pos] != ch)
355 bool getTokenValue(string const & str, char const * token, docstring & ret)
358 bool const success = getTokenValue(str, token, tmp);
359 ret = from_utf8(tmp);
364 bool getTokenValue(string const & str, char const * token, int & num)
368 if (!getTokenValue(str, token, tmp))
370 num = convert<int>(tmp);
375 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
378 return getTokenValue(str, token, tmp) && string2type(tmp, num);
382 bool getTokenValue(string const & str, char const * token,
383 Tabular::HAlignment & num)
386 return getTokenValue(str, token, tmp) && string2type(tmp, num);
390 bool getTokenValue(string const & str, char const * token,
391 Tabular::VAlignment & num)
394 return getTokenValue(str, token, tmp) && string2type(tmp, num);
398 bool getTokenValue(string const & str, char const * token,
399 Tabular::BoxType & num)
402 return getTokenValue(str, token, tmp) && string2type(tmp, num);
406 bool getTokenValue(string const & str, char const * token, bool & flag)
408 // set the flag always to false as this should be the default for bools
409 // not in the file-format.
412 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
416 bool getTokenValue(string const & str, char const * token, Length & len)
418 // set the length to be zero() as default as this it should be if not
419 // in the file format.
422 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
426 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
431 if (!getTokenValue(str, token, tmp))
433 if (tmp == "default") {
437 return isValidLength(tmp, &len);
441 void l_getline(istream & is, string & str)
444 while (str.empty()) {
446 if (!str.empty() && str[str.length() - 1] == '\r')
447 str.erase(str.length() - 1);
452 string const write_attribute(string const & name, T const & t)
454 string const s = tostr(t);
455 return s.empty() ? s : " " + name + "=\"" + s + "\"";
459 string const write_attribute(string const & name, string const & t)
461 return t.empty() ? t : " " + name + "=\"" + t + "\"";
466 string const write_attribute(string const & name, docstring const & t)
468 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
473 string const write_attribute(string const & name, bool const & b)
475 // we write only true attribute values so we remove a bit of the
476 // file format bloat for tabulars.
477 return b ? write_attribute(name, convert<string>(b)) : string();
482 string const write_attribute(string const & name, int const & i)
484 // we write only true attribute values so we remove a bit of the
485 // file format bloat for tabulars.
486 return i ? write_attribute(name, convert<string>(i)) : string();
491 string const write_attribute(string const & name, Tabular::idx_type const & i)
493 // we write only true attribute values so we remove a bit of the
494 // file format bloat for tabulars.
495 return i ? write_attribute(name, convert<string>(i)) : string();
500 string const write_attribute(string const & name, Length const & value)
502 // we write only the value if we really have one same reson as above.
503 return value.zero() ? string() : write_attribute(name, value.asString());
509 string const featureAsString(Tabular::Feature action)
511 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
512 if (tabularFeature[i].action == action)
513 return tabularFeature[i].feature;
519 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
521 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
522 for (; dit; dit.forwardChar())
523 if (dit.inTexted() && dit.depth() == 1
524 && dit.paragraph().find(align_d, false, false, dit.pos()))
531 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
533 InsetTableCell tail = InsetTableCell(head);
534 DocIterator const dit = separatorPos(&head, align_d);
537 pit_type const psize = head.paragraphs().front().size();
538 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
539 tail.paragraphs().front().eraseChars(0,
540 dit.pos() < psize ? dit.pos() + 1 : psize, false);
547 /////////////////////////////////////////////////////////////////////
551 /////////////////////////////////////////////////////////////////////
554 Tabular::CellData::CellData(Buffer * buf)
557 multicolumn(Tabular::CELL_NORMAL),
558 multirow(Tabular::CELL_NORMAL),
559 alignment(LYX_ALIGN_CENTER),
560 valignment(LYX_VALIGN_TOP),
570 inset(new InsetTableCell(buf))
572 inset->setBuffer(*buf);
576 Tabular::CellData::CellData(CellData const & cs)
579 multicolumn(cs.multicolumn),
580 multirow(cs.multirow),
581 mroffset(cs.mroffset),
582 alignment(cs.alignment),
583 valignment(cs.valignment),
584 decimal_hoffset(cs.decimal_hoffset),
585 decimal_width(cs.decimal_width),
587 top_line(cs.top_line),
588 bottom_line(cs.bottom_line),
589 left_line(cs.left_line),
590 right_line(cs.right_line),
593 align_special(cs.align_special),
595 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
599 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
605 void Tabular::CellData::swap(CellData & rhs)
607 std::swap(cellno, rhs.cellno);
608 std::swap(width, rhs.width);
609 std::swap(multicolumn, rhs.multicolumn);
610 std::swap(multirow, rhs.multirow);
611 std::swap(mroffset, rhs.mroffset);
612 std::swap(alignment, rhs.alignment);
613 std::swap(valignment, rhs.valignment);
614 std::swap(decimal_hoffset, rhs.decimal_hoffset);
615 std::swap(decimal_width, rhs.decimal_width);
616 std::swap(voffset, rhs.voffset);
617 std::swap(top_line, rhs.top_line);
618 std::swap(bottom_line, rhs.bottom_line);
619 std::swap(left_line, rhs.left_line);
620 std::swap(right_line, rhs.right_line);
621 std::swap(usebox, rhs.usebox);
622 std::swap(rotate, rhs.rotate);
623 std::swap(align_special, rhs.align_special);
624 p_width.swap(rhs.p_width);
625 inset.swap(rhs.inset);
629 Tabular::RowData::RowData()
632 top_space_default(false),
633 bottom_space_default(false),
634 interline_space_default(false),
644 Tabular::ColumnData::ColumnData()
645 : alignment(LYX_ALIGN_CENTER),
646 valignment(LYX_VALIGN_TOP),
652 Tabular::ltType::ltType()
659 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
661 init(buffer, rows_arg, columns_arg);
665 void Tabular::setBuffer(Buffer & buffer)
668 for (row_type i = 0; i < nrows(); ++i)
669 for (col_type j = 0; j < ncols(); ++j)
670 cell_info[i][j].inset->setBuffer(*buffer_);
674 // activates all lines and sets all widths to 0
675 void Tabular::init(Buffer * buf, row_type rows_arg,
676 col_type columns_arg)
679 row_info = row_vector(rows_arg);
680 column_info = column_vector(columns_arg);
681 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
682 row_info.reserve(10);
683 column_info.reserve(10);
684 cell_info.reserve(100);
686 is_long_tabular = false;
687 tabular_valignment = LYX_VALIGN_MIDDLE;
688 tabular_width = Length();
689 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
691 use_booktabs = false;
692 // set silly default lines
693 for (row_type r = 0; r < nrows(); ++r)
694 for (col_type c = 0; c < ncols(); ++c) {
695 cell_info[r][c].inset->setBuffer(*buffer_);
696 cell_info[r][c].top_line = true;
697 cell_info[r][c].left_line = true;
698 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
699 cell_info[r][c].right_line = c == ncols() - 1;
704 void Tabular::deleteRow(row_type const row)
706 // Not allowed to delete last row
710 for (col_type c = 0; c < ncols(); ++c) {
711 // Care about multirow cells
712 if (row + 1 < nrows() &&
713 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
714 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
715 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
718 row_info.erase(row_info.begin() + row);
719 cell_info.erase(cell_info.begin() + row);
724 void Tabular::copyRow(row_type const row)
726 insertRow(row, true);
730 void Tabular::appendRow(row_type row)
732 insertRow(row, false);
736 void Tabular::insertRow(row_type const row, bool copy)
738 row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
739 cell_info.insert(cell_info.begin() + row + 1,
740 cell_vector(0, CellData(buffer_)));
742 for (col_type c = 0; c < ncols(); ++c) {
743 cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
744 copy ? CellData(cell_info[row][c]) : CellData(buffer_));
745 if (buffer().params().trackChanges)
746 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
747 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
748 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
752 for (col_type c = 0; c < ncols(); ++c) {
753 if (isPartOfMultiRow(row, c))
755 // inherit line settings
756 idx_type const i = cellIndex(row + 1, c);
757 idx_type const j = cellIndex(row, c);
758 setLeftLine(i, leftLine(j));
759 setRightLine(i, rightLine(j));
760 setTopLine(i, topLine(j));
761 if (topLine(j) && bottomLine(j)) {
762 setBottomLine(i, true);
763 setBottomLine(j, false);
765 // mark track changes
766 if (buffer().params().trackChanges)
767 cellInfo(i).inset->setChange(Change(Change::INSERTED));
772 void Tabular::deleteColumn(col_type const col)
774 // Not allowed to delete last column
778 for (row_type r = 0; r < nrows(); ++r) {
779 // Care about multicolumn cells
780 if (col + 1 < ncols() &&
781 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
782 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
783 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
785 cell_info[r].erase(cell_info[r].begin() + col);
787 column_info.erase(column_info.begin() + col);
792 void Tabular::copyColumn(col_type const col)
794 insertColumn(col, true);
798 void Tabular::appendColumn(col_type col)
800 insertColumn(col, false);
804 void Tabular::insertColumn(col_type const col, bool copy)
806 BufferParams const & bp = buffer().params();
807 column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
809 for (row_type r = 0; r < nrows(); ++r) {
810 cell_info[r].insert(cell_info[r].begin() + col + 1,
811 copy ? CellData(cell_info[r][col]) : CellData(buffer_));
813 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
814 if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
815 cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
818 for (row_type r = 0; r < nrows(); ++r) {
819 // inherit line settings
820 idx_type const i = cellIndex(r, col + 1);
821 idx_type const j = cellIndex(r, col);
822 setBottomLine(i, bottomLine(j));
823 setTopLine(i, topLine(j));
824 setLeftLine(i, leftLine(j));
825 if (rightLine(j) && rightLine(j)) {
826 setRightLine(i, true);
827 setRightLine(j, false);
829 if (buffer().params().trackChanges)
830 cellInfo(i).inset->setChange(Change(Change::INSERTED));
835 void Tabular::updateIndexes()
840 for (row_type row = 0; row < nrows(); ++row)
841 for (col_type column = 0; column < ncols(); ++column) {
842 if (!isPartOfMultiColumn(row, column)
843 && !isPartOfMultiRow(row, column))
845 if (isPartOfMultiRow(row, column))
846 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
848 cell_info[row][column].cellno = numberofcells - 1;
851 rowofcell.resize(numberofcells);
852 columnofcell.resize(numberofcells);
854 // reset column and row of cells and update their width and alignment
855 for (row_type row = 0; row < nrows(); ++row)
856 for (col_type column = 0; column < ncols(); ++column) {
857 if (isPartOfMultiColumn(row, column))
859 // columnofcell needs to be called before setting width and aligment
860 // multirow cells inherit the width from the column width
861 if (!isPartOfMultiRow(row, column)) {
862 columnofcell[i] = column;
865 setFixedWidth(row, column);
866 if (isPartOfMultiRow(row, column))
868 cell_info[row][column].inset->setContentAlignment(
869 getAlignment(cellIndex(row, column)));
875 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
878 for (col_type c = 0; c < ncols(); ++c)
879 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
885 bool Tabular::topLine(idx_type const cell) const
887 return cellInfo(cell).top_line;
891 bool Tabular::bottomLine(idx_type const cell) const
893 return cellInfo(cell).bottom_line;
897 bool Tabular::leftLine(idx_type cell) const
901 return cellInfo(cell).left_line;
905 bool Tabular::rightLine(idx_type cell) const
909 return cellInfo(cell).right_line;
913 int Tabular::interRowSpace(row_type row) const
915 if (!row || row >= nrows())
918 int const interline_space = row_info[row - 1].interline_space_default ?
920 row_info[row - 1].interline_space.inPixels(width());
921 if (rowTopLine(row) && rowBottomLine(row - 1))
922 return interline_space + WIDTH_OF_LINE;
923 return interline_space;
927 int Tabular::interColumnSpace(idx_type cell) const
929 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
930 if (rightLine(cell) && nextcol < ncols()
931 && leftLine(cellIndex(cellRow(cell), nextcol)))
932 return WIDTH_OF_LINE;
937 int Tabular::cellWidth(idx_type cell) const
940 col_type const span = columnSpan(cell);
941 col_type const col = cellColumn(cell);
942 for(col_type c = col; c < col + span ; ++c)
943 w += column_info[c].width;
948 int Tabular::cellHeight(idx_type cell) const
950 row_type const span = rowSpan(cell);
951 row_type const row = cellRow(cell);
953 for(row_type r = row; r < row + span ; ++r) {
954 h += rowAscent(r) + rowDescent(r);
955 if (r != row + span - 1)
956 h += interRowSpace(r + 1);
963 bool Tabular::updateColumnWidths()
965 vector<int> max_dwidth(ncols(), 0);
966 for(col_type c = 0; c < ncols(); ++c)
967 for(row_type r = 0; r < nrows(); ++r) {
968 idx_type const i = cellIndex(r, c);
969 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
970 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
974 // for each col get max of single col cells
975 for(col_type c = 0; c < ncols(); ++c) {
977 for(row_type r = 0; r < nrows(); ++r) {
978 idx_type const i = cellIndex(r, c);
979 if (columnSpan(i) == 1) {
980 if (getAlignment(i) == LYX_ALIGN_DECIMAL
981 && cell_info[r][c].decimal_width!=0)
982 new_width = max(new_width, cellInfo(i).width
983 + max_dwidth[c] - cellInfo(i).decimal_width);
985 new_width = max(new_width, cellInfo(i).width);
989 if (column_info[c].width != new_width) {
990 column_info[c].width = new_width;
994 // update col widths to fit merged cells
995 for(col_type c = 0; c < ncols(); ++c)
996 for(row_type r = 0; r < nrows(); ++r) {
997 idx_type const i = cellIndex(r, c);
998 int const span = columnSpan(i);
999 if (span == 1 || c > cellColumn(i))
1003 for(col_type j = c; j < c + span ; ++j)
1004 old_width += column_info[j].width;
1006 if (cellInfo(i).width > old_width) {
1007 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1016 int Tabular::width() const
1019 for (col_type c = 0; c < ncols(); ++c)
1020 width += column_info[c].width;
1025 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1028 col_type const col = cellColumn(cell);
1029 // set alignment for the whole row if we are not in a multicolumn cell,
1030 // exclude possible multicolumn cells in the row
1031 if (!isMultiColumn(cell)) {
1032 for (row_type r = 0; r < nrows(); ++r) {
1033 // only if the column has no width the multirow inherits the
1034 // alignment of the column, otherwise it is left aligned
1035 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1036 && !isMultiColumn(cellIndex(r, col))) {
1037 cell_info[r][col].alignment = align;
1038 cell_info[r][col].inset->setContentAlignment(align);
1040 if ((isMultiRow(cellIndex(r, col)) && has_width)
1041 && !isMultiColumn(cellIndex(r, col))) {
1042 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1043 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1046 column_info[col].alignment = align;
1047 docstring & dpoint = column_info[col].decimal_point;
1048 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1049 dpoint = from_utf8(lyxrc.default_decimal_point);
1051 cellInfo(cell).alignment = align;
1052 cellInset(cell).get()->setContentAlignment(align);
1057 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1060 if (!isMultiColumn(cell) || onlycolumn)
1061 column_info[cellColumn(cell)].valignment = align;
1063 cellInfo(cell).valignment = align;
1070 * Allow line and paragraph breaks for fixed width cells or disallow them,
1071 * merge cell paragraphs and reset layout to standard for variable width
1074 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1076 inset->setAutoBreakRows(fixedWidth);
1077 inset->toggleFixedWidth(fixedWidth);
1081 // merge all paragraphs to one
1082 BufferParams const & bp = cur.bv().buffer().params();
1083 while (inset->paragraphs().size() > 1)
1084 mergeParagraph(bp, inset->paragraphs(), 0);
1088 // undo information has already been recorded
1089 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1090 bp.documentClass().plainLayoutName());
1097 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1098 Length const & width)
1100 col_type const c = cellColumn(cell);
1102 column_info[c].p_width = width;
1103 // reset the vertical alignment to top if the fixed with
1104 // is removed or zero because only fixed width columns can
1105 // have a vertical alignment
1106 if (column_info[c].p_width.zero())
1107 column_info[c].valignment = LYX_VALIGN_TOP;
1108 for (row_type r = 0; r < nrows(); ++r) {
1109 idx_type const cell = cellIndex(r, c);
1110 // because of multicolumns
1111 toggleFixedWidth(cur, cellInset(cell).get(),
1112 !getPWidth(cell).zero());
1114 // cur paragraph can become invalid after paragraphs were merged
1115 if (cur.pit() > cur.lastpit())
1116 cur.pit() = cur.lastpit();
1117 // cur position can become invalid after newlines were removed
1118 if (cur.pos() > cur.lastpos())
1119 cur.pos() = cur.lastpos();
1123 bool Tabular::setFixedWidth(row_type r, col_type c)
1125 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1126 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1127 || (multicol && !cell_info[r][c].p_width.zero());
1128 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1133 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1134 Length const & width)
1136 if (!isMultiColumn(cell))
1139 cellInfo(cell).p_width = width;
1140 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1141 // cur paragraph can become invalid after paragraphs were merged
1142 if (cur.pit() > cur.lastpit())
1143 cur.pit() = cur.lastpit();
1144 // cur position can become invalid after newlines were removed
1145 if (cur.pos() > cur.lastpos())
1146 cur.pos() = cur.lastpos();
1151 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1153 cellInfo(cell).mroffset = mroffset;
1158 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1159 Tabular::Feature what)
1161 if (what == SET_SPECIAL_MULTICOLUMN)
1162 cellInfo(cell).align_special = special;
1164 column_info[cellColumn(cell)].align_special = special;
1168 void Tabular::setTopLine(idx_type i, bool line)
1170 cellInfo(i).top_line = line;
1174 void Tabular::setBottomLine(idx_type i, bool line)
1176 cellInfo(i).bottom_line = line;
1180 void Tabular::setLeftLine(idx_type cell, bool line)
1182 cellInfo(cell).left_line = line;
1186 void Tabular::setRightLine(idx_type cell, bool line)
1188 cellInfo(cell).right_line = line;
1191 bool Tabular::rowTopLine(row_type r) const
1193 bool all_rows_set = true;
1194 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1195 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1196 return all_rows_set;
1200 bool Tabular::rowBottomLine(row_type r) const
1202 bool all_rows_set = true;
1203 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1204 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1205 return all_rows_set;
1209 bool Tabular::columnLeftLine(col_type c) const
1216 for (row_type r = 0; r < nrows(); ++r) {
1217 idx_type const i = cellIndex(r, c);
1218 if (c == cellColumn(i)) {
1220 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1221 if (cellInfo(i).left_line || right)
1225 return 2 * nrows_left >= total;
1229 bool Tabular::columnRightLine(col_type c) const
1234 int nrows_right = 0;
1236 for (row_type r = 0; r < nrows(); ++r) {
1237 idx_type i = cellIndex(r, c);
1238 if (c == cellColumn(i) + columnSpan(i) - 1) {
1240 bool left = (c + 1 < ncols()
1241 && cellInfo(cellIndex(r, c + 1)).left_line)
1242 || c + 1 == ncols();
1243 if (cellInfo(i).right_line && left)
1247 return 2 * nrows_right >= total;
1251 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1253 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1254 return cellInfo(cell).alignment;
1256 return column_info[cellColumn(cell)].alignment;
1261 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1263 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1264 return cellInfo(cell).valignment;
1265 return column_info[cellColumn(cell)].valignment;
1269 Length const Tabular::getPWidth(idx_type cell) const
1271 if (isMultiColumn(cell))
1272 return cellInfo(cell).p_width;
1273 return column_info[cellColumn(cell)].p_width;
1277 Length const Tabular::getMROffset(idx_type cell) const
1279 return cellInfo(cell).mroffset;
1283 int Tabular::textHOffset(idx_type cell) const
1285 // the LaTeX Way :-(
1286 int x = WIDTH_OF_LINE;
1288 int const w = cellWidth(cell) - cellInfo(cell).width;
1290 switch (getAlignment(cell)) {
1291 case LYX_ALIGN_CENTER:
1294 case LYX_ALIGN_RIGHT:
1297 case LYX_ALIGN_DECIMAL: {
1298 // we center when no decimal point
1299 if (cellInfo(cell).decimal_width == 0) {
1303 col_type const c = cellColumn(cell);
1304 int max_dhoffset = 0;
1305 for(row_type r = 0; r < row_info.size() ; ++r) {
1306 idx_type const i = cellIndex(r, c);
1307 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1308 && cellInfo(i).decimal_width != 0)
1309 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1311 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1314 // LYX_ALIGN_LEFT: nothing :-)
1322 int Tabular::textVOffset(idx_type cell) const
1324 int voffset = cellInfo(cell).voffset;
1325 if (isMultiRow(cell)) {
1326 row_type const row = cellRow(cell);
1327 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1333 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1336 idx_type const numcells = numberOfCellsInRow(row);
1337 // we check against numcells to make sure we do not crash if all the
1338 // cells are multirow (bug #7535), but in that case our return value
1339 // is really invalid, i.e., it is NOT the first cell in the row. but
1340 // i do not know what to do here. (rgh)
1341 while (c < numcells - 1
1342 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1344 return cell_info[row][c].cellno;
1348 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1350 col_type c = ncols() - 1;
1351 // of course we check against 0 so we don't crash. but we have the same
1352 // problem as in the previous routine: if all the cells are part of a
1353 // multirow or part of a multi column, then our return value is invalid.
1355 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1356 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1358 return cell_info[row][c].cellno;
1362 Tabular::row_type Tabular::cellRow(idx_type cell) const
1364 if (cell >= numberofcells)
1368 return rowofcell[cell];
1372 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1374 if (cell >= numberofcells)
1378 return columnofcell[cell];
1382 void Tabular::write(ostream & os) const
1386 << write_attribute("version", 3)
1387 << write_attribute("rows", nrows())
1388 << write_attribute("columns", ncols())
1390 // global longtable options
1392 << write_attribute("rotate", convert<string>(rotate))
1393 << write_attribute("booktabs", use_booktabs)
1394 << write_attribute("islongtable", is_long_tabular)
1395 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1396 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1397 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1398 << write_attribute("headTopDL", endhead.topDL)
1399 << write_attribute("headBottomDL", endhead.bottomDL)
1400 << write_attribute("footTopDL", endfoot.topDL)
1401 << write_attribute("footBottomDL", endfoot.bottomDL)
1402 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1403 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1404 << write_attribute("lastFootEmpty", endlastfoot.empty);
1405 // longtables cannot be aligned vertically
1406 if (!is_long_tabular) {
1407 os << write_attribute("tabularvalignment", tabular_valignment);
1408 os << write_attribute("tabularwidth", tabular_width);
1410 if (is_long_tabular)
1411 os << write_attribute("longtabularalignment",
1412 longtabular_alignment);
1414 for (col_type c = 0; c < ncols(); ++c) {
1416 << write_attribute("alignment", column_info[c].alignment);
1417 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1418 os << write_attribute("decimal_point", column_info[c].decimal_point);
1419 os << write_attribute("valignment", column_info[c].valignment)
1420 << write_attribute("width", column_info[c].p_width.asString())
1421 << write_attribute("special", column_info[c].align_special)
1424 for (row_type r = 0; r < nrows(); ++r) {
1425 static const string def("default");
1427 if (row_info[r].top_space_default)
1428 os << write_attribute("topspace", def);
1430 os << write_attribute("topspace", row_info[r].top_space);
1431 if (row_info[r].bottom_space_default)
1432 os << write_attribute("bottomspace", def);
1434 os << write_attribute("bottomspace", row_info[r].bottom_space);
1435 if (row_info[r].interline_space_default)
1436 os << write_attribute("interlinespace", def);
1438 os << write_attribute("interlinespace", row_info[r].interline_space);
1439 os << write_attribute("endhead", row_info[r].endhead)
1440 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1441 << write_attribute("endfoot", row_info[r].endfoot)
1442 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1443 << write_attribute("newpage", row_info[r].newpage)
1444 << write_attribute("caption", row_info[r].caption)
1446 for (col_type c = 0; c < ncols(); ++c) {
1448 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1449 << write_attribute("multirow", cell_info[r][c].multirow)
1450 << write_attribute("mroffset", cell_info[r][c].mroffset)
1451 << write_attribute("alignment", cell_info[r][c].alignment)
1452 << write_attribute("valignment", cell_info[r][c].valignment)
1453 << write_attribute("topline", cell_info[r][c].top_line)
1454 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1455 << write_attribute("leftline", cell_info[r][c].left_line)
1456 << write_attribute("rightline", cell_info[r][c].right_line)
1457 << write_attribute("rotate", cell_info[r][c].rotate)
1458 << write_attribute("usebox", cell_info[r][c].usebox)
1459 << write_attribute("width", cell_info[r][c].p_width)
1460 << write_attribute("special", cell_info[r][c].align_special)
1462 os << "\\begin_inset ";
1463 cell_info[r][c].inset->write(os);
1464 os << "\n\\end_inset\n"
1469 os << "</lyxtabular>\n";
1473 void Tabular::read(Lexer & lex)
1476 istream & is = lex.getStream();
1478 l_getline(is, line);
1479 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1480 LASSERT(false, /**/);
1485 if (!getTokenValue(line, "version", version))
1487 LASSERT(version >= 2, /**/);
1490 if (!getTokenValue(line, "rows", rows_arg))
1493 if (!getTokenValue(line, "columns", columns_arg))
1495 init(buffer_, rows_arg, columns_arg);
1496 l_getline(is, line);
1497 if (!prefixIs(line, "<features")) {
1498 lyxerr << "Wrong tabular format (expected <features ...> got"
1499 << line << ')' << endl;
1502 getTokenValue(line, "rotate", rotate);
1503 getTokenValue(line, "booktabs", use_booktabs);
1504 getTokenValue(line, "islongtable", is_long_tabular);
1505 getTokenValue(line, "tabularvalignment", tabular_valignment);
1506 getTokenValue(line, "tabularwidth", tabular_width);
1507 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1508 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1509 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1510 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1511 getTokenValue(line, "headTopDL", endhead.topDL);
1512 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1513 getTokenValue(line, "footTopDL", endfoot.topDL);
1514 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1515 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1516 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1517 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1519 for (col_type c = 0; c < ncols(); ++c) {
1521 if (!prefixIs(line,"<column")) {
1522 lyxerr << "Wrong tabular format (expected <column ...> got"
1523 << line << ')' << endl;
1526 getTokenValue(line, "alignment", column_info[c].alignment);
1527 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1528 getTokenValue(line, "valignment", column_info[c].valignment);
1529 getTokenValue(line, "width", column_info[c].p_width);
1530 getTokenValue(line, "special", column_info[c].align_special);
1533 for (row_type i = 0; i < nrows(); ++i) {
1534 l_getline(is, line);
1535 if (!prefixIs(line, "<row")) {
1536 lyxerr << "Wrong tabular format (expected <row ...> got"
1537 << line << ')' << endl;
1540 getTokenValue(line, "topspace", row_info[i].top_space,
1541 row_info[i].top_space_default);
1542 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1543 row_info[i].bottom_space_default);
1544 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1545 row_info[i].interline_space_default);
1546 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1547 getTokenValue(line, "endhead", row_info[i].endhead);
1548 getTokenValue(line, "endfoot", row_info[i].endfoot);
1549 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1550 getTokenValue(line, "newpage", row_info[i].newpage);
1551 getTokenValue(line, "caption", row_info[i].caption);
1552 for (col_type j = 0; j < ncols(); ++j) {
1553 l_getline(is, line);
1554 if (!prefixIs(line, "<cell")) {
1555 lyxerr << "Wrong tabular format (expected <cell ...> got"
1556 << line << ')' << endl;
1559 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1560 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1561 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1562 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1563 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1564 getTokenValue(line, "topline", cell_info[i][j].top_line);
1565 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1566 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1567 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1568 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1569 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1570 getTokenValue(line, "width", cell_info[i][j].p_width);
1572 getTokenValue(line, "special", cell_info[i][j].align_special);
1573 l_getline(is, line);
1574 if (prefixIs(line, "\\begin_inset")) {
1575 cell_info[i][j].inset->setBuffer(*buffer_);
1576 cell_info[i][j].inset->read(lex);
1577 l_getline(is, line);
1579 if (!prefixIs(line, "</cell>")) {
1580 lyxerr << "Wrong tabular format (expected </cell> got"
1581 << line << ')' << endl;
1585 l_getline(is, line);
1586 if (!prefixIs(line, "</row>")) {
1587 lyxerr << "Wrong tabular format (expected </row> got"
1588 << line << ')' << endl;
1592 while (!prefixIs(line, "</lyxtabular>")) {
1593 l_getline(is, line);
1599 bool Tabular::isMultiColumn(idx_type cell) const
1601 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1602 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1606 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1608 return cell_info[cellRow(cell)][cellColumn(cell)];
1612 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1613 bool const right_border)
1615 idx_type const col = cellColumn(cell);
1616 idx_type const row = cellRow(cell);
1617 for (idx_type i = 0; i < number; ++i)
1618 unsetMultiRow(cellIndex(row, col + i));
1620 // unsetting of multirow may have invalidated cell index
1621 cell = cellIndex(row, col);
1622 CellData & cs = cellInfo(cell);
1623 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1624 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1625 cs.alignment = column_info[col].alignment;
1627 setRightLine(cell, right_border);
1629 for (idx_type i = 1; i < number; ++i) {
1630 CellData & cs1 = cellInfo(cell + i);
1631 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1632 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1640 bool Tabular::isMultiRow(idx_type cell) const
1642 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1643 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1647 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1648 bool const bottom_border)
1650 idx_type const col = cellColumn(cell);
1651 idx_type const row = cellRow(cell);
1652 for (idx_type i = 0; i < number; ++i)
1653 unsetMultiColumn(cellIndex(row + i, col));
1655 // unsetting of multirow may have invalidated cell index
1656 cell = cellIndex(row, col);
1657 CellData & cs = cellInfo(cell);
1658 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1659 cs.valignment = LYX_VALIGN_MIDDLE;
1660 // the horizontal alignment of multirow cells can only
1661 // be changed for the whole table row,
1662 // support changing this only for the multirow cell can be done via
1664 // this feature would be a fileformat change
1665 // until LyX supports this, use the deault alignment of multirow
1667 cs.alignment = LYX_ALIGN_LEFT;
1669 // set the bottom line of the last selected cell
1670 setBottomLine(cell, bottom_border);
1672 for (idx_type i = 1; i < number; ++i) {
1673 CellData & cs1 = cell_info[row + i][col];
1674 cs1.multirow = CELL_PART_OF_MULTIROW;
1675 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1683 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1685 row_type const row = cellRow(cell);
1686 col_type const col = cellColumn(cell);
1688 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1695 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1697 col_type const column = cellColumn(cell);
1698 col_type row = cellRow(cell) + 1;
1699 while (row < nrows() && isPartOfMultiRow(row, column))
1702 return row - cellRow(cell);
1706 void Tabular::unsetMultiColumn(idx_type cell)
1708 if (!isMultiColumn(cell))
1711 row_type const row = cellRow(cell);
1712 col_type const col = cellColumn(cell);
1713 row_type const span = columnSpan(cell);
1714 for (col_type c = 0; c < span; ++c) {
1715 // in the table dialog the lines are set in every case
1716 // when unsetting a multicolumn this leads to an additional right
1717 // line for every cell that was part of the former multicolumn cell,
1718 // except if the cell is in the last column
1719 // therefore remove this line
1720 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1721 && (col + c) < (col + span - 1))
1722 cell_info[row][col + c].right_line = false;
1723 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1729 void Tabular::unsetMultiRow(idx_type cell)
1731 if (!isMultiRow(cell))
1734 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1735 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1736 row_type const row = cellRow(cell);
1737 col_type const col = cellColumn(cell);
1738 row_type const span = rowSpan(cell);
1739 for (row_type r = 0; r < span; ++r)
1740 cell_info[row + r][col].multirow = CELL_NORMAL;
1745 void Tabular::setRotateCell(idx_type cell, int value)
1747 cellInfo(cell).rotate = value;
1751 int Tabular::getRotateCell(idx_type cell) const
1753 return cellInfo(cell).rotate;
1757 bool Tabular::needRotating() const
1761 for (row_type r = 0; r < nrows(); ++r)
1762 for (col_type c = 0; c < ncols(); ++c)
1763 if (cell_info[r][c].rotate != 0)
1769 bool Tabular::isLastCell(idx_type cell) const
1771 if (cell + 1 < numberofcells)
1777 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1779 if (cellRow(cell) == 0)
1782 col_type const col = cellColumn(cell);
1783 row_type r = cellRow(cell) - 1;
1784 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1787 return cell_info[r][col].cellno;
1791 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1793 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1794 if (nextrow < nrows())
1795 return cell_info[nextrow][cellColumn(cell)].cellno;
1800 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1802 LASSERT(column != npos && column < ncols()
1803 && row != npos && row < nrows(), /**/);
1804 return cell_info[row][column].cellno;
1808 void Tabular::setUsebox(idx_type cell, BoxType type)
1810 cellInfo(cell).usebox = type;
1814 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1815 // adds line breaks, see bug 4886.
1816 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1818 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1819 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1821 if (cellInfo(cell).usebox > 1)
1822 return cellInfo(cell).usebox;
1823 return useParbox(cell);
1828 // This are functions used for the longtable support
1830 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1836 row_info[row].endfirsthead = flag;
1840 row_info[row].endhead = flag;
1845 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1848 hd.set = haveLTHead();
1849 return row_info[row].endhead;
1853 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1856 hd.set = haveLTFirstHead();
1857 return row_info[row].endfirsthead;
1861 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1867 row_info[row].endlastfoot = flag;
1871 row_info[row].endfoot = flag;
1876 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1879 fd.set = haveLTFoot();
1880 return row_info[row].endfoot;
1884 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1887 fd.set = haveLTLastFoot();
1888 return row_info[row].endlastfoot;
1892 void Tabular::setLTNewPage(row_type row, bool what)
1894 row_info[row].newpage = what;
1898 bool Tabular::getLTNewPage(row_type row) const
1900 return row_info[row].newpage;
1904 bool Tabular::haveLTHead(bool withcaptions) const
1906 if (!is_long_tabular)
1908 for (row_type i = 0; i < nrows(); ++i)
1909 if (row_info[i].endhead &&
1910 (withcaptions || !row_info[i].caption))
1916 bool Tabular::haveLTFirstHead(bool withcaptions) const
1918 if (!is_long_tabular || endfirsthead.empty)
1920 for (row_type r = 0; r < nrows(); ++r)
1921 if (row_info[r].endfirsthead &&
1922 (withcaptions || !row_info[r].caption))
1928 bool Tabular::haveLTFoot(bool withcaptions) const
1930 if (!is_long_tabular)
1932 for (row_type r = 0; r < nrows(); ++r)
1933 if (row_info[r].endfoot &&
1934 (withcaptions || !row_info[r].caption))
1940 bool Tabular::haveLTLastFoot(bool withcaptions) const
1942 if (!is_long_tabular || endlastfoot.empty)
1944 for (row_type r = 0; r < nrows(); ++r)
1945 if (row_info[r].endlastfoot &&
1946 (withcaptions || !row_info[r].caption))
1952 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1954 idx_type i = getFirstCellInRow(row);
1956 setMultiColumn(i, numberOfCellsInRow(row), false);
1957 setTopLine(i, false);
1958 setBottomLine(i, false);
1959 setLeftLine(i, false);
1960 setRightLine(i, false);
1961 if (!row_info[row].endfirsthead && !row_info[row].endhead &&
1962 !row_info[row].endfoot && !row_info[row].endlastfoot) {
1963 setLTHead(row, true, endfirsthead, true);
1964 row_info[row].endfirsthead = true;
1967 unsetMultiColumn(i);
1968 // When unsetting a caption row, also all existing
1969 // captions in this row must be dissolved.
1971 row_info[row].caption = what;
1976 bool Tabular::ltCaption(row_type row) const
1978 return row_info[row].caption;
1982 bool Tabular::haveLTCaption(CaptionType captiontype) const
1984 if (!is_long_tabular)
1986 for (row_type r = 0; r < nrows(); ++r) {
1987 if (row_info[r].caption) {
1988 switch (captiontype) {
1989 case CAPTION_FIRSTHEAD:
1990 if (row_info[r].endfirsthead)
1994 if (row_info[r].endhead)
1998 if (row_info[r].endfoot)
2001 case CAPTION_LASTFOOT:
2002 if (row_info[r].endlastfoot)
2014 // end longtable support functions
2016 void Tabular::setRowAscent(row_type row, int height)
2018 if (row >= nrows() || row_info[row].ascent == height)
2020 row_info[row].ascent = height;
2024 void Tabular::setRowDescent(row_type row, int height)
2026 if (row >= nrows() || row_info[row].descent == height)
2028 row_info[row].descent = height;
2032 int Tabular::rowAscent(row_type row) const
2034 LASSERT(row < nrows(), /**/);
2035 return row_info[row].ascent;
2039 int Tabular::rowDescent(row_type row) const
2041 LASSERT(row < nrows(), /**/);
2042 return row_info[row].descent;
2046 int Tabular::height() const
2049 for (row_type row = 0; row < nrows(); ++row)
2050 height += rowAscent(row) + rowDescent(row) +
2056 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2058 LASSERT(row < nrows(), /**/);
2059 LASSERT(column < ncols(), /**/);
2060 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2064 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2066 LASSERT(row < nrows(), /**/);
2067 LASSERT(column < ncols(), /**/);
2068 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2072 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2074 // we only output complete row lines and the 1st row here, the rest
2075 // is done in Tabular::TeXBottomHLine(...)
2077 // get for each column the topline (if any)
2078 vector<bool> topline;
2080 for (col_type c = 0; c < ncols(); ++c) {
2081 topline.push_back(topLine(cellIndex(row, c)));
2082 // If cell is part of a multirow and not the first cell of the
2083 // multirow, no line must be drawn.
2085 if (isMultiRow(cellIndex(row, c))
2086 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2092 // do nothing if empty first row, or incomplete row line after
2093 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2096 // only output complete row lines and the 1st row's clines
2097 if (nset == ncols()) {
2099 os << (row == 0 ? "\\toprule " : "\\midrule ");
2103 } else if (row == 0) {
2104 for (col_type c = 0; c < ncols(); ++c) {
2106 col_type offset = 0;
2107 for (col_type j = 0 ; j < c; ++j)
2108 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2111 //babel makes the "-" character an active one, so we have to suppress this here
2112 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2113 if (lang == "slovak" || lang == "czech")
2114 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2115 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2117 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2119 col_type cstart = c;
2120 for ( ; c < ncols() && topline[c]; ++c) {}
2122 for (col_type j = cstart ; j < c ; ++j)
2123 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2126 os << c + offset << "} ";
2134 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2136 // we output bottomlines of row r and the toplines of row r+1
2137 // if the latter do not span the whole tabular
2139 // get the bottomlines of row r, and toplines in next row
2140 bool lastrow = row == nrows() - 1;
2141 vector<bool> bottomline, topline;
2142 bool nextrowset = true;
2143 for (col_type c = 0; c < ncols(); ++c) {
2144 bottomline.push_back(bottomLine(cellIndex(row, c)));
2145 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2146 // If cell is part of a multirow and not the last cell of the
2147 // multirow, no line must be drawn.
2149 if (isMultiRow(cellIndex(row, c))
2150 && isMultiRow(cellIndex(row + 1, c))
2151 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2152 bottomline[c] = false;
2155 nextrowset &= topline[c];
2158 // combine this row's bottom lines and next row's toplines if necessary
2160 for (col_type c = 0; c < ncols(); ++c) {
2162 bottomline[c] = bottomline[c] || topline[c];
2167 // do nothing if empty, OR incomplete row line with a topline in next row
2168 if (nset == 0 || (nextrowset && nset != ncols()))
2171 if (nset == ncols()) {
2173 os << (lastrow ? "\\bottomrule" : "\\midrule");
2177 for (col_type c = 0; c < ncols(); ++c) {
2178 if (bottomline[c]) {
2179 col_type offset = 0;
2180 for (col_type j = 0 ; j < c; ++j)
2181 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2184 //babel makes the "-" character an active one, so we have to suppress this here
2185 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2186 if (lang == "slovak" || lang == "czech")
2187 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2188 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2190 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2192 col_type cstart = c;
2193 for ( ; c < ncols() && bottomline[c]; ++c) {}
2195 for (col_type j = cstart ; j < c ; ++j)
2196 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2199 os << c + offset << "} ";
2207 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2208 bool & ismulticol, bool & ismultirow) const
2210 row_type const r = cellRow(cell);
2211 if (is_long_tabular && row_info[r].caption)
2214 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2215 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2216 // figure out how to set the lines
2217 // we always set double lines to the right of the cell
2218 col_type const c = cellColumn(cell);
2219 col_type const nextcol = c + columnSpan(cell);
2220 bool colright = columnRightLine(c);
2221 bool colleft = columnLeftLine(c);
2222 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2223 bool nextcellleft = nextcol < ncols()
2224 && leftLine(cellIndex(r, nextcol));
2225 bool coldouble = colright && nextcolleft;
2226 bool celldouble = rightLine(cell) && nextcellleft;
2228 ismulticol = isMultiColumn(cell)
2229 || (c == 0 && colleft != leftLine(cell))
2230 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2231 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2232 || (coldouble != celldouble);
2234 // we center in multicol when no decimal point
2235 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2236 docstring const align_d = column_info[c].decimal_point;
2237 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2241 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2242 int latexcolspan = columnSpan(cell);
2243 for(col_type col = c; col < c + columnSpan(cell); ++col)
2244 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2248 os << "\\multicolumn{" << latexcolspan << "}{";
2249 if (c ==0 && leftLine(cell))
2251 if (!cellInfo(cell).align_special.empty()) {
2252 os << cellInfo(cell).align_special;
2254 if (!getPWidth(cell).zero()) {
2256 case LYX_ALIGN_LEFT:
2257 os << ">{\\raggedright}";
2259 case LYX_ALIGN_RIGHT:
2260 os << ">{\\raggedleft}";
2262 case LYX_ALIGN_CENTER:
2263 os << ">{\\centering}";
2269 case LYX_VALIGN_TOP:
2272 case LYX_VALIGN_MIDDLE:
2275 case LYX_VALIGN_BOTTOM:
2280 << from_ascii(getPWidth(cell).asLatexString())
2284 case LYX_ALIGN_LEFT:
2287 case LYX_ALIGN_RIGHT:
2294 } // end if else !getPWidth
2295 } // end if else !cellinfo_of_cell
2296 if (rightLine(cell) || nextcellleft)
2299 // add extra vertical line if we want a double one
2302 } // end if ismulticol
2304 // we only need code for the first multirow cell
2305 ismultirow = isMultiRow(cell);
2307 os << "\\multirow{" << rowSpan(cell) << "}{";
2308 if (!getPWidth(cell).zero())
2309 os << from_ascii(getPWidth(cell).asLatexString());
2311 // we need to set a default value
2314 if (!getMROffset(cell).zero())
2315 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2317 } // end if ismultirow
2319 if (getRotateCell(cell) != 0)
2320 os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
2322 if (getUsebox(cell) == BOX_PARBOX) {
2325 case LYX_VALIGN_TOP:
2328 case LYX_VALIGN_MIDDLE:
2331 case LYX_VALIGN_BOTTOM:
2335 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2337 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2338 os << "\\begin{minipage}[";
2340 case LYX_VALIGN_TOP:
2343 case LYX_VALIGN_MIDDLE:
2346 case LYX_VALIGN_BOTTOM:
2350 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2356 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2357 bool ismulticol, bool ismultirow) const
2359 row_type const r = cellRow(cell);
2360 if (is_long_tabular && row_info[r].caption)
2364 if (getUsebox(cell) == BOX_PARBOX)
2366 else if (getUsebox(cell) == BOX_MINIPAGE)
2367 os << breakln << "\\end{minipage}";
2368 if (getRotateCell(cell) != 0)
2369 os << breakln << "\\end{turn}";
2377 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2378 OutputParams const & runparams) const
2380 if (!is_long_tabular)
2383 // output first header info
2384 if (haveLTFirstHead()) {
2385 if (endfirsthead.topDL)
2387 for (row_type r = 0; r < nrows(); ++r) {
2388 if (row_info[r].endfirsthead)
2389 TeXRow(os, r, runparams);
2391 if (endfirsthead.bottomDL)
2393 os << "\\endfirsthead\n";
2395 // output header info
2397 if (endfirsthead.empty && !haveLTFirstHead())
2398 os << "\\endfirsthead\n";
2401 for (row_type r = 0; r < nrows(); ++r) {
2402 if (row_info[r].endhead)
2403 TeXRow(os, r, runparams);
2405 if (endhead.bottomDL)
2407 os << "\\endhead\n";
2409 // output footer info
2413 for (row_type r = 0; r < nrows(); ++r) {
2414 if (row_info[r].endfoot)
2415 TeXRow(os, r, runparams);
2417 if (endfoot.bottomDL)
2419 os << "\\endfoot\n";
2420 if (endlastfoot.empty && !haveLTLastFoot())
2421 os << "\\endlastfoot\n";
2423 // output lastfooter info
2424 if (haveLTLastFoot()) {
2425 if (endlastfoot.topDL)
2427 for (row_type r = 0; r < nrows(); ++r) {
2428 if (row_info[r].endlastfoot)
2429 TeXRow(os, r, runparams);
2431 if (endlastfoot.bottomDL)
2433 os << "\\endlastfoot\n";
2438 bool Tabular::isValidRow(row_type row) const
2440 if (!is_long_tabular)
2442 return !row_info[row].endhead && !row_info[row].endfirsthead
2443 && !row_info[row].endfoot && !row_info[row].endlastfoot
2444 && !row_info[row].caption;
2448 void Tabular::TeXRow(otexstream & os, row_type row,
2449 OutputParams const & runparams) const
2451 idx_type cell = cellIndex(row, 0);
2452 shared_ptr<InsetTableCell> inset = cellInset(cell);
2453 Paragraph const & par = inset->paragraphs().front();
2454 string const lang = par.getParLanguage(buffer().params())->lang();
2456 //output the top line
2457 TeXTopHLine(os, row, lang);
2459 if (row_info[row].top_space_default) {
2461 os << "\\addlinespace\n";
2463 os << "\\noalign{\\vskip\\doublerulesep}\n";
2464 } else if(!row_info[row].top_space.zero()) {
2466 os << "\\addlinespace["
2467 << from_ascii(row_info[row].top_space.asLatexString())
2470 os << "\\noalign{\\vskip"
2471 << from_ascii(row_info[row].top_space.asLatexString())
2475 bool ismulticol = false;
2476 bool ismultirow = false;
2477 for (col_type c = 0; c < ncols(); ++c) {
2478 if (isPartOfMultiColumn(row, c))
2481 cell = cellIndex(row, c);
2483 if (isPartOfMultiRow(row, c)
2484 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2485 if (cell != getLastCellInRow(row))
2490 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2491 shared_ptr<InsetTableCell> inset = cellInset(cell);
2493 Paragraph const & par = inset->paragraphs().front();
2494 bool rtl = par.isRTL(buffer().params())
2496 && getPWidth(cell).zero()
2497 && !runparams.use_polyglossia;
2501 par.getParLanguage(buffer().params())->lang();
2502 if (lang == "farsi")
2504 else if (lang == "arabic_arabi")
2506 // currently, remaining RTL languages are
2507 // arabic_arabtex and hebrew
2511 // pass to the OutputParams that we are in a cell and
2512 // which alignment we have set.
2513 // InsetNewline needs this context information.
2514 OutputParams newrp(runparams);
2515 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2516 ? OutputParams::PLAIN
2517 : OutputParams::ALIGNED;
2519 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2520 // copy cell and split in 2
2521 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2522 head.setBuffer(buffer());
2523 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2525 dit.push_back(CursorSlice(head));
2526 head.setMacrocontextPositionRecursive(dit);
2527 bool hassep = false;
2528 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2529 head.latex(os, newrp);
2532 tail.setBuffer(head.buffer());
2534 dit.push_back(CursorSlice(tail));
2535 tail.setMacrocontextPositionRecursive(dit);
2536 tail.latex(os, newrp);
2538 } else if (!isPartOfMultiRow(row, c)) {
2539 if (!runparams.nice)
2540 os.texrow().start(par.id(), 0);
2541 inset->latex(os, newrp);
2544 runparams.encoding = newrp.encoding;
2548 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2549 if (cell != getLastCellInRow(row)) { // not last cell in row
2556 os << "\\tabularnewline";
2557 if (row_info[row].bottom_space_default) {
2559 os << "\\addlinespace";
2561 os << "[\\doublerulesep]";
2562 } else if (!row_info[row].bottom_space.zero()) {
2564 os << "\\addlinespace";
2566 << from_ascii(row_info[row].bottom_space.asLatexString())
2571 //output the bottom line
2572 TeXBottomHLine(os, row, lang);
2574 if (row_info[row].interline_space_default) {
2576 os << "\\addlinespace\n";
2578 os << "\\noalign{\\vskip\\doublerulesep}\n";
2579 } else if (!row_info[row].interline_space.zero()) {
2581 os << "\\addlinespace["
2582 << from_ascii(row_info[row].interline_space.asLatexString())
2585 os << "\\noalign{\\vskip"
2586 << from_ascii(row_info[row].interline_space.asLatexString())
2592 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2594 bool const is_tabular_star = !tabular_width.zero();
2596 //+---------------------------------------------------------------------
2597 //+ first the opening preamble +
2598 //+---------------------------------------------------------------------
2601 if (runparams.lastid != -1)
2602 os.texrow().start(runparams.lastid, runparams.lastpos);
2605 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2607 if (is_long_tabular) {
2608 os << "\\begin{longtable}";
2609 switch (longtabular_alignment) {
2610 case LYX_LONGTABULAR_ALIGN_LEFT:
2613 case LYX_LONGTABULAR_ALIGN_CENTER:
2615 case LYX_LONGTABULAR_ALIGN_RIGHT:
2620 if (is_tabular_star)
2621 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2623 os << "\\begin{tabular}";
2624 switch (tabular_valignment) {
2625 case LYX_VALIGN_TOP:
2628 case LYX_VALIGN_MIDDLE:
2630 case LYX_VALIGN_BOTTOM:
2638 if (is_tabular_star)
2639 os << "@{\\extracolsep{\\fill}}";
2641 for (col_type c = 0; c < ncols(); ++c) {
2642 if (columnLeftLine(c))
2644 if (!column_info[c].align_special.empty()) {
2645 os << column_info[c].align_special;
2647 if (!column_info[c].p_width.zero()) {
2648 switch (column_info[c].alignment) {
2649 case LYX_ALIGN_LEFT:
2650 os << ">{\\raggedright}";
2652 case LYX_ALIGN_RIGHT:
2653 os << ">{\\raggedleft}";
2655 case LYX_ALIGN_CENTER:
2656 os << ">{\\centering}";
2658 case LYX_ALIGN_NONE:
2659 case LYX_ALIGN_BLOCK:
2660 case LYX_ALIGN_LAYOUT:
2661 case LYX_ALIGN_SPECIAL:
2662 case LYX_ALIGN_DECIMAL:
2666 switch (column_info[c].valignment) {
2667 case LYX_VALIGN_TOP:
2670 case LYX_VALIGN_MIDDLE:
2673 case LYX_VALIGN_BOTTOM:
2678 << from_ascii(column_info[c].p_width.asLatexString())
2681 switch (column_info[c].alignment) {
2682 case LYX_ALIGN_LEFT:
2685 case LYX_ALIGN_RIGHT:
2688 case LYX_ALIGN_DECIMAL:
2689 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2695 } // end if else !column_info[i].p_width
2696 } // end if else !column_info[i].align_special
2697 if (columnRightLine(c))
2702 TeXLongtableHeaderFooter(os, runparams);
2704 //+---------------------------------------------------------------------
2705 //+ the single row and columns (cells) +
2706 //+---------------------------------------------------------------------
2708 for (row_type r = 0; r < nrows(); ++r) {
2709 if (isValidRow(r)) {
2710 TeXRow(os, r, runparams);
2711 if (is_long_tabular && row_info[r].newpage)
2712 os << "\\newpage\n";
2716 //+---------------------------------------------------------------------
2717 //+ the closing of the tabular +
2718 //+---------------------------------------------------------------------
2720 if (is_long_tabular)
2721 os << "\\end{longtable}";
2723 if (is_tabular_star)
2724 os << "\\end{tabular*}";
2726 os << "\\end{tabular}";
2730 os << breakln << "\\end{turn}";
2734 int Tabular::docbookRow(odocstream & os, row_type row,
2735 OutputParams const & runparams) const
2738 idx_type cell = getFirstCellInRow(row);
2741 for (col_type c = 0; c < ncols(); ++c) {
2742 if (isPartOfMultiColumn(row, c))
2745 os << "<entry align=\"";
2746 switch (getAlignment(cell)) {
2747 case LYX_ALIGN_LEFT:
2750 case LYX_ALIGN_RIGHT:
2758 os << "\" valign=\"";
2759 switch (getVAlignment(cell)) {
2760 case LYX_VALIGN_TOP:
2763 case LYX_VALIGN_BOTTOM:
2766 case LYX_VALIGN_MIDDLE:
2771 if (isMultiColumn(cell)) {
2772 os << " namest=\"col" << c << "\" ";
2773 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2777 ret += cellInset(cell)->docbook(os, runparams);
2786 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2790 //+---------------------------------------------------------------------
2791 //+ first the opening preamble +
2792 //+---------------------------------------------------------------------
2794 os << "<tgroup cols=\"" << ncols()
2795 << "\" colsep=\"1\" rowsep=\"1\">\n";
2797 for (col_type c = 0; c < ncols(); ++c) {
2798 os << "<colspec colname=\"col" << c << "\" align=\"";
2799 switch (column_info[c].alignment) {
2800 case LYX_ALIGN_LEFT:
2803 case LYX_ALIGN_RIGHT:
2811 if (runparams.flavor == OutputParams::XML)
2817 //+---------------------------------------------------------------------
2818 //+ Long Tabular case +
2819 //+---------------------------------------------------------------------
2821 // output caption info
2822 // The caption flag wins over head/foot
2823 if (haveLTCaption()) {
2824 os << "<caption>\n";
2826 for (row_type r = 0; r < nrows(); ++r) {
2827 if (row_info[r].caption) {
2828 ret += docbookRow(os, r, runparams);
2831 os << "</caption>\n";
2834 // output header info
2835 if (haveLTHead(false) || haveLTFirstHead(false)) {
2838 for (row_type r = 0; r < nrows(); ++r) {
2839 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2840 !row_info[r].caption) {
2841 ret += docbookRow(os, r, runparams);
2847 // output footer info
2848 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2851 for (row_type r = 0; r < nrows(); ++r) {
2852 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2853 !row_info[r].caption) {
2854 ret += docbookRow(os, r, runparams);
2861 //+---------------------------------------------------------------------
2862 //+ the single row and columns (cells) +
2863 //+---------------------------------------------------------------------
2867 for (row_type r = 0; r < nrows(); ++r) {
2868 if (isValidRow(r)) {
2869 ret += docbookRow(os, r, runparams);
2874 //+---------------------------------------------------------------------
2875 //+ the closing of the tabular +
2876 //+---------------------------------------------------------------------
2885 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2886 OutputParams const & runparams, bool header) const
2889 string const celltag = header ? "th" : "td";
2890 idx_type cell = getFirstCellInRow(row);
2892 xs << html::StartTag("tr");
2893 for (col_type c = 0; c < ncols(); ++c) {
2894 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2899 switch (getAlignment(cell)) {
2900 case LYX_ALIGN_LEFT:
2903 case LYX_ALIGN_RIGHT:
2911 attr << " valign='";
2912 switch (getVAlignment(cell)) {
2913 case LYX_VALIGN_TOP:
2916 case LYX_VALIGN_BOTTOM:
2919 case LYX_VALIGN_MIDDLE:
2924 if (isMultiColumn(cell))
2925 attr << " colspan='" << columnSpan(cell) << "'";
2926 else if (isMultiRow(cell))
2927 attr << " rowspan='" << rowSpan(cell) << "'";
2929 xs << html::StartTag(celltag, attr.str()) << html::CR();
2930 ret += cellInset(cell)->xhtml(xs, runparams);
2931 xs << html::EndTag(celltag) << html::CR();
2934 xs << html::EndTag("tr");
2939 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2943 if (is_long_tabular) {
2944 // we'll wrap it in a div, so as to deal with alignment
2946 switch (longtabular_alignment) {
2947 case LYX_LONGTABULAR_ALIGN_LEFT:
2950 case LYX_LONGTABULAR_ALIGN_CENTER:
2953 case LYX_LONGTABULAR_ALIGN_RIGHT:
2957 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2959 // The caption flag wins over head/foot
2960 if (haveLTCaption()) {
2961 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2963 for (row_type r = 0; r < nrows(); ++r)
2964 if (row_info[r].caption)
2965 ret += xhtmlRow(xs, r, runparams);
2966 xs << html::EndTag("div") << html::CR();
2970 xs << html::StartTag("table") << html::CR();
2972 // output header info
2973 bool const havefirsthead = haveLTFirstHead(false);
2974 // if we have a first head, then we are going to ignore the
2975 // headers for the additional pages, since there aren't any
2976 // in XHTML. this test accomplishes that.
2977 bool const havehead = !havefirsthead && haveLTHead(false);
2978 if (havehead || havefirsthead) {
2979 xs << html::StartTag("thead") << html::CR();
2980 for (row_type r = 0; r < nrows(); ++r) {
2981 if (((havefirsthead && row_info[r].endfirsthead) ||
2982 (havehead && row_info[r].endhead)) &&
2983 !row_info[r].caption) {
2984 ret += xhtmlRow(xs, r, runparams, true);
2987 xs << html::EndTag("thead") << html::CR();
2989 // output footer info
2990 bool const havelastfoot = haveLTLastFoot(false);
2992 bool const havefoot = !havelastfoot && haveLTFoot(false);
2993 if (havefoot || havelastfoot) {
2994 xs << html::StartTag("tfoot") << html::CR();
2995 for (row_type r = 0; r < nrows(); ++r) {
2996 if (((havelastfoot && row_info[r].endlastfoot) ||
2997 (havefoot && row_info[r].endfoot)) &&
2998 !row_info[r].caption) {
2999 ret += xhtmlRow(xs, r, runparams);
3002 xs << html::EndTag("tfoot") << html::CR();
3005 xs << html::StartTag("tbody") << html::CR();
3006 for (row_type r = 0; r < nrows(); ++r) {
3007 if (isValidRow(r)) {
3008 ret += xhtmlRow(xs, r, runparams);
3011 xs << html::EndTag("tbody")
3013 << html::EndTag("table")
3015 if (is_long_tabular)
3016 xs << html::EndTag("div") << html::CR();
3021 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3022 vector<unsigned int> const & clen) const
3024 idx_type const fcell = getFirstCellInRow(row);
3025 idx_type const n = numberOfCellsInRow(row) + fcell;
3028 for (idx_type i = fcell; i < n; ++i) {
3038 for (idx_type i = fcell; i < n; ++i) {
3049 col_type column = cellColumn(i);
3050 int len = clen[column];
3051 while (column < ncols() - 1
3052 && isPartOfMultiColumn(row, ++column))
3053 len += clen[column] + 4;
3054 os << docstring(len, ch);
3069 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3070 vector<unsigned int> const & clen) const
3072 idx_type const fcell = getFirstCellInRow(row);
3073 idx_type const n = numberOfCellsInRow(row) + fcell;
3076 for (idx_type i = fcell; i < n; ++i) {
3077 if (bottomLine(i)) {
3086 for (idx_type i = fcell; i < n; ++i) {
3087 if (bottomLine(i)) {
3097 col_type column = cellColumn(i);
3098 int len = clen[column];
3099 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3100 len += clen[column] + 4;
3101 os << docstring(len, ch);
3102 if (bottomLine(i)) {
3116 void Tabular::plaintextPrintCell(odocstream & os,
3117 OutputParams const & runparams,
3118 idx_type cell, row_type row, col_type column,
3119 vector<unsigned int> const & clen,
3120 bool onlydata) const
3122 odocstringstream sstr;
3123 cellInset(cell)->plaintext(sstr, runparams);
3135 unsigned int len1 = sstr.str().length();
3136 unsigned int len2 = clen[column];
3137 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3138 len2 += clen[column] + 4;
3141 switch (getAlignment(cell)) {
3143 case LYX_ALIGN_LEFT:
3146 case LYX_ALIGN_RIGHT:
3150 case LYX_ALIGN_CENTER:
3156 os << docstring(len1, ' ') << sstr.str()
3157 << docstring(len2, ' ');
3159 if (rightLine(cell))
3166 void Tabular::plaintext(odocstream & os,
3167 OutputParams const & runparams, int const depth,
3168 bool onlydata, char_type delim) const
3170 // first calculate the width of the single columns
3171 vector<unsigned int> clen(ncols());
3174 // first all non multicolumn cells!
3175 for (col_type c = 0; c < ncols(); ++c) {
3177 for (row_type r = 0; r < nrows(); ++r) {
3178 idx_type cell = cellIndex(r, c);
3179 if (isMultiColumn(cell))
3181 odocstringstream sstr;
3182 cellInset(cell)->plaintext(sstr, runparams);
3183 if (clen[c] < sstr.str().length())
3184 clen[c] = sstr.str().length();
3187 // then all multicolumn cells!
3188 for (col_type c = 0; c < ncols(); ++c) {
3189 for (row_type r = 0; r < nrows(); ++r) {
3190 idx_type cell = cellIndex(r, c);
3191 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3193 odocstringstream sstr;
3194 cellInset(cell)->plaintext(sstr, runparams);
3195 int len = int(sstr.str().length());
3196 idx_type const n = columnSpan(cell);
3197 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3199 if (len > int(clen[c + n - 1]))
3200 clen[c + n - 1] = len;
3205 for (row_type r = 0; r < nrows(); ++r) {
3206 if (!onlydata && plaintextTopHLine(os, r, clen))
3207 os << docstring(depth * 2, ' ');
3208 for (col_type c = 0; c < ncols(); ++c) {
3209 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3211 if (onlydata && c > 0)
3212 // we don't use operator<< for single UCS4 character.
3213 // see explanation in docstream.h
3215 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3220 os << docstring(depth * 2, ' ');
3221 if (plaintextBottomHLine(os, r, clen))
3222 os << docstring(depth * 2, ' ');
3228 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3230 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3234 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3235 col_type column) const
3237 return cell_info[row][column].inset;
3241 void Tabular::setCellInset(row_type row, col_type column,
3242 shared_ptr<InsetTableCell> ins) const
3244 CellData & cd = cell_info[row][column];
3249 void Tabular::validate(LaTeXFeatures & features) const
3251 features.require("NeedTabularnewline");
3253 features.require("booktabs");
3254 if (is_long_tabular)
3255 features.require("longtable");
3257 features.require("rotating");
3258 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3259 if (isMultiRow(cell))
3260 features.require("multirow");
3261 if (getVAlignment(cell) != LYX_VALIGN_TOP
3262 || !getPWidth(cell).zero())
3263 features.require("array");
3264 cellInset(cell)->validate(features);
3269 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3271 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3272 ParagraphList::const_iterator cit = parlist.begin();
3273 ParagraphList::const_iterator end = parlist.end();
3275 for (; cit != end; ++cit)
3276 for (int i = 0; i < cit->size(); ++i)
3277 if (cit->isNewline(i))
3284 /////////////////////////////////////////////////////////////////////
3288 /////////////////////////////////////////////////////////////////////
3290 InsetTableCell::InsetTableCell(Buffer * buf)
3291 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3292 contentAlign(LYX_ALIGN_CENTER)
3296 bool InsetTableCell::forcePlainLayout(idx_type) const
3298 return !isFixedWidth;
3302 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3304 return isFixedWidth;
3308 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3309 FuncStatus & status) const
3311 bool enabled = true;
3312 switch (cmd.action()) {
3314 enabled = !forcePlainLayout();
3316 case LFUN_LAYOUT_PARAGRAPH:
3317 enabled = allowParagraphCustomization();
3320 case LFUN_MATH_DISPLAY:
3321 if (!hasFixedWidth()) {
3326 return InsetText::getStatus(cur, cmd, status);
3328 status.setEnabled(enabled);
3332 docstring InsetTableCell::asString(bool intoInsets)
3335 if (paragraphs().empty())
3337 ParagraphList::const_iterator it = paragraphs().begin();
3338 ParagraphList::const_iterator en = paragraphs().end();
3340 for (; it != en; ++it) {
3345 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3351 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3354 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3355 return InsetText::xhtml(xs, rp);
3360 /////////////////////////////////////////////////////////////////////
3364 /////////////////////////////////////////////////////////////////////
3366 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3368 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3369 rowselect_(false), colselect_(false)
3374 InsetTabular::InsetTabular(InsetTabular const & tab)
3375 : Inset(tab), tabular(tab.tabular), scx_(0)
3380 InsetTabular::~InsetTabular()
3382 hideDialogs("tabular", this);
3386 void InsetTabular::setBuffer(Buffer & buf)
3388 tabular.setBuffer(buf);
3389 Inset::setBuffer(buf);
3393 bool InsetTabular::insetAllowed(InsetCode code) const
3398 case MATHMACRO_CODE:
3403 return tabular.is_long_tabular;
3411 void InsetTabular::write(ostream & os) const
3413 os << "Tabular" << endl;
3418 string InsetTabular::contextMenu(BufferView const &, int, int) const
3420 // FIXME: depending on the selection state,
3421 // we could offer a different menu.
3422 return cell(0)->contextMenuName() + ";" + contextMenuName();
3426 string InsetTabular::contextMenuName() const
3428 return "context-tabular";
3432 void InsetTabular::read(Lexer & lex)
3434 //bool const old_format = (lex.getString() == "\\LyXTable");
3442 string token = lex.getString();
3443 while (lex && token != "\\end_inset") {
3445 token = lex.getString();
3448 lex.printError("Missing \\end_inset at this point. ");
3452 int InsetTabular::rowFromY(Cursor & cur, int y) const
3454 // top y coordinate of tabular
3455 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3457 for (; r < tabular.nrows() && y > h; ++r)
3458 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3459 + tabular.interRowSpace(r);
3465 int InsetTabular::columnFromX(Cursor & cur, int x) const
3467 // left x coordinate of tabular
3468 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3470 for (; c < tabular.ncols() && x > w; ++c)
3471 w += tabular.cellWidth(c);
3476 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3478 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3479 // mi.base.textwidth << "\n";
3482 LASSERT(false, /**/);
3485 for (row_type r = 0; r < tabular.nrows(); ++r) {
3488 for (col_type c = 0; c < tabular.ncols(); ++c) {
3489 if (tabular.isPartOfMultiColumn(r, c)
3490 || tabular.isPartOfMultiRow(r, c))
3491 // multicolumn or multirow cell, but not first one
3493 idx_type const cell = tabular.cellIndex(r, c);
3496 Length const p_width = tabular.getPWidth(cell);
3497 if (!p_width.zero())
3498 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3499 tabular.cellInset(cell)->metrics(m, dim);
3500 if (!p_width.zero())
3501 dim.wid = m.base.textwidth;
3502 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3503 + tabular.interColumnSpace(cell);
3505 // FIXME(?): do we need a second metrics call?
3506 TextMetrics const & tm =
3507 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3509 // determine horizontal offset because of decimal align (if necessary)
3510 int decimal_width = 0;
3511 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3512 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3513 tail.setBuffer(tabular.buffer());
3514 // we need to set macrocontext position everywhere
3515 // otherwise we crash with nested insets (e.g. footnotes)
3516 // after decimal point
3517 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3519 dit.push_back(CursorSlice(tail));
3520 tail.setMacrocontextPositionRecursive(dit);
3522 // remove text leading decimal point
3523 docstring const align_d = tabular.column_info[c].decimal_point;
3524 dit = separatorPos(&tail, align_d);
3526 pit_type const psize = tail.paragraphs().front().size();
3528 tail.paragraphs().front().eraseChars(0,
3529 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3531 tail.metrics(m, dim1);
3532 decimal_width = dim1.width();
3535 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3536 tabular.cell_info[r][c].decimal_width = decimal_width;
3538 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3539 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3540 int const lastpardes = tm.last().second->descent()
3541 + TEXT_TO_INSET_OFFSET;
3543 switch (tabular.getVAlignment(cell)) {
3544 case Tabular::LYX_VALIGN_TOP:
3546 case Tabular::LYX_VALIGN_MIDDLE:
3547 offset = -(dim.des - lastpardes)/2;
3549 case Tabular::LYX_VALIGN_BOTTOM:
3550 offset = -(dim.des - lastpardes);
3553 tabular.cell_info[r][c].voffset = offset;
3554 maxasc = max(maxasc, dim.asc - offset);
3555 maxdes = max(maxdes, dim.des + offset);
3557 int const top_space = tabular.row_info[r].top_space_default ?
3558 default_line_space :
3559 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3560 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3561 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3562 default_line_space :
3563 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3564 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3567 // for top-alignment the first horizontal table line must be exactly at
3568 // the position of the base line of the surrounding text line
3569 // for bottom alignment, the same is for the last table line
3570 switch (tabular.tabular_valignment) {
3571 case Tabular::LYX_VALIGN_BOTTOM:
3572 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3574 case Tabular::LYX_VALIGN_MIDDLE:
3575 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3577 case Tabular::LYX_VALIGN_TOP:
3578 offset_valign_ = tabular.rowAscent(0);
3582 tabular.updateColumnWidths();
3583 dim.asc = tabular.rowAscent(0) - offset_valign_;
3584 dim.des = tabular.height() - dim.asc;
3585 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3589 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3592 if (&cur.inset() == this && cur.selection()) {
3593 if (cur.selIsMultiCell()) {
3596 getSelection(cur, rs, re, cs, ce);
3598 idx_type const cell = tabular.cellIndex(row, col);
3599 col_type const cspan = tabular.columnSpan(cell);
3600 row_type const rspan = tabular.rowSpan(cell);
3601 if (col + cspan - 1 >= cs && col <= ce
3602 && row + rspan - 1 >= rs && row <= re)
3605 if (col == tabular.cellColumn(cur.idx())
3606 && row == tabular.cellRow(cur.idx())) {
3607 CursorSlice const & beg = cur.selBegin();
3608 CursorSlice const & end = cur.selEnd();
3610 if ((end.lastpos() > 0 || end.lastpit() > 0)
3611 && end.pos() == end.lastpos() && beg.pos() == 0
3612 && end.pit() == end.lastpit() && beg.pit() == 0)
3620 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3622 x += scx_ + ADD_TO_TABULAR_WIDTH;
3624 BufferView * bv = pi.base.bv;
3625 Cursor & cur = pi.base.bv->cursor();
3628 // FIXME: As the full background is painted in drawBackground(),
3629 // we have no choice but to do a full repaint for the Text cells.
3630 pi.full_repaint = true;
3632 bool const original_selection_state = pi.selected;
3635 first_visible_cell = Tabular::npos;
3637 int yy = y + offset_valign_;
3638 for (row_type r = 0; r < tabular.nrows(); ++r) {
3640 for (col_type c = 0; c < tabular.ncols(); ++c) {
3641 if (tabular.isPartOfMultiColumn(r, c))
3644 idx = tabular.cellIndex(r, c);
3646 if (tabular.isPartOfMultiRow(r, c)) {
3647 nx += tabular.cellWidth(idx);
3651 if (first_visible_cell == Tabular::npos)
3652 first_visible_cell = idx;
3654 pi.selected |= isCellSelected(cur, r, c);
3655 int const cx = nx + tabular.textHOffset(idx);
3656 int const cy = yy + tabular.textVOffset(idx);
3657 // Cache the Inset position.
3658 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3659 cell(idx)->draw(pi, cx, cy);
3660 drawCellLines(pi, nx, yy, r, idx);
3661 nx += tabular.cellWidth(idx);
3662 pi.selected = original_selection_state;
3665 if (r + 1 < tabular.nrows())
3666 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3667 + tabular.interRowSpace(r + 1);
3672 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3674 x += scx_ + ADD_TO_TABULAR_WIDTH;
3675 y += offset_valign_ - tabular.rowAscent(0);
3676 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3677 pi.backgroundColor(this));
3681 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3683 Cursor & cur = pi.base.bv->cursor();
3686 x += scx_ + ADD_TO_TABULAR_WIDTH;
3688 if (!cur.selection())
3690 if (&cur.inset() != this)
3695 bool const full_cell_selected = isCellSelected(cur,
3696 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3698 if (cur.selIsMultiCell() || full_cell_selected) {
3699 for (row_type r = 0; r < tabular.nrows(); ++r) {
3701 for (col_type c = 0; c < tabular.ncols(); ++c) {
3702 if (tabular.isPartOfMultiColumn(r, c))
3705 idx_type const cell = tabular.cellIndex(r, c);
3707 if (tabular.isPartOfMultiRow(r, c)) {
3708 xx += tabular.cellWidth(cell);
3711 int const w = tabular.cellWidth(cell);
3712 int const h = tabular.cellHeight(cell);
3713 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3714 if (isCellSelected(cur, r, c))
3715 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3718 if (r + 1 < tabular.nrows())
3719 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3720 + tabular.interRowSpace(r + 1);
3724 // FIXME: This code has no effect because InsetTableCell does not handle
3725 // drawSelection other than the trivial implementation in Inset.
3727 // x += cellXPos(cur.idx());
3728 // x += tabular.textHOffset(cur.idx());
3729 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3734 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3735 row_type row, idx_type cell) const
3737 y -= tabular.rowAscent(row);
3738 int const w = tabular.cellWidth(cell);
3739 int const h = tabular.cellHeight(cell);
3740 Color const linecolor = pi.textColor(Color_tabularline);
3741 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3744 bool drawline = tabular.topLine(cell)
3745 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3746 pi.pain.line(x, y, x + w, y,
3747 drawline ? linecolor : gridcolor,
3748 drawline ? Painter::line_solid : Painter::line_onoffdash);
3751 drawline = tabular.bottomLine(cell);
3752 pi.pain.line(x, y + h, x + w, y + h,
3753 drawline ? linecolor : gridcolor,
3754 drawline ? Painter::line_solid : Painter::line_onoffdash);
3757 col_type const col = tabular.cellColumn(cell);
3758 drawline = tabular.leftLine(cell)
3759 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3760 pi.pain.line(x, y, x, y + h,
3761 drawline ? linecolor : gridcolor,
3762 drawline ? Painter::line_solid : Painter::line_onoffdash);
3765 x -= tabular.interColumnSpace(cell);
3766 drawline = tabular.rightLine(cell)
3767 || (col + 1 < tabular.ncols()
3768 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3769 pi.pain.line(x + w, y, x + w, y + h,
3770 drawline ? linecolor : gridcolor,
3771 drawline ? Painter::line_solid : Painter::line_onoffdash);
3775 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3777 //lyxerr << "InsetTabular::edit: " << this << endl;
3779 cur.setSelection(false);
3782 if (isRightToLeft(cur))
3783 cur.idx() = tabular.getLastCellInRow(0);
3789 if (isRightToLeft(cur))
3790 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3792 cur.idx() = tabular.numberofcells - 1;
3794 cur.pos() = cur.lastpos(); // FIXME crude guess
3796 cur.setCurrentFont();
3797 // FIXME: this accesses the position cache before it is initialized
3799 //cur.bv().fitCursor();
3803 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3805 // In a longtable, tell captions what the current float is
3806 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3807 string const saveflt = cnts.current_float();
3808 if (tabular.is_long_tabular)
3809 cnts.current_float("table");
3811 ParIterator it2 = it;
3813 size_t const end = it2.nargs();
3814 for ( ; it2.idx() < end; it2.top().forwardIdx())
3815 buffer().updateBuffer(it2, utype);
3818 if (tabular.is_long_tabular)
3819 cnts.current_float(saveflt);
3823 void InsetTabular::addToToc(DocIterator const & cpit) const
3825 DocIterator dit = cpit;
3827 size_t const end = dit.nargs();
3828 for ( ; dit.idx() < end; dit.top().forwardIdx())
3829 cell(dit.idx())->addToToc(dit);
3833 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3835 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3836 << "\n cur:" << cur);
3837 CursorSlice sl = cur.top();
3838 Cursor & bvcur = cur.bv().cursor();
3840 FuncCode const act = cmd.action();
3844 case LFUN_MOUSE_PRESS: {
3845 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3847 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3848 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3849 row_type r = rowFromY(cur, cmd.y());
3850 cur.idx() = tabular.getFirstCellInRow(r);
3853 cur.idx() = tabular.getLastCellInRow(r);
3854 cur.pos() = cur.lastpos();
3855 cur.setSelection(true);
3861 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3862 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3863 || cmd.y() > y0 + tabular.height()) {
3864 col_type c = columnFromX(cur, cmd.x());
3865 cur.idx() = tabular.cellIndex(0, c);
3868 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3869 cur.pos() = cur.lastpos();
3870 cur.setSelection(true);
3875 // do not reset cursor/selection if we have selected
3876 // some cells (bug 2715).
3877 if (cmd.button() == mouse_button::button3
3878 && &bvcur.selBegin().inset() == this
3879 && bvcur.selIsMultiCell())
3882 // Let InsetTableCell do it
3883 cell(cur.idx())->dispatch(cur, cmd);
3886 case LFUN_MOUSE_MOTION:
3887 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3888 if (cmd.button() == mouse_button::button1) {
3889 // only accept motions to places not deeper nested than the real anchor
3890 if (!bvcur.realAnchor().hasPart(cur)) {
3894 // select (additional) row
3896 row_type r = rowFromY(cur, cmd.y());
3897 cur.idx() = tabular.getLastCellInRow(r);
3898 // we need to reset the cursor's pit and pos now, as the old ones
3899 // may no longer be valid.
3902 bvcur.setCursor(cur);
3903 bvcur.setSelection(true);
3906 // select (additional) column
3908 col_type c = columnFromX(cur, cmd.x());
3909 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3910 // we need to reset the cursor's pit and pos now, as the old ones
3911 // may no longer be valid.
3914 bvcur.setCursor(cur);
3915 bvcur.setSelection(true);
3918 // only update if selection changes
3919 if (bvcur.idx() == cur.idx() &&
3920 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3921 cur.noScreenUpdate();
3922 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3923 bvcur.setCursor(cur);
3924 bvcur.setSelection(true);
3925 // if this is a multicell selection, we just set the cursor to
3926 // the beginning of the cell's text.
3927 if (bvcur.selIsMultiCell()) {
3928 bvcur.pit() = bvcur.lastpit();
3929 bvcur.pos() = bvcur.lastpos();
3934 case LFUN_MOUSE_RELEASE:
3939 case LFUN_CELL_BACKWARD:
3941 cur.setSelection(false);
3944 case LFUN_CELL_FORWARD:
3946 cur.setSelection(false);
3949 case LFUN_CHAR_FORWARD_SELECT:
3950 case LFUN_CHAR_FORWARD:
3951 case LFUN_CHAR_BACKWARD_SELECT:
3952 case LFUN_CHAR_BACKWARD:
3953 case LFUN_CHAR_RIGHT_SELECT:
3954 case LFUN_CHAR_RIGHT:
3955 case LFUN_CHAR_LEFT_SELECT:
3956 case LFUN_CHAR_LEFT:
3957 case LFUN_WORD_FORWARD:
3958 case LFUN_WORD_FORWARD_SELECT:
3959 case LFUN_WORD_BACKWARD:
3960 case LFUN_WORD_BACKWARD_SELECT:
3961 case LFUN_WORD_RIGHT:
3962 case LFUN_WORD_RIGHT_SELECT:
3963 case LFUN_WORD_LEFT:
3964 case LFUN_WORD_LEFT_SELECT: {
3965 // determine whether we move to next or previous cell, where to enter
3966 // the new cell from, and which command to "finish" (i.e., exit the
3969 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3970 FuncCode finish_lfun;
3972 if (act == LFUN_CHAR_FORWARD
3973 || act == LFUN_CHAR_FORWARD_SELECT
3974 || act == LFUN_WORD_FORWARD
3975 || act == LFUN_WORD_FORWARD_SELECT) {
3977 finish_lfun = LFUN_FINISHED_FORWARD;
3979 else if (act == LFUN_CHAR_BACKWARD
3980 || act == LFUN_CHAR_BACKWARD_SELECT
3981 || act == LFUN_WORD_BACKWARD
3982 || act == LFUN_WORD_BACKWARD_SELECT) {
3984 finish_lfun = LFUN_FINISHED_BACKWARD;
3986 // LEFT or RIGHT commands --- the interpretation will depend on the
3987 // table's direction.
3989 bool const right = act == LFUN_CHAR_RIGHT
3990 || act == LFUN_CHAR_RIGHT_SELECT
3991 || act == LFUN_WORD_RIGHT
3992 || act == LFUN_WORD_RIGHT_SELECT;
3993 next_cell = isRightToLeft(cur) != right;
3995 if (lyxrc.visual_cursor)
3996 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3998 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4001 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4002 || act == LFUN_CHAR_BACKWARD_SELECT
4003 || act == LFUN_CHAR_RIGHT_SELECT
4004 || act == LFUN_CHAR_LEFT_SELECT
4005 || act == LFUN_WORD_FORWARD_SELECT
4006 || act == LFUN_WORD_RIGHT_SELECT
4007 || act == LFUN_WORD_BACKWARD_SELECT
4008 || act == LFUN_WORD_LEFT_SELECT;
4010 // If we have a multicell selection or we're
4011 // not doing some LFUN_*_SELECT thing anyway...
4012 if (!cur.selIsMultiCell() || !select) {
4013 col_type const c = tabular.cellColumn(cur.idx());
4014 row_type const r = tabular.cellRow(cur.idx());
4015 // Are we trying to select the whole cell and is the whole cell
4016 // not yet selected?
4017 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4018 ((next_cell && cur.pit() == cur.lastpit()
4019 && cur.pos() == cur.lastpos())
4020 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4022 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4024 // ...try to dispatch to the cell's inset.
4025 cell(cur.idx())->dispatch(cur, cmd);
4027 // When we already have a selection we want to select the whole cell
4028 // before going to the next cell.
4029 if (select_whole && !empty_cell){
4030 getText(cur.idx())->selectAll(cur);
4035 // FIXME: When we support the selection of an empty cell, remove
4036 // the !empty_cell from this condition. For now we jump to the next
4037 // cell if the current cell is empty.
4038 if (cur.result().dispatched() && !empty_cell)
4042 // move to next/prev cell, as appropriate
4043 // note that we will always do this if we're selecting and we have
4044 // a multicell selection
4045 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4046 << " cell from: " << int(entry_from));
4048 moveNextCell(cur, entry_from);
4050 movePrevCell(cur, entry_from);
4051 // if we're exiting the table, call the appropriate FINISHED lfun
4052 if (sl == cur.top())
4053 cmd = FuncRequest(finish_lfun);
4057 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4062 case LFUN_DOWN_SELECT:
4064 if (!(cur.selection() && cur.selIsMultiCell()))
4065 cell(cur.idx())->dispatch(cur, cmd);
4067 cur.dispatched(); // override the cell's decision
4068 if (sl == cur.top()) {
4069 // if our Text didn't do anything to the cursor
4070 // then we try to put the cursor into the cell below
4071 // setting also the right targetX.
4072 cur.selHandle(act == LFUN_DOWN_SELECT);
4073 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4074 int const xtarget = cur.targetX();
4075 // WARNING: Once cur.idx() has been reset, the cursor is in
4076 // an inconsistent state until pos() has been set. Be careful
4077 // what you do with it!
4078 cur.idx() = tabular.cellBelow(cur.idx());
4080 TextMetrics const & tm =
4081 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4082 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4083 cur.setCurrentFont();
4086 if (sl == cur.top()) {
4087 // we trick it to go to forward after leaving the
4089 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4092 if (cur.selIsMultiCell()) {
4093 cur.pit() = cur.lastpit();
4094 cur.pos() = cur.lastpos();
4095 cur.setCurrentFont();
4098 cur.screenUpdateFlags(Update::FitCursor);
4101 case LFUN_UP_SELECT:
4103 if (!(cur.selection() && cur.selIsMultiCell()))
4104 cell(cur.idx())->dispatch(cur, cmd);
4105 cur.dispatched(); // override the cell's decision
4106 if (sl == cur.top()) {
4107 // if our Text didn't do anything to the cursor
4108 // then we try to put the cursor into the cell above
4109 // setting also the right targetX.
4110 cur.selHandle(act == LFUN_UP_SELECT);
4111 if (tabular.cellRow(cur.idx()) != 0) {
4112 int const xtarget = cur.targetX();
4113 // WARNING: Once cur.idx() has been reset, the cursor is in
4114 // an inconsistent state until pos() has been set. Be careful
4115 // what you do with it!
4116 cur.idx() = tabular.cellAbove(cur.idx());
4117 cur.pit() = cur.lastpit();
4118 Text const * text = cell(cur.idx())->getText(0);
4119 TextMetrics const & tm = cur.bv().textMetrics(text);
4120 ParagraphMetrics const & pm =
4121 tm.parMetrics(cur.lastpit());
4122 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4123 cur.setCurrentFont();
4126 if (sl == cur.top()) {
4127 cmd = FuncRequest(LFUN_UP);
4130 if (cur.selIsMultiCell()) {
4132 cur.pos() = cur.lastpos();
4133 cur.setCurrentFont();
4136 cur.screenUpdateFlags(Update::FitCursor);
4139 // case LFUN_SCREEN_DOWN: {
4140 // //if (hasSelection())
4141 // // cur.selection() = false;
4142 // col_type const col = tabular.cellColumn(cur.idx());
4143 // int const t = cur.bv().top_y() + cur.bv().height();
4144 // if (t < yo() + tabular.getHeightOfTabular()) {
4145 // cur.bv().scrollDocView(t, true);
4146 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4148 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4155 // case LFUN_SCREEN_UP: {
4156 // //if (hasSelection())
4157 // // cur.selection() = false;
4158 // col_type const col = tabular.cellColumn(cur.idx());
4159 // int const t = cur.bv().top_y() + cur.bv().height();
4161 // cur.bv().scrollDocView(t, true);
4165 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4169 // cur.par() = cur.lastpar();
4170 // cur.pos() = cur.lastpos();
4174 case LFUN_LAYOUT_TABULAR:
4175 cur.bv().showDialog("tabular");
4178 case LFUN_INSET_MODIFY: {
4180 if (cmd.getArg(1) == "from-dialog")
4181 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4183 arg = to_utf8(cmd.argument());
4184 if (!tabularFeatures(cur, arg))
4189 // insert file functions
4190 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4191 case LFUN_FILE_INSERT_PLAINTEXT:
4193 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4194 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4195 FileName(to_utf8(cmd.argument())));
4198 cur.recordUndoInset(INSERT_UNDO);
4199 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4200 // content has been replaced,
4201 // so cursor might be invalid
4202 cur.pos() = cur.lastpos();
4203 cur.pit() = cur.lastpit();
4204 bvcur.setCursor(cur);
4211 if (cur.selIsMultiCell()) {
4212 if (copySelection(cur)) {
4213 cur.recordUndoInset(DELETE_UNDO);
4217 cell(cur.idx())->dispatch(cur, cmd);
4220 case LFUN_SELF_INSERT:
4221 if (cur.selIsMultiCell()) {
4222 cur.recordUndoInset(DELETE_UNDO);
4225 cell(cur.idx())->dispatch(cur, cmd);
4228 case LFUN_CHAR_DELETE_BACKWARD:
4229 case LFUN_CHAR_DELETE_FORWARD:
4230 if (cur.selIsMultiCell()) {
4231 cur.recordUndoInset(DELETE_UNDO);
4234 cell(cur.idx())->dispatch(cur, cmd);
4238 if (!cur.selection())
4240 if (cur.selIsMultiCell()) {
4244 cell(cur.idx())->dispatch(cur, cmd);
4247 case LFUN_CLIPBOARD_PASTE:
4248 case LFUN_PRIMARY_SELECTION_PASTE: {
4249 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4250 theClipboard().getAsText() :
4251 theSelection().get();
4254 // pass to InsertPlaintextString, but
4255 // only if we have multi-cell content
4256 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4257 cur.recordUndoInset(INSERT_UNDO);
4258 if (insertPlaintextString(cur.bv(), clip, false)) {
4259 // content has been replaced,
4260 // so cursor might be invalid
4261 cur.pos() = cur.lastpos();
4262 cur.pit() = cur.lastpit();
4263 bvcur.setCursor(cur);
4267 // Let the cell handle normal text
4268 cell(cur.idx())->dispatch(cur, cmd);
4273 if (!tabularStackDirty()) {
4274 if (!cur.selIsMultiCell())
4275 cell(cur.idx())->dispatch(cur, cmd);
4278 if (theClipboard().isInternal()) {
4279 cur.recordUndoInset(INSERT_UNDO);
4280 pasteClipboard(cur);
4284 case LFUN_FONT_EMPH:
4285 case LFUN_FONT_BOLD:
4286 case LFUN_FONT_BOLDSYMBOL:
4287 case LFUN_FONT_ROMAN:
4288 case LFUN_FONT_NOUN:
4289 case LFUN_FONT_ITAL:
4290 case LFUN_FONT_FRAK:
4291 case LFUN_FONT_TYPEWRITER:
4292 case LFUN_FONT_SANS:
4293 case LFUN_TEXTSTYLE_APPLY:
4294 case LFUN_TEXTSTYLE_UPDATE:
4295 case LFUN_FONT_SIZE:
4296 case LFUN_FONT_UNDERLINE:
4297 case LFUN_FONT_STRIKEOUT:
4298 case LFUN_FONT_UULINE:
4299 case LFUN_FONT_UWAVE:
4301 case LFUN_WORD_CAPITALIZE:
4302 case LFUN_WORD_UPCASE:
4303 case LFUN_WORD_LOWCASE:
4304 case LFUN_CHARS_TRANSPOSE:
4305 if (cur.selIsMultiCell()) {
4308 getSelection(cur, rs, re, cs, ce);
4309 Cursor tmpcur = cur;
4310 for (row_type r = rs; r <= re; ++r) {
4311 for (col_type c = cs; c <= ce; ++c) {
4312 // cursor follows cell:
4313 tmpcur.idx() = tabular.cellIndex(r, c);
4314 // select this cell only:
4317 tmpcur.resetAnchor();
4318 tmpcur.pit() = tmpcur.lastpit();
4319 tmpcur.pos() = tmpcur.top().lastpos();
4320 tmpcur.setCursor(tmpcur);
4321 tmpcur.setSelection();
4322 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4327 cell(cur.idx())->dispatch(cur, cmd);
4331 case LFUN_INSET_SETTINGS:
4332 // relay this lfun to Inset, not to the cell.
4333 Inset::doDispatch(cur, cmd);
4337 // we try to handle this event in the insets dispatch function.
4338 cell(cur.idx())->dispatch(cur, cmd);
4344 // function sets an object as defined in func_status.h:
4345 // states OK, Unknown, Disabled, On, Off.
4346 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4347 FuncStatus & status) const
4349 switch (cmd.action()) {
4350 case LFUN_INSET_MODIFY: {
4351 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4354 // FIXME: We only check for the very first argument...
4355 string const s = cmd.getArg(1);
4356 // We always enable the lfun if it is coming from the dialog
4357 // because the dialog makes sure all the settings are valid,
4358 // even though the first argument might not be valid now.
4359 if (s == "from-dialog") {
4360 status.setEnabled(true);
4364 int action = Tabular::LAST_ACTION;
4366 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4367 if (tabularFeature[i].feature == s) {
4368 action = tabularFeature[i].action;
4372 if (action == Tabular::LAST_ACTION) {
4374 status.setUnknown(true);
4378 string const argument = cmd.getLongArg(2);
4380 row_type sel_row_start = 0;
4381 row_type sel_row_end = 0;
4382 col_type sel_col_start = 0;
4383 col_type sel_col_end = 0;
4384 Tabular::ltType dummyltt;
4387 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4390 case Tabular::SET_PWIDTH:
4391 case Tabular::SET_MPWIDTH:
4392 case Tabular::SET_SPECIAL_COLUMN:
4393 case Tabular::SET_SPECIAL_MULTICOLUMN:
4394 case Tabular::APPEND_ROW:
4395 case Tabular::APPEND_COLUMN:
4396 case Tabular::DELETE_ROW:
4397 case Tabular::DELETE_COLUMN:
4398 case Tabular::COPY_ROW:
4399 case Tabular::COPY_COLUMN:
4400 case Tabular::SET_TOP_SPACE:
4401 case Tabular::SET_BOTTOM_SPACE:
4402 case Tabular::SET_INTERLINE_SPACE:
4406 case Tabular::SET_TABULAR_WIDTH:
4407 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4408 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4411 case Tabular::SET_DECIMAL_POINT:
4413 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4416 case Tabular::SET_MULTICOLUMN:
4417 case Tabular::UNSET_MULTICOLUMN:
4418 case Tabular::MULTICOLUMN:
4419 // If a row is set as longtable caption, it must not be allowed
4420 // to unset that this row is a multicolumn.
4421 status.setEnabled(sel_row_start == sel_row_end
4422 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4423 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4426 case Tabular::SET_MULTIROW:
4427 case Tabular::UNSET_MULTIROW:
4428 case Tabular::MULTIROW:
4429 // If a row is set as longtable caption, it must not be allowed
4430 // to unset that this row is a multirow.
4431 status.setEnabled(sel_col_start == sel_col_end
4432 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4433 status.setOnOff(tabular.isMultiRow(cur.idx()));
4436 case Tabular::SET_ALL_LINES:
4437 case Tabular::UNSET_ALL_LINES:
4438 case Tabular::SET_BORDER_LINES:
4439 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4442 case Tabular::SET_LINE_TOP:
4443 case Tabular::SET_LINE_BOTTOM:
4444 case Tabular::SET_LINE_LEFT:
4445 case Tabular::SET_LINE_RIGHT:
4446 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4449 case Tabular::TOGGLE_LINE_TOP:
4450 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4451 status.setOnOff(tabular.topLine(cur.idx()));
4454 case Tabular::TOGGLE_LINE_BOTTOM:
4455 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4456 status.setOnOff(tabular.bottomLine(cur.idx()));
4459 case Tabular::TOGGLE_LINE_LEFT:
4460 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4461 status.setOnOff(tabular.leftLine(cur.idx()));
4464 case Tabular::TOGGLE_LINE_RIGHT:
4465 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4466 status.setOnOff(tabular.rightLine(cur.idx()));
4469 // multirow cells only inherit the alignment of the column if the column has
4470 // no width, otherwise they are left-aligned
4471 // therefore allow always left but right and center only if there is no width
4472 case Tabular::M_ALIGN_LEFT:
4474 case Tabular::ALIGN_LEFT:
4475 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4478 case Tabular::M_ALIGN_RIGHT:
4480 case Tabular::ALIGN_RIGHT:
4481 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4482 && !tabular.getPWidth(cur.idx()).zero()));
4483 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4486 case Tabular::M_ALIGN_CENTER:
4488 case Tabular::ALIGN_CENTER:
4489 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4490 && !tabular.getPWidth(cur.idx()).zero()));
4491 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4494 case Tabular::ALIGN_BLOCK:
4495 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4496 && !tabular.isMultiRow(cur.idx()));
4497 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4500 case Tabular::ALIGN_DECIMAL:
4501 status.setEnabled(!tabular.isMultiRow(cur.idx())
4502 && !tabular.isMultiColumn(cur.idx()));
4503 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4506 case Tabular::M_VALIGN_TOP:
4508 case Tabular::VALIGN_TOP:
4509 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4510 && !tabular.isMultiRow(cur.idx()));
4512 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4515 case Tabular::M_VALIGN_BOTTOM:
4517 case Tabular::VALIGN_BOTTOM:
4518 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4519 && !tabular.isMultiRow(cur.idx()));
4521 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4524 case Tabular::M_VALIGN_MIDDLE:
4526 case Tabular::VALIGN_MIDDLE:
4527 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4528 && !tabular.isMultiRow(cur.idx()));
4530 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4533 case Tabular::SET_LONGTABULAR:
4534 // setting as longtable is not allowed when table is inside a float
4535 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4536 || cur.innerInsetOfType(WRAP_CODE) != 0)
4537 status.setEnabled(false);
4539 status.setEnabled(true);
4540 status.setOnOff(tabular.is_long_tabular);
4543 case Tabular::UNSET_LONGTABULAR:
4544 status.setOnOff(!tabular.is_long_tabular);
4547 case Tabular::TOGGLE_ROTATE_TABULAR:
4548 case Tabular::SET_ROTATE_TABULAR:
4549 status.setOnOff(tabular.rotate != 0);
4552 case Tabular::TABULAR_VALIGN_TOP:
4553 status.setEnabled(tabular.tabular_width.zero());
4554 status.setOnOff(tabular.tabular_valignment
4555 == Tabular::LYX_VALIGN_TOP);
4557 case Tabular::TABULAR_VALIGN_MIDDLE:
4558 status.setEnabled(tabular.tabular_width.zero());
4559 status.setOnOff(tabular.tabular_valignment
4560 == Tabular::LYX_VALIGN_MIDDLE);
4562 case Tabular::TABULAR_VALIGN_BOTTOM:
4563 status.setEnabled(tabular.tabular_width.zero());
4564 status.setOnOff(tabular.tabular_valignment
4565 == Tabular::LYX_VALIGN_BOTTOM);
4568 case Tabular::LONGTABULAR_ALIGN_LEFT:
4569 status.setOnOff(tabular.longtabular_alignment
4570 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4572 case Tabular::LONGTABULAR_ALIGN_CENTER:
4573 status.setOnOff(tabular.longtabular_alignment
4574 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4576 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4577 status.setOnOff(tabular.longtabular_alignment
4578 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4581 case Tabular::UNSET_ROTATE_TABULAR:
4582 status.setOnOff(tabular.rotate == 0);
4585 case Tabular::TOGGLE_ROTATE_CELL:
4586 case Tabular::SET_ROTATE_CELL:
4587 status.setOnOff(!oneCellHasRotationState(false,
4588 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4591 case Tabular::UNSET_ROTATE_CELL:
4592 status.setOnOff(!oneCellHasRotationState(true,
4593 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4596 case Tabular::SET_USEBOX:
4597 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4600 // every row can only be one thing:
4601 // either a footer or header
4602 case Tabular::SET_LTFIRSTHEAD:
4603 status.setEnabled(sel_row_start == sel_row_end);
4604 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4607 case Tabular::UNSET_LTFIRSTHEAD:
4608 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4609 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4612 case Tabular::SET_LTHEAD:
4613 status.setEnabled(sel_row_start == sel_row_end);
4614 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4617 case Tabular::UNSET_LTHEAD:
4618 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4619 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4622 case Tabular::SET_LTFOOT:
4623 status.setEnabled(sel_row_start == sel_row_end);
4624 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4627 case Tabular::UNSET_LTFOOT:
4628 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4629 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4632 case Tabular::SET_LTLASTFOOT:
4633 status.setEnabled(sel_row_start == sel_row_end);
4634 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4637 case Tabular::UNSET_LTLASTFOOT:
4638 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4639 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4642 case Tabular::SET_LTNEWPAGE:
4643 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4645 case Tabular::UNSET_LTNEWPAGE:
4646 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4649 // only one row in head/firsthead/foot/lasthead can be the caption
4650 // and a multirow cannot be set as caption
4651 case Tabular::SET_LTCAPTION:
4652 status.setEnabled(sel_row_start == sel_row_end
4653 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4654 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4655 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4656 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4657 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4658 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4659 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4660 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4661 && !tabular.isMultiRow(sel_row_start));
4662 status.setOnOff(tabular.ltCaption(sel_row_start));
4665 case Tabular::UNSET_LTCAPTION:
4666 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4669 case Tabular::TOGGLE_LTCAPTION:
4670 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4671 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4672 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4673 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4674 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4675 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4676 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4677 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4678 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4679 status.setOnOff(tabular.ltCaption(sel_row_start));
4682 case Tabular::SET_BOOKTABS:
4683 status.setOnOff(tabular.use_booktabs);
4686 case Tabular::UNSET_BOOKTABS:
4687 status.setOnOff(!tabular.use_booktabs);
4692 status.setEnabled(false);
4698 // These are only enabled inside tabular
4699 case LFUN_CELL_BACKWARD:
4700 case LFUN_CELL_FORWARD:
4701 status.setEnabled(true);
4704 // disable these with multiple cells selected
4705 case LFUN_INSET_INSERT:
4706 case LFUN_TABULAR_INSERT:
4707 case LFUN_FLEX_INSERT:
4708 case LFUN_FLOAT_INSERT:
4709 case LFUN_FLOAT_WIDE_INSERT:
4710 case LFUN_FOOTNOTE_INSERT:
4711 case LFUN_MARGINALNOTE_INSERT:
4712 case LFUN_MATH_INSERT:
4713 case LFUN_MATH_MODE:
4714 case LFUN_MATH_MUTATE:
4715 case LFUN_MATH_DISPLAY:
4716 case LFUN_NOTE_INSERT:
4717 case LFUN_ARGUMENT_INSERT:
4718 case LFUN_BOX_INSERT:
4719 case LFUN_BRANCH_INSERT:
4720 case LFUN_PHANTOM_INSERT:
4721 case LFUN_WRAP_INSERT:
4722 case LFUN_PREVIEW_INSERT:
4723 case LFUN_ERT_INSERT: {
4724 if (cur.selIsMultiCell()) {
4725 status.setEnabled(false);
4728 return cell(cur.idx())->getStatus(cur, cmd, status);
4731 // disable in non-fixed-width cells
4732 case LFUN_BREAK_PARAGRAPH:
4733 // multirow does not allow paragraph breaks
4734 if (tabular.isMultiRow(cur.idx())) {
4735 status.setEnabled(false);
4738 case LFUN_NEWLINE_INSERT: {
4739 if (tabular.getPWidth(cur.idx()).zero()) {
4740 status.setEnabled(false);
4743 return cell(cur.idx())->getStatus(cur, cmd, status);
4746 case LFUN_NEWPAGE_INSERT:
4747 status.setEnabled(false);
4751 if (tabularStackDirty() && theClipboard().isInternal()) {
4752 if (cur.selIsMultiCell()) {
4755 getSelection(cur, rs, re, cs, ce);
4756 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4757 && paste_tabular->nrows() == re - rs + 1)
4758 status.setEnabled(true);
4760 status.setEnabled(false);
4761 status.message(_("Selection size should match clipboard content."));
4764 status.setEnabled(true);
4767 return cell(cur.idx())->getStatus(cur, cmd, status);
4769 case LFUN_INSET_SETTINGS:
4770 // relay this lfun to Inset, not to the cell.
4771 return Inset::getStatus(cur, cmd, status);
4774 // we try to handle this event in the insets dispatch function.
4775 return cell(cur.idx())->getStatus(cur, cmd, status);
4781 Inset::DisplayType InsetTabular::display() const
4783 if (tabular.is_long_tabular) {
4784 switch (tabular.longtabular_alignment) {
4785 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4787 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4789 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4799 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4801 tabular.latex(os, runparams);
4805 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4807 os << '\n'; // output table on a new line
4808 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4809 tabular.plaintext(os, runparams, dp, false, 0);
4810 return PLAINTEXT_NEWLINE;
4814 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4819 // FIXME: Why not pass a proper DocIterator here?
4821 // if the table is inside a float it doesn't need the informaltable
4822 // wrapper. Search for it.
4823 for (master = owner(); master; master = master->owner())
4824 if (master->lyxCode() == FLOAT_CODE)
4829 os << "<informaltable>";
4832 ret += tabular.docbook(os, runparams);
4834 os << "</informaltable>";
4841 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4843 return tabular.xhtml(xs, rp);
4847 void InsetTabular::validate(LaTeXFeatures & features) const
4849 tabular.validate(features);
4851 // It'd be better to be able to get this from an InsetLayout, but at present
4852 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4853 if (features.runparams().flavor == OutputParams::HTML)
4854 features.addCSSSnippet(
4855 "table { border: 1px solid black; display: inline-block; }\n"
4856 "td { border: 1px solid black; padding: 0.5ex; }");
4860 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4862 return tabular.cellInset(idx);
4866 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4868 return tabular.cellInset(idx);
4872 void InsetTabular::cursorPos(BufferView const & bv,
4873 CursorSlice const & sl, bool boundary, int & x, int & y) const
4875 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4877 // y offset correction
4878 y += cellYPos(sl.idx());
4879 y += tabular.textVOffset(sl.idx());
4880 y += offset_valign_;
4882 // x offset correction
4883 x += cellXPos(sl.idx());
4884 x += tabular.textHOffset(sl.idx());
4885 x += ADD_TO_TABULAR_WIDTH;
4890 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4894 Inset const & inset = *tabular.cellInset(cell);
4895 Point o = bv.coordCache().getInsets().xy(&inset);
4896 int const xbeg = o.x_ - tabular.textHOffset(cell);
4897 int const xend = xbeg + tabular.cellWidth(cell);
4898 row_type const row = tabular.cellRow(cell);
4899 int const ybeg = o.y_ - tabular.rowAscent(row)
4900 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
4901 int const yend = ybeg + tabular.cellHeight(cell);
4913 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4914 // << " ybeg=" << ybeg << " yend=" << yend
4915 // << " xx=" << xx << " yy=" << yy
4916 // << " dist=" << xx + yy << endl;
4921 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4923 //lyxerr << "InsetTabular::editXY: " << this << endl;
4924 cur.setSelection(false);
4926 cur.idx() = getNearestCell(cur.bv(), x, y);
4928 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4932 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4934 cur.idx() = getNearestCell(cur.bv(), x, y);
4935 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4939 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4941 idx_type idx_min = 0;
4942 int dist_min = numeric_limits<int>::max();
4943 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4944 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4945 int const d = dist(bv, i, x, y);
4956 int InsetTabular::cellYPos(idx_type const cell) const
4958 row_type row = tabular.cellRow(cell);
4960 for (row_type r = 0; r < row; ++r)
4961 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4962 + tabular.interRowSpace(r + 1);
4967 int InsetTabular::cellXPos(idx_type const cell) const
4969 col_type col = tabular.cellColumn(cell);
4971 for (col_type c = 0; c < col; ++c)
4972 lx += tabular.column_info[c].width;
4977 void InsetTabular::resetPos(Cursor & cur) const
4979 BufferView & bv = cur.bv();
4980 int const maxwidth = bv.workWidth();
4982 int const scx_old = scx_;
4983 int const i = cur.find(this);
4988 int const X2 = maxwidth;
4989 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4990 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4991 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4994 scx_ = X1 + 20 - x1;
4996 scx_ = X2 - 20 - x2;
5001 // only update if offset changed
5002 if (scx_ != scx_old)
5003 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5007 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5009 row_type const row = tabular.cellRow(cur.idx());
5010 col_type const col = tabular.cellColumn(cur.idx());
5012 if (isRightToLeft(cur)) {
5013 if (tabular.cellColumn(cur.idx()) == 0) {
5014 if (row == tabular.nrows() - 1)
5016 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5021 cur.idx() = tabular.getLastCellInRow(row - 1);
5023 cur.idx() = tabular.cellIndex(row, col - 1);
5026 if (tabular.isLastCell(cur.idx()))
5028 if (cur.idx() == tabular.getLastCellInRow(row))
5029 cur.idx() = tabular.cellIndex(row + 1, 0);
5031 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5032 cur.idx() = tabular.cellIndex(row, colnextcell);
5036 cur.boundary(false);
5038 if (cur.selIsMultiCell()) {
5039 cur.pit() = cur.lastpit();
5040 cur.pos() = cur.lastpos();
5048 // in visual mode, place cursor at extreme left or right
5050 switch(entry_from) {
5052 case ENTRY_DIRECTION_RIGHT:
5053 cur.posVisToRowExtremity(false /* !left */);
5055 case ENTRY_DIRECTION_LEFT:
5056 cur.posVisToRowExtremity(true /* left */);
5058 case ENTRY_DIRECTION_IGNORE:
5059 // nothing to do in this case
5063 cur.setCurrentFont();
5068 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5070 row_type const row = tabular.cellRow(cur.idx());
5071 col_type const col = tabular.cellColumn(cur.idx());
5073 if (isRightToLeft(cur)) {
5074 if (cur.idx() == tabular.getLastCellInRow(row)) {
5077 cur.idx() = tabular.getFirstCellInRow(row);
5078 cur.idx() = tabular.cellAbove(cur.idx());
5080 if (tabular.isLastCell(cur.idx()))
5082 if (cur.idx() == tabular.getLastCellInRow(row))
5083 cur.idx() = tabular.cellIndex(row + 1, 0);
5085 cur.idx() = tabular.cellIndex(row, col + 1);
5088 if (cur.idx() == 0) // first cell
5091 cur.idx() = tabular.getLastCellInRow(row - 1);
5093 cur.idx() = tabular.cellIndex(row, col - 1);
5096 if (cur.selIsMultiCell()) {
5097 cur.pit() = cur.lastpit();
5098 cur.pos() = cur.lastpos();
5103 cur.pit() = cur.lastpit();
5104 cur.pos() = cur.lastpos();
5106 // in visual mode, place cursor at extreme left or right
5108 switch(entry_from) {
5110 case ENTRY_DIRECTION_RIGHT:
5111 cur.posVisToRowExtremity(false /* !left */);
5113 case ENTRY_DIRECTION_LEFT:
5114 cur.posVisToRowExtremity(true /* left */);
5116 case ENTRY_DIRECTION_IGNORE:
5117 // nothing to do in this case
5121 cur.setCurrentFont();
5126 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5128 istringstream is(argument);
5131 if (insetCode(s) != TABULAR_CODE)
5135 size_t safe_guard = 0;
5140 if (safe_guard > 1000) {
5141 LYXERR0("parameter max count reached!");
5145 Tabular::Feature action = Tabular::LAST_ACTION;
5148 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5149 if (s != tabularFeature[i].feature)
5151 action = tabularFeature[i].action;
5154 if (action == Tabular::LAST_ACTION) {
5155 LYXERR0("Feature not found " << s);
5159 if (tabularFeature[i].need_value)
5161 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5162 tabularFeatures(cur, action, val);
5168 static void checkLongtableSpecial(Tabular::ltType & ltt,
5169 string const & special, bool & flag)
5171 if (special == "dl_above") {
5174 } else if (special == "dl_below") {
5175 ltt.bottomDL = flag;
5177 } else if (special == "empty") {
5187 bool InsetTabular::oneCellHasRotationState(bool rotated,
5188 row_type row_start, row_type row_end,
5189 col_type col_start, col_type col_end) const
5191 for (row_type r = row_start; r <= row_end; ++r)
5192 for (col_type c = col_start; c <= col_end; ++c)
5194 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5197 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5204 void InsetTabular::tabularFeatures(Cursor & cur,
5205 Tabular::Feature feature, string const & value)
5207 col_type sel_col_start;
5208 col_type sel_col_end;
5209 row_type sel_row_start;
5210 row_type sel_row_end;
5211 bool setLines = false;
5212 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5213 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5217 case Tabular::M_ALIGN_LEFT:
5218 case Tabular::ALIGN_LEFT:
5219 setAlign = LYX_ALIGN_LEFT;
5222 case Tabular::M_ALIGN_RIGHT:
5223 case Tabular::ALIGN_RIGHT:
5224 setAlign = LYX_ALIGN_RIGHT;
5227 case Tabular::M_ALIGN_CENTER:
5228 case Tabular::ALIGN_CENTER:
5229 setAlign = LYX_ALIGN_CENTER;
5232 case Tabular::ALIGN_BLOCK:
5233 setAlign = LYX_ALIGN_BLOCK;
5236 case Tabular::ALIGN_DECIMAL:
5237 setAlign = LYX_ALIGN_DECIMAL;
5240 case Tabular::M_VALIGN_TOP:
5241 case Tabular::VALIGN_TOP:
5242 setVAlign = Tabular::LYX_VALIGN_TOP;
5245 case Tabular::M_VALIGN_BOTTOM:
5246 case Tabular::VALIGN_BOTTOM:
5247 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5250 case Tabular::M_VALIGN_MIDDLE:
5251 case Tabular::VALIGN_MIDDLE:
5252 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5259 cur.recordUndoInset(ATOMIC_UNDO, this);
5261 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5262 row_type const row = tabular.cellRow(cur.idx());
5263 col_type const column = tabular.cellColumn(cur.idx());
5265 Tabular::ltType ltt;
5269 case Tabular::SET_TABULAR_WIDTH:
5270 tabular.setTabularWidth(Length(value));
5273 case Tabular::SET_PWIDTH: {
5274 Length const len(value);
5275 tabular.setColumnPWidth(cur, cur.idx(), len);
5277 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5278 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5282 case Tabular::SET_MPWIDTH:
5283 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5286 case Tabular::SET_MROFFSET:
5287 tabular.setMROffset(cur, cur.idx(), Length(value));
5290 case Tabular::SET_SPECIAL_COLUMN:
5291 case Tabular::SET_SPECIAL_MULTICOLUMN:
5292 if (value == "none")
5293 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5295 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5298 case Tabular::APPEND_ROW:
5299 // append the row into the tabular
5300 tabular.appendRow(row);
5303 case Tabular::APPEND_COLUMN:
5304 // append the column into the tabular
5305 tabular.appendColumn(column);
5306 cur.idx() = tabular.cellIndex(row, column);
5309 case Tabular::DELETE_ROW:
5310 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5311 tabular.deleteRow(sel_row_start);
5312 if (sel_row_start >= tabular.nrows())
5314 cur.idx() = tabular.cellIndex(sel_row_start, column);
5317 cur.setSelection(false);
5320 case Tabular::DELETE_COLUMN:
5321 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5322 tabular.deleteColumn(sel_col_start);
5323 if (sel_col_start >= tabular.ncols())
5325 cur.idx() = tabular.cellIndex(row, sel_col_start);
5328 cur.setSelection(false);
5331 case Tabular::COPY_ROW:
5332 tabular.copyRow(row);
5335 case Tabular::COPY_COLUMN:
5336 tabular.copyColumn(column);
5337 cur.idx() = tabular.cellIndex(row, column);
5340 case Tabular::SET_LINE_TOP:
5341 case Tabular::TOGGLE_LINE_TOP: {
5342 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5343 ? (value == "true") : !tabular.topLine(cur.idx());
5344 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5345 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5346 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5350 case Tabular::SET_LINE_BOTTOM:
5351 case Tabular::TOGGLE_LINE_BOTTOM: {
5352 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5353 ? (value == "true") : !tabular.bottomLine(cur.idx());
5354 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5355 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5356 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5360 case Tabular::SET_LINE_LEFT:
5361 case Tabular::TOGGLE_LINE_LEFT: {
5362 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5363 ? (value == "true") : !tabular.leftLine(cur.idx());
5364 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5365 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5366 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5370 case Tabular::SET_LINE_RIGHT:
5371 case Tabular::TOGGLE_LINE_RIGHT: {
5372 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5373 ? (value == "true") : !tabular.rightLine(cur.idx());
5374 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5375 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5376 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5380 case Tabular::M_ALIGN_LEFT:
5381 case Tabular::M_ALIGN_RIGHT:
5382 case Tabular::M_ALIGN_CENTER:
5383 case Tabular::ALIGN_LEFT:
5384 case Tabular::ALIGN_RIGHT:
5385 case Tabular::ALIGN_CENTER:
5386 case Tabular::ALIGN_BLOCK:
5387 case Tabular::ALIGN_DECIMAL:
5388 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5389 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5390 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5391 !tabular.getPWidth(c).zero());
5394 case Tabular::M_VALIGN_TOP:
5395 case Tabular::M_VALIGN_BOTTOM:
5396 case Tabular::M_VALIGN_MIDDLE:
5398 case Tabular::VALIGN_TOP:
5399 case Tabular::VALIGN_BOTTOM:
5400 case Tabular::VALIGN_MIDDLE:
5401 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5402 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5403 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5406 case Tabular::SET_MULTICOLUMN: {
5407 if (!cur.selection()) {
5408 // just multicol for one single cell
5409 // check whether we are completely in a multicol
5410 if (!tabular.isMultiColumn(cur.idx()))
5411 tabular.setMultiColumn(cur.idx(), 1,
5412 tabular.rightLine(cur.idx()));
5415 // we have a selection so this means we just add all this
5416 // cells to form a multicolumn cell
5417 idx_type const s_start = cur.selBegin().idx();
5418 row_type const col_start = tabular.cellColumn(s_start);
5419 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5420 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5421 tabular.rightLine(cur.selEnd().idx()));
5424 cur.setSelection(false);
5428 case Tabular::UNSET_MULTICOLUMN: {
5429 if (!cur.selection()) {
5430 if (tabular.isMultiColumn(cur.idx()))
5431 tabular.unsetMultiColumn(cur.idx());
5436 case Tabular::MULTICOLUMN: {
5437 if (!cur.selection()) {
5438 if (tabular.isMultiColumn(cur.idx()))
5439 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5441 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5445 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5446 row_type const r = sel_row_start;
5447 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5448 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5451 // If the selection contains at least one singlecol cell
5452 // or multiple multicol cells,
5453 // we assume the user will merge is to a single multicol
5455 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5457 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5461 case Tabular::SET_MULTIROW: {
5462 if (!cur.selection()) {
5463 // just multirow for one single cell
5464 // check whether we are completely in a multirow
5465 if (!tabular.isMultiRow(cur.idx()))
5466 tabular.setMultiRow(cur.idx(), 1,
5467 tabular.bottomLine(cur.idx()));
5470 // we have a selection so this means we just add all this
5471 // cells to form a multirow cell
5472 idx_type const s_start = cur.selBegin().idx();
5473 row_type const row_start = tabular.cellRow(s_start);
5474 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5475 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5476 tabular.bottomLine(cur.selEnd().idx()));
5479 cur.setSelection(false);
5483 case Tabular::UNSET_MULTIROW: {
5484 if (!cur.selection()) {
5485 if (tabular.isMultiRow(cur.idx()))
5486 tabular.unsetMultiRow(cur.idx());
5491 case Tabular::MULTIROW: {
5492 if (!cur.selection()) {
5493 if (tabular.isMultiRow(cur.idx()))
5494 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5496 tabularFeatures(cur, Tabular::SET_MULTIROW);
5500 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5501 col_type const c = sel_col_start;
5502 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5503 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5506 // If the selection contains at least one singlerow cell
5507 // or multiple multirow cells,
5508 // we assume the user will merge is to a single multirow
5510 tabularFeatures(cur, Tabular::SET_MULTIROW);
5512 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5516 case Tabular::SET_ALL_LINES:
5518 case Tabular::UNSET_ALL_LINES:
5519 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5520 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5521 idx_type const cell = tabular.cellIndex(r, c);
5522 tabular.setTopLine(cell, setLines);
5523 tabular.setBottomLine(cell, setLines);
5524 tabular.setRightLine(cell, setLines);
5525 tabular.setLeftLine(cell, setLines);
5529 case Tabular::SET_BORDER_LINES:
5530 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5531 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5532 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5534 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5535 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5536 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5540 case Tabular::SET_LONGTABULAR:
5541 tabular.is_long_tabular = true;
5544 case Tabular::UNSET_LONGTABULAR:
5545 for (row_type r = 0; r < tabular.nrows(); ++r) {
5546 if (tabular.ltCaption(r)) {
5547 cur.idx() = tabular.cellIndex(r, 0);
5550 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5553 tabular.is_long_tabular = false;
5556 case Tabular::SET_ROTATE_TABULAR:
5557 tabular.rotate = convert<int>(value);
5560 case Tabular::UNSET_ROTATE_TABULAR:
5564 case Tabular::TOGGLE_ROTATE_TABULAR:
5565 // when pressing the rotate button we default to 90° rotation
5566 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5569 case Tabular::TABULAR_VALIGN_TOP:
5570 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5573 case Tabular::TABULAR_VALIGN_MIDDLE:
5574 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5577 case Tabular::TABULAR_VALIGN_BOTTOM:
5578 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5581 case Tabular::LONGTABULAR_ALIGN_LEFT:
5582 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5585 case Tabular::LONGTABULAR_ALIGN_CENTER:
5586 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5589 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5590 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5595 case Tabular::SET_ROTATE_CELL:
5596 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5597 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5598 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5601 case Tabular::UNSET_ROTATE_CELL:
5602 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5603 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5604 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5607 case Tabular::TOGGLE_ROTATE_CELL:
5609 bool oneNotRotated = oneCellHasRotationState(false,
5610 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5612 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5613 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5614 // when pressing the rotate cell button we default to 90° rotation
5616 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5618 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5623 case Tabular::SET_USEBOX: {
5624 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5625 if (val == tabular.getUsebox(cur.idx()))
5626 val = Tabular::BOX_NONE;
5627 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5628 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5629 tabular.setUsebox(tabular.cellIndex(r, c), val);
5633 case Tabular::UNSET_LTFIRSTHEAD:
5635 case Tabular::SET_LTFIRSTHEAD:
5636 tabular.getRowOfLTFirstHead(row, ltt);
5637 checkLongtableSpecial(ltt, value, flag);
5638 tabular.setLTHead(row, flag, ltt, true);
5641 case Tabular::UNSET_LTHEAD:
5643 case Tabular::SET_LTHEAD:
5644 tabular.getRowOfLTHead(row, ltt);
5645 checkLongtableSpecial(ltt, value, flag);
5646 tabular.setLTHead(row, flag, ltt, false);
5649 case Tabular::UNSET_LTFOOT:
5651 case Tabular::SET_LTFOOT:
5652 tabular.getRowOfLTFoot(row, ltt);
5653 checkLongtableSpecial(ltt, value, flag);
5654 tabular.setLTFoot(row, flag, ltt, false);
5657 case Tabular::UNSET_LTLASTFOOT:
5659 case Tabular::SET_LTLASTFOOT:
5660 tabular.getRowOfLTLastFoot(row, ltt);
5661 checkLongtableSpecial(ltt, value, flag);
5662 tabular.setLTFoot(row, flag, ltt, true);
5665 case Tabular::UNSET_LTNEWPAGE:
5667 case Tabular::SET_LTNEWPAGE:
5668 tabular.setLTNewPage(row, flag);
5671 case Tabular::SET_LTCAPTION: {
5672 if (tabular.ltCaption(row))
5674 cur.idx() = tabular.setLTCaption(row, true);
5677 cur.setSelection(false);
5678 // If a row is set as caption, then also insert
5679 // a caption. Otherwise the LaTeX output is broken.
5680 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5681 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5685 case Tabular::UNSET_LTCAPTION: {
5686 if (!tabular.ltCaption(row))
5688 cur.idx() = tabular.setLTCaption(row, false);
5691 cur.setSelection(false);
5692 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5693 if (lyx::getStatus(fr).enabled())
5698 case Tabular::TOGGLE_LTCAPTION: {
5699 if (tabular.ltCaption(row))
5700 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5702 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5706 case Tabular::SET_BOOKTABS:
5707 tabular.use_booktabs = true;
5710 case Tabular::UNSET_BOOKTABS:
5711 tabular.use_booktabs = false;
5714 case Tabular::SET_TOP_SPACE: {
5716 if (value == "default")
5717 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5718 tabular.row_info[r].top_space_default = true;
5719 else if (value == "none")
5720 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5721 tabular.row_info[r].top_space_default = false;
5722 tabular.row_info[r].top_space = len;
5724 else if (isValidLength(value, &len))
5725 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5726 tabular.row_info[r].top_space_default = false;
5727 tabular.row_info[r].top_space = len;
5732 case Tabular::SET_BOTTOM_SPACE: {
5734 if (value == "default")
5735 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5736 tabular.row_info[r].bottom_space_default = true;
5737 else if (value == "none")
5738 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5739 tabular.row_info[r].bottom_space_default = false;
5740 tabular.row_info[r].bottom_space = len;
5742 else if (isValidLength(value, &len))
5743 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5744 tabular.row_info[r].bottom_space_default = false;
5745 tabular.row_info[r].bottom_space = len;
5750 case Tabular::SET_INTERLINE_SPACE: {
5752 if (value == "default")
5753 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5754 tabular.row_info[r].interline_space_default = true;
5755 else if (value == "none")
5756 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5757 tabular.row_info[r].interline_space_default = false;
5758 tabular.row_info[r].interline_space = len;
5760 else if (isValidLength(value, &len))
5761 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5762 tabular.row_info[r].interline_space_default = false;
5763 tabular.row_info[r].interline_space = len;
5768 case Tabular::SET_DECIMAL_POINT:
5769 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5770 tabular.column_info[c].decimal_point = from_utf8(value);
5773 // dummy stuff just to avoid warnings
5774 case Tabular::LAST_ACTION:
5780 bool InsetTabular::copySelection(Cursor & cur)
5782 if (!cur.selection())
5787 getSelection(cur, rs, re, cs, ce);
5789 paste_tabular.reset(new Tabular(tabular));
5791 for (row_type r = 0; r < rs; ++r)
5792 paste_tabular->deleteRow(0);
5794 row_type const rows = re - rs + 1;
5795 while (paste_tabular->nrows() > rows)
5796 paste_tabular->deleteRow(rows);
5798 for (col_type c = 0; c < cs; ++c)
5799 paste_tabular->deleteColumn(0);
5801 col_type const columns = ce - cs + 1;
5802 while (paste_tabular->ncols() > columns)
5803 paste_tabular->deleteColumn(columns);
5805 paste_tabular->setBuffer(tabular.buffer());
5807 odocstringstream os;
5808 OutputParams const runparams(0);
5809 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5810 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5811 cap::copySelection(cur, os.str());
5813 // mark tabular stack dirty
5814 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5815 // when we (hopefully) have a one-for-all paste mechanism.
5816 // This must be called after cap::copySelection.
5817 dirtyTabularStack(true);
5823 bool InsetTabular::pasteClipboard(Cursor & cur)
5827 col_type actcol = tabular.cellColumn(cur.idx());
5828 row_type actrow = tabular.cellRow(cur.idx());
5830 if (cur.selIsMultiCell()) {
5833 getSelection(cur, actrow, re, actcol, ce);
5836 for (row_type r1 = 0, r2 = actrow;
5837 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5839 for (col_type c1 = 0, c2 = actcol;
5840 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5842 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5843 tabular.isPartOfMultiColumn(r2, c2))
5845 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5849 if (tabular.isPartOfMultiColumn(r2, c2)) {
5853 shared_ptr<InsetTableCell> inset(
5854 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5855 tabular.setCellInset(r2, c2, inset);
5856 // FIXME?: why do we need to do this explicitly? (EL)
5857 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5859 // FIXME: change tracking (MG)
5860 inset->setChange(Change(buffer().params().trackChanges ?
5861 Change::INSERTED : Change::UNCHANGED));
5869 void InsetTabular::cutSelection(Cursor & cur)
5871 if (!cur.selection())
5876 getSelection(cur, rs, re, cs, ce);
5877 for (row_type r = rs; r <= re; ++r) {
5878 for (col_type c = cs; c <= ce; ++c) {
5879 shared_ptr<InsetTableCell> t
5880 = cell(tabular.cellIndex(r, c));
5881 if (buffer().params().trackChanges)
5882 // FIXME: Change tracking (MG)
5883 t->setChange(Change(Change::DELETED));
5889 // cursor position might be invalid now
5890 if (cur.pit() > cur.lastpit())
5891 cur.pit() = cur.lastpit();
5892 if (cur.pos() > cur.lastpos())
5893 cur.pos() = cur.lastpos();
5894 cur.clearSelection();
5898 bool InsetTabular::isRightToLeft(Cursor & cur) const
5900 LASSERT(cur.depth() > 1, /**/);
5901 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5902 pos_type const parentpos = cur[cur.depth() - 2].pos();
5903 return parentpar.getFontSettings(buffer().params(),
5904 parentpos).language()->rightToLeft();
5908 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5911 LASSERT(stidx <= enidx, return docstring());
5913 col_type const col1 = tabular.cellColumn(stidx);
5914 col_type const col2 = tabular.cellColumn(enidx);
5915 row_type const row1 = tabular.cellRow(stidx);
5916 row_type const row2 = tabular.cellRow(enidx);
5918 for (col_type col = col1; col <= col2; col++)
5919 for (row_type row = row1; row <= row2; row++) {
5924 retval += tabular.cellInset(row, col)->asString(intoInsets);
5930 void InsetTabular::getSelection(Cursor & cur,
5931 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5933 CursorSlice const & beg = cur.selBegin();
5934 CursorSlice const & end = cur.selEnd();
5935 cs = tabular.cellColumn(beg.idx());
5936 ce = tabular.cellColumn(end.idx());
5940 rs = tabular.cellRow(beg.idx());
5941 re = tabular.cellRow(end.idx());
5947 Text * InsetTabular::getText(int idx) const
5949 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5953 void InsetTabular::setChange(Change const & change)
5955 for (idx_type idx = 0; idx < nargs(); ++idx)
5956 cell(idx)->setChange(change);
5960 void InsetTabular::acceptChanges()
5962 for (idx_type idx = 0; idx < nargs(); ++idx)
5963 cell(idx)->acceptChanges();
5967 void InsetTabular::rejectChanges()
5969 for (idx_type idx = 0; idx < nargs(); ++idx)
5970 cell(idx)->rejectChanges();
5974 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5976 return tabular.getPWidth(cell).zero();
5980 bool InsetTabular::forcePlainLayout(idx_type cell) const
5982 return !tabular.getPWidth(cell).zero();
5986 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5989 if (buf.length() <= 0)
5994 col_type maxCols = 1;
5995 size_t const len = buf.length();
5999 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6007 maxCols = max(cols, maxCols);
6013 maxCols = max(cols, maxCols);
6019 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6020 loctab = paste_tabular.get();
6022 dirtyTabularStack(true);
6025 cell = bv.cursor().idx();
6026 ocol = tabular.cellColumn(cell);
6027 row = tabular.cellRow(cell);
6031 idx_type const cells = loctab->numberofcells;
6034 rows = loctab->nrows();
6035 col_type const columns = loctab->ncols();
6037 while (cell < cells && p < len && row < rows &&
6038 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6044 // we can only set this if we are not too far right
6045 if (cols < columns) {
6046 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6047 Font const font = bv.textMetrics(&inset->text()).
6049 inset->setText(buf.substr(op, p - op), font,
6050 buffer().params().trackChanges);
6056 // we can only set this if we are not too far right
6057 if (cols < columns) {
6058 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6059 Font const font = bv.textMetrics(&inset->text()).
6061 inset->setText(buf.substr(op, p - op), font,
6062 buffer().params().trackChanges);
6067 cell = loctab->cellIndex(row, cols);
6073 // check for the last cell if there is no trailing '\n'
6074 if (cell < cells && op < len) {
6075 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6076 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6077 inset->setText(buf.substr(op, len - op), font,
6078 buffer().params().trackChanges);
6084 void InsetTabular::addPreview(DocIterator const & inset_pos,
6085 PreviewLoader & loader) const
6087 DocIterator cell_pos = inset_pos;
6089 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6090 for (row_type r = 0; r < tabular.nrows(); ++r) {
6091 for (col_type c = 0; c < tabular.ncols(); ++c) {
6092 cell_pos.top().idx() = tabular.cellIndex(r, c);
6093 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6099 bool InsetTabular::completionSupported(Cursor const & cur) const
6101 Cursor const & bvCur = cur.bv().cursor();
6102 if (&bvCur.inset() != this)
6104 return cur.text()->completionSupported(cur);
6108 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6110 return completionSupported(cur);
6114 bool InsetTabular::automaticInlineCompletion() const
6116 return lyxrc.completion_inline_text;
6120 bool InsetTabular::automaticPopupCompletion() const
6122 return lyxrc.completion_popup_text;
6126 bool InsetTabular::showCompletionCursor() const
6128 return lyxrc.completion_cursor_text;
6132 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6134 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6138 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6140 if (!completionSupported(cur))
6142 return cur.text()->completionPrefix(cur);
6146 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6148 if (!completionSupported(cur))
6151 return cur.text()->insertCompletion(cur, s, finished);
6155 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6156 Dimension & dim) const
6158 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6159 tm.completionPosAndDim(cur, x, y, dim);
6163 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6165 istringstream data(in);
6167 lex.setStream(data);
6174 if (!lex || token != "tabular") {
6175 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6179 // This is part of the inset proper that is usually swallowed
6180 // by Buffer::readInset
6182 if (!lex || token != "Tabular") {
6183 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6191 string InsetTabular::params2string(InsetTabular const & inset)
6194 data << "tabular" << ' ';
6196 data << "\\end_inset\n";