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();
2500 par.getParLanguage(buffer().params())->lang();
2501 if (lang == "farsi")
2503 else if (lang == "arabic_arabi")
2505 // currently, remaining RTL languages are
2506 // arabic_arabtex and hebrew
2510 // pass to the OutputParams that we are in a cell and
2511 // which alignment we have set.
2512 // InsetNewline needs this context information.
2513 OutputParams newrp(runparams);
2514 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2515 ? OutputParams::PLAIN
2516 : OutputParams::ALIGNED;
2518 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2519 // copy cell and split in 2
2520 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2521 head.setBuffer(buffer());
2522 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2524 dit.push_back(CursorSlice(head));
2525 head.setMacrocontextPositionRecursive(dit);
2526 bool hassep = false;
2527 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2528 head.latex(os, newrp);
2531 tail.setBuffer(head.buffer());
2533 dit.push_back(CursorSlice(tail));
2534 tail.setMacrocontextPositionRecursive(dit);
2535 tail.latex(os, newrp);
2537 } else if (!isPartOfMultiRow(row, c)) {
2538 if (!runparams.nice)
2539 os.texrow().start(par.id(), 0);
2540 inset->latex(os, newrp);
2543 runparams.encoding = newrp.encoding;
2547 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2548 if (cell != getLastCellInRow(row)) { // not last cell in row
2555 os << "\\tabularnewline";
2556 if (row_info[row].bottom_space_default) {
2558 os << "\\addlinespace";
2560 os << "[\\doublerulesep]";
2561 } else if (!row_info[row].bottom_space.zero()) {
2563 os << "\\addlinespace";
2565 << from_ascii(row_info[row].bottom_space.asLatexString())
2570 //output the bottom line
2571 TeXBottomHLine(os, row, lang);
2573 if (row_info[row].interline_space_default) {
2575 os << "\\addlinespace\n";
2577 os << "\\noalign{\\vskip\\doublerulesep}\n";
2578 } else if (!row_info[row].interline_space.zero()) {
2580 os << "\\addlinespace["
2581 << from_ascii(row_info[row].interline_space.asLatexString())
2584 os << "\\noalign{\\vskip"
2585 << from_ascii(row_info[row].interline_space.asLatexString())
2591 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2593 bool const is_tabular_star = !tabular_width.zero();
2595 //+---------------------------------------------------------------------
2596 //+ first the opening preamble +
2597 //+---------------------------------------------------------------------
2600 if (runparams.lastid != -1)
2601 os.texrow().start(runparams.lastid, runparams.lastpos);
2604 os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
2606 if (is_long_tabular) {
2607 os << "\\begin{longtable}";
2608 switch (longtabular_alignment) {
2609 case LYX_LONGTABULAR_ALIGN_LEFT:
2612 case LYX_LONGTABULAR_ALIGN_CENTER:
2614 case LYX_LONGTABULAR_ALIGN_RIGHT:
2619 if (is_tabular_star)
2620 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2622 os << "\\begin{tabular}";
2623 switch (tabular_valignment) {
2624 case LYX_VALIGN_TOP:
2627 case LYX_VALIGN_MIDDLE:
2629 case LYX_VALIGN_BOTTOM:
2637 if (is_tabular_star)
2638 os << "@{\\extracolsep{\\fill}}";
2640 for (col_type c = 0; c < ncols(); ++c) {
2641 if (columnLeftLine(c))
2643 if (!column_info[c].align_special.empty()) {
2644 os << column_info[c].align_special;
2646 if (!column_info[c].p_width.zero()) {
2647 switch (column_info[c].alignment) {
2648 case LYX_ALIGN_LEFT:
2649 os << ">{\\raggedright}";
2651 case LYX_ALIGN_RIGHT:
2652 os << ">{\\raggedleft}";
2654 case LYX_ALIGN_CENTER:
2655 os << ">{\\centering}";
2657 case LYX_ALIGN_NONE:
2658 case LYX_ALIGN_BLOCK:
2659 case LYX_ALIGN_LAYOUT:
2660 case LYX_ALIGN_SPECIAL:
2661 case LYX_ALIGN_DECIMAL:
2665 switch (column_info[c].valignment) {
2666 case LYX_VALIGN_TOP:
2669 case LYX_VALIGN_MIDDLE:
2672 case LYX_VALIGN_BOTTOM:
2677 << from_ascii(column_info[c].p_width.asLatexString())
2680 switch (column_info[c].alignment) {
2681 case LYX_ALIGN_LEFT:
2684 case LYX_ALIGN_RIGHT:
2687 case LYX_ALIGN_DECIMAL:
2688 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2694 } // end if else !column_info[i].p_width
2695 } // end if else !column_info[i].align_special
2696 if (columnRightLine(c))
2701 TeXLongtableHeaderFooter(os, runparams);
2703 //+---------------------------------------------------------------------
2704 //+ the single row and columns (cells) +
2705 //+---------------------------------------------------------------------
2707 for (row_type r = 0; r < nrows(); ++r) {
2708 if (isValidRow(r)) {
2709 TeXRow(os, r, runparams);
2710 if (is_long_tabular && row_info[r].newpage)
2711 os << "\\newpage\n";
2715 //+---------------------------------------------------------------------
2716 //+ the closing of the tabular +
2717 //+---------------------------------------------------------------------
2719 if (is_long_tabular)
2720 os << "\\end{longtable}";
2722 if (is_tabular_star)
2723 os << "\\end{tabular*}";
2725 os << "\\end{tabular}";
2729 os << breakln << "\\end{turn}";
2733 int Tabular::docbookRow(odocstream & os, row_type row,
2734 OutputParams const & runparams) const
2737 idx_type cell = getFirstCellInRow(row);
2740 for (col_type c = 0; c < ncols(); ++c) {
2741 if (isPartOfMultiColumn(row, c))
2744 os << "<entry align=\"";
2745 switch (getAlignment(cell)) {
2746 case LYX_ALIGN_LEFT:
2749 case LYX_ALIGN_RIGHT:
2757 os << "\" valign=\"";
2758 switch (getVAlignment(cell)) {
2759 case LYX_VALIGN_TOP:
2762 case LYX_VALIGN_BOTTOM:
2765 case LYX_VALIGN_MIDDLE:
2770 if (isMultiColumn(cell)) {
2771 os << " namest=\"col" << c << "\" ";
2772 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2776 ret += cellInset(cell)->docbook(os, runparams);
2785 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2789 //+---------------------------------------------------------------------
2790 //+ first the opening preamble +
2791 //+---------------------------------------------------------------------
2793 os << "<tgroup cols=\"" << ncols()
2794 << "\" colsep=\"1\" rowsep=\"1\">\n";
2796 for (col_type c = 0; c < ncols(); ++c) {
2797 os << "<colspec colname=\"col" << c << "\" align=\"";
2798 switch (column_info[c].alignment) {
2799 case LYX_ALIGN_LEFT:
2802 case LYX_ALIGN_RIGHT:
2810 if (runparams.flavor == OutputParams::XML)
2816 //+---------------------------------------------------------------------
2817 //+ Long Tabular case +
2818 //+---------------------------------------------------------------------
2820 // output caption info
2821 // The caption flag wins over head/foot
2822 if (haveLTCaption()) {
2823 os << "<caption>\n";
2825 for (row_type r = 0; r < nrows(); ++r) {
2826 if (row_info[r].caption) {
2827 ret += docbookRow(os, r, runparams);
2830 os << "</caption>\n";
2833 // output header info
2834 if (haveLTHead(false) || haveLTFirstHead(false)) {
2837 for (row_type r = 0; r < nrows(); ++r) {
2838 if ((row_info[r].endhead || row_info[r].endfirsthead) &&
2839 !row_info[r].caption) {
2840 ret += docbookRow(os, r, runparams);
2846 // output footer info
2847 if (haveLTFoot(false) || haveLTLastFoot(false)) {
2850 for (row_type r = 0; r < nrows(); ++r) {
2851 if ((row_info[r].endfoot || row_info[r].endlastfoot) &&
2852 !row_info[r].caption) {
2853 ret += docbookRow(os, r, runparams);
2860 //+---------------------------------------------------------------------
2861 //+ the single row and columns (cells) +
2862 //+---------------------------------------------------------------------
2866 for (row_type r = 0; r < nrows(); ++r) {
2867 if (isValidRow(r)) {
2868 ret += docbookRow(os, r, runparams);
2873 //+---------------------------------------------------------------------
2874 //+ the closing of the tabular +
2875 //+---------------------------------------------------------------------
2884 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2885 OutputParams const & runparams, bool header) const
2888 string const celltag = header ? "th" : "td";
2889 idx_type cell = getFirstCellInRow(row);
2891 xs << html::StartTag("tr");
2892 for (col_type c = 0; c < ncols(); ++c) {
2893 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2898 switch (getAlignment(cell)) {
2899 case LYX_ALIGN_LEFT:
2902 case LYX_ALIGN_RIGHT:
2910 attr << " valign='";
2911 switch (getVAlignment(cell)) {
2912 case LYX_VALIGN_TOP:
2915 case LYX_VALIGN_BOTTOM:
2918 case LYX_VALIGN_MIDDLE:
2923 if (isMultiColumn(cell))
2924 attr << " colspan='" << columnSpan(cell) << "'";
2925 else if (isMultiRow(cell))
2926 attr << " rowspan='" << rowSpan(cell) << "'";
2928 xs << html::StartTag(celltag, attr.str()) << html::CR();
2929 ret += cellInset(cell)->xhtml(xs, runparams);
2930 xs << html::EndTag(celltag) << html::CR();
2933 xs << html::EndTag("tr");
2938 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2942 if (is_long_tabular) {
2943 // we'll wrap it in a div, so as to deal with alignment
2945 switch (longtabular_alignment) {
2946 case LYX_LONGTABULAR_ALIGN_LEFT:
2949 case LYX_LONGTABULAR_ALIGN_CENTER:
2952 case LYX_LONGTABULAR_ALIGN_RIGHT:
2956 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2958 // The caption flag wins over head/foot
2959 if (haveLTCaption()) {
2960 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2962 for (row_type r = 0; r < nrows(); ++r)
2963 if (row_info[r].caption)
2964 ret += xhtmlRow(xs, r, runparams);
2965 xs << html::EndTag("div") << html::CR();
2969 xs << html::StartTag("table") << html::CR();
2971 // output header info
2972 bool const havefirsthead = haveLTFirstHead(false);
2973 // if we have a first head, then we are going to ignore the
2974 // headers for the additional pages, since there aren't any
2975 // in XHTML. this test accomplishes that.
2976 bool const havehead = !havefirsthead && haveLTHead(false);
2977 if (havehead || havefirsthead) {
2978 xs << html::StartTag("thead") << html::CR();
2979 for (row_type r = 0; r < nrows(); ++r) {
2980 if (((havefirsthead && row_info[r].endfirsthead) ||
2981 (havehead && row_info[r].endhead)) &&
2982 !row_info[r].caption) {
2983 ret += xhtmlRow(xs, r, runparams, true);
2986 xs << html::EndTag("thead") << html::CR();
2988 // output footer info
2989 bool const havelastfoot = haveLTLastFoot(false);
2991 bool const havefoot = !havelastfoot && haveLTFoot(false);
2992 if (havefoot || havelastfoot) {
2993 xs << html::StartTag("tfoot") << html::CR();
2994 for (row_type r = 0; r < nrows(); ++r) {
2995 if (((havelastfoot && row_info[r].endlastfoot) ||
2996 (havefoot && row_info[r].endfoot)) &&
2997 !row_info[r].caption) {
2998 ret += xhtmlRow(xs, r, runparams);
3001 xs << html::EndTag("tfoot") << html::CR();
3004 xs << html::StartTag("tbody") << html::CR();
3005 for (row_type r = 0; r < nrows(); ++r) {
3006 if (isValidRow(r)) {
3007 ret += xhtmlRow(xs, r, runparams);
3010 xs << html::EndTag("tbody")
3012 << html::EndTag("table")
3014 if (is_long_tabular)
3015 xs << html::EndTag("div") << html::CR();
3020 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3021 vector<unsigned int> const & clen) const
3023 idx_type const fcell = getFirstCellInRow(row);
3024 idx_type const n = numberOfCellsInRow(row) + fcell;
3027 for (idx_type i = fcell; i < n; ++i) {
3037 for (idx_type i = fcell; i < n; ++i) {
3048 col_type column = cellColumn(i);
3049 int len = clen[column];
3050 while (column < ncols() - 1
3051 && isPartOfMultiColumn(row, ++column))
3052 len += clen[column] + 4;
3053 os << docstring(len, ch);
3068 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3069 vector<unsigned int> const & clen) const
3071 idx_type const fcell = getFirstCellInRow(row);
3072 idx_type const n = numberOfCellsInRow(row) + fcell;
3075 for (idx_type i = fcell; i < n; ++i) {
3076 if (bottomLine(i)) {
3085 for (idx_type i = fcell; i < n; ++i) {
3086 if (bottomLine(i)) {
3096 col_type column = cellColumn(i);
3097 int len = clen[column];
3098 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3099 len += clen[column] + 4;
3100 os << docstring(len, ch);
3101 if (bottomLine(i)) {
3115 void Tabular::plaintextPrintCell(odocstream & os,
3116 OutputParams const & runparams,
3117 idx_type cell, row_type row, col_type column,
3118 vector<unsigned int> const & clen,
3119 bool onlydata) const
3121 odocstringstream sstr;
3122 cellInset(cell)->plaintext(sstr, runparams);
3134 unsigned int len1 = sstr.str().length();
3135 unsigned int len2 = clen[column];
3136 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3137 len2 += clen[column] + 4;
3140 switch (getAlignment(cell)) {
3142 case LYX_ALIGN_LEFT:
3145 case LYX_ALIGN_RIGHT:
3149 case LYX_ALIGN_CENTER:
3155 os << docstring(len1, ' ') << sstr.str()
3156 << docstring(len2, ' ');
3158 if (rightLine(cell))
3165 void Tabular::plaintext(odocstream & os,
3166 OutputParams const & runparams, int const depth,
3167 bool onlydata, char_type delim) const
3169 // first calculate the width of the single columns
3170 vector<unsigned int> clen(ncols());
3173 // first all non multicolumn cells!
3174 for (col_type c = 0; c < ncols(); ++c) {
3176 for (row_type r = 0; r < nrows(); ++r) {
3177 idx_type cell = cellIndex(r, c);
3178 if (isMultiColumn(cell))
3180 odocstringstream sstr;
3181 cellInset(cell)->plaintext(sstr, runparams);
3182 if (clen[c] < sstr.str().length())
3183 clen[c] = sstr.str().length();
3186 // then all multicolumn cells!
3187 for (col_type c = 0; c < ncols(); ++c) {
3188 for (row_type r = 0; r < nrows(); ++r) {
3189 idx_type cell = cellIndex(r, c);
3190 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3192 odocstringstream sstr;
3193 cellInset(cell)->plaintext(sstr, runparams);
3194 int len = int(sstr.str().length());
3195 idx_type const n = columnSpan(cell);
3196 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3198 if (len > int(clen[c + n - 1]))
3199 clen[c + n - 1] = len;
3204 for (row_type r = 0; r < nrows(); ++r) {
3205 if (!onlydata && plaintextTopHLine(os, r, clen))
3206 os << docstring(depth * 2, ' ');
3207 for (col_type c = 0; c < ncols(); ++c) {
3208 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3210 if (onlydata && c > 0)
3211 // we don't use operator<< for single UCS4 character.
3212 // see explanation in docstream.h
3214 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3219 os << docstring(depth * 2, ' ');
3220 if (plaintextBottomHLine(os, r, clen))
3221 os << docstring(depth * 2, ' ');
3227 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3229 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3233 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3234 col_type column) const
3236 return cell_info[row][column].inset;
3240 void Tabular::setCellInset(row_type row, col_type column,
3241 shared_ptr<InsetTableCell> ins) const
3243 CellData & cd = cell_info[row][column];
3248 void Tabular::validate(LaTeXFeatures & features) const
3250 features.require("NeedTabularnewline");
3252 features.require("booktabs");
3253 if (is_long_tabular)
3254 features.require("longtable");
3256 features.require("rotating");
3257 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3258 if (isMultiRow(cell))
3259 features.require("multirow");
3260 if (getVAlignment(cell) != LYX_VALIGN_TOP
3261 || !getPWidth(cell).zero())
3262 features.require("array");
3263 cellInset(cell)->validate(features);
3268 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3270 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3271 ParagraphList::const_iterator cit = parlist.begin();
3272 ParagraphList::const_iterator end = parlist.end();
3274 for (; cit != end; ++cit)
3275 for (int i = 0; i < cit->size(); ++i)
3276 if (cit->isNewline(i))
3283 /////////////////////////////////////////////////////////////////////
3287 /////////////////////////////////////////////////////////////////////
3289 InsetTableCell::InsetTableCell(Buffer * buf)
3290 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3291 contentAlign(LYX_ALIGN_CENTER)
3295 bool InsetTableCell::forcePlainLayout(idx_type) const
3297 return !isFixedWidth;
3301 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3303 return isFixedWidth;
3307 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3308 FuncStatus & status) const
3310 bool enabled = true;
3311 switch (cmd.action()) {
3313 enabled = !forcePlainLayout();
3315 case LFUN_LAYOUT_PARAGRAPH:
3316 enabled = allowParagraphCustomization();
3319 case LFUN_MATH_DISPLAY:
3320 if (!hasFixedWidth()) {
3325 return InsetText::getStatus(cur, cmd, status);
3327 status.setEnabled(enabled);
3331 docstring InsetTableCell::asString(bool intoInsets)
3334 if (paragraphs().empty())
3336 ParagraphList::const_iterator it = paragraphs().begin();
3337 ParagraphList::const_iterator en = paragraphs().end();
3339 for (; it != en; ++it) {
3344 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3350 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3353 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3354 return InsetText::xhtml(xs, rp);
3359 /////////////////////////////////////////////////////////////////////
3363 /////////////////////////////////////////////////////////////////////
3365 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3367 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3368 rowselect_(false), colselect_(false)
3373 InsetTabular::InsetTabular(InsetTabular const & tab)
3374 : Inset(tab), tabular(tab.tabular), scx_(0)
3379 InsetTabular::~InsetTabular()
3381 hideDialogs("tabular", this);
3385 void InsetTabular::setBuffer(Buffer & buf)
3387 tabular.setBuffer(buf);
3388 Inset::setBuffer(buf);
3392 bool InsetTabular::insetAllowed(InsetCode code) const
3397 case MATHMACRO_CODE:
3402 return tabular.is_long_tabular;
3410 void InsetTabular::write(ostream & os) const
3412 os << "Tabular" << endl;
3417 string InsetTabular::contextMenu(BufferView const &, int, int) const
3419 // FIXME: depending on the selection state,
3420 // we could offer a different menu.
3421 return cell(0)->contextMenuName() + ";" + contextMenuName();
3425 string InsetTabular::contextMenuName() const
3427 return "context-tabular";
3431 void InsetTabular::read(Lexer & lex)
3433 //bool const old_format = (lex.getString() == "\\LyXTable");
3441 string token = lex.getString();
3442 while (lex && token != "\\end_inset") {
3444 token = lex.getString();
3447 lex.printError("Missing \\end_inset at this point. ");
3451 int InsetTabular::rowFromY(Cursor & cur, int y) const
3453 // top y coordinate of tabular
3454 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3456 for (; r < tabular.nrows() && y > h; ++r)
3457 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3458 + tabular.interRowSpace(r);
3464 int InsetTabular::columnFromX(Cursor & cur, int x) const
3466 // left x coordinate of tabular
3467 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3469 for (; c < tabular.ncols() && x > w; ++c)
3470 w += tabular.cellWidth(c);
3475 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3477 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3478 // mi.base.textwidth << "\n";
3481 LASSERT(false, /**/);
3484 for (row_type r = 0; r < tabular.nrows(); ++r) {
3487 for (col_type c = 0; c < tabular.ncols(); ++c) {
3488 if (tabular.isPartOfMultiColumn(r, c)
3489 || tabular.isPartOfMultiRow(r, c))
3490 // multicolumn or multirow cell, but not first one
3492 idx_type const cell = tabular.cellIndex(r, c);
3495 Length const p_width = tabular.getPWidth(cell);
3496 if (!p_width.zero())
3497 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3498 tabular.cellInset(cell)->metrics(m, dim);
3499 if (!p_width.zero())
3500 dim.wid = m.base.textwidth;
3501 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3502 + tabular.interColumnSpace(cell);
3504 // FIXME(?): do we need a second metrics call?
3505 TextMetrics const & tm =
3506 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3508 // determine horizontal offset because of decimal align (if necessary)
3509 int decimal_width = 0;
3510 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3511 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3512 tail.setBuffer(tabular.buffer());
3513 // we need to set macrocontext position everywhere
3514 // otherwise we crash with nested insets (e.g. footnotes)
3515 // after decimal point
3516 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3518 dit.push_back(CursorSlice(tail));
3519 tail.setMacrocontextPositionRecursive(dit);
3521 // remove text leading decimal point
3522 docstring const align_d = tabular.column_info[c].decimal_point;
3523 dit = separatorPos(&tail, align_d);
3525 pit_type const psize = tail.paragraphs().front().size();
3527 tail.paragraphs().front().eraseChars(0,
3528 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3530 tail.metrics(m, dim1);
3531 decimal_width = dim1.width();
3534 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3535 tabular.cell_info[r][c].decimal_width = decimal_width;
3537 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3538 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3539 int const lastpardes = tm.last().second->descent()
3540 + TEXT_TO_INSET_OFFSET;
3542 switch (tabular.getVAlignment(cell)) {
3543 case Tabular::LYX_VALIGN_TOP:
3545 case Tabular::LYX_VALIGN_MIDDLE:
3546 offset = -(dim.des - lastpardes)/2;
3548 case Tabular::LYX_VALIGN_BOTTOM:
3549 offset = -(dim.des - lastpardes);
3552 tabular.cell_info[r][c].voffset = offset;
3553 maxasc = max(maxasc, dim.asc - offset);
3554 maxdes = max(maxdes, dim.des + offset);
3556 int const top_space = tabular.row_info[r].top_space_default ?
3557 default_line_space :
3558 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3559 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3560 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3561 default_line_space :
3562 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3563 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3566 // for top-alignment the first horizontal table line must be exactly at
3567 // the position of the base line of the surrounding text line
3568 // for bottom alignment, the same is for the last table line
3569 switch (tabular.tabular_valignment) {
3570 case Tabular::LYX_VALIGN_BOTTOM:
3571 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3573 case Tabular::LYX_VALIGN_MIDDLE:
3574 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3576 case Tabular::LYX_VALIGN_TOP:
3577 offset_valign_ = tabular.rowAscent(0);
3581 tabular.updateColumnWidths();
3582 dim.asc = tabular.rowAscent(0) - offset_valign_;
3583 dim.des = tabular.height() - dim.asc;
3584 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3588 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3591 if (&cur.inset() == this && cur.selection()) {
3592 if (cur.selIsMultiCell()) {
3595 getSelection(cur, rs, re, cs, ce);
3597 idx_type const cell = tabular.cellIndex(row, col);
3598 col_type const cspan = tabular.columnSpan(cell);
3599 row_type const rspan = tabular.rowSpan(cell);
3600 if (col + cspan - 1 >= cs && col <= ce
3601 && row + rspan - 1 >= rs && row <= re)
3604 if (col == tabular.cellColumn(cur.idx())
3605 && row == tabular.cellRow(cur.idx())) {
3606 CursorSlice const & beg = cur.selBegin();
3607 CursorSlice const & end = cur.selEnd();
3609 if ((end.lastpos() > 0 || end.lastpit() > 0)
3610 && end.pos() == end.lastpos() && beg.pos() == 0
3611 && end.pit() == end.lastpit() && beg.pit() == 0)
3619 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3621 x += scx_ + ADD_TO_TABULAR_WIDTH;
3623 BufferView * bv = pi.base.bv;
3624 Cursor & cur = pi.base.bv->cursor();
3627 // FIXME: As the full background is painted in drawBackground(),
3628 // we have no choice but to do a full repaint for the Text cells.
3629 pi.full_repaint = true;
3631 bool const original_selection_state = pi.selected;
3634 first_visible_cell = Tabular::npos;
3636 int yy = y + offset_valign_;
3637 for (row_type r = 0; r < tabular.nrows(); ++r) {
3639 for (col_type c = 0; c < tabular.ncols(); ++c) {
3640 if (tabular.isPartOfMultiColumn(r, c))
3643 idx = tabular.cellIndex(r, c);
3645 if (tabular.isPartOfMultiRow(r, c)) {
3646 nx += tabular.cellWidth(idx);
3650 if (first_visible_cell == Tabular::npos)
3651 first_visible_cell = idx;
3653 pi.selected |= isCellSelected(cur, r, c);
3654 int const cx = nx + tabular.textHOffset(idx);
3655 int const cy = yy + tabular.textVOffset(idx);
3656 // Cache the Inset position.
3657 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3658 cell(idx)->draw(pi, cx, cy);
3659 drawCellLines(pi, nx, yy, r, idx);
3660 nx += tabular.cellWidth(idx);
3661 pi.selected = original_selection_state;
3664 if (r + 1 < tabular.nrows())
3665 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3666 + tabular.interRowSpace(r + 1);
3671 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3673 x += scx_ + ADD_TO_TABULAR_WIDTH;
3674 y += offset_valign_ - tabular.rowAscent(0);
3675 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3676 pi.backgroundColor(this));
3680 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3682 Cursor & cur = pi.base.bv->cursor();
3685 x += scx_ + ADD_TO_TABULAR_WIDTH;
3687 if (!cur.selection())
3689 if (&cur.inset() != this)
3694 bool const full_cell_selected = isCellSelected(cur,
3695 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3697 if (cur.selIsMultiCell() || full_cell_selected) {
3698 for (row_type r = 0; r < tabular.nrows(); ++r) {
3700 for (col_type c = 0; c < tabular.ncols(); ++c) {
3701 if (tabular.isPartOfMultiColumn(r, c))
3704 idx_type const cell = tabular.cellIndex(r, c);
3706 if (tabular.isPartOfMultiRow(r, c)) {
3707 xx += tabular.cellWidth(cell);
3710 int const w = tabular.cellWidth(cell);
3711 int const h = tabular.cellHeight(cell);
3712 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3713 if (isCellSelected(cur, r, c))
3714 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3717 if (r + 1 < tabular.nrows())
3718 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3719 + tabular.interRowSpace(r + 1);
3723 // FIXME: This code has no effect because InsetTableCell does not handle
3724 // drawSelection other than the trivial implementation in Inset.
3726 // x += cellXPos(cur.idx());
3727 // x += tabular.textHOffset(cur.idx());
3728 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3733 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3734 row_type row, idx_type cell) const
3736 y -= tabular.rowAscent(row);
3737 int const w = tabular.cellWidth(cell);
3738 int const h = tabular.cellHeight(cell);
3739 Color const linecolor = pi.textColor(Color_tabularline);
3740 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3743 bool drawline = tabular.topLine(cell)
3744 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3745 pi.pain.line(x, y, x + w, y,
3746 drawline ? linecolor : gridcolor,
3747 drawline ? Painter::line_solid : Painter::line_onoffdash);
3750 drawline = tabular.bottomLine(cell);
3751 pi.pain.line(x, y + h, x + w, y + h,
3752 drawline ? linecolor : gridcolor,
3753 drawline ? Painter::line_solid : Painter::line_onoffdash);
3756 col_type const col = tabular.cellColumn(cell);
3757 drawline = tabular.leftLine(cell)
3758 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3759 pi.pain.line(x, y, x, y + h,
3760 drawline ? linecolor : gridcolor,
3761 drawline ? Painter::line_solid : Painter::line_onoffdash);
3764 x -= tabular.interColumnSpace(cell);
3765 drawline = tabular.rightLine(cell)
3766 || (col + 1 < tabular.ncols()
3767 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3768 pi.pain.line(x + w, y, x + w, y + h,
3769 drawline ? linecolor : gridcolor,
3770 drawline ? Painter::line_solid : Painter::line_onoffdash);
3774 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3776 //lyxerr << "InsetTabular::edit: " << this << endl;
3778 cur.setSelection(false);
3781 if (isRightToLeft(cur))
3782 cur.idx() = tabular.getLastCellInRow(0);
3788 if (isRightToLeft(cur))
3789 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3791 cur.idx() = tabular.numberofcells - 1;
3793 cur.pos() = cur.lastpos(); // FIXME crude guess
3795 cur.setCurrentFont();
3796 // FIXME: this accesses the position cache before it is initialized
3798 //cur.bv().fitCursor();
3802 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3804 // In a longtable, tell captions what the current float is
3805 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3806 string const saveflt = cnts.current_float();
3807 if (tabular.is_long_tabular)
3808 cnts.current_float("table");
3810 ParIterator it2 = it;
3812 size_t const end = it2.nargs();
3813 for ( ; it2.idx() < end; it2.top().forwardIdx())
3814 buffer().updateBuffer(it2, utype);
3817 if (tabular.is_long_tabular)
3818 cnts.current_float(saveflt);
3822 void InsetTabular::addToToc(DocIterator const & cpit) const
3824 DocIterator dit = cpit;
3826 size_t const end = dit.nargs();
3827 for ( ; dit.idx() < end; dit.top().forwardIdx())
3828 cell(dit.idx())->addToToc(dit);
3832 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3834 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3835 << "\n cur:" << cur);
3836 CursorSlice sl = cur.top();
3837 Cursor & bvcur = cur.bv().cursor();
3839 FuncCode const act = cmd.action();
3843 case LFUN_MOUSE_PRESS: {
3844 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3846 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3847 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3848 row_type r = rowFromY(cur, cmd.y());
3849 cur.idx() = tabular.getFirstCellInRow(r);
3852 cur.idx() = tabular.getLastCellInRow(r);
3853 cur.pos() = cur.lastpos();
3854 cur.setSelection(true);
3860 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3861 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3862 || cmd.y() > y0 + tabular.height()) {
3863 col_type c = columnFromX(cur, cmd.x());
3864 cur.idx() = tabular.cellIndex(0, c);
3867 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3868 cur.pos() = cur.lastpos();
3869 cur.setSelection(true);
3874 // do not reset cursor/selection if we have selected
3875 // some cells (bug 2715).
3876 if (cmd.button() == mouse_button::button3
3877 && &bvcur.selBegin().inset() == this
3878 && bvcur.selIsMultiCell())
3881 // Let InsetTableCell do it
3882 cell(cur.idx())->dispatch(cur, cmd);
3885 case LFUN_MOUSE_MOTION:
3886 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3887 if (cmd.button() == mouse_button::button1) {
3888 // only accept motions to places not deeper nested than the real anchor
3889 if (!bvcur.realAnchor().hasPart(cur)) {
3893 // select (additional) row
3895 row_type r = rowFromY(cur, cmd.y());
3896 cur.idx() = tabular.getLastCellInRow(r);
3897 // we need to reset the cursor's pit and pos now, as the old ones
3898 // may no longer be valid.
3901 bvcur.setCursor(cur);
3902 bvcur.setSelection(true);
3905 // select (additional) column
3907 col_type c = columnFromX(cur, cmd.x());
3908 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3909 // we need to reset the cursor's pit and pos now, as the old ones
3910 // may no longer be valid.
3913 bvcur.setCursor(cur);
3914 bvcur.setSelection(true);
3917 // only update if selection changes
3918 if (bvcur.idx() == cur.idx() &&
3919 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3920 cur.noScreenUpdate();
3921 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3922 bvcur.setCursor(cur);
3923 bvcur.setSelection(true);
3924 // if this is a multicell selection, we just set the cursor to
3925 // the beginning of the cell's text.
3926 if (bvcur.selIsMultiCell()) {
3927 bvcur.pit() = bvcur.lastpit();
3928 bvcur.pos() = bvcur.lastpos();
3933 case LFUN_MOUSE_RELEASE:
3938 case LFUN_CELL_BACKWARD:
3940 cur.setSelection(false);
3943 case LFUN_CELL_FORWARD:
3945 cur.setSelection(false);
3948 case LFUN_CHAR_FORWARD_SELECT:
3949 case LFUN_CHAR_FORWARD:
3950 case LFUN_CHAR_BACKWARD_SELECT:
3951 case LFUN_CHAR_BACKWARD:
3952 case LFUN_CHAR_RIGHT_SELECT:
3953 case LFUN_CHAR_RIGHT:
3954 case LFUN_CHAR_LEFT_SELECT:
3955 case LFUN_CHAR_LEFT:
3956 case LFUN_WORD_FORWARD:
3957 case LFUN_WORD_FORWARD_SELECT:
3958 case LFUN_WORD_BACKWARD:
3959 case LFUN_WORD_BACKWARD_SELECT:
3960 case LFUN_WORD_RIGHT:
3961 case LFUN_WORD_RIGHT_SELECT:
3962 case LFUN_WORD_LEFT:
3963 case LFUN_WORD_LEFT_SELECT: {
3964 // determine whether we move to next or previous cell, where to enter
3965 // the new cell from, and which command to "finish" (i.e., exit the
3968 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3969 FuncCode finish_lfun;
3971 if (act == LFUN_CHAR_FORWARD
3972 || act == LFUN_CHAR_FORWARD_SELECT
3973 || act == LFUN_WORD_FORWARD
3974 || act == LFUN_WORD_FORWARD_SELECT) {
3976 finish_lfun = LFUN_FINISHED_FORWARD;
3978 else if (act == LFUN_CHAR_BACKWARD
3979 || act == LFUN_CHAR_BACKWARD_SELECT
3980 || act == LFUN_WORD_BACKWARD
3981 || act == LFUN_WORD_BACKWARD_SELECT) {
3983 finish_lfun = LFUN_FINISHED_BACKWARD;
3985 // LEFT or RIGHT commands --- the interpretation will depend on the
3986 // table's direction.
3988 bool const right = act == LFUN_CHAR_RIGHT
3989 || act == LFUN_CHAR_RIGHT_SELECT
3990 || act == LFUN_WORD_RIGHT
3991 || act == LFUN_WORD_RIGHT_SELECT;
3992 next_cell = isRightToLeft(cur) != right;
3994 if (lyxrc.visual_cursor)
3995 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3997 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
4000 bool const select = act == LFUN_CHAR_FORWARD_SELECT
4001 || act == LFUN_CHAR_BACKWARD_SELECT
4002 || act == LFUN_CHAR_RIGHT_SELECT
4003 || act == LFUN_CHAR_LEFT_SELECT
4004 || act == LFUN_WORD_FORWARD_SELECT
4005 || act == LFUN_WORD_RIGHT_SELECT
4006 || act == LFUN_WORD_BACKWARD_SELECT
4007 || act == LFUN_WORD_LEFT_SELECT;
4009 // If we have a multicell selection or we're
4010 // not doing some LFUN_*_SELECT thing anyway...
4011 if (!cur.selIsMultiCell() || !select) {
4012 col_type const c = tabular.cellColumn(cur.idx());
4013 row_type const r = tabular.cellRow(cur.idx());
4014 // Are we trying to select the whole cell and is the whole cell
4015 // not yet selected?
4016 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4017 ((next_cell && cur.pit() == cur.lastpit()
4018 && cur.pos() == cur.lastpos())
4019 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4021 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4023 // ...try to dispatch to the cell's inset.
4024 cell(cur.idx())->dispatch(cur, cmd);
4026 // When we already have a selection we want to select the whole cell
4027 // before going to the next cell.
4028 if (select_whole && !empty_cell){
4029 getText(cur.idx())->selectAll(cur);
4034 // FIXME: When we support the selection of an empty cell, remove
4035 // the !empty_cell from this condition. For now we jump to the next
4036 // cell if the current cell is empty.
4037 if (cur.result().dispatched() && !empty_cell)
4041 // move to next/prev cell, as appropriate
4042 // note that we will always do this if we're selecting and we have
4043 // a multicell selection
4044 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4045 << " cell from: " << int(entry_from));
4047 moveNextCell(cur, entry_from);
4049 movePrevCell(cur, entry_from);
4050 // if we're exiting the table, call the appropriate FINISHED lfun
4051 if (sl == cur.top())
4052 cmd = FuncRequest(finish_lfun);
4056 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4061 case LFUN_DOWN_SELECT:
4063 if (!(cur.selection() && cur.selIsMultiCell()))
4064 cell(cur.idx())->dispatch(cur, cmd);
4066 cur.dispatched(); // override the cell's decision
4067 if (sl == cur.top()) {
4068 // if our Text didn't do anything to the cursor
4069 // then we try to put the cursor into the cell below
4070 // setting also the right targetX.
4071 cur.selHandle(act == LFUN_DOWN_SELECT);
4072 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4073 int const xtarget = cur.targetX();
4074 // WARNING: Once cur.idx() has been reset, the cursor is in
4075 // an inconsistent state until pos() has been set. Be careful
4076 // what you do with it!
4077 cur.idx() = tabular.cellBelow(cur.idx());
4079 TextMetrics const & tm =
4080 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4081 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4082 cur.setCurrentFont();
4085 if (sl == cur.top()) {
4086 // we trick it to go to forward after leaving the
4088 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4091 if (cur.selIsMultiCell()) {
4092 cur.pit() = cur.lastpit();
4093 cur.pos() = cur.lastpos();
4094 cur.setCurrentFont();
4097 cur.screenUpdateFlags(Update::FitCursor);
4100 case LFUN_UP_SELECT:
4102 if (!(cur.selection() && cur.selIsMultiCell()))
4103 cell(cur.idx())->dispatch(cur, cmd);
4104 cur.dispatched(); // override the cell's decision
4105 if (sl == cur.top()) {
4106 // if our Text didn't do anything to the cursor
4107 // then we try to put the cursor into the cell above
4108 // setting also the right targetX.
4109 cur.selHandle(act == LFUN_UP_SELECT);
4110 if (tabular.cellRow(cur.idx()) != 0) {
4111 int const xtarget = cur.targetX();
4112 // WARNING: Once cur.idx() has been reset, the cursor is in
4113 // an inconsistent state until pos() has been set. Be careful
4114 // what you do with it!
4115 cur.idx() = tabular.cellAbove(cur.idx());
4116 cur.pit() = cur.lastpit();
4117 Text const * text = cell(cur.idx())->getText(0);
4118 TextMetrics const & tm = cur.bv().textMetrics(text);
4119 ParagraphMetrics const & pm =
4120 tm.parMetrics(cur.lastpit());
4121 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4122 cur.setCurrentFont();
4125 if (sl == cur.top()) {
4126 cmd = FuncRequest(LFUN_UP);
4129 if (cur.selIsMultiCell()) {
4131 cur.pos() = cur.lastpos();
4132 cur.setCurrentFont();
4135 cur.screenUpdateFlags(Update::FitCursor);
4138 // case LFUN_SCREEN_DOWN: {
4139 // //if (hasSelection())
4140 // // cur.selection() = false;
4141 // col_type const col = tabular.cellColumn(cur.idx());
4142 // int const t = cur.bv().top_y() + cur.bv().height();
4143 // if (t < yo() + tabular.getHeightOfTabular()) {
4144 // cur.bv().scrollDocView(t, true);
4145 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4147 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4154 // case LFUN_SCREEN_UP: {
4155 // //if (hasSelection())
4156 // // cur.selection() = false;
4157 // col_type const col = tabular.cellColumn(cur.idx());
4158 // int const t = cur.bv().top_y() + cur.bv().height();
4160 // cur.bv().scrollDocView(t, true);
4164 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4168 // cur.par() = cur.lastpar();
4169 // cur.pos() = cur.lastpos();
4173 case LFUN_LAYOUT_TABULAR:
4174 cur.bv().showDialog("tabular");
4177 case LFUN_INSET_MODIFY: {
4179 if (cmd.getArg(1) == "from-dialog")
4180 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4182 arg = to_utf8(cmd.argument());
4183 if (!tabularFeatures(cur, arg))
4188 // insert file functions
4189 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4190 case LFUN_FILE_INSERT_PLAINTEXT:
4192 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4193 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4194 FileName(to_utf8(cmd.argument())));
4197 cur.recordUndoInset(INSERT_UNDO);
4198 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4199 // content has been replaced,
4200 // so cursor might be invalid
4201 cur.pos() = cur.lastpos();
4202 cur.pit() = cur.lastpit();
4203 bvcur.setCursor(cur);
4210 if (cur.selIsMultiCell()) {
4211 if (copySelection(cur)) {
4212 cur.recordUndoInset(DELETE_UNDO);
4216 cell(cur.idx())->dispatch(cur, cmd);
4219 case LFUN_SELF_INSERT:
4220 if (cur.selIsMultiCell()) {
4221 cur.recordUndoInset(DELETE_UNDO);
4224 cell(cur.idx())->dispatch(cur, cmd);
4227 case LFUN_CHAR_DELETE_BACKWARD:
4228 case LFUN_CHAR_DELETE_FORWARD:
4229 if (cur.selIsMultiCell()) {
4230 cur.recordUndoInset(DELETE_UNDO);
4233 cell(cur.idx())->dispatch(cur, cmd);
4237 if (!cur.selection())
4239 if (cur.selIsMultiCell()) {
4243 cell(cur.idx())->dispatch(cur, cmd);
4246 case LFUN_CLIPBOARD_PASTE:
4247 case LFUN_PRIMARY_SELECTION_PASTE: {
4248 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4249 theClipboard().getAsText() :
4250 theSelection().get();
4253 // pass to InsertPlaintextString, but
4254 // only if we have multi-cell content
4255 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4256 cur.recordUndoInset(INSERT_UNDO);
4257 if (insertPlaintextString(cur.bv(), clip, false)) {
4258 // content has been replaced,
4259 // so cursor might be invalid
4260 cur.pos() = cur.lastpos();
4261 cur.pit() = cur.lastpit();
4262 bvcur.setCursor(cur);
4266 // Let the cell handle normal text
4267 cell(cur.idx())->dispatch(cur, cmd);
4272 if (!tabularStackDirty()) {
4273 if (!cur.selIsMultiCell())
4274 cell(cur.idx())->dispatch(cur, cmd);
4277 if (theClipboard().isInternal()) {
4278 cur.recordUndoInset(INSERT_UNDO);
4279 pasteClipboard(cur);
4283 case LFUN_FONT_EMPH:
4284 case LFUN_FONT_BOLD:
4285 case LFUN_FONT_BOLDSYMBOL:
4286 case LFUN_FONT_ROMAN:
4287 case LFUN_FONT_NOUN:
4288 case LFUN_FONT_ITAL:
4289 case LFUN_FONT_FRAK:
4290 case LFUN_FONT_TYPEWRITER:
4291 case LFUN_FONT_SANS:
4292 case LFUN_TEXTSTYLE_APPLY:
4293 case LFUN_TEXTSTYLE_UPDATE:
4294 case LFUN_FONT_SIZE:
4295 case LFUN_FONT_UNDERLINE:
4296 case LFUN_FONT_STRIKEOUT:
4297 case LFUN_FONT_UULINE:
4298 case LFUN_FONT_UWAVE:
4300 case LFUN_WORD_CAPITALIZE:
4301 case LFUN_WORD_UPCASE:
4302 case LFUN_WORD_LOWCASE:
4303 case LFUN_CHARS_TRANSPOSE:
4304 if (cur.selIsMultiCell()) {
4307 getSelection(cur, rs, re, cs, ce);
4308 Cursor tmpcur = cur;
4309 for (row_type r = rs; r <= re; ++r) {
4310 for (col_type c = cs; c <= ce; ++c) {
4311 // cursor follows cell:
4312 tmpcur.idx() = tabular.cellIndex(r, c);
4313 // select this cell only:
4316 tmpcur.resetAnchor();
4317 tmpcur.pit() = tmpcur.lastpit();
4318 tmpcur.pos() = tmpcur.top().lastpos();
4319 tmpcur.setCursor(tmpcur);
4320 tmpcur.setSelection();
4321 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4326 cell(cur.idx())->dispatch(cur, cmd);
4330 case LFUN_INSET_SETTINGS:
4331 // relay this lfun to Inset, not to the cell.
4332 Inset::doDispatch(cur, cmd);
4336 // we try to handle this event in the insets dispatch function.
4337 cell(cur.idx())->dispatch(cur, cmd);
4343 // function sets an object as defined in func_status.h:
4344 // states OK, Unknown, Disabled, On, Off.
4345 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4346 FuncStatus & status) const
4348 switch (cmd.action()) {
4349 case LFUN_INSET_MODIFY: {
4350 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4353 // FIXME: We only check for the very first argument...
4354 string const s = cmd.getArg(1);
4355 // We always enable the lfun if it is coming from the dialog
4356 // because the dialog makes sure all the settings are valid,
4357 // even though the first argument might not be valid now.
4358 if (s == "from-dialog") {
4359 status.setEnabled(true);
4363 int action = Tabular::LAST_ACTION;
4365 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4366 if (tabularFeature[i].feature == s) {
4367 action = tabularFeature[i].action;
4371 if (action == Tabular::LAST_ACTION) {
4373 status.setUnknown(true);
4377 string const argument = cmd.getLongArg(2);
4379 row_type sel_row_start = 0;
4380 row_type sel_row_end = 0;
4381 col_type sel_col_start = 0;
4382 col_type sel_col_end = 0;
4383 Tabular::ltType dummyltt;
4386 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4389 case Tabular::SET_PWIDTH:
4390 case Tabular::SET_MPWIDTH:
4391 case Tabular::SET_SPECIAL_COLUMN:
4392 case Tabular::SET_SPECIAL_MULTICOLUMN:
4393 case Tabular::APPEND_ROW:
4394 case Tabular::APPEND_COLUMN:
4395 case Tabular::DELETE_ROW:
4396 case Tabular::DELETE_COLUMN:
4397 case Tabular::COPY_ROW:
4398 case Tabular::COPY_COLUMN:
4399 case Tabular::SET_TOP_SPACE:
4400 case Tabular::SET_BOTTOM_SPACE:
4401 case Tabular::SET_INTERLINE_SPACE:
4405 case Tabular::SET_TABULAR_WIDTH:
4406 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4407 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4410 case Tabular::SET_DECIMAL_POINT:
4412 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4415 case Tabular::SET_MULTICOLUMN:
4416 case Tabular::UNSET_MULTICOLUMN:
4417 case Tabular::MULTICOLUMN:
4418 // If a row is set as longtable caption, it must not be allowed
4419 // to unset that this row is a multicolumn.
4420 status.setEnabled(sel_row_start == sel_row_end
4421 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4422 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4425 case Tabular::SET_MULTIROW:
4426 case Tabular::UNSET_MULTIROW:
4427 case Tabular::MULTIROW:
4428 // If a row is set as longtable caption, it must not be allowed
4429 // to unset that this row is a multirow.
4430 status.setEnabled(sel_col_start == sel_col_end
4431 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4432 status.setOnOff(tabular.isMultiRow(cur.idx()));
4435 case Tabular::SET_ALL_LINES:
4436 case Tabular::UNSET_ALL_LINES:
4437 case Tabular::SET_BORDER_LINES:
4438 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4441 case Tabular::SET_LINE_TOP:
4442 case Tabular::SET_LINE_BOTTOM:
4443 case Tabular::SET_LINE_LEFT:
4444 case Tabular::SET_LINE_RIGHT:
4445 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4448 case Tabular::TOGGLE_LINE_TOP:
4449 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4450 status.setOnOff(tabular.topLine(cur.idx()));
4453 case Tabular::TOGGLE_LINE_BOTTOM:
4454 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4455 status.setOnOff(tabular.bottomLine(cur.idx()));
4458 case Tabular::TOGGLE_LINE_LEFT:
4459 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4460 status.setOnOff(tabular.leftLine(cur.idx()));
4463 case Tabular::TOGGLE_LINE_RIGHT:
4464 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4465 status.setOnOff(tabular.rightLine(cur.idx()));
4468 // multirow cells only inherit the alignment of the column if the column has
4469 // no width, otherwise they are left-aligned
4470 // therefore allow always left but right and center only if there is no width
4471 case Tabular::M_ALIGN_LEFT:
4473 case Tabular::ALIGN_LEFT:
4474 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4477 case Tabular::M_ALIGN_RIGHT:
4479 case Tabular::ALIGN_RIGHT:
4480 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4481 && !tabular.getPWidth(cur.idx()).zero()));
4482 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4485 case Tabular::M_ALIGN_CENTER:
4487 case Tabular::ALIGN_CENTER:
4488 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4489 && !tabular.getPWidth(cur.idx()).zero()));
4490 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4493 case Tabular::ALIGN_BLOCK:
4494 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4495 && !tabular.isMultiRow(cur.idx()));
4496 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4499 case Tabular::ALIGN_DECIMAL:
4500 status.setEnabled(!tabular.isMultiRow(cur.idx())
4501 && !tabular.isMultiColumn(cur.idx()));
4502 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4505 case Tabular::M_VALIGN_TOP:
4507 case Tabular::VALIGN_TOP:
4508 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4509 && !tabular.isMultiRow(cur.idx()));
4511 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4514 case Tabular::M_VALIGN_BOTTOM:
4516 case Tabular::VALIGN_BOTTOM:
4517 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4518 && !tabular.isMultiRow(cur.idx()));
4520 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4523 case Tabular::M_VALIGN_MIDDLE:
4525 case Tabular::VALIGN_MIDDLE:
4526 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4527 && !tabular.isMultiRow(cur.idx()));
4529 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4532 case Tabular::SET_LONGTABULAR:
4533 // setting as longtable is not allowed when table is inside a float
4534 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4535 || cur.innerInsetOfType(WRAP_CODE) != 0)
4536 status.setEnabled(false);
4538 status.setEnabled(true);
4539 status.setOnOff(tabular.is_long_tabular);
4542 case Tabular::UNSET_LONGTABULAR:
4543 status.setOnOff(!tabular.is_long_tabular);
4546 case Tabular::TOGGLE_ROTATE_TABULAR:
4547 case Tabular::SET_ROTATE_TABULAR:
4548 status.setOnOff(tabular.rotate != 0);
4551 case Tabular::TABULAR_VALIGN_TOP:
4552 status.setEnabled(tabular.tabular_width.zero());
4553 status.setOnOff(tabular.tabular_valignment
4554 == Tabular::LYX_VALIGN_TOP);
4556 case Tabular::TABULAR_VALIGN_MIDDLE:
4557 status.setEnabled(tabular.tabular_width.zero());
4558 status.setOnOff(tabular.tabular_valignment
4559 == Tabular::LYX_VALIGN_MIDDLE);
4561 case Tabular::TABULAR_VALIGN_BOTTOM:
4562 status.setEnabled(tabular.tabular_width.zero());
4563 status.setOnOff(tabular.tabular_valignment
4564 == Tabular::LYX_VALIGN_BOTTOM);
4567 case Tabular::LONGTABULAR_ALIGN_LEFT:
4568 status.setOnOff(tabular.longtabular_alignment
4569 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4571 case Tabular::LONGTABULAR_ALIGN_CENTER:
4572 status.setOnOff(tabular.longtabular_alignment
4573 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4575 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4576 status.setOnOff(tabular.longtabular_alignment
4577 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4580 case Tabular::UNSET_ROTATE_TABULAR:
4581 status.setOnOff(tabular.rotate == 0);
4584 case Tabular::TOGGLE_ROTATE_CELL:
4585 case Tabular::SET_ROTATE_CELL:
4586 status.setOnOff(!oneCellHasRotationState(false,
4587 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4590 case Tabular::UNSET_ROTATE_CELL:
4591 status.setOnOff(!oneCellHasRotationState(true,
4592 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4595 case Tabular::SET_USEBOX:
4596 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4599 // every row can only be one thing:
4600 // either a footer or header
4601 case Tabular::SET_LTFIRSTHEAD:
4602 status.setEnabled(sel_row_start == sel_row_end);
4603 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4606 case Tabular::UNSET_LTFIRSTHEAD:
4607 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4608 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4611 case Tabular::SET_LTHEAD:
4612 status.setEnabled(sel_row_start == sel_row_end);
4613 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4616 case Tabular::UNSET_LTHEAD:
4617 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4618 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4621 case Tabular::SET_LTFOOT:
4622 status.setEnabled(sel_row_start == sel_row_end);
4623 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4626 case Tabular::UNSET_LTFOOT:
4627 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4628 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4631 case Tabular::SET_LTLASTFOOT:
4632 status.setEnabled(sel_row_start == sel_row_end);
4633 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4636 case Tabular::UNSET_LTLASTFOOT:
4637 status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
4638 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4641 case Tabular::SET_LTNEWPAGE:
4642 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4644 case Tabular::UNSET_LTNEWPAGE:
4645 status.setOnOff(!tabular.getLTNewPage(sel_row_start));
4648 // only one row in head/firsthead/foot/lasthead can be the caption
4649 // and a multirow cannot be set as caption
4650 case Tabular::SET_LTCAPTION:
4651 status.setEnabled(sel_row_start == sel_row_end
4652 && (!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4653 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4654 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4655 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4656 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4657 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4658 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4659 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT))
4660 && !tabular.isMultiRow(sel_row_start));
4661 status.setOnOff(tabular.ltCaption(sel_row_start));
4664 case Tabular::UNSET_LTCAPTION:
4665 status.setEnabled(sel_row_start == sel_row_end && tabular.ltCaption(sel_row_start));
4668 case Tabular::TOGGLE_LTCAPTION:
4669 status.setEnabled(sel_row_start == sel_row_end && (tabular.ltCaption(sel_row_start)
4670 || ((!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4671 || !tabular.haveLTCaption(Tabular::CAPTION_FIRSTHEAD))
4672 && (!tabular.getRowOfLTHead(sel_row_start, dummyltt)
4673 || !tabular.haveLTCaption(Tabular::CAPTION_HEAD))
4674 && (!tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4675 || !tabular.haveLTCaption(Tabular::CAPTION_FOOT))
4676 && (!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4677 || !tabular.haveLTCaption(Tabular::CAPTION_LASTFOOT)))));
4678 status.setOnOff(tabular.ltCaption(sel_row_start));
4681 case Tabular::SET_BOOKTABS:
4682 status.setOnOff(tabular.use_booktabs);
4685 case Tabular::UNSET_BOOKTABS:
4686 status.setOnOff(!tabular.use_booktabs);
4691 status.setEnabled(false);
4697 // These are only enabled inside tabular
4698 case LFUN_CELL_BACKWARD:
4699 case LFUN_CELL_FORWARD:
4700 status.setEnabled(true);
4703 // disable these with multiple cells selected
4704 case LFUN_INSET_INSERT:
4705 case LFUN_TABULAR_INSERT:
4706 case LFUN_FLEX_INSERT:
4707 case LFUN_FLOAT_INSERT:
4708 case LFUN_FLOAT_WIDE_INSERT:
4709 case LFUN_FOOTNOTE_INSERT:
4710 case LFUN_MARGINALNOTE_INSERT:
4711 case LFUN_MATH_INSERT:
4712 case LFUN_MATH_MODE:
4713 case LFUN_MATH_MUTATE:
4714 case LFUN_MATH_DISPLAY:
4715 case LFUN_NOTE_INSERT:
4716 case LFUN_ARGUMENT_INSERT:
4717 case LFUN_BOX_INSERT:
4718 case LFUN_BRANCH_INSERT:
4719 case LFUN_PHANTOM_INSERT:
4720 case LFUN_WRAP_INSERT:
4721 case LFUN_PREVIEW_INSERT:
4722 case LFUN_ERT_INSERT: {
4723 if (cur.selIsMultiCell()) {
4724 status.setEnabled(false);
4727 return cell(cur.idx())->getStatus(cur, cmd, status);
4730 // disable in non-fixed-width cells
4731 case LFUN_BREAK_PARAGRAPH:
4732 // multirow does not allow paragraph breaks
4733 if (tabular.isMultiRow(cur.idx())) {
4734 status.setEnabled(false);
4737 case LFUN_NEWLINE_INSERT: {
4738 if (tabular.getPWidth(cur.idx()).zero()) {
4739 status.setEnabled(false);
4742 return cell(cur.idx())->getStatus(cur, cmd, status);
4745 case LFUN_NEWPAGE_INSERT:
4746 status.setEnabled(false);
4750 if (tabularStackDirty() && theClipboard().isInternal()) {
4751 if (cur.selIsMultiCell()) {
4754 getSelection(cur, rs, re, cs, ce);
4755 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4756 && paste_tabular->nrows() == re - rs + 1)
4757 status.setEnabled(true);
4759 status.setEnabled(false);
4760 status.message(_("Selection size should match clipboard content."));
4763 status.setEnabled(true);
4766 return cell(cur.idx())->getStatus(cur, cmd, status);
4768 case LFUN_INSET_SETTINGS:
4769 // relay this lfun to Inset, not to the cell.
4770 return Inset::getStatus(cur, cmd, status);
4773 // we try to handle this event in the insets dispatch function.
4774 return cell(cur.idx())->getStatus(cur, cmd, status);
4780 Inset::DisplayType InsetTabular::display() const
4782 if (tabular.is_long_tabular) {
4783 switch (tabular.longtabular_alignment) {
4784 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4786 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4788 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4798 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4800 tabular.latex(os, runparams);
4804 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4806 os << '\n'; // output table on a new line
4807 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4808 tabular.plaintext(os, runparams, dp, false, 0);
4809 return PLAINTEXT_NEWLINE;
4813 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4818 // FIXME: Why not pass a proper DocIterator here?
4820 // if the table is inside a float it doesn't need the informaltable
4821 // wrapper. Search for it.
4822 for (master = owner(); master; master = master->owner())
4823 if (master->lyxCode() == FLOAT_CODE)
4828 os << "<informaltable>";
4831 ret += tabular.docbook(os, runparams);
4833 os << "</informaltable>";
4840 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4842 return tabular.xhtml(xs, rp);
4846 void InsetTabular::validate(LaTeXFeatures & features) const
4848 tabular.validate(features);
4850 // It'd be better to be able to get this from an InsetLayout, but at present
4851 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4852 if (features.runparams().flavor == OutputParams::HTML)
4853 features.addCSSSnippet(
4854 "table { border: 1px solid black; display: inline-block; }\n"
4855 "td { border: 1px solid black; padding: 0.5ex; }");
4859 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4861 return tabular.cellInset(idx);
4865 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4867 return tabular.cellInset(idx);
4871 void InsetTabular::cursorPos(BufferView const & bv,
4872 CursorSlice const & sl, bool boundary, int & x, int & y) const
4874 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4876 // y offset correction
4877 y += cellYPos(sl.idx());
4878 y += tabular.textVOffset(sl.idx());
4879 y += offset_valign_;
4881 // x offset correction
4882 x += cellXPos(sl.idx());
4883 x += tabular.textHOffset(sl.idx());
4884 x += ADD_TO_TABULAR_WIDTH;
4889 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4893 Inset const & inset = *tabular.cellInset(cell);
4894 Point o = bv.coordCache().getInsets().xy(&inset);
4895 int const xbeg = o.x_ - tabular.textHOffset(cell);
4896 int const xend = xbeg + tabular.cellWidth(cell);
4897 row_type const row = tabular.cellRow(cell);
4898 int const ybeg = o.y_ - tabular.rowAscent(row)
4899 - tabular.interRowSpace(row) - tabular.textVOffset(cell);
4900 int const yend = ybeg + tabular.cellHeight(cell);
4912 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4913 // << " ybeg=" << ybeg << " yend=" << yend
4914 // << " xx=" << xx << " yy=" << yy
4915 // << " dist=" << xx + yy << endl;
4920 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4922 //lyxerr << "InsetTabular::editXY: " << this << endl;
4923 cur.setSelection(false);
4925 cur.idx() = getNearestCell(cur.bv(), x, y);
4927 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4931 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4933 cur.idx() = getNearestCell(cur.bv(), x, y);
4934 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4938 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4940 idx_type idx_min = 0;
4941 int dist_min = numeric_limits<int>::max();
4942 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4943 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4944 int const d = dist(bv, i, x, y);
4955 int InsetTabular::cellYPos(idx_type const cell) const
4957 row_type row = tabular.cellRow(cell);
4959 for (row_type r = 0; r < row; ++r)
4960 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4961 + tabular.interRowSpace(r + 1);
4966 int InsetTabular::cellXPos(idx_type const cell) const
4968 col_type col = tabular.cellColumn(cell);
4970 for (col_type c = 0; c < col; ++c)
4971 lx += tabular.column_info[c].width;
4976 void InsetTabular::resetPos(Cursor & cur) const
4978 BufferView & bv = cur.bv();
4979 int const maxwidth = bv.workWidth();
4981 int const scx_old = scx_;
4982 int const i = cur.find(this);
4987 int const X2 = maxwidth;
4988 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4989 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4990 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4993 scx_ = X1 + 20 - x1;
4995 scx_ = X2 - 20 - x2;
5000 // only update if offset changed
5001 if (scx_ != scx_old)
5002 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
5006 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
5008 row_type const row = tabular.cellRow(cur.idx());
5009 col_type const col = tabular.cellColumn(cur.idx());
5011 if (isRightToLeft(cur)) {
5012 if (tabular.cellColumn(cur.idx()) == 0) {
5013 if (row == tabular.nrows() - 1)
5015 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
5020 cur.idx() = tabular.getLastCellInRow(row - 1);
5022 cur.idx() = tabular.cellIndex(row, col - 1);
5025 if (tabular.isLastCell(cur.idx()))
5027 if (cur.idx() == tabular.getLastCellInRow(row))
5028 cur.idx() = tabular.cellIndex(row + 1, 0);
5030 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
5031 cur.idx() = tabular.cellIndex(row, colnextcell);
5035 cur.boundary(false);
5037 if (cur.selIsMultiCell()) {
5038 cur.pit() = cur.lastpit();
5039 cur.pos() = cur.lastpos();
5047 // in visual mode, place cursor at extreme left or right
5049 switch(entry_from) {
5051 case ENTRY_DIRECTION_RIGHT:
5052 cur.posVisToRowExtremity(false /* !left */);
5054 case ENTRY_DIRECTION_LEFT:
5055 cur.posVisToRowExtremity(true /* left */);
5057 case ENTRY_DIRECTION_IGNORE:
5058 // nothing to do in this case
5062 cur.setCurrentFont();
5067 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5069 row_type const row = tabular.cellRow(cur.idx());
5070 col_type const col = tabular.cellColumn(cur.idx());
5072 if (isRightToLeft(cur)) {
5073 if (cur.idx() == tabular.getLastCellInRow(row)) {
5076 cur.idx() = tabular.getFirstCellInRow(row);
5077 cur.idx() = tabular.cellAbove(cur.idx());
5079 if (tabular.isLastCell(cur.idx()))
5081 if (cur.idx() == tabular.getLastCellInRow(row))
5082 cur.idx() = tabular.cellIndex(row + 1, 0);
5084 cur.idx() = tabular.cellIndex(row, col + 1);
5087 if (cur.idx() == 0) // first cell
5090 cur.idx() = tabular.getLastCellInRow(row - 1);
5092 cur.idx() = tabular.cellIndex(row, col - 1);
5095 if (cur.selIsMultiCell()) {
5096 cur.pit() = cur.lastpit();
5097 cur.pos() = cur.lastpos();
5102 cur.pit() = cur.lastpit();
5103 cur.pos() = cur.lastpos();
5105 // in visual mode, place cursor at extreme left or right
5107 switch(entry_from) {
5109 case ENTRY_DIRECTION_RIGHT:
5110 cur.posVisToRowExtremity(false /* !left */);
5112 case ENTRY_DIRECTION_LEFT:
5113 cur.posVisToRowExtremity(true /* left */);
5115 case ENTRY_DIRECTION_IGNORE:
5116 // nothing to do in this case
5120 cur.setCurrentFont();
5125 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5127 istringstream is(argument);
5130 if (insetCode(s) != TABULAR_CODE)
5134 size_t safe_guard = 0;
5139 if (safe_guard > 1000) {
5140 LYXERR0("parameter max count reached!");
5144 Tabular::Feature action = Tabular::LAST_ACTION;
5147 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5148 if (s != tabularFeature[i].feature)
5150 action = tabularFeature[i].action;
5153 if (action == Tabular::LAST_ACTION) {
5154 LYXERR0("Feature not found " << s);
5158 if (tabularFeature[i].need_value)
5160 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5161 tabularFeatures(cur, action, val);
5167 static void checkLongtableSpecial(Tabular::ltType & ltt,
5168 string const & special, bool & flag)
5170 if (special == "dl_above") {
5173 } else if (special == "dl_below") {
5174 ltt.bottomDL = flag;
5176 } else if (special == "empty") {
5186 bool InsetTabular::oneCellHasRotationState(bool rotated,
5187 row_type row_start, row_type row_end,
5188 col_type col_start, col_type col_end) const
5190 for (row_type r = row_start; r <= row_end; ++r)
5191 for (col_type c = col_start; c <= col_end; ++c)
5193 if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
5196 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
5203 void InsetTabular::tabularFeatures(Cursor & cur,
5204 Tabular::Feature feature, string const & value)
5206 col_type sel_col_start;
5207 col_type sel_col_end;
5208 row_type sel_row_start;
5209 row_type sel_row_end;
5210 bool setLines = false;
5211 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5212 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5216 case Tabular::M_ALIGN_LEFT:
5217 case Tabular::ALIGN_LEFT:
5218 setAlign = LYX_ALIGN_LEFT;
5221 case Tabular::M_ALIGN_RIGHT:
5222 case Tabular::ALIGN_RIGHT:
5223 setAlign = LYX_ALIGN_RIGHT;
5226 case Tabular::M_ALIGN_CENTER:
5227 case Tabular::ALIGN_CENTER:
5228 setAlign = LYX_ALIGN_CENTER;
5231 case Tabular::ALIGN_BLOCK:
5232 setAlign = LYX_ALIGN_BLOCK;
5235 case Tabular::ALIGN_DECIMAL:
5236 setAlign = LYX_ALIGN_DECIMAL;
5239 case Tabular::M_VALIGN_TOP:
5240 case Tabular::VALIGN_TOP:
5241 setVAlign = Tabular::LYX_VALIGN_TOP;
5244 case Tabular::M_VALIGN_BOTTOM:
5245 case Tabular::VALIGN_BOTTOM:
5246 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5249 case Tabular::M_VALIGN_MIDDLE:
5250 case Tabular::VALIGN_MIDDLE:
5251 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5258 cur.recordUndoInset(ATOMIC_UNDO, this);
5260 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5261 row_type const row = tabular.cellRow(cur.idx());
5262 col_type const column = tabular.cellColumn(cur.idx());
5264 Tabular::ltType ltt;
5268 case Tabular::SET_TABULAR_WIDTH:
5269 tabular.setTabularWidth(Length(value));
5272 case Tabular::SET_PWIDTH: {
5273 Length const len(value);
5274 tabular.setColumnPWidth(cur, cur.idx(), len);
5276 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5277 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5281 case Tabular::SET_MPWIDTH:
5282 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5285 case Tabular::SET_MROFFSET:
5286 tabular.setMROffset(cur, cur.idx(), Length(value));
5289 case Tabular::SET_SPECIAL_COLUMN:
5290 case Tabular::SET_SPECIAL_MULTICOLUMN:
5291 if (value == "none")
5292 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5294 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5297 case Tabular::APPEND_ROW:
5298 // append the row into the tabular
5299 tabular.appendRow(row);
5302 case Tabular::APPEND_COLUMN:
5303 // append the column into the tabular
5304 tabular.appendColumn(column);
5305 cur.idx() = tabular.cellIndex(row, column);
5308 case Tabular::DELETE_ROW:
5309 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5310 tabular.deleteRow(sel_row_start);
5311 if (sel_row_start >= tabular.nrows())
5313 cur.idx() = tabular.cellIndex(sel_row_start, column);
5316 cur.setSelection(false);
5319 case Tabular::DELETE_COLUMN:
5320 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5321 tabular.deleteColumn(sel_col_start);
5322 if (sel_col_start >= tabular.ncols())
5324 cur.idx() = tabular.cellIndex(row, sel_col_start);
5327 cur.setSelection(false);
5330 case Tabular::COPY_ROW:
5331 tabular.copyRow(row);
5334 case Tabular::COPY_COLUMN:
5335 tabular.copyColumn(column);
5336 cur.idx() = tabular.cellIndex(row, column);
5339 case Tabular::SET_LINE_TOP:
5340 case Tabular::TOGGLE_LINE_TOP: {
5341 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5342 ? (value == "true") : !tabular.topLine(cur.idx());
5343 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5344 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5345 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5349 case Tabular::SET_LINE_BOTTOM:
5350 case Tabular::TOGGLE_LINE_BOTTOM: {
5351 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5352 ? (value == "true") : !tabular.bottomLine(cur.idx());
5353 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5354 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5355 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5359 case Tabular::SET_LINE_LEFT:
5360 case Tabular::TOGGLE_LINE_LEFT: {
5361 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5362 ? (value == "true") : !tabular.leftLine(cur.idx());
5363 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5364 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5365 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5369 case Tabular::SET_LINE_RIGHT:
5370 case Tabular::TOGGLE_LINE_RIGHT: {
5371 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5372 ? (value == "true") : !tabular.rightLine(cur.idx());
5373 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5374 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5375 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5379 case Tabular::M_ALIGN_LEFT:
5380 case Tabular::M_ALIGN_RIGHT:
5381 case Tabular::M_ALIGN_CENTER:
5382 case Tabular::ALIGN_LEFT:
5383 case Tabular::ALIGN_RIGHT:
5384 case Tabular::ALIGN_CENTER:
5385 case Tabular::ALIGN_BLOCK:
5386 case Tabular::ALIGN_DECIMAL:
5387 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5388 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5389 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5390 !tabular.getPWidth(c).zero());
5393 case Tabular::M_VALIGN_TOP:
5394 case Tabular::M_VALIGN_BOTTOM:
5395 case Tabular::M_VALIGN_MIDDLE:
5397 case Tabular::VALIGN_TOP:
5398 case Tabular::VALIGN_BOTTOM:
5399 case Tabular::VALIGN_MIDDLE:
5400 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5401 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5402 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5405 case Tabular::SET_MULTICOLUMN: {
5406 if (!cur.selection()) {
5407 // just multicol for one single cell
5408 // check whether we are completely in a multicol
5409 if (!tabular.isMultiColumn(cur.idx()))
5410 tabular.setMultiColumn(cur.idx(), 1,
5411 tabular.rightLine(cur.idx()));
5414 // we have a selection so this means we just add all this
5415 // cells to form a multicolumn cell
5416 idx_type const s_start = cur.selBegin().idx();
5417 row_type const col_start = tabular.cellColumn(s_start);
5418 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5419 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5420 tabular.rightLine(cur.selEnd().idx()));
5423 cur.setSelection(false);
5427 case Tabular::UNSET_MULTICOLUMN: {
5428 if (!cur.selection()) {
5429 if (tabular.isMultiColumn(cur.idx()))
5430 tabular.unsetMultiColumn(cur.idx());
5435 case Tabular::MULTICOLUMN: {
5436 if (!cur.selection()) {
5437 if (tabular.isMultiColumn(cur.idx()))
5438 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5440 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5444 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5445 row_type const r = sel_row_start;
5446 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5447 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5450 // If the selection contains at least one singlecol cell
5451 // or multiple multicol cells,
5452 // we assume the user will merge is to a single multicol
5454 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5456 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5460 case Tabular::SET_MULTIROW: {
5461 if (!cur.selection()) {
5462 // just multirow for one single cell
5463 // check whether we are completely in a multirow
5464 if (!tabular.isMultiRow(cur.idx()))
5465 tabular.setMultiRow(cur.idx(), 1,
5466 tabular.bottomLine(cur.idx()));
5469 // we have a selection so this means we just add all this
5470 // cells to form a multirow cell
5471 idx_type const s_start = cur.selBegin().idx();
5472 row_type const row_start = tabular.cellRow(s_start);
5473 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5474 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5475 tabular.bottomLine(cur.selEnd().idx()));
5478 cur.setSelection(false);
5482 case Tabular::UNSET_MULTIROW: {
5483 if (!cur.selection()) {
5484 if (tabular.isMultiRow(cur.idx()))
5485 tabular.unsetMultiRow(cur.idx());
5490 case Tabular::MULTIROW: {
5491 if (!cur.selection()) {
5492 if (tabular.isMultiRow(cur.idx()))
5493 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5495 tabularFeatures(cur, Tabular::SET_MULTIROW);
5499 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5500 col_type const c = sel_col_start;
5501 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5502 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5505 // If the selection contains at least one singlerow cell
5506 // or multiple multirow cells,
5507 // we assume the user will merge is to a single multirow
5509 tabularFeatures(cur, Tabular::SET_MULTIROW);
5511 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5515 case Tabular::SET_ALL_LINES:
5517 case Tabular::UNSET_ALL_LINES:
5518 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5519 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5520 idx_type const cell = tabular.cellIndex(r, c);
5521 tabular.setTopLine(cell, setLines);
5522 tabular.setBottomLine(cell, setLines);
5523 tabular.setRightLine(cell, setLines);
5524 tabular.setLeftLine(cell, setLines);
5528 case Tabular::SET_BORDER_LINES:
5529 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5530 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5531 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5533 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5534 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5535 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5539 case Tabular::SET_LONGTABULAR:
5540 tabular.is_long_tabular = true;
5543 case Tabular::UNSET_LONGTABULAR:
5544 for (row_type r = 0; r < tabular.nrows(); ++r) {
5545 if (tabular.ltCaption(r)) {
5546 cur.idx() = tabular.cellIndex(r, 0);
5549 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5552 tabular.is_long_tabular = false;
5555 case Tabular::SET_ROTATE_TABULAR:
5556 tabular.rotate = convert<int>(value);
5559 case Tabular::UNSET_ROTATE_TABULAR:
5563 case Tabular::TOGGLE_ROTATE_TABULAR:
5564 // when pressing the rotate button we default to 90° rotation
5565 tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
5568 case Tabular::TABULAR_VALIGN_TOP:
5569 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5572 case Tabular::TABULAR_VALIGN_MIDDLE:
5573 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5576 case Tabular::TABULAR_VALIGN_BOTTOM:
5577 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5580 case Tabular::LONGTABULAR_ALIGN_LEFT:
5581 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5584 case Tabular::LONGTABULAR_ALIGN_CENTER:
5585 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5588 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5589 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5594 case Tabular::SET_ROTATE_CELL:
5595 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5596 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5597 tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
5600 case Tabular::UNSET_ROTATE_CELL:
5601 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5602 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5603 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5606 case Tabular::TOGGLE_ROTATE_CELL:
5608 bool oneNotRotated = oneCellHasRotationState(false,
5609 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5611 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5612 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5613 // when pressing the rotate cell button we default to 90° rotation
5615 tabular.setRotateCell(tabular.cellIndex(r, c), 90);
5617 tabular.setRotateCell(tabular.cellIndex(r, c), 0);
5622 case Tabular::SET_USEBOX: {
5623 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5624 if (val == tabular.getUsebox(cur.idx()))
5625 val = Tabular::BOX_NONE;
5626 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5627 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5628 tabular.setUsebox(tabular.cellIndex(r, c), val);
5632 case Tabular::UNSET_LTFIRSTHEAD:
5634 case Tabular::SET_LTFIRSTHEAD:
5635 tabular.getRowOfLTFirstHead(row, ltt);
5636 checkLongtableSpecial(ltt, value, flag);
5637 tabular.setLTHead(row, flag, ltt, true);
5640 case Tabular::UNSET_LTHEAD:
5642 case Tabular::SET_LTHEAD:
5643 tabular.getRowOfLTHead(row, ltt);
5644 checkLongtableSpecial(ltt, value, flag);
5645 tabular.setLTHead(row, flag, ltt, false);
5648 case Tabular::UNSET_LTFOOT:
5650 case Tabular::SET_LTFOOT:
5651 tabular.getRowOfLTFoot(row, ltt);
5652 checkLongtableSpecial(ltt, value, flag);
5653 tabular.setLTFoot(row, flag, ltt, false);
5656 case Tabular::UNSET_LTLASTFOOT:
5658 case Tabular::SET_LTLASTFOOT:
5659 tabular.getRowOfLTLastFoot(row, ltt);
5660 checkLongtableSpecial(ltt, value, flag);
5661 tabular.setLTFoot(row, flag, ltt, true);
5664 case Tabular::UNSET_LTNEWPAGE:
5666 case Tabular::SET_LTNEWPAGE:
5667 tabular.setLTNewPage(row, flag);
5670 case Tabular::SET_LTCAPTION: {
5671 if (tabular.ltCaption(row))
5673 cur.idx() = tabular.setLTCaption(row, true);
5676 cur.setSelection(false);
5677 // If a row is set as caption, then also insert
5678 // a caption. Otherwise the LaTeX output is broken.
5679 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5680 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5684 case Tabular::UNSET_LTCAPTION: {
5685 if (!tabular.ltCaption(row))
5687 cur.idx() = tabular.setLTCaption(row, false);
5690 cur.setSelection(false);
5691 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5692 if (lyx::getStatus(fr).enabled())
5697 case Tabular::TOGGLE_LTCAPTION: {
5698 if (tabular.ltCaption(row))
5699 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5701 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5705 case Tabular::SET_BOOKTABS:
5706 tabular.use_booktabs = true;
5709 case Tabular::UNSET_BOOKTABS:
5710 tabular.use_booktabs = false;
5713 case Tabular::SET_TOP_SPACE: {
5715 if (value == "default")
5716 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5717 tabular.row_info[r].top_space_default = true;
5718 else if (value == "none")
5719 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5720 tabular.row_info[r].top_space_default = false;
5721 tabular.row_info[r].top_space = len;
5723 else if (isValidLength(value, &len))
5724 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5725 tabular.row_info[r].top_space_default = false;
5726 tabular.row_info[r].top_space = len;
5731 case Tabular::SET_BOTTOM_SPACE: {
5733 if (value == "default")
5734 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5735 tabular.row_info[r].bottom_space_default = true;
5736 else if (value == "none")
5737 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5738 tabular.row_info[r].bottom_space_default = false;
5739 tabular.row_info[r].bottom_space = len;
5741 else if (isValidLength(value, &len))
5742 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5743 tabular.row_info[r].bottom_space_default = false;
5744 tabular.row_info[r].bottom_space = len;
5749 case Tabular::SET_INTERLINE_SPACE: {
5751 if (value == "default")
5752 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5753 tabular.row_info[r].interline_space_default = true;
5754 else if (value == "none")
5755 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5756 tabular.row_info[r].interline_space_default = false;
5757 tabular.row_info[r].interline_space = len;
5759 else if (isValidLength(value, &len))
5760 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5761 tabular.row_info[r].interline_space_default = false;
5762 tabular.row_info[r].interline_space = len;
5767 case Tabular::SET_DECIMAL_POINT:
5768 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5769 tabular.column_info[c].decimal_point = from_utf8(value);
5772 // dummy stuff just to avoid warnings
5773 case Tabular::LAST_ACTION:
5779 bool InsetTabular::copySelection(Cursor & cur)
5781 if (!cur.selection())
5786 getSelection(cur, rs, re, cs, ce);
5788 paste_tabular.reset(new Tabular(tabular));
5790 for (row_type r = 0; r < rs; ++r)
5791 paste_tabular->deleteRow(0);
5793 row_type const rows = re - rs + 1;
5794 while (paste_tabular->nrows() > rows)
5795 paste_tabular->deleteRow(rows);
5797 for (col_type c = 0; c < cs; ++c)
5798 paste_tabular->deleteColumn(0);
5800 col_type const columns = ce - cs + 1;
5801 while (paste_tabular->ncols() > columns)
5802 paste_tabular->deleteColumn(columns);
5804 paste_tabular->setBuffer(tabular.buffer());
5806 odocstringstream os;
5807 OutputParams const runparams(0);
5808 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5809 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5810 cap::copySelection(cur, os.str());
5812 // mark tabular stack dirty
5813 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5814 // when we (hopefully) have a one-for-all paste mechanism.
5815 // This must be called after cap::copySelection.
5816 dirtyTabularStack(true);
5822 bool InsetTabular::pasteClipboard(Cursor & cur)
5826 col_type actcol = tabular.cellColumn(cur.idx());
5827 row_type actrow = tabular.cellRow(cur.idx());
5829 if (cur.selIsMultiCell()) {
5832 getSelection(cur, actrow, re, actcol, ce);
5835 for (row_type r1 = 0, r2 = actrow;
5836 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5838 for (col_type c1 = 0, c2 = actcol;
5839 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5841 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5842 tabular.isPartOfMultiColumn(r2, c2))
5844 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5848 if (tabular.isPartOfMultiColumn(r2, c2)) {
5852 shared_ptr<InsetTableCell> inset(
5853 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5854 tabular.setCellInset(r2, c2, inset);
5855 // FIXME?: why do we need to do this explicitly? (EL)
5856 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5858 // FIXME: change tracking (MG)
5859 inset->setChange(Change(buffer().params().trackChanges ?
5860 Change::INSERTED : Change::UNCHANGED));
5868 void InsetTabular::cutSelection(Cursor & cur)
5870 if (!cur.selection())
5875 getSelection(cur, rs, re, cs, ce);
5876 for (row_type r = rs; r <= re; ++r) {
5877 for (col_type c = cs; c <= ce; ++c) {
5878 shared_ptr<InsetTableCell> t
5879 = cell(tabular.cellIndex(r, c));
5880 if (buffer().params().trackChanges)
5881 // FIXME: Change tracking (MG)
5882 t->setChange(Change(Change::DELETED));
5888 // cursor position might be invalid now
5889 if (cur.pit() > cur.lastpit())
5890 cur.pit() = cur.lastpit();
5891 if (cur.pos() > cur.lastpos())
5892 cur.pos() = cur.lastpos();
5893 cur.clearSelection();
5897 bool InsetTabular::isRightToLeft(Cursor & cur) const
5899 LASSERT(cur.depth() > 1, /**/);
5900 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5901 pos_type const parentpos = cur[cur.depth() - 2].pos();
5902 return parentpar.getFontSettings(buffer().params(),
5903 parentpos).language()->rightToLeft();
5907 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5910 LASSERT(stidx <= enidx, return docstring());
5912 col_type const col1 = tabular.cellColumn(stidx);
5913 col_type const col2 = tabular.cellColumn(enidx);
5914 row_type const row1 = tabular.cellRow(stidx);
5915 row_type const row2 = tabular.cellRow(enidx);
5917 for (col_type col = col1; col <= col2; col++)
5918 for (row_type row = row1; row <= row2; row++) {
5923 retval += tabular.cellInset(row, col)->asString(intoInsets);
5929 void InsetTabular::getSelection(Cursor & cur,
5930 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5932 CursorSlice const & beg = cur.selBegin();
5933 CursorSlice const & end = cur.selEnd();
5934 cs = tabular.cellColumn(beg.idx());
5935 ce = tabular.cellColumn(end.idx());
5939 rs = tabular.cellRow(beg.idx());
5940 re = tabular.cellRow(end.idx());
5946 Text * InsetTabular::getText(int idx) const
5948 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5952 void InsetTabular::setChange(Change const & change)
5954 for (idx_type idx = 0; idx < nargs(); ++idx)
5955 cell(idx)->setChange(change);
5959 void InsetTabular::acceptChanges()
5961 for (idx_type idx = 0; idx < nargs(); ++idx)
5962 cell(idx)->acceptChanges();
5966 void InsetTabular::rejectChanges()
5968 for (idx_type idx = 0; idx < nargs(); ++idx)
5969 cell(idx)->rejectChanges();
5973 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5975 return tabular.getPWidth(cell).zero();
5979 bool InsetTabular::forcePlainLayout(idx_type cell) const
5981 return !tabular.getPWidth(cell).zero();
5985 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5988 if (buf.length() <= 0)
5993 col_type maxCols = 1;
5994 size_t const len = buf.length();
5998 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
6006 maxCols = max(cols, maxCols);
6012 maxCols = max(cols, maxCols);
6018 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
6019 loctab = paste_tabular.get();
6021 dirtyTabularStack(true);
6024 cell = bv.cursor().idx();
6025 ocol = tabular.cellColumn(cell);
6026 row = tabular.cellRow(cell);
6030 idx_type const cells = loctab->numberofcells;
6033 rows = loctab->nrows();
6034 col_type const columns = loctab->ncols();
6036 while (cell < cells && p < len && row < rows &&
6037 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
6043 // we can only set this if we are not too far right
6044 if (cols < columns) {
6045 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6046 Font const font = bv.textMetrics(&inset->text()).
6048 inset->setText(buf.substr(op, p - op), font,
6049 buffer().params().trackChanges);
6055 // we can only set this if we are not too far right
6056 if (cols < columns) {
6057 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6058 Font const font = bv.textMetrics(&inset->text()).
6060 inset->setText(buf.substr(op, p - op), font,
6061 buffer().params().trackChanges);
6066 cell = loctab->cellIndex(row, cols);
6072 // check for the last cell if there is no trailing '\n'
6073 if (cell < cells && op < len) {
6074 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6075 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6076 inset->setText(buf.substr(op, len - op), font,
6077 buffer().params().trackChanges);
6083 void InsetTabular::addPreview(DocIterator const & inset_pos,
6084 PreviewLoader & loader) const
6086 DocIterator cell_pos = inset_pos;
6088 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6089 for (row_type r = 0; r < tabular.nrows(); ++r) {
6090 for (col_type c = 0; c < tabular.ncols(); ++c) {
6091 cell_pos.top().idx() = tabular.cellIndex(r, c);
6092 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6098 bool InsetTabular::completionSupported(Cursor const & cur) const
6100 Cursor const & bvCur = cur.bv().cursor();
6101 if (&bvCur.inset() != this)
6103 return cur.text()->completionSupported(cur);
6107 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6109 return completionSupported(cur);
6113 bool InsetTabular::automaticInlineCompletion() const
6115 return lyxrc.completion_inline_text;
6119 bool InsetTabular::automaticPopupCompletion() const
6121 return lyxrc.completion_popup_text;
6125 bool InsetTabular::showCompletionCursor() const
6127 return lyxrc.completion_cursor_text;
6131 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6133 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6137 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6139 if (!completionSupported(cur))
6141 return cur.text()->completionPrefix(cur);
6145 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6147 if (!completionSupported(cur))
6150 return cur.text()->insertCompletion(cur, s, finished);
6154 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6155 Dimension & dim) const
6157 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6158 tm.completionPosAndDim(cur, x, y, dim);
6162 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6164 istringstream data(in);
6166 lex.setStream(data);
6173 if (!lex || token != "tabular") {
6174 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6178 // This is part of the inset proper that is usually swallowed
6179 // by Buffer::readInset
6181 if (!lex || token != "Tabular") {
6182 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6190 string InsetTabular::params2string(InsetTabular const & inset)
6193 data << "tabular" << ' ';
6195 data << "\\end_inset\n";