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::LAST_ACTION, "", false }
191 string const write_attribute(string const & name, T const & t)
193 string const s = tostr(t);
194 return s.empty() ? s : " " + name + "=\"" + s + "\"";
198 string const write_attribute(string const & name, string const & t)
200 return t.empty() ? t : " " + name + "=\"" + t + "\"";
205 string const write_attribute(string const & name, docstring const & t)
207 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
212 string const write_attribute(string const & name, bool const & b)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return b ? write_attribute(name, convert<string>(b)) : string();
221 string const write_attribute(string const & name, int const & i)
223 // we write only true attribute values so we remove a bit of the
224 // file format bloat for tabulars.
225 return i ? write_attribute(name, convert<string>(i)) : string();
230 string const write_attribute(string const & name, Tabular::idx_type const & i)
232 // we write only true attribute values so we remove a bit of the
233 // file format bloat for tabulars.
234 return i ? write_attribute(name, convert<string>(i)) : string();
239 string const write_attribute(string const & name, Length const & value)
241 // we write only the value if we really have one same reson as above.
242 return value.zero() ? string() : write_attribute(name, value.asString());
246 string const tostr(LyXAlignment const & num)
251 case LYX_ALIGN_BLOCK:
255 case LYX_ALIGN_CENTER:
257 case LYX_ALIGN_RIGHT:
259 case LYX_ALIGN_LAYOUT:
261 case LYX_ALIGN_SPECIAL:
263 case LYX_ALIGN_DECIMAL:
270 string const tostr(Tabular::HAlignment const & num)
273 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
275 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
277 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
284 string const tostr(Tabular::VAlignment const & num)
287 case Tabular::LYX_VALIGN_TOP:
289 case Tabular::LYX_VALIGN_MIDDLE:
291 case Tabular::LYX_VALIGN_BOTTOM:
298 string const tostr(Tabular::BoxType const & num)
301 case Tabular::BOX_NONE:
303 case Tabular::BOX_PARBOX:
305 case Tabular::BOX_MINIPAGE:
312 // I would have liked a fromstr template a lot better. (Lgb)
313 bool string2type(string const str, LyXAlignment & num)
316 num = LYX_ALIGN_NONE;
317 else if (str == "block")
318 num = LYX_ALIGN_BLOCK;
319 else if (str == "left")
320 num = LYX_ALIGN_LEFT;
321 else if (str == "center")
322 num = LYX_ALIGN_CENTER;
323 else if (str == "right")
324 num = LYX_ALIGN_RIGHT;
325 else if (str == "decimal")
326 num = LYX_ALIGN_DECIMAL;
333 bool string2type(string const str, Tabular::HAlignment & num)
336 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
337 else if (str == "center" )
338 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
339 else if (str == "right")
340 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
347 bool string2type(string const str, Tabular::VAlignment & num)
350 num = Tabular::LYX_VALIGN_TOP;
351 else if (str == "middle" )
352 num = Tabular::LYX_VALIGN_MIDDLE;
353 else if (str == "bottom")
354 num = Tabular::LYX_VALIGN_BOTTOM;
361 bool string2type(string const str, Tabular::BoxType & num)
364 num = Tabular::BOX_NONE;
365 else if (str == "parbox")
366 num = Tabular::BOX_PARBOX;
367 else if (str == "minipage")
368 num = Tabular::BOX_MINIPAGE;
375 bool string2type(string const str, bool & num)
379 else if (str == "false")
387 bool getTokenValue(string const & str, char const * token, string & ret)
390 size_t token_length = strlen(token);
391 size_t pos = str.find(token);
393 if (pos == string::npos || pos + token_length + 1 >= str.length()
394 || str[pos + token_length] != '=')
396 pos += token_length + 1;
398 if (ch != '"' && ch != '\'') { // only read till next space
402 while (pos < str.length() - 1 && str[++pos] != ch)
409 bool getTokenValue(string const & str, char const * token, docstring & ret)
412 bool const success = getTokenValue(str, token, tmp);
413 ret = from_utf8(tmp);
418 bool getTokenValue(string const & str, char const * token, int & num)
422 if (!getTokenValue(str, token, tmp))
424 num = convert<int>(tmp);
429 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
432 return getTokenValue(str, token, tmp) && string2type(tmp, num);
436 bool getTokenValue(string const & str, char const * token,
437 Tabular::HAlignment & num)
440 return getTokenValue(str, token, tmp) && string2type(tmp, num);
444 bool getTokenValue(string const & str, char const * token,
445 Tabular::VAlignment & num)
448 return getTokenValue(str, token, tmp) && string2type(tmp, num);
452 bool getTokenValue(string const & str, char const * token,
453 Tabular::BoxType & num)
456 return getTokenValue(str, token, tmp) && string2type(tmp, num);
460 bool getTokenValue(string const & str, char const * token, bool & flag)
462 // set the flag always to false as this should be the default for bools
463 // not in the file-format.
466 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
470 bool getTokenValue(string const & str, char const * token, Length & len)
472 // set the length to be zero() as default as this it should be if not
473 // in the file format.
476 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
480 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
485 if (!getTokenValue(str, token, tmp))
487 if (tmp == "default") {
491 return isValidLength(tmp, &len);
495 void l_getline(istream & is, string & str)
498 while (str.empty()) {
500 if (!str.empty() && str[str.length() - 1] == '\r')
501 str.erase(str.length() - 1);
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 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
520 InsetTableCell tail = InsetTableCell(head);
522 DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
523 for (; dit; dit.forwardChar())
524 if (dit.inTexted() && dit.depth()==1
525 && dit.paragraph().find(align_d, false, false, dit.pos()))
528 pit_type const psize = head.paragraphs().front().size();
531 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
532 tail.paragraphs().front().eraseChars(0,
533 dit.pos() < psize ? dit.pos() + 1 : psize, false);
540 /////////////////////////////////////////////////////////////////////
544 /////////////////////////////////////////////////////////////////////
547 Tabular::CellData::CellData(Buffer * buf)
550 multicolumn(Tabular::CELL_NORMAL),
551 multirow(Tabular::CELL_NORMAL),
552 alignment(LYX_ALIGN_CENTER),
553 valignment(LYX_VALIGN_TOP),
563 inset(new InsetTableCell(buf))
565 inset->setBuffer(*buf);
569 Tabular::CellData::CellData(CellData const & cs)
572 multicolumn(cs.multicolumn),
573 multirow(cs.multirow),
574 mroffset(cs.mroffset),
575 alignment(cs.alignment),
576 valignment(cs.valignment),
577 decimal_hoffset(cs.decimal_hoffset),
578 decimal_width(cs.decimal_width),
580 top_line(cs.top_line),
581 bottom_line(cs.bottom_line),
582 left_line(cs.left_line),
583 right_line(cs.right_line),
586 align_special(cs.align_special),
588 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
592 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
598 void Tabular::CellData::swap(CellData & rhs)
600 std::swap(cellno, rhs.cellno);
601 std::swap(width, rhs.width);
602 std::swap(multicolumn, rhs.multicolumn);
603 std::swap(multirow, rhs.multirow);
604 std::swap(mroffset, rhs.mroffset);
605 std::swap(alignment, rhs.alignment);
606 std::swap(valignment, rhs.valignment);
607 std::swap(decimal_hoffset, rhs.decimal_hoffset);
608 std::swap(decimal_width, rhs.decimal_width);
609 std::swap(voffset, rhs.voffset);
610 std::swap(top_line, rhs.top_line);
611 std::swap(bottom_line, rhs.bottom_line);
612 std::swap(left_line, rhs.left_line);
613 std::swap(right_line, rhs.right_line);
614 std::swap(usebox, rhs.usebox);
615 std::swap(rotate, rhs.rotate);
616 std::swap(align_special, rhs.align_special);
617 p_width.swap(rhs.p_width);
618 inset.swap(rhs.inset);
622 Tabular::RowData::RowData()
625 top_space_default(false),
626 bottom_space_default(false),
627 interline_space_default(false),
637 Tabular::ColumnData::ColumnData()
638 : alignment(LYX_ALIGN_CENTER),
639 valignment(LYX_VALIGN_TOP),
645 Tabular::ltType::ltType()
652 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
654 init(buffer, rows_arg, columns_arg);
658 void Tabular::setBuffer(Buffer & buffer)
661 for (row_type i = 0; i < nrows(); ++i)
662 for (col_type j = 0; j < ncols(); ++j)
663 cell_info[i][j].inset->setBuffer(*buffer_);
667 // activates all lines and sets all widths to 0
668 void Tabular::init(Buffer * buf, row_type rows_arg,
669 col_type columns_arg)
672 row_info = row_vector(rows_arg);
673 column_info = column_vector(columns_arg);
674 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
675 row_info.reserve(10);
676 column_info.reserve(10);
677 cell_info.reserve(100);
679 is_long_tabular = false;
680 tabular_valignment = LYX_VALIGN_MIDDLE;
681 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
683 use_booktabs = false;
684 // set silly default lines
685 for (row_type r = 0; r < nrows(); ++r)
686 for (col_type c = 0; c < ncols(); ++c) {
687 cell_info[r][c].inset->setBuffer(*buffer_);
688 cell_info[r][c].top_line = true;
689 cell_info[r][c].left_line = true;
690 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
691 cell_info[r][c].right_line = c == ncols() - 1;
696 void Tabular::appendRow(idx_type const cell)
698 row_type const row = cellRow(cell);
700 row_info.insert(row_info.begin() + row + 1, RowData());
701 row_info[row + 1] = row_info[row];
703 cell_info.insert(cell_info.begin() + row + 1,
704 cell_vector(ncols(), CellData(buffer_)));
705 for (col_type c = 0; c < ncols(); ++c) {
706 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
707 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
709 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
713 for (col_type c = 0; c < ncols(); ++c) {
714 if (isPartOfMultiRow(row, c))
716 // inherit line settings
717 idx_type const i = cellIndex(row + 1, c);
718 idx_type const j = cellIndex(row, c);
719 setLeftLine(i, leftLine(j));
720 setRightLine(i, rightLine(j));
721 setTopLine(i, topLine(j));
722 if (topLine(j) && bottomLine(j)) {
723 setBottomLine(i, true);
724 setBottomLine(j, false);
726 // mark track changes
727 if (buffer().params().trackChanges)
728 cellInfo(i).inset->setChange(Change(Change::INSERTED));
733 void Tabular::deleteRow(row_type const row)
735 // Not allowed to delete last row
739 for (col_type c = 0; c < ncols(); ++c) {
740 // Care about multirow cells
741 if (row + 1 < nrows() &&
742 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
743 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
744 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
747 row_info.erase(row_info.begin() + row);
748 cell_info.erase(cell_info.begin() + row);
753 void Tabular::copyRow(row_type const row)
755 row_info.insert(row_info.begin() + row, row_info[row]);
756 cell_info.insert(cell_info.begin() + row, cell_info[row]);
758 if (buffer().params().trackChanges)
759 for (col_type c = 0; c < ncols(); ++c)
760 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
766 void Tabular::appendColumn(idx_type const cell)
768 col_type const c = cellColumn(cell);
770 column_info.insert(column_info.begin() + c + 1, ColumnData());
771 column_info[c + 1] = column_info[c];
773 for (row_type r = 0; r < nrows(); ++r) {
774 cell_info[r].insert(cell_info[r].begin() + c + 1,
776 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
777 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
779 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
782 for (row_type r = 0; r < nrows(); ++r) {
783 // inherit line settings
784 idx_type const i = cellIndex(r, c + 1);
785 idx_type const j = cellIndex(r, c);
786 setBottomLine(i, bottomLine(j));
787 setTopLine(i, topLine(j));
788 setLeftLine(i, leftLine(j));
789 if (rightLine(j) && rightLine(j)) {
790 setRightLine(i, true);
791 setRightLine(j, false);
793 if (buffer().params().trackChanges)
794 cellInfo(i).inset->setChange(Change(Change::INSERTED));
799 void Tabular::deleteColumn(col_type const col)
801 // Not allowed to delete last column
805 for (row_type r = 0; r < nrows(); ++r) {
806 // Care about multicolumn cells
807 if (col + 1 < ncols() &&
808 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
809 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
810 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
812 cell_info[r].erase(cell_info[r].begin() + col);
814 column_info.erase(column_info.begin() + col);
819 void Tabular::copyColumn(col_type const col)
821 BufferParams const & bp = buffer().params();
822 column_info.insert(column_info.begin() + col, column_info[col]);
824 for (row_type r = 0; r < nrows(); ++r) {
825 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
827 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
833 void Tabular::updateIndexes()
838 for (row_type row = 0; row < nrows(); ++row)
839 for (col_type column = 0; column < ncols(); ++column) {
840 if (!isPartOfMultiColumn(row, column)
841 && !isPartOfMultiRow(row, column))
843 if (isPartOfMultiRow(row, column))
844 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
846 cell_info[row][column].cellno = numberofcells - 1;
849 rowofcell.resize(numberofcells);
850 columnofcell.resize(numberofcells);
852 // reset column and row of cells and update their width and alignment
853 for (row_type row = 0; row < nrows(); ++row)
854 for (col_type column = 0; column < ncols(); ++column) {
855 if (isPartOfMultiColumn(row, column))
857 // columnofcell needs to be called before setting width and aligment
858 // multirow cells inherit the width from the column width
859 if (!isPartOfMultiRow(row, column)) {
860 columnofcell[i] = column;
863 setFixedWidth(row, column);
864 if (isPartOfMultiRow(row, column))
866 cell_info[row][column].inset->setContentAlignment(
867 getAlignment(cellIndex(row, column)));
873 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
876 for (col_type c = 0; c < ncols(); ++c)
877 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
883 bool Tabular::topLine(idx_type const cell) const
885 return cellInfo(cell).top_line;
889 bool Tabular::bottomLine(idx_type const cell) const
891 return cellInfo(cell).bottom_line;
895 bool Tabular::leftLine(idx_type cell) const
899 return cellInfo(cell).left_line;
903 bool Tabular::rightLine(idx_type cell) const
907 return cellInfo(cell).right_line;
911 int Tabular::interRowSpace(row_type row) const
913 if (!row || row >= nrows())
916 int const interline_space = row_info[row - 1].interline_space_default ?
918 row_info[row - 1].interline_space.inPixels(width());
919 if (rowTopLine(row) && rowBottomLine(row - 1))
920 return interline_space + WIDTH_OF_LINE;
921 return interline_space;
925 int Tabular::interColumnSpace(idx_type cell) const
927 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
928 if (rightLine(cell) && nextcol < ncols()
929 && leftLine(cellIndex(cellRow(cell), nextcol)))
930 return WIDTH_OF_LINE;
935 int Tabular::cellWidth(idx_type cell) const
938 col_type const span = columnSpan(cell);
939 col_type const col = cellColumn(cell);
940 for(col_type c = col; c < col + span ; ++c)
941 w += column_info[c].width;
946 int Tabular::cellHeight(idx_type cell) const
948 row_type const span = rowSpan(cell);
949 row_type const row = cellRow(cell);
951 for(row_type r = row; r < row + span ; ++r) {
952 h += rowAscent(r) + rowDescent(r);
953 if (r != row + span - 1)
954 h += interRowSpace(r + 1);
961 bool Tabular::updateColumnWidths()
963 vector<int> max_dwidth(ncols(), 0);
964 for(col_type c = 0; c < ncols(); ++c)
965 for(row_type r = 0; r < nrows(); ++r) {
966 idx_type const i = cellIndex(r, c);
967 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
968 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
972 // for each col get max of single col cells
973 for(col_type c = 0; c < ncols(); ++c) {
975 for(row_type r = 0; r < nrows(); ++r) {
976 idx_type const i = cellIndex(r, c);
977 if (columnSpan(i) == 1) {
978 if (getAlignment(i) == LYX_ALIGN_DECIMAL
979 && cell_info[r][c].decimal_width!=0)
980 new_width = max(new_width, cellInfo(i).width
981 + max_dwidth[c] - cellInfo(i).decimal_width);
983 new_width = max(new_width, cellInfo(i).width);
987 if (column_info[c].width != new_width) {
988 column_info[c].width = new_width;
992 // update col widths to fit merged cells
993 for(col_type c = 0; c < ncols(); ++c)
994 for(row_type r = 0; r < nrows(); ++r) {
995 idx_type const i = cellIndex(r, c);
996 int const span = columnSpan(i);
997 if (span == 1 || c > cellColumn(i))
1001 for(col_type j = c; j < c + span ; ++j)
1002 old_width += column_info[j].width;
1004 if (cellInfo(i).width > old_width) {
1005 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1014 int Tabular::width() const
1017 for (col_type c = 0; c < ncols(); ++c)
1018 width += column_info[c].width;
1023 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1026 col_type const col = cellColumn(cell);
1027 // set alignment for the whole row if we are not in a multicolumn cell,
1028 // exclude possible multicolumn cells in the row
1029 if (!isMultiColumn(cell)) {
1030 for (row_type r = 0; r < nrows(); ++r) {
1031 // only if the column has no width the multirow inherits the
1032 // alignment of the column, otherwise it is left aligned
1033 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1034 && !isMultiColumn(cellIndex(r, col))) {
1035 cell_info[r][col].alignment = align;
1036 cell_info[r][col].inset->setContentAlignment(align);
1038 if ((isMultiRow(cellIndex(r, col)) && has_width)
1039 && !isMultiColumn(cellIndex(r, col))) {
1040 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1041 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1044 column_info[col].alignment = align;
1045 docstring & dpoint = column_info[col].decimal_point;
1046 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1047 dpoint = from_utf8(lyxrc.default_decimal_point);
1049 cellInfo(cell).alignment = align;
1050 cellInset(cell).get()->setContentAlignment(align);
1055 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1058 if (!isMultiColumn(cell) || onlycolumn)
1059 column_info[cellColumn(cell)].valignment = align;
1061 cellInfo(cell).valignment = align;
1068 * Allow line and paragraph breaks for fixed width cells or disallow them,
1069 * merge cell paragraphs and reset layout to standard for variable width
1072 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1074 inset->setAutoBreakRows(fixedWidth);
1075 inset->toggleFixedWidth(fixedWidth);
1079 // merge all paragraphs to one
1080 BufferParams const & bp = cur.bv().buffer().params();
1081 while (inset->paragraphs().size() > 1)
1082 mergeParagraph(bp, inset->paragraphs(), 0);
1086 // undo information has already been recorded
1087 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1088 bp.documentClass().plainLayoutName());
1095 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1096 Length const & width)
1098 col_type const c = cellColumn(cell);
1100 column_info[c].p_width = width;
1101 // reset the vertical alignment to top if the fixed with
1102 // is removed or zero because only fixed width columns can
1103 // have a vertical alignment
1104 if (column_info[c].p_width.zero())
1105 column_info[c].valignment = LYX_VALIGN_TOP;
1106 for (row_type r = 0; r < nrows(); ++r) {
1107 idx_type const cell = cellIndex(r, c);
1108 // because of multicolumns
1109 toggleFixedWidth(cur, cellInset(cell).get(),
1110 !getPWidth(cell).zero());
1112 // cur paragraph can become invalid after paragraphs were merged
1113 if (cur.pit() > cur.lastpit())
1114 cur.pit() = cur.lastpit();
1115 // cur position can become invalid after newlines were removed
1116 if (cur.pos() > cur.lastpos())
1117 cur.pos() = cur.lastpos();
1121 bool Tabular::setFixedWidth(row_type r, col_type c)
1123 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1124 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1125 || (multicol && !cell_info[r][c].p_width.zero());
1126 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1131 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1132 Length const & width)
1134 if (!isMultiColumn(cell))
1137 cellInfo(cell).p_width = width;
1138 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1139 // cur paragraph can become invalid after paragraphs were merged
1140 if (cur.pit() > cur.lastpit())
1141 cur.pit() = cur.lastpit();
1142 // cur position can become invalid after newlines were removed
1143 if (cur.pos() > cur.lastpos())
1144 cur.pos() = cur.lastpos();
1149 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1151 cellInfo(cell).mroffset = mroffset;
1156 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1157 Tabular::Feature what)
1159 if (what == SET_SPECIAL_MULTICOLUMN)
1160 cellInfo(cell).align_special = special;
1162 column_info[cellColumn(cell)].align_special = special;
1166 void Tabular::setTopLine(idx_type i, bool line)
1168 cellInfo(i).top_line = line;
1172 void Tabular::setBottomLine(idx_type i, bool line)
1174 cellInfo(i).bottom_line = line;
1178 void Tabular::setLeftLine(idx_type cell, bool line)
1180 cellInfo(cell).left_line = line;
1184 void Tabular::setRightLine(idx_type cell, bool line)
1186 cellInfo(cell).right_line = line;
1189 bool Tabular::rowTopLine(row_type r) const
1191 bool all_rows_set = true;
1192 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1193 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1194 return all_rows_set;
1198 bool Tabular::rowBottomLine(row_type r) const
1200 bool all_rows_set = true;
1201 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1202 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1203 return all_rows_set;
1207 bool Tabular::columnLeftLine(col_type c) const
1214 for (row_type r = 0; r < nrows(); ++r) {
1215 idx_type const i = cellIndex(r, c);
1216 if (c == cellColumn(i)) {
1218 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1219 if (cellInfo(i).left_line || right)
1223 return 2 * nrows_left >= total;
1227 bool Tabular::columnRightLine(col_type c) const
1232 int nrows_right = 0;
1234 for (row_type r = 0; r < nrows(); ++r) {
1235 idx_type i = cellIndex(r, c);
1236 if (c == cellColumn(i) + columnSpan(i) - 1) {
1238 bool left = (c + 1 < ncols()
1239 && cellInfo(cellIndex(r, c + 1)).left_line)
1240 || c + 1 == ncols();
1241 if (cellInfo(i).right_line && left)
1245 return 2 * nrows_right >= total;
1249 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1251 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1252 return cellInfo(cell).alignment;
1254 return column_info[cellColumn(cell)].alignment;
1259 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1261 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1262 return cellInfo(cell).valignment;
1263 return column_info[cellColumn(cell)].valignment;
1267 Length const Tabular::getPWidth(idx_type cell) const
1269 if (isMultiColumn(cell))
1270 return cellInfo(cell).p_width;
1271 return column_info[cellColumn(cell)].p_width;
1275 Length const Tabular::getMROffset(idx_type cell) const
1277 return cellInfo(cell).mroffset;
1281 int Tabular::textHOffset(idx_type cell) const
1283 // the LaTeX Way :-(
1284 int x = WIDTH_OF_LINE;
1286 int const w = cellWidth(cell) - cellInfo(cell).width;
1288 switch (getAlignment(cell)) {
1289 case LYX_ALIGN_CENTER:
1292 case LYX_ALIGN_RIGHT:
1295 case LYX_ALIGN_DECIMAL: {
1296 // we center when no decimal point
1297 if (cellInfo(cell).decimal_width == 0) {
1301 col_type const c = cellColumn(cell);
1302 int max_dhoffset = 0;
1303 for(row_type r = 0; r < row_info.size() ; ++r) {
1304 idx_type const i = cellIndex(r, c);
1305 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1306 && cellInfo(i).decimal_width != 0)
1307 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1309 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1312 // LYX_ALIGN_LEFT: nothing :-)
1320 int Tabular::textVOffset(idx_type cell) const
1322 int voffset = cellInfo(cell).voffset;
1323 if (isMultiRow(cell)) {
1324 row_type const row = cellRow(cell);
1325 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1331 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1334 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1336 return cell_info[row][c].cellno;
1340 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1342 col_type c = ncols() - 1;
1343 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1344 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1346 return cell_info[row][c].cellno;
1350 Tabular::row_type Tabular::cellRow(idx_type cell) const
1352 if (cell >= numberofcells)
1356 return rowofcell[cell];
1360 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1362 if (cell >= numberofcells)
1366 return columnofcell[cell];
1370 void Tabular::write(ostream & os) const
1374 << write_attribute("version", 3)
1375 << write_attribute("rows", nrows())
1376 << write_attribute("columns", ncols())
1378 // global longtable options
1380 << write_attribute("rotate", rotate)
1381 << write_attribute("booktabs", use_booktabs)
1382 << write_attribute("islongtable", is_long_tabular)
1383 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1384 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1385 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1386 << write_attribute("headTopDL", endhead.topDL)
1387 << write_attribute("headBottomDL", endhead.bottomDL)
1388 << write_attribute("footTopDL", endfoot.topDL)
1389 << write_attribute("footBottomDL", endfoot.bottomDL)
1390 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1391 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1392 << write_attribute("lastFootEmpty", endlastfoot.empty);
1393 // longtables cannot be aligned vertically
1394 if (!is_long_tabular)
1395 os << write_attribute("tabularvalignment", tabular_valignment);
1396 if (is_long_tabular)
1397 os << write_attribute("longtabularalignment",
1398 longtabular_alignment);
1400 for (col_type c = 0; c < ncols(); ++c) {
1402 << write_attribute("alignment", column_info[c].alignment);
1403 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1404 os << write_attribute("decimal_point", column_info[c].decimal_point);
1405 os << write_attribute("valignment", column_info[c].valignment)
1406 << write_attribute("width", column_info[c].p_width.asString())
1407 << write_attribute("special", column_info[c].align_special)
1410 for (row_type r = 0; r < nrows(); ++r) {
1411 static const string def("default");
1413 if (row_info[r].top_space_default)
1414 os << write_attribute("topspace", def);
1416 os << write_attribute("topspace", row_info[r].top_space);
1417 if (row_info[r].bottom_space_default)
1418 os << write_attribute("bottomspace", def);
1420 os << write_attribute("bottomspace", row_info[r].bottom_space);
1421 if (row_info[r].interline_space_default)
1422 os << write_attribute("interlinespace", def);
1424 os << write_attribute("interlinespace", row_info[r].interline_space);
1425 os << write_attribute("endhead", row_info[r].endhead)
1426 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1427 << write_attribute("endfoot", row_info[r].endfoot)
1428 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1429 << write_attribute("newpage", row_info[r].newpage)
1430 << write_attribute("caption", row_info[r].caption)
1432 for (col_type c = 0; c < ncols(); ++c) {
1434 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1435 << write_attribute("multirow", cell_info[r][c].multirow)
1436 << write_attribute("mroffset", cell_info[r][c].mroffset)
1437 << write_attribute("alignment", cell_info[r][c].alignment)
1438 << write_attribute("valignment", cell_info[r][c].valignment)
1439 << write_attribute("topline", cell_info[r][c].top_line)
1440 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1441 << write_attribute("leftline", cell_info[r][c].left_line)
1442 << write_attribute("rightline", cell_info[r][c].right_line)
1443 << write_attribute("rotate", cell_info[r][c].rotate)
1444 << write_attribute("usebox", cell_info[r][c].usebox)
1445 << write_attribute("width", cell_info[r][c].p_width)
1446 << write_attribute("special", cell_info[r][c].align_special)
1448 os << "\\begin_inset ";
1449 cell_info[r][c].inset->write(os);
1450 os << "\n\\end_inset\n"
1455 os << "</lyxtabular>\n";
1459 void Tabular::read(Lexer & lex)
1462 istream & is = lex.getStream();
1464 l_getline(is, line);
1465 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1466 LASSERT(false, /**/);
1471 if (!getTokenValue(line, "version", version))
1473 LASSERT(version >= 2, /**/);
1476 if (!getTokenValue(line, "rows", rows_arg))
1479 if (!getTokenValue(line, "columns", columns_arg))
1481 init(buffer_, rows_arg, columns_arg);
1482 l_getline(is, line);
1483 if (!prefixIs(line, "<features")) {
1484 lyxerr << "Wrong tabular format (expected <features ...> got"
1485 << line << ')' << endl;
1488 getTokenValue(line, "rotate", rotate);
1489 getTokenValue(line, "booktabs", use_booktabs);
1490 getTokenValue(line, "islongtable", is_long_tabular);
1491 getTokenValue(line, "tabularvalignment", tabular_valignment);
1492 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1493 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1494 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1495 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1496 getTokenValue(line, "headTopDL", endhead.topDL);
1497 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1498 getTokenValue(line, "footTopDL", endfoot.topDL);
1499 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1500 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1501 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1502 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1504 for (col_type c = 0; c < ncols(); ++c) {
1506 if (!prefixIs(line,"<column")) {
1507 lyxerr << "Wrong tabular format (expected <column ...> got"
1508 << line << ')' << endl;
1511 getTokenValue(line, "alignment", column_info[c].alignment);
1512 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1513 getTokenValue(line, "valignment", column_info[c].valignment);
1514 getTokenValue(line, "width", column_info[c].p_width);
1515 getTokenValue(line, "special", column_info[c].align_special);
1518 for (row_type i = 0; i < nrows(); ++i) {
1519 l_getline(is, line);
1520 if (!prefixIs(line, "<row")) {
1521 lyxerr << "Wrong tabular format (expected <row ...> got"
1522 << line << ')' << endl;
1525 getTokenValue(line, "topspace", row_info[i].top_space,
1526 row_info[i].top_space_default);
1527 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1528 row_info[i].bottom_space_default);
1529 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1530 row_info[i].interline_space_default);
1531 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1532 getTokenValue(line, "endhead", row_info[i].endhead);
1533 getTokenValue(line, "endfoot", row_info[i].endfoot);
1534 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1535 getTokenValue(line, "newpage", row_info[i].newpage);
1536 getTokenValue(line, "caption", row_info[i].caption);
1537 for (col_type j = 0; j < ncols(); ++j) {
1538 l_getline(is, line);
1539 if (!prefixIs(line, "<cell")) {
1540 lyxerr << "Wrong tabular format (expected <cell ...> got"
1541 << line << ')' << endl;
1544 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1545 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1546 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1547 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1548 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1549 getTokenValue(line, "topline", cell_info[i][j].top_line);
1550 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1551 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1552 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1553 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1554 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1555 getTokenValue(line, "width", cell_info[i][j].p_width);
1557 getTokenValue(line, "special", cell_info[i][j].align_special);
1558 l_getline(is, line);
1559 if (prefixIs(line, "\\begin_inset")) {
1560 cell_info[i][j].inset->setBuffer(*buffer_);
1561 cell_info[i][j].inset->read(lex);
1562 l_getline(is, line);
1564 if (!prefixIs(line, "</cell>")) {
1565 lyxerr << "Wrong tabular format (expected </cell> got"
1566 << line << ')' << endl;
1570 l_getline(is, line);
1571 if (!prefixIs(line, "</row>")) {
1572 lyxerr << "Wrong tabular format (expected </row> got"
1573 << line << ')' << endl;
1577 while (!prefixIs(line, "</lyxtabular>")) {
1578 l_getline(is, line);
1584 bool Tabular::isMultiColumn(idx_type cell) const
1586 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1587 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1591 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1593 return cell_info[cellRow(cell)][cellColumn(cell)];
1597 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1599 idx_type const col = cellColumn(cell);
1600 idx_type const row = cellRow(cell);
1601 for (idx_type i = 0; i < number; ++i)
1602 unsetMultiRow(cellIndex(row, col + i));
1604 // unsetting of multirow may have invalidated cell index
1605 cell = cellIndex(row, col);
1606 CellData & cs = cellInfo(cell);
1607 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1608 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1609 cs.alignment = column_info[col].alignment;
1611 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1613 for (idx_type i = 1; i < number; ++i) {
1614 CellData & cs1 = cellInfo(cell + i);
1615 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1616 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1624 bool Tabular::isMultiRow(idx_type cell) const
1626 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1627 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1631 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1633 idx_type const col = cellColumn(cell);
1634 idx_type const row = cellRow(cell);
1635 for (idx_type i = 0; i < number; ++i)
1636 unsetMultiColumn(cellIndex(row + i, col));
1638 // unsetting of multirow may have invalidated cell index
1639 cell = cellIndex(row, col);
1640 CellData & cs = cellInfo(cell);
1641 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1642 cs.valignment = LYX_VALIGN_MIDDLE;
1643 // the horizontal alignment of multirow cells can only
1644 // be changed for the whole table row,
1645 // support changing this only for the multirow cell can be done via
1647 // this feature would be a fileformat change
1648 // until LyX supports this, use the deault alignment of multirow
1650 cs.alignment = LYX_ALIGN_LEFT;
1652 // set the bottom row of the last selected cell
1653 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1655 for (idx_type i = 1; i < number; ++i) {
1656 CellData & cs1 = cell_info[row + i][col];
1657 cs1.multirow = CELL_PART_OF_MULTIROW;
1658 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1666 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1668 row_type const row = cellRow(cell);
1669 col_type const col = cellColumn(cell);
1671 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1678 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1680 col_type const column = cellColumn(cell);
1681 col_type row = cellRow(cell) + 1;
1682 while (row < nrows() && isPartOfMultiRow(row, column))
1685 return row - cellRow(cell);
1689 void Tabular::unsetMultiColumn(idx_type cell)
1691 if (!isMultiColumn(cell))
1694 row_type const row = cellRow(cell);
1695 col_type const col = cellColumn(cell);
1696 row_type const span = columnSpan(cell);
1697 for (col_type c = 0; c < span; ++c) {
1698 // in the table dialog the lines are set in every case
1699 // when unsetting a multicolumn this leads to an additional right
1700 // line for every cell that was part of the former multicolumn cell,
1701 // except if the cell is in the last column
1702 // therefore remove this line
1703 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1704 && (col + c) < (col + span - 1))
1705 cell_info[row][col + c].right_line = false;
1706 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1712 void Tabular::unsetMultiRow(idx_type cell)
1714 if (!isMultiRow(cell))
1717 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1718 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1719 row_type const row = cellRow(cell);
1720 col_type const col = cellColumn(cell);
1721 row_type const span = rowSpan(cell);
1722 for (row_type r = 0; r < span; ++r)
1723 cell_info[row + r][col].multirow = CELL_NORMAL;
1728 void Tabular::setRotateCell(idx_type cell, bool flag)
1730 cellInfo(cell).rotate = flag;
1734 bool Tabular::getRotateCell(idx_type cell) const
1736 return cellInfo(cell).rotate;
1740 bool Tabular::needRotating() const
1744 for (row_type r = 0; r < nrows(); ++r)
1745 for (col_type c = 0; c < ncols(); ++c)
1746 if (cell_info[r][c].rotate)
1752 bool Tabular::isLastCell(idx_type cell) const
1754 if (cell + 1 < numberofcells)
1760 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1762 if (cellRow(cell) == 0)
1765 col_type const col = cellColumn(cell);
1766 row_type r = cellRow(cell) - 1;
1767 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1770 return cell_info[r][col].cellno;
1774 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1776 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1777 if (nextrow < nrows())
1778 return cell_info[nextrow][cellColumn(cell)].cellno;
1783 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1785 LASSERT(column != npos && column < ncols()
1786 && row != npos && row < nrows(), /**/);
1787 return cell_info[row][column].cellno;
1791 void Tabular::setUsebox(idx_type cell, BoxType type)
1793 cellInfo(cell).usebox = type;
1797 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1798 // adds line breaks, see bug 4886.
1799 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1801 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1802 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1804 if (cellInfo(cell).usebox > 1)
1805 return cellInfo(cell).usebox;
1806 return useParbox(cell);
1811 // This are functions used for the longtable support
1813 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1819 row_info[row].endfirsthead = flag;
1823 row_info[row].endhead = flag;
1828 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1831 hd.set = haveLTHead();
1832 return row_info[row].endhead;
1836 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1839 hd.set = haveLTFirstHead();
1840 return row_info[row].endfirsthead;
1844 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1850 row_info[row].endlastfoot = flag;
1854 row_info[row].endfoot = flag;
1859 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1862 fd.set = haveLTFoot();
1863 return row_info[row].endfoot;
1867 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1870 fd.set = haveLTLastFoot();
1871 return row_info[row].endlastfoot;
1875 void Tabular::setLTNewPage(row_type row, bool what)
1877 row_info[row].newpage = what;
1881 bool Tabular::getLTNewPage(row_type row) const
1883 return row_info[row].newpage;
1887 bool Tabular::haveLTHead() const
1889 if (!is_long_tabular)
1891 for (row_type i = 0; i < nrows(); ++i)
1892 if (row_info[i].endhead)
1898 bool Tabular::haveLTFirstHead() const
1900 if (!is_long_tabular || endfirsthead.empty)
1902 for (row_type r = 0; r < nrows(); ++r)
1903 if (row_info[r].endfirsthead)
1909 bool Tabular::haveLTFoot() const
1911 if (!is_long_tabular)
1913 for (row_type r = 0; r < nrows(); ++r)
1914 if (row_info[r].endfoot)
1920 bool Tabular::haveLTLastFoot() const
1922 if (!is_long_tabular || endlastfoot.empty)
1924 for (row_type r = 0; r < nrows(); ++r)
1925 if (row_info[r].endlastfoot)
1931 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1933 idx_type i = getFirstCellInRow(row);
1935 setMultiColumn(i, numberOfCellsInRow(row));
1936 setTopLine(i, false);
1937 setBottomLine(i, false);
1938 setLeftLine(i, false);
1939 setRightLine(i, false);
1941 unsetMultiColumn(i);
1942 // When unsetting a caption row, also all existing
1943 // captions in this row must be dissolved.
1945 row_info[row].caption = what;
1950 bool Tabular::ltCaption(row_type row) const
1952 return row_info[row].caption;
1956 bool Tabular::haveLTCaption() const
1958 if (!is_long_tabular)
1960 for (row_type r = 0; r < nrows(); ++r)
1961 if (row_info[r].caption)
1967 // end longtable support functions
1969 void Tabular::setRowAscent(row_type row, int height)
1971 if (row >= nrows() || row_info[row].ascent == height)
1973 row_info[row].ascent = height;
1977 void Tabular::setRowDescent(row_type row, int height)
1979 if (row >= nrows() || row_info[row].descent == height)
1981 row_info[row].descent = height;
1985 int Tabular::rowAscent(row_type row) const
1987 LASSERT(row < nrows(), /**/);
1988 return row_info[row].ascent;
1992 int Tabular::rowDescent(row_type row) const
1994 LASSERT(row < nrows(), /**/);
1995 return row_info[row].descent;
1999 int Tabular::height() const
2002 for (row_type row = 0; row < nrows(); ++row)
2003 height += rowAscent(row) + rowDescent(row) +
2009 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2011 LASSERT(row < nrows(), /**/);
2012 LASSERT(column < ncols(), /**/);
2013 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2017 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2019 LASSERT(row < nrows(), /**/);
2020 LASSERT(column < ncols(), /**/);
2021 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2025 int Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2027 // we only output complete row lines and the 1st row here, the rest
2028 // is done in Tabular::TeXBottomHLine(...)
2030 // get for each column the topline (if any)
2031 vector<bool> topline;
2033 for (col_type c = 0; c < ncols(); ++c) {
2034 topline.push_back(topLine(cellIndex(row, c)));
2035 // If cell is part of a multirow and not the first cell of the
2036 // multirow, no line must be drawn.
2038 if (isMultiRow(cellIndex(row, c))
2039 && isMultiRow(cellIndex(row - 1, c)))
2045 // do nothing if empty first row, or incomplete row line after
2046 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2049 // only output complete row lines and the 1st row's clines
2050 if (nset == ncols()) {
2052 os << (row == 0 ? "\\toprule " : "\\midrule ");
2056 } else if (row == 0) {
2057 for (col_type c = 0; c < ncols(); ++c) {
2059 col_type offset = 0;
2060 for (col_type j = 0 ; j < c; ++j)
2061 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2064 //babel makes the "-" character an active one, so we have to suppress this here
2065 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2066 if (lang == "slovak" || lang == "czech")
2067 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2068 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2070 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2072 col_type cstart = c;
2073 for ( ; c < ncols() && topline[c]; ++c) {}
2075 for (col_type j = cstart ; j < c ; ++j)
2076 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2079 os << c + offset << "} ";
2088 int Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2090 // we output bottomlines of row r and the toplines of row r+1
2091 // if the latter do not span the whole tabular
2093 // get the bottomlines of row r, and toplines in next row
2094 bool lastrow = row == nrows() - 1;
2095 vector<bool> bottomline, topline;
2096 bool nextrowset = true;
2097 for (col_type c = 0; c < ncols(); ++c) {
2098 bottomline.push_back(bottomLine(cellIndex(row, c)));
2099 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2100 // If cell is part of a multirow and not the last or first cell of the
2101 // multirow, no line must be drawn.
2103 if (isMultiRow(cellIndex(row, c))
2104 && isMultiRow(cellIndex(row + 1, c))) {
2105 bottomline[c] = false;
2108 nextrowset &= topline[c];
2111 // combine this row's bottom lines and next row's toplines if necessary
2113 for (col_type c = 0; c < ncols(); ++c) {
2115 bottomline[c] = bottomline[c] || topline[c];
2120 // do nothing if empty, OR incomplete row line with a topline in next row
2121 if (nset == 0 || (nextrowset && nset != ncols()))
2124 if (nset == ncols()) {
2126 os << (lastrow ? "\\bottomrule" : "\\midrule");
2130 for (col_type c = 0; c < ncols(); ++c) {
2131 if (bottomline[c]) {
2132 col_type offset = 0;
2133 for (col_type j = 0 ; j < c; ++j)
2134 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2137 //babel makes the "-" character an active one, so we have to suppress this here
2138 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2139 if (lang == "slovak" || lang == "czech")
2140 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2141 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2143 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2145 col_type cstart = c;
2146 for ( ; c < ncols() && bottomline[c]; ++c) {}
2148 for (col_type j = cstart ; j < c ; ++j)
2149 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2152 os << c + offset << "} ";
2161 int Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2162 bool & ismulticol, bool & ismultirow) const
2165 row_type const r = cellRow(cell);
2166 if (is_long_tabular && row_info[r].caption)
2169 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2170 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2171 // figure out how to set the lines
2172 // we always set double lines to the right of the cell
2173 col_type const c = cellColumn(cell);
2174 col_type const nextcol = c + columnSpan(cell);
2175 bool colright = columnRightLine(c);
2176 bool colleft = columnLeftLine(c);
2177 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2178 bool nextcellleft = nextcol < ncols()
2179 && leftLine(cellIndex(r, nextcol));
2180 bool coldouble = colright && nextcolleft;
2181 bool celldouble = rightLine(cell) && nextcellleft;
2183 ismulticol = isMultiColumn(cell)
2184 || (c == 0 && colleft != leftLine(cell))
2185 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2186 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2187 || (coldouble != celldouble);
2189 // we center in multicol when no decimal point
2190 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2191 && (cellInfo(cell).decimal_width == 0));
2193 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2194 int latexcolspan = columnSpan(cell);
2195 for(col_type col = c; col < c + columnSpan(cell); ++col)
2196 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2200 os << "\\multicolumn{" << latexcolspan << "}{";
2201 if (c ==0 && leftLine(cell))
2203 if (!cellInfo(cell).align_special.empty()) {
2204 os << cellInfo(cell).align_special;
2206 if (!getPWidth(cell).zero()) {
2208 case LYX_ALIGN_LEFT:
2209 os << ">{\\raggedright}";
2211 case LYX_ALIGN_RIGHT:
2212 os << ">{\\raggedleft}";
2214 case LYX_ALIGN_CENTER:
2215 os << ">{\\centering}";
2221 case LYX_VALIGN_TOP:
2224 case LYX_VALIGN_MIDDLE:
2227 case LYX_VALIGN_BOTTOM:
2232 << from_ascii(getPWidth(cell).asLatexString())
2236 case LYX_ALIGN_LEFT:
2239 case LYX_ALIGN_RIGHT:
2246 } // end if else !getPWidth
2247 } // end if else !cellinfo_of_cell
2248 if (rightLine(cell) || nextcellleft)
2251 // add extra vertical line if we want a double one
2254 } // end if ismulticol
2256 // we only need code for the first multirow cell
2257 ismultirow = isMultiRow(cell);
2259 os << "\\multirow{" << rowSpan(cell) << "}{";
2260 if (!getPWidth(cell).zero())
2261 os << from_ascii(getPWidth(cell).asLatexString());
2263 // we need to set a default value
2266 if (!getMROffset(cell).zero())
2267 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2269 } // end if ismultirow
2271 if (getRotateCell(cell)) {
2272 os << "\\begin{sideways}\n";
2275 if (getUsebox(cell) == BOX_PARBOX) {
2278 case LYX_VALIGN_TOP:
2281 case LYX_VALIGN_MIDDLE:
2284 case LYX_VALIGN_BOTTOM:
2288 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2290 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2291 os << "\\begin{minipage}[";
2293 case LYX_VALIGN_TOP:
2296 case LYX_VALIGN_MIDDLE:
2299 case LYX_VALIGN_BOTTOM:
2303 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2311 int Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2312 bool ismulticol, bool ismultirow) const
2315 row_type const r = cellRow(cell);
2316 if (is_long_tabular && row_info[r].caption)
2320 if (getUsebox(cell) == BOX_PARBOX)
2322 else if (getUsebox(cell) == BOX_MINIPAGE) {
2323 os << "%\n\\end{minipage}";
2326 if (getRotateCell(cell)) {
2327 os << "%\n\\end{sideways}";
2339 int Tabular::TeXLongtableHeaderFooter(otexstream & os,
2340 OutputParams const & runparams) const
2342 if (!is_long_tabular)
2347 // the caption must be output before the headers
2348 if (haveLTCaption()) {
2349 for (row_type r = 0; r < nrows(); ++r) {
2350 if (row_info[r].caption) {
2351 ret += TeXRow(os, r, runparams);
2355 // output first header info
2356 // first header must be output before the header, otherwise the
2357 // correct caption placement becomes really weird
2358 if (haveLTFirstHead()) {
2359 if (endfirsthead.topDL) {
2363 for (row_type r = 0; r < nrows(); ++r) {
2364 if (row_info[r].endfirsthead) {
2365 ret += TeXRow(os, r, runparams);
2368 if (endfirsthead.bottomDL) {
2372 os << "\\endfirsthead\n";
2375 // output header info
2377 if (endfirsthead.empty && !haveLTFirstHead()) {
2378 os << "\\endfirsthead\n";
2381 if (endhead.topDL) {
2385 for (row_type r = 0; r < nrows(); ++r) {
2386 if (row_info[r].endhead) {
2387 ret += TeXRow(os, r, runparams);
2390 if (endhead.bottomDL) {
2394 os << "\\endhead\n";
2397 // output footer info
2399 if (endfoot.topDL) {
2403 for (row_type r = 0; r < nrows(); ++r) {
2404 if (row_info[r].endfoot) {
2405 ret += TeXRow(os, r, runparams);
2408 if (endfoot.bottomDL) {
2412 os << "\\endfoot\n";
2414 if (endlastfoot.empty && !haveLTLastFoot()) {
2415 os << "\\endlastfoot\n";
2419 // output lastfooter info
2420 if (haveLTLastFoot()) {
2421 if (endlastfoot.topDL) {
2425 for (row_type r = 0; r < nrows(); ++r) {
2426 if (row_info[r].endlastfoot) {
2427 ret += TeXRow(os, r, runparams);
2430 if (endlastfoot.bottomDL) {
2434 os << "\\endlastfoot\n";
2441 bool Tabular::isValidRow(row_type row) const
2443 if (!is_long_tabular)
2445 return !row_info[row].endhead && !row_info[row].endfirsthead
2446 && !row_info[row].endfoot && !row_info[row].endlastfoot
2447 && !row_info[row].caption;
2451 int Tabular::TeXRow(otexstream & os, row_type row,
2452 OutputParams const & runparams) const
2454 idx_type cell = cellIndex(row, 0);
2455 shared_ptr<InsetTableCell> inset = cellInset(cell);
2456 Paragraph const & par = inset->paragraphs().front();
2457 string const lang = par.getParLanguage(buffer().params())->lang();
2459 //output the top line
2460 int ret = TeXTopHLine(os, row, lang);
2462 if (row_info[row].top_space_default) {
2464 os << "\\addlinespace\n";
2466 os << "\\noalign{\\vskip\\doublerulesep}\n";
2468 } else if(!row_info[row].top_space.zero()) {
2470 os << "\\addlinespace["
2471 << from_ascii(row_info[row].top_space.asLatexString())
2474 os << "\\noalign{\\vskip"
2475 << from_ascii(row_info[row].top_space.asLatexString())
2480 bool ismulticol = false;
2481 bool ismultirow = false;
2482 for (col_type c = 0; c < ncols(); ++c) {
2483 if (isPartOfMultiColumn(row, c))
2486 if (isPartOfMultiRow(row, c) &&
2487 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2492 cell = cellIndex(row, c);
2493 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2494 shared_ptr<InsetTableCell> inset = cellInset(cell);
2496 Paragraph const & par = inset->paragraphs().front();
2497 bool rtl = par.isRTL(buffer().params())
2499 && getPWidth(cell).zero();
2503 par.getParLanguage(buffer().params())->lang();
2504 if (lang == "farsi")
2506 else if (lang == "arabic_arabi")
2508 // currently, remaning RTL languages are
2509 // arabic_arabtex and hebrew
2513 // pass to the OutputParams that we are in a cell and
2514 // which alignment we have set.
2515 // InsetNewline needs this context information.
2516 OutputParams newrp(runparams);
2517 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2518 ? OutputParams::PLAIN
2519 : OutputParams::ALIGNED;
2521 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2522 && cellInfo(cell).decimal_width != 0) {
2523 // copy cell and split in 2
2524 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2525 head.getText(0)->setMacrocontextPosition(
2526 cellInset(cell)->getText(0)->macrocontextPosition());
2527 head.setBuffer(buffer());
2528 bool hassep = false;
2529 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2530 tail.getText(0)->setMacrocontextPosition(
2531 head.getText(0)->macrocontextPosition());
2532 tail.setBuffer(head.buffer());
2533 head.latex(os, newrp);
2535 ret += tail.latex(os, newrp);
2536 } else if (!isPartOfMultiRow(row, c))
2537 ret += inset->latex(os, newrp);
2539 runparams.encoding = newrp.encoding;
2543 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2544 if (cell != getLastCellInRow(row)) { // not last cell in row
2548 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2549 // if no first header and no empty first header is used,
2550 // the caption needs to be terminated by \endfirsthead
2552 os << "\\endfirsthead";
2554 os << "\\tabularnewline";
2555 if (row_info[row].bottom_space_default) {
2557 os << "\\addlinespace";
2559 os << "[\\doublerulesep]";
2560 } else if (!row_info[row].bottom_space.zero()) {
2562 os << "\\addlinespace";
2564 << from_ascii(row_info[row].bottom_space.asLatexString())
2570 //output the bottom line
2571 ret += TeXBottomHLine(os, row, lang);
2573 if (row_info[row].interline_space_default) {
2575 os << "\\addlinespace\n";
2577 os << "\\noalign{\\vskip\\doublerulesep}\n";
2579 } else if (!row_info[row].interline_space.zero()) {
2581 os << "\\addlinespace["
2582 << from_ascii(row_info[row].interline_space.asLatexString())
2585 os << "\\noalign{\\vskip"
2586 << from_ascii(row_info[row].interline_space.asLatexString())
2594 int Tabular::latex(otexstream & os, OutputParams const & runparams) const
2598 //+---------------------------------------------------------------------
2599 //+ first the opening preamble +
2600 //+---------------------------------------------------------------------
2603 os << "\\begin{sideways}\n";
2606 if (is_long_tabular) {
2607 os << "\\begin{longtable}";
2608 switch (longtabular_alignment) {
2609 case LYX_LONGTABULAR_ALIGN_LEFT:
2612 case LYX_LONGTABULAR_ALIGN_CENTER:
2614 case LYX_LONGTABULAR_ALIGN_RIGHT:
2619 os << "\\begin{tabular}";
2620 switch (tabular_valignment) {
2621 case LYX_VALIGN_TOP:
2624 case LYX_VALIGN_MIDDLE:
2626 case LYX_VALIGN_BOTTOM:
2634 for (col_type c = 0; c < ncols(); ++c) {
2635 if (columnLeftLine(c))
2637 if (!column_info[c].align_special.empty()) {
2638 os << column_info[c].align_special;
2640 if (!column_info[c].p_width.zero()) {
2641 switch (column_info[c].alignment) {
2642 case LYX_ALIGN_LEFT:
2643 os << ">{\\raggedright}";
2645 case LYX_ALIGN_RIGHT:
2646 os << ">{\\raggedleft}";
2648 case LYX_ALIGN_CENTER:
2649 os << ">{\\centering}";
2651 case LYX_ALIGN_NONE:
2652 case LYX_ALIGN_BLOCK:
2653 case LYX_ALIGN_LAYOUT:
2654 case LYX_ALIGN_SPECIAL:
2655 case LYX_ALIGN_DECIMAL:
2659 switch (column_info[c].valignment) {
2660 case LYX_VALIGN_TOP:
2663 case LYX_VALIGN_MIDDLE:
2666 case LYX_VALIGN_BOTTOM:
2671 << from_ascii(column_info[c].p_width.asLatexString())
2674 switch (column_info[c].alignment) {
2675 case LYX_ALIGN_LEFT:
2678 case LYX_ALIGN_RIGHT:
2681 case LYX_ALIGN_DECIMAL:
2682 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2688 } // end if else !column_info[i].p_width
2689 } // end if else !column_info[i].align_special
2690 if (columnRightLine(c))
2696 ret += TeXLongtableHeaderFooter(os, runparams);
2698 //+---------------------------------------------------------------------
2699 //+ the single row and columns (cells) +
2700 //+---------------------------------------------------------------------
2702 for (row_type r = 0; r < nrows(); ++r) {
2703 if (isValidRow(r)) {
2704 ret += TeXRow(os, r, runparams);
2705 if (is_long_tabular && row_info[r].newpage) {
2706 os << "\\newpage\n";
2712 //+---------------------------------------------------------------------
2713 //+ the closing of the tabular +
2714 //+---------------------------------------------------------------------
2716 if (is_long_tabular)
2717 os << "\\end{longtable}";
2719 os << "\\end{tabular}";
2723 os << breakln << "\\end{sideways}";
2724 ret += os.countLines();
2731 int Tabular::docbookRow(odocstream & os, row_type row,
2732 OutputParams const & runparams) const
2735 idx_type cell = getFirstCellInRow(row);
2738 for (col_type c = 0; c < ncols(); ++c) {
2739 if (isPartOfMultiColumn(row, c))
2742 os << "<entry align=\"";
2743 switch (getAlignment(cell)) {
2744 case LYX_ALIGN_LEFT:
2747 case LYX_ALIGN_RIGHT:
2755 os << "\" valign=\"";
2756 switch (getVAlignment(cell)) {
2757 case LYX_VALIGN_TOP:
2760 case LYX_VALIGN_BOTTOM:
2763 case LYX_VALIGN_MIDDLE:
2768 if (isMultiColumn(cell)) {
2769 os << " namest=\"col" << c << "\" ";
2770 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2774 ret += cellInset(cell)->docbook(os, runparams);
2783 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2787 //+---------------------------------------------------------------------
2788 //+ first the opening preamble +
2789 //+---------------------------------------------------------------------
2791 os << "<tgroup cols=\"" << ncols()
2792 << "\" colsep=\"1\" rowsep=\"1\">\n";
2794 for (col_type c = 0; c < ncols(); ++c) {
2795 os << "<colspec colname=\"col" << c << "\" align=\"";
2796 switch (column_info[c].alignment) {
2797 case LYX_ALIGN_LEFT:
2800 case LYX_ALIGN_RIGHT:
2808 if (runparams.flavor == OutputParams::XML)
2814 //+---------------------------------------------------------------------
2815 //+ Long Tabular case +
2816 //+---------------------------------------------------------------------
2818 // output caption info
2819 if (haveLTCaption()) {
2820 os << "<caption>\n";
2822 for (row_type r = 0; r < nrows(); ++r) {
2823 if (row_info[r].caption) {
2824 ret += docbookRow(os, r, runparams);
2827 os << "</caption>\n";
2830 // output header info
2831 if (haveLTHead() || haveLTFirstHead()) {
2834 for (row_type r = 0; r < nrows(); ++r) {
2835 if (row_info[r].endhead || row_info[r].endfirsthead) {
2836 ret += docbookRow(os, r, runparams);
2842 // output footer info
2843 if (haveLTFoot() || haveLTLastFoot()) {
2846 for (row_type r = 0; r < nrows(); ++r) {
2847 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2848 ret += docbookRow(os, r, runparams);
2855 //+---------------------------------------------------------------------
2856 //+ the single row and columns (cells) +
2857 //+---------------------------------------------------------------------
2861 for (row_type r = 0; r < nrows(); ++r) {
2862 if (isValidRow(r)) {
2863 ret += docbookRow(os, r, runparams);
2868 //+---------------------------------------------------------------------
2869 //+ the closing of the tabular +
2870 //+---------------------------------------------------------------------
2879 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2880 OutputParams const & runparams, bool header) const
2883 string const celltag = header ? "th" : "td";
2884 idx_type cell = getFirstCellInRow(row);
2886 xs << html::StartTag("tr");
2887 for (col_type c = 0; c < ncols(); ++c) {
2888 if (isPartOfMultiColumn(row, c))
2893 switch (getAlignment(cell)) {
2894 case LYX_ALIGN_LEFT:
2897 case LYX_ALIGN_RIGHT:
2905 attr << " valign='";
2906 switch (getVAlignment(cell)) {
2907 case LYX_VALIGN_TOP:
2910 case LYX_VALIGN_BOTTOM:
2913 case LYX_VALIGN_MIDDLE:
2918 if (isMultiColumn(cell))
2919 attr << " colspan='" << columnSpan(cell) << "'";
2921 xs << html::StartTag(celltag, attr.str());
2922 ret += cellInset(cell)->xhtml(xs, runparams);
2923 xs << html::EndTag(celltag);
2926 xs << html::EndTag("tr");
2931 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2935 if (is_long_tabular) {
2936 // we'll wrap it in a div, so as to deal with alignment
2938 switch (longtabular_alignment) {
2939 case LYX_LONGTABULAR_ALIGN_LEFT:
2942 case LYX_LONGTABULAR_ALIGN_CENTER:
2945 case LYX_LONGTABULAR_ALIGN_RIGHT:
2949 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
2950 if (haveLTCaption()) {
2951 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
2952 for (row_type r = 0; r < nrows(); ++r)
2953 if (row_info[r].caption)
2954 ret += xhtmlRow(xs, r, runparams);
2955 xs << html::EndTag("div");
2959 xs << html::StartTag("table");
2961 // output header info
2962 bool const havefirsthead = haveLTFirstHead();
2963 // if we have a first head, then we are going to ignore the
2964 // headers for the additional pages, since there aren't any
2965 // in XHTML. this test accomplishes that.
2966 bool const havehead = !havefirsthead && haveLTHead();
2967 if (havehead || havefirsthead) {
2968 xs << html::StartTag("thead");
2969 for (row_type r = 0; r < nrows(); ++r) {
2970 if ((havefirsthead && row_info[r].endfirsthead)
2971 || (havehead && row_info[r].endhead)) {
2972 ret += xhtmlRow(xs, r, runparams, true);
2975 xs << html::EndTag("thead");
2977 // output footer info
2978 bool const havelastfoot = haveLTLastFoot();
2980 bool const havefoot = !havelastfoot && haveLTFoot();
2981 if (havefoot || havelastfoot) {
2982 xs << html::StartTag("tfoot");
2983 for (row_type r = 0; r < nrows(); ++r) {
2984 if ((havelastfoot && row_info[r].endlastfoot)
2985 || (havefoot && row_info[r].endfoot)) {
2986 ret += xhtmlRow(xs, r, runparams);
2989 xs << html::EndTag("tfoot");
2992 xs << html::StartTag("tbody");
2993 for (row_type r = 0; r < nrows(); ++r) {
2994 if (isValidRow(r)) {
2995 ret += xhtmlRow(xs, r, runparams);
2998 xs << html::EndTag("tbody")
2999 << html::EndTag("table");
3000 if (is_long_tabular)
3001 xs << html::EndTag("div");
3006 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3007 vector<unsigned int> const & clen) const
3009 idx_type const fcell = getFirstCellInRow(row);
3010 idx_type const n = numberOfCellsInRow(row) + fcell;
3013 for (idx_type i = fcell; i < n; ++i) {
3023 for (idx_type i = fcell; i < n; ++i) {
3034 col_type column = cellColumn(i);
3035 int len = clen[column];
3036 while (column < ncols() - 1
3037 && isPartOfMultiColumn(row, ++column))
3038 len += clen[column] + 4;
3039 os << docstring(len, ch);
3054 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3055 vector<unsigned int> const & clen) const
3057 idx_type const fcell = getFirstCellInRow(row);
3058 idx_type const n = numberOfCellsInRow(row) + fcell;
3061 for (idx_type i = fcell; i < n; ++i) {
3062 if (bottomLine(i)) {
3071 for (idx_type i = fcell; i < n; ++i) {
3072 if (bottomLine(i)) {
3082 col_type column = cellColumn(i);
3083 int len = clen[column];
3084 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3085 len += clen[column] + 4;
3086 os << docstring(len, ch);
3087 if (bottomLine(i)) {
3101 void Tabular::plaintextPrintCell(odocstream & os,
3102 OutputParams const & runparams,
3103 idx_type cell, row_type row, col_type column,
3104 vector<unsigned int> const & clen,
3105 bool onlydata) const
3107 odocstringstream sstr;
3108 cellInset(cell)->plaintext(sstr, runparams);
3120 unsigned int len1 = sstr.str().length();
3121 unsigned int len2 = clen[column];
3122 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3123 len2 += clen[column] + 4;
3126 switch (getAlignment(cell)) {
3128 case LYX_ALIGN_LEFT:
3131 case LYX_ALIGN_RIGHT:
3135 case LYX_ALIGN_CENTER:
3141 os << docstring(len1, ' ') << sstr.str()
3142 << docstring(len2, ' ');
3144 if (rightLine(cell))
3151 void Tabular::plaintext(odocstream & os,
3152 OutputParams const & runparams, int const depth,
3153 bool onlydata, char_type delim) const
3155 // first calculate the width of the single columns
3156 vector<unsigned int> clen(ncols());
3159 // first all non multicolumn cells!
3160 for (col_type c = 0; c < ncols(); ++c) {
3162 for (row_type r = 0; r < nrows(); ++r) {
3163 idx_type cell = cellIndex(r, c);
3164 if (isMultiColumn(cell))
3166 odocstringstream sstr;
3167 cellInset(cell)->plaintext(sstr, runparams);
3168 if (clen[c] < sstr.str().length())
3169 clen[c] = sstr.str().length();
3172 // then all multicolumn cells!
3173 for (col_type c = 0; c < ncols(); ++c) {
3174 for (row_type r = 0; r < nrows(); ++r) {
3175 idx_type cell = cellIndex(r, c);
3176 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3178 odocstringstream sstr;
3179 cellInset(cell)->plaintext(sstr, runparams);
3180 int len = int(sstr.str().length());
3181 idx_type const n = columnSpan(cell);
3182 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3184 if (len > int(clen[c + n - 1]))
3185 clen[c + n - 1] = len;
3190 for (row_type r = 0; r < nrows(); ++r) {
3191 if (!onlydata && plaintextTopHLine(os, r, clen))
3192 os << docstring(depth * 2, ' ');
3193 for (col_type c = 0; c < ncols(); ++c) {
3194 if (isPartOfMultiColumn(r, c))
3196 if (onlydata && c > 0)
3197 // we don't use operator<< for single UCS4 character.
3198 // see explanation in docstream.h
3200 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3205 os << docstring(depth * 2, ' ');
3206 if (plaintextBottomHLine(os, r, clen))
3207 os << docstring(depth * 2, ' ');
3213 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3215 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3219 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3220 col_type column) const
3222 return cell_info[row][column].inset;
3226 void Tabular::setCellInset(row_type row, col_type column,
3227 shared_ptr<InsetTableCell> ins) const
3229 CellData & cd = cell_info[row][column];
3234 void Tabular::validate(LaTeXFeatures & features) const
3236 features.require("NeedTabularnewline");
3238 features.require("booktabs");
3239 if (is_long_tabular)
3240 features.require("longtable");
3242 features.require("rotating");
3243 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3244 if (isMultiRow(cell))
3245 features.require("multirow");
3246 if (getVAlignment(cell) != LYX_VALIGN_TOP
3247 || !getPWidth(cell).zero())
3248 features.require("array");
3249 cellInset(cell)->validate(features);
3254 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3256 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3257 ParagraphList::const_iterator cit = parlist.begin();
3258 ParagraphList::const_iterator end = parlist.end();
3260 for (; cit != end; ++cit)
3261 for (int i = 0; i < cit->size(); ++i)
3262 if (cit->isNewline(i))
3269 /////////////////////////////////////////////////////////////////////
3273 /////////////////////////////////////////////////////////////////////
3275 InsetTableCell::InsetTableCell(Buffer * buf)
3276 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3277 contentAlign(LYX_ALIGN_CENTER)
3281 bool InsetTableCell::forcePlainLayout(idx_type) const
3283 return !isFixedWidth;
3287 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3289 return isFixedWidth;
3293 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3294 FuncStatus & status) const
3296 bool enabled = true;
3297 switch (cmd.action()) {
3299 enabled = !forcePlainLayout();
3301 case LFUN_LAYOUT_PARAGRAPH:
3302 enabled = allowParagraphCustomization();
3305 case LFUN_MATH_DISPLAY:
3306 if (!hasFixedWidth()) {
3311 return InsetText::getStatus(cur, cmd, status);
3313 status.setEnabled(enabled);
3317 docstring InsetTableCell::asString(bool intoInsets)
3320 if (paragraphs().empty())
3322 ParagraphList::const_iterator it = paragraphs().begin();
3323 ParagraphList::const_iterator en = paragraphs().end();
3325 for (; it != en; ++it) {
3330 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3336 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3339 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3340 return InsetText::xhtml(xs, rp);
3345 /////////////////////////////////////////////////////////////////////
3349 /////////////////////////////////////////////////////////////////////
3351 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3353 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3354 rowselect_(false), colselect_(false)
3359 InsetTabular::InsetTabular(InsetTabular const & tab)
3360 : Inset(tab), tabular(tab.tabular), scx_(0)
3365 InsetTabular::~InsetTabular()
3367 hideDialogs("tabular", this);
3371 void InsetTabular::setBuffer(Buffer & buf)
3373 tabular.setBuffer(buf);
3374 Inset::setBuffer(buf);
3378 bool InsetTabular::insetAllowed(InsetCode code) const
3383 case MATHMACRO_CODE:
3388 return tabular.is_long_tabular;
3396 void InsetTabular::write(ostream & os) const
3398 os << "Tabular" << endl;
3403 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3405 // FIXME: depending on the selection state,
3406 // we could offer a different menu.
3407 return cell(0)->contextMenuName() + ";" + contextMenuName();
3411 docstring InsetTabular::contextMenuName() const
3413 return from_ascii("context-tabular");
3417 void InsetTabular::read(Lexer & lex)
3419 //bool const old_format = (lex.getString() == "\\LyXTable");
3427 string token = lex.getString();
3428 while (lex && token != "\\end_inset") {
3430 token = lex.getString();
3433 lex.printError("Missing \\end_inset at this point. ");
3437 int InsetTabular::rowFromY(Cursor & cur, int y) const
3439 // top y coordinate of tabular
3440 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3442 for (; r < tabular.nrows() && y > h; ++r)
3443 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3444 + tabular.interRowSpace(r);
3450 int InsetTabular::columnFromX(Cursor & cur, int x) const
3452 // left x coordinate of tabular
3453 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3455 for (; c < tabular.ncols() && x > w; ++c)
3456 w += tabular.cellWidth(c);
3461 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3463 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3464 // mi.base.textwidth << "\n";
3467 LASSERT(false, /**/);
3470 for (row_type r = 0; r < tabular.nrows(); ++r) {
3473 for (col_type c = 0; c < tabular.ncols(); ++c) {
3474 if (tabular.isPartOfMultiColumn(r, c)
3475 || tabular.isPartOfMultiRow(r, c))
3476 // multicolumn or multirow cell, but not first one
3478 idx_type const cell = tabular.cellIndex(r, c);
3481 Length const p_width = tabular.getPWidth(cell);
3482 if (!p_width.zero())
3483 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3484 tabular.cellInset(cell)->metrics(m, dim);
3485 if (!p_width.zero())
3486 dim.wid = m.base.textwidth;
3487 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3488 + tabular.interColumnSpace(cell);
3490 // FIXME(?): do we need a second metrics call?
3491 TextMetrics const & tm =
3492 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3494 // determine horizontal offset because of decimal align (if necessary)
3495 int decimal_hoffset = 0;
3496 int decimal_width = 0;
3497 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3498 // make a copy which we will split in 2
3499 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3500 head.getText(0)->setMacrocontextPosition(
3501 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3502 head.setBuffer(tabular.buffer());
3503 // split in 2 and calculate width of each part
3504 bool hassep = false;
3505 InsetTableCell tail =
3506 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3507 tail.getText(0)->setMacrocontextPosition(
3508 head.getText(0)->macrocontextPosition());
3509 tail.setBuffer(head.buffer());
3511 head.metrics(m, dim1);
3512 decimal_hoffset = dim1.width();
3514 tail.metrics(m, dim1);
3515 decimal_width = dim1.width();
3518 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3519 tabular.cell_info[r][c].decimal_width = decimal_width;
3521 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3522 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3523 int const lastpardes = tm.last().second->descent()
3524 + TEXT_TO_INSET_OFFSET;
3526 switch (tabular.getVAlignment(cell)) {
3527 case Tabular::LYX_VALIGN_TOP:
3529 case Tabular::LYX_VALIGN_MIDDLE:
3530 offset = -(dim.des - lastpardes)/2;
3532 case Tabular::LYX_VALIGN_BOTTOM:
3533 offset = -(dim.des - lastpardes);
3536 tabular.cell_info[r][c].voffset = offset;
3537 maxasc = max(maxasc, dim.asc - offset);
3538 maxdes = max(maxdes, dim.des + offset);
3540 int const top_space = tabular.row_info[r].top_space_default ?
3541 default_line_space :
3542 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3543 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3544 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3545 default_line_space :
3546 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3547 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3550 // for top-alignment the first horizontal table line must be exactly at
3551 // the position of the base line of the surrounding text line
3552 // for bottom alignment, the same is for the last table line
3553 switch (tabular.tabular_valignment) {
3554 case Tabular::LYX_VALIGN_BOTTOM:
3555 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3557 case Tabular::LYX_VALIGN_MIDDLE:
3558 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3560 case Tabular::LYX_VALIGN_TOP:
3561 offset_valign_ = tabular.rowAscent(0);
3565 tabular.updateColumnWidths();
3566 dim.asc = tabular.rowAscent(0) - offset_valign_;
3567 dim.des = tabular.height() - dim.asc;
3568 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3572 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3575 if (&cur.inset() == this && cur.selection()) {
3576 if (cur.selIsMultiCell()) {
3579 getSelection(cur, rs, re, cs, ce);
3581 idx_type const cell = tabular.cellIndex(row, col);
3582 col_type const cspan = tabular.columnSpan(cell);
3583 row_type const rspan = tabular.rowSpan(cell);
3584 if (col + cspan - 1 >= cs && col <= ce
3585 && row + rspan - 1 >= rs && row <= re)
3588 if (col == tabular.cellColumn(cur.idx())
3589 && row == tabular.cellRow(cur.idx())) {
3590 CursorSlice const & beg = cur.selBegin();
3591 CursorSlice const & end = cur.selEnd();
3593 if ((end.lastpos() > 0 || end.lastpit() > 0)
3594 && end.pos() == end.lastpos() && beg.pos() == 0
3595 && end.pit() == end.lastpit() && beg.pit() == 0)
3603 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3605 x += scx_ + ADD_TO_TABULAR_WIDTH;
3607 BufferView * bv = pi.base.bv;
3608 Cursor & cur = pi.base.bv->cursor();
3611 // FIXME: As the full background is painted in drawBackground(),
3612 // we have no choice but to do a full repaint for the Text cells.
3613 pi.full_repaint = true;
3615 bool const original_selection_state = pi.selected;
3618 first_visible_cell = Tabular::npos;
3620 int yy = y + offset_valign_;
3621 for (row_type r = 0; r < tabular.nrows(); ++r) {
3623 for (col_type c = 0; c < tabular.ncols(); ++c) {
3624 if (tabular.isPartOfMultiColumn(r, c))
3627 idx = tabular.cellIndex(r, c);
3629 if (tabular.isPartOfMultiRow(r, c)) {
3630 nx += tabular.cellWidth(idx);
3634 if (first_visible_cell == Tabular::npos)
3635 first_visible_cell = idx;
3637 pi.selected |= isCellSelected(cur, r, c);
3638 int const cx = nx + tabular.textHOffset(idx);
3639 int const cy = yy + tabular.textVOffset(idx);
3640 // Cache the Inset position.
3641 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3642 cell(idx)->draw(pi, cx, cy);
3643 drawCellLines(pi, nx, yy, r, idx);
3644 nx += tabular.cellWidth(idx);
3645 pi.selected = original_selection_state;
3648 if (r + 1 < tabular.nrows())
3649 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3650 + tabular.interRowSpace(r + 1);
3655 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3657 x += scx_ + ADD_TO_TABULAR_WIDTH;
3658 y += offset_valign_ - tabular.rowAscent(0);
3659 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3660 pi.backgroundColor(this));
3664 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3666 Cursor & cur = pi.base.bv->cursor();
3669 x += scx_ + ADD_TO_TABULAR_WIDTH;
3671 if (!cur.selection())
3673 if (&cur.inset() != this)
3678 bool const full_cell_selected = isCellSelected(cur,
3679 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3681 if (cur.selIsMultiCell() || full_cell_selected) {
3682 for (row_type r = 0; r < tabular.nrows(); ++r) {
3684 for (col_type c = 0; c < tabular.ncols(); ++c) {
3685 if (tabular.isPartOfMultiColumn(r, c))
3688 idx_type const cell = tabular.cellIndex(r, c);
3690 if (tabular.isPartOfMultiRow(r, c)) {
3691 xx += tabular.cellWidth(cell);
3694 int const w = tabular.cellWidth(cell);
3695 int const h = tabular.cellHeight(cell);
3696 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3697 if (isCellSelected(cur, r, c))
3698 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3701 if (r + 1 < tabular.nrows())
3702 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3703 + tabular.interRowSpace(r + 1);
3707 // FIXME: This code has no effect because InsetTableCell does not handle
3708 // drawSelection other than the trivial implementation in Inset.
3710 // x += cellXPos(cur.idx());
3711 // x += tabular.textHOffset(cur.idx());
3712 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3717 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3718 row_type row, idx_type cell) const
3720 y -= tabular.rowAscent(row);
3721 int const w = tabular.cellWidth(cell);
3722 int const h = tabular.cellHeight(cell);
3723 Color const linecolor = pi.textColor(Color_tabularline);
3724 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3727 bool drawline = tabular.topLine(cell)
3728 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3729 pi.pain.line(x, y, x + w, y,
3730 drawline ? linecolor : gridcolor,
3731 drawline ? Painter::line_solid : Painter::line_onoffdash);
3734 drawline = tabular.bottomLine(cell);
3735 pi.pain.line(x, y + h, x + w, y + h,
3736 drawline ? linecolor : gridcolor,
3737 drawline ? Painter::line_solid : Painter::line_onoffdash);
3740 col_type const col = tabular.cellColumn(cell);
3741 drawline = tabular.leftLine(cell)
3742 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3743 pi.pain.line(x, y, x, y + h,
3744 drawline ? linecolor : gridcolor,
3745 drawline ? Painter::line_solid : Painter::line_onoffdash);
3748 x -= tabular.interColumnSpace(cell);
3749 drawline = tabular.rightLine(cell)
3750 || (col + 1 < tabular.ncols()
3751 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3752 pi.pain.line(x + w, y, x + w, y + h,
3753 drawline ? linecolor : gridcolor,
3754 drawline ? Painter::line_solid : Painter::line_onoffdash);
3758 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3760 //lyxerr << "InsetTabular::edit: " << this << endl;
3762 cur.setSelection(false);
3765 if (isRightToLeft(cur))
3766 cur.idx() = tabular.getLastCellInRow(0);
3772 if (isRightToLeft(cur))
3773 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3775 cur.idx() = tabular.numberofcells - 1;
3777 cur.pos() = cur.lastpos(); // FIXME crude guess
3779 cur.setCurrentFont();
3780 // FIXME: this accesses the position cache before it is initialized
3782 //cur.bv().fitCursor();
3786 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3788 // In a longtable, tell captions what the current float is
3789 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3790 string const saveflt = cnts.current_float();
3791 if (tabular.is_long_tabular)
3792 cnts.current_float("table");
3794 ParIterator it2 = it;
3796 size_t const end = it2.nargs();
3797 for ( ; it2.idx() < end; it2.top().forwardIdx())
3798 buffer().updateBuffer(it2, utype);
3801 if (tabular.is_long_tabular)
3802 cnts.current_float(saveflt);
3806 void InsetTabular::addToToc(DocIterator const & cpit) const
3808 DocIterator dit = cpit;
3810 size_t const end = dit.nargs();
3811 for ( ; dit.idx() < end; dit.top().forwardIdx())
3812 cell(dit.idx())->addToToc(dit);
3816 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3818 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3819 << "\n cur:" << cur);
3820 CursorSlice sl = cur.top();
3821 Cursor & bvcur = cur.bv().cursor();
3823 FuncCode const act = cmd.action();
3827 case LFUN_MOUSE_PRESS: {
3828 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3830 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3831 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3832 row_type r = rowFromY(cur, cmd.y());
3833 cur.idx() = tabular.getFirstCellInRow(r);
3836 cur.idx() = tabular.getLastCellInRow(r);
3837 cur.pos() = cur.lastpos();
3838 cur.setSelection(true);
3844 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3845 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3846 || cmd.y() > y0 + tabular.height()) {
3847 col_type c = columnFromX(cur, cmd.x());
3848 cur.idx() = tabular.cellIndex(0, c);
3851 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3852 cur.pos() = cur.lastpos();
3853 cur.setSelection(true);
3858 // do not reset cursor/selection if we have selected
3859 // some cells (bug 2715).
3860 if (cmd.button() == mouse_button::button3
3861 && &bvcur.selBegin().inset() == this
3862 && bvcur.selIsMultiCell())
3865 // Let InsetTableCell do it
3866 cell(cur.idx())->dispatch(cur, cmd);
3869 case LFUN_MOUSE_MOTION:
3870 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3871 if (cmd.button() == mouse_button::button1) {
3872 // only accept motions to places not deeper nested than the real anchor
3873 if (!bvcur.realAnchor().hasPart(cur)) {
3877 // select (additional) row
3879 row_type r = rowFromY(cur, cmd.y());
3880 cur.idx() = tabular.getLastCellInRow(r);
3881 // we need to reset the cursor's pit and pos now, as the old ones
3882 // may no longer be valid.
3885 bvcur.setCursor(cur);
3886 bvcur.setSelection(true);
3889 // select (additional) column
3891 col_type c = columnFromX(cur, cmd.x());
3892 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3893 // we need to reset the cursor's pit and pos now, as the old ones
3894 // may no longer be valid.
3897 bvcur.setCursor(cur);
3898 bvcur.setSelection(true);
3901 // only update if selection changes
3902 if (bvcur.idx() == cur.idx() &&
3903 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3904 cur.noScreenUpdate();
3905 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3906 bvcur.setCursor(cur);
3907 bvcur.setSelection(true);
3908 // if this is a multicell selection, we just set the cursor to
3909 // the beginning of the cell's text.
3910 if (bvcur.selIsMultiCell()) {
3911 bvcur.pit() = bvcur.lastpit();
3912 bvcur.pos() = bvcur.lastpos();
3917 case LFUN_MOUSE_RELEASE:
3922 case LFUN_CELL_BACKWARD:
3924 cur.setSelection(false);
3927 case LFUN_CELL_FORWARD:
3929 cur.setSelection(false);
3932 case LFUN_CHAR_FORWARD_SELECT:
3933 case LFUN_CHAR_FORWARD:
3934 case LFUN_CHAR_BACKWARD_SELECT:
3935 case LFUN_CHAR_BACKWARD:
3936 case LFUN_CHAR_RIGHT_SELECT:
3937 case LFUN_CHAR_RIGHT:
3938 case LFUN_CHAR_LEFT_SELECT:
3939 case LFUN_CHAR_LEFT:
3940 case LFUN_WORD_FORWARD:
3941 case LFUN_WORD_FORWARD_SELECT:
3942 case LFUN_WORD_BACKWARD:
3943 case LFUN_WORD_BACKWARD_SELECT:
3944 case LFUN_WORD_RIGHT:
3945 case LFUN_WORD_RIGHT_SELECT:
3946 case LFUN_WORD_LEFT:
3947 case LFUN_WORD_LEFT_SELECT: {
3948 // determine whether we move to next or previous cell, where to enter
3949 // the new cell from, and which command to "finish" (i.e., exit the
3952 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3953 FuncCode finish_lfun;
3955 if (act == LFUN_CHAR_FORWARD
3956 || act == LFUN_CHAR_FORWARD_SELECT
3957 || act == LFUN_WORD_FORWARD
3958 || act == LFUN_WORD_FORWARD_SELECT) {
3960 finish_lfun = LFUN_FINISHED_FORWARD;
3962 else if (act == LFUN_CHAR_BACKWARD
3963 || act == LFUN_CHAR_BACKWARD_SELECT
3964 || act == LFUN_WORD_BACKWARD
3965 || act == LFUN_WORD_BACKWARD_SELECT) {
3967 finish_lfun = LFUN_FINISHED_BACKWARD;
3969 // LEFT or RIGHT commands --- the interpretation will depend on the
3970 // table's direction.
3972 bool const right = act == LFUN_CHAR_RIGHT
3973 || act == LFUN_CHAR_RIGHT_SELECT
3974 || act == LFUN_WORD_RIGHT
3975 || act == LFUN_WORD_RIGHT_SELECT;
3976 next_cell = isRightToLeft(cur) != right;
3978 if (lyxrc.visual_cursor)
3979 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3981 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3984 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3985 || act == LFUN_CHAR_BACKWARD_SELECT
3986 || act == LFUN_CHAR_RIGHT_SELECT
3987 || act == LFUN_CHAR_LEFT_SELECT
3988 || act == LFUN_WORD_FORWARD_SELECT
3989 || act == LFUN_WORD_RIGHT_SELECT
3990 || act == LFUN_WORD_BACKWARD_SELECT
3991 || act == LFUN_WORD_LEFT_SELECT;
3993 // If we have a multicell selection or we're
3994 // not doing some LFUN_*_SELECT thing anyway...
3995 if (!cur.selIsMultiCell() || !select) {
3996 col_type const c = tabular.cellColumn(cur.idx());
3997 row_type const r = tabular.cellRow(cur.idx());
3998 // Are we trying to select the whole cell and is the whole cell
3999 // not yet selected?
4000 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4001 ((next_cell && cur.pit() == cur.lastpit()
4002 && cur.pos() == cur.lastpos())
4003 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4005 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4007 // ...try to dispatch to the cell's inset.
4008 cell(cur.idx())->dispatch(cur, cmd);
4010 // When we already have a selection we want to select the whole cell
4011 // before going to the next cell.
4012 if (select_whole && !empty_cell){
4013 getText(cur.idx())->selectAll(cur);
4018 // FIXME: When we support the selection of an empty cell, remove
4019 // the !empty_cell from this condition. For now we jump to the next
4020 // cell if the current cell is empty.
4021 if (cur.result().dispatched() && !empty_cell)
4025 // move to next/prev cell, as appropriate
4026 // note that we will always do this if we're selecting and we have
4027 // a multicell selection
4028 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4029 << " cell from: " << int(entry_from));
4031 moveNextCell(cur, entry_from);
4033 movePrevCell(cur, entry_from);
4034 // if we're exiting the table, call the appropriate FINISHED lfun
4035 if (sl == cur.top())
4036 cmd = FuncRequest(finish_lfun);
4043 case LFUN_DOWN_SELECT:
4045 if (!(cur.selection() && cur.selIsMultiCell()))
4046 cell(cur.idx())->dispatch(cur, cmd);
4048 cur.dispatched(); // override the cell's decision
4049 if (sl == cur.top()) {
4050 // if our Text didn't do anything to the cursor
4051 // then we try to put the cursor into the cell below
4052 // setting also the right targetX.
4053 cur.selHandle(act == LFUN_DOWN_SELECT);
4054 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4055 cur.idx() = tabular.cellBelow(cur.idx());
4057 TextMetrics const & tm =
4058 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4059 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
4060 cur.setCurrentFont();
4063 if (sl == cur.top()) {
4064 // we trick it to go to forward after leaving the
4066 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4069 if (cur.selIsMultiCell()) {
4070 cur.pit() = cur.lastpit();
4071 cur.pos() = cur.lastpos();
4072 cur.setCurrentFont();
4077 case LFUN_UP_SELECT:
4079 if (!(cur.selection() && cur.selIsMultiCell()))
4080 cell(cur.idx())->dispatch(cur, cmd);
4081 cur.dispatched(); // override the cell's decision
4082 if (sl == cur.top()) {
4083 // if our Text didn't do anything to the cursor
4084 // then we try to put the cursor into the cell above
4085 // setting also the right targetX.
4086 cur.selHandle(act == LFUN_UP_SELECT);
4087 if (tabular.cellRow(cur.idx()) != 0) {
4088 cur.idx() = tabular.cellAbove(cur.idx());
4089 cur.pit() = cur.lastpit();
4090 Text const * text = cell(cur.idx())->getText(0);
4091 TextMetrics const & tm = cur.bv().textMetrics(text);
4092 ParagraphMetrics const & pm =
4093 tm.parMetrics(cur.lastpit());
4094 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
4095 cur.setCurrentFont();
4098 if (sl == cur.top()) {
4099 cmd = FuncRequest(LFUN_UP);
4102 if (cur.selIsMultiCell()) {
4104 cur.pos() = cur.lastpos();
4105 cur.setCurrentFont();
4110 // case LFUN_SCREEN_DOWN: {
4111 // //if (hasSelection())
4112 // // cur.selection() = false;
4113 // col_type const col = tabular.cellColumn(cur.idx());
4114 // int const t = cur.bv().top_y() + cur.bv().height();
4115 // if (t < yo() + tabular.getHeightOfTabular()) {
4116 // cur.bv().scrollDocView(t, true);
4117 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4119 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4126 // case LFUN_SCREEN_UP: {
4127 // //if (hasSelection())
4128 // // cur.selection() = false;
4129 // col_type const col = tabular.cellColumn(cur.idx());
4130 // int const t = cur.bv().top_y() + cur.bv().height();
4132 // cur.bv().scrollDocView(t, true);
4136 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4140 // cur.par() = cur.lastpar();
4141 // cur.pos() = cur.lastpos();
4145 case LFUN_LAYOUT_TABULAR:
4146 cur.bv().showDialog("tabular");
4149 case LFUN_INSET_MODIFY:
4150 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4154 // insert file functions
4155 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4156 case LFUN_FILE_INSERT_PLAINTEXT:
4158 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4159 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4160 FileName(to_utf8(cmd.argument())));
4163 cur.recordUndoInset(INSERT_UNDO);
4164 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4165 // content has been replaced,
4166 // so cursor might be invalid
4167 cur.pos() = cur.lastpos();
4168 cur.pit() = cur.lastpit();
4169 bvcur.setCursor(cur);
4176 if (cur.selIsMultiCell()) {
4177 if (copySelection(cur)) {
4178 cur.recordUndoInset(DELETE_UNDO);
4182 cell(cur.idx())->dispatch(cur, cmd);
4185 case LFUN_SELF_INSERT:
4186 if (cur.selIsMultiCell()) {
4187 cur.recordUndoInset(DELETE_UNDO);
4190 cell(cur.idx())->dispatch(cur, cmd);
4193 case LFUN_CHAR_DELETE_BACKWARD:
4194 case LFUN_CHAR_DELETE_FORWARD:
4195 if (cur.selIsMultiCell()) {
4196 cur.recordUndoInset(DELETE_UNDO);
4199 cell(cur.idx())->dispatch(cur, cmd);
4203 if (!cur.selection())
4205 if (cur.selIsMultiCell()) {
4209 cell(cur.idx())->dispatch(cur, cmd);
4212 case LFUN_CLIPBOARD_PASTE:
4213 case LFUN_PRIMARY_SELECTION_PASTE: {
4214 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4215 theClipboard().getAsText() :
4216 theSelection().get();
4219 // pass to InsertPlaintextString, but
4220 // only if we have multi-cell content
4221 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4222 cur.recordUndoInset(INSERT_UNDO);
4223 if (insertPlaintextString(cur.bv(), clip, false)) {
4224 // content has been replaced,
4225 // so cursor might be invalid
4226 cur.pos() = cur.lastpos();
4227 cur.pit() = cur.lastpit();
4228 bvcur.setCursor(cur);
4232 // Let the cell handle normal text
4233 cell(cur.idx())->dispatch(cur, cmd);
4238 if (!tabularStackDirty()) {
4239 if (!cur.selIsMultiCell())
4240 cell(cur.idx())->dispatch(cur, cmd);
4243 if (theClipboard().isInternal()) {
4244 cur.recordUndoInset(INSERT_UNDO);
4245 pasteClipboard(cur);
4249 case LFUN_FONT_EMPH:
4250 case LFUN_FONT_BOLD:
4251 case LFUN_FONT_BOLDSYMBOL:
4252 case LFUN_FONT_ROMAN:
4253 case LFUN_FONT_NOUN:
4254 case LFUN_FONT_ITAL:
4255 case LFUN_FONT_FRAK:
4256 case LFUN_FONT_TYPEWRITER:
4257 case LFUN_FONT_SANS:
4258 case LFUN_TEXTSTYLE_APPLY:
4259 case LFUN_TEXTSTYLE_UPDATE:
4260 case LFUN_FONT_SIZE:
4261 case LFUN_FONT_UNDERLINE:
4262 case LFUN_FONT_STRIKEOUT:
4263 case LFUN_FONT_UULINE:
4264 case LFUN_FONT_UWAVE:
4266 case LFUN_WORD_CAPITALIZE:
4267 case LFUN_WORD_UPCASE:
4268 case LFUN_WORD_LOWCASE:
4269 case LFUN_CHARS_TRANSPOSE:
4270 if (cur.selIsMultiCell()) {
4273 getSelection(cur, rs, re, cs, ce);
4274 Cursor tmpcur = cur;
4275 for (row_type r = rs; r <= re; ++r) {
4276 for (col_type c = cs; c <= ce; ++c) {
4277 // cursor follows cell:
4278 tmpcur.idx() = tabular.cellIndex(r, c);
4279 // select this cell only:
4282 tmpcur.resetAnchor();
4283 tmpcur.pit() = tmpcur.lastpit();
4284 tmpcur.pos() = tmpcur.top().lastpos();
4285 tmpcur.setCursor(tmpcur);
4286 tmpcur.setSelection();
4287 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4292 cell(cur.idx())->dispatch(cur, cmd);
4296 case LFUN_INSET_SETTINGS:
4297 // relay this lfun to Inset, not to the cell.
4298 Inset::doDispatch(cur, cmd);
4302 // we try to handle this event in the insets dispatch function.
4303 cell(cur.idx())->dispatch(cur, cmd);
4309 // function sets an object as defined in func_status.h:
4310 // states OK, Unknown, Disabled, On, Off.
4311 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4312 FuncStatus & status) const
4314 switch (cmd.action()) {
4315 case LFUN_INSET_MODIFY: {
4316 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4319 string const s = cmd.getArg(1);
4320 // FIXME: We only check for the very first argument...
4321 int action = Tabular::LAST_ACTION;
4323 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4324 if (tabularFeature[i].feature == s) {
4325 action = tabularFeature[i].action;
4329 if (action == Tabular::LAST_ACTION) {
4331 status.setUnknown(true);
4335 string const argument = cmd.getLongArg(2);
4337 row_type sel_row_start = 0;
4338 row_type sel_row_end = 0;
4339 col_type sel_col_start = 0;
4340 col_type sel_col_end = 0;
4341 Tabular::ltType dummyltt;
4344 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4347 case Tabular::SET_PWIDTH:
4348 case Tabular::SET_MPWIDTH:
4349 case Tabular::SET_SPECIAL_COLUMN:
4350 case Tabular::SET_SPECIAL_MULTICOLUMN:
4351 case Tabular::APPEND_ROW:
4352 case Tabular::APPEND_COLUMN:
4353 case Tabular::DELETE_ROW:
4354 case Tabular::DELETE_COLUMN:
4355 case Tabular::COPY_ROW:
4356 case Tabular::COPY_COLUMN:
4357 case Tabular::SET_TOP_SPACE:
4358 case Tabular::SET_BOTTOM_SPACE:
4359 case Tabular::SET_INTERLINE_SPACE:
4363 case Tabular::SET_DECIMAL_POINT:
4365 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4368 case Tabular::SET_MULTICOLUMN:
4369 case Tabular::UNSET_MULTICOLUMN:
4370 case Tabular::MULTICOLUMN:
4371 // If a row is set as longtable caption, it must not be allowed
4372 // to unset that this row is a multicolumn.
4373 status.setEnabled(sel_row_start == sel_row_end
4374 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4375 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4378 case Tabular::SET_MULTIROW:
4379 case Tabular::UNSET_MULTIROW:
4380 case Tabular::MULTIROW:
4381 // If a row is set as longtable caption, it must not be allowed
4382 // to unset that this row is a multirow.
4383 status.setEnabled(sel_col_start == sel_col_end
4384 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4385 status.setOnOff(tabular.isMultiRow(cur.idx()));
4388 case Tabular::SET_ALL_LINES:
4389 case Tabular::UNSET_ALL_LINES:
4390 case Tabular::SET_BORDER_LINES:
4391 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4394 case Tabular::SET_LINE_TOP:
4395 case Tabular::SET_LINE_BOTTOM:
4396 case Tabular::SET_LINE_LEFT:
4397 case Tabular::SET_LINE_RIGHT:
4398 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4401 case Tabular::TOGGLE_LINE_TOP:
4402 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4403 status.setOnOff(tabular.topLine(cur.idx()));
4406 case Tabular::TOGGLE_LINE_BOTTOM:
4407 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4408 status.setOnOff(tabular.bottomLine(cur.idx()));
4411 case Tabular::TOGGLE_LINE_LEFT:
4412 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4413 status.setOnOff(tabular.leftLine(cur.idx()));
4416 case Tabular::TOGGLE_LINE_RIGHT:
4417 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4418 status.setOnOff(tabular.rightLine(cur.idx()));
4421 // multirow cells only inherit the alignment of the column if the column has
4422 // no width, otherwise they are left-aligned
4423 // therefore allow always left but right and center only if there is no width
4424 case Tabular::M_ALIGN_LEFT:
4426 case Tabular::ALIGN_LEFT:
4427 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4430 case Tabular::M_ALIGN_RIGHT:
4432 case Tabular::ALIGN_RIGHT:
4433 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4434 && !tabular.getPWidth(cur.idx()).zero()));
4435 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4438 case Tabular::M_ALIGN_CENTER:
4440 case Tabular::ALIGN_CENTER:
4441 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4442 && !tabular.getPWidth(cur.idx()).zero()));
4443 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4446 case Tabular::ALIGN_BLOCK:
4447 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4448 && !tabular.isMultiRow(cur.idx()));
4449 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4452 case Tabular::ALIGN_DECIMAL:
4453 status.setEnabled(!tabular.isMultiRow(cur.idx())
4454 && !tabular.isMultiColumn(cur.idx()));
4455 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4458 case Tabular::M_VALIGN_TOP:
4460 case Tabular::VALIGN_TOP:
4461 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4462 && !tabular.isMultiRow(cur.idx()));
4464 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4467 case Tabular::M_VALIGN_BOTTOM:
4469 case Tabular::VALIGN_BOTTOM:
4470 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4471 && !tabular.isMultiRow(cur.idx()));
4473 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4476 case Tabular::M_VALIGN_MIDDLE:
4478 case Tabular::VALIGN_MIDDLE:
4479 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4480 && !tabular.isMultiRow(cur.idx()));
4482 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4485 case Tabular::SET_LONGTABULAR:
4486 // setting as longtable is not allowed when table is inside a float
4487 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4488 || cur.innerInsetOfType(WRAP_CODE) != 0)
4489 status.setEnabled(false);
4491 status.setEnabled(true);
4492 status.setOnOff(tabular.is_long_tabular);
4495 case Tabular::UNSET_LONGTABULAR:
4496 status.setOnOff(!tabular.is_long_tabular);
4499 case Tabular::TOGGLE_ROTATE_TABULAR:
4500 case Tabular::SET_ROTATE_TABULAR:
4501 status.setOnOff(tabular.rotate);
4504 case Tabular::TABULAR_VALIGN_TOP:
4505 status.setOnOff(tabular.tabular_valignment
4506 == Tabular::LYX_VALIGN_TOP);
4508 case Tabular::TABULAR_VALIGN_MIDDLE:
4509 status.setOnOff(tabular.tabular_valignment
4510 == Tabular::LYX_VALIGN_MIDDLE);
4512 case Tabular::TABULAR_VALIGN_BOTTOM:
4513 status.setOnOff(tabular.tabular_valignment
4514 == Tabular::LYX_VALIGN_BOTTOM);
4517 case Tabular::LONGTABULAR_ALIGN_LEFT:
4518 status.setOnOff(tabular.longtabular_alignment
4519 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4521 case Tabular::LONGTABULAR_ALIGN_CENTER:
4522 status.setOnOff(tabular.longtabular_alignment
4523 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4525 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4526 status.setOnOff(tabular.longtabular_alignment
4527 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4530 case Tabular::UNSET_ROTATE_TABULAR:
4531 status.setOnOff(!tabular.rotate);
4534 case Tabular::TOGGLE_ROTATE_CELL:
4535 case Tabular::SET_ROTATE_CELL:
4536 status.setOnOff(!oneCellHasRotationState(false,
4537 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4540 case Tabular::UNSET_ROTATE_CELL:
4541 status.setOnOff(!oneCellHasRotationState(true,
4542 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4545 case Tabular::SET_USEBOX:
4546 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4549 // every row can only be one thing:
4550 // either a footer or header or caption
4551 case Tabular::SET_LTFIRSTHEAD:
4552 status.setEnabled(sel_row_start == sel_row_end
4553 && !tabular.ltCaption(sel_row_start));
4554 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4557 case Tabular::UNSET_LTFIRSTHEAD:
4558 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4561 case Tabular::SET_LTHEAD:
4562 status.setEnabled(sel_row_start == sel_row_end
4563 && !tabular.ltCaption(sel_row_start));
4564 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4567 case Tabular::UNSET_LTHEAD:
4568 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4571 case Tabular::SET_LTFOOT:
4572 status.setEnabled(sel_row_start == sel_row_end
4573 && !tabular.ltCaption(sel_row_start));
4574 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4577 case Tabular::UNSET_LTFOOT:
4578 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4581 case Tabular::SET_LTLASTFOOT:
4582 status.setEnabled(sel_row_start == sel_row_end
4583 && !tabular.ltCaption(sel_row_start));
4584 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4587 case Tabular::UNSET_LTLASTFOOT:
4588 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4591 case Tabular::SET_LTNEWPAGE:
4592 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4595 // only one row can be the caption
4596 // and a multirow cannot be set as caption
4597 case Tabular::SET_LTCAPTION:
4598 case Tabular::UNSET_LTCAPTION:
4599 case Tabular::TOGGLE_LTCAPTION:
4600 status.setEnabled(sel_row_start == sel_row_end
4601 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4602 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4603 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4604 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4605 && (!tabular.haveLTCaption()
4606 || tabular.ltCaption(sel_row_start))
4607 && !tabular.isMultiRow(sel_row_start));
4608 status.setOnOff(tabular.ltCaption(sel_row_start));
4611 case Tabular::SET_BOOKTABS:
4612 status.setOnOff(tabular.use_booktabs);
4615 case Tabular::UNSET_BOOKTABS:
4616 status.setOnOff(!tabular.use_booktabs);
4621 status.setEnabled(false);
4627 // These are only enabled inside tabular
4628 case LFUN_CELL_BACKWARD:
4629 case LFUN_CELL_FORWARD:
4630 status.setEnabled(true);
4633 // disable these with multiple cells selected
4634 case LFUN_INSET_INSERT:
4635 case LFUN_TABULAR_INSERT:
4636 case LFUN_FLEX_INSERT:
4637 case LFUN_FLOAT_INSERT:
4638 case LFUN_FLOAT_WIDE_INSERT:
4639 case LFUN_FOOTNOTE_INSERT:
4640 case LFUN_MARGINALNOTE_INSERT:
4641 case LFUN_MATH_INSERT:
4642 case LFUN_MATH_MODE:
4643 case LFUN_MATH_MUTATE:
4644 case LFUN_MATH_DISPLAY:
4645 case LFUN_NOTE_INSERT:
4646 case LFUN_ARGUMENT_INSERT:
4647 case LFUN_BOX_INSERT:
4648 case LFUN_BRANCH_INSERT:
4649 case LFUN_PHANTOM_INSERT:
4650 case LFUN_WRAP_INSERT:
4651 case LFUN_PREVIEW_INSERT:
4652 case LFUN_ERT_INSERT: {
4653 if (cur.selIsMultiCell()) {
4654 status.setEnabled(false);
4657 return cell(cur.idx())->getStatus(cur, cmd, status);
4660 // disable in non-fixed-width cells
4661 case LFUN_NEWLINE_INSERT:
4662 case LFUN_BREAK_PARAGRAPH: {
4663 if (tabular.getPWidth(cur.idx()).zero()) {
4664 status.setEnabled(false);
4667 return cell(cur.idx())->getStatus(cur, cmd, status);
4670 case LFUN_NEWPAGE_INSERT:
4671 status.setEnabled(false);
4675 if (tabularStackDirty() && theClipboard().isInternal()) {
4676 if (cur.selIsMultiCell()) {
4679 getSelection(cur, rs, re, cs, ce);
4680 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4681 && paste_tabular->nrows() == re - rs + 1)
4682 status.setEnabled(true);
4684 status.setEnabled(false);
4685 status.message(_("Selection size should match clipboard content."));
4688 status.setEnabled(true);
4691 return cell(cur.idx())->getStatus(cur, cmd, status);
4693 case LFUN_INSET_SETTINGS:
4694 // relay this lfun to Inset, not to the cell.
4695 return Inset::getStatus(cur, cmd, status);
4698 // we try to handle this event in the insets dispatch function.
4699 return cell(cur.idx())->getStatus(cur, cmd, status);
4705 Inset::DisplayType InsetTabular::display() const
4707 if (tabular.is_long_tabular) {
4708 switch (tabular.longtabular_alignment) {
4709 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4711 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4713 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4723 int InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4725 return tabular.latex(os, runparams);
4729 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4731 os << '\n'; // output table on a new line
4732 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4733 tabular.plaintext(os, runparams, dp, false, 0);
4734 return PLAINTEXT_NEWLINE;
4738 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4743 // FIXME: Why not pass a proper DocIterator here?
4745 // if the table is inside a float it doesn't need the informaltable
4746 // wrapper. Search for it.
4747 for (master = owner(); master; master = master->owner())
4748 if (master->lyxCode() == FLOAT_CODE)
4753 os << "<informaltable>";
4756 ret += tabular.docbook(os, runparams);
4758 os << "</informaltable>";
4765 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4767 return tabular.xhtml(xs, rp);
4771 void InsetTabular::validate(LaTeXFeatures & features) const
4773 tabular.validate(features);
4775 // It'd be better to be able to get this from an InsetLayout, but at present
4776 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4777 if (features.runparams().flavor == OutputParams::HTML)
4778 features.addPreambleSnippet("<style type=\"text/css\">\n"
4779 "table { border: 1px solid black; display: inline-block; }\n"
4780 "td { border: 1px solid black; padding: 0.5ex; }\n"
4785 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4787 return tabular.cellInset(idx);
4791 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4793 return tabular.cellInset(idx);
4797 void InsetTabular::cursorPos(BufferView const & bv,
4798 CursorSlice const & sl, bool boundary, int & x, int & y) const
4800 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4802 // y offset correction
4803 y += cellYPos(sl.idx());
4804 y += tabular.textVOffset(sl.idx());
4805 y += offset_valign_;
4807 // x offset correction
4808 x += cellXPos(sl.idx());
4809 x += tabular.textHOffset(sl.idx());
4810 x += ADD_TO_TABULAR_WIDTH;
4815 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4819 Inset const & inset = *tabular.cellInset(cell);
4820 Point o = bv.coordCache().getInsets().xy(&inset);
4821 int const xbeg = o.x_ - tabular.textHOffset(cell);
4822 int const xend = xbeg + tabular.cellWidth(cell);
4823 row_type const row = tabular.cellRow(cell);
4824 int const ybeg = o.y_ - tabular.rowAscent(row)
4825 - tabular.interRowSpace(row);
4826 int const yend = ybeg + tabular.cellHeight(cell);
4838 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4839 // << " ybeg=" << ybeg << " yend=" << yend
4840 // << " xx=" << xx << " yy=" << yy
4841 // << " dist=" << xx + yy << endl;
4846 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4848 //lyxerr << "InsetTabular::editXY: " << this << endl;
4849 cur.setSelection(false);
4851 cur.idx() = getNearestCell(cur.bv(), x, y);
4853 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4857 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4859 cur.idx() = getNearestCell(cur.bv(), x, y);
4860 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4864 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4866 idx_type idx_min = 0;
4867 int dist_min = numeric_limits<int>::max();
4868 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4869 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4870 int const d = dist(bv, i, x, y);
4881 int InsetTabular::cellYPos(idx_type const cell) const
4883 row_type row = tabular.cellRow(cell);
4885 for (row_type r = 0; r < row; ++r)
4886 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4887 + tabular.interRowSpace(r + 1);
4892 int InsetTabular::cellXPos(idx_type const cell) const
4894 col_type col = tabular.cellColumn(cell);
4896 for (col_type c = 0; c < col; ++c)
4897 lx += tabular.column_info[c].width;
4902 void InsetTabular::resetPos(Cursor & cur) const
4904 BufferView & bv = cur.bv();
4905 int const maxwidth = bv.workWidth();
4907 int const scx_old = scx_;
4908 int const i = cur.find(this);
4913 int const X2 = maxwidth;
4914 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4915 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4916 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4919 scx_ = X1 + 20 - x1;
4921 scx_ = X2 - 20 - x2;
4926 // only update if offset changed
4927 if (scx_ != scx_old)
4928 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4932 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4934 row_type const row = tabular.cellRow(cur.idx());
4935 col_type const col = tabular.cellColumn(cur.idx());
4937 if (isRightToLeft(cur)) {
4938 if (tabular.cellColumn(cur.idx()) == 0) {
4939 if (row == tabular.nrows() - 1)
4941 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4946 cur.idx() = tabular.getLastCellInRow(row - 1);
4948 cur.idx() = tabular.cellIndex(row, col - 1);
4951 if (tabular.isLastCell(cur.idx()))
4953 if (cur.idx() == tabular.getLastCellInRow(row))
4954 cur.idx() = tabular.cellIndex(row + 1, 0);
4956 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4957 cur.idx() = tabular.cellIndex(row, colnextcell);
4961 cur.boundary(false);
4963 if (cur.selIsMultiCell()) {
4964 cur.pit() = cur.lastpit();
4965 cur.pos() = cur.lastpos();
4973 // in visual mode, place cursor at extreme left or right
4975 switch(entry_from) {
4977 case ENTRY_DIRECTION_RIGHT:
4978 cur.posVisToRowExtremity(false /* !left */);
4980 case ENTRY_DIRECTION_LEFT:
4981 cur.posVisToRowExtremity(true /* left */);
4983 case ENTRY_DIRECTION_IGNORE:
4984 // nothing to do in this case
4988 cur.setCurrentFont();
4993 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4995 row_type const row = tabular.cellRow(cur.idx());
4996 col_type const col = tabular.cellColumn(cur.idx());
4998 if (isRightToLeft(cur)) {
4999 if (cur.idx() == tabular.getLastCellInRow(row)) {
5002 cur.idx() = tabular.getFirstCellInRow(row);
5003 cur.idx() = tabular.cellAbove(cur.idx());
5005 if (tabular.isLastCell(cur.idx()))
5007 if (cur.idx() == tabular.getLastCellInRow(row))
5008 cur.idx() = tabular.cellIndex(row + 1, 0);
5010 cur.idx() = tabular.cellIndex(row, col + 1);
5013 if (cur.idx() == 0) // first cell
5016 cur.idx() = tabular.getLastCellInRow(row - 1);
5018 cur.idx() = tabular.cellIndex(row, col - 1);
5021 if (cur.selIsMultiCell()) {
5022 cur.pit() = cur.lastpit();
5023 cur.pos() = cur.lastpos();
5028 cur.pit() = cur.lastpit();
5029 cur.pos() = cur.lastpos();
5031 // in visual mode, place cursor at extreme left or right
5033 switch(entry_from) {
5035 case ENTRY_DIRECTION_RIGHT:
5036 cur.posVisToRowExtremity(false /* !left */);
5038 case ENTRY_DIRECTION_LEFT:
5039 cur.posVisToRowExtremity(true /* left */);
5041 case ENTRY_DIRECTION_IGNORE:
5042 // nothing to do in this case
5046 cur.setCurrentFont();
5051 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5053 istringstream is(argument);
5056 if (insetCode(s) != TABULAR_CODE)
5060 size_t safe_guard = 0;
5065 if (safe_guard > 1000) {
5066 LYXERR0("parameter max count reached!");
5070 Tabular::Feature action = Tabular::LAST_ACTION;
5073 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5074 if (s != tabularFeature[i].feature)
5077 action = tabularFeature[i].action;
5080 if (action == Tabular::LAST_ACTION) {
5081 LYXERR0("Feature not found " << s);
5085 if (tabularFeature[i].need_value)
5087 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5088 tabularFeatures(cur, action, val);
5094 static void checkLongtableSpecial(Tabular::ltType & ltt,
5095 string const & special, bool & flag)
5097 if (special == "dl_above") {
5100 } else if (special == "dl_below") {
5101 ltt.bottomDL = flag;
5103 } else if (special == "empty") {
5113 bool InsetTabular::oneCellHasRotationState(bool rotated,
5114 row_type row_start, row_type row_end,
5115 col_type col_start, col_type col_end) const
5117 for (row_type r = row_start; r <= row_end; ++r)
5118 for (col_type c = col_start; c <= col_end; ++c)
5119 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5125 void InsetTabular::tabularFeatures(Cursor & cur,
5126 Tabular::Feature feature, string const & value)
5128 col_type sel_col_start;
5129 col_type sel_col_end;
5130 row_type sel_row_start;
5131 row_type sel_row_end;
5132 bool setLines = false;
5133 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5134 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5138 case Tabular::M_ALIGN_LEFT:
5139 case Tabular::ALIGN_LEFT:
5140 setAlign = LYX_ALIGN_LEFT;
5143 case Tabular::M_ALIGN_RIGHT:
5144 case Tabular::ALIGN_RIGHT:
5145 setAlign = LYX_ALIGN_RIGHT;
5148 case Tabular::M_ALIGN_CENTER:
5149 case Tabular::ALIGN_CENTER:
5150 setAlign = LYX_ALIGN_CENTER;
5153 case Tabular::ALIGN_BLOCK:
5154 setAlign = LYX_ALIGN_BLOCK;
5157 case Tabular::ALIGN_DECIMAL:
5158 setAlign = LYX_ALIGN_DECIMAL;
5161 case Tabular::M_VALIGN_TOP:
5162 case Tabular::VALIGN_TOP:
5163 setVAlign = Tabular::LYX_VALIGN_TOP;
5166 case Tabular::M_VALIGN_BOTTOM:
5167 case Tabular::VALIGN_BOTTOM:
5168 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5171 case Tabular::M_VALIGN_MIDDLE:
5172 case Tabular::VALIGN_MIDDLE:
5173 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5180 cur.recordUndoInset(ATOMIC_UNDO, this);
5182 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5183 row_type const row = tabular.cellRow(cur.idx());
5184 col_type const column = tabular.cellColumn(cur.idx());
5186 Tabular::ltType ltt;
5190 case Tabular::SET_PWIDTH: {
5191 Length const len(value);
5192 tabular.setColumnPWidth(cur, cur.idx(), len);
5194 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5195 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5196 // check if there is a 1-column multicolumn cell
5197 // if so it must get the same width
5198 for (row_type r = 0; r < tabular.nrows(); ++r) {
5199 if (tabular.isMultiColumn(tabular.cellIndex(r, column))
5200 && tabular.columnSpan(tabular.cellIndex(r, column)) == 1)
5201 tabular.setMColumnPWidth(cur, tabular.cellIndex(r, column), len);
5206 case Tabular::SET_MPWIDTH:
5207 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5208 // if the multicolumn only spans 1 column, the width of the whole column
5209 // must have the same width, see bug #7055
5210 if (tabular.columnSpan(cur.idx()) == 1)
5211 for (row_type r = 0; r < tabular.nrows(); ++r) {
5212 if (!tabular.isMultiColumn(tabular.cellIndex(r, column)))
5213 tabular.setColumnPWidth(cur, tabular.cellIndex(r, column), Length(value));
5214 else if (tabular.isMultiColumn(tabular.cellIndex(r, column))
5215 && tabular.columnSpan(tabular.cellIndex(r, column)) == 1)
5216 tabular.setMColumnPWidth(cur, tabular.cellIndex(r, column), Length(value));
5220 case Tabular::SET_MROFFSET:
5221 tabular.setMROffset(cur, cur.idx(), Length(value));
5224 case Tabular::SET_SPECIAL_COLUMN:
5225 case Tabular::SET_SPECIAL_MULTICOLUMN:
5226 if (value == "none")
5227 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5229 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5232 case Tabular::APPEND_ROW:
5233 // append the row into the tabular
5234 tabular.appendRow(cur.idx());
5237 case Tabular::APPEND_COLUMN:
5238 // append the column into the tabular
5239 tabular.appendColumn(cur.idx());
5240 cur.idx() = tabular.cellIndex(row, column);
5243 case Tabular::DELETE_ROW:
5244 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5245 tabular.deleteRow(sel_row_start);
5246 if (sel_row_start >= tabular.nrows())
5248 cur.idx() = tabular.cellIndex(sel_row_start, column);
5251 cur.setSelection(false);
5254 case Tabular::DELETE_COLUMN:
5255 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5256 tabular.deleteColumn(sel_col_start);
5257 if (sel_col_start >= tabular.ncols())
5259 cur.idx() = tabular.cellIndex(row, sel_col_start);
5262 cur.setSelection(false);
5265 case Tabular::COPY_ROW:
5266 tabular.copyRow(row);
5269 case Tabular::COPY_COLUMN:
5270 tabular.copyColumn(column);
5271 cur.idx() = tabular.cellIndex(row, column);
5274 case Tabular::SET_LINE_TOP:
5275 case Tabular::TOGGLE_LINE_TOP: {
5276 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5277 ? (value == "true") : !tabular.topLine(cur.idx());
5278 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5279 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5280 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5284 case Tabular::SET_LINE_BOTTOM:
5285 case Tabular::TOGGLE_LINE_BOTTOM: {
5286 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5287 ? (value == "true") : !tabular.bottomLine(cur.idx());
5288 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5289 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5290 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5294 case Tabular::SET_LINE_LEFT:
5295 case Tabular::TOGGLE_LINE_LEFT: {
5296 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5297 ? (value == "true") : !tabular.leftLine(cur.idx());
5298 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5299 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5300 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5304 case Tabular::SET_LINE_RIGHT:
5305 case Tabular::TOGGLE_LINE_RIGHT: {
5306 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5307 ? (value == "true") : !tabular.rightLine(cur.idx());
5308 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5309 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5310 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5314 case Tabular::M_ALIGN_LEFT:
5315 case Tabular::M_ALIGN_RIGHT:
5316 case Tabular::M_ALIGN_CENTER:
5317 case Tabular::ALIGN_LEFT:
5318 case Tabular::ALIGN_RIGHT:
5319 case Tabular::ALIGN_CENTER:
5320 case Tabular::ALIGN_BLOCK:
5321 case Tabular::ALIGN_DECIMAL:
5322 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5323 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5324 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5325 !tabular.getPWidth(c).zero());
5328 case Tabular::M_VALIGN_TOP:
5329 case Tabular::M_VALIGN_BOTTOM:
5330 case Tabular::M_VALIGN_MIDDLE:
5332 case Tabular::VALIGN_TOP:
5333 case Tabular::VALIGN_BOTTOM:
5334 case Tabular::VALIGN_MIDDLE:
5335 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5336 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5337 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5340 case Tabular::SET_MULTICOLUMN: {
5341 if (!cur.selection()) {
5342 // just multicol for one single cell
5343 // check whether we are completely in a multicol
5344 if (!tabular.isMultiColumn(cur.idx()))
5345 tabular.setMultiColumn(cur.idx(), 1);
5348 // we have a selection so this means we just add all this
5349 // cells to form a multicolumn cell
5350 idx_type const s_start = cur.selBegin().idx();
5351 row_type const col_start = tabular.cellColumn(s_start);
5352 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5353 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5356 cur.setSelection(false);
5360 case Tabular::UNSET_MULTICOLUMN: {
5361 if (!cur.selection()) {
5362 if (tabular.isMultiColumn(cur.idx()))
5363 tabular.unsetMultiColumn(cur.idx());
5368 case Tabular::MULTICOLUMN: {
5369 if (tabular.isMultiColumn(cur.idx()))
5370 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5372 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5376 case Tabular::SET_MULTIROW: {
5377 if (!cur.selection()) {
5378 // just multirow for one single cell
5379 // check whether we are completely in a multirow
5380 if (!tabular.isMultiRow(cur.idx()))
5381 tabular.setMultiRow(cur.idx(), 1);
5384 // we have a selection so this means we just add all this
5385 // cells to form a multirow cell
5386 idx_type const s_start = cur.selBegin().idx();
5387 row_type const row_start = tabular.cellRow(s_start);
5388 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5389 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5392 cur.setSelection(false);
5396 case Tabular::UNSET_MULTIROW: {
5397 if (!cur.selection()) {
5398 if (tabular.isMultiRow(cur.idx()))
5399 tabular.unsetMultiRow(cur.idx());
5404 case Tabular::MULTIROW: {
5405 if (tabular.isMultiRow(cur.idx()))
5406 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5408 tabularFeatures(cur, Tabular::SET_MULTIROW);
5412 case Tabular::SET_ALL_LINES:
5414 case Tabular::UNSET_ALL_LINES:
5415 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5416 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5417 idx_type const cell = tabular.cellIndex(r, c);
5418 tabular.setTopLine(cell, setLines);
5419 tabular.setBottomLine(cell, setLines);
5420 tabular.setRightLine(cell, setLines);
5421 tabular.setLeftLine(cell, setLines);
5425 case Tabular::SET_BORDER_LINES:
5426 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5427 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5428 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5430 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5431 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5432 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5436 case Tabular::SET_LONGTABULAR:
5437 tabular.is_long_tabular = true;
5440 case Tabular::UNSET_LONGTABULAR:
5441 for (row_type r = 0; r < tabular.nrows(); ++r) {
5442 if (tabular.ltCaption(r)) {
5443 cur.idx() = tabular.cellIndex(r, 0);
5446 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5449 tabular.is_long_tabular = false;
5452 case Tabular::SET_ROTATE_TABULAR:
5453 tabular.rotate = true;
5456 case Tabular::UNSET_ROTATE_TABULAR:
5457 tabular.rotate = false;
5460 case Tabular::TOGGLE_ROTATE_TABULAR:
5461 tabular.rotate = !tabular.rotate;
5464 case Tabular::TABULAR_VALIGN_TOP:
5465 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5468 case Tabular::TABULAR_VALIGN_MIDDLE:
5469 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5472 case Tabular::TABULAR_VALIGN_BOTTOM:
5473 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5476 case Tabular::LONGTABULAR_ALIGN_LEFT:
5477 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5480 case Tabular::LONGTABULAR_ALIGN_CENTER:
5481 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5484 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5485 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5490 case Tabular::SET_ROTATE_CELL:
5491 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5492 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5493 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5496 case Tabular::UNSET_ROTATE_CELL:
5497 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5498 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5499 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5502 case Tabular::TOGGLE_ROTATE_CELL:
5504 bool oneNotRotated = oneCellHasRotationState(false,
5505 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5507 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5508 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5509 tabular.setRotateCell(tabular.cellIndex(r, c),
5514 case Tabular::SET_USEBOX: {
5515 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5516 if (val == tabular.getUsebox(cur.idx()))
5517 val = Tabular::BOX_NONE;
5518 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5519 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5520 tabular.setUsebox(tabular.cellIndex(r, c), val);
5524 case Tabular::UNSET_LTFIRSTHEAD:
5526 case Tabular::SET_LTFIRSTHEAD:
5527 tabular.getRowOfLTFirstHead(row, ltt);
5528 checkLongtableSpecial(ltt, value, flag);
5529 tabular.setLTHead(row, flag, ltt, true);
5532 case Tabular::UNSET_LTHEAD:
5534 case Tabular::SET_LTHEAD:
5535 tabular.getRowOfLTHead(row, ltt);
5536 checkLongtableSpecial(ltt, value, flag);
5537 tabular.setLTHead(row, flag, ltt, false);
5540 case Tabular::UNSET_LTFOOT:
5542 case Tabular::SET_LTFOOT:
5543 tabular.getRowOfLTFoot(row, ltt);
5544 checkLongtableSpecial(ltt, value, flag);
5545 tabular.setLTFoot(row, flag, ltt, false);
5548 case Tabular::UNSET_LTLASTFOOT:
5550 case Tabular::SET_LTLASTFOOT:
5551 tabular.getRowOfLTLastFoot(row, ltt);
5552 checkLongtableSpecial(ltt, value, flag);
5553 tabular.setLTFoot(row, flag, ltt, true);
5556 case Tabular::SET_LTNEWPAGE:
5557 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5560 case Tabular::SET_LTCAPTION: {
5561 if (tabular.ltCaption(row))
5563 cur.idx() = tabular.setLTCaption(row, true);
5566 cur.setSelection(false);
5567 // If a row is set as caption, then also insert
5568 // a caption. Otherwise the LaTeX output is broken.
5569 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5570 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5574 case Tabular::UNSET_LTCAPTION: {
5575 if (!tabular.ltCaption(row))
5577 cur.idx() = tabular.setLTCaption(row, false);
5580 cur.setSelection(false);
5581 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5582 if (lyx::getStatus(fr).enabled())
5587 case Tabular::TOGGLE_LTCAPTION: {
5588 if (tabular.ltCaption(row))
5589 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5591 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5595 case Tabular::SET_BOOKTABS:
5596 tabular.use_booktabs = true;
5599 case Tabular::UNSET_BOOKTABS:
5600 tabular.use_booktabs = false;
5603 case Tabular::SET_TOP_SPACE: {
5605 if (value == "default")
5606 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5607 tabular.row_info[r].top_space_default = true;
5608 else if (value == "none")
5609 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5610 tabular.row_info[r].top_space_default = false;
5611 tabular.row_info[r].top_space = len;
5613 else if (isValidLength(value, &len))
5614 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5615 tabular.row_info[r].top_space_default = false;
5616 tabular.row_info[r].top_space = len;
5621 case Tabular::SET_BOTTOM_SPACE: {
5623 if (value == "default")
5624 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5625 tabular.row_info[r].bottom_space_default = true;
5626 else if (value == "none")
5627 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5628 tabular.row_info[r].bottom_space_default = false;
5629 tabular.row_info[r].bottom_space = len;
5631 else if (isValidLength(value, &len))
5632 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5633 tabular.row_info[r].bottom_space_default = false;
5634 tabular.row_info[r].bottom_space = len;
5639 case Tabular::SET_INTERLINE_SPACE: {
5641 if (value == "default")
5642 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5643 tabular.row_info[r].interline_space_default = true;
5644 else if (value == "none")
5645 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5646 tabular.row_info[r].interline_space_default = false;
5647 tabular.row_info[r].interline_space = len;
5649 else if (isValidLength(value, &len))
5650 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5651 tabular.row_info[r].interline_space_default = false;
5652 tabular.row_info[r].interline_space = len;
5657 case Tabular::SET_DECIMAL_POINT:
5658 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5659 tabular.column_info[c].decimal_point = from_utf8(value);
5662 // dummy stuff just to avoid warnings
5663 case Tabular::LAST_ACTION:
5669 bool InsetTabular::copySelection(Cursor & cur)
5671 if (!cur.selection())
5676 getSelection(cur, rs, re, cs, ce);
5678 paste_tabular.reset(new Tabular(tabular));
5680 for (row_type r = 0; r < rs; ++r)
5681 paste_tabular->deleteRow(0);
5683 row_type const rows = re - rs + 1;
5684 while (paste_tabular->nrows() > rows)
5685 paste_tabular->deleteRow(rows);
5687 for (col_type c = 0; c < cs; ++c)
5688 paste_tabular->deleteColumn(0);
5690 col_type const columns = ce - cs + 1;
5691 while (paste_tabular->ncols() > columns)
5692 paste_tabular->deleteColumn(columns);
5694 paste_tabular->setBuffer(tabular.buffer());
5696 odocstringstream os;
5697 OutputParams const runparams(0);
5698 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5699 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5700 cap::copySelection(cur, os.str());
5702 // mark tabular stack dirty
5703 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5704 // when we (hopefully) have a one-for-all paste mechanism.
5705 // This must be called after cap::copySelection.
5706 dirtyTabularStack(true);
5712 bool InsetTabular::pasteClipboard(Cursor & cur)
5716 col_type actcol = tabular.cellColumn(cur.idx());
5717 row_type actrow = tabular.cellRow(cur.idx());
5719 if (cur.selIsMultiCell()) {
5722 getSelection(cur, actrow, re, actcol, ce);
5725 for (row_type r1 = 0, r2 = actrow;
5726 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5728 for (col_type c1 = 0, c2 = actcol;
5729 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5731 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5732 tabular.isPartOfMultiColumn(r2, c2))
5734 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5738 if (tabular.isPartOfMultiColumn(r2, c2)) {
5742 shared_ptr<InsetTableCell> inset(
5743 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5744 tabular.setCellInset(r2, c2, inset);
5745 // FIXME?: why do we need to do this explicitly? (EL)
5746 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5748 // FIXME: change tracking (MG)
5749 inset->setChange(Change(buffer().params().trackChanges ?
5750 Change::INSERTED : Change::UNCHANGED));
5758 void InsetTabular::cutSelection(Cursor & cur)
5760 if (!cur.selection())
5765 getSelection(cur, rs, re, cs, ce);
5766 for (row_type r = rs; r <= re; ++r) {
5767 for (col_type c = cs; c <= ce; ++c) {
5768 shared_ptr<InsetTableCell> t
5769 = cell(tabular.cellIndex(r, c));
5770 if (buffer().params().trackChanges)
5771 // FIXME: Change tracking (MG)
5772 t->setChange(Change(Change::DELETED));
5778 // cursor position might be invalid now
5779 if (cur.pit() > cur.lastpit())
5780 cur.pit() = cur.lastpit();
5781 if (cur.pos() > cur.lastpos())
5782 cur.pos() = cur.lastpos();
5783 cur.clearSelection();
5787 bool InsetTabular::isRightToLeft(Cursor & cur) const
5789 LASSERT(cur.depth() > 1, /**/);
5790 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5791 pos_type const parentpos = cur[cur.depth() - 2].pos();
5792 return parentpar.getFontSettings(buffer().params(),
5793 parentpos).language()->rightToLeft();
5797 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5800 LASSERT(stidx <= enidx, return docstring());
5802 col_type const col1 = tabular.cellColumn(stidx);
5803 col_type const col2 = tabular.cellColumn(enidx);
5804 row_type const row1 = tabular.cellRow(stidx);
5805 row_type const row2 = tabular.cellRow(enidx);
5807 for (col_type col = col1; col <= col2; col++)
5808 for (row_type row = row1; row <= row2; row++) {
5813 retval += tabular.cellInset(row, col)->asString(intoInsets);
5819 void InsetTabular::getSelection(Cursor & cur,
5820 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5822 CursorSlice const & beg = cur.selBegin();
5823 CursorSlice const & end = cur.selEnd();
5824 cs = tabular.cellColumn(beg.idx());
5825 ce = tabular.cellColumn(end.idx());
5829 rs = tabular.cellRow(beg.idx());
5830 re = tabular.cellRow(end.idx());
5836 Text * InsetTabular::getText(int idx) const
5838 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5842 void InsetTabular::setChange(Change const & change)
5844 for (idx_type idx = 0; idx < nargs(); ++idx)
5845 cell(idx)->setChange(change);
5849 void InsetTabular::acceptChanges()
5851 for (idx_type idx = 0; idx < nargs(); ++idx)
5852 cell(idx)->acceptChanges();
5856 void InsetTabular::rejectChanges()
5858 for (idx_type idx = 0; idx < nargs(); ++idx)
5859 cell(idx)->rejectChanges();
5863 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5865 return tabular.getPWidth(cell).zero();
5869 bool InsetTabular::forcePlainLayout(idx_type cell) const
5871 return !tabular.getPWidth(cell).zero();
5875 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5878 if (buf.length() <= 0)
5883 col_type maxCols = 1;
5884 size_t const len = buf.length();
5888 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5896 maxCols = max(cols, maxCols);
5902 maxCols = max(cols, maxCols);
5908 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5909 loctab = paste_tabular.get();
5911 dirtyTabularStack(true);
5914 cell = bv.cursor().idx();
5915 ocol = tabular.cellColumn(cell);
5916 row = tabular.cellRow(cell);
5920 idx_type const cells = loctab->numberofcells;
5923 rows = loctab->nrows();
5924 col_type const columns = loctab->ncols();
5926 while (cell < cells && p < len && row < rows &&
5927 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5933 // we can only set this if we are not too far right
5934 if (cols < columns) {
5935 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5936 Font const font = bv.textMetrics(&inset->text()).
5938 inset->setText(buf.substr(op, p - op), font,
5939 buffer().params().trackChanges);
5945 // we can only set this if we are not too far right
5946 if (cols < columns) {
5947 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5948 Font const font = bv.textMetrics(&inset->text()).
5950 inset->setText(buf.substr(op, p - op), font,
5951 buffer().params().trackChanges);
5956 cell = loctab->cellIndex(row, cols);
5962 // check for the last cell if there is no trailing '\n'
5963 if (cell < cells && op < len) {
5964 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5965 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5966 inset->setText(buf.substr(op, len - op), font,
5967 buffer().params().trackChanges);
5973 void InsetTabular::addPreview(DocIterator const & inset_pos,
5974 PreviewLoader & loader) const
5976 DocIterator cell_pos = inset_pos;
5978 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5979 for (row_type r = 0; r < tabular.nrows(); ++r) {
5980 for (col_type c = 0; c < tabular.ncols(); ++c) {
5981 cell_pos.top().idx() = tabular.cellIndex(r, c);
5982 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5988 bool InsetTabular::completionSupported(Cursor const & cur) const
5990 Cursor const & bvCur = cur.bv().cursor();
5991 if (&bvCur.inset() != this)
5993 return cur.text()->completionSupported(cur);
5997 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5999 return completionSupported(cur);
6003 bool InsetTabular::automaticInlineCompletion() const
6005 return lyxrc.completion_inline_text;
6009 bool InsetTabular::automaticPopupCompletion() const
6011 return lyxrc.completion_popup_text;
6015 bool InsetTabular::showCompletionCursor() const
6017 return lyxrc.completion_cursor_text;
6021 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6023 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6027 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6029 if (!completionSupported(cur))
6031 return cur.text()->completionPrefix(cur);
6035 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6037 if (!completionSupported(cur))
6040 return cur.text()->insertCompletion(cur, s, finished);
6044 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6045 Dimension & dim) const
6047 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6048 tm.completionPosAndDim(cur, x, y, dim);
6052 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6054 istringstream data(in);
6056 lex.setStream(data);
6063 if (!lex || token != "tabular") {
6064 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6068 // This is part of the inset proper that is usually swallowed
6069 // by Buffer::readInset
6071 if (!lex || token != "Tabular") {
6072 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6080 string InsetTabular::params2string(InsetTabular const & inset)
6083 data << "tabular" << ' ';
6085 data << "\\end_inset\n";