2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
169 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
170 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
172 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
173 { Tabular::SET_BOOKTABS, "set-booktabs", false },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
175 { Tabular::SET_TOP_SPACE, "set-top-space", true },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
178 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
179 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
180 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
181 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
182 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
183 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
184 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
185 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
186 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
187 { Tabular::LAST_ACTION, "", false }
191 string const tostr(LyXAlignment const & num)
196 case LYX_ALIGN_BLOCK:
200 case LYX_ALIGN_CENTER:
202 case LYX_ALIGN_RIGHT:
204 case LYX_ALIGN_LAYOUT:
206 case LYX_ALIGN_SPECIAL:
208 case LYX_ALIGN_DECIMAL:
215 string const tostr(Tabular::HAlignment const & num)
218 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
220 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
222 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
229 string const tostr(Tabular::VAlignment const & num)
232 case Tabular::LYX_VALIGN_TOP:
234 case Tabular::LYX_VALIGN_MIDDLE:
236 case Tabular::LYX_VALIGN_BOTTOM:
243 string const tostr(Tabular::BoxType const & num)
246 case Tabular::BOX_NONE:
248 case Tabular::BOX_PARBOX:
250 case Tabular::BOX_MINIPAGE:
257 // I would have liked a fromstr template a lot better. (Lgb)
258 bool string2type(string const str, LyXAlignment & num)
261 num = LYX_ALIGN_NONE;
262 else if (str == "block")
263 num = LYX_ALIGN_BLOCK;
264 else if (str == "left")
265 num = LYX_ALIGN_LEFT;
266 else if (str == "center")
267 num = LYX_ALIGN_CENTER;
268 else if (str == "right")
269 num = LYX_ALIGN_RIGHT;
270 else if (str == "decimal")
271 num = LYX_ALIGN_DECIMAL;
278 bool string2type(string const str, Tabular::HAlignment & num)
281 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
282 else if (str == "center" )
283 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
284 else if (str == "right")
285 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
292 bool string2type(string const str, Tabular::VAlignment & num)
295 num = Tabular::LYX_VALIGN_TOP;
296 else if (str == "middle" )
297 num = Tabular::LYX_VALIGN_MIDDLE;
298 else if (str == "bottom")
299 num = Tabular::LYX_VALIGN_BOTTOM;
306 bool string2type(string const str, Tabular::BoxType & num)
309 num = Tabular::BOX_NONE;
310 else if (str == "parbox")
311 num = Tabular::BOX_PARBOX;
312 else if (str == "minipage")
313 num = Tabular::BOX_MINIPAGE;
320 bool string2type(string const str, bool & num)
324 else if (str == "false")
332 bool getTokenValue(string const & str, char const * token, string & ret)
335 size_t token_length = strlen(token);
336 size_t pos = str.find(token);
338 if (pos == string::npos || pos + token_length + 1 >= str.length()
339 || str[pos + token_length] != '=')
341 pos += token_length + 1;
343 if (ch != '"' && ch != '\'') { // only read till next space
347 while (pos < str.length() - 1 && str[++pos] != ch)
354 bool getTokenValue(string const & str, char const * token, docstring & ret)
357 bool const success = getTokenValue(str, token, tmp);
358 ret = from_utf8(tmp);
363 bool getTokenValue(string const & str, char const * token, int & num)
367 if (!getTokenValue(str, token, tmp))
369 num = convert<int>(tmp);
374 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
377 return getTokenValue(str, token, tmp) && string2type(tmp, num);
381 bool getTokenValue(string const & str, char const * token,
382 Tabular::HAlignment & num)
385 return getTokenValue(str, token, tmp) && string2type(tmp, num);
389 bool getTokenValue(string const & str, char const * token,
390 Tabular::VAlignment & num)
393 return getTokenValue(str, token, tmp) && string2type(tmp, num);
397 bool getTokenValue(string const & str, char const * token,
398 Tabular::BoxType & num)
401 return getTokenValue(str, token, tmp) && string2type(tmp, num);
405 bool getTokenValue(string const & str, char const * token, bool & flag)
407 // set the flag always to false as this should be the default for bools
408 // not in the file-format.
411 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
415 bool getTokenValue(string const & str, char const * token, Length & len)
417 // set the length to be zero() as default as this it should be if not
418 // in the file format.
421 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
425 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
430 if (!getTokenValue(str, token, tmp))
432 if (tmp == "default") {
436 return isValidLength(tmp, &len);
440 void l_getline(istream & is, string & str)
443 while (str.empty()) {
445 if (!str.empty() && str[str.length() - 1] == '\r')
446 str.erase(str.length() - 1);
451 string const write_attribute(string const & name, T const & t)
453 string const s = tostr(t);
454 return s.empty() ? s : " " + name + "=\"" + s + "\"";
458 string const write_attribute(string const & name, string const & t)
460 return t.empty() ? t : " " + name + "=\"" + t + "\"";
465 string const write_attribute(string const & name, docstring const & t)
467 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
472 string const write_attribute(string const & name, bool const & b)
474 // we write only true attribute values so we remove a bit of the
475 // file format bloat for tabulars.
476 return b ? write_attribute(name, convert<string>(b)) : string();
481 string const write_attribute(string const & name, int const & i)
483 // we write only true attribute values so we remove a bit of the
484 // file format bloat for tabulars.
485 return i ? write_attribute(name, convert<string>(i)) : string();
490 string const write_attribute(string const & name, Tabular::idx_type const & i)
492 // we write only true attribute values so we remove a bit of the
493 // file format bloat for tabulars.
494 return i ? write_attribute(name, convert<string>(i)) : string();
499 string const write_attribute(string const & name, Length const & value)
501 // we write only the value if we really have one same reson as above.
502 return value.zero() ? string() : write_attribute(name, value.asString());
508 string const featureAsString(Tabular::Feature action)
510 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
511 if (tabularFeature[i].action == action)
512 return tabularFeature[i].feature;
518 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
520 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
521 for (; dit; dit.forwardChar())
522 if (dit.inTexted() && dit.depth() == 1
523 && dit.paragraph().find(align_d, false, false, dit.pos()))
530 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
532 InsetTableCell tail = InsetTableCell(head);
533 DocIterator const dit = separatorPos(&head, align_d);
536 pit_type const psize = head.paragraphs().front().size();
537 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
538 tail.paragraphs().front().eraseChars(0,
539 dit.pos() < psize ? dit.pos() + 1 : psize, false);
546 /////////////////////////////////////////////////////////////////////
550 /////////////////////////////////////////////////////////////////////
553 Tabular::CellData::CellData(Buffer * buf)
556 multicolumn(Tabular::CELL_NORMAL),
557 multirow(Tabular::CELL_NORMAL),
558 alignment(LYX_ALIGN_CENTER),
559 valignment(LYX_VALIGN_TOP),
569 inset(new InsetTableCell(buf))
571 inset->setBuffer(*buf);
575 Tabular::CellData::CellData(CellData const & cs)
578 multicolumn(cs.multicolumn),
579 multirow(cs.multirow),
580 mroffset(cs.mroffset),
581 alignment(cs.alignment),
582 valignment(cs.valignment),
583 decimal_hoffset(cs.decimal_hoffset),
584 decimal_width(cs.decimal_width),
586 top_line(cs.top_line),
587 bottom_line(cs.bottom_line),
588 left_line(cs.left_line),
589 right_line(cs.right_line),
592 align_special(cs.align_special),
594 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
598 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
604 void Tabular::CellData::swap(CellData & rhs)
606 std::swap(cellno, rhs.cellno);
607 std::swap(width, rhs.width);
608 std::swap(multicolumn, rhs.multicolumn);
609 std::swap(multirow, rhs.multirow);
610 std::swap(mroffset, rhs.mroffset);
611 std::swap(alignment, rhs.alignment);
612 std::swap(valignment, rhs.valignment);
613 std::swap(decimal_hoffset, rhs.decimal_hoffset);
614 std::swap(decimal_width, rhs.decimal_width);
615 std::swap(voffset, rhs.voffset);
616 std::swap(top_line, rhs.top_line);
617 std::swap(bottom_line, rhs.bottom_line);
618 std::swap(left_line, rhs.left_line);
619 std::swap(right_line, rhs.right_line);
620 std::swap(usebox, rhs.usebox);
621 std::swap(rotate, rhs.rotate);
622 std::swap(align_special, rhs.align_special);
623 p_width.swap(rhs.p_width);
624 inset.swap(rhs.inset);
628 Tabular::RowData::RowData()
631 top_space_default(false),
632 bottom_space_default(false),
633 interline_space_default(false),
643 Tabular::ColumnData::ColumnData()
644 : alignment(LYX_ALIGN_CENTER),
645 valignment(LYX_VALIGN_TOP),
651 Tabular::ltType::ltType()
658 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
660 init(buffer, rows_arg, columns_arg);
664 void Tabular::setBuffer(Buffer & buffer)
667 for (row_type i = 0; i < nrows(); ++i)
668 for (col_type j = 0; j < ncols(); ++j)
669 cell_info[i][j].inset->setBuffer(*buffer_);
673 // activates all lines and sets all widths to 0
674 void Tabular::init(Buffer * buf, row_type rows_arg,
675 col_type columns_arg)
678 row_info = row_vector(rows_arg);
679 column_info = column_vector(columns_arg);
680 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
681 row_info.reserve(10);
682 column_info.reserve(10);
683 cell_info.reserve(100);
685 is_long_tabular = false;
686 tabular_valignment = LYX_VALIGN_MIDDLE;
687 tabular_width = Length();
688 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
690 use_booktabs = false;
691 // set silly default lines
692 for (row_type r = 0; r < nrows(); ++r)
693 for (col_type c = 0; c < ncols(); ++c) {
694 cell_info[r][c].inset->setBuffer(*buffer_);
695 cell_info[r][c].top_line = true;
696 cell_info[r][c].left_line = true;
697 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
698 cell_info[r][c].right_line = c == ncols() - 1;
703 void Tabular::appendRow(idx_type const cell)
705 row_type const row = cellRow(cell);
707 row_info.insert(row_info.begin() + row + 1, RowData());
708 row_info[row + 1] = row_info[row];
710 cell_info.insert(cell_info.begin() + row + 1,
711 cell_vector(ncols(), CellData(buffer_)));
712 for (col_type c = 0; c < ncols(); ++c) {
713 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
714 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
716 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
720 for (col_type c = 0; c < ncols(); ++c) {
721 if (isPartOfMultiRow(row, c))
723 // inherit line settings
724 idx_type const i = cellIndex(row + 1, c);
725 idx_type const j = cellIndex(row, c);
726 setLeftLine(i, leftLine(j));
727 setRightLine(i, rightLine(j));
728 setTopLine(i, topLine(j));
729 if (topLine(j) && bottomLine(j)) {
730 setBottomLine(i, true);
731 setBottomLine(j, false);
733 // mark track changes
734 if (buffer().params().trackChanges)
735 cellInfo(i).inset->setChange(Change(Change::INSERTED));
740 void Tabular::deleteRow(row_type const row)
742 // Not allowed to delete last row
746 for (col_type c = 0; c < ncols(); ++c) {
747 // Care about multirow cells
748 if (row + 1 < nrows() &&
749 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
750 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
751 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
754 row_info.erase(row_info.begin() + row);
755 cell_info.erase(cell_info.begin() + row);
760 void Tabular::copyRow(row_type const row)
762 row_info.insert(row_info.begin() + row, row_info[row]);
763 cell_info.insert(cell_info.begin() + row, cell_info[row]);
765 if (buffer().params().trackChanges)
766 for (col_type c = 0; c < ncols(); ++c)
767 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
773 void Tabular::appendColumn(idx_type const cell)
775 col_type const c = cellColumn(cell);
777 column_info.insert(column_info.begin() + c + 1, ColumnData());
778 column_info[c + 1] = column_info[c];
780 for (row_type r = 0; r < nrows(); ++r) {
781 cell_info[r].insert(cell_info[r].begin() + c + 1,
783 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
784 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
786 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
789 for (row_type r = 0; r < nrows(); ++r) {
790 // inherit line settings
791 idx_type const i = cellIndex(r, c + 1);
792 idx_type const j = cellIndex(r, c);
793 setBottomLine(i, bottomLine(j));
794 setTopLine(i, topLine(j));
795 setLeftLine(i, leftLine(j));
796 if (rightLine(j) && rightLine(j)) {
797 setRightLine(i, true);
798 setRightLine(j, false);
800 if (buffer().params().trackChanges)
801 cellInfo(i).inset->setChange(Change(Change::INSERTED));
806 void Tabular::deleteColumn(col_type const col)
808 // Not allowed to delete last column
812 for (row_type r = 0; r < nrows(); ++r) {
813 // Care about multicolumn cells
814 if (col + 1 < ncols() &&
815 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
816 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
817 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
819 cell_info[r].erase(cell_info[r].begin() + col);
821 column_info.erase(column_info.begin() + col);
826 void Tabular::copyColumn(col_type const col)
828 BufferParams const & bp = buffer().params();
829 column_info.insert(column_info.begin() + col, column_info[col]);
831 for (row_type r = 0; r < nrows(); ++r) {
832 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
834 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
840 void Tabular::updateIndexes()
845 for (row_type row = 0; row < nrows(); ++row)
846 for (col_type column = 0; column < ncols(); ++column) {
847 if (!isPartOfMultiColumn(row, column)
848 && !isPartOfMultiRow(row, column))
850 if (isPartOfMultiRow(row, column))
851 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
853 cell_info[row][column].cellno = numberofcells - 1;
856 rowofcell.resize(numberofcells);
857 columnofcell.resize(numberofcells);
859 // reset column and row of cells and update their width and alignment
860 for (row_type row = 0; row < nrows(); ++row)
861 for (col_type column = 0; column < ncols(); ++column) {
862 if (isPartOfMultiColumn(row, column))
864 // columnofcell needs to be called before setting width and aligment
865 // multirow cells inherit the width from the column width
866 if (!isPartOfMultiRow(row, column)) {
867 columnofcell[i] = column;
870 setFixedWidth(row, column);
871 if (isPartOfMultiRow(row, column))
873 cell_info[row][column].inset->setContentAlignment(
874 getAlignment(cellIndex(row, column)));
880 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
883 for (col_type c = 0; c < ncols(); ++c)
884 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
890 bool Tabular::topLine(idx_type const cell) const
892 return cellInfo(cell).top_line;
896 bool Tabular::bottomLine(idx_type const cell) const
898 return cellInfo(cell).bottom_line;
902 bool Tabular::leftLine(idx_type cell) const
906 return cellInfo(cell).left_line;
910 bool Tabular::rightLine(idx_type cell) const
914 return cellInfo(cell).right_line;
918 int Tabular::interRowSpace(row_type row) const
920 if (!row || row >= nrows())
923 int const interline_space = row_info[row - 1].interline_space_default ?
925 row_info[row - 1].interline_space.inPixels(width());
926 if (rowTopLine(row) && rowBottomLine(row - 1))
927 return interline_space + WIDTH_OF_LINE;
928 return interline_space;
932 int Tabular::interColumnSpace(idx_type cell) const
934 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
935 if (rightLine(cell) && nextcol < ncols()
936 && leftLine(cellIndex(cellRow(cell), nextcol)))
937 return WIDTH_OF_LINE;
942 int Tabular::cellWidth(idx_type cell) const
945 col_type const span = columnSpan(cell);
946 col_type const col = cellColumn(cell);
947 for(col_type c = col; c < col + span ; ++c)
948 w += column_info[c].width;
953 int Tabular::cellHeight(idx_type cell) const
955 row_type const span = rowSpan(cell);
956 row_type const row = cellRow(cell);
958 for(row_type r = row; r < row + span ; ++r) {
959 h += rowAscent(r) + rowDescent(r);
960 if (r != row + span - 1)
961 h += interRowSpace(r + 1);
968 bool Tabular::updateColumnWidths()
970 vector<int> max_dwidth(ncols(), 0);
971 for(col_type c = 0; c < ncols(); ++c)
972 for(row_type r = 0; r < nrows(); ++r) {
973 idx_type const i = cellIndex(r, c);
974 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
975 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
979 // for each col get max of single col cells
980 for(col_type c = 0; c < ncols(); ++c) {
982 for(row_type r = 0; r < nrows(); ++r) {
983 idx_type const i = cellIndex(r, c);
984 if (columnSpan(i) == 1) {
985 if (getAlignment(i) == LYX_ALIGN_DECIMAL
986 && cell_info[r][c].decimal_width!=0)
987 new_width = max(new_width, cellInfo(i).width
988 + max_dwidth[c] - cellInfo(i).decimal_width);
990 new_width = max(new_width, cellInfo(i).width);
994 if (column_info[c].width != new_width) {
995 column_info[c].width = new_width;
999 // update col widths to fit merged cells
1000 for(col_type c = 0; c < ncols(); ++c)
1001 for(row_type r = 0; r < nrows(); ++r) {
1002 idx_type const i = cellIndex(r, c);
1003 int const span = columnSpan(i);
1004 if (span == 1 || c > cellColumn(i))
1008 for(col_type j = c; j < c + span ; ++j)
1009 old_width += column_info[j].width;
1011 if (cellInfo(i).width > old_width) {
1012 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1021 int Tabular::width() const
1024 for (col_type c = 0; c < ncols(); ++c)
1025 width += column_info[c].width;
1030 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1033 col_type const col = cellColumn(cell);
1034 // set alignment for the whole row if we are not in a multicolumn cell,
1035 // exclude possible multicolumn cells in the row
1036 if (!isMultiColumn(cell)) {
1037 for (row_type r = 0; r < nrows(); ++r) {
1038 // only if the column has no width the multirow inherits the
1039 // alignment of the column, otherwise it is left aligned
1040 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1041 && !isMultiColumn(cellIndex(r, col))) {
1042 cell_info[r][col].alignment = align;
1043 cell_info[r][col].inset->setContentAlignment(align);
1045 if ((isMultiRow(cellIndex(r, col)) && has_width)
1046 && !isMultiColumn(cellIndex(r, col))) {
1047 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1048 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1051 column_info[col].alignment = align;
1052 docstring & dpoint = column_info[col].decimal_point;
1053 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1054 dpoint = from_utf8(lyxrc.default_decimal_point);
1056 cellInfo(cell).alignment = align;
1057 cellInset(cell).get()->setContentAlignment(align);
1062 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1065 if (!isMultiColumn(cell) || onlycolumn)
1066 column_info[cellColumn(cell)].valignment = align;
1068 cellInfo(cell).valignment = align;
1075 * Allow line and paragraph breaks for fixed width cells or disallow them,
1076 * merge cell paragraphs and reset layout to standard for variable width
1079 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1081 inset->setAutoBreakRows(fixedWidth);
1082 inset->toggleFixedWidth(fixedWidth);
1086 // merge all paragraphs to one
1087 BufferParams const & bp = cur.bv().buffer().params();
1088 while (inset->paragraphs().size() > 1)
1089 mergeParagraph(bp, inset->paragraphs(), 0);
1093 // undo information has already been recorded
1094 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1095 bp.documentClass().plainLayoutName());
1102 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1103 Length const & width)
1105 col_type const c = cellColumn(cell);
1107 column_info[c].p_width = width;
1108 // reset the vertical alignment to top if the fixed with
1109 // is removed or zero because only fixed width columns can
1110 // have a vertical alignment
1111 if (column_info[c].p_width.zero())
1112 column_info[c].valignment = LYX_VALIGN_TOP;
1113 for (row_type r = 0; r < nrows(); ++r) {
1114 idx_type const cell = cellIndex(r, c);
1115 // because of multicolumns
1116 toggleFixedWidth(cur, cellInset(cell).get(),
1117 !getPWidth(cell).zero());
1119 // cur paragraph can become invalid after paragraphs were merged
1120 if (cur.pit() > cur.lastpit())
1121 cur.pit() = cur.lastpit();
1122 // cur position can become invalid after newlines were removed
1123 if (cur.pos() > cur.lastpos())
1124 cur.pos() = cur.lastpos();
1128 bool Tabular::setFixedWidth(row_type r, col_type c)
1130 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1131 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1132 || (multicol && !cell_info[r][c].p_width.zero());
1133 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1138 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1139 Length const & width)
1141 if (!isMultiColumn(cell))
1144 cellInfo(cell).p_width = width;
1145 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1146 // cur paragraph can become invalid after paragraphs were merged
1147 if (cur.pit() > cur.lastpit())
1148 cur.pit() = cur.lastpit();
1149 // cur position can become invalid after newlines were removed
1150 if (cur.pos() > cur.lastpos())
1151 cur.pos() = cur.lastpos();
1156 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1158 cellInfo(cell).mroffset = mroffset;
1163 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1164 Tabular::Feature what)
1166 if (what == SET_SPECIAL_MULTICOLUMN)
1167 cellInfo(cell).align_special = special;
1169 column_info[cellColumn(cell)].align_special = special;
1173 void Tabular::setTopLine(idx_type i, bool line)
1175 cellInfo(i).top_line = line;
1179 void Tabular::setBottomLine(idx_type i, bool line)
1181 cellInfo(i).bottom_line = line;
1185 void Tabular::setLeftLine(idx_type cell, bool line)
1187 cellInfo(cell).left_line = line;
1191 void Tabular::setRightLine(idx_type cell, bool line)
1193 cellInfo(cell).right_line = line;
1196 bool Tabular::rowTopLine(row_type r) const
1198 bool all_rows_set = true;
1199 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1200 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1201 return all_rows_set;
1205 bool Tabular::rowBottomLine(row_type r) const
1207 bool all_rows_set = true;
1208 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1209 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1210 return all_rows_set;
1214 bool Tabular::columnLeftLine(col_type c) const
1221 for (row_type r = 0; r < nrows(); ++r) {
1222 idx_type const i = cellIndex(r, c);
1223 if (c == cellColumn(i)) {
1225 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1226 if (cellInfo(i).left_line || right)
1230 return 2 * nrows_left >= total;
1234 bool Tabular::columnRightLine(col_type c) const
1239 int nrows_right = 0;
1241 for (row_type r = 0; r < nrows(); ++r) {
1242 idx_type i = cellIndex(r, c);
1243 if (c == cellColumn(i) + columnSpan(i) - 1) {
1245 bool left = (c + 1 < ncols()
1246 && cellInfo(cellIndex(r, c + 1)).left_line)
1247 || c + 1 == ncols();
1248 if (cellInfo(i).right_line && left)
1252 return 2 * nrows_right >= total;
1256 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1258 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1259 return cellInfo(cell).alignment;
1261 return column_info[cellColumn(cell)].alignment;
1266 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1268 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1269 return cellInfo(cell).valignment;
1270 return column_info[cellColumn(cell)].valignment;
1274 Length const Tabular::getPWidth(idx_type cell) const
1276 if (isMultiColumn(cell))
1277 return cellInfo(cell).p_width;
1278 return column_info[cellColumn(cell)].p_width;
1282 Length const Tabular::getMROffset(idx_type cell) const
1284 return cellInfo(cell).mroffset;
1288 int Tabular::textHOffset(idx_type cell) const
1290 // the LaTeX Way :-(
1291 int x = WIDTH_OF_LINE;
1293 int const w = cellWidth(cell) - cellInfo(cell).width;
1295 switch (getAlignment(cell)) {
1296 case LYX_ALIGN_CENTER:
1299 case LYX_ALIGN_RIGHT:
1302 case LYX_ALIGN_DECIMAL: {
1303 // we center when no decimal point
1304 if (cellInfo(cell).decimal_width == 0) {
1308 col_type const c = cellColumn(cell);
1309 int max_dhoffset = 0;
1310 for(row_type r = 0; r < row_info.size() ; ++r) {
1311 idx_type const i = cellIndex(r, c);
1312 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1313 && cellInfo(i).decimal_width != 0)
1314 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1316 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1319 // LYX_ALIGN_LEFT: nothing :-)
1327 int Tabular::textVOffset(idx_type cell) const
1329 int voffset = cellInfo(cell).voffset;
1330 if (isMultiRow(cell)) {
1331 row_type const row = cellRow(cell);
1332 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1338 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1341 idx_type const numcells = numberOfCellsInRow(row);
1342 // we check against numcells to make sure we do not crash if all the
1343 // cells are multirow (bug #7535), but in that case our return value
1344 // is really invalid, i.e., it is NOT the first cell in the row. but
1345 // i do not know what to do here. (rgh)
1346 while (c < numcells - 1
1347 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1349 return cell_info[row][c].cellno;
1353 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1355 col_type c = ncols() - 1;
1356 // of course we check against 0 so we don't crash. but we have the same
1357 // problem as in the previous routine: if all the cells are part of a
1358 // multirow or part of a multi column, then our return value is invalid.
1360 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1361 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1363 return cell_info[row][c].cellno;
1367 Tabular::row_type Tabular::cellRow(idx_type cell) const
1369 if (cell >= numberofcells)
1373 return rowofcell[cell];
1377 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1379 if (cell >= numberofcells)
1383 return columnofcell[cell];
1387 void Tabular::write(ostream & os) const
1391 << write_attribute("version", 3)
1392 << write_attribute("rows", nrows())
1393 << write_attribute("columns", ncols())
1395 // global longtable options
1397 << write_attribute("rotate", rotate)
1398 << write_attribute("booktabs", use_booktabs)
1399 << write_attribute("islongtable", is_long_tabular)
1400 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1401 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1402 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1403 << write_attribute("headTopDL", endhead.topDL)
1404 << write_attribute("headBottomDL", endhead.bottomDL)
1405 << write_attribute("footTopDL", endfoot.topDL)
1406 << write_attribute("footBottomDL", endfoot.bottomDL)
1407 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1408 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1409 << write_attribute("lastFootEmpty", endlastfoot.empty);
1410 // longtables cannot be aligned vertically
1411 if (!is_long_tabular) {
1412 os << write_attribute("tabularvalignment", tabular_valignment);
1413 os << write_attribute("tabularwidth", tabular_width);
1415 if (is_long_tabular)
1416 os << write_attribute("longtabularalignment",
1417 longtabular_alignment);
1419 for (col_type c = 0; c < ncols(); ++c) {
1421 << write_attribute("alignment", column_info[c].alignment);
1422 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1423 os << write_attribute("decimal_point", column_info[c].decimal_point);
1424 os << write_attribute("valignment", column_info[c].valignment)
1425 << write_attribute("width", column_info[c].p_width.asString())
1426 << write_attribute("special", column_info[c].align_special)
1429 for (row_type r = 0; r < nrows(); ++r) {
1430 static const string def("default");
1432 if (row_info[r].top_space_default)
1433 os << write_attribute("topspace", def);
1435 os << write_attribute("topspace", row_info[r].top_space);
1436 if (row_info[r].bottom_space_default)
1437 os << write_attribute("bottomspace", def);
1439 os << write_attribute("bottomspace", row_info[r].bottom_space);
1440 if (row_info[r].interline_space_default)
1441 os << write_attribute("interlinespace", def);
1443 os << write_attribute("interlinespace", row_info[r].interline_space);
1444 os << write_attribute("endhead", row_info[r].endhead)
1445 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1446 << write_attribute("endfoot", row_info[r].endfoot)
1447 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1448 << write_attribute("newpage", row_info[r].newpage)
1449 << write_attribute("caption", row_info[r].caption)
1451 for (col_type c = 0; c < ncols(); ++c) {
1453 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1454 << write_attribute("multirow", cell_info[r][c].multirow)
1455 << write_attribute("mroffset", cell_info[r][c].mroffset)
1456 << write_attribute("alignment", cell_info[r][c].alignment)
1457 << write_attribute("valignment", cell_info[r][c].valignment)
1458 << write_attribute("topline", cell_info[r][c].top_line)
1459 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1460 << write_attribute("leftline", cell_info[r][c].left_line)
1461 << write_attribute("rightline", cell_info[r][c].right_line)
1462 << write_attribute("rotate", cell_info[r][c].rotate)
1463 << write_attribute("usebox", cell_info[r][c].usebox)
1464 << write_attribute("width", cell_info[r][c].p_width)
1465 << write_attribute("special", cell_info[r][c].align_special)
1467 os << "\\begin_inset ";
1468 cell_info[r][c].inset->write(os);
1469 os << "\n\\end_inset\n"
1474 os << "</lyxtabular>\n";
1478 void Tabular::read(Lexer & lex)
1481 istream & is = lex.getStream();
1483 l_getline(is, line);
1484 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1485 LASSERT(false, /**/);
1490 if (!getTokenValue(line, "version", version))
1492 LASSERT(version >= 2, /**/);
1495 if (!getTokenValue(line, "rows", rows_arg))
1498 if (!getTokenValue(line, "columns", columns_arg))
1500 init(buffer_, rows_arg, columns_arg);
1501 l_getline(is, line);
1502 if (!prefixIs(line, "<features")) {
1503 lyxerr << "Wrong tabular format (expected <features ...> got"
1504 << line << ')' << endl;
1507 getTokenValue(line, "rotate", rotate);
1508 getTokenValue(line, "booktabs", use_booktabs);
1509 getTokenValue(line, "islongtable", is_long_tabular);
1510 getTokenValue(line, "tabularvalignment", tabular_valignment);
1511 getTokenValue(line, "tabularwidth", tabular_width);
1512 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1513 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1514 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1515 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1516 getTokenValue(line, "headTopDL", endhead.topDL);
1517 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1518 getTokenValue(line, "footTopDL", endfoot.topDL);
1519 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1520 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1521 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1522 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1524 for (col_type c = 0; c < ncols(); ++c) {
1526 if (!prefixIs(line,"<column")) {
1527 lyxerr << "Wrong tabular format (expected <column ...> got"
1528 << line << ')' << endl;
1531 getTokenValue(line, "alignment", column_info[c].alignment);
1532 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1533 getTokenValue(line, "valignment", column_info[c].valignment);
1534 getTokenValue(line, "width", column_info[c].p_width);
1535 getTokenValue(line, "special", column_info[c].align_special);
1538 for (row_type i = 0; i < nrows(); ++i) {
1539 l_getline(is, line);
1540 if (!prefixIs(line, "<row")) {
1541 lyxerr << "Wrong tabular format (expected <row ...> got"
1542 << line << ')' << endl;
1545 getTokenValue(line, "topspace", row_info[i].top_space,
1546 row_info[i].top_space_default);
1547 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1548 row_info[i].bottom_space_default);
1549 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1550 row_info[i].interline_space_default);
1551 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1552 getTokenValue(line, "endhead", row_info[i].endhead);
1553 getTokenValue(line, "endfoot", row_info[i].endfoot);
1554 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1555 getTokenValue(line, "newpage", row_info[i].newpage);
1556 getTokenValue(line, "caption", row_info[i].caption);
1557 for (col_type j = 0; j < ncols(); ++j) {
1558 l_getline(is, line);
1559 if (!prefixIs(line, "<cell")) {
1560 lyxerr << "Wrong tabular format (expected <cell ...> got"
1561 << line << ')' << endl;
1564 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1565 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1566 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1567 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1568 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1569 getTokenValue(line, "topline", cell_info[i][j].top_line);
1570 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1571 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1572 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1573 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1574 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1575 getTokenValue(line, "width", cell_info[i][j].p_width);
1577 getTokenValue(line, "special", cell_info[i][j].align_special);
1578 l_getline(is, line);
1579 if (prefixIs(line, "\\begin_inset")) {
1580 cell_info[i][j].inset->setBuffer(*buffer_);
1581 cell_info[i][j].inset->read(lex);
1582 l_getline(is, line);
1584 if (!prefixIs(line, "</cell>")) {
1585 lyxerr << "Wrong tabular format (expected </cell> got"
1586 << line << ')' << endl;
1590 l_getline(is, line);
1591 if (!prefixIs(line, "</row>")) {
1592 lyxerr << "Wrong tabular format (expected </row> got"
1593 << line << ')' << endl;
1597 while (!prefixIs(line, "</lyxtabular>")) {
1598 l_getline(is, line);
1604 bool Tabular::isMultiColumn(idx_type cell) const
1606 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1607 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1611 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1613 return cell_info[cellRow(cell)][cellColumn(cell)];
1617 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number,
1618 bool const right_border)
1620 idx_type const col = cellColumn(cell);
1621 idx_type const row = cellRow(cell);
1622 for (idx_type i = 0; i < number; ++i)
1623 unsetMultiRow(cellIndex(row, col + i));
1625 // unsetting of multirow may have invalidated cell index
1626 cell = cellIndex(row, col);
1627 CellData & cs = cellInfo(cell);
1628 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1629 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1630 cs.alignment = column_info[col].alignment;
1632 setRightLine(cell, right_border);
1634 for (idx_type i = 1; i < number; ++i) {
1635 CellData & cs1 = cellInfo(cell + i);
1636 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1637 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1645 bool Tabular::isMultiRow(idx_type cell) const
1647 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1648 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1652 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1653 bool const bottom_border)
1655 idx_type const col = cellColumn(cell);
1656 idx_type const row = cellRow(cell);
1657 for (idx_type i = 0; i < number; ++i)
1658 unsetMultiColumn(cellIndex(row + i, col));
1660 // unsetting of multirow may have invalidated cell index
1661 cell = cellIndex(row, col);
1662 CellData & cs = cellInfo(cell);
1663 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1664 cs.valignment = LYX_VALIGN_MIDDLE;
1665 // the horizontal alignment of multirow cells can only
1666 // be changed for the whole table row,
1667 // support changing this only for the multirow cell can be done via
1669 // this feature would be a fileformat change
1670 // until LyX supports this, use the deault alignment of multirow
1672 cs.alignment = LYX_ALIGN_LEFT;
1674 // set the bottom line of the last selected cell
1675 setBottomLine(cell, bottom_border);
1677 for (idx_type i = 1; i < number; ++i) {
1678 CellData & cs1 = cell_info[row + i][col];
1679 cs1.multirow = CELL_PART_OF_MULTIROW;
1680 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1688 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1690 row_type const row = cellRow(cell);
1691 col_type const col = cellColumn(cell);
1693 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1700 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1702 col_type const column = cellColumn(cell);
1703 col_type row = cellRow(cell) + 1;
1704 while (row < nrows() && isPartOfMultiRow(row, column))
1707 return row - cellRow(cell);
1711 void Tabular::unsetMultiColumn(idx_type cell)
1713 if (!isMultiColumn(cell))
1716 row_type const row = cellRow(cell);
1717 col_type const col = cellColumn(cell);
1718 row_type const span = columnSpan(cell);
1719 for (col_type c = 0; c < span; ++c) {
1720 // in the table dialog the lines are set in every case
1721 // when unsetting a multicolumn this leads to an additional right
1722 // line for every cell that was part of the former multicolumn cell,
1723 // except if the cell is in the last column
1724 // therefore remove this line
1725 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1726 && (col + c) < (col + span - 1))
1727 cell_info[row][col + c].right_line = false;
1728 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1734 void Tabular::unsetMultiRow(idx_type cell)
1736 if (!isMultiRow(cell))
1739 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1740 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1741 row_type const row = cellRow(cell);
1742 col_type const col = cellColumn(cell);
1743 row_type const span = rowSpan(cell);
1744 for (row_type r = 0; r < span; ++r)
1745 cell_info[row + r][col].multirow = CELL_NORMAL;
1750 void Tabular::setRotateCell(idx_type cell, bool flag)
1752 cellInfo(cell).rotate = flag;
1756 bool Tabular::getRotateCell(idx_type cell) const
1758 return cellInfo(cell).rotate;
1762 bool Tabular::needRotating() const
1766 for (row_type r = 0; r < nrows(); ++r)
1767 for (col_type c = 0; c < ncols(); ++c)
1768 if (cell_info[r][c].rotate)
1774 bool Tabular::isLastCell(idx_type cell) const
1776 if (cell + 1 < numberofcells)
1782 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1784 if (cellRow(cell) == 0)
1787 col_type const col = cellColumn(cell);
1788 row_type r = cellRow(cell) - 1;
1789 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1792 return cell_info[r][col].cellno;
1796 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1798 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1799 if (nextrow < nrows())
1800 return cell_info[nextrow][cellColumn(cell)].cellno;
1805 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1807 LASSERT(column != npos && column < ncols()
1808 && row != npos && row < nrows(), /**/);
1809 return cell_info[row][column].cellno;
1813 void Tabular::setUsebox(idx_type cell, BoxType type)
1815 cellInfo(cell).usebox = type;
1819 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1820 // adds line breaks, see bug 4886.
1821 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1823 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1824 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1826 if (cellInfo(cell).usebox > 1)
1827 return cellInfo(cell).usebox;
1828 return useParbox(cell);
1833 // This are functions used for the longtable support
1835 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1841 row_info[row].endfirsthead = flag;
1845 row_info[row].endhead = flag;
1850 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1853 hd.set = haveLTHead();
1854 return row_info[row].endhead;
1858 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1861 hd.set = haveLTFirstHead();
1862 return row_info[row].endfirsthead;
1866 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1872 row_info[row].endlastfoot = flag;
1876 row_info[row].endfoot = flag;
1881 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1884 fd.set = haveLTFoot();
1885 return row_info[row].endfoot;
1889 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1892 fd.set = haveLTLastFoot();
1893 return row_info[row].endlastfoot;
1897 void Tabular::setLTNewPage(row_type row, bool what)
1899 row_info[row].newpage = what;
1903 bool Tabular::getLTNewPage(row_type row) const
1905 return row_info[row].newpage;
1909 bool Tabular::haveLTHead() const
1911 if (!is_long_tabular)
1913 for (row_type i = 0; i < nrows(); ++i)
1914 if (row_info[i].endhead)
1920 bool Tabular::haveLTFirstHead() const
1922 if (!is_long_tabular || endfirsthead.empty)
1924 for (row_type r = 0; r < nrows(); ++r)
1925 if (row_info[r].endfirsthead)
1931 bool Tabular::haveLTFoot() const
1933 if (!is_long_tabular)
1935 for (row_type r = 0; r < nrows(); ++r)
1936 if (row_info[r].endfoot)
1942 bool Tabular::haveLTLastFoot() const
1944 if (!is_long_tabular || endlastfoot.empty)
1946 for (row_type r = 0; r < nrows(); ++r)
1947 if (row_info[r].endlastfoot)
1953 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1955 idx_type i = getFirstCellInRow(row);
1957 setMultiColumn(i, numberOfCellsInRow(row), false);
1958 setTopLine(i, false);
1959 setBottomLine(i, false);
1960 setLeftLine(i, false);
1961 setRightLine(i, false);
1963 unsetMultiColumn(i);
1964 // When unsetting a caption row, also all existing
1965 // captions in this row must be dissolved.
1967 row_info[row].caption = what;
1972 bool Tabular::ltCaption(row_type row) const
1974 return row_info[row].caption;
1978 bool Tabular::haveLTCaption() const
1980 if (!is_long_tabular)
1982 for (row_type r = 0; r < nrows(); ++r)
1983 if (row_info[r].caption)
1989 // end longtable support functions
1991 void Tabular::setRowAscent(row_type row, int height)
1993 if (row >= nrows() || row_info[row].ascent == height)
1995 row_info[row].ascent = height;
1999 void Tabular::setRowDescent(row_type row, int height)
2001 if (row >= nrows() || row_info[row].descent == height)
2003 row_info[row].descent = height;
2007 int Tabular::rowAscent(row_type row) const
2009 LASSERT(row < nrows(), /**/);
2010 return row_info[row].ascent;
2014 int Tabular::rowDescent(row_type row) const
2016 LASSERT(row < nrows(), /**/);
2017 return row_info[row].descent;
2021 int Tabular::height() const
2024 for (row_type row = 0; row < nrows(); ++row)
2025 height += rowAscent(row) + rowDescent(row) +
2031 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2033 LASSERT(row < nrows(), /**/);
2034 LASSERT(column < ncols(), /**/);
2035 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2039 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2041 LASSERT(row < nrows(), /**/);
2042 LASSERT(column < ncols(), /**/);
2043 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2047 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2049 // we only output complete row lines and the 1st row here, the rest
2050 // is done in Tabular::TeXBottomHLine(...)
2052 // get for each column the topline (if any)
2053 vector<bool> topline;
2055 for (col_type c = 0; c < ncols(); ++c) {
2056 topline.push_back(topLine(cellIndex(row, c)));
2057 // If cell is part of a multirow and not the first cell of the
2058 // multirow, no line must be drawn.
2060 if (isMultiRow(cellIndex(row, c))
2061 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2067 // do nothing if empty first row, or incomplete row line after
2068 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2071 // only output complete row lines and the 1st row's clines
2072 if (nset == ncols()) {
2074 os << (row == 0 ? "\\toprule " : "\\midrule ");
2078 } else if (row == 0) {
2079 for (col_type c = 0; c < ncols(); ++c) {
2081 col_type offset = 0;
2082 for (col_type j = 0 ; j < c; ++j)
2083 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2086 //babel makes the "-" character an active one, so we have to suppress this here
2087 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2088 if (lang == "slovak" || lang == "czech")
2089 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2090 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2092 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2094 col_type cstart = c;
2095 for ( ; c < ncols() && topline[c]; ++c) {}
2097 for (col_type j = cstart ; j < c ; ++j)
2098 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2101 os << c + offset << "} ";
2109 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2111 // we output bottomlines of row r and the toplines of row r+1
2112 // if the latter do not span the whole tabular
2114 // get the bottomlines of row r, and toplines in next row
2115 bool lastrow = row == nrows() - 1;
2116 vector<bool> bottomline, topline;
2117 bool nextrowset = true;
2118 for (col_type c = 0; c < ncols(); ++c) {
2119 bottomline.push_back(bottomLine(cellIndex(row, c)));
2120 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2121 // If cell is part of a multirow and not the last cell of the
2122 // multirow, no line must be drawn.
2124 if (isMultiRow(cellIndex(row, c))
2125 && isMultiRow(cellIndex(row + 1, c))
2126 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2127 bottomline[c] = false;
2130 nextrowset &= topline[c];
2133 // combine this row's bottom lines and next row's toplines if necessary
2135 for (col_type c = 0; c < ncols(); ++c) {
2137 bottomline[c] = bottomline[c] || topline[c];
2142 // do nothing if empty, OR incomplete row line with a topline in next row
2143 if (nset == 0 || (nextrowset && nset != ncols()))
2146 if (nset == ncols()) {
2148 os << (lastrow ? "\\bottomrule" : "\\midrule");
2152 for (col_type c = 0; c < ncols(); ++c) {
2153 if (bottomline[c]) {
2154 col_type offset = 0;
2155 for (col_type j = 0 ; j < c; ++j)
2156 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2159 //babel makes the "-" character an active one, so we have to suppress this here
2160 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2161 if (lang == "slovak" || lang == "czech")
2162 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2163 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2165 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2167 col_type cstart = c;
2168 for ( ; c < ncols() && bottomline[c]; ++c) {}
2170 for (col_type j = cstart ; j < c ; ++j)
2171 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2174 os << c + offset << "} ";
2182 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2183 bool & ismulticol, bool & ismultirow) const
2185 row_type const r = cellRow(cell);
2186 if (is_long_tabular && row_info[r].caption)
2189 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2190 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2191 // figure out how to set the lines
2192 // we always set double lines to the right of the cell
2193 col_type const c = cellColumn(cell);
2194 col_type const nextcol = c + columnSpan(cell);
2195 bool colright = columnRightLine(c);
2196 bool colleft = columnLeftLine(c);
2197 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2198 bool nextcellleft = nextcol < ncols()
2199 && leftLine(cellIndex(r, nextcol));
2200 bool coldouble = colright && nextcolleft;
2201 bool celldouble = rightLine(cell) && nextcellleft;
2203 ismulticol = isMultiColumn(cell)
2204 || (c == 0 && colleft != leftLine(cell))
2205 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2206 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2207 || (coldouble != celldouble);
2209 // we center in multicol when no decimal point
2210 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2211 docstring const align_d = column_info[c].decimal_point;
2212 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2216 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2217 int latexcolspan = columnSpan(cell);
2218 for(col_type col = c; col < c + columnSpan(cell); ++col)
2219 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2223 os << "\\multicolumn{" << latexcolspan << "}{";
2224 if (c ==0 && leftLine(cell))
2226 if (!cellInfo(cell).align_special.empty()) {
2227 os << cellInfo(cell).align_special;
2229 if (!getPWidth(cell).zero()) {
2231 case LYX_ALIGN_LEFT:
2232 os << ">{\\raggedright}";
2234 case LYX_ALIGN_RIGHT:
2235 os << ">{\\raggedleft}";
2237 case LYX_ALIGN_CENTER:
2238 os << ">{\\centering}";
2244 case LYX_VALIGN_TOP:
2247 case LYX_VALIGN_MIDDLE:
2250 case LYX_VALIGN_BOTTOM:
2255 << from_ascii(getPWidth(cell).asLatexString())
2259 case LYX_ALIGN_LEFT:
2262 case LYX_ALIGN_RIGHT:
2269 } // end if else !getPWidth
2270 } // end if else !cellinfo_of_cell
2271 if (rightLine(cell) || nextcellleft)
2274 // add extra vertical line if we want a double one
2277 } // end if ismulticol
2279 // we only need code for the first multirow cell
2280 ismultirow = isMultiRow(cell);
2282 os << "\\multirow{" << rowSpan(cell) << "}{";
2283 if (!getPWidth(cell).zero())
2284 os << from_ascii(getPWidth(cell).asLatexString());
2286 // we need to set a default value
2289 if (!getMROffset(cell).zero())
2290 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2292 } // end if ismultirow
2294 if (getRotateCell(cell)) {
2295 os << "\\begin{sideways}\n";
2297 if (getUsebox(cell) == BOX_PARBOX) {
2300 case LYX_VALIGN_TOP:
2303 case LYX_VALIGN_MIDDLE:
2306 case LYX_VALIGN_BOTTOM:
2310 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2312 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2313 os << "\\begin{minipage}[";
2315 case LYX_VALIGN_TOP:
2318 case LYX_VALIGN_MIDDLE:
2321 case LYX_VALIGN_BOTTOM:
2325 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2331 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2332 bool ismulticol, bool ismultirow) const
2334 row_type const r = cellRow(cell);
2335 if (is_long_tabular && row_info[r].caption)
2339 if (getUsebox(cell) == BOX_PARBOX)
2341 else if (getUsebox(cell) == BOX_MINIPAGE)
2342 os << breakln << "\\end{minipage}";
2343 if (getRotateCell(cell))
2344 os << breakln << "\\end{sideways}";
2352 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2353 OutputParams const & runparams) const
2355 if (!is_long_tabular)
2359 // the caption must be output before the headers
2360 if (haveLTCaption()) {
2361 for (row_type r = 0; r < nrows(); ++r) {
2362 if (row_info[r].caption)
2363 TeXRow(os, r, runparams);
2366 // output first header info
2367 // first header must be output before the header, otherwise the
2368 // correct caption placement becomes really weird
2369 if (haveLTFirstHead()) {
2370 if (endfirsthead.topDL)
2372 for (row_type r = 0; r < nrows(); ++r) {
2373 if (row_info[r].endfirsthead)
2374 TeXRow(os, r, runparams);
2376 if (endfirsthead.bottomDL)
2378 os << "\\endfirsthead\n";
2380 // output header info
2382 if (endfirsthead.empty && !haveLTFirstHead())
2383 os << "\\endfirsthead\n";
2386 for (row_type r = 0; r < nrows(); ++r) {
2387 if (row_info[r].endhead)
2388 TeXRow(os, r, runparams);
2390 if (endhead.bottomDL)
2392 os << "\\endhead\n";
2394 // output footer info
2398 for (row_type r = 0; r < nrows(); ++r) {
2399 if (row_info[r].endfoot)
2400 TeXRow(os, r, runparams);
2402 if (endfoot.bottomDL)
2404 os << "\\endfoot\n";
2405 if (endlastfoot.empty && !haveLTLastFoot())
2406 os << "\\endlastfoot\n";
2408 // output lastfooter info
2409 if (haveLTLastFoot()) {
2410 if (endlastfoot.topDL)
2412 for (row_type r = 0; r < nrows(); ++r) {
2413 if (row_info[r].endlastfoot)
2414 TeXRow(os, r, runparams);
2416 if (endlastfoot.bottomDL)
2418 os << "\\endlastfoot\n";
2423 bool Tabular::isValidRow(row_type row) const
2425 if (!is_long_tabular)
2427 return !row_info[row].endhead && !row_info[row].endfirsthead
2428 && !row_info[row].endfoot && !row_info[row].endlastfoot
2429 && !row_info[row].caption;
2433 void Tabular::TeXRow(otexstream & os, row_type row,
2434 OutputParams const & runparams) const
2436 idx_type cell = cellIndex(row, 0);
2437 shared_ptr<InsetTableCell> inset = cellInset(cell);
2438 Paragraph const & par = inset->paragraphs().front();
2439 string const lang = par.getParLanguage(buffer().params())->lang();
2441 //output the top line
2442 TeXTopHLine(os, row, lang);
2444 if (row_info[row].top_space_default) {
2446 os << "\\addlinespace\n";
2448 os << "\\noalign{\\vskip\\doublerulesep}\n";
2449 } else if(!row_info[row].top_space.zero()) {
2451 os << "\\addlinespace["
2452 << from_ascii(row_info[row].top_space.asLatexString())
2455 os << "\\noalign{\\vskip"
2456 << from_ascii(row_info[row].top_space.asLatexString())
2460 bool ismulticol = false;
2461 bool ismultirow = false;
2462 for (col_type c = 0; c < ncols(); ++c) {
2463 if (isPartOfMultiColumn(row, c))
2466 cell = cellIndex(row, c);
2468 if (isPartOfMultiRow(row, c)
2469 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2470 if (cell != getLastCellInRow(row))
2475 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2476 shared_ptr<InsetTableCell> inset = cellInset(cell);
2478 Paragraph const & par = inset->paragraphs().front();
2479 bool rtl = par.isRTL(buffer().params())
2481 && getPWidth(cell).zero();
2485 par.getParLanguage(buffer().params())->lang();
2486 if (lang == "farsi")
2488 else if (lang == "arabic_arabi")
2490 // currently, remaning RTL languages are
2491 // arabic_arabtex and hebrew
2495 // pass to the OutputParams that we are in a cell and
2496 // which alignment we have set.
2497 // InsetNewline needs this context information.
2498 OutputParams newrp(runparams);
2499 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2500 ? OutputParams::PLAIN
2501 : OutputParams::ALIGNED;
2503 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2504 // copy cell and split in 2
2505 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2506 head.setBuffer(buffer());
2507 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2509 dit.push_back(CursorSlice(head));
2510 head.setMacrocontextPositionRecursive(dit);
2511 bool hassep = false;
2512 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2513 head.latex(os, newrp);
2516 tail.setBuffer(head.buffer());
2518 dit.push_back(CursorSlice(tail));
2519 tail.setMacrocontextPositionRecursive(dit);
2520 tail.latex(os, newrp);
2522 } else if (!isPartOfMultiRow(row, c)) {
2523 if (!runparams.nice)
2524 os.texrow().start(par.id(), 0);
2525 inset->latex(os, newrp);
2528 runparams.encoding = newrp.encoding;
2532 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2533 if (cell != getLastCellInRow(row)) { // not last cell in row
2540 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2541 // if no first header and no empty first header is used,
2542 // the caption needs to be terminated by \endfirsthead
2544 os << "\\endfirsthead";
2546 os << "\\tabularnewline";
2547 if (row_info[row].bottom_space_default) {
2549 os << "\\addlinespace";
2551 os << "[\\doublerulesep]";
2552 } else if (!row_info[row].bottom_space.zero()) {
2554 os << "\\addlinespace";
2556 << from_ascii(row_info[row].bottom_space.asLatexString())
2561 //output the bottom line
2562 TeXBottomHLine(os, row, lang);
2564 if (row_info[row].interline_space_default) {
2566 os << "\\addlinespace\n";
2568 os << "\\noalign{\\vskip\\doublerulesep}\n";
2569 } else if (!row_info[row].interline_space.zero()) {
2571 os << "\\addlinespace["
2572 << from_ascii(row_info[row].interline_space.asLatexString())
2575 os << "\\noalign{\\vskip"
2576 << from_ascii(row_info[row].interline_space.asLatexString())
2582 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2584 bool const is_tabular_star = !tabular_width.zero();
2586 //+---------------------------------------------------------------------
2587 //+ first the opening preamble +
2588 //+---------------------------------------------------------------------
2591 if (runparams.lastid != -1)
2592 os.texrow().start(runparams.lastid, runparams.lastpos);
2595 os << "\\begin{sideways}\n";
2597 if (is_long_tabular) {
2598 os << "\\begin{longtable}";
2599 switch (longtabular_alignment) {
2600 case LYX_LONGTABULAR_ALIGN_LEFT:
2603 case LYX_LONGTABULAR_ALIGN_CENTER:
2605 case LYX_LONGTABULAR_ALIGN_RIGHT:
2610 if (is_tabular_star)
2611 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2613 os << "\\begin{tabular}";
2614 switch (tabular_valignment) {
2615 case LYX_VALIGN_TOP:
2618 case LYX_VALIGN_MIDDLE:
2620 case LYX_VALIGN_BOTTOM:
2628 if (is_tabular_star)
2629 os << "@{\\extracolsep{\\fill}}";
2631 for (col_type c = 0; c < ncols(); ++c) {
2632 if (columnLeftLine(c))
2634 if (!column_info[c].align_special.empty()) {
2635 os << column_info[c].align_special;
2637 if (!column_info[c].p_width.zero()) {
2638 switch (column_info[c].alignment) {
2639 case LYX_ALIGN_LEFT:
2640 os << ">{\\raggedright}";
2642 case LYX_ALIGN_RIGHT:
2643 os << ">{\\raggedleft}";
2645 case LYX_ALIGN_CENTER:
2646 os << ">{\\centering}";
2648 case LYX_ALIGN_NONE:
2649 case LYX_ALIGN_BLOCK:
2650 case LYX_ALIGN_LAYOUT:
2651 case LYX_ALIGN_SPECIAL:
2652 case LYX_ALIGN_DECIMAL:
2656 switch (column_info[c].valignment) {
2657 case LYX_VALIGN_TOP:
2660 case LYX_VALIGN_MIDDLE:
2663 case LYX_VALIGN_BOTTOM:
2668 << from_ascii(column_info[c].p_width.asLatexString())
2671 switch (column_info[c].alignment) {
2672 case LYX_ALIGN_LEFT:
2675 case LYX_ALIGN_RIGHT:
2678 case LYX_ALIGN_DECIMAL:
2679 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2685 } // end if else !column_info[i].p_width
2686 } // end if else !column_info[i].align_special
2687 if (columnRightLine(c))
2692 TeXLongtableHeaderFooter(os, runparams);
2694 //+---------------------------------------------------------------------
2695 //+ the single row and columns (cells) +
2696 //+---------------------------------------------------------------------
2698 for (row_type r = 0; r < nrows(); ++r) {
2699 if (isValidRow(r)) {
2700 TeXRow(os, r, runparams);
2701 if (is_long_tabular && row_info[r].newpage)
2702 os << "\\newpage\n";
2706 //+---------------------------------------------------------------------
2707 //+ the closing of the tabular +
2708 //+---------------------------------------------------------------------
2710 if (is_long_tabular)
2711 os << "\\end{longtable}";
2713 if (is_tabular_star)
2714 os << "\\end{tabular*}";
2716 os << "\\end{tabular}";
2720 os << breakln << "\\end{sideways}";
2724 int Tabular::docbookRow(odocstream & os, row_type row,
2725 OutputParams const & runparams) const
2728 idx_type cell = getFirstCellInRow(row);
2731 for (col_type c = 0; c < ncols(); ++c) {
2732 if (isPartOfMultiColumn(row, c))
2735 os << "<entry align=\"";
2736 switch (getAlignment(cell)) {
2737 case LYX_ALIGN_LEFT:
2740 case LYX_ALIGN_RIGHT:
2748 os << "\" valign=\"";
2749 switch (getVAlignment(cell)) {
2750 case LYX_VALIGN_TOP:
2753 case LYX_VALIGN_BOTTOM:
2756 case LYX_VALIGN_MIDDLE:
2761 if (isMultiColumn(cell)) {
2762 os << " namest=\"col" << c << "\" ";
2763 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2767 ret += cellInset(cell)->docbook(os, runparams);
2776 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2780 //+---------------------------------------------------------------------
2781 //+ first the opening preamble +
2782 //+---------------------------------------------------------------------
2784 os << "<tgroup cols=\"" << ncols()
2785 << "\" colsep=\"1\" rowsep=\"1\">\n";
2787 for (col_type c = 0; c < ncols(); ++c) {
2788 os << "<colspec colname=\"col" << c << "\" align=\"";
2789 switch (column_info[c].alignment) {
2790 case LYX_ALIGN_LEFT:
2793 case LYX_ALIGN_RIGHT:
2801 if (runparams.flavor == OutputParams::XML)
2807 //+---------------------------------------------------------------------
2808 //+ Long Tabular case +
2809 //+---------------------------------------------------------------------
2811 // output caption info
2812 if (haveLTCaption()) {
2813 os << "<caption>\n";
2815 for (row_type r = 0; r < nrows(); ++r) {
2816 if (row_info[r].caption) {
2817 ret += docbookRow(os, r, runparams);
2820 os << "</caption>\n";
2823 // output header info
2824 if (haveLTHead() || haveLTFirstHead()) {
2827 for (row_type r = 0; r < nrows(); ++r) {
2828 if (row_info[r].endhead || row_info[r].endfirsthead) {
2829 ret += docbookRow(os, r, runparams);
2835 // output footer info
2836 if (haveLTFoot() || haveLTLastFoot()) {
2839 for (row_type r = 0; r < nrows(); ++r) {
2840 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2841 ret += docbookRow(os, r, runparams);
2848 //+---------------------------------------------------------------------
2849 //+ the single row and columns (cells) +
2850 //+---------------------------------------------------------------------
2854 for (row_type r = 0; r < nrows(); ++r) {
2855 if (isValidRow(r)) {
2856 ret += docbookRow(os, r, runparams);
2861 //+---------------------------------------------------------------------
2862 //+ the closing of the tabular +
2863 //+---------------------------------------------------------------------
2872 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2873 OutputParams const & runparams, bool header) const
2876 string const celltag = header ? "th" : "td";
2877 idx_type cell = getFirstCellInRow(row);
2879 xs << html::StartTag("tr");
2880 for (col_type c = 0; c < ncols(); ++c) {
2881 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2886 switch (getAlignment(cell)) {
2887 case LYX_ALIGN_LEFT:
2890 case LYX_ALIGN_RIGHT:
2898 attr << " valign='";
2899 switch (getVAlignment(cell)) {
2900 case LYX_VALIGN_TOP:
2903 case LYX_VALIGN_BOTTOM:
2906 case LYX_VALIGN_MIDDLE:
2911 if (isMultiColumn(cell))
2912 attr << " colspan='" << columnSpan(cell) << "'";
2913 else if (isMultiRow(cell))
2914 attr << " rowspan='" << rowSpan(cell) << "'";
2916 xs << html::StartTag(celltag, attr.str()) << html::CR();
2917 ret += cellInset(cell)->xhtml(xs, runparams);
2918 xs << html::EndTag(celltag) << html::CR();
2921 xs << html::EndTag("tr");
2926 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2930 if (is_long_tabular) {
2931 // we'll wrap it in a div, so as to deal with alignment
2933 switch (longtabular_alignment) {
2934 case LYX_LONGTABULAR_ALIGN_LEFT:
2937 case LYX_LONGTABULAR_ALIGN_CENTER:
2940 case LYX_LONGTABULAR_ALIGN_RIGHT:
2944 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2946 if (haveLTCaption()) {
2947 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2949 for (row_type r = 0; r < nrows(); ++r)
2950 if (row_info[r].caption)
2951 ret += xhtmlRow(xs, r, runparams);
2952 xs << html::EndTag("div") << html::CR();
2956 xs << html::StartTag("table") << html::CR();
2958 // output header info
2959 bool const havefirsthead = haveLTFirstHead();
2960 // if we have a first head, then we are going to ignore the
2961 // headers for the additional pages, since there aren't any
2962 // in XHTML. this test accomplishes that.
2963 bool const havehead = !havefirsthead && haveLTHead();
2964 if (havehead || havefirsthead) {
2965 xs << html::StartTag("thead") << html::CR();
2966 for (row_type r = 0; r < nrows(); ++r) {
2967 if ((havefirsthead && row_info[r].endfirsthead)
2968 || (havehead && row_info[r].endhead)) {
2969 ret += xhtmlRow(xs, r, runparams, true);
2972 xs << html::EndTag("thead") << html::CR();
2974 // output footer info
2975 bool const havelastfoot = haveLTLastFoot();
2977 bool const havefoot = !havelastfoot && haveLTFoot();
2978 if (havefoot || havelastfoot) {
2979 xs << html::StartTag("tfoot") << html::CR();
2980 for (row_type r = 0; r < nrows(); ++r) {
2981 if ((havelastfoot && row_info[r].endlastfoot)
2982 || (havefoot && row_info[r].endfoot)) {
2983 ret += xhtmlRow(xs, r, runparams);
2986 xs << html::EndTag("tfoot") << html::CR();
2989 xs << html::StartTag("tbody") << html::CR();
2990 for (row_type r = 0; r < nrows(); ++r) {
2991 if (isValidRow(r)) {
2992 ret += xhtmlRow(xs, r, runparams);
2995 xs << html::EndTag("tbody")
2997 << html::EndTag("table")
2999 if (is_long_tabular)
3000 xs << html::EndTag("div") << html::CR();
3005 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3006 vector<unsigned int> const & clen) const
3008 idx_type const fcell = getFirstCellInRow(row);
3009 idx_type const n = numberOfCellsInRow(row) + fcell;
3012 for (idx_type i = fcell; i < n; ++i) {
3022 for (idx_type i = fcell; i < n; ++i) {
3033 col_type column = cellColumn(i);
3034 int len = clen[column];
3035 while (column < ncols() - 1
3036 && isPartOfMultiColumn(row, ++column))
3037 len += clen[column] + 4;
3038 os << docstring(len, ch);
3053 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3054 vector<unsigned int> const & clen) const
3056 idx_type const fcell = getFirstCellInRow(row);
3057 idx_type const n = numberOfCellsInRow(row) + fcell;
3060 for (idx_type i = fcell; i < n; ++i) {
3061 if (bottomLine(i)) {
3070 for (idx_type i = fcell; i < n; ++i) {
3071 if (bottomLine(i)) {
3081 col_type column = cellColumn(i);
3082 int len = clen[column];
3083 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3084 len += clen[column] + 4;
3085 os << docstring(len, ch);
3086 if (bottomLine(i)) {
3100 void Tabular::plaintextPrintCell(odocstream & os,
3101 OutputParams const & runparams,
3102 idx_type cell, row_type row, col_type column,
3103 vector<unsigned int> const & clen,
3104 bool onlydata) const
3106 odocstringstream sstr;
3107 cellInset(cell)->plaintext(sstr, runparams);
3119 unsigned int len1 = sstr.str().length();
3120 unsigned int len2 = clen[column];
3121 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3122 len2 += clen[column] + 4;
3125 switch (getAlignment(cell)) {
3127 case LYX_ALIGN_LEFT:
3130 case LYX_ALIGN_RIGHT:
3134 case LYX_ALIGN_CENTER:
3140 os << docstring(len1, ' ') << sstr.str()
3141 << docstring(len2, ' ');
3143 if (rightLine(cell))
3150 void Tabular::plaintext(odocstream & os,
3151 OutputParams const & runparams, int const depth,
3152 bool onlydata, char_type delim) const
3154 // first calculate the width of the single columns
3155 vector<unsigned int> clen(ncols());
3158 // first all non multicolumn cells!
3159 for (col_type c = 0; c < ncols(); ++c) {
3161 for (row_type r = 0; r < nrows(); ++r) {
3162 idx_type cell = cellIndex(r, c);
3163 if (isMultiColumn(cell))
3165 odocstringstream sstr;
3166 cellInset(cell)->plaintext(sstr, runparams);
3167 if (clen[c] < sstr.str().length())
3168 clen[c] = sstr.str().length();
3171 // then all multicolumn cells!
3172 for (col_type c = 0; c < ncols(); ++c) {
3173 for (row_type r = 0; r < nrows(); ++r) {
3174 idx_type cell = cellIndex(r, c);
3175 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3177 odocstringstream sstr;
3178 cellInset(cell)->plaintext(sstr, runparams);
3179 int len = int(sstr.str().length());
3180 idx_type const n = columnSpan(cell);
3181 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3183 if (len > int(clen[c + n - 1]))
3184 clen[c + n - 1] = len;
3189 for (row_type r = 0; r < nrows(); ++r) {
3190 if (!onlydata && plaintextTopHLine(os, r, clen))
3191 os << docstring(depth * 2, ' ');
3192 for (col_type c = 0; c < ncols(); ++c) {
3193 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3195 if (onlydata && c > 0)
3196 // we don't use operator<< for single UCS4 character.
3197 // see explanation in docstream.h
3199 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3204 os << docstring(depth * 2, ' ');
3205 if (plaintextBottomHLine(os, r, clen))
3206 os << docstring(depth * 2, ' ');
3212 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3214 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3218 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3219 col_type column) const
3221 return cell_info[row][column].inset;
3225 void Tabular::setCellInset(row_type row, col_type column,
3226 shared_ptr<InsetTableCell> ins) const
3228 CellData & cd = cell_info[row][column];
3233 void Tabular::validate(LaTeXFeatures & features) const
3235 features.require("NeedTabularnewline");
3237 features.require("booktabs");
3238 if (is_long_tabular)
3239 features.require("longtable");
3241 features.require("rotating");
3242 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3243 if (isMultiRow(cell))
3244 features.require("multirow");
3245 if (getVAlignment(cell) != LYX_VALIGN_TOP
3246 || !getPWidth(cell).zero())
3247 features.require("array");
3248 cellInset(cell)->validate(features);
3253 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3255 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3256 ParagraphList::const_iterator cit = parlist.begin();
3257 ParagraphList::const_iterator end = parlist.end();
3259 for (; cit != end; ++cit)
3260 for (int i = 0; i < cit->size(); ++i)
3261 if (cit->isNewline(i))
3268 /////////////////////////////////////////////////////////////////////
3272 /////////////////////////////////////////////////////////////////////
3274 InsetTableCell::InsetTableCell(Buffer * buf)
3275 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3276 contentAlign(LYX_ALIGN_CENTER)
3280 bool InsetTableCell::forcePlainLayout(idx_type) const
3282 return !isFixedWidth;
3286 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3288 return isFixedWidth;
3292 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3293 FuncStatus & status) const
3295 bool enabled = true;
3296 switch (cmd.action()) {
3298 enabled = !forcePlainLayout();
3300 case LFUN_LAYOUT_PARAGRAPH:
3301 enabled = allowParagraphCustomization();
3304 case LFUN_MATH_DISPLAY:
3305 if (!hasFixedWidth()) {
3310 return InsetText::getStatus(cur, cmd, status);
3312 status.setEnabled(enabled);
3316 docstring InsetTableCell::asString(bool intoInsets)
3319 if (paragraphs().empty())
3321 ParagraphList::const_iterator it = paragraphs().begin();
3322 ParagraphList::const_iterator en = paragraphs().end();
3324 for (; it != en; ++it) {
3329 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3335 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3338 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3339 return InsetText::xhtml(xs, rp);
3344 /////////////////////////////////////////////////////////////////////
3348 /////////////////////////////////////////////////////////////////////
3350 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3352 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3353 rowselect_(false), colselect_(false)
3358 InsetTabular::InsetTabular(InsetTabular const & tab)
3359 : Inset(tab), tabular(tab.tabular), scx_(0)
3364 InsetTabular::~InsetTabular()
3366 hideDialogs("tabular", this);
3370 void InsetTabular::setBuffer(Buffer & buf)
3372 tabular.setBuffer(buf);
3373 Inset::setBuffer(buf);
3377 bool InsetTabular::insetAllowed(InsetCode code) const
3382 case MATHMACRO_CODE:
3387 return tabular.is_long_tabular;
3395 void InsetTabular::write(ostream & os) const
3397 os << "Tabular" << endl;
3402 string InsetTabular::contextMenu(BufferView const &, int, int) const
3404 // FIXME: depending on the selection state,
3405 // we could offer a different menu.
3406 return cell(0)->contextMenuName() + ";" + contextMenuName();
3410 string InsetTabular::contextMenuName() const
3412 return "context-tabular";
3416 void InsetTabular::read(Lexer & lex)
3418 //bool const old_format = (lex.getString() == "\\LyXTable");
3426 string token = lex.getString();
3427 while (lex && token != "\\end_inset") {
3429 token = lex.getString();
3432 lex.printError("Missing \\end_inset at this point. ");
3436 int InsetTabular::rowFromY(Cursor & cur, int y) const
3438 // top y coordinate of tabular
3439 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3441 for (; r < tabular.nrows() && y > h; ++r)
3442 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3443 + tabular.interRowSpace(r);
3449 int InsetTabular::columnFromX(Cursor & cur, int x) const
3451 // left x coordinate of tabular
3452 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3454 for (; c < tabular.ncols() && x > w; ++c)
3455 w += tabular.cellWidth(c);
3460 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3462 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3463 // mi.base.textwidth << "\n";
3466 LASSERT(false, /**/);
3469 for (row_type r = 0; r < tabular.nrows(); ++r) {
3472 for (col_type c = 0; c < tabular.ncols(); ++c) {
3473 if (tabular.isPartOfMultiColumn(r, c)
3474 || tabular.isPartOfMultiRow(r, c))
3475 // multicolumn or multirow cell, but not first one
3477 idx_type const cell = tabular.cellIndex(r, c);
3480 Length const p_width = tabular.getPWidth(cell);
3481 if (!p_width.zero())
3482 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3483 tabular.cellInset(cell)->metrics(m, dim);
3484 if (!p_width.zero())
3485 dim.wid = m.base.textwidth;
3486 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3487 + tabular.interColumnSpace(cell);
3489 // FIXME(?): do we need a second metrics call?
3490 TextMetrics const & tm =
3491 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3493 // determine horizontal offset because of decimal align (if necessary)
3494 int decimal_width = 0;
3495 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3496 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3497 tail.setBuffer(tabular.buffer());
3498 // we need to set macrocontext position everywhere
3499 // otherwise we crash with nested insets (e.g. footnotes)
3500 // after decimal point
3501 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3503 dit.push_back(CursorSlice(tail));
3504 tail.setMacrocontextPositionRecursive(dit);
3506 // remove text leading decimal point
3507 docstring const align_d = tabular.column_info[c].decimal_point;
3508 dit = separatorPos(&tail, align_d);
3510 pit_type const psize = tail.paragraphs().front().size();
3512 tail.paragraphs().front().eraseChars(0,
3513 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3515 tail.metrics(m, dim1);
3516 decimal_width = dim1.width();
3519 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3520 tabular.cell_info[r][c].decimal_width = decimal_width;
3522 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3523 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3524 int const lastpardes = tm.last().second->descent()
3525 + TEXT_TO_INSET_OFFSET;
3527 switch (tabular.getVAlignment(cell)) {
3528 case Tabular::LYX_VALIGN_TOP:
3530 case Tabular::LYX_VALIGN_MIDDLE:
3531 offset = -(dim.des - lastpardes)/2;
3533 case Tabular::LYX_VALIGN_BOTTOM:
3534 offset = -(dim.des - lastpardes);
3537 tabular.cell_info[r][c].voffset = offset;
3538 maxasc = max(maxasc, dim.asc - offset);
3539 maxdes = max(maxdes, dim.des + offset);
3541 int const top_space = tabular.row_info[r].top_space_default ?
3542 default_line_space :
3543 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3544 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3545 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3546 default_line_space :
3547 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3548 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3551 // for top-alignment the first horizontal table line must be exactly at
3552 // the position of the base line of the surrounding text line
3553 // for bottom alignment, the same is for the last table line
3554 switch (tabular.tabular_valignment) {
3555 case Tabular::LYX_VALIGN_BOTTOM:
3556 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3558 case Tabular::LYX_VALIGN_MIDDLE:
3559 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3561 case Tabular::LYX_VALIGN_TOP:
3562 offset_valign_ = tabular.rowAscent(0);
3566 tabular.updateColumnWidths();
3567 dim.asc = tabular.rowAscent(0) - offset_valign_;
3568 dim.des = tabular.height() - dim.asc;
3569 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3573 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3576 if (&cur.inset() == this && cur.selection()) {
3577 if (cur.selIsMultiCell()) {
3580 getSelection(cur, rs, re, cs, ce);
3582 idx_type const cell = tabular.cellIndex(row, col);
3583 col_type const cspan = tabular.columnSpan(cell);
3584 row_type const rspan = tabular.rowSpan(cell);
3585 if (col + cspan - 1 >= cs && col <= ce
3586 && row + rspan - 1 >= rs && row <= re)
3589 if (col == tabular.cellColumn(cur.idx())
3590 && row == tabular.cellRow(cur.idx())) {
3591 CursorSlice const & beg = cur.selBegin();
3592 CursorSlice const & end = cur.selEnd();
3594 if ((end.lastpos() > 0 || end.lastpit() > 0)
3595 && end.pos() == end.lastpos() && beg.pos() == 0
3596 && end.pit() == end.lastpit() && beg.pit() == 0)
3604 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3606 x += scx_ + ADD_TO_TABULAR_WIDTH;
3608 BufferView * bv = pi.base.bv;
3609 Cursor & cur = pi.base.bv->cursor();
3612 // FIXME: As the full background is painted in drawBackground(),
3613 // we have no choice but to do a full repaint for the Text cells.
3614 pi.full_repaint = true;
3616 bool const original_selection_state = pi.selected;
3619 first_visible_cell = Tabular::npos;
3621 int yy = y + offset_valign_;
3622 for (row_type r = 0; r < tabular.nrows(); ++r) {
3624 for (col_type c = 0; c < tabular.ncols(); ++c) {
3625 if (tabular.isPartOfMultiColumn(r, c))
3628 idx = tabular.cellIndex(r, c);
3630 if (tabular.isPartOfMultiRow(r, c)) {
3631 nx += tabular.cellWidth(idx);
3635 if (first_visible_cell == Tabular::npos)
3636 first_visible_cell = idx;
3638 pi.selected |= isCellSelected(cur, r, c);
3639 int const cx = nx + tabular.textHOffset(idx);
3640 int const cy = yy + tabular.textVOffset(idx);
3641 // Cache the Inset position.
3642 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3643 cell(idx)->draw(pi, cx, cy);
3644 drawCellLines(pi, nx, yy, r, idx);
3645 nx += tabular.cellWidth(idx);
3646 pi.selected = original_selection_state;
3649 if (r + 1 < tabular.nrows())
3650 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3651 + tabular.interRowSpace(r + 1);
3656 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3658 x += scx_ + ADD_TO_TABULAR_WIDTH;
3659 y += offset_valign_ - tabular.rowAscent(0);
3660 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3661 pi.backgroundColor(this));
3665 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3667 Cursor & cur = pi.base.bv->cursor();
3670 x += scx_ + ADD_TO_TABULAR_WIDTH;
3672 if (!cur.selection())
3674 if (&cur.inset() != this)
3679 bool const full_cell_selected = isCellSelected(cur,
3680 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3682 if (cur.selIsMultiCell() || full_cell_selected) {
3683 for (row_type r = 0; r < tabular.nrows(); ++r) {
3685 for (col_type c = 0; c < tabular.ncols(); ++c) {
3686 if (tabular.isPartOfMultiColumn(r, c))
3689 idx_type const cell = tabular.cellIndex(r, c);
3691 if (tabular.isPartOfMultiRow(r, c)) {
3692 xx += tabular.cellWidth(cell);
3695 int const w = tabular.cellWidth(cell);
3696 int const h = tabular.cellHeight(cell);
3697 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3698 if (isCellSelected(cur, r, c))
3699 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3702 if (r + 1 < tabular.nrows())
3703 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3704 + tabular.interRowSpace(r + 1);
3708 // FIXME: This code has no effect because InsetTableCell does not handle
3709 // drawSelection other than the trivial implementation in Inset.
3711 // x += cellXPos(cur.idx());
3712 // x += tabular.textHOffset(cur.idx());
3713 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3718 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3719 row_type row, idx_type cell) const
3721 y -= tabular.rowAscent(row);
3722 int const w = tabular.cellWidth(cell);
3723 int const h = tabular.cellHeight(cell);
3724 Color const linecolor = pi.textColor(Color_tabularline);
3725 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3728 bool drawline = tabular.topLine(cell)
3729 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3730 pi.pain.line(x, y, x + w, y,
3731 drawline ? linecolor : gridcolor,
3732 drawline ? Painter::line_solid : Painter::line_onoffdash);
3735 drawline = tabular.bottomLine(cell);
3736 pi.pain.line(x, y + h, x + w, y + h,
3737 drawline ? linecolor : gridcolor,
3738 drawline ? Painter::line_solid : Painter::line_onoffdash);
3741 col_type const col = tabular.cellColumn(cell);
3742 drawline = tabular.leftLine(cell)
3743 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3744 pi.pain.line(x, y, x, y + h,
3745 drawline ? linecolor : gridcolor,
3746 drawline ? Painter::line_solid : Painter::line_onoffdash);
3749 x -= tabular.interColumnSpace(cell);
3750 drawline = tabular.rightLine(cell)
3751 || (col + 1 < tabular.ncols()
3752 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3753 pi.pain.line(x + w, y, x + w, y + h,
3754 drawline ? linecolor : gridcolor,
3755 drawline ? Painter::line_solid : Painter::line_onoffdash);
3759 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3761 //lyxerr << "InsetTabular::edit: " << this << endl;
3763 cur.setSelection(false);
3766 if (isRightToLeft(cur))
3767 cur.idx() = tabular.getLastCellInRow(0);
3773 if (isRightToLeft(cur))
3774 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3776 cur.idx() = tabular.numberofcells - 1;
3778 cur.pos() = cur.lastpos(); // FIXME crude guess
3780 cur.setCurrentFont();
3781 // FIXME: this accesses the position cache before it is initialized
3783 //cur.bv().fitCursor();
3787 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3789 // In a longtable, tell captions what the current float is
3790 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3791 string const saveflt = cnts.current_float();
3792 if (tabular.is_long_tabular)
3793 cnts.current_float("table");
3795 ParIterator it2 = it;
3797 size_t const end = it2.nargs();
3798 for ( ; it2.idx() < end; it2.top().forwardIdx())
3799 buffer().updateBuffer(it2, utype);
3802 if (tabular.is_long_tabular)
3803 cnts.current_float(saveflt);
3807 void InsetTabular::addToToc(DocIterator const & cpit) const
3809 DocIterator dit = cpit;
3811 size_t const end = dit.nargs();
3812 for ( ; dit.idx() < end; dit.top().forwardIdx())
3813 cell(dit.idx())->addToToc(dit);
3817 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3819 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3820 << "\n cur:" << cur);
3821 CursorSlice sl = cur.top();
3822 Cursor & bvcur = cur.bv().cursor();
3824 FuncCode const act = cmd.action();
3828 case LFUN_MOUSE_PRESS: {
3829 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3831 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3832 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3833 row_type r = rowFromY(cur, cmd.y());
3834 cur.idx() = tabular.getFirstCellInRow(r);
3837 cur.idx() = tabular.getLastCellInRow(r);
3838 cur.pos() = cur.lastpos();
3839 cur.setSelection(true);
3845 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3846 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3847 || cmd.y() > y0 + tabular.height()) {
3848 col_type c = columnFromX(cur, cmd.x());
3849 cur.idx() = tabular.cellIndex(0, c);
3852 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3853 cur.pos() = cur.lastpos();
3854 cur.setSelection(true);
3859 // do not reset cursor/selection if we have selected
3860 // some cells (bug 2715).
3861 if (cmd.button() == mouse_button::button3
3862 && &bvcur.selBegin().inset() == this
3863 && bvcur.selIsMultiCell())
3866 // Let InsetTableCell do it
3867 cell(cur.idx())->dispatch(cur, cmd);
3870 case LFUN_MOUSE_MOTION:
3871 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3872 if (cmd.button() == mouse_button::button1) {
3873 // only accept motions to places not deeper nested than the real anchor
3874 if (!bvcur.realAnchor().hasPart(cur)) {
3878 // select (additional) row
3880 row_type r = rowFromY(cur, cmd.y());
3881 cur.idx() = tabular.getLastCellInRow(r);
3882 // we need to reset the cursor's pit and pos now, as the old ones
3883 // may no longer be valid.
3886 bvcur.setCursor(cur);
3887 bvcur.setSelection(true);
3890 // select (additional) column
3892 col_type c = columnFromX(cur, cmd.x());
3893 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3894 // we need to reset the cursor's pit and pos now, as the old ones
3895 // may no longer be valid.
3898 bvcur.setCursor(cur);
3899 bvcur.setSelection(true);
3902 // only update if selection changes
3903 if (bvcur.idx() == cur.idx() &&
3904 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3905 cur.noScreenUpdate();
3906 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3907 bvcur.setCursor(cur);
3908 bvcur.setSelection(true);
3909 // if this is a multicell selection, we just set the cursor to
3910 // the beginning of the cell's text.
3911 if (bvcur.selIsMultiCell()) {
3912 bvcur.pit() = bvcur.lastpit();
3913 bvcur.pos() = bvcur.lastpos();
3918 case LFUN_MOUSE_RELEASE:
3923 case LFUN_CELL_BACKWARD:
3925 cur.setSelection(false);
3928 case LFUN_CELL_FORWARD:
3930 cur.setSelection(false);
3933 case LFUN_CHAR_FORWARD_SELECT:
3934 case LFUN_CHAR_FORWARD:
3935 case LFUN_CHAR_BACKWARD_SELECT:
3936 case LFUN_CHAR_BACKWARD:
3937 case LFUN_CHAR_RIGHT_SELECT:
3938 case LFUN_CHAR_RIGHT:
3939 case LFUN_CHAR_LEFT_SELECT:
3940 case LFUN_CHAR_LEFT:
3941 case LFUN_WORD_FORWARD:
3942 case LFUN_WORD_FORWARD_SELECT:
3943 case LFUN_WORD_BACKWARD:
3944 case LFUN_WORD_BACKWARD_SELECT:
3945 case LFUN_WORD_RIGHT:
3946 case LFUN_WORD_RIGHT_SELECT:
3947 case LFUN_WORD_LEFT:
3948 case LFUN_WORD_LEFT_SELECT: {
3949 // determine whether we move to next or previous cell, where to enter
3950 // the new cell from, and which command to "finish" (i.e., exit the
3953 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3954 FuncCode finish_lfun;
3956 if (act == LFUN_CHAR_FORWARD
3957 || act == LFUN_CHAR_FORWARD_SELECT
3958 || act == LFUN_WORD_FORWARD
3959 || act == LFUN_WORD_FORWARD_SELECT) {
3961 finish_lfun = LFUN_FINISHED_FORWARD;
3963 else if (act == LFUN_CHAR_BACKWARD
3964 || act == LFUN_CHAR_BACKWARD_SELECT
3965 || act == LFUN_WORD_BACKWARD
3966 || act == LFUN_WORD_BACKWARD_SELECT) {
3968 finish_lfun = LFUN_FINISHED_BACKWARD;
3970 // LEFT or RIGHT commands --- the interpretation will depend on the
3971 // table's direction.
3973 bool const right = act == LFUN_CHAR_RIGHT
3974 || act == LFUN_CHAR_RIGHT_SELECT
3975 || act == LFUN_WORD_RIGHT
3976 || act == LFUN_WORD_RIGHT_SELECT;
3977 next_cell = isRightToLeft(cur) != right;
3979 if (lyxrc.visual_cursor)
3980 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3982 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3985 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3986 || act == LFUN_CHAR_BACKWARD_SELECT
3987 || act == LFUN_CHAR_RIGHT_SELECT
3988 || act == LFUN_CHAR_LEFT_SELECT
3989 || act == LFUN_WORD_FORWARD_SELECT
3990 || act == LFUN_WORD_RIGHT_SELECT
3991 || act == LFUN_WORD_BACKWARD_SELECT
3992 || act == LFUN_WORD_LEFT_SELECT;
3994 // If we have a multicell selection or we're
3995 // not doing some LFUN_*_SELECT thing anyway...
3996 if (!cur.selIsMultiCell() || !select) {
3997 col_type const c = tabular.cellColumn(cur.idx());
3998 row_type const r = tabular.cellRow(cur.idx());
3999 // Are we trying to select the whole cell and is the whole cell
4000 // not yet selected?
4001 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4002 ((next_cell && cur.pit() == cur.lastpit()
4003 && cur.pos() == cur.lastpos())
4004 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4006 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4008 // ...try to dispatch to the cell's inset.
4009 cell(cur.idx())->dispatch(cur, cmd);
4011 // When we already have a selection we want to select the whole cell
4012 // before going to the next cell.
4013 if (select_whole && !empty_cell){
4014 getText(cur.idx())->selectAll(cur);
4019 // FIXME: When we support the selection of an empty cell, remove
4020 // the !empty_cell from this condition. For now we jump to the next
4021 // cell if the current cell is empty.
4022 if (cur.result().dispatched() && !empty_cell)
4026 // move to next/prev cell, as appropriate
4027 // note that we will always do this if we're selecting and we have
4028 // a multicell selection
4029 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4030 << " cell from: " << int(entry_from));
4032 moveNextCell(cur, entry_from);
4034 movePrevCell(cur, entry_from);
4035 // if we're exiting the table, call the appropriate FINISHED lfun
4036 if (sl == cur.top())
4037 cmd = FuncRequest(finish_lfun);
4041 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4046 case LFUN_DOWN_SELECT:
4048 if (!(cur.selection() && cur.selIsMultiCell()))
4049 cell(cur.idx())->dispatch(cur, cmd);
4051 cur.dispatched(); // override the cell's decision
4052 if (sl == cur.top()) {
4053 // if our Text didn't do anything to the cursor
4054 // then we try to put the cursor into the cell below
4055 // setting also the right targetX.
4056 cur.selHandle(act == LFUN_DOWN_SELECT);
4057 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4058 int const xtarget = cur.targetX();
4059 // WARNING: Once cur.idx() has been reset, the cursor is in
4060 // an inconsistent state until pos() has been set. Be careful
4061 // what you do with it!
4062 cur.idx() = tabular.cellBelow(cur.idx());
4064 TextMetrics const & tm =
4065 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4066 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4067 cur.setCurrentFont();
4070 if (sl == cur.top()) {
4071 // we trick it to go to forward after leaving the
4073 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4076 if (cur.selIsMultiCell()) {
4077 cur.pit() = cur.lastpit();
4078 cur.pos() = cur.lastpos();
4079 cur.setCurrentFont();
4082 cur.screenUpdateFlags(Update::FitCursor);
4085 case LFUN_UP_SELECT:
4087 if (!(cur.selection() && cur.selIsMultiCell()))
4088 cell(cur.idx())->dispatch(cur, cmd);
4089 cur.dispatched(); // override the cell's decision
4090 if (sl == cur.top()) {
4091 // if our Text didn't do anything to the cursor
4092 // then we try to put the cursor into the cell above
4093 // setting also the right targetX.
4094 cur.selHandle(act == LFUN_UP_SELECT);
4095 if (tabular.cellRow(cur.idx()) != 0) {
4096 int const xtarget = cur.targetX();
4097 // WARNING: Once cur.idx() has been reset, the cursor is in
4098 // an inconsistent state until pos() has been set. Be careful
4099 // what you do with it!
4100 cur.idx() = tabular.cellAbove(cur.idx());
4101 cur.pit() = cur.lastpit();
4102 Text const * text = cell(cur.idx())->getText(0);
4103 TextMetrics const & tm = cur.bv().textMetrics(text);
4104 ParagraphMetrics const & pm =
4105 tm.parMetrics(cur.lastpit());
4106 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4107 cur.setCurrentFont();
4110 if (sl == cur.top()) {
4111 cmd = FuncRequest(LFUN_UP);
4114 if (cur.selIsMultiCell()) {
4116 cur.pos() = cur.lastpos();
4117 cur.setCurrentFont();
4120 cur.screenUpdateFlags(Update::FitCursor);
4123 // case LFUN_SCREEN_DOWN: {
4124 // //if (hasSelection())
4125 // // cur.selection() = false;
4126 // col_type const col = tabular.cellColumn(cur.idx());
4127 // int const t = cur.bv().top_y() + cur.bv().height();
4128 // if (t < yo() + tabular.getHeightOfTabular()) {
4129 // cur.bv().scrollDocView(t, true);
4130 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4132 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4139 // case LFUN_SCREEN_UP: {
4140 // //if (hasSelection())
4141 // // cur.selection() = false;
4142 // col_type const col = tabular.cellColumn(cur.idx());
4143 // int const t = cur.bv().top_y() + cur.bv().height();
4145 // cur.bv().scrollDocView(t, true);
4149 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4153 // cur.par() = cur.lastpar();
4154 // cur.pos() = cur.lastpos();
4158 case LFUN_LAYOUT_TABULAR:
4159 cur.bv().showDialog("tabular");
4162 case LFUN_INSET_MODIFY: {
4164 if (cmd.getArg(1) == "from-dialog")
4165 arg = cmd.getArg(0) + to_utf8(cmd.argument().substr(19));
4167 arg = to_utf8(cmd.argument());
4168 if (!tabularFeatures(cur, arg))
4173 // insert file functions
4174 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4175 case LFUN_FILE_INSERT_PLAINTEXT:
4177 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4178 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4179 FileName(to_utf8(cmd.argument())));
4182 cur.recordUndoInset(INSERT_UNDO);
4183 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4184 // content has been replaced,
4185 // so cursor might be invalid
4186 cur.pos() = cur.lastpos();
4187 cur.pit() = cur.lastpit();
4188 bvcur.setCursor(cur);
4195 if (cur.selIsMultiCell()) {
4196 if (copySelection(cur)) {
4197 cur.recordUndoInset(DELETE_UNDO);
4201 cell(cur.idx())->dispatch(cur, cmd);
4204 case LFUN_SELF_INSERT:
4205 if (cur.selIsMultiCell()) {
4206 cur.recordUndoInset(DELETE_UNDO);
4209 cell(cur.idx())->dispatch(cur, cmd);
4212 case LFUN_CHAR_DELETE_BACKWARD:
4213 case LFUN_CHAR_DELETE_FORWARD:
4214 if (cur.selIsMultiCell()) {
4215 cur.recordUndoInset(DELETE_UNDO);
4218 cell(cur.idx())->dispatch(cur, cmd);
4222 if (!cur.selection())
4224 if (cur.selIsMultiCell()) {
4228 cell(cur.idx())->dispatch(cur, cmd);
4231 case LFUN_CLIPBOARD_PASTE:
4232 case LFUN_PRIMARY_SELECTION_PASTE: {
4233 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4234 theClipboard().getAsText() :
4235 theSelection().get();
4238 // pass to InsertPlaintextString, but
4239 // only if we have multi-cell content
4240 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4241 cur.recordUndoInset(INSERT_UNDO);
4242 if (insertPlaintextString(cur.bv(), clip, false)) {
4243 // content has been replaced,
4244 // so cursor might be invalid
4245 cur.pos() = cur.lastpos();
4246 cur.pit() = cur.lastpit();
4247 bvcur.setCursor(cur);
4251 // Let the cell handle normal text
4252 cell(cur.idx())->dispatch(cur, cmd);
4257 if (!tabularStackDirty()) {
4258 if (!cur.selIsMultiCell())
4259 cell(cur.idx())->dispatch(cur, cmd);
4262 if (theClipboard().isInternal()) {
4263 cur.recordUndoInset(INSERT_UNDO);
4264 pasteClipboard(cur);
4268 case LFUN_FONT_EMPH:
4269 case LFUN_FONT_BOLD:
4270 case LFUN_FONT_BOLDSYMBOL:
4271 case LFUN_FONT_ROMAN:
4272 case LFUN_FONT_NOUN:
4273 case LFUN_FONT_ITAL:
4274 case LFUN_FONT_FRAK:
4275 case LFUN_FONT_TYPEWRITER:
4276 case LFUN_FONT_SANS:
4277 case LFUN_TEXTSTYLE_APPLY:
4278 case LFUN_TEXTSTYLE_UPDATE:
4279 case LFUN_FONT_SIZE:
4280 case LFUN_FONT_UNDERLINE:
4281 case LFUN_FONT_STRIKEOUT:
4282 case LFUN_FONT_UULINE:
4283 case LFUN_FONT_UWAVE:
4285 case LFUN_WORD_CAPITALIZE:
4286 case LFUN_WORD_UPCASE:
4287 case LFUN_WORD_LOWCASE:
4288 case LFUN_CHARS_TRANSPOSE:
4289 if (cur.selIsMultiCell()) {
4292 getSelection(cur, rs, re, cs, ce);
4293 Cursor tmpcur = cur;
4294 for (row_type r = rs; r <= re; ++r) {
4295 for (col_type c = cs; c <= ce; ++c) {
4296 // cursor follows cell:
4297 tmpcur.idx() = tabular.cellIndex(r, c);
4298 // select this cell only:
4301 tmpcur.resetAnchor();
4302 tmpcur.pit() = tmpcur.lastpit();
4303 tmpcur.pos() = tmpcur.top().lastpos();
4304 tmpcur.setCursor(tmpcur);
4305 tmpcur.setSelection();
4306 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4311 cell(cur.idx())->dispatch(cur, cmd);
4315 case LFUN_INSET_SETTINGS:
4316 // relay this lfun to Inset, not to the cell.
4317 Inset::doDispatch(cur, cmd);
4321 // we try to handle this event in the insets dispatch function.
4322 cell(cur.idx())->dispatch(cur, cmd);
4328 // function sets an object as defined in func_status.h:
4329 // states OK, Unknown, Disabled, On, Off.
4330 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4331 FuncStatus & status) const
4333 switch (cmd.action()) {
4334 case LFUN_INSET_MODIFY: {
4335 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4338 // FIXME: We only check for the very first argument...
4339 string const s = cmd.getArg(1);
4340 // We always enable the lfun if it is coming from the dialog
4341 // because the dialog makes sure all the settings are valid,
4342 // even though the first argument might not be valid now.
4343 if (s == "from-dialog") {
4344 status.setEnabled(true);
4348 int action = Tabular::LAST_ACTION;
4350 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4351 if (tabularFeature[i].feature == s) {
4352 action = tabularFeature[i].action;
4356 if (action == Tabular::LAST_ACTION) {
4358 status.setUnknown(true);
4362 string const argument = cmd.getLongArg(2);
4364 row_type sel_row_start = 0;
4365 row_type sel_row_end = 0;
4366 col_type sel_col_start = 0;
4367 col_type sel_col_end = 0;
4368 Tabular::ltType dummyltt;
4371 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4374 case Tabular::SET_PWIDTH:
4375 case Tabular::SET_MPWIDTH:
4376 case Tabular::SET_SPECIAL_COLUMN:
4377 case Tabular::SET_SPECIAL_MULTICOLUMN:
4378 case Tabular::APPEND_ROW:
4379 case Tabular::APPEND_COLUMN:
4380 case Tabular::DELETE_ROW:
4381 case Tabular::DELETE_COLUMN:
4382 case Tabular::COPY_ROW:
4383 case Tabular::COPY_COLUMN:
4384 case Tabular::SET_TOP_SPACE:
4385 case Tabular::SET_BOTTOM_SPACE:
4386 case Tabular::SET_INTERLINE_SPACE:
4390 case Tabular::SET_TABULAR_WIDTH:
4391 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4392 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4395 case Tabular::SET_DECIMAL_POINT:
4397 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4400 case Tabular::SET_MULTICOLUMN:
4401 case Tabular::UNSET_MULTICOLUMN:
4402 case Tabular::MULTICOLUMN:
4403 // If a row is set as longtable caption, it must not be allowed
4404 // to unset that this row is a multicolumn.
4405 status.setEnabled(sel_row_start == sel_row_end
4406 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4407 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4410 case Tabular::SET_MULTIROW:
4411 case Tabular::UNSET_MULTIROW:
4412 case Tabular::MULTIROW:
4413 // If a row is set as longtable caption, it must not be allowed
4414 // to unset that this row is a multirow.
4415 status.setEnabled(sel_col_start == sel_col_end
4416 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4417 status.setOnOff(tabular.isMultiRow(cur.idx()));
4420 case Tabular::SET_ALL_LINES:
4421 case Tabular::UNSET_ALL_LINES:
4422 case Tabular::SET_BORDER_LINES:
4423 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4426 case Tabular::SET_LINE_TOP:
4427 case Tabular::SET_LINE_BOTTOM:
4428 case Tabular::SET_LINE_LEFT:
4429 case Tabular::SET_LINE_RIGHT:
4430 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4433 case Tabular::TOGGLE_LINE_TOP:
4434 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4435 status.setOnOff(tabular.topLine(cur.idx()));
4438 case Tabular::TOGGLE_LINE_BOTTOM:
4439 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4440 status.setOnOff(tabular.bottomLine(cur.idx()));
4443 case Tabular::TOGGLE_LINE_LEFT:
4444 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4445 status.setOnOff(tabular.leftLine(cur.idx()));
4448 case Tabular::TOGGLE_LINE_RIGHT:
4449 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4450 status.setOnOff(tabular.rightLine(cur.idx()));
4453 // multirow cells only inherit the alignment of the column if the column has
4454 // no width, otherwise they are left-aligned
4455 // therefore allow always left but right and center only if there is no width
4456 case Tabular::M_ALIGN_LEFT:
4458 case Tabular::ALIGN_LEFT:
4459 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4462 case Tabular::M_ALIGN_RIGHT:
4464 case Tabular::ALIGN_RIGHT:
4465 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4466 && !tabular.getPWidth(cur.idx()).zero()));
4467 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4470 case Tabular::M_ALIGN_CENTER:
4472 case Tabular::ALIGN_CENTER:
4473 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4474 && !tabular.getPWidth(cur.idx()).zero()));
4475 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4478 case Tabular::ALIGN_BLOCK:
4479 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4480 && !tabular.isMultiRow(cur.idx()));
4481 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4484 case Tabular::ALIGN_DECIMAL:
4485 status.setEnabled(!tabular.isMultiRow(cur.idx())
4486 && !tabular.isMultiColumn(cur.idx()));
4487 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4490 case Tabular::M_VALIGN_TOP:
4492 case Tabular::VALIGN_TOP:
4493 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4494 && !tabular.isMultiRow(cur.idx()));
4496 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4499 case Tabular::M_VALIGN_BOTTOM:
4501 case Tabular::VALIGN_BOTTOM:
4502 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4503 && !tabular.isMultiRow(cur.idx()));
4505 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4508 case Tabular::M_VALIGN_MIDDLE:
4510 case Tabular::VALIGN_MIDDLE:
4511 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4512 && !tabular.isMultiRow(cur.idx()));
4514 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4517 case Tabular::SET_LONGTABULAR:
4518 // setting as longtable is not allowed when table is inside a float
4519 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4520 || cur.innerInsetOfType(WRAP_CODE) != 0)
4521 status.setEnabled(false);
4523 status.setEnabled(true);
4524 status.setOnOff(tabular.is_long_tabular);
4527 case Tabular::UNSET_LONGTABULAR:
4528 status.setOnOff(!tabular.is_long_tabular);
4531 case Tabular::TOGGLE_ROTATE_TABULAR:
4532 case Tabular::SET_ROTATE_TABULAR:
4533 status.setEnabled(tabular.tabular_width.zero());
4534 status.setOnOff(tabular.rotate);
4537 case Tabular::TABULAR_VALIGN_TOP:
4538 status.setEnabled(tabular.tabular_width.zero());
4539 status.setOnOff(tabular.tabular_valignment
4540 == Tabular::LYX_VALIGN_TOP);
4542 case Tabular::TABULAR_VALIGN_MIDDLE:
4543 status.setEnabled(tabular.tabular_width.zero());
4544 status.setOnOff(tabular.tabular_valignment
4545 == Tabular::LYX_VALIGN_MIDDLE);
4547 case Tabular::TABULAR_VALIGN_BOTTOM:
4548 status.setEnabled(tabular.tabular_width.zero());
4549 status.setOnOff(tabular.tabular_valignment
4550 == Tabular::LYX_VALIGN_BOTTOM);
4553 case Tabular::LONGTABULAR_ALIGN_LEFT:
4554 status.setOnOff(tabular.longtabular_alignment
4555 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4557 case Tabular::LONGTABULAR_ALIGN_CENTER:
4558 status.setOnOff(tabular.longtabular_alignment
4559 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4561 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4562 status.setOnOff(tabular.longtabular_alignment
4563 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4566 case Tabular::UNSET_ROTATE_TABULAR:
4567 status.setOnOff(!tabular.rotate);
4570 case Tabular::TOGGLE_ROTATE_CELL:
4571 case Tabular::SET_ROTATE_CELL:
4572 status.setOnOff(!oneCellHasRotationState(false,
4573 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4576 case Tabular::UNSET_ROTATE_CELL:
4577 status.setOnOff(!oneCellHasRotationState(true,
4578 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4581 case Tabular::SET_USEBOX:
4582 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4585 // every row can only be one thing:
4586 // either a footer or header or caption
4587 case Tabular::SET_LTFIRSTHEAD:
4588 status.setEnabled(sel_row_start == sel_row_end
4589 && !tabular.ltCaption(sel_row_start));
4590 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4593 case Tabular::UNSET_LTFIRSTHEAD:
4594 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4597 case Tabular::SET_LTHEAD:
4598 status.setEnabled(sel_row_start == sel_row_end
4599 && !tabular.ltCaption(sel_row_start));
4600 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4603 case Tabular::UNSET_LTHEAD:
4604 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4607 case Tabular::SET_LTFOOT:
4608 status.setEnabled(sel_row_start == sel_row_end
4609 && !tabular.ltCaption(sel_row_start));
4610 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4613 case Tabular::UNSET_LTFOOT:
4614 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4617 case Tabular::SET_LTLASTFOOT:
4618 status.setEnabled(sel_row_start == sel_row_end
4619 && !tabular.ltCaption(sel_row_start));
4620 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4623 case Tabular::UNSET_LTLASTFOOT:
4624 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4627 case Tabular::SET_LTNEWPAGE:
4628 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4631 // only one row can be the caption
4632 // and a multirow cannot be set as caption
4633 case Tabular::SET_LTCAPTION:
4634 case Tabular::UNSET_LTCAPTION:
4635 case Tabular::TOGGLE_LTCAPTION:
4636 status.setEnabled(sel_row_start == sel_row_end
4637 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4638 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4639 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4640 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4641 && (!tabular.haveLTCaption()
4642 || tabular.ltCaption(sel_row_start))
4643 && !tabular.isMultiRow(sel_row_start));
4644 status.setOnOff(tabular.ltCaption(sel_row_start));
4647 case Tabular::SET_BOOKTABS:
4648 status.setOnOff(tabular.use_booktabs);
4651 case Tabular::UNSET_BOOKTABS:
4652 status.setOnOff(!tabular.use_booktabs);
4657 status.setEnabled(false);
4663 // These are only enabled inside tabular
4664 case LFUN_CELL_BACKWARD:
4665 case LFUN_CELL_FORWARD:
4666 status.setEnabled(true);
4669 // disable these with multiple cells selected
4670 case LFUN_INSET_INSERT:
4671 case LFUN_TABULAR_INSERT:
4672 case LFUN_FLEX_INSERT:
4673 case LFUN_FLOAT_INSERT:
4674 case LFUN_FLOAT_WIDE_INSERT:
4675 case LFUN_FOOTNOTE_INSERT:
4676 case LFUN_MARGINALNOTE_INSERT:
4677 case LFUN_MATH_INSERT:
4678 case LFUN_MATH_MODE:
4679 case LFUN_MATH_MUTATE:
4680 case LFUN_MATH_DISPLAY:
4681 case LFUN_NOTE_INSERT:
4682 case LFUN_ARGUMENT_INSERT:
4683 case LFUN_BOX_INSERT:
4684 case LFUN_BRANCH_INSERT:
4685 case LFUN_PHANTOM_INSERT:
4686 case LFUN_WRAP_INSERT:
4687 case LFUN_PREVIEW_INSERT:
4688 case LFUN_ERT_INSERT: {
4689 if (cur.selIsMultiCell()) {
4690 status.setEnabled(false);
4693 return cell(cur.idx())->getStatus(cur, cmd, status);
4696 // disable in non-fixed-width cells
4697 case LFUN_BREAK_PARAGRAPH:
4698 // multirow does not allow paragraph breaks
4699 if (tabular.isMultiRow(cur.idx())) {
4700 status.setEnabled(false);
4703 case LFUN_NEWLINE_INSERT: {
4704 if (tabular.getPWidth(cur.idx()).zero()) {
4705 status.setEnabled(false);
4708 return cell(cur.idx())->getStatus(cur, cmd, status);
4711 case LFUN_NEWPAGE_INSERT:
4712 status.setEnabled(false);
4716 if (tabularStackDirty() && theClipboard().isInternal()) {
4717 if (cur.selIsMultiCell()) {
4720 getSelection(cur, rs, re, cs, ce);
4721 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4722 && paste_tabular->nrows() == re - rs + 1)
4723 status.setEnabled(true);
4725 status.setEnabled(false);
4726 status.message(_("Selection size should match clipboard content."));
4729 status.setEnabled(true);
4732 return cell(cur.idx())->getStatus(cur, cmd, status);
4734 case LFUN_INSET_SETTINGS:
4735 // relay this lfun to Inset, not to the cell.
4736 return Inset::getStatus(cur, cmd, status);
4739 // we try to handle this event in the insets dispatch function.
4740 return cell(cur.idx())->getStatus(cur, cmd, status);
4746 Inset::DisplayType InsetTabular::display() const
4748 if (tabular.is_long_tabular) {
4749 switch (tabular.longtabular_alignment) {
4750 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4752 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4754 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4764 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4766 tabular.latex(os, runparams);
4770 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4772 os << '\n'; // output table on a new line
4773 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4774 tabular.plaintext(os, runparams, dp, false, 0);
4775 return PLAINTEXT_NEWLINE;
4779 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4784 // FIXME: Why not pass a proper DocIterator here?
4786 // if the table is inside a float it doesn't need the informaltable
4787 // wrapper. Search for it.
4788 for (master = owner(); master; master = master->owner())
4789 if (master->lyxCode() == FLOAT_CODE)
4794 os << "<informaltable>";
4797 ret += tabular.docbook(os, runparams);
4799 os << "</informaltable>";
4806 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4808 return tabular.xhtml(xs, rp);
4812 void InsetTabular::validate(LaTeXFeatures & features) const
4814 tabular.validate(features);
4816 // It'd be better to be able to get this from an InsetLayout, but at present
4817 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4818 if (features.runparams().flavor == OutputParams::HTML)
4819 features.addCSSSnippet(
4820 "table { border: 1px solid black; display: inline-block; }\n"
4821 "td { border: 1px solid black; padding: 0.5ex; }");
4825 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4827 return tabular.cellInset(idx);
4831 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4833 return tabular.cellInset(idx);
4837 void InsetTabular::cursorPos(BufferView const & bv,
4838 CursorSlice const & sl, bool boundary, int & x, int & y) const
4840 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4842 // y offset correction
4843 y += cellYPos(sl.idx());
4844 y += tabular.textVOffset(sl.idx());
4845 y += offset_valign_;
4847 // x offset correction
4848 x += cellXPos(sl.idx());
4849 x += tabular.textHOffset(sl.idx());
4850 x += ADD_TO_TABULAR_WIDTH;
4855 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4859 Inset const & inset = *tabular.cellInset(cell);
4860 Point o = bv.coordCache().getInsets().xy(&inset);
4861 int const xbeg = o.x_ - tabular.textHOffset(cell);
4862 int const xend = xbeg + tabular.cellWidth(cell);
4863 row_type const row = tabular.cellRow(cell);
4864 int const ybeg = o.y_ - tabular.rowAscent(row)
4865 - tabular.interRowSpace(row);
4866 int const yend = ybeg + tabular.cellHeight(cell);
4878 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4879 // << " ybeg=" << ybeg << " yend=" << yend
4880 // << " xx=" << xx << " yy=" << yy
4881 // << " dist=" << xx + yy << endl;
4886 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4888 //lyxerr << "InsetTabular::editXY: " << this << endl;
4889 cur.setSelection(false);
4891 cur.idx() = getNearestCell(cur.bv(), x, y);
4893 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4897 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4899 cur.idx() = getNearestCell(cur.bv(), x, y);
4900 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4904 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4906 idx_type idx_min = 0;
4907 int dist_min = numeric_limits<int>::max();
4908 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4909 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4910 int const d = dist(bv, i, x, y);
4921 int InsetTabular::cellYPos(idx_type const cell) const
4923 row_type row = tabular.cellRow(cell);
4925 for (row_type r = 0; r < row; ++r)
4926 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4927 + tabular.interRowSpace(r + 1);
4932 int InsetTabular::cellXPos(idx_type const cell) const
4934 col_type col = tabular.cellColumn(cell);
4936 for (col_type c = 0; c < col; ++c)
4937 lx += tabular.column_info[c].width;
4942 void InsetTabular::resetPos(Cursor & cur) const
4944 BufferView & bv = cur.bv();
4945 int const maxwidth = bv.workWidth();
4947 int const scx_old = scx_;
4948 int const i = cur.find(this);
4953 int const X2 = maxwidth;
4954 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4955 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4956 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4959 scx_ = X1 + 20 - x1;
4961 scx_ = X2 - 20 - x2;
4966 // only update if offset changed
4967 if (scx_ != scx_old)
4968 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4972 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4974 row_type const row = tabular.cellRow(cur.idx());
4975 col_type const col = tabular.cellColumn(cur.idx());
4977 if (isRightToLeft(cur)) {
4978 if (tabular.cellColumn(cur.idx()) == 0) {
4979 if (row == tabular.nrows() - 1)
4981 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4986 cur.idx() = tabular.getLastCellInRow(row - 1);
4988 cur.idx() = tabular.cellIndex(row, col - 1);
4991 if (tabular.isLastCell(cur.idx()))
4993 if (cur.idx() == tabular.getLastCellInRow(row))
4994 cur.idx() = tabular.cellIndex(row + 1, 0);
4996 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4997 cur.idx() = tabular.cellIndex(row, colnextcell);
5001 cur.boundary(false);
5003 if (cur.selIsMultiCell()) {
5004 cur.pit() = cur.lastpit();
5005 cur.pos() = cur.lastpos();
5013 // in visual mode, place cursor at extreme left or right
5015 switch(entry_from) {
5017 case ENTRY_DIRECTION_RIGHT:
5018 cur.posVisToRowExtremity(false /* !left */);
5020 case ENTRY_DIRECTION_LEFT:
5021 cur.posVisToRowExtremity(true /* left */);
5023 case ENTRY_DIRECTION_IGNORE:
5024 // nothing to do in this case
5028 cur.setCurrentFont();
5033 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5035 row_type const row = tabular.cellRow(cur.idx());
5036 col_type const col = tabular.cellColumn(cur.idx());
5038 if (isRightToLeft(cur)) {
5039 if (cur.idx() == tabular.getLastCellInRow(row)) {
5042 cur.idx() = tabular.getFirstCellInRow(row);
5043 cur.idx() = tabular.cellAbove(cur.idx());
5045 if (tabular.isLastCell(cur.idx()))
5047 if (cur.idx() == tabular.getLastCellInRow(row))
5048 cur.idx() = tabular.cellIndex(row + 1, 0);
5050 cur.idx() = tabular.cellIndex(row, col + 1);
5053 if (cur.idx() == 0) // first cell
5056 cur.idx() = tabular.getLastCellInRow(row - 1);
5058 cur.idx() = tabular.cellIndex(row, col - 1);
5061 if (cur.selIsMultiCell()) {
5062 cur.pit() = cur.lastpit();
5063 cur.pos() = cur.lastpos();
5068 cur.pit() = cur.lastpit();
5069 cur.pos() = cur.lastpos();
5071 // in visual mode, place cursor at extreme left or right
5073 switch(entry_from) {
5075 case ENTRY_DIRECTION_RIGHT:
5076 cur.posVisToRowExtremity(false /* !left */);
5078 case ENTRY_DIRECTION_LEFT:
5079 cur.posVisToRowExtremity(true /* left */);
5081 case ENTRY_DIRECTION_IGNORE:
5082 // nothing to do in this case
5086 cur.setCurrentFont();
5091 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5093 istringstream is(argument);
5096 if (insetCode(s) != TABULAR_CODE)
5100 size_t safe_guard = 0;
5105 if (safe_guard > 1000) {
5106 LYXERR0("parameter max count reached!");
5110 Tabular::Feature action = Tabular::LAST_ACTION;
5113 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5114 if (s != tabularFeature[i].feature)
5117 action = tabularFeature[i].action;
5120 if (action == Tabular::LAST_ACTION) {
5121 LYXERR0("Feature not found " << s);
5125 if (tabularFeature[i].need_value)
5127 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5128 tabularFeatures(cur, action, val);
5134 static void checkLongtableSpecial(Tabular::ltType & ltt,
5135 string const & special, bool & flag)
5137 if (special == "dl_above") {
5140 } else if (special == "dl_below") {
5141 ltt.bottomDL = flag;
5143 } else if (special == "empty") {
5153 bool InsetTabular::oneCellHasRotationState(bool rotated,
5154 row_type row_start, row_type row_end,
5155 col_type col_start, col_type col_end) const
5157 for (row_type r = row_start; r <= row_end; ++r)
5158 for (col_type c = col_start; c <= col_end; ++c)
5159 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5165 void InsetTabular::tabularFeatures(Cursor & cur,
5166 Tabular::Feature feature, string const & value)
5168 col_type sel_col_start;
5169 col_type sel_col_end;
5170 row_type sel_row_start;
5171 row_type sel_row_end;
5172 bool setLines = false;
5173 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5174 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5178 case Tabular::M_ALIGN_LEFT:
5179 case Tabular::ALIGN_LEFT:
5180 setAlign = LYX_ALIGN_LEFT;
5183 case Tabular::M_ALIGN_RIGHT:
5184 case Tabular::ALIGN_RIGHT:
5185 setAlign = LYX_ALIGN_RIGHT;
5188 case Tabular::M_ALIGN_CENTER:
5189 case Tabular::ALIGN_CENTER:
5190 setAlign = LYX_ALIGN_CENTER;
5193 case Tabular::ALIGN_BLOCK:
5194 setAlign = LYX_ALIGN_BLOCK;
5197 case Tabular::ALIGN_DECIMAL:
5198 setAlign = LYX_ALIGN_DECIMAL;
5201 case Tabular::M_VALIGN_TOP:
5202 case Tabular::VALIGN_TOP:
5203 setVAlign = Tabular::LYX_VALIGN_TOP;
5206 case Tabular::M_VALIGN_BOTTOM:
5207 case Tabular::VALIGN_BOTTOM:
5208 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5211 case Tabular::M_VALIGN_MIDDLE:
5212 case Tabular::VALIGN_MIDDLE:
5213 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5220 cur.recordUndoInset(ATOMIC_UNDO, this);
5222 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5223 row_type const row = tabular.cellRow(cur.idx());
5224 col_type const column = tabular.cellColumn(cur.idx());
5226 Tabular::ltType ltt;
5230 case Tabular::SET_TABULAR_WIDTH:
5231 tabular.setTabularWidth(Length(value));
5234 case Tabular::SET_PWIDTH: {
5235 Length const len(value);
5236 tabular.setColumnPWidth(cur, cur.idx(), len);
5238 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5239 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5243 case Tabular::SET_MPWIDTH:
5244 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5247 case Tabular::SET_MROFFSET:
5248 tabular.setMROffset(cur, cur.idx(), Length(value));
5251 case Tabular::SET_SPECIAL_COLUMN:
5252 case Tabular::SET_SPECIAL_MULTICOLUMN:
5253 if (value == "none")
5254 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5256 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5259 case Tabular::APPEND_ROW:
5260 // append the row into the tabular
5261 tabular.appendRow(cur.idx());
5264 case Tabular::APPEND_COLUMN:
5265 // append the column into the tabular
5266 tabular.appendColumn(cur.idx());
5267 cur.idx() = tabular.cellIndex(row, column);
5270 case Tabular::DELETE_ROW:
5271 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5272 tabular.deleteRow(sel_row_start);
5273 if (sel_row_start >= tabular.nrows())
5275 cur.idx() = tabular.cellIndex(sel_row_start, column);
5278 cur.setSelection(false);
5281 case Tabular::DELETE_COLUMN:
5282 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5283 tabular.deleteColumn(sel_col_start);
5284 if (sel_col_start >= tabular.ncols())
5286 cur.idx() = tabular.cellIndex(row, sel_col_start);
5289 cur.setSelection(false);
5292 case Tabular::COPY_ROW:
5293 tabular.copyRow(row);
5296 case Tabular::COPY_COLUMN:
5297 tabular.copyColumn(column);
5298 cur.idx() = tabular.cellIndex(row, column);
5301 case Tabular::SET_LINE_TOP:
5302 case Tabular::TOGGLE_LINE_TOP: {
5303 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5304 ? (value == "true") : !tabular.topLine(cur.idx());
5305 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5306 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5307 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5311 case Tabular::SET_LINE_BOTTOM:
5312 case Tabular::TOGGLE_LINE_BOTTOM: {
5313 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5314 ? (value == "true") : !tabular.bottomLine(cur.idx());
5315 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5316 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5317 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5321 case Tabular::SET_LINE_LEFT:
5322 case Tabular::TOGGLE_LINE_LEFT: {
5323 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5324 ? (value == "true") : !tabular.leftLine(cur.idx());
5325 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5326 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5327 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5331 case Tabular::SET_LINE_RIGHT:
5332 case Tabular::TOGGLE_LINE_RIGHT: {
5333 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5334 ? (value == "true") : !tabular.rightLine(cur.idx());
5335 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5336 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5337 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5341 case Tabular::M_ALIGN_LEFT:
5342 case Tabular::M_ALIGN_RIGHT:
5343 case Tabular::M_ALIGN_CENTER:
5344 case Tabular::ALIGN_LEFT:
5345 case Tabular::ALIGN_RIGHT:
5346 case Tabular::ALIGN_CENTER:
5347 case Tabular::ALIGN_BLOCK:
5348 case Tabular::ALIGN_DECIMAL:
5349 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5350 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5351 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5352 !tabular.getPWidth(c).zero());
5355 case Tabular::M_VALIGN_TOP:
5356 case Tabular::M_VALIGN_BOTTOM:
5357 case Tabular::M_VALIGN_MIDDLE:
5359 case Tabular::VALIGN_TOP:
5360 case Tabular::VALIGN_BOTTOM:
5361 case Tabular::VALIGN_MIDDLE:
5362 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5363 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5364 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5367 case Tabular::SET_MULTICOLUMN: {
5368 if (!cur.selection()) {
5369 // just multicol for one single cell
5370 // check whether we are completely in a multicol
5371 if (!tabular.isMultiColumn(cur.idx()))
5372 tabular.setMultiColumn(cur.idx(), 1,
5373 tabular.rightLine(cur.idx()));
5376 // we have a selection so this means we just add all this
5377 // cells to form a multicolumn cell
5378 idx_type const s_start = cur.selBegin().idx();
5379 row_type const col_start = tabular.cellColumn(s_start);
5380 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5381 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5382 tabular.rightLine(cur.selEnd().idx()));
5385 cur.setSelection(false);
5389 case Tabular::UNSET_MULTICOLUMN: {
5390 if (!cur.selection()) {
5391 if (tabular.isMultiColumn(cur.idx()))
5392 tabular.unsetMultiColumn(cur.idx());
5397 case Tabular::MULTICOLUMN: {
5398 if (!cur.selection()) {
5399 if (tabular.isMultiColumn(cur.idx()))
5400 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5402 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5406 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5407 row_type const r = sel_row_start;
5408 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5409 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5412 // If the selection contains at least one singlecol cell
5413 // or multiple multicol cells,
5414 // we assume the user will merge is to a single multicol
5416 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5418 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5422 case Tabular::SET_MULTIROW: {
5423 if (!cur.selection()) {
5424 // just multirow for one single cell
5425 // check whether we are completely in a multirow
5426 if (!tabular.isMultiRow(cur.idx()))
5427 tabular.setMultiRow(cur.idx(), 1,
5428 tabular.bottomLine(cur.idx()));
5431 // we have a selection so this means we just add all this
5432 // cells to form a multirow cell
5433 idx_type const s_start = cur.selBegin().idx();
5434 row_type const row_start = tabular.cellRow(s_start);
5435 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5436 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5437 tabular.bottomLine(cur.selEnd().idx()));
5440 cur.setSelection(false);
5444 case Tabular::UNSET_MULTIROW: {
5445 if (!cur.selection()) {
5446 if (tabular.isMultiRow(cur.idx()))
5447 tabular.unsetMultiRow(cur.idx());
5452 case Tabular::MULTIROW: {
5453 if (!cur.selection()) {
5454 if (tabular.isMultiRow(cur.idx()))
5455 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5457 tabularFeatures(cur, Tabular::SET_MULTIROW);
5461 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5462 col_type const c = sel_col_start;
5463 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5464 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5467 // If the selection contains at least one singlerow cell
5468 // or multiple multirow cells,
5469 // we assume the user will merge is to a single multirow
5471 tabularFeatures(cur, Tabular::SET_MULTIROW);
5473 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5477 case Tabular::SET_ALL_LINES:
5479 case Tabular::UNSET_ALL_LINES:
5480 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5481 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5482 idx_type const cell = tabular.cellIndex(r, c);
5483 tabular.setTopLine(cell, setLines);
5484 tabular.setBottomLine(cell, setLines);
5485 tabular.setRightLine(cell, setLines);
5486 tabular.setLeftLine(cell, setLines);
5490 case Tabular::SET_BORDER_LINES:
5491 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5492 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5493 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5495 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5496 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5497 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5501 case Tabular::SET_LONGTABULAR:
5502 tabular.is_long_tabular = true;
5505 case Tabular::UNSET_LONGTABULAR:
5506 for (row_type r = 0; r < tabular.nrows(); ++r) {
5507 if (tabular.ltCaption(r)) {
5508 cur.idx() = tabular.cellIndex(r, 0);
5511 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5514 tabular.is_long_tabular = false;
5517 case Tabular::SET_ROTATE_TABULAR:
5518 tabular.rotate = true;
5521 case Tabular::UNSET_ROTATE_TABULAR:
5522 tabular.rotate = false;
5525 case Tabular::TOGGLE_ROTATE_TABULAR:
5526 tabular.rotate = !tabular.rotate;
5529 case Tabular::TABULAR_VALIGN_TOP:
5530 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5533 case Tabular::TABULAR_VALIGN_MIDDLE:
5534 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5537 case Tabular::TABULAR_VALIGN_BOTTOM:
5538 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5541 case Tabular::LONGTABULAR_ALIGN_LEFT:
5542 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5545 case Tabular::LONGTABULAR_ALIGN_CENTER:
5546 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5549 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5550 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5555 case Tabular::SET_ROTATE_CELL:
5556 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5557 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5558 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5561 case Tabular::UNSET_ROTATE_CELL:
5562 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5563 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5564 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5567 case Tabular::TOGGLE_ROTATE_CELL:
5569 bool oneNotRotated = oneCellHasRotationState(false,
5570 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5572 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5573 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5574 tabular.setRotateCell(tabular.cellIndex(r, c),
5579 case Tabular::SET_USEBOX: {
5580 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5581 if (val == tabular.getUsebox(cur.idx()))
5582 val = Tabular::BOX_NONE;
5583 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5584 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5585 tabular.setUsebox(tabular.cellIndex(r, c), val);
5589 case Tabular::UNSET_LTFIRSTHEAD:
5591 case Tabular::SET_LTFIRSTHEAD:
5592 tabular.getRowOfLTFirstHead(row, ltt);
5593 checkLongtableSpecial(ltt, value, flag);
5594 tabular.setLTHead(row, flag, ltt, true);
5597 case Tabular::UNSET_LTHEAD:
5599 case Tabular::SET_LTHEAD:
5600 tabular.getRowOfLTHead(row, ltt);
5601 checkLongtableSpecial(ltt, value, flag);
5602 tabular.setLTHead(row, flag, ltt, false);
5605 case Tabular::UNSET_LTFOOT:
5607 case Tabular::SET_LTFOOT:
5608 tabular.getRowOfLTFoot(row, ltt);
5609 checkLongtableSpecial(ltt, value, flag);
5610 tabular.setLTFoot(row, flag, ltt, false);
5613 case Tabular::UNSET_LTLASTFOOT:
5615 case Tabular::SET_LTLASTFOOT:
5616 tabular.getRowOfLTLastFoot(row, ltt);
5617 checkLongtableSpecial(ltt, value, flag);
5618 tabular.setLTFoot(row, flag, ltt, true);
5621 case Tabular::SET_LTNEWPAGE:
5622 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5625 case Tabular::SET_LTCAPTION: {
5626 if (tabular.ltCaption(row))
5628 cur.idx() = tabular.setLTCaption(row, true);
5631 cur.setSelection(false);
5632 // If a row is set as caption, then also insert
5633 // a caption. Otherwise the LaTeX output is broken.
5634 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5635 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5639 case Tabular::UNSET_LTCAPTION: {
5640 if (!tabular.ltCaption(row))
5642 cur.idx() = tabular.setLTCaption(row, false);
5645 cur.setSelection(false);
5646 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5647 if (lyx::getStatus(fr).enabled())
5652 case Tabular::TOGGLE_LTCAPTION: {
5653 if (tabular.ltCaption(row))
5654 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5656 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5660 case Tabular::SET_BOOKTABS:
5661 tabular.use_booktabs = true;
5664 case Tabular::UNSET_BOOKTABS:
5665 tabular.use_booktabs = false;
5668 case Tabular::SET_TOP_SPACE: {
5670 if (value == "default")
5671 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5672 tabular.row_info[r].top_space_default = true;
5673 else if (value == "none")
5674 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5675 tabular.row_info[r].top_space_default = false;
5676 tabular.row_info[r].top_space = len;
5678 else if (isValidLength(value, &len))
5679 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5680 tabular.row_info[r].top_space_default = false;
5681 tabular.row_info[r].top_space = len;
5686 case Tabular::SET_BOTTOM_SPACE: {
5688 if (value == "default")
5689 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5690 tabular.row_info[r].bottom_space_default = true;
5691 else if (value == "none")
5692 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5693 tabular.row_info[r].bottom_space_default = false;
5694 tabular.row_info[r].bottom_space = len;
5696 else if (isValidLength(value, &len))
5697 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5698 tabular.row_info[r].bottom_space_default = false;
5699 tabular.row_info[r].bottom_space = len;
5704 case Tabular::SET_INTERLINE_SPACE: {
5706 if (value == "default")
5707 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5708 tabular.row_info[r].interline_space_default = true;
5709 else if (value == "none")
5710 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5711 tabular.row_info[r].interline_space_default = false;
5712 tabular.row_info[r].interline_space = len;
5714 else if (isValidLength(value, &len))
5715 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5716 tabular.row_info[r].interline_space_default = false;
5717 tabular.row_info[r].interline_space = len;
5722 case Tabular::SET_DECIMAL_POINT:
5723 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5724 tabular.column_info[c].decimal_point = from_utf8(value);
5727 // dummy stuff just to avoid warnings
5728 case Tabular::LAST_ACTION:
5734 bool InsetTabular::copySelection(Cursor & cur)
5736 if (!cur.selection())
5741 getSelection(cur, rs, re, cs, ce);
5743 paste_tabular.reset(new Tabular(tabular));
5745 for (row_type r = 0; r < rs; ++r)
5746 paste_tabular->deleteRow(0);
5748 row_type const rows = re - rs + 1;
5749 while (paste_tabular->nrows() > rows)
5750 paste_tabular->deleteRow(rows);
5752 for (col_type c = 0; c < cs; ++c)
5753 paste_tabular->deleteColumn(0);
5755 col_type const columns = ce - cs + 1;
5756 while (paste_tabular->ncols() > columns)
5757 paste_tabular->deleteColumn(columns);
5759 paste_tabular->setBuffer(tabular.buffer());
5761 odocstringstream os;
5762 OutputParams const runparams(0);
5763 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5764 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5765 cap::copySelection(cur, os.str());
5767 // mark tabular stack dirty
5768 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5769 // when we (hopefully) have a one-for-all paste mechanism.
5770 // This must be called after cap::copySelection.
5771 dirtyTabularStack(true);
5777 bool InsetTabular::pasteClipboard(Cursor & cur)
5781 col_type actcol = tabular.cellColumn(cur.idx());
5782 row_type actrow = tabular.cellRow(cur.idx());
5784 if (cur.selIsMultiCell()) {
5787 getSelection(cur, actrow, re, actcol, ce);
5790 for (row_type r1 = 0, r2 = actrow;
5791 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5793 for (col_type c1 = 0, c2 = actcol;
5794 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5796 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5797 tabular.isPartOfMultiColumn(r2, c2))
5799 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5803 if (tabular.isPartOfMultiColumn(r2, c2)) {
5807 shared_ptr<InsetTableCell> inset(
5808 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5809 tabular.setCellInset(r2, c2, inset);
5810 // FIXME?: why do we need to do this explicitly? (EL)
5811 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5813 // FIXME: change tracking (MG)
5814 inset->setChange(Change(buffer().params().trackChanges ?
5815 Change::INSERTED : Change::UNCHANGED));
5823 void InsetTabular::cutSelection(Cursor & cur)
5825 if (!cur.selection())
5830 getSelection(cur, rs, re, cs, ce);
5831 for (row_type r = rs; r <= re; ++r) {
5832 for (col_type c = cs; c <= ce; ++c) {
5833 shared_ptr<InsetTableCell> t
5834 = cell(tabular.cellIndex(r, c));
5835 if (buffer().params().trackChanges)
5836 // FIXME: Change tracking (MG)
5837 t->setChange(Change(Change::DELETED));
5843 // cursor position might be invalid now
5844 if (cur.pit() > cur.lastpit())
5845 cur.pit() = cur.lastpit();
5846 if (cur.pos() > cur.lastpos())
5847 cur.pos() = cur.lastpos();
5848 cur.clearSelection();
5852 bool InsetTabular::isRightToLeft(Cursor & cur) const
5854 LASSERT(cur.depth() > 1, /**/);
5855 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5856 pos_type const parentpos = cur[cur.depth() - 2].pos();
5857 return parentpar.getFontSettings(buffer().params(),
5858 parentpos).language()->rightToLeft();
5862 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5865 LASSERT(stidx <= enidx, return docstring());
5867 col_type const col1 = tabular.cellColumn(stidx);
5868 col_type const col2 = tabular.cellColumn(enidx);
5869 row_type const row1 = tabular.cellRow(stidx);
5870 row_type const row2 = tabular.cellRow(enidx);
5872 for (col_type col = col1; col <= col2; col++)
5873 for (row_type row = row1; row <= row2; row++) {
5878 retval += tabular.cellInset(row, col)->asString(intoInsets);
5884 void InsetTabular::getSelection(Cursor & cur,
5885 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5887 CursorSlice const & beg = cur.selBegin();
5888 CursorSlice const & end = cur.selEnd();
5889 cs = tabular.cellColumn(beg.idx());
5890 ce = tabular.cellColumn(end.idx());
5894 rs = tabular.cellRow(beg.idx());
5895 re = tabular.cellRow(end.idx());
5901 Text * InsetTabular::getText(int idx) const
5903 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5907 void InsetTabular::setChange(Change const & change)
5909 for (idx_type idx = 0; idx < nargs(); ++idx)
5910 cell(idx)->setChange(change);
5914 void InsetTabular::acceptChanges()
5916 for (idx_type idx = 0; idx < nargs(); ++idx)
5917 cell(idx)->acceptChanges();
5921 void InsetTabular::rejectChanges()
5923 for (idx_type idx = 0; idx < nargs(); ++idx)
5924 cell(idx)->rejectChanges();
5928 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5930 return tabular.getPWidth(cell).zero();
5934 bool InsetTabular::forcePlainLayout(idx_type cell) const
5936 return !tabular.getPWidth(cell).zero();
5940 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5943 if (buf.length() <= 0)
5948 col_type maxCols = 1;
5949 size_t const len = buf.length();
5953 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5961 maxCols = max(cols, maxCols);
5967 maxCols = max(cols, maxCols);
5973 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5974 loctab = paste_tabular.get();
5976 dirtyTabularStack(true);
5979 cell = bv.cursor().idx();
5980 ocol = tabular.cellColumn(cell);
5981 row = tabular.cellRow(cell);
5985 idx_type const cells = loctab->numberofcells;
5988 rows = loctab->nrows();
5989 col_type const columns = loctab->ncols();
5991 while (cell < cells && p < len && row < rows &&
5992 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5998 // we can only set this if we are not too far right
5999 if (cols < columns) {
6000 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6001 Font const font = bv.textMetrics(&inset->text()).
6003 inset->setText(buf.substr(op, p - op), font,
6004 buffer().params().trackChanges);
6010 // we can only set this if we are not too far right
6011 if (cols < columns) {
6012 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6013 Font const font = bv.textMetrics(&inset->text()).
6015 inset->setText(buf.substr(op, p - op), font,
6016 buffer().params().trackChanges);
6021 cell = loctab->cellIndex(row, cols);
6027 // check for the last cell if there is no trailing '\n'
6028 if (cell < cells && op < len) {
6029 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6030 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6031 inset->setText(buf.substr(op, len - op), font,
6032 buffer().params().trackChanges);
6038 void InsetTabular::addPreview(DocIterator const & inset_pos,
6039 PreviewLoader & loader) const
6041 DocIterator cell_pos = inset_pos;
6043 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6044 for (row_type r = 0; r < tabular.nrows(); ++r) {
6045 for (col_type c = 0; c < tabular.ncols(); ++c) {
6046 cell_pos.top().idx() = tabular.cellIndex(r, c);
6047 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6053 bool InsetTabular::completionSupported(Cursor const & cur) const
6055 Cursor const & bvCur = cur.bv().cursor();
6056 if (&bvCur.inset() != this)
6058 return cur.text()->completionSupported(cur);
6062 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6064 return completionSupported(cur);
6068 bool InsetTabular::automaticInlineCompletion() const
6070 return lyxrc.completion_inline_text;
6074 bool InsetTabular::automaticPopupCompletion() const
6076 return lyxrc.completion_popup_text;
6080 bool InsetTabular::showCompletionCursor() const
6082 return lyxrc.completion_cursor_text;
6086 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6088 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6092 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6094 if (!completionSupported(cur))
6096 return cur.text()->completionPrefix(cur);
6100 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6102 if (!completionSupported(cur))
6105 return cur.text()->insertCompletion(cur, s, finished);
6109 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6110 Dimension & dim) const
6112 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6113 tm.completionPosAndDim(cur, x, y, dim);
6117 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6119 istringstream data(in);
6121 lex.setStream(data);
6128 if (!lex || token != "tabular") {
6129 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6133 // This is part of the inset proper that is usually swallowed
6134 // by Buffer::readInset
6136 if (!lex || token != "Tabular") {
6137 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6145 string InsetTabular::params2string(InsetTabular const & inset)
6148 data << "tabular" << ' ';
6150 data << "\\end_inset\n";