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 docstring 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 docstring InsetTabular::contextMenuName() const
3412 return from_ascii("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:
4163 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4167 // insert file functions
4168 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4169 case LFUN_FILE_INSERT_PLAINTEXT:
4171 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4172 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4173 FileName(to_utf8(cmd.argument())));
4176 cur.recordUndoInset(INSERT_UNDO);
4177 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4178 // content has been replaced,
4179 // so cursor might be invalid
4180 cur.pos() = cur.lastpos();
4181 cur.pit() = cur.lastpit();
4182 bvcur.setCursor(cur);
4189 if (cur.selIsMultiCell()) {
4190 if (copySelection(cur)) {
4191 cur.recordUndoInset(DELETE_UNDO);
4195 cell(cur.idx())->dispatch(cur, cmd);
4198 case LFUN_SELF_INSERT:
4199 if (cur.selIsMultiCell()) {
4200 cur.recordUndoInset(DELETE_UNDO);
4203 cell(cur.idx())->dispatch(cur, cmd);
4206 case LFUN_CHAR_DELETE_BACKWARD:
4207 case LFUN_CHAR_DELETE_FORWARD:
4208 if (cur.selIsMultiCell()) {
4209 cur.recordUndoInset(DELETE_UNDO);
4212 cell(cur.idx())->dispatch(cur, cmd);
4216 if (!cur.selection())
4218 if (cur.selIsMultiCell()) {
4222 cell(cur.idx())->dispatch(cur, cmd);
4225 case LFUN_CLIPBOARD_PASTE:
4226 case LFUN_PRIMARY_SELECTION_PASTE: {
4227 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4228 theClipboard().getAsText() :
4229 theSelection().get();
4232 // pass to InsertPlaintextString, but
4233 // only if we have multi-cell content
4234 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4235 cur.recordUndoInset(INSERT_UNDO);
4236 if (insertPlaintextString(cur.bv(), clip, false)) {
4237 // content has been replaced,
4238 // so cursor might be invalid
4239 cur.pos() = cur.lastpos();
4240 cur.pit() = cur.lastpit();
4241 bvcur.setCursor(cur);
4245 // Let the cell handle normal text
4246 cell(cur.idx())->dispatch(cur, cmd);
4251 if (!tabularStackDirty()) {
4252 if (!cur.selIsMultiCell())
4253 cell(cur.idx())->dispatch(cur, cmd);
4256 if (theClipboard().isInternal()) {
4257 cur.recordUndoInset(INSERT_UNDO);
4258 pasteClipboard(cur);
4262 case LFUN_FONT_EMPH:
4263 case LFUN_FONT_BOLD:
4264 case LFUN_FONT_BOLDSYMBOL:
4265 case LFUN_FONT_ROMAN:
4266 case LFUN_FONT_NOUN:
4267 case LFUN_FONT_ITAL:
4268 case LFUN_FONT_FRAK:
4269 case LFUN_FONT_TYPEWRITER:
4270 case LFUN_FONT_SANS:
4271 case LFUN_TEXTSTYLE_APPLY:
4272 case LFUN_TEXTSTYLE_UPDATE:
4273 case LFUN_FONT_SIZE:
4274 case LFUN_FONT_UNDERLINE:
4275 case LFUN_FONT_STRIKEOUT:
4276 case LFUN_FONT_UULINE:
4277 case LFUN_FONT_UWAVE:
4279 case LFUN_WORD_CAPITALIZE:
4280 case LFUN_WORD_UPCASE:
4281 case LFUN_WORD_LOWCASE:
4282 case LFUN_CHARS_TRANSPOSE:
4283 if (cur.selIsMultiCell()) {
4286 getSelection(cur, rs, re, cs, ce);
4287 Cursor tmpcur = cur;
4288 for (row_type r = rs; r <= re; ++r) {
4289 for (col_type c = cs; c <= ce; ++c) {
4290 // cursor follows cell:
4291 tmpcur.idx() = tabular.cellIndex(r, c);
4292 // select this cell only:
4295 tmpcur.resetAnchor();
4296 tmpcur.pit() = tmpcur.lastpit();
4297 tmpcur.pos() = tmpcur.top().lastpos();
4298 tmpcur.setCursor(tmpcur);
4299 tmpcur.setSelection();
4300 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4305 cell(cur.idx())->dispatch(cur, cmd);
4309 case LFUN_INSET_SETTINGS:
4310 // relay this lfun to Inset, not to the cell.
4311 Inset::doDispatch(cur, cmd);
4315 // we try to handle this event in the insets dispatch function.
4316 cell(cur.idx())->dispatch(cur, cmd);
4322 // function sets an object as defined in func_status.h:
4323 // states OK, Unknown, Disabled, On, Off.
4324 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4325 FuncStatus & status) const
4327 switch (cmd.action()) {
4328 case LFUN_INSET_MODIFY: {
4329 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4332 // FIXME: We only check for the very first argument...
4333 string const s = cmd.getArg(1);
4334 // We always enable the lfun if it is coming from the dialog
4335 // because the dialog makes sure all the settings are valid,
4336 // even though the first argument might not be valid now.
4337 if (s == "from-dialog") {
4338 status.setEnabled(true);
4342 int action = Tabular::LAST_ACTION;
4344 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4345 if (tabularFeature[i].feature == s) {
4346 action = tabularFeature[i].action;
4350 if (action == Tabular::LAST_ACTION) {
4352 status.setUnknown(true);
4356 string const argument = cmd.getLongArg(2);
4358 row_type sel_row_start = 0;
4359 row_type sel_row_end = 0;
4360 col_type sel_col_start = 0;
4361 col_type sel_col_end = 0;
4362 Tabular::ltType dummyltt;
4365 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4368 case Tabular::SET_PWIDTH:
4369 case Tabular::SET_MPWIDTH:
4370 case Tabular::SET_SPECIAL_COLUMN:
4371 case Tabular::SET_SPECIAL_MULTICOLUMN:
4372 case Tabular::APPEND_ROW:
4373 case Tabular::APPEND_COLUMN:
4374 case Tabular::DELETE_ROW:
4375 case Tabular::DELETE_COLUMN:
4376 case Tabular::COPY_ROW:
4377 case Tabular::COPY_COLUMN:
4378 case Tabular::SET_TOP_SPACE:
4379 case Tabular::SET_BOTTOM_SPACE:
4380 case Tabular::SET_INTERLINE_SPACE:
4384 case Tabular::SET_TABULAR_WIDTH:
4385 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4386 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4389 case Tabular::SET_DECIMAL_POINT:
4391 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4394 case Tabular::SET_MULTICOLUMN:
4395 case Tabular::UNSET_MULTICOLUMN:
4396 case Tabular::MULTICOLUMN:
4397 // If a row is set as longtable caption, it must not be allowed
4398 // to unset that this row is a multicolumn.
4399 status.setEnabled(sel_row_start == sel_row_end
4400 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4401 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4404 case Tabular::SET_MULTIROW:
4405 case Tabular::UNSET_MULTIROW:
4406 case Tabular::MULTIROW:
4407 // If a row is set as longtable caption, it must not be allowed
4408 // to unset that this row is a multirow.
4409 status.setEnabled(sel_col_start == sel_col_end
4410 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4411 status.setOnOff(tabular.isMultiRow(cur.idx()));
4414 case Tabular::SET_ALL_LINES:
4415 case Tabular::UNSET_ALL_LINES:
4416 case Tabular::SET_BORDER_LINES:
4417 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4420 case Tabular::SET_LINE_TOP:
4421 case Tabular::SET_LINE_BOTTOM:
4422 case Tabular::SET_LINE_LEFT:
4423 case Tabular::SET_LINE_RIGHT:
4424 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4427 case Tabular::TOGGLE_LINE_TOP:
4428 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4429 status.setOnOff(tabular.topLine(cur.idx()));
4432 case Tabular::TOGGLE_LINE_BOTTOM:
4433 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4434 status.setOnOff(tabular.bottomLine(cur.idx()));
4437 case Tabular::TOGGLE_LINE_LEFT:
4438 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4439 status.setOnOff(tabular.leftLine(cur.idx()));
4442 case Tabular::TOGGLE_LINE_RIGHT:
4443 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4444 status.setOnOff(tabular.rightLine(cur.idx()));
4447 // multirow cells only inherit the alignment of the column if the column has
4448 // no width, otherwise they are left-aligned
4449 // therefore allow always left but right and center only if there is no width
4450 case Tabular::M_ALIGN_LEFT:
4452 case Tabular::ALIGN_LEFT:
4453 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4456 case Tabular::M_ALIGN_RIGHT:
4458 case Tabular::ALIGN_RIGHT:
4459 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4460 && !tabular.getPWidth(cur.idx()).zero()));
4461 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4464 case Tabular::M_ALIGN_CENTER:
4466 case Tabular::ALIGN_CENTER:
4467 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4468 && !tabular.getPWidth(cur.idx()).zero()));
4469 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4472 case Tabular::ALIGN_BLOCK:
4473 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4474 && !tabular.isMultiRow(cur.idx()));
4475 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4478 case Tabular::ALIGN_DECIMAL:
4479 status.setEnabled(!tabular.isMultiRow(cur.idx())
4480 && !tabular.isMultiColumn(cur.idx()));
4481 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4484 case Tabular::M_VALIGN_TOP:
4486 case Tabular::VALIGN_TOP:
4487 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4488 && !tabular.isMultiRow(cur.idx()));
4490 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4493 case Tabular::M_VALIGN_BOTTOM:
4495 case Tabular::VALIGN_BOTTOM:
4496 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4497 && !tabular.isMultiRow(cur.idx()));
4499 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4502 case Tabular::M_VALIGN_MIDDLE:
4504 case Tabular::VALIGN_MIDDLE:
4505 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4506 && !tabular.isMultiRow(cur.idx()));
4508 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4511 case Tabular::SET_LONGTABULAR:
4512 // setting as longtable is not allowed when table is inside a float
4513 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4514 || cur.innerInsetOfType(WRAP_CODE) != 0)
4515 status.setEnabled(false);
4517 status.setEnabled(true);
4518 status.setOnOff(tabular.is_long_tabular);
4521 case Tabular::UNSET_LONGTABULAR:
4522 status.setOnOff(!tabular.is_long_tabular);
4525 case Tabular::TOGGLE_ROTATE_TABULAR:
4526 case Tabular::SET_ROTATE_TABULAR:
4527 status.setEnabled(tabular.tabular_width.zero());
4528 status.setOnOff(tabular.rotate);
4531 case Tabular::TABULAR_VALIGN_TOP:
4532 status.setEnabled(tabular.tabular_width.zero());
4533 status.setOnOff(tabular.tabular_valignment
4534 == Tabular::LYX_VALIGN_TOP);
4536 case Tabular::TABULAR_VALIGN_MIDDLE:
4537 status.setEnabled(tabular.tabular_width.zero());
4538 status.setOnOff(tabular.tabular_valignment
4539 == Tabular::LYX_VALIGN_MIDDLE);
4541 case Tabular::TABULAR_VALIGN_BOTTOM:
4542 status.setEnabled(tabular.tabular_width.zero());
4543 status.setOnOff(tabular.tabular_valignment
4544 == Tabular::LYX_VALIGN_BOTTOM);
4547 case Tabular::LONGTABULAR_ALIGN_LEFT:
4548 status.setOnOff(tabular.longtabular_alignment
4549 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4551 case Tabular::LONGTABULAR_ALIGN_CENTER:
4552 status.setOnOff(tabular.longtabular_alignment
4553 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4555 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4556 status.setOnOff(tabular.longtabular_alignment
4557 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4560 case Tabular::UNSET_ROTATE_TABULAR:
4561 status.setOnOff(!tabular.rotate);
4564 case Tabular::TOGGLE_ROTATE_CELL:
4565 case Tabular::SET_ROTATE_CELL:
4566 status.setOnOff(!oneCellHasRotationState(false,
4567 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4570 case Tabular::UNSET_ROTATE_CELL:
4571 status.setOnOff(!oneCellHasRotationState(true,
4572 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4575 case Tabular::SET_USEBOX:
4576 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4579 // every row can only be one thing:
4580 // either a footer or header or caption
4581 case Tabular::SET_LTFIRSTHEAD:
4582 status.setEnabled(sel_row_start == sel_row_end
4583 && !tabular.ltCaption(sel_row_start));
4584 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4587 case Tabular::UNSET_LTFIRSTHEAD:
4588 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4591 case Tabular::SET_LTHEAD:
4592 status.setEnabled(sel_row_start == sel_row_end
4593 && !tabular.ltCaption(sel_row_start));
4594 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4597 case Tabular::UNSET_LTHEAD:
4598 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4601 case Tabular::SET_LTFOOT:
4602 status.setEnabled(sel_row_start == sel_row_end
4603 && !tabular.ltCaption(sel_row_start));
4604 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4607 case Tabular::UNSET_LTFOOT:
4608 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4611 case Tabular::SET_LTLASTFOOT:
4612 status.setEnabled(sel_row_start == sel_row_end
4613 && !tabular.ltCaption(sel_row_start));
4614 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4617 case Tabular::UNSET_LTLASTFOOT:
4618 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4621 case Tabular::SET_LTNEWPAGE:
4622 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4625 // only one row can be the caption
4626 // and a multirow cannot be set as caption
4627 case Tabular::SET_LTCAPTION:
4628 case Tabular::UNSET_LTCAPTION:
4629 case Tabular::TOGGLE_LTCAPTION:
4630 status.setEnabled(sel_row_start == sel_row_end
4631 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4632 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4633 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4634 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4635 && (!tabular.haveLTCaption()
4636 || tabular.ltCaption(sel_row_start))
4637 && !tabular.isMultiRow(sel_row_start));
4638 status.setOnOff(tabular.ltCaption(sel_row_start));
4641 case Tabular::SET_BOOKTABS:
4642 status.setOnOff(tabular.use_booktabs);
4645 case Tabular::UNSET_BOOKTABS:
4646 status.setOnOff(!tabular.use_booktabs);
4651 status.setEnabled(false);
4657 // These are only enabled inside tabular
4658 case LFUN_CELL_BACKWARD:
4659 case LFUN_CELL_FORWARD:
4660 status.setEnabled(true);
4663 // disable these with multiple cells selected
4664 case LFUN_INSET_INSERT:
4665 case LFUN_TABULAR_INSERT:
4666 case LFUN_FLEX_INSERT:
4667 case LFUN_FLOAT_INSERT:
4668 case LFUN_FLOAT_WIDE_INSERT:
4669 case LFUN_FOOTNOTE_INSERT:
4670 case LFUN_MARGINALNOTE_INSERT:
4671 case LFUN_MATH_INSERT:
4672 case LFUN_MATH_MODE:
4673 case LFUN_MATH_MUTATE:
4674 case LFUN_MATH_DISPLAY:
4675 case LFUN_NOTE_INSERT:
4676 case LFUN_ARGUMENT_INSERT:
4677 case LFUN_BOX_INSERT:
4678 case LFUN_BRANCH_INSERT:
4679 case LFUN_PHANTOM_INSERT:
4680 case LFUN_WRAP_INSERT:
4681 case LFUN_PREVIEW_INSERT:
4682 case LFUN_ERT_INSERT: {
4683 if (cur.selIsMultiCell()) {
4684 status.setEnabled(false);
4687 return cell(cur.idx())->getStatus(cur, cmd, status);
4690 // disable in non-fixed-width cells
4691 case LFUN_BREAK_PARAGRAPH:
4692 // multirow does not allow paragraph breaks
4693 if (tabular.isMultiRow(cur.idx())) {
4694 status.setEnabled(false);
4697 case LFUN_NEWLINE_INSERT: {
4698 if (tabular.getPWidth(cur.idx()).zero()) {
4699 status.setEnabled(false);
4702 return cell(cur.idx())->getStatus(cur, cmd, status);
4705 case LFUN_NEWPAGE_INSERT:
4706 status.setEnabled(false);
4710 if (tabularStackDirty() && theClipboard().isInternal()) {
4711 if (cur.selIsMultiCell()) {
4714 getSelection(cur, rs, re, cs, ce);
4715 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4716 && paste_tabular->nrows() == re - rs + 1)
4717 status.setEnabled(true);
4719 status.setEnabled(false);
4720 status.message(_("Selection size should match clipboard content."));
4723 status.setEnabled(true);
4726 return cell(cur.idx())->getStatus(cur, cmd, status);
4728 case LFUN_INSET_SETTINGS:
4729 // relay this lfun to Inset, not to the cell.
4730 return Inset::getStatus(cur, cmd, status);
4733 // we try to handle this event in the insets dispatch function.
4734 return cell(cur.idx())->getStatus(cur, cmd, status);
4740 Inset::DisplayType InsetTabular::display() const
4742 if (tabular.is_long_tabular) {
4743 switch (tabular.longtabular_alignment) {
4744 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4746 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4748 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4758 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4760 tabular.latex(os, runparams);
4764 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4766 os << '\n'; // output table on a new line
4767 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4768 tabular.plaintext(os, runparams, dp, false, 0);
4769 return PLAINTEXT_NEWLINE;
4773 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4778 // FIXME: Why not pass a proper DocIterator here?
4780 // if the table is inside a float it doesn't need the informaltable
4781 // wrapper. Search for it.
4782 for (master = owner(); master; master = master->owner())
4783 if (master->lyxCode() == FLOAT_CODE)
4788 os << "<informaltable>";
4791 ret += tabular.docbook(os, runparams);
4793 os << "</informaltable>";
4800 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4802 return tabular.xhtml(xs, rp);
4806 void InsetTabular::validate(LaTeXFeatures & features) const
4808 tabular.validate(features);
4810 // It'd be better to be able to get this from an InsetLayout, but at present
4811 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4812 if (features.runparams().flavor == OutputParams::HTML)
4813 features.addPreambleSnippet("<style type=\"text/css\">\n"
4814 "table { border: 1px solid black; display: inline-block; }\n"
4815 "td { border: 1px solid black; padding: 0.5ex; }\n"
4820 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4822 return tabular.cellInset(idx);
4826 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4828 return tabular.cellInset(idx);
4832 void InsetTabular::cursorPos(BufferView const & bv,
4833 CursorSlice const & sl, bool boundary, int & x, int & y) const
4835 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4837 // y offset correction
4838 y += cellYPos(sl.idx());
4839 y += tabular.textVOffset(sl.idx());
4840 y += offset_valign_;
4842 // x offset correction
4843 x += cellXPos(sl.idx());
4844 x += tabular.textHOffset(sl.idx());
4845 x += ADD_TO_TABULAR_WIDTH;
4850 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4854 Inset const & inset = *tabular.cellInset(cell);
4855 Point o = bv.coordCache().getInsets().xy(&inset);
4856 int const xbeg = o.x_ - tabular.textHOffset(cell);
4857 int const xend = xbeg + tabular.cellWidth(cell);
4858 row_type const row = tabular.cellRow(cell);
4859 int const ybeg = o.y_ - tabular.rowAscent(row)
4860 - tabular.interRowSpace(row);
4861 int const yend = ybeg + tabular.cellHeight(cell);
4873 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4874 // << " ybeg=" << ybeg << " yend=" << yend
4875 // << " xx=" << xx << " yy=" << yy
4876 // << " dist=" << xx + yy << endl;
4881 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4883 //lyxerr << "InsetTabular::editXY: " << this << endl;
4884 cur.setSelection(false);
4886 cur.idx() = getNearestCell(cur.bv(), x, y);
4888 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4892 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4894 cur.idx() = getNearestCell(cur.bv(), x, y);
4895 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4899 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4901 idx_type idx_min = 0;
4902 int dist_min = numeric_limits<int>::max();
4903 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4904 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4905 int const d = dist(bv, i, x, y);
4916 int InsetTabular::cellYPos(idx_type const cell) const
4918 row_type row = tabular.cellRow(cell);
4920 for (row_type r = 0; r < row; ++r)
4921 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4922 + tabular.interRowSpace(r + 1);
4927 int InsetTabular::cellXPos(idx_type const cell) const
4929 col_type col = tabular.cellColumn(cell);
4931 for (col_type c = 0; c < col; ++c)
4932 lx += tabular.column_info[c].width;
4937 void InsetTabular::resetPos(Cursor & cur) const
4939 BufferView & bv = cur.bv();
4940 int const maxwidth = bv.workWidth();
4942 int const scx_old = scx_;
4943 int const i = cur.find(this);
4948 int const X2 = maxwidth;
4949 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4950 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4951 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4954 scx_ = X1 + 20 - x1;
4956 scx_ = X2 - 20 - x2;
4961 // only update if offset changed
4962 if (scx_ != scx_old)
4963 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4967 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4969 row_type const row = tabular.cellRow(cur.idx());
4970 col_type const col = tabular.cellColumn(cur.idx());
4972 if (isRightToLeft(cur)) {
4973 if (tabular.cellColumn(cur.idx()) == 0) {
4974 if (row == tabular.nrows() - 1)
4976 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4981 cur.idx() = tabular.getLastCellInRow(row - 1);
4983 cur.idx() = tabular.cellIndex(row, col - 1);
4986 if (tabular.isLastCell(cur.idx()))
4988 if (cur.idx() == tabular.getLastCellInRow(row))
4989 cur.idx() = tabular.cellIndex(row + 1, 0);
4991 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4992 cur.idx() = tabular.cellIndex(row, colnextcell);
4996 cur.boundary(false);
4998 if (cur.selIsMultiCell()) {
4999 cur.pit() = cur.lastpit();
5000 cur.pos() = cur.lastpos();
5008 // in visual mode, place cursor at extreme left or right
5010 switch(entry_from) {
5012 case ENTRY_DIRECTION_RIGHT:
5013 cur.posVisToRowExtremity(false /* !left */);
5015 case ENTRY_DIRECTION_LEFT:
5016 cur.posVisToRowExtremity(true /* left */);
5018 case ENTRY_DIRECTION_IGNORE:
5019 // nothing to do in this case
5023 cur.setCurrentFont();
5028 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5030 row_type const row = tabular.cellRow(cur.idx());
5031 col_type const col = tabular.cellColumn(cur.idx());
5033 if (isRightToLeft(cur)) {
5034 if (cur.idx() == tabular.getLastCellInRow(row)) {
5037 cur.idx() = tabular.getFirstCellInRow(row);
5038 cur.idx() = tabular.cellAbove(cur.idx());
5040 if (tabular.isLastCell(cur.idx()))
5042 if (cur.idx() == tabular.getLastCellInRow(row))
5043 cur.idx() = tabular.cellIndex(row + 1, 0);
5045 cur.idx() = tabular.cellIndex(row, col + 1);
5048 if (cur.idx() == 0) // first cell
5051 cur.idx() = tabular.getLastCellInRow(row - 1);
5053 cur.idx() = tabular.cellIndex(row, col - 1);
5056 if (cur.selIsMultiCell()) {
5057 cur.pit() = cur.lastpit();
5058 cur.pos() = cur.lastpos();
5063 cur.pit() = cur.lastpit();
5064 cur.pos() = cur.lastpos();
5066 // in visual mode, place cursor at extreme left or right
5068 switch(entry_from) {
5070 case ENTRY_DIRECTION_RIGHT:
5071 cur.posVisToRowExtremity(false /* !left */);
5073 case ENTRY_DIRECTION_LEFT:
5074 cur.posVisToRowExtremity(true /* left */);
5076 case ENTRY_DIRECTION_IGNORE:
5077 // nothing to do in this case
5081 cur.setCurrentFont();
5086 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5088 istringstream is(argument);
5091 if (insetCode(s) != TABULAR_CODE)
5095 size_t safe_guard = 0;
5100 if (safe_guard > 1000) {
5101 LYXERR0("parameter max count reached!");
5105 Tabular::Feature action = Tabular::LAST_ACTION;
5108 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5109 if (s != tabularFeature[i].feature)
5112 action = tabularFeature[i].action;
5115 if (action == Tabular::LAST_ACTION) {
5116 LYXERR0("Feature not found " << s);
5120 if (tabularFeature[i].need_value)
5122 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5123 tabularFeatures(cur, action, val);
5129 static void checkLongtableSpecial(Tabular::ltType & ltt,
5130 string const & special, bool & flag)
5132 if (special == "dl_above") {
5135 } else if (special == "dl_below") {
5136 ltt.bottomDL = flag;
5138 } else if (special == "empty") {
5148 bool InsetTabular::oneCellHasRotationState(bool rotated,
5149 row_type row_start, row_type row_end,
5150 col_type col_start, col_type col_end) const
5152 for (row_type r = row_start; r <= row_end; ++r)
5153 for (col_type c = col_start; c <= col_end; ++c)
5154 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5160 void InsetTabular::tabularFeatures(Cursor & cur,
5161 Tabular::Feature feature, string const & value)
5163 col_type sel_col_start;
5164 col_type sel_col_end;
5165 row_type sel_row_start;
5166 row_type sel_row_end;
5167 bool setLines = false;
5168 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5169 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5173 case Tabular::M_ALIGN_LEFT:
5174 case Tabular::ALIGN_LEFT:
5175 setAlign = LYX_ALIGN_LEFT;
5178 case Tabular::M_ALIGN_RIGHT:
5179 case Tabular::ALIGN_RIGHT:
5180 setAlign = LYX_ALIGN_RIGHT;
5183 case Tabular::M_ALIGN_CENTER:
5184 case Tabular::ALIGN_CENTER:
5185 setAlign = LYX_ALIGN_CENTER;
5188 case Tabular::ALIGN_BLOCK:
5189 setAlign = LYX_ALIGN_BLOCK;
5192 case Tabular::ALIGN_DECIMAL:
5193 setAlign = LYX_ALIGN_DECIMAL;
5196 case Tabular::M_VALIGN_TOP:
5197 case Tabular::VALIGN_TOP:
5198 setVAlign = Tabular::LYX_VALIGN_TOP;
5201 case Tabular::M_VALIGN_BOTTOM:
5202 case Tabular::VALIGN_BOTTOM:
5203 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5206 case Tabular::M_VALIGN_MIDDLE:
5207 case Tabular::VALIGN_MIDDLE:
5208 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5215 cur.recordUndoInset(ATOMIC_UNDO, this);
5217 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5218 row_type const row = tabular.cellRow(cur.idx());
5219 col_type const column = tabular.cellColumn(cur.idx());
5221 Tabular::ltType ltt;
5225 case Tabular::SET_TABULAR_WIDTH:
5226 tabular.setTabularWidth(Length(value));
5229 case Tabular::SET_PWIDTH: {
5230 Length const len(value);
5231 tabular.setColumnPWidth(cur, cur.idx(), len);
5233 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5234 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5238 case Tabular::SET_MPWIDTH:
5239 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5242 case Tabular::SET_MROFFSET:
5243 tabular.setMROffset(cur, cur.idx(), Length(value));
5246 case Tabular::SET_SPECIAL_COLUMN:
5247 case Tabular::SET_SPECIAL_MULTICOLUMN:
5248 if (value == "none")
5249 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5251 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5254 case Tabular::APPEND_ROW:
5255 // append the row into the tabular
5256 tabular.appendRow(cur.idx());
5259 case Tabular::APPEND_COLUMN:
5260 // append the column into the tabular
5261 tabular.appendColumn(cur.idx());
5262 cur.idx() = tabular.cellIndex(row, column);
5265 case Tabular::DELETE_ROW:
5266 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5267 tabular.deleteRow(sel_row_start);
5268 if (sel_row_start >= tabular.nrows())
5270 cur.idx() = tabular.cellIndex(sel_row_start, column);
5273 cur.setSelection(false);
5276 case Tabular::DELETE_COLUMN:
5277 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5278 tabular.deleteColumn(sel_col_start);
5279 if (sel_col_start >= tabular.ncols())
5281 cur.idx() = tabular.cellIndex(row, sel_col_start);
5284 cur.setSelection(false);
5287 case Tabular::COPY_ROW:
5288 tabular.copyRow(row);
5291 case Tabular::COPY_COLUMN:
5292 tabular.copyColumn(column);
5293 cur.idx() = tabular.cellIndex(row, column);
5296 case Tabular::SET_LINE_TOP:
5297 case Tabular::TOGGLE_LINE_TOP: {
5298 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5299 ? (value == "true") : !tabular.topLine(cur.idx());
5300 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5301 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5302 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5306 case Tabular::SET_LINE_BOTTOM:
5307 case Tabular::TOGGLE_LINE_BOTTOM: {
5308 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5309 ? (value == "true") : !tabular.bottomLine(cur.idx());
5310 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5311 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5312 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5316 case Tabular::SET_LINE_LEFT:
5317 case Tabular::TOGGLE_LINE_LEFT: {
5318 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5319 ? (value == "true") : !tabular.leftLine(cur.idx());
5320 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5321 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5322 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5326 case Tabular::SET_LINE_RIGHT:
5327 case Tabular::TOGGLE_LINE_RIGHT: {
5328 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5329 ? (value == "true") : !tabular.rightLine(cur.idx());
5330 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5331 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5332 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5336 case Tabular::M_ALIGN_LEFT:
5337 case Tabular::M_ALIGN_RIGHT:
5338 case Tabular::M_ALIGN_CENTER:
5339 case Tabular::ALIGN_LEFT:
5340 case Tabular::ALIGN_RIGHT:
5341 case Tabular::ALIGN_CENTER:
5342 case Tabular::ALIGN_BLOCK:
5343 case Tabular::ALIGN_DECIMAL:
5344 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5345 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5346 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5347 !tabular.getPWidth(c).zero());
5350 case Tabular::M_VALIGN_TOP:
5351 case Tabular::M_VALIGN_BOTTOM:
5352 case Tabular::M_VALIGN_MIDDLE:
5354 case Tabular::VALIGN_TOP:
5355 case Tabular::VALIGN_BOTTOM:
5356 case Tabular::VALIGN_MIDDLE:
5357 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5358 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5359 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5362 case Tabular::SET_MULTICOLUMN: {
5363 if (!cur.selection()) {
5364 // just multicol for one single cell
5365 // check whether we are completely in a multicol
5366 if (!tabular.isMultiColumn(cur.idx()))
5367 tabular.setMultiColumn(cur.idx(), 1,
5368 tabular.rightLine(cur.idx()));
5371 // we have a selection so this means we just add all this
5372 // cells to form a multicolumn cell
5373 idx_type const s_start = cur.selBegin().idx();
5374 row_type const col_start = tabular.cellColumn(s_start);
5375 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5376 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5377 tabular.rightLine(cur.selEnd().idx()));
5380 cur.setSelection(false);
5384 case Tabular::UNSET_MULTICOLUMN: {
5385 if (!cur.selection()) {
5386 if (tabular.isMultiColumn(cur.idx()))
5387 tabular.unsetMultiColumn(cur.idx());
5392 case Tabular::MULTICOLUMN: {
5393 if (!cur.selection()) {
5394 if (tabular.isMultiColumn(cur.idx()))
5395 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5397 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5401 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5402 row_type const r = sel_row_start;
5403 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5404 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5407 // If the selection contains at least one singlecol cell
5408 // or multiple multicol cells,
5409 // we assume the user will merge is to a single multicol
5411 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5413 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5417 case Tabular::SET_MULTIROW: {
5418 if (!cur.selection()) {
5419 // just multirow for one single cell
5420 // check whether we are completely in a multirow
5421 if (!tabular.isMultiRow(cur.idx()))
5422 tabular.setMultiRow(cur.idx(), 1,
5423 tabular.bottomLine(cur.idx()));
5426 // we have a selection so this means we just add all this
5427 // cells to form a multirow cell
5428 idx_type const s_start = cur.selBegin().idx();
5429 row_type const row_start = tabular.cellRow(s_start);
5430 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5431 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5432 tabular.bottomLine(cur.selEnd().idx()));
5435 cur.setSelection(false);
5439 case Tabular::UNSET_MULTIROW: {
5440 if (!cur.selection()) {
5441 if (tabular.isMultiRow(cur.idx()))
5442 tabular.unsetMultiRow(cur.idx());
5447 case Tabular::MULTIROW: {
5448 if (!cur.selection()) {
5449 if (tabular.isMultiRow(cur.idx()))
5450 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5452 tabularFeatures(cur, Tabular::SET_MULTIROW);
5456 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5457 col_type const c = sel_col_start;
5458 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5459 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5462 // If the selection contains at least one singlerow cell
5463 // or multiple multirow cells,
5464 // we assume the user will merge is to a single multirow
5466 tabularFeatures(cur, Tabular::SET_MULTIROW);
5468 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5472 case Tabular::SET_ALL_LINES:
5474 case Tabular::UNSET_ALL_LINES:
5475 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5476 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5477 idx_type const cell = tabular.cellIndex(r, c);
5478 tabular.setTopLine(cell, setLines);
5479 tabular.setBottomLine(cell, setLines);
5480 tabular.setRightLine(cell, setLines);
5481 tabular.setLeftLine(cell, setLines);
5485 case Tabular::SET_BORDER_LINES:
5486 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5487 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5488 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5490 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5491 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5492 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5496 case Tabular::SET_LONGTABULAR:
5497 tabular.is_long_tabular = true;
5500 case Tabular::UNSET_LONGTABULAR:
5501 for (row_type r = 0; r < tabular.nrows(); ++r) {
5502 if (tabular.ltCaption(r)) {
5503 cur.idx() = tabular.cellIndex(r, 0);
5506 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5509 tabular.is_long_tabular = false;
5512 case Tabular::SET_ROTATE_TABULAR:
5513 tabular.rotate = true;
5516 case Tabular::UNSET_ROTATE_TABULAR:
5517 tabular.rotate = false;
5520 case Tabular::TOGGLE_ROTATE_TABULAR:
5521 tabular.rotate = !tabular.rotate;
5524 case Tabular::TABULAR_VALIGN_TOP:
5525 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5528 case Tabular::TABULAR_VALIGN_MIDDLE:
5529 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5532 case Tabular::TABULAR_VALIGN_BOTTOM:
5533 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5536 case Tabular::LONGTABULAR_ALIGN_LEFT:
5537 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5540 case Tabular::LONGTABULAR_ALIGN_CENTER:
5541 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5544 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5545 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5550 case Tabular::SET_ROTATE_CELL:
5551 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5552 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5553 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5556 case Tabular::UNSET_ROTATE_CELL:
5557 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5558 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5559 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5562 case Tabular::TOGGLE_ROTATE_CELL:
5564 bool oneNotRotated = oneCellHasRotationState(false,
5565 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5567 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5568 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5569 tabular.setRotateCell(tabular.cellIndex(r, c),
5574 case Tabular::SET_USEBOX: {
5575 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5576 if (val == tabular.getUsebox(cur.idx()))
5577 val = Tabular::BOX_NONE;
5578 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5579 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5580 tabular.setUsebox(tabular.cellIndex(r, c), val);
5584 case Tabular::UNSET_LTFIRSTHEAD:
5586 case Tabular::SET_LTFIRSTHEAD:
5587 tabular.getRowOfLTFirstHead(row, ltt);
5588 checkLongtableSpecial(ltt, value, flag);
5589 tabular.setLTHead(row, flag, ltt, true);
5592 case Tabular::UNSET_LTHEAD:
5594 case Tabular::SET_LTHEAD:
5595 tabular.getRowOfLTHead(row, ltt);
5596 checkLongtableSpecial(ltt, value, flag);
5597 tabular.setLTHead(row, flag, ltt, false);
5600 case Tabular::UNSET_LTFOOT:
5602 case Tabular::SET_LTFOOT:
5603 tabular.getRowOfLTFoot(row, ltt);
5604 checkLongtableSpecial(ltt, value, flag);
5605 tabular.setLTFoot(row, flag, ltt, false);
5608 case Tabular::UNSET_LTLASTFOOT:
5610 case Tabular::SET_LTLASTFOOT:
5611 tabular.getRowOfLTLastFoot(row, ltt);
5612 checkLongtableSpecial(ltt, value, flag);
5613 tabular.setLTFoot(row, flag, ltt, true);
5616 case Tabular::SET_LTNEWPAGE:
5617 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5620 case Tabular::SET_LTCAPTION: {
5621 if (tabular.ltCaption(row))
5623 cur.idx() = tabular.setLTCaption(row, true);
5626 cur.setSelection(false);
5627 // If a row is set as caption, then also insert
5628 // a caption. Otherwise the LaTeX output is broken.
5629 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5630 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5634 case Tabular::UNSET_LTCAPTION: {
5635 if (!tabular.ltCaption(row))
5637 cur.idx() = tabular.setLTCaption(row, false);
5640 cur.setSelection(false);
5641 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5642 if (lyx::getStatus(fr).enabled())
5647 case Tabular::TOGGLE_LTCAPTION: {
5648 if (tabular.ltCaption(row))
5649 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5651 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5655 case Tabular::SET_BOOKTABS:
5656 tabular.use_booktabs = true;
5659 case Tabular::UNSET_BOOKTABS:
5660 tabular.use_booktabs = false;
5663 case Tabular::SET_TOP_SPACE: {
5665 if (value == "default")
5666 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5667 tabular.row_info[r].top_space_default = true;
5668 else if (value == "none")
5669 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5670 tabular.row_info[r].top_space_default = false;
5671 tabular.row_info[r].top_space = len;
5673 else if (isValidLength(value, &len))
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;
5681 case Tabular::SET_BOTTOM_SPACE: {
5683 if (value == "default")
5684 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5685 tabular.row_info[r].bottom_space_default = true;
5686 else if (value == "none")
5687 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5688 tabular.row_info[r].bottom_space_default = false;
5689 tabular.row_info[r].bottom_space = len;
5691 else if (isValidLength(value, &len))
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;
5699 case Tabular::SET_INTERLINE_SPACE: {
5701 if (value == "default")
5702 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5703 tabular.row_info[r].interline_space_default = true;
5704 else if (value == "none")
5705 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5706 tabular.row_info[r].interline_space_default = false;
5707 tabular.row_info[r].interline_space = len;
5709 else if (isValidLength(value, &len))
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;
5717 case Tabular::SET_DECIMAL_POINT:
5718 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5719 tabular.column_info[c].decimal_point = from_utf8(value);
5722 // dummy stuff just to avoid warnings
5723 case Tabular::LAST_ACTION:
5729 bool InsetTabular::copySelection(Cursor & cur)
5731 if (!cur.selection())
5736 getSelection(cur, rs, re, cs, ce);
5738 paste_tabular.reset(new Tabular(tabular));
5740 for (row_type r = 0; r < rs; ++r)
5741 paste_tabular->deleteRow(0);
5743 row_type const rows = re - rs + 1;
5744 while (paste_tabular->nrows() > rows)
5745 paste_tabular->deleteRow(rows);
5747 for (col_type c = 0; c < cs; ++c)
5748 paste_tabular->deleteColumn(0);
5750 col_type const columns = ce - cs + 1;
5751 while (paste_tabular->ncols() > columns)
5752 paste_tabular->deleteColumn(columns);
5754 paste_tabular->setBuffer(tabular.buffer());
5756 odocstringstream os;
5757 OutputParams const runparams(0);
5758 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5759 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5760 cap::copySelection(cur, os.str());
5762 // mark tabular stack dirty
5763 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5764 // when we (hopefully) have a one-for-all paste mechanism.
5765 // This must be called after cap::copySelection.
5766 dirtyTabularStack(true);
5772 bool InsetTabular::pasteClipboard(Cursor & cur)
5776 col_type actcol = tabular.cellColumn(cur.idx());
5777 row_type actrow = tabular.cellRow(cur.idx());
5779 if (cur.selIsMultiCell()) {
5782 getSelection(cur, actrow, re, actcol, ce);
5785 for (row_type r1 = 0, r2 = actrow;
5786 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5788 for (col_type c1 = 0, c2 = actcol;
5789 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5791 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5792 tabular.isPartOfMultiColumn(r2, c2))
5794 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5798 if (tabular.isPartOfMultiColumn(r2, c2)) {
5802 shared_ptr<InsetTableCell> inset(
5803 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5804 tabular.setCellInset(r2, c2, inset);
5805 // FIXME?: why do we need to do this explicitly? (EL)
5806 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5808 // FIXME: change tracking (MG)
5809 inset->setChange(Change(buffer().params().trackChanges ?
5810 Change::INSERTED : Change::UNCHANGED));
5818 void InsetTabular::cutSelection(Cursor & cur)
5820 if (!cur.selection())
5825 getSelection(cur, rs, re, cs, ce);
5826 for (row_type r = rs; r <= re; ++r) {
5827 for (col_type c = cs; c <= ce; ++c) {
5828 shared_ptr<InsetTableCell> t
5829 = cell(tabular.cellIndex(r, c));
5830 if (buffer().params().trackChanges)
5831 // FIXME: Change tracking (MG)
5832 t->setChange(Change(Change::DELETED));
5838 // cursor position might be invalid now
5839 if (cur.pit() > cur.lastpit())
5840 cur.pit() = cur.lastpit();
5841 if (cur.pos() > cur.lastpos())
5842 cur.pos() = cur.lastpos();
5843 cur.clearSelection();
5847 bool InsetTabular::isRightToLeft(Cursor & cur) const
5849 LASSERT(cur.depth() > 1, /**/);
5850 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5851 pos_type const parentpos = cur[cur.depth() - 2].pos();
5852 return parentpar.getFontSettings(buffer().params(),
5853 parentpos).language()->rightToLeft();
5857 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5860 LASSERT(stidx <= enidx, return docstring());
5862 col_type const col1 = tabular.cellColumn(stidx);
5863 col_type const col2 = tabular.cellColumn(enidx);
5864 row_type const row1 = tabular.cellRow(stidx);
5865 row_type const row2 = tabular.cellRow(enidx);
5867 for (col_type col = col1; col <= col2; col++)
5868 for (row_type row = row1; row <= row2; row++) {
5873 retval += tabular.cellInset(row, col)->asString(intoInsets);
5879 void InsetTabular::getSelection(Cursor & cur,
5880 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5882 CursorSlice const & beg = cur.selBegin();
5883 CursorSlice const & end = cur.selEnd();
5884 cs = tabular.cellColumn(beg.idx());
5885 ce = tabular.cellColumn(end.idx());
5889 rs = tabular.cellRow(beg.idx());
5890 re = tabular.cellRow(end.idx());
5896 Text * InsetTabular::getText(int idx) const
5898 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5902 void InsetTabular::setChange(Change const & change)
5904 for (idx_type idx = 0; idx < nargs(); ++idx)
5905 cell(idx)->setChange(change);
5909 void InsetTabular::acceptChanges()
5911 for (idx_type idx = 0; idx < nargs(); ++idx)
5912 cell(idx)->acceptChanges();
5916 void InsetTabular::rejectChanges()
5918 for (idx_type idx = 0; idx < nargs(); ++idx)
5919 cell(idx)->rejectChanges();
5923 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5925 return tabular.getPWidth(cell).zero();
5929 bool InsetTabular::forcePlainLayout(idx_type cell) const
5931 return !tabular.getPWidth(cell).zero();
5935 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5938 if (buf.length() <= 0)
5943 col_type maxCols = 1;
5944 size_t const len = buf.length();
5948 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5956 maxCols = max(cols, maxCols);
5962 maxCols = max(cols, maxCols);
5968 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5969 loctab = paste_tabular.get();
5971 dirtyTabularStack(true);
5974 cell = bv.cursor().idx();
5975 ocol = tabular.cellColumn(cell);
5976 row = tabular.cellRow(cell);
5980 idx_type const cells = loctab->numberofcells;
5983 rows = loctab->nrows();
5984 col_type const columns = loctab->ncols();
5986 while (cell < cells && p < len && row < rows &&
5987 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5993 // we can only set this if we are not too far right
5994 if (cols < columns) {
5995 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5996 Font const font = bv.textMetrics(&inset->text()).
5998 inset->setText(buf.substr(op, p - op), font,
5999 buffer().params().trackChanges);
6005 // we can only set this if we are not too far right
6006 if (cols < columns) {
6007 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6008 Font const font = bv.textMetrics(&inset->text()).
6010 inset->setText(buf.substr(op, p - op), font,
6011 buffer().params().trackChanges);
6016 cell = loctab->cellIndex(row, cols);
6022 // check for the last cell if there is no trailing '\n'
6023 if (cell < cells && op < len) {
6024 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6025 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6026 inset->setText(buf.substr(op, len - op), font,
6027 buffer().params().trackChanges);
6033 void InsetTabular::addPreview(DocIterator const & inset_pos,
6034 PreviewLoader & loader) const
6036 DocIterator cell_pos = inset_pos;
6038 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6039 for (row_type r = 0; r < tabular.nrows(); ++r) {
6040 for (col_type c = 0; c < tabular.ncols(); ++c) {
6041 cell_pos.top().idx() = tabular.cellIndex(r, c);
6042 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6048 bool InsetTabular::completionSupported(Cursor const & cur) const
6050 Cursor const & bvCur = cur.bv().cursor();
6051 if (&bvCur.inset() != this)
6053 return cur.text()->completionSupported(cur);
6057 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6059 return completionSupported(cur);
6063 bool InsetTabular::automaticInlineCompletion() const
6065 return lyxrc.completion_inline_text;
6069 bool InsetTabular::automaticPopupCompletion() const
6071 return lyxrc.completion_popup_text;
6075 bool InsetTabular::showCompletionCursor() const
6077 return lyxrc.completion_cursor_text;
6081 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6083 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6087 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6089 if (!completionSupported(cur))
6091 return cur.text()->completionPrefix(cur);
6095 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6097 if (!completionSupported(cur))
6100 return cur.text()->insertCompletion(cur, s, finished);
6104 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6105 Dimension & dim) const
6107 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6108 tm.completionPosAndDim(cur, x, y, dim);
6112 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6114 istringstream data(in);
6116 lex.setStream(data);
6123 if (!lex || token != "tabular") {
6124 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6128 // This is part of the inset proper that is usually swallowed
6129 // by Buffer::readInset
6131 if (!lex || token != "Tabular") {
6132 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6140 string InsetTabular::params2string(InsetTabular const & inset)
6143 data << "tabular" << ' ';
6145 data << "\\end_inset\n";