2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
169 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
170 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
172 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
173 { Tabular::SET_BOOKTABS, "set-booktabs", false },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
175 { Tabular::SET_TOP_SPACE, "set-top-space", true },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
178 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
179 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
180 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
181 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
182 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
183 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
184 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
185 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
186 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
187 { Tabular::LAST_ACTION, "", false }
192 string const write_attribute(string const & name, T const & t)
194 string const s = tostr(t);
195 return s.empty() ? s : " " + name + "=\"" + s + "\"";
199 string const write_attribute(string const & name, string const & t)
201 return t.empty() ? t : " " + name + "=\"" + t + "\"";
206 string const write_attribute(string const & name, docstring const & t)
208 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
213 string const write_attribute(string const & name, bool const & b)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return b ? write_attribute(name, convert<string>(b)) : string();
222 string const write_attribute(string const & name, int const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Tabular::idx_type const & i)
233 // we write only true attribute values so we remove a bit of the
234 // file format bloat for tabulars.
235 return i ? write_attribute(name, convert<string>(i)) : string();
240 string const write_attribute(string const & name, Length const & value)
242 // we write only the value if we really have one same reson as above.
243 return value.zero() ? string() : write_attribute(name, value.asString());
247 string const tostr(LyXAlignment const & num)
252 case LYX_ALIGN_BLOCK:
256 case LYX_ALIGN_CENTER:
258 case LYX_ALIGN_RIGHT:
260 case LYX_ALIGN_LAYOUT:
262 case LYX_ALIGN_SPECIAL:
264 case LYX_ALIGN_DECIMAL:
271 string const tostr(Tabular::HAlignment const & num)
274 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
276 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
278 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
285 string const tostr(Tabular::VAlignment const & num)
288 case Tabular::LYX_VALIGN_TOP:
290 case Tabular::LYX_VALIGN_MIDDLE:
292 case Tabular::LYX_VALIGN_BOTTOM:
299 string const tostr(Tabular::BoxType const & num)
302 case Tabular::BOX_NONE:
304 case Tabular::BOX_PARBOX:
306 case Tabular::BOX_MINIPAGE:
313 // I would have liked a fromstr template a lot better. (Lgb)
314 bool string2type(string const str, LyXAlignment & num)
317 num = LYX_ALIGN_NONE;
318 else if (str == "block")
319 num = LYX_ALIGN_BLOCK;
320 else if (str == "left")
321 num = LYX_ALIGN_LEFT;
322 else if (str == "center")
323 num = LYX_ALIGN_CENTER;
324 else if (str == "right")
325 num = LYX_ALIGN_RIGHT;
326 else if (str == "decimal")
327 num = LYX_ALIGN_DECIMAL;
334 bool string2type(string const str, Tabular::HAlignment & num)
337 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
338 else if (str == "center" )
339 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
340 else if (str == "right")
341 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
348 bool string2type(string const str, Tabular::VAlignment & num)
351 num = Tabular::LYX_VALIGN_TOP;
352 else if (str == "middle" )
353 num = Tabular::LYX_VALIGN_MIDDLE;
354 else if (str == "bottom")
355 num = Tabular::LYX_VALIGN_BOTTOM;
362 bool string2type(string const str, Tabular::BoxType & num)
365 num = Tabular::BOX_NONE;
366 else if (str == "parbox")
367 num = Tabular::BOX_PARBOX;
368 else if (str == "minipage")
369 num = Tabular::BOX_MINIPAGE;
376 bool string2type(string const str, bool & num)
380 else if (str == "false")
388 bool getTokenValue(string const & str, char const * token, string & ret)
391 size_t token_length = strlen(token);
392 size_t pos = str.find(token);
394 if (pos == string::npos || pos + token_length + 1 >= str.length()
395 || str[pos + token_length] != '=')
397 pos += token_length + 1;
399 if (ch != '"' && ch != '\'') { // only read till next space
403 while (pos < str.length() - 1 && str[++pos] != ch)
410 bool getTokenValue(string const & str, char const * token, docstring & ret)
413 bool const success = getTokenValue(str, token, tmp);
414 ret = from_utf8(tmp);
419 bool getTokenValue(string const & str, char const * token, int & num)
423 if (!getTokenValue(str, token, tmp))
425 num = convert<int>(tmp);
430 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
433 return getTokenValue(str, token, tmp) && string2type(tmp, num);
437 bool getTokenValue(string const & str, char const * token,
438 Tabular::HAlignment & num)
441 return getTokenValue(str, token, tmp) && string2type(tmp, num);
445 bool getTokenValue(string const & str, char const * token,
446 Tabular::VAlignment & num)
449 return getTokenValue(str, token, tmp) && string2type(tmp, num);
453 bool getTokenValue(string const & str, char const * token,
454 Tabular::BoxType & num)
457 return getTokenValue(str, token, tmp) && string2type(tmp, num);
461 bool getTokenValue(string const & str, char const * token, bool & flag)
463 // set the flag always to false as this should be the default for bools
464 // not in the file-format.
467 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
471 bool getTokenValue(string const & str, char const * token, Length & len)
473 // set the length to be zero() as default as this it should be if not
474 // in the file format.
477 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
481 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
486 if (!getTokenValue(str, token, tmp))
488 if (tmp == "default") {
492 return isValidLength(tmp, &len);
496 void l_getline(istream & is, string & str)
499 while (str.empty()) {
501 if (!str.empty() && str[str.length() - 1] == '\r')
502 str.erase(str.length() - 1);
509 string const featureAsString(Tabular::Feature action)
511 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
512 if (tabularFeature[i].action == action)
513 return tabularFeature[i].feature;
519 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
521 InsetTableCell tail = InsetTableCell(head);
523 DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
524 for (; dit; dit.forwardChar())
525 if (dit.inTexted() && dit.depth()==1
526 && dit.paragraph().find(align_d, false, false, dit.pos()))
529 pit_type const psize = head.paragraphs().front().size();
532 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
533 tail.paragraphs().front().eraseChars(0,
534 dit.pos() < psize ? dit.pos() + 1 : psize, false);
541 /////////////////////////////////////////////////////////////////////
545 /////////////////////////////////////////////////////////////////////
548 Tabular::CellData::CellData(Buffer * buf)
551 multicolumn(Tabular::CELL_NORMAL),
552 multirow(Tabular::CELL_NORMAL),
553 alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
564 inset(new InsetTableCell(buf))
566 inset->setBuffer(*buf);
570 Tabular::CellData::CellData(CellData const & cs)
573 multicolumn(cs.multicolumn),
574 multirow(cs.multirow),
575 mroffset(cs.mroffset),
576 alignment(cs.alignment),
577 valignment(cs.valignment),
578 decimal_hoffset(cs.decimal_hoffset),
579 decimal_width(cs.decimal_width),
581 top_line(cs.top_line),
582 bottom_line(cs.bottom_line),
583 left_line(cs.left_line),
584 right_line(cs.right_line),
587 align_special(cs.align_special),
589 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
593 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
599 void Tabular::CellData::swap(CellData & rhs)
601 std::swap(cellno, rhs.cellno);
602 std::swap(width, rhs.width);
603 std::swap(multicolumn, rhs.multicolumn);
604 std::swap(multirow, rhs.multirow);
605 std::swap(mroffset, rhs.mroffset);
606 std::swap(alignment, rhs.alignment);
607 std::swap(valignment, rhs.valignment);
608 std::swap(decimal_hoffset, rhs.decimal_hoffset);
609 std::swap(decimal_width, rhs.decimal_width);
610 std::swap(voffset, rhs.voffset);
611 std::swap(top_line, rhs.top_line);
612 std::swap(bottom_line, rhs.bottom_line);
613 std::swap(left_line, rhs.left_line);
614 std::swap(right_line, rhs.right_line);
615 std::swap(usebox, rhs.usebox);
616 std::swap(rotate, rhs.rotate);
617 std::swap(align_special, rhs.align_special);
618 p_width.swap(rhs.p_width);
619 inset.swap(rhs.inset);
623 Tabular::RowData::RowData()
626 top_space_default(false),
627 bottom_space_default(false),
628 interline_space_default(false),
638 Tabular::ColumnData::ColumnData()
639 : alignment(LYX_ALIGN_CENTER),
640 valignment(LYX_VALIGN_TOP),
646 Tabular::ltType::ltType()
653 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
655 init(buffer, rows_arg, columns_arg);
659 void Tabular::setBuffer(Buffer & buffer)
662 for (row_type i = 0; i < nrows(); ++i)
663 for (col_type j = 0; j < ncols(); ++j)
664 cell_info[i][j].inset->setBuffer(*buffer_);
668 // activates all lines and sets all widths to 0
669 void Tabular::init(Buffer * buf, row_type rows_arg,
670 col_type columns_arg)
673 row_info = row_vector(rows_arg);
674 column_info = column_vector(columns_arg);
675 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
676 row_info.reserve(10);
677 column_info.reserve(10);
678 cell_info.reserve(100);
680 is_long_tabular = false;
681 tabular_valignment = LYX_VALIGN_MIDDLE;
682 tabular_width = Length();
683 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
685 use_booktabs = false;
686 // set silly default lines
687 for (row_type r = 0; r < nrows(); ++r)
688 for (col_type c = 0; c < ncols(); ++c) {
689 cell_info[r][c].inset->setBuffer(*buffer_);
690 cell_info[r][c].top_line = true;
691 cell_info[r][c].left_line = true;
692 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
693 cell_info[r][c].right_line = c == ncols() - 1;
698 void Tabular::appendRow(idx_type const cell)
700 row_type const row = cellRow(cell);
702 row_info.insert(row_info.begin() + row + 1, RowData());
703 row_info[row + 1] = row_info[row];
705 cell_info.insert(cell_info.begin() + row + 1,
706 cell_vector(ncols(), CellData(buffer_)));
707 for (col_type c = 0; c < ncols(); ++c) {
708 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
709 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
711 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
715 for (col_type c = 0; c < ncols(); ++c) {
716 if (isPartOfMultiRow(row, c))
718 // inherit line settings
719 idx_type const i = cellIndex(row + 1, c);
720 idx_type const j = cellIndex(row, c);
721 setLeftLine(i, leftLine(j));
722 setRightLine(i, rightLine(j));
723 setTopLine(i, topLine(j));
724 if (topLine(j) && bottomLine(j)) {
725 setBottomLine(i, true);
726 setBottomLine(j, false);
728 // mark track changes
729 if (buffer().params().trackChanges)
730 cellInfo(i).inset->setChange(Change(Change::INSERTED));
735 void Tabular::deleteRow(row_type const row)
737 // Not allowed to delete last row
741 for (col_type c = 0; c < ncols(); ++c) {
742 // Care about multirow cells
743 if (row + 1 < nrows() &&
744 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
745 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
746 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
749 row_info.erase(row_info.begin() + row);
750 cell_info.erase(cell_info.begin() + row);
755 void Tabular::copyRow(row_type const row)
757 row_info.insert(row_info.begin() + row, row_info[row]);
758 cell_info.insert(cell_info.begin() + row, cell_info[row]);
760 if (buffer().params().trackChanges)
761 for (col_type c = 0; c < ncols(); ++c)
762 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
768 void Tabular::appendColumn(idx_type const cell)
770 col_type const c = cellColumn(cell);
772 column_info.insert(column_info.begin() + c + 1, ColumnData());
773 column_info[c + 1] = column_info[c];
775 for (row_type r = 0; r < nrows(); ++r) {
776 cell_info[r].insert(cell_info[r].begin() + c + 1,
778 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
779 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
781 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
784 for (row_type r = 0; r < nrows(); ++r) {
785 // inherit line settings
786 idx_type const i = cellIndex(r, c + 1);
787 idx_type const j = cellIndex(r, c);
788 setBottomLine(i, bottomLine(j));
789 setTopLine(i, topLine(j));
790 setLeftLine(i, leftLine(j));
791 if (rightLine(j) && rightLine(j)) {
792 setRightLine(i, true);
793 setRightLine(j, false);
795 if (buffer().params().trackChanges)
796 cellInfo(i).inset->setChange(Change(Change::INSERTED));
801 void Tabular::deleteColumn(col_type const col)
803 // Not allowed to delete last column
807 for (row_type r = 0; r < nrows(); ++r) {
808 // Care about multicolumn cells
809 if (col + 1 < ncols() &&
810 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
811 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
812 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
814 cell_info[r].erase(cell_info[r].begin() + col);
816 column_info.erase(column_info.begin() + col);
821 void Tabular::copyColumn(col_type const col)
823 BufferParams const & bp = buffer().params();
824 column_info.insert(column_info.begin() + col, column_info[col]);
826 for (row_type r = 0; r < nrows(); ++r) {
827 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
829 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
835 void Tabular::updateIndexes()
840 for (row_type row = 0; row < nrows(); ++row)
841 for (col_type column = 0; column < ncols(); ++column) {
842 if (!isPartOfMultiColumn(row, column)
843 && !isPartOfMultiRow(row, column))
845 if (isPartOfMultiRow(row, column))
846 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
848 cell_info[row][column].cellno = numberofcells - 1;
851 rowofcell.resize(numberofcells);
852 columnofcell.resize(numberofcells);
854 // reset column and row of cells and update their width and alignment
855 for (row_type row = 0; row < nrows(); ++row)
856 for (col_type column = 0; column < ncols(); ++column) {
857 if (isPartOfMultiColumn(row, column))
859 // columnofcell needs to be called before setting width and aligment
860 // multirow cells inherit the width from the column width
861 if (!isPartOfMultiRow(row, column)) {
862 columnofcell[i] = column;
865 setFixedWidth(row, column);
866 if (isPartOfMultiRow(row, column))
868 cell_info[row][column].inset->setContentAlignment(
869 getAlignment(cellIndex(row, column)));
875 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
878 for (col_type c = 0; c < ncols(); ++c)
879 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
885 bool Tabular::topLine(idx_type const cell) const
887 return cellInfo(cell).top_line;
891 bool Tabular::bottomLine(idx_type const cell) const
893 return cellInfo(cell).bottom_line;
897 bool Tabular::leftLine(idx_type cell) const
901 return cellInfo(cell).left_line;
905 bool Tabular::rightLine(idx_type cell) const
909 return cellInfo(cell).right_line;
913 int Tabular::interRowSpace(row_type row) const
915 if (!row || row >= nrows())
918 int const interline_space = row_info[row - 1].interline_space_default ?
920 row_info[row - 1].interline_space.inPixels(width());
921 if (rowTopLine(row) && rowBottomLine(row - 1))
922 return interline_space + WIDTH_OF_LINE;
923 return interline_space;
927 int Tabular::interColumnSpace(idx_type cell) const
929 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
930 if (rightLine(cell) && nextcol < ncols()
931 && leftLine(cellIndex(cellRow(cell), nextcol)))
932 return WIDTH_OF_LINE;
937 int Tabular::cellWidth(idx_type cell) const
940 col_type const span = columnSpan(cell);
941 col_type const col = cellColumn(cell);
942 for(col_type c = col; c < col + span ; ++c)
943 w += column_info[c].width;
948 int Tabular::cellHeight(idx_type cell) const
950 row_type const span = rowSpan(cell);
951 row_type const row = cellRow(cell);
953 for(row_type r = row; r < row + span ; ++r) {
954 h += rowAscent(r) + rowDescent(r);
955 if (r != row + span - 1)
956 h += interRowSpace(r + 1);
963 bool Tabular::updateColumnWidths()
965 vector<int> max_dwidth(ncols(), 0);
966 for(col_type c = 0; c < ncols(); ++c)
967 for(row_type r = 0; r < nrows(); ++r) {
968 idx_type const i = cellIndex(r, c);
969 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
970 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
974 // for each col get max of single col cells
975 for(col_type c = 0; c < ncols(); ++c) {
977 for(row_type r = 0; r < nrows(); ++r) {
978 idx_type const i = cellIndex(r, c);
979 if (columnSpan(i) == 1) {
980 if (getAlignment(i) == LYX_ALIGN_DECIMAL
981 && cell_info[r][c].decimal_width!=0)
982 new_width = max(new_width, cellInfo(i).width
983 + max_dwidth[c] - cellInfo(i).decimal_width);
985 new_width = max(new_width, cellInfo(i).width);
989 if (column_info[c].width != new_width) {
990 column_info[c].width = new_width;
994 // update col widths to fit merged cells
995 for(col_type c = 0; c < ncols(); ++c)
996 for(row_type r = 0; r < nrows(); ++r) {
997 idx_type const i = cellIndex(r, c);
998 int const span = columnSpan(i);
999 if (span == 1 || c > cellColumn(i))
1003 for(col_type j = c; j < c + span ; ++j)
1004 old_width += column_info[j].width;
1006 if (cellInfo(i).width > old_width) {
1007 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1016 int Tabular::width() const
1019 for (col_type c = 0; c < ncols(); ++c)
1020 width += column_info[c].width;
1025 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1028 col_type const col = cellColumn(cell);
1029 // set alignment for the whole row if we are not in a multicolumn cell,
1030 // exclude possible multicolumn cells in the row
1031 if (!isMultiColumn(cell)) {
1032 for (row_type r = 0; r < nrows(); ++r) {
1033 // only if the column has no width the multirow inherits the
1034 // alignment of the column, otherwise it is left aligned
1035 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1036 && !isMultiColumn(cellIndex(r, col))) {
1037 cell_info[r][col].alignment = align;
1038 cell_info[r][col].inset->setContentAlignment(align);
1040 if ((isMultiRow(cellIndex(r, col)) && has_width)
1041 && !isMultiColumn(cellIndex(r, col))) {
1042 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1043 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1046 column_info[col].alignment = align;
1047 docstring & dpoint = column_info[col].decimal_point;
1048 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1049 dpoint = from_utf8(lyxrc.default_decimal_point);
1051 cellInfo(cell).alignment = align;
1052 cellInset(cell).get()->setContentAlignment(align);
1057 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1060 if (!isMultiColumn(cell) || onlycolumn)
1061 column_info[cellColumn(cell)].valignment = align;
1063 cellInfo(cell).valignment = align;
1070 * Allow line and paragraph breaks for fixed width cells or disallow them,
1071 * merge cell paragraphs and reset layout to standard for variable width
1074 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1076 inset->setAutoBreakRows(fixedWidth);
1077 inset->toggleFixedWidth(fixedWidth);
1081 // merge all paragraphs to one
1082 BufferParams const & bp = cur.bv().buffer().params();
1083 while (inset->paragraphs().size() > 1)
1084 mergeParagraph(bp, inset->paragraphs(), 0);
1088 // undo information has already been recorded
1089 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1090 bp.documentClass().plainLayoutName());
1097 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1098 Length const & width)
1100 col_type const c = cellColumn(cell);
1102 column_info[c].p_width = width;
1103 // reset the vertical alignment to top if the fixed with
1104 // is removed or zero because only fixed width columns can
1105 // have a vertical alignment
1106 if (column_info[c].p_width.zero())
1107 column_info[c].valignment = LYX_VALIGN_TOP;
1108 for (row_type r = 0; r < nrows(); ++r) {
1109 idx_type const cell = cellIndex(r, c);
1110 // because of multicolumns
1111 toggleFixedWidth(cur, cellInset(cell).get(),
1112 !getPWidth(cell).zero());
1114 // cur paragraph can become invalid after paragraphs were merged
1115 if (cur.pit() > cur.lastpit())
1116 cur.pit() = cur.lastpit();
1117 // cur position can become invalid after newlines were removed
1118 if (cur.pos() > cur.lastpos())
1119 cur.pos() = cur.lastpos();
1123 bool Tabular::setFixedWidth(row_type r, col_type c)
1125 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1126 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1127 || (multicol && !cell_info[r][c].p_width.zero());
1128 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1133 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1134 Length const & width)
1136 if (!isMultiColumn(cell))
1139 cellInfo(cell).p_width = width;
1140 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1141 // cur paragraph can become invalid after paragraphs were merged
1142 if (cur.pit() > cur.lastpit())
1143 cur.pit() = cur.lastpit();
1144 // cur position can become invalid after newlines were removed
1145 if (cur.pos() > cur.lastpos())
1146 cur.pos() = cur.lastpos();
1151 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1153 cellInfo(cell).mroffset = mroffset;
1158 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1159 Tabular::Feature what)
1161 if (what == SET_SPECIAL_MULTICOLUMN)
1162 cellInfo(cell).align_special = special;
1164 column_info[cellColumn(cell)].align_special = special;
1168 void Tabular::setTopLine(idx_type i, bool line)
1170 cellInfo(i).top_line = line;
1174 void Tabular::setBottomLine(idx_type i, bool line)
1176 cellInfo(i).bottom_line = line;
1180 void Tabular::setLeftLine(idx_type cell, bool line)
1182 cellInfo(cell).left_line = line;
1186 void Tabular::setRightLine(idx_type cell, bool line)
1188 cellInfo(cell).right_line = line;
1191 bool Tabular::rowTopLine(row_type r) const
1193 bool all_rows_set = true;
1194 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1195 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1196 return all_rows_set;
1200 bool Tabular::rowBottomLine(row_type r) const
1202 bool all_rows_set = true;
1203 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1204 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1205 return all_rows_set;
1209 bool Tabular::columnLeftLine(col_type c) const
1216 for (row_type r = 0; r < nrows(); ++r) {
1217 idx_type const i = cellIndex(r, c);
1218 if (c == cellColumn(i)) {
1220 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1221 if (cellInfo(i).left_line || right)
1225 return 2 * nrows_left >= total;
1229 bool Tabular::columnRightLine(col_type c) const
1234 int nrows_right = 0;
1236 for (row_type r = 0; r < nrows(); ++r) {
1237 idx_type i = cellIndex(r, c);
1238 if (c == cellColumn(i) + columnSpan(i) - 1) {
1240 bool left = (c + 1 < ncols()
1241 && cellInfo(cellIndex(r, c + 1)).left_line)
1242 || c + 1 == ncols();
1243 if (cellInfo(i).right_line && left)
1247 return 2 * nrows_right >= total;
1251 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1253 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1254 return cellInfo(cell).alignment;
1256 return column_info[cellColumn(cell)].alignment;
1261 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1263 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1264 return cellInfo(cell).valignment;
1265 return column_info[cellColumn(cell)].valignment;
1269 Length const Tabular::getPWidth(idx_type cell) const
1271 if (isMultiColumn(cell))
1272 return cellInfo(cell).p_width;
1273 return column_info[cellColumn(cell)].p_width;
1277 Length const Tabular::getMROffset(idx_type cell) const
1279 return cellInfo(cell).mroffset;
1283 int Tabular::textHOffset(idx_type cell) const
1285 // the LaTeX Way :-(
1286 int x = WIDTH_OF_LINE;
1288 int const w = cellWidth(cell) - cellInfo(cell).width;
1290 switch (getAlignment(cell)) {
1291 case LYX_ALIGN_CENTER:
1294 case LYX_ALIGN_RIGHT:
1297 case LYX_ALIGN_DECIMAL: {
1298 // we center when no decimal point
1299 if (cellInfo(cell).decimal_width == 0) {
1303 col_type const c = cellColumn(cell);
1304 int max_dhoffset = 0;
1305 for(row_type r = 0; r < row_info.size() ; ++r) {
1306 idx_type const i = cellIndex(r, c);
1307 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1308 && cellInfo(i).decimal_width != 0)
1309 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1311 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1314 // LYX_ALIGN_LEFT: nothing :-)
1322 int Tabular::textVOffset(idx_type cell) const
1324 int voffset = cellInfo(cell).voffset;
1325 if (isMultiRow(cell)) {
1326 row_type const row = cellRow(cell);
1327 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1333 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1336 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1338 return cell_info[row][c].cellno;
1342 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1344 col_type c = ncols() - 1;
1345 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1346 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN)
1348 return cell_info[row][c].cellno;
1352 Tabular::row_type Tabular::cellRow(idx_type cell) const
1354 if (cell >= numberofcells)
1358 return rowofcell[cell];
1362 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1364 if (cell >= numberofcells)
1368 return columnofcell[cell];
1372 void Tabular::write(ostream & os) const
1376 << write_attribute("version", 3)
1377 << write_attribute("rows", nrows())
1378 << write_attribute("columns", ncols())
1380 // global longtable options
1382 << write_attribute("rotate", rotate)
1383 << write_attribute("booktabs", use_booktabs)
1384 << write_attribute("islongtable", is_long_tabular)
1385 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1386 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1387 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1388 << write_attribute("headTopDL", endhead.topDL)
1389 << write_attribute("headBottomDL", endhead.bottomDL)
1390 << write_attribute("footTopDL", endfoot.topDL)
1391 << write_attribute("footBottomDL", endfoot.bottomDL)
1392 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1393 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1394 << write_attribute("lastFootEmpty", endlastfoot.empty);
1395 // longtables cannot be aligned vertically
1396 if (!is_long_tabular) {
1397 os << write_attribute("tabularvalignment", tabular_valignment);
1398 os << write_attribute("tabularwidth", tabular_width);
1400 if (is_long_tabular)
1401 os << write_attribute("longtabularalignment",
1402 longtabular_alignment);
1404 for (col_type c = 0; c < ncols(); ++c) {
1406 << write_attribute("alignment", column_info[c].alignment);
1407 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1408 os << write_attribute("decimal_point", column_info[c].decimal_point);
1409 os << write_attribute("valignment", column_info[c].valignment)
1410 << write_attribute("width", column_info[c].p_width.asString())
1411 << write_attribute("special", column_info[c].align_special)
1414 for (row_type r = 0; r < nrows(); ++r) {
1415 static const string def("default");
1417 if (row_info[r].top_space_default)
1418 os << write_attribute("topspace", def);
1420 os << write_attribute("topspace", row_info[r].top_space);
1421 if (row_info[r].bottom_space_default)
1422 os << write_attribute("bottomspace", def);
1424 os << write_attribute("bottomspace", row_info[r].bottom_space);
1425 if (row_info[r].interline_space_default)
1426 os << write_attribute("interlinespace", def);
1428 os << write_attribute("interlinespace", row_info[r].interline_space);
1429 os << write_attribute("endhead", row_info[r].endhead)
1430 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1431 << write_attribute("endfoot", row_info[r].endfoot)
1432 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1433 << write_attribute("newpage", row_info[r].newpage)
1434 << write_attribute("caption", row_info[r].caption)
1436 for (col_type c = 0; c < ncols(); ++c) {
1438 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1439 << write_attribute("multirow", cell_info[r][c].multirow)
1440 << write_attribute("mroffset", cell_info[r][c].mroffset)
1441 << write_attribute("alignment", cell_info[r][c].alignment)
1442 << write_attribute("valignment", cell_info[r][c].valignment)
1443 << write_attribute("topline", cell_info[r][c].top_line)
1444 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1445 << write_attribute("leftline", cell_info[r][c].left_line)
1446 << write_attribute("rightline", cell_info[r][c].right_line)
1447 << write_attribute("rotate", cell_info[r][c].rotate)
1448 << write_attribute("usebox", cell_info[r][c].usebox)
1449 << write_attribute("width", cell_info[r][c].p_width)
1450 << write_attribute("special", cell_info[r][c].align_special)
1452 os << "\\begin_inset ";
1453 cell_info[r][c].inset->write(os);
1454 os << "\n\\end_inset\n"
1459 os << "</lyxtabular>\n";
1463 void Tabular::read(Lexer & lex)
1466 istream & is = lex.getStream();
1468 l_getline(is, line);
1469 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1470 LASSERT(false, /**/);
1475 if (!getTokenValue(line, "version", version))
1477 LASSERT(version >= 2, /**/);
1480 if (!getTokenValue(line, "rows", rows_arg))
1483 if (!getTokenValue(line, "columns", columns_arg))
1485 init(buffer_, rows_arg, columns_arg);
1486 l_getline(is, line);
1487 if (!prefixIs(line, "<features")) {
1488 lyxerr << "Wrong tabular format (expected <features ...> got"
1489 << line << ')' << endl;
1492 getTokenValue(line, "rotate", rotate);
1493 getTokenValue(line, "booktabs", use_booktabs);
1494 getTokenValue(line, "islongtable", is_long_tabular);
1495 getTokenValue(line, "tabularvalignment", tabular_valignment);
1496 getTokenValue(line, "tabularwidth", tabular_width);
1497 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1498 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1499 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1500 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1501 getTokenValue(line, "headTopDL", endhead.topDL);
1502 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1503 getTokenValue(line, "footTopDL", endfoot.topDL);
1504 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1505 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1506 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1507 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1509 for (col_type c = 0; c < ncols(); ++c) {
1511 if (!prefixIs(line,"<column")) {
1512 lyxerr << "Wrong tabular format (expected <column ...> got"
1513 << line << ')' << endl;
1516 getTokenValue(line, "alignment", column_info[c].alignment);
1517 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1518 getTokenValue(line, "valignment", column_info[c].valignment);
1519 getTokenValue(line, "width", column_info[c].p_width);
1520 getTokenValue(line, "special", column_info[c].align_special);
1523 for (row_type i = 0; i < nrows(); ++i) {
1524 l_getline(is, line);
1525 if (!prefixIs(line, "<row")) {
1526 lyxerr << "Wrong tabular format (expected <row ...> got"
1527 << line << ')' << endl;
1530 getTokenValue(line, "topspace", row_info[i].top_space,
1531 row_info[i].top_space_default);
1532 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1533 row_info[i].bottom_space_default);
1534 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1535 row_info[i].interline_space_default);
1536 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1537 getTokenValue(line, "endhead", row_info[i].endhead);
1538 getTokenValue(line, "endfoot", row_info[i].endfoot);
1539 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1540 getTokenValue(line, "newpage", row_info[i].newpage);
1541 getTokenValue(line, "caption", row_info[i].caption);
1542 for (col_type j = 0; j < ncols(); ++j) {
1543 l_getline(is, line);
1544 if (!prefixIs(line, "<cell")) {
1545 lyxerr << "Wrong tabular format (expected <cell ...> got"
1546 << line << ')' << endl;
1549 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1550 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1551 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1552 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1553 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1554 getTokenValue(line, "topline", cell_info[i][j].top_line);
1555 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1556 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1557 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1558 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1559 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1560 getTokenValue(line, "width", cell_info[i][j].p_width);
1562 getTokenValue(line, "special", cell_info[i][j].align_special);
1563 l_getline(is, line);
1564 if (prefixIs(line, "\\begin_inset")) {
1565 cell_info[i][j].inset->setBuffer(*buffer_);
1566 cell_info[i][j].inset->read(lex);
1567 l_getline(is, line);
1569 if (!prefixIs(line, "</cell>")) {
1570 lyxerr << "Wrong tabular format (expected </cell> got"
1571 << line << ')' << endl;
1575 l_getline(is, line);
1576 if (!prefixIs(line, "</row>")) {
1577 lyxerr << "Wrong tabular format (expected </row> got"
1578 << line << ')' << endl;
1582 while (!prefixIs(line, "</lyxtabular>")) {
1583 l_getline(is, line);
1589 bool Tabular::isMultiColumn(idx_type cell) const
1591 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1592 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1596 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1598 return cell_info[cellRow(cell)][cellColumn(cell)];
1602 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1604 idx_type const col = cellColumn(cell);
1605 idx_type const row = cellRow(cell);
1606 for (idx_type i = 0; i < number; ++i)
1607 unsetMultiRow(cellIndex(row, col + i));
1609 // unsetting of multirow may have invalidated cell index
1610 cell = cellIndex(row, col);
1611 CellData & cs = cellInfo(cell);
1612 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1613 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1614 cs.alignment = column_info[col].alignment;
1616 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1618 for (idx_type i = 1; i < number; ++i) {
1619 CellData & cs1 = cellInfo(cell + i);
1620 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1621 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1629 bool Tabular::isMultiRow(idx_type cell) const
1631 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1632 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1636 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1638 idx_type const col = cellColumn(cell);
1639 idx_type const row = cellRow(cell);
1640 for (idx_type i = 0; i < number; ++i)
1641 unsetMultiColumn(cellIndex(row + i, col));
1643 // unsetting of multirow may have invalidated cell index
1644 cell = cellIndex(row, col);
1645 CellData & cs = cellInfo(cell);
1646 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1647 cs.valignment = LYX_VALIGN_MIDDLE;
1648 // the horizontal alignment of multirow cells can only
1649 // be changed for the whole table row,
1650 // support changing this only for the multirow cell can be done via
1652 // this feature would be a fileformat change
1653 // until LyX supports this, use the deault alignment of multirow
1655 cs.alignment = LYX_ALIGN_LEFT;
1657 // set the bottom row of the last selected cell
1658 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1660 for (idx_type i = 1; i < number; ++i) {
1661 CellData & cs1 = cell_info[row + i][col];
1662 cs1.multirow = CELL_PART_OF_MULTIROW;
1663 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1671 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1673 row_type const row = cellRow(cell);
1674 col_type const col = cellColumn(cell);
1676 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1683 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1685 col_type const column = cellColumn(cell);
1686 col_type row = cellRow(cell) + 1;
1687 while (row < nrows() && isPartOfMultiRow(row, column))
1690 return row - cellRow(cell);
1694 void Tabular::unsetMultiColumn(idx_type cell)
1696 if (!isMultiColumn(cell))
1699 row_type const row = cellRow(cell);
1700 col_type const col = cellColumn(cell);
1701 row_type const span = columnSpan(cell);
1702 for (col_type c = 0; c < span; ++c) {
1703 // in the table dialog the lines are set in every case
1704 // when unsetting a multicolumn this leads to an additional right
1705 // line for every cell that was part of the former multicolumn cell,
1706 // except if the cell is in the last column
1707 // therefore remove this line
1708 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1709 && (col + c) < (col + span - 1))
1710 cell_info[row][col + c].right_line = false;
1711 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1717 void Tabular::unsetMultiRow(idx_type cell)
1719 if (!isMultiRow(cell))
1722 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1723 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1724 row_type const row = cellRow(cell);
1725 col_type const col = cellColumn(cell);
1726 row_type const span = rowSpan(cell);
1727 for (row_type r = 0; r < span; ++r)
1728 cell_info[row + r][col].multirow = CELL_NORMAL;
1733 void Tabular::setRotateCell(idx_type cell, bool flag)
1735 cellInfo(cell).rotate = flag;
1739 bool Tabular::getRotateCell(idx_type cell) const
1741 return cellInfo(cell).rotate;
1745 bool Tabular::needRotating() const
1749 for (row_type r = 0; r < nrows(); ++r)
1750 for (col_type c = 0; c < ncols(); ++c)
1751 if (cell_info[r][c].rotate)
1757 bool Tabular::isLastCell(idx_type cell) const
1759 if (cell + 1 < numberofcells)
1765 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1767 if (cellRow(cell) == 0)
1770 col_type const col = cellColumn(cell);
1771 row_type r = cellRow(cell) - 1;
1772 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1775 return cell_info[r][col].cellno;
1779 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1781 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1782 if (nextrow < nrows())
1783 return cell_info[nextrow][cellColumn(cell)].cellno;
1788 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1790 LASSERT(column != npos && column < ncols()
1791 && row != npos && row < nrows(), /**/);
1792 return cell_info[row][column].cellno;
1796 void Tabular::setUsebox(idx_type cell, BoxType type)
1798 cellInfo(cell).usebox = type;
1802 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1803 // adds line breaks, see bug 4886.
1804 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1806 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1807 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1809 if (cellInfo(cell).usebox > 1)
1810 return cellInfo(cell).usebox;
1811 return useParbox(cell);
1816 // This are functions used for the longtable support
1818 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1824 row_info[row].endfirsthead = flag;
1828 row_info[row].endhead = flag;
1833 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1836 hd.set = haveLTHead();
1837 return row_info[row].endhead;
1841 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1844 hd.set = haveLTFirstHead();
1845 return row_info[row].endfirsthead;
1849 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1855 row_info[row].endlastfoot = flag;
1859 row_info[row].endfoot = flag;
1864 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1867 fd.set = haveLTFoot();
1868 return row_info[row].endfoot;
1872 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1875 fd.set = haveLTLastFoot();
1876 return row_info[row].endlastfoot;
1880 void Tabular::setLTNewPage(row_type row, bool what)
1882 row_info[row].newpage = what;
1886 bool Tabular::getLTNewPage(row_type row) const
1888 return row_info[row].newpage;
1892 bool Tabular::haveLTHead() const
1894 if (!is_long_tabular)
1896 for (row_type i = 0; i < nrows(); ++i)
1897 if (row_info[i].endhead)
1903 bool Tabular::haveLTFirstHead() const
1905 if (!is_long_tabular || endfirsthead.empty)
1907 for (row_type r = 0; r < nrows(); ++r)
1908 if (row_info[r].endfirsthead)
1914 bool Tabular::haveLTFoot() const
1916 if (!is_long_tabular)
1918 for (row_type r = 0; r < nrows(); ++r)
1919 if (row_info[r].endfoot)
1925 bool Tabular::haveLTLastFoot() const
1927 if (!is_long_tabular || endlastfoot.empty)
1929 for (row_type r = 0; r < nrows(); ++r)
1930 if (row_info[r].endlastfoot)
1936 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1938 idx_type i = getFirstCellInRow(row);
1940 setMultiColumn(i, numberOfCellsInRow(row));
1941 setTopLine(i, false);
1942 setBottomLine(i, false);
1943 setLeftLine(i, false);
1944 setRightLine(i, false);
1946 unsetMultiColumn(i);
1947 // When unsetting a caption row, also all existing
1948 // captions in this row must be dissolved.
1950 row_info[row].caption = what;
1955 bool Tabular::ltCaption(row_type row) const
1957 return row_info[row].caption;
1961 bool Tabular::haveLTCaption() const
1963 if (!is_long_tabular)
1965 for (row_type r = 0; r < nrows(); ++r)
1966 if (row_info[r].caption)
1972 // end longtable support functions
1974 void Tabular::setRowAscent(row_type row, int height)
1976 if (row >= nrows() || row_info[row].ascent == height)
1978 row_info[row].ascent = height;
1982 void Tabular::setRowDescent(row_type row, int height)
1984 if (row >= nrows() || row_info[row].descent == height)
1986 row_info[row].descent = height;
1990 int Tabular::rowAscent(row_type row) const
1992 LASSERT(row < nrows(), /**/);
1993 return row_info[row].ascent;
1997 int Tabular::rowDescent(row_type row) const
1999 LASSERT(row < nrows(), /**/);
2000 return row_info[row].descent;
2004 int Tabular::height() const
2007 for (row_type row = 0; row < nrows(); ++row)
2008 height += rowAscent(row) + rowDescent(row) +
2014 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2016 LASSERT(row < nrows(), /**/);
2017 LASSERT(column < ncols(), /**/);
2018 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2022 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2024 LASSERT(row < nrows(), /**/);
2025 LASSERT(column < ncols(), /**/);
2026 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2030 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2032 // we only output complete row lines and the 1st row here, the rest
2033 // is done in Tabular::TeXBottomHLine(...)
2035 // get for each column the topline (if any)
2036 vector<bool> topline;
2038 for (col_type c = 0; c < ncols(); ++c) {
2039 topline.push_back(topLine(cellIndex(row, c)));
2040 // If cell is part of a multirow and not the first cell of the
2041 // multirow, no line must be drawn.
2043 if (isMultiRow(cellIndex(row, c))
2044 && isMultiRow(cellIndex(row - 1, c)))
2050 // do nothing if empty first row, or incomplete row line after
2051 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2054 // only output complete row lines and the 1st row's clines
2055 if (nset == ncols()) {
2057 os << (row == 0 ? "\\toprule " : "\\midrule ");
2061 } else if (row == 0) {
2062 for (col_type c = 0; c < ncols(); ++c) {
2064 col_type offset = 0;
2065 for (col_type j = 0 ; j < c; ++j)
2066 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2069 //babel makes the "-" character an active one, so we have to suppress this here
2070 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2071 if (lang == "slovak" || lang == "czech")
2072 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2073 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2075 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2077 col_type cstart = c;
2078 for ( ; c < ncols() && topline[c]; ++c) {}
2080 for (col_type j = cstart ; j < c ; ++j)
2081 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2084 os << c + offset << "} ";
2092 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2094 // we output bottomlines of row r and the toplines of row r+1
2095 // if the latter do not span the whole tabular
2097 // get the bottomlines of row r, and toplines in next row
2098 bool lastrow = row == nrows() - 1;
2099 vector<bool> bottomline, topline;
2100 bool nextrowset = true;
2101 for (col_type c = 0; c < ncols(); ++c) {
2102 bottomline.push_back(bottomLine(cellIndex(row, c)));
2103 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2104 // If cell is part of a multirow and not the last or first cell of the
2105 // multirow, no line must be drawn.
2107 if (isMultiRow(cellIndex(row, c))
2108 && isMultiRow(cellIndex(row + 1, c))) {
2109 bottomline[c] = false;
2112 nextrowset &= topline[c];
2115 // combine this row's bottom lines and next row's toplines if necessary
2117 for (col_type c = 0; c < ncols(); ++c) {
2119 bottomline[c] = bottomline[c] || topline[c];
2124 // do nothing if empty, OR incomplete row line with a topline in next row
2125 if (nset == 0 || (nextrowset && nset != ncols()))
2128 if (nset == ncols()) {
2130 os << (lastrow ? "\\bottomrule" : "\\midrule");
2134 for (col_type c = 0; c < ncols(); ++c) {
2135 if (bottomline[c]) {
2136 col_type offset = 0;
2137 for (col_type j = 0 ; j < c; ++j)
2138 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2141 //babel makes the "-" character an active one, so we have to suppress this here
2142 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2143 if (lang == "slovak" || lang == "czech")
2144 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2145 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2147 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2149 col_type cstart = c;
2150 for ( ; c < ncols() && bottomline[c]; ++c) {}
2152 for (col_type j = cstart ; j < c ; ++j)
2153 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2156 os << c + offset << "} ";
2164 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2165 bool & ismulticol, bool & ismultirow) const
2167 row_type const r = cellRow(cell);
2168 if (is_long_tabular && row_info[r].caption)
2171 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2172 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2173 // figure out how to set the lines
2174 // we always set double lines to the right of the cell
2175 col_type const c = cellColumn(cell);
2176 col_type const nextcol = c + columnSpan(cell);
2177 bool colright = columnRightLine(c);
2178 bool colleft = columnLeftLine(c);
2179 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2180 bool nextcellleft = nextcol < ncols()
2181 && leftLine(cellIndex(r, nextcol));
2182 bool coldouble = colright && nextcolleft;
2183 bool celldouble = rightLine(cell) && nextcellleft;
2185 ismulticol = isMultiColumn(cell)
2186 || (c == 0 && colleft != leftLine(cell))
2187 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2188 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2189 || (coldouble != celldouble);
2191 // we center in multicol when no decimal point
2192 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2193 && (cellInfo(cell).decimal_width == 0));
2195 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2196 int latexcolspan = columnSpan(cell);
2197 for(col_type col = c; col < c + columnSpan(cell); ++col)
2198 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2202 os << "\\multicolumn{" << latexcolspan << "}{";
2203 if (c ==0 && leftLine(cell))
2205 if (!cellInfo(cell).align_special.empty()) {
2206 os << cellInfo(cell).align_special;
2208 if (!getPWidth(cell).zero()) {
2210 case LYX_ALIGN_LEFT:
2211 os << ">{\\raggedright}";
2213 case LYX_ALIGN_RIGHT:
2214 os << ">{\\raggedleft}";
2216 case LYX_ALIGN_CENTER:
2217 os << ">{\\centering}";
2223 case LYX_VALIGN_TOP:
2226 case LYX_VALIGN_MIDDLE:
2229 case LYX_VALIGN_BOTTOM:
2234 << from_ascii(getPWidth(cell).asLatexString())
2238 case LYX_ALIGN_LEFT:
2241 case LYX_ALIGN_RIGHT:
2248 } // end if else !getPWidth
2249 } // end if else !cellinfo_of_cell
2250 if (rightLine(cell) || nextcellleft)
2253 // add extra vertical line if we want a double one
2256 } // end if ismulticol
2258 // we only need code for the first multirow cell
2259 ismultirow = isMultiRow(cell);
2261 os << "\\multirow{" << rowSpan(cell) << "}{";
2262 if (!getPWidth(cell).zero())
2263 os << from_ascii(getPWidth(cell).asLatexString());
2265 // we need to set a default value
2268 if (!getMROffset(cell).zero())
2269 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2271 } // end if ismultirow
2273 if (getRotateCell(cell)) {
2274 os << "\\begin{sideways}\n";
2276 if (getUsebox(cell) == BOX_PARBOX) {
2279 case LYX_VALIGN_TOP:
2282 case LYX_VALIGN_MIDDLE:
2285 case LYX_VALIGN_BOTTOM:
2289 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2291 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2292 os << "\\begin{minipage}[";
2294 case LYX_VALIGN_TOP:
2297 case LYX_VALIGN_MIDDLE:
2300 case LYX_VALIGN_BOTTOM:
2304 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2310 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2311 bool ismulticol, bool ismultirow) const
2313 row_type const r = cellRow(cell);
2314 if (is_long_tabular && row_info[r].caption)
2318 if (getUsebox(cell) == BOX_PARBOX)
2320 else if (getUsebox(cell) == BOX_MINIPAGE)
2321 os << breakln << "\\end{minipage}";
2322 if (getRotateCell(cell))
2323 os << breakln << "\\end{sideways}";
2331 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2332 OutputParams const & runparams) const
2334 if (!is_long_tabular)
2338 // the caption must be output before the headers
2339 if (haveLTCaption()) {
2340 for (row_type r = 0; r < nrows(); ++r) {
2341 if (row_info[r].caption)
2342 TeXRow(os, r, runparams);
2345 // output first header info
2346 // first header must be output before the header, otherwise the
2347 // correct caption placement becomes really weird
2348 if (haveLTFirstHead()) {
2349 if (endfirsthead.topDL)
2351 for (row_type r = 0; r < nrows(); ++r) {
2352 if (row_info[r].endfirsthead)
2353 TeXRow(os, r, runparams);
2355 if (endfirsthead.bottomDL)
2357 os << "\\endfirsthead\n";
2359 // output header info
2361 if (endfirsthead.empty && !haveLTFirstHead())
2362 os << "\\endfirsthead\n";
2365 for (row_type r = 0; r < nrows(); ++r) {
2366 if (row_info[r].endhead)
2367 TeXRow(os, r, runparams);
2369 if (endhead.bottomDL)
2371 os << "\\endhead\n";
2373 // output footer info
2377 for (row_type r = 0; r < nrows(); ++r) {
2378 if (row_info[r].endfoot)
2379 TeXRow(os, r, runparams);
2381 if (endfoot.bottomDL)
2383 os << "\\endfoot\n";
2384 if (endlastfoot.empty && !haveLTLastFoot())
2385 os << "\\endlastfoot\n";
2387 // output lastfooter info
2388 if (haveLTLastFoot()) {
2389 if (endlastfoot.topDL)
2391 for (row_type r = 0; r < nrows(); ++r) {
2392 if (row_info[r].endlastfoot)
2393 TeXRow(os, r, runparams);
2395 if (endlastfoot.bottomDL)
2397 os << "\\endlastfoot\n";
2402 bool Tabular::isValidRow(row_type row) const
2404 if (!is_long_tabular)
2406 return !row_info[row].endhead && !row_info[row].endfirsthead
2407 && !row_info[row].endfoot && !row_info[row].endlastfoot
2408 && !row_info[row].caption;
2412 void Tabular::TeXRow(otexstream & os, row_type row,
2413 OutputParams const & runparams) const
2415 idx_type cell = cellIndex(row, 0);
2416 shared_ptr<InsetTableCell> inset = cellInset(cell);
2417 Paragraph const & par = inset->paragraphs().front();
2418 string const lang = par.getParLanguage(buffer().params())->lang();
2420 //output the top line
2421 TeXTopHLine(os, row, lang);
2423 if (row_info[row].top_space_default) {
2425 os << "\\addlinespace\n";
2427 os << "\\noalign{\\vskip\\doublerulesep}\n";
2428 } else if(!row_info[row].top_space.zero()) {
2430 os << "\\addlinespace["
2431 << from_ascii(row_info[row].top_space.asLatexString())
2434 os << "\\noalign{\\vskip"
2435 << from_ascii(row_info[row].top_space.asLatexString())
2439 bool ismulticol = false;
2440 bool ismultirow = false;
2441 for (col_type c = 0; c < ncols(); ++c) {
2442 if (isPartOfMultiColumn(row, c))
2445 if (isPartOfMultiRow(row, c) &&
2446 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2451 cell = cellIndex(row, c);
2452 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2453 shared_ptr<InsetTableCell> inset = cellInset(cell);
2455 Paragraph const & par = inset->paragraphs().front();
2456 bool rtl = par.isRTL(buffer().params())
2458 && getPWidth(cell).zero();
2462 par.getParLanguage(buffer().params())->lang();
2463 if (lang == "farsi")
2465 else if (lang == "arabic_arabi")
2467 // currently, remaning RTL languages are
2468 // arabic_arabtex and hebrew
2472 // pass to the OutputParams that we are in a cell and
2473 // which alignment we have set.
2474 // InsetNewline needs this context information.
2475 OutputParams newrp(runparams);
2476 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2477 ? OutputParams::PLAIN
2478 : OutputParams::ALIGNED;
2480 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2481 && cellInfo(cell).decimal_width != 0) {
2482 // copy cell and split in 2
2483 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2484 head.getText(0)->setMacrocontextPosition(
2485 cellInset(cell)->getText(0)->macrocontextPosition());
2486 head.setBuffer(buffer());
2487 bool hassep = false;
2488 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2489 tail.getText(0)->setMacrocontextPosition(
2490 head.getText(0)->macrocontextPosition());
2491 tail.setBuffer(head.buffer());
2492 head.latex(os, newrp);
2494 tail.latex(os, newrp);
2495 } else if (!isPartOfMultiRow(row, c)) {
2496 if (!runparams.nice)
2497 os.texrow().start(par.id(), 0);
2498 inset->latex(os, newrp);
2501 runparams.encoding = newrp.encoding;
2505 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2506 if (cell != getLastCellInRow(row)) { // not last cell in row
2513 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2514 // if no first header and no empty first header is used,
2515 // the caption needs to be terminated by \endfirsthead
2517 os << "\\endfirsthead";
2519 os << "\\tabularnewline";
2520 if (row_info[row].bottom_space_default) {
2522 os << "\\addlinespace";
2524 os << "[\\doublerulesep]";
2525 } else if (!row_info[row].bottom_space.zero()) {
2527 os << "\\addlinespace";
2529 << from_ascii(row_info[row].bottom_space.asLatexString())
2534 //output the bottom line
2535 TeXBottomHLine(os, row, lang);
2537 if (row_info[row].interline_space_default) {
2539 os << "\\addlinespace\n";
2541 os << "\\noalign{\\vskip\\doublerulesep}\n";
2542 } else if (!row_info[row].interline_space.zero()) {
2544 os << "\\addlinespace["
2545 << from_ascii(row_info[row].interline_space.asLatexString())
2548 os << "\\noalign{\\vskip"
2549 << from_ascii(row_info[row].interline_space.asLatexString())
2555 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2557 bool const is_tabular_star = !tabular_width.zero();
2559 //+---------------------------------------------------------------------
2560 //+ first the opening preamble +
2561 //+---------------------------------------------------------------------
2564 os << "\\begin{sideways}\n";
2566 if (is_long_tabular) {
2567 os << "\\begin{longtable}";
2568 switch (longtabular_alignment) {
2569 case LYX_LONGTABULAR_ALIGN_LEFT:
2572 case LYX_LONGTABULAR_ALIGN_CENTER:
2574 case LYX_LONGTABULAR_ALIGN_RIGHT:
2579 if (is_tabular_star)
2580 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2582 os << "\\begin{tabular}";
2583 switch (tabular_valignment) {
2584 case LYX_VALIGN_TOP:
2587 case LYX_VALIGN_MIDDLE:
2589 case LYX_VALIGN_BOTTOM:
2597 if (is_tabular_star)
2598 os << "@{\\extracolsep{\\fill}}";
2600 for (col_type c = 0; c < ncols(); ++c) {
2601 if (columnLeftLine(c))
2603 if (!column_info[c].align_special.empty()) {
2604 os << column_info[c].align_special;
2606 if (!column_info[c].p_width.zero()) {
2607 switch (column_info[c].alignment) {
2608 case LYX_ALIGN_LEFT:
2609 os << ">{\\raggedright}";
2611 case LYX_ALIGN_RIGHT:
2612 os << ">{\\raggedleft}";
2614 case LYX_ALIGN_CENTER:
2615 os << ">{\\centering}";
2617 case LYX_ALIGN_NONE:
2618 case LYX_ALIGN_BLOCK:
2619 case LYX_ALIGN_LAYOUT:
2620 case LYX_ALIGN_SPECIAL:
2621 case LYX_ALIGN_DECIMAL:
2625 switch (column_info[c].valignment) {
2626 case LYX_VALIGN_TOP:
2629 case LYX_VALIGN_MIDDLE:
2632 case LYX_VALIGN_BOTTOM:
2637 << from_ascii(column_info[c].p_width.asLatexString())
2640 switch (column_info[c].alignment) {
2641 case LYX_ALIGN_LEFT:
2644 case LYX_ALIGN_RIGHT:
2647 case LYX_ALIGN_DECIMAL:
2648 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2654 } // end if else !column_info[i].p_width
2655 } // end if else !column_info[i].align_special
2656 if (columnRightLine(c))
2661 TeXLongtableHeaderFooter(os, runparams);
2663 //+---------------------------------------------------------------------
2664 //+ the single row and columns (cells) +
2665 //+---------------------------------------------------------------------
2667 for (row_type r = 0; r < nrows(); ++r) {
2668 if (isValidRow(r)) {
2669 TeXRow(os, r, runparams);
2670 if (is_long_tabular && row_info[r].newpage)
2671 os << "\\newpage\n";
2675 //+---------------------------------------------------------------------
2676 //+ the closing of the tabular +
2677 //+---------------------------------------------------------------------
2679 if (is_long_tabular)
2680 os << "\\end{longtable}";
2682 if (is_tabular_star)
2683 os << "\\end{tabular*}";
2685 os << "\\end{tabular}";
2689 os << breakln << "\\end{sideways}";
2693 int Tabular::docbookRow(odocstream & os, row_type row,
2694 OutputParams const & runparams) const
2697 idx_type cell = getFirstCellInRow(row);
2700 for (col_type c = 0; c < ncols(); ++c) {
2701 if (isPartOfMultiColumn(row, c))
2704 os << "<entry align=\"";
2705 switch (getAlignment(cell)) {
2706 case LYX_ALIGN_LEFT:
2709 case LYX_ALIGN_RIGHT:
2717 os << "\" valign=\"";
2718 switch (getVAlignment(cell)) {
2719 case LYX_VALIGN_TOP:
2722 case LYX_VALIGN_BOTTOM:
2725 case LYX_VALIGN_MIDDLE:
2730 if (isMultiColumn(cell)) {
2731 os << " namest=\"col" << c << "\" ";
2732 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2736 ret += cellInset(cell)->docbook(os, runparams);
2745 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2749 //+---------------------------------------------------------------------
2750 //+ first the opening preamble +
2751 //+---------------------------------------------------------------------
2753 os << "<tgroup cols=\"" << ncols()
2754 << "\" colsep=\"1\" rowsep=\"1\">\n";
2756 for (col_type c = 0; c < ncols(); ++c) {
2757 os << "<colspec colname=\"col" << c << "\" align=\"";
2758 switch (column_info[c].alignment) {
2759 case LYX_ALIGN_LEFT:
2762 case LYX_ALIGN_RIGHT:
2770 if (runparams.flavor == OutputParams::XML)
2776 //+---------------------------------------------------------------------
2777 //+ Long Tabular case +
2778 //+---------------------------------------------------------------------
2780 // output caption info
2781 if (haveLTCaption()) {
2782 os << "<caption>\n";
2784 for (row_type r = 0; r < nrows(); ++r) {
2785 if (row_info[r].caption) {
2786 ret += docbookRow(os, r, runparams);
2789 os << "</caption>\n";
2792 // output header info
2793 if (haveLTHead() || haveLTFirstHead()) {
2796 for (row_type r = 0; r < nrows(); ++r) {
2797 if (row_info[r].endhead || row_info[r].endfirsthead) {
2798 ret += docbookRow(os, r, runparams);
2804 // output footer info
2805 if (haveLTFoot() || haveLTLastFoot()) {
2808 for (row_type r = 0; r < nrows(); ++r) {
2809 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2810 ret += docbookRow(os, r, runparams);
2817 //+---------------------------------------------------------------------
2818 //+ the single row and columns (cells) +
2819 //+---------------------------------------------------------------------
2823 for (row_type r = 0; r < nrows(); ++r) {
2824 if (isValidRow(r)) {
2825 ret += docbookRow(os, r, runparams);
2830 //+---------------------------------------------------------------------
2831 //+ the closing of the tabular +
2832 //+---------------------------------------------------------------------
2841 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2842 OutputParams const & runparams, bool header) const
2845 string const celltag = header ? "th" : "td";
2846 idx_type cell = getFirstCellInRow(row);
2848 xs << html::StartTag("tr");
2849 for (col_type c = 0; c < ncols(); ++c) {
2850 if (isPartOfMultiColumn(row, c))
2855 switch (getAlignment(cell)) {
2856 case LYX_ALIGN_LEFT:
2859 case LYX_ALIGN_RIGHT:
2867 attr << " valign='";
2868 switch (getVAlignment(cell)) {
2869 case LYX_VALIGN_TOP:
2872 case LYX_VALIGN_BOTTOM:
2875 case LYX_VALIGN_MIDDLE:
2880 if (isMultiColumn(cell))
2881 attr << " colspan='" << columnSpan(cell) << "'";
2883 xs << html::StartTag(celltag, attr.str());
2884 ret += cellInset(cell)->xhtml(xs, runparams);
2885 xs << html::EndTag(celltag);
2888 xs << html::EndTag("tr");
2893 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2897 if (is_long_tabular) {
2898 // we'll wrap it in a div, so as to deal with alignment
2900 switch (longtabular_alignment) {
2901 case LYX_LONGTABULAR_ALIGN_LEFT:
2904 case LYX_LONGTABULAR_ALIGN_CENTER:
2907 case LYX_LONGTABULAR_ALIGN_RIGHT:
2911 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
2912 if (haveLTCaption()) {
2913 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
2914 for (row_type r = 0; r < nrows(); ++r)
2915 if (row_info[r].caption)
2916 ret += xhtmlRow(xs, r, runparams);
2917 xs << html::EndTag("div");
2921 xs << html::StartTag("table");
2923 // output header info
2924 bool const havefirsthead = haveLTFirstHead();
2925 // if we have a first head, then we are going to ignore the
2926 // headers for the additional pages, since there aren't any
2927 // in XHTML. this test accomplishes that.
2928 bool const havehead = !havefirsthead && haveLTHead();
2929 if (havehead || havefirsthead) {
2930 xs << html::StartTag("thead");
2931 for (row_type r = 0; r < nrows(); ++r) {
2932 if ((havefirsthead && row_info[r].endfirsthead)
2933 || (havehead && row_info[r].endhead)) {
2934 ret += xhtmlRow(xs, r, runparams, true);
2937 xs << html::EndTag("thead");
2939 // output footer info
2940 bool const havelastfoot = haveLTLastFoot();
2942 bool const havefoot = !havelastfoot && haveLTFoot();
2943 if (havefoot || havelastfoot) {
2944 xs << html::StartTag("tfoot");
2945 for (row_type r = 0; r < nrows(); ++r) {
2946 if ((havelastfoot && row_info[r].endlastfoot)
2947 || (havefoot && row_info[r].endfoot)) {
2948 ret += xhtmlRow(xs, r, runparams);
2951 xs << html::EndTag("tfoot");
2954 xs << html::StartTag("tbody");
2955 for (row_type r = 0; r < nrows(); ++r) {
2956 if (isValidRow(r)) {
2957 ret += xhtmlRow(xs, r, runparams);
2960 xs << html::EndTag("tbody")
2961 << html::EndTag("table");
2962 if (is_long_tabular)
2963 xs << html::EndTag("div");
2968 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2969 vector<unsigned int> const & clen) const
2971 idx_type const fcell = getFirstCellInRow(row);
2972 idx_type const n = numberOfCellsInRow(row) + fcell;
2975 for (idx_type i = fcell; i < n; ++i) {
2985 for (idx_type i = fcell; i < n; ++i) {
2996 col_type column = cellColumn(i);
2997 int len = clen[column];
2998 while (column < ncols() - 1
2999 && isPartOfMultiColumn(row, ++column))
3000 len += clen[column] + 4;
3001 os << docstring(len, ch);
3016 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3017 vector<unsigned int> const & clen) const
3019 idx_type const fcell = getFirstCellInRow(row);
3020 idx_type const n = numberOfCellsInRow(row) + fcell;
3023 for (idx_type i = fcell; i < n; ++i) {
3024 if (bottomLine(i)) {
3033 for (idx_type i = fcell; i < n; ++i) {
3034 if (bottomLine(i)) {
3044 col_type column = cellColumn(i);
3045 int len = clen[column];
3046 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3047 len += clen[column] + 4;
3048 os << docstring(len, ch);
3049 if (bottomLine(i)) {
3063 void Tabular::plaintextPrintCell(odocstream & os,
3064 OutputParams const & runparams,
3065 idx_type cell, row_type row, col_type column,
3066 vector<unsigned int> const & clen,
3067 bool onlydata) const
3069 odocstringstream sstr;
3070 cellInset(cell)->plaintext(sstr, runparams);
3082 unsigned int len1 = sstr.str().length();
3083 unsigned int len2 = clen[column];
3084 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3085 len2 += clen[column] + 4;
3088 switch (getAlignment(cell)) {
3090 case LYX_ALIGN_LEFT:
3093 case LYX_ALIGN_RIGHT:
3097 case LYX_ALIGN_CENTER:
3103 os << docstring(len1, ' ') << sstr.str()
3104 << docstring(len2, ' ');
3106 if (rightLine(cell))
3113 void Tabular::plaintext(odocstream & os,
3114 OutputParams const & runparams, int const depth,
3115 bool onlydata, char_type delim) const
3117 // first calculate the width of the single columns
3118 vector<unsigned int> clen(ncols());
3121 // first all non multicolumn cells!
3122 for (col_type c = 0; c < ncols(); ++c) {
3124 for (row_type r = 0; r < nrows(); ++r) {
3125 idx_type cell = cellIndex(r, c);
3126 if (isMultiColumn(cell))
3128 odocstringstream sstr;
3129 cellInset(cell)->plaintext(sstr, runparams);
3130 if (clen[c] < sstr.str().length())
3131 clen[c] = sstr.str().length();
3134 // then all multicolumn cells!
3135 for (col_type c = 0; c < ncols(); ++c) {
3136 for (row_type r = 0; r < nrows(); ++r) {
3137 idx_type cell = cellIndex(r, c);
3138 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3140 odocstringstream sstr;
3141 cellInset(cell)->plaintext(sstr, runparams);
3142 int len = int(sstr.str().length());
3143 idx_type const n = columnSpan(cell);
3144 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3146 if (len > int(clen[c + n - 1]))
3147 clen[c + n - 1] = len;
3152 for (row_type r = 0; r < nrows(); ++r) {
3153 if (!onlydata && plaintextTopHLine(os, r, clen))
3154 os << docstring(depth * 2, ' ');
3155 for (col_type c = 0; c < ncols(); ++c) {
3156 if (isPartOfMultiColumn(r, c))
3158 if (onlydata && c > 0)
3159 // we don't use operator<< for single UCS4 character.
3160 // see explanation in docstream.h
3162 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3167 os << docstring(depth * 2, ' ');
3168 if (plaintextBottomHLine(os, r, clen))
3169 os << docstring(depth * 2, ' ');
3175 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3177 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3181 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3182 col_type column) const
3184 return cell_info[row][column].inset;
3188 void Tabular::setCellInset(row_type row, col_type column,
3189 shared_ptr<InsetTableCell> ins) const
3191 CellData & cd = cell_info[row][column];
3196 void Tabular::validate(LaTeXFeatures & features) const
3198 features.require("NeedTabularnewline");
3200 features.require("booktabs");
3201 if (is_long_tabular)
3202 features.require("longtable");
3204 features.require("rotating");
3205 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3206 if (isMultiRow(cell))
3207 features.require("multirow");
3208 if (getVAlignment(cell) != LYX_VALIGN_TOP
3209 || !getPWidth(cell).zero())
3210 features.require("array");
3211 cellInset(cell)->validate(features);
3216 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3218 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3219 ParagraphList::const_iterator cit = parlist.begin();
3220 ParagraphList::const_iterator end = parlist.end();
3222 for (; cit != end; ++cit)
3223 for (int i = 0; i < cit->size(); ++i)
3224 if (cit->isNewline(i))
3231 /////////////////////////////////////////////////////////////////////
3235 /////////////////////////////////////////////////////////////////////
3237 InsetTableCell::InsetTableCell(Buffer * buf)
3238 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3239 contentAlign(LYX_ALIGN_CENTER)
3243 bool InsetTableCell::forcePlainLayout(idx_type) const
3245 return !isFixedWidth;
3249 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3251 return isFixedWidth;
3255 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3256 FuncStatus & status) const
3258 bool enabled = true;
3259 switch (cmd.action()) {
3261 enabled = !forcePlainLayout();
3263 case LFUN_LAYOUT_PARAGRAPH:
3264 enabled = allowParagraphCustomization();
3267 case LFUN_MATH_DISPLAY:
3268 if (!hasFixedWidth()) {
3273 return InsetText::getStatus(cur, cmd, status);
3275 status.setEnabled(enabled);
3279 docstring InsetTableCell::asString(bool intoInsets)
3282 if (paragraphs().empty())
3284 ParagraphList::const_iterator it = paragraphs().begin();
3285 ParagraphList::const_iterator en = paragraphs().end();
3287 for (; it != en; ++it) {
3292 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3298 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3301 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3302 return InsetText::xhtml(xs, rp);
3307 /////////////////////////////////////////////////////////////////////
3311 /////////////////////////////////////////////////////////////////////
3313 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3315 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3316 rowselect_(false), colselect_(false)
3321 InsetTabular::InsetTabular(InsetTabular const & tab)
3322 : Inset(tab), tabular(tab.tabular), scx_(0)
3327 InsetTabular::~InsetTabular()
3329 hideDialogs("tabular", this);
3333 void InsetTabular::setBuffer(Buffer & buf)
3335 tabular.setBuffer(buf);
3336 Inset::setBuffer(buf);
3340 bool InsetTabular::insetAllowed(InsetCode code) const
3345 case MATHMACRO_CODE:
3350 return tabular.is_long_tabular;
3358 void InsetTabular::write(ostream & os) const
3360 os << "Tabular" << endl;
3365 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3367 // FIXME: depending on the selection state,
3368 // we could offer a different menu.
3369 return cell(0)->contextMenuName() + ";" + contextMenuName();
3373 docstring InsetTabular::contextMenuName() const
3375 return from_ascii("context-tabular");
3379 void InsetTabular::read(Lexer & lex)
3381 //bool const old_format = (lex.getString() == "\\LyXTable");
3389 string token = lex.getString();
3390 while (lex && token != "\\end_inset") {
3392 token = lex.getString();
3395 lex.printError("Missing \\end_inset at this point. ");
3399 int InsetTabular::rowFromY(Cursor & cur, int y) const
3401 // top y coordinate of tabular
3402 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3404 for (; r < tabular.nrows() && y > h; ++r)
3405 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3406 + tabular.interRowSpace(r);
3412 int InsetTabular::columnFromX(Cursor & cur, int x) const
3414 // left x coordinate of tabular
3415 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3417 for (; c < tabular.ncols() && x > w; ++c)
3418 w += tabular.cellWidth(c);
3423 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3425 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3426 // mi.base.textwidth << "\n";
3429 LASSERT(false, /**/);
3432 for (row_type r = 0; r < tabular.nrows(); ++r) {
3435 for (col_type c = 0; c < tabular.ncols(); ++c) {
3436 if (tabular.isPartOfMultiColumn(r, c)
3437 || tabular.isPartOfMultiRow(r, c))
3438 // multicolumn or multirow cell, but not first one
3440 idx_type const cell = tabular.cellIndex(r, c);
3443 Length const p_width = tabular.getPWidth(cell);
3444 if (!p_width.zero())
3445 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3446 tabular.cellInset(cell)->metrics(m, dim);
3447 if (!p_width.zero())
3448 dim.wid = m.base.textwidth;
3449 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3450 + tabular.interColumnSpace(cell);
3452 // FIXME(?): do we need a second metrics call?
3453 TextMetrics const & tm =
3454 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3456 // determine horizontal offset because of decimal align (if necessary)
3457 int decimal_hoffset = 0;
3458 int decimal_width = 0;
3459 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3460 // make a copy which we will split in 2
3461 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3462 head.getText(0)->setMacrocontextPosition(
3463 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3464 head.setBuffer(tabular.buffer());
3465 // split in 2 and calculate width of each part
3466 bool hassep = false;
3467 InsetTableCell tail =
3468 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3469 tail.getText(0)->setMacrocontextPosition(
3470 head.getText(0)->macrocontextPosition());
3471 tail.setBuffer(head.buffer());
3473 head.metrics(m, dim1);
3474 decimal_hoffset = dim1.width();
3476 tail.metrics(m, dim1);
3477 decimal_width = dim1.width();
3480 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3481 tabular.cell_info[r][c].decimal_width = decimal_width;
3483 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3484 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3485 int const lastpardes = tm.last().second->descent()
3486 + TEXT_TO_INSET_OFFSET;
3488 switch (tabular.getVAlignment(cell)) {
3489 case Tabular::LYX_VALIGN_TOP:
3491 case Tabular::LYX_VALIGN_MIDDLE:
3492 offset = -(dim.des - lastpardes)/2;
3494 case Tabular::LYX_VALIGN_BOTTOM:
3495 offset = -(dim.des - lastpardes);
3498 tabular.cell_info[r][c].voffset = offset;
3499 maxasc = max(maxasc, dim.asc - offset);
3500 maxdes = max(maxdes, dim.des + offset);
3502 int const top_space = tabular.row_info[r].top_space_default ?
3503 default_line_space :
3504 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3505 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3506 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3507 default_line_space :
3508 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3509 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3512 // for top-alignment the first horizontal table line must be exactly at
3513 // the position of the base line of the surrounding text line
3514 // for bottom alignment, the same is for the last table line
3515 switch (tabular.tabular_valignment) {
3516 case Tabular::LYX_VALIGN_BOTTOM:
3517 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3519 case Tabular::LYX_VALIGN_MIDDLE:
3520 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3522 case Tabular::LYX_VALIGN_TOP:
3523 offset_valign_ = tabular.rowAscent(0);
3527 tabular.updateColumnWidths();
3528 dim.asc = tabular.rowAscent(0) - offset_valign_;
3529 dim.des = tabular.height() - dim.asc;
3530 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3534 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3537 if (&cur.inset() == this && cur.selection()) {
3538 if (cur.selIsMultiCell()) {
3541 getSelection(cur, rs, re, cs, ce);
3543 idx_type const cell = tabular.cellIndex(row, col);
3544 col_type const cspan = tabular.columnSpan(cell);
3545 row_type const rspan = tabular.rowSpan(cell);
3546 if (col + cspan - 1 >= cs && col <= ce
3547 && row + rspan - 1 >= rs && row <= re)
3550 if (col == tabular.cellColumn(cur.idx())
3551 && row == tabular.cellRow(cur.idx())) {
3552 CursorSlice const & beg = cur.selBegin();
3553 CursorSlice const & end = cur.selEnd();
3555 if ((end.lastpos() > 0 || end.lastpit() > 0)
3556 && end.pos() == end.lastpos() && beg.pos() == 0
3557 && end.pit() == end.lastpit() && beg.pit() == 0)
3565 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3567 x += scx_ + ADD_TO_TABULAR_WIDTH;
3569 BufferView * bv = pi.base.bv;
3570 Cursor & cur = pi.base.bv->cursor();
3573 // FIXME: As the full background is painted in drawBackground(),
3574 // we have no choice but to do a full repaint for the Text cells.
3575 pi.full_repaint = true;
3577 bool const original_selection_state = pi.selected;
3580 first_visible_cell = Tabular::npos;
3582 int yy = y + offset_valign_;
3583 for (row_type r = 0; r < tabular.nrows(); ++r) {
3585 for (col_type c = 0; c < tabular.ncols(); ++c) {
3586 if (tabular.isPartOfMultiColumn(r, c))
3589 idx = tabular.cellIndex(r, c);
3591 if (tabular.isPartOfMultiRow(r, c)) {
3592 nx += tabular.cellWidth(idx);
3596 if (first_visible_cell == Tabular::npos)
3597 first_visible_cell = idx;
3599 pi.selected |= isCellSelected(cur, r, c);
3600 int const cx = nx + tabular.textHOffset(idx);
3601 int const cy = yy + tabular.textVOffset(idx);
3602 // Cache the Inset position.
3603 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3604 cell(idx)->draw(pi, cx, cy);
3605 drawCellLines(pi, nx, yy, r, idx);
3606 nx += tabular.cellWidth(idx);
3607 pi.selected = original_selection_state;
3610 if (r + 1 < tabular.nrows())
3611 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3612 + tabular.interRowSpace(r + 1);
3617 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3619 x += scx_ + ADD_TO_TABULAR_WIDTH;
3620 y += offset_valign_ - tabular.rowAscent(0);
3621 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3622 pi.backgroundColor(this));
3626 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3628 Cursor & cur = pi.base.bv->cursor();
3631 x += scx_ + ADD_TO_TABULAR_WIDTH;
3633 if (!cur.selection())
3635 if (&cur.inset() != this)
3640 bool const full_cell_selected = isCellSelected(cur,
3641 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3643 if (cur.selIsMultiCell() || full_cell_selected) {
3644 for (row_type r = 0; r < tabular.nrows(); ++r) {
3646 for (col_type c = 0; c < tabular.ncols(); ++c) {
3647 if (tabular.isPartOfMultiColumn(r, c))
3650 idx_type const cell = tabular.cellIndex(r, c);
3652 if (tabular.isPartOfMultiRow(r, c)) {
3653 xx += tabular.cellWidth(cell);
3656 int const w = tabular.cellWidth(cell);
3657 int const h = tabular.cellHeight(cell);
3658 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3659 if (isCellSelected(cur, r, c))
3660 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3663 if (r + 1 < tabular.nrows())
3664 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3665 + tabular.interRowSpace(r + 1);
3669 // FIXME: This code has no effect because InsetTableCell does not handle
3670 // drawSelection other than the trivial implementation in Inset.
3672 // x += cellXPos(cur.idx());
3673 // x += tabular.textHOffset(cur.idx());
3674 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3679 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3680 row_type row, idx_type cell) const
3682 y -= tabular.rowAscent(row);
3683 int const w = tabular.cellWidth(cell);
3684 int const h = tabular.cellHeight(cell);
3685 Color const linecolor = pi.textColor(Color_tabularline);
3686 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3689 bool drawline = tabular.topLine(cell)
3690 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3691 pi.pain.line(x, y, x + w, y,
3692 drawline ? linecolor : gridcolor,
3693 drawline ? Painter::line_solid : Painter::line_onoffdash);
3696 drawline = tabular.bottomLine(cell);
3697 pi.pain.line(x, y + h, x + w, y + h,
3698 drawline ? linecolor : gridcolor,
3699 drawline ? Painter::line_solid : Painter::line_onoffdash);
3702 col_type const col = tabular.cellColumn(cell);
3703 drawline = tabular.leftLine(cell)
3704 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3705 pi.pain.line(x, y, x, y + h,
3706 drawline ? linecolor : gridcolor,
3707 drawline ? Painter::line_solid : Painter::line_onoffdash);
3710 x -= tabular.interColumnSpace(cell);
3711 drawline = tabular.rightLine(cell)
3712 || (col + 1 < tabular.ncols()
3713 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3714 pi.pain.line(x + w, y, x + w, y + h,
3715 drawline ? linecolor : gridcolor,
3716 drawline ? Painter::line_solid : Painter::line_onoffdash);
3720 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3722 //lyxerr << "InsetTabular::edit: " << this << endl;
3724 cur.setSelection(false);
3727 if (isRightToLeft(cur))
3728 cur.idx() = tabular.getLastCellInRow(0);
3734 if (isRightToLeft(cur))
3735 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3737 cur.idx() = tabular.numberofcells - 1;
3739 cur.pos() = cur.lastpos(); // FIXME crude guess
3741 cur.setCurrentFont();
3742 // FIXME: this accesses the position cache before it is initialized
3744 //cur.bv().fitCursor();
3748 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3750 // In a longtable, tell captions what the current float is
3751 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3752 string const saveflt = cnts.current_float();
3753 if (tabular.is_long_tabular)
3754 cnts.current_float("table");
3756 ParIterator it2 = it;
3758 size_t const end = it2.nargs();
3759 for ( ; it2.idx() < end; it2.top().forwardIdx())
3760 buffer().updateBuffer(it2, utype);
3763 if (tabular.is_long_tabular)
3764 cnts.current_float(saveflt);
3768 void InsetTabular::addToToc(DocIterator const & cpit) const
3770 DocIterator dit = cpit;
3772 size_t const end = dit.nargs();
3773 for ( ; dit.idx() < end; dit.top().forwardIdx())
3774 cell(dit.idx())->addToToc(dit);
3778 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3780 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3781 << "\n cur:" << cur);
3782 CursorSlice sl = cur.top();
3783 Cursor & bvcur = cur.bv().cursor();
3785 FuncCode const act = cmd.action();
3789 case LFUN_MOUSE_PRESS: {
3790 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3792 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3793 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3794 row_type r = rowFromY(cur, cmd.y());
3795 cur.idx() = tabular.getFirstCellInRow(r);
3798 cur.idx() = tabular.getLastCellInRow(r);
3799 cur.pos() = cur.lastpos();
3800 cur.setSelection(true);
3806 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3807 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3808 || cmd.y() > y0 + tabular.height()) {
3809 col_type c = columnFromX(cur, cmd.x());
3810 cur.idx() = tabular.cellIndex(0, c);
3813 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3814 cur.pos() = cur.lastpos();
3815 cur.setSelection(true);
3820 // do not reset cursor/selection if we have selected
3821 // some cells (bug 2715).
3822 if (cmd.button() == mouse_button::button3
3823 && &bvcur.selBegin().inset() == this
3824 && bvcur.selIsMultiCell())
3827 // Let InsetTableCell do it
3828 cell(cur.idx())->dispatch(cur, cmd);
3831 case LFUN_MOUSE_MOTION:
3832 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3833 if (cmd.button() == mouse_button::button1) {
3834 // only accept motions to places not deeper nested than the real anchor
3835 if (!bvcur.realAnchor().hasPart(cur)) {
3839 // select (additional) row
3841 row_type r = rowFromY(cur, cmd.y());
3842 cur.idx() = tabular.getLastCellInRow(r);
3843 // we need to reset the cursor's pit and pos now, as the old ones
3844 // may no longer be valid.
3847 bvcur.setCursor(cur);
3848 bvcur.setSelection(true);
3851 // select (additional) column
3853 col_type c = columnFromX(cur, cmd.x());
3854 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3855 // we need to reset the cursor's pit and pos now, as the old ones
3856 // may no longer be valid.
3859 bvcur.setCursor(cur);
3860 bvcur.setSelection(true);
3863 // only update if selection changes
3864 if (bvcur.idx() == cur.idx() &&
3865 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3866 cur.noScreenUpdate();
3867 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3868 bvcur.setCursor(cur);
3869 bvcur.setSelection(true);
3870 // if this is a multicell selection, we just set the cursor to
3871 // the beginning of the cell's text.
3872 if (bvcur.selIsMultiCell()) {
3873 bvcur.pit() = bvcur.lastpit();
3874 bvcur.pos() = bvcur.lastpos();
3879 case LFUN_MOUSE_RELEASE:
3884 case LFUN_CELL_BACKWARD:
3886 cur.setSelection(false);
3889 case LFUN_CELL_FORWARD:
3891 cur.setSelection(false);
3894 case LFUN_CHAR_FORWARD_SELECT:
3895 case LFUN_CHAR_FORWARD:
3896 case LFUN_CHAR_BACKWARD_SELECT:
3897 case LFUN_CHAR_BACKWARD:
3898 case LFUN_CHAR_RIGHT_SELECT:
3899 case LFUN_CHAR_RIGHT:
3900 case LFUN_CHAR_LEFT_SELECT:
3901 case LFUN_CHAR_LEFT:
3902 case LFUN_WORD_FORWARD:
3903 case LFUN_WORD_FORWARD_SELECT:
3904 case LFUN_WORD_BACKWARD:
3905 case LFUN_WORD_BACKWARD_SELECT:
3906 case LFUN_WORD_RIGHT:
3907 case LFUN_WORD_RIGHT_SELECT:
3908 case LFUN_WORD_LEFT:
3909 case LFUN_WORD_LEFT_SELECT: {
3910 // determine whether we move to next or previous cell, where to enter
3911 // the new cell from, and which command to "finish" (i.e., exit the
3914 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3915 FuncCode finish_lfun;
3917 if (act == LFUN_CHAR_FORWARD
3918 || act == LFUN_CHAR_FORWARD_SELECT
3919 || act == LFUN_WORD_FORWARD
3920 || act == LFUN_WORD_FORWARD_SELECT) {
3922 finish_lfun = LFUN_FINISHED_FORWARD;
3924 else if (act == LFUN_CHAR_BACKWARD
3925 || act == LFUN_CHAR_BACKWARD_SELECT
3926 || act == LFUN_WORD_BACKWARD
3927 || act == LFUN_WORD_BACKWARD_SELECT) {
3929 finish_lfun = LFUN_FINISHED_BACKWARD;
3931 // LEFT or RIGHT commands --- the interpretation will depend on the
3932 // table's direction.
3934 bool const right = act == LFUN_CHAR_RIGHT
3935 || act == LFUN_CHAR_RIGHT_SELECT
3936 || act == LFUN_WORD_RIGHT
3937 || act == LFUN_WORD_RIGHT_SELECT;
3938 next_cell = isRightToLeft(cur) != right;
3940 if (lyxrc.visual_cursor)
3941 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3943 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3946 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3947 || act == LFUN_CHAR_BACKWARD_SELECT
3948 || act == LFUN_CHAR_RIGHT_SELECT
3949 || act == LFUN_CHAR_LEFT_SELECT
3950 || act == LFUN_WORD_FORWARD_SELECT
3951 || act == LFUN_WORD_RIGHT_SELECT
3952 || act == LFUN_WORD_BACKWARD_SELECT
3953 || act == LFUN_WORD_LEFT_SELECT;
3955 // If we have a multicell selection or we're
3956 // not doing some LFUN_*_SELECT thing anyway...
3957 if (!cur.selIsMultiCell() || !select) {
3958 col_type const c = tabular.cellColumn(cur.idx());
3959 row_type const r = tabular.cellRow(cur.idx());
3960 // Are we trying to select the whole cell and is the whole cell
3961 // not yet selected?
3962 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3963 ((next_cell && cur.pit() == cur.lastpit()
3964 && cur.pos() == cur.lastpos())
3965 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3967 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3969 // ...try to dispatch to the cell's inset.
3970 cell(cur.idx())->dispatch(cur, cmd);
3972 // When we already have a selection we want to select the whole cell
3973 // before going to the next cell.
3974 if (select_whole && !empty_cell){
3975 getText(cur.idx())->selectAll(cur);
3980 // FIXME: When we support the selection of an empty cell, remove
3981 // the !empty_cell from this condition. For now we jump to the next
3982 // cell if the current cell is empty.
3983 if (cur.result().dispatched() && !empty_cell)
3987 // move to next/prev cell, as appropriate
3988 // note that we will always do this if we're selecting and we have
3989 // a multicell selection
3990 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3991 << " cell from: " << int(entry_from));
3993 moveNextCell(cur, entry_from);
3995 movePrevCell(cur, entry_from);
3996 // if we're exiting the table, call the appropriate FINISHED lfun
3997 if (sl == cur.top())
3998 cmd = FuncRequest(finish_lfun);
4005 case LFUN_DOWN_SELECT:
4007 if (!(cur.selection() && cur.selIsMultiCell()))
4008 cell(cur.idx())->dispatch(cur, cmd);
4010 cur.dispatched(); // override the cell's decision
4011 if (sl == cur.top()) {
4012 // if our Text didn't do anything to the cursor
4013 // then we try to put the cursor into the cell below
4014 // setting also the right targetX.
4015 cur.selHandle(act == LFUN_DOWN_SELECT);
4016 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4017 cur.idx() = tabular.cellBelow(cur.idx());
4019 TextMetrics const & tm =
4020 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4021 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
4022 cur.setCurrentFont();
4025 if (sl == cur.top()) {
4026 // we trick it to go to forward after leaving the
4028 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4031 if (cur.selIsMultiCell()) {
4032 cur.pit() = cur.lastpit();
4033 cur.pos() = cur.lastpos();
4034 cur.setCurrentFont();
4039 case LFUN_UP_SELECT:
4041 if (!(cur.selection() && cur.selIsMultiCell()))
4042 cell(cur.idx())->dispatch(cur, cmd);
4043 cur.dispatched(); // override the cell's decision
4044 if (sl == cur.top()) {
4045 // if our Text didn't do anything to the cursor
4046 // then we try to put the cursor into the cell above
4047 // setting also the right targetX.
4048 cur.selHandle(act == LFUN_UP_SELECT);
4049 if (tabular.cellRow(cur.idx()) != 0) {
4050 cur.idx() = tabular.cellAbove(cur.idx());
4051 cur.pit() = cur.lastpit();
4052 Text const * text = cell(cur.idx())->getText(0);
4053 TextMetrics const & tm = cur.bv().textMetrics(text);
4054 ParagraphMetrics const & pm =
4055 tm.parMetrics(cur.lastpit());
4056 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
4057 cur.setCurrentFont();
4060 if (sl == cur.top()) {
4061 cmd = FuncRequest(LFUN_UP);
4064 if (cur.selIsMultiCell()) {
4066 cur.pos() = cur.lastpos();
4067 cur.setCurrentFont();
4072 // case LFUN_SCREEN_DOWN: {
4073 // //if (hasSelection())
4074 // // cur.selection() = false;
4075 // col_type const col = tabular.cellColumn(cur.idx());
4076 // int const t = cur.bv().top_y() + cur.bv().height();
4077 // if (t < yo() + tabular.getHeightOfTabular()) {
4078 // cur.bv().scrollDocView(t, true);
4079 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4081 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4088 // case LFUN_SCREEN_UP: {
4089 // //if (hasSelection())
4090 // // cur.selection() = false;
4091 // col_type const col = tabular.cellColumn(cur.idx());
4092 // int const t = cur.bv().top_y() + cur.bv().height();
4094 // cur.bv().scrollDocView(t, true);
4098 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4102 // cur.par() = cur.lastpar();
4103 // cur.pos() = cur.lastpos();
4107 case LFUN_LAYOUT_TABULAR:
4108 cur.bv().showDialog("tabular");
4111 case LFUN_INSET_MODIFY:
4112 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4116 // insert file functions
4117 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4118 case LFUN_FILE_INSERT_PLAINTEXT:
4120 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4121 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4122 FileName(to_utf8(cmd.argument())));
4125 cur.recordUndoInset(INSERT_UNDO);
4126 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4127 // content has been replaced,
4128 // so cursor might be invalid
4129 cur.pos() = cur.lastpos();
4130 cur.pit() = cur.lastpit();
4131 bvcur.setCursor(cur);
4138 if (cur.selIsMultiCell()) {
4139 if (copySelection(cur)) {
4140 cur.recordUndoInset(DELETE_UNDO);
4144 cell(cur.idx())->dispatch(cur, cmd);
4147 case LFUN_SELF_INSERT:
4148 if (cur.selIsMultiCell()) {
4149 cur.recordUndoInset(DELETE_UNDO);
4152 cell(cur.idx())->dispatch(cur, cmd);
4155 case LFUN_CHAR_DELETE_BACKWARD:
4156 case LFUN_CHAR_DELETE_FORWARD:
4157 if (cur.selIsMultiCell()) {
4158 cur.recordUndoInset(DELETE_UNDO);
4161 cell(cur.idx())->dispatch(cur, cmd);
4165 if (!cur.selection())
4167 if (cur.selIsMultiCell()) {
4171 cell(cur.idx())->dispatch(cur, cmd);
4174 case LFUN_CLIPBOARD_PASTE:
4175 case LFUN_PRIMARY_SELECTION_PASTE: {
4176 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4177 theClipboard().getAsText() :
4178 theSelection().get();
4181 // pass to InsertPlaintextString, but
4182 // only if we have multi-cell content
4183 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4184 cur.recordUndoInset(INSERT_UNDO);
4185 if (insertPlaintextString(cur.bv(), clip, false)) {
4186 // content has been replaced,
4187 // so cursor might be invalid
4188 cur.pos() = cur.lastpos();
4189 cur.pit() = cur.lastpit();
4190 bvcur.setCursor(cur);
4194 // Let the cell handle normal text
4195 cell(cur.idx())->dispatch(cur, cmd);
4200 if (!tabularStackDirty()) {
4201 if (!cur.selIsMultiCell())
4202 cell(cur.idx())->dispatch(cur, cmd);
4205 if (theClipboard().isInternal()) {
4206 cur.recordUndoInset(INSERT_UNDO);
4207 pasteClipboard(cur);
4211 case LFUN_FONT_EMPH:
4212 case LFUN_FONT_BOLD:
4213 case LFUN_FONT_BOLDSYMBOL:
4214 case LFUN_FONT_ROMAN:
4215 case LFUN_FONT_NOUN:
4216 case LFUN_FONT_ITAL:
4217 case LFUN_FONT_FRAK:
4218 case LFUN_FONT_TYPEWRITER:
4219 case LFUN_FONT_SANS:
4220 case LFUN_TEXTSTYLE_APPLY:
4221 case LFUN_TEXTSTYLE_UPDATE:
4222 case LFUN_FONT_SIZE:
4223 case LFUN_FONT_UNDERLINE:
4224 case LFUN_FONT_STRIKEOUT:
4225 case LFUN_FONT_UULINE:
4226 case LFUN_FONT_UWAVE:
4228 case LFUN_WORD_CAPITALIZE:
4229 case LFUN_WORD_UPCASE:
4230 case LFUN_WORD_LOWCASE:
4231 case LFUN_CHARS_TRANSPOSE:
4232 if (cur.selIsMultiCell()) {
4235 getSelection(cur, rs, re, cs, ce);
4236 Cursor tmpcur = cur;
4237 for (row_type r = rs; r <= re; ++r) {
4238 for (col_type c = cs; c <= ce; ++c) {
4239 // cursor follows cell:
4240 tmpcur.idx() = tabular.cellIndex(r, c);
4241 // select this cell only:
4244 tmpcur.resetAnchor();
4245 tmpcur.pit() = tmpcur.lastpit();
4246 tmpcur.pos() = tmpcur.top().lastpos();
4247 tmpcur.setCursor(tmpcur);
4248 tmpcur.setSelection();
4249 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4254 cell(cur.idx())->dispatch(cur, cmd);
4258 case LFUN_INSET_SETTINGS:
4259 // relay this lfun to Inset, not to the cell.
4260 Inset::doDispatch(cur, cmd);
4264 // we try to handle this event in the insets dispatch function.
4265 cell(cur.idx())->dispatch(cur, cmd);
4271 // function sets an object as defined in func_status.h:
4272 // states OK, Unknown, Disabled, On, Off.
4273 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4274 FuncStatus & status) const
4276 switch (cmd.action()) {
4277 case LFUN_INSET_MODIFY: {
4278 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4281 // FIXME: We only check for the very first argument...
4282 string const s = cmd.getArg(1);
4283 // We always enable the lfun if it is coming from the dialog
4284 // because the dialog makes sure all the settings are valid,
4285 // even though the first argument might not be valid now.
4286 if (s == "from-dialog") {
4287 status.setEnabled(true);
4291 int action = Tabular::LAST_ACTION;
4293 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4294 if (tabularFeature[i].feature == s) {
4295 action = tabularFeature[i].action;
4299 if (action == Tabular::LAST_ACTION) {
4301 status.setUnknown(true);
4305 string const argument = cmd.getLongArg(2);
4307 row_type sel_row_start = 0;
4308 row_type sel_row_end = 0;
4309 col_type sel_col_start = 0;
4310 col_type sel_col_end = 0;
4311 Tabular::ltType dummyltt;
4314 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4317 case Tabular::SET_PWIDTH:
4318 case Tabular::SET_MPWIDTH:
4319 case Tabular::SET_SPECIAL_COLUMN:
4320 case Tabular::SET_SPECIAL_MULTICOLUMN:
4321 case Tabular::APPEND_ROW:
4322 case Tabular::APPEND_COLUMN:
4323 case Tabular::DELETE_ROW:
4324 case Tabular::DELETE_COLUMN:
4325 case Tabular::COPY_ROW:
4326 case Tabular::COPY_COLUMN:
4327 case Tabular::SET_TOP_SPACE:
4328 case Tabular::SET_BOTTOM_SPACE:
4329 case Tabular::SET_INTERLINE_SPACE:
4333 case Tabular::SET_TABULAR_WIDTH:
4334 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4335 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4338 case Tabular::SET_DECIMAL_POINT:
4340 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4343 case Tabular::SET_MULTICOLUMN:
4344 case Tabular::UNSET_MULTICOLUMN:
4345 case Tabular::MULTICOLUMN:
4346 // If a row is set as longtable caption, it must not be allowed
4347 // to unset that this row is a multicolumn.
4348 status.setEnabled(sel_row_start == sel_row_end
4349 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4350 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4353 case Tabular::SET_MULTIROW:
4354 case Tabular::UNSET_MULTIROW:
4355 case Tabular::MULTIROW:
4356 // If a row is set as longtable caption, it must not be allowed
4357 // to unset that this row is a multirow.
4358 status.setEnabled(sel_col_start == sel_col_end
4359 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4360 status.setOnOff(tabular.isMultiRow(cur.idx()));
4363 case Tabular::SET_ALL_LINES:
4364 case Tabular::UNSET_ALL_LINES:
4365 case Tabular::SET_BORDER_LINES:
4366 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4369 case Tabular::SET_LINE_TOP:
4370 case Tabular::SET_LINE_BOTTOM:
4371 case Tabular::SET_LINE_LEFT:
4372 case Tabular::SET_LINE_RIGHT:
4373 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4376 case Tabular::TOGGLE_LINE_TOP:
4377 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4378 status.setOnOff(tabular.topLine(cur.idx()));
4381 case Tabular::TOGGLE_LINE_BOTTOM:
4382 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4383 status.setOnOff(tabular.bottomLine(cur.idx()));
4386 case Tabular::TOGGLE_LINE_LEFT:
4387 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4388 status.setOnOff(tabular.leftLine(cur.idx()));
4391 case Tabular::TOGGLE_LINE_RIGHT:
4392 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4393 status.setOnOff(tabular.rightLine(cur.idx()));
4396 // multirow cells only inherit the alignment of the column if the column has
4397 // no width, otherwise they are left-aligned
4398 // therefore allow always left but right and center only if there is no width
4399 case Tabular::M_ALIGN_LEFT:
4401 case Tabular::ALIGN_LEFT:
4402 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4405 case Tabular::M_ALIGN_RIGHT:
4407 case Tabular::ALIGN_RIGHT:
4408 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4409 && !tabular.getPWidth(cur.idx()).zero()));
4410 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4413 case Tabular::M_ALIGN_CENTER:
4415 case Tabular::ALIGN_CENTER:
4416 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4417 && !tabular.getPWidth(cur.idx()).zero()));
4418 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4421 case Tabular::ALIGN_BLOCK:
4422 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4423 && !tabular.isMultiRow(cur.idx()));
4424 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4427 case Tabular::ALIGN_DECIMAL:
4428 status.setEnabled(!tabular.isMultiRow(cur.idx())
4429 && !tabular.isMultiColumn(cur.idx()));
4430 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4433 case Tabular::M_VALIGN_TOP:
4435 case Tabular::VALIGN_TOP:
4436 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4437 && !tabular.isMultiRow(cur.idx()));
4439 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4442 case Tabular::M_VALIGN_BOTTOM:
4444 case Tabular::VALIGN_BOTTOM:
4445 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4446 && !tabular.isMultiRow(cur.idx()));
4448 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4451 case Tabular::M_VALIGN_MIDDLE:
4453 case Tabular::VALIGN_MIDDLE:
4454 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4455 && !tabular.isMultiRow(cur.idx()));
4457 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4460 case Tabular::SET_LONGTABULAR:
4461 // setting as longtable is not allowed when table is inside a float
4462 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4463 || cur.innerInsetOfType(WRAP_CODE) != 0)
4464 status.setEnabled(false);
4466 status.setEnabled(true);
4467 status.setOnOff(tabular.is_long_tabular);
4470 case Tabular::UNSET_LONGTABULAR:
4471 status.setOnOff(!tabular.is_long_tabular);
4474 case Tabular::TOGGLE_ROTATE_TABULAR:
4475 case Tabular::SET_ROTATE_TABULAR:
4476 status.setEnabled(tabular.tabular_width.zero());
4477 status.setOnOff(tabular.rotate);
4480 case Tabular::TABULAR_VALIGN_TOP:
4481 status.setEnabled(tabular.tabular_width.zero());
4482 status.setOnOff(tabular.tabular_valignment
4483 == Tabular::LYX_VALIGN_TOP);
4485 case Tabular::TABULAR_VALIGN_MIDDLE:
4486 status.setEnabled(tabular.tabular_width.zero());
4487 status.setOnOff(tabular.tabular_valignment
4488 == Tabular::LYX_VALIGN_MIDDLE);
4490 case Tabular::TABULAR_VALIGN_BOTTOM:
4491 status.setEnabled(tabular.tabular_width.zero());
4492 status.setOnOff(tabular.tabular_valignment
4493 == Tabular::LYX_VALIGN_BOTTOM);
4496 case Tabular::LONGTABULAR_ALIGN_LEFT:
4497 status.setOnOff(tabular.longtabular_alignment
4498 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4500 case Tabular::LONGTABULAR_ALIGN_CENTER:
4501 status.setOnOff(tabular.longtabular_alignment
4502 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4504 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4505 status.setOnOff(tabular.longtabular_alignment
4506 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4509 case Tabular::UNSET_ROTATE_TABULAR:
4510 status.setOnOff(!tabular.rotate);
4513 case Tabular::TOGGLE_ROTATE_CELL:
4514 case Tabular::SET_ROTATE_CELL:
4515 status.setOnOff(!oneCellHasRotationState(false,
4516 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4519 case Tabular::UNSET_ROTATE_CELL:
4520 status.setOnOff(!oneCellHasRotationState(true,
4521 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4524 case Tabular::SET_USEBOX:
4525 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4528 // every row can only be one thing:
4529 // either a footer or header or caption
4530 case Tabular::SET_LTFIRSTHEAD:
4531 status.setEnabled(sel_row_start == sel_row_end
4532 && !tabular.ltCaption(sel_row_start));
4533 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4536 case Tabular::UNSET_LTFIRSTHEAD:
4537 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4540 case Tabular::SET_LTHEAD:
4541 status.setEnabled(sel_row_start == sel_row_end
4542 && !tabular.ltCaption(sel_row_start));
4543 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4546 case Tabular::UNSET_LTHEAD:
4547 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4550 case Tabular::SET_LTFOOT:
4551 status.setEnabled(sel_row_start == sel_row_end
4552 && !tabular.ltCaption(sel_row_start));
4553 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4556 case Tabular::UNSET_LTFOOT:
4557 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4560 case Tabular::SET_LTLASTFOOT:
4561 status.setEnabled(sel_row_start == sel_row_end
4562 && !tabular.ltCaption(sel_row_start));
4563 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4566 case Tabular::UNSET_LTLASTFOOT:
4567 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4570 case Tabular::SET_LTNEWPAGE:
4571 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4574 // only one row can be the caption
4575 // and a multirow cannot be set as caption
4576 case Tabular::SET_LTCAPTION:
4577 case Tabular::UNSET_LTCAPTION:
4578 case Tabular::TOGGLE_LTCAPTION:
4579 status.setEnabled(sel_row_start == sel_row_end
4580 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4581 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4582 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4583 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4584 && (!tabular.haveLTCaption()
4585 || tabular.ltCaption(sel_row_start))
4586 && !tabular.isMultiRow(sel_row_start));
4587 status.setOnOff(tabular.ltCaption(sel_row_start));
4590 case Tabular::SET_BOOKTABS:
4591 status.setOnOff(tabular.use_booktabs);
4594 case Tabular::UNSET_BOOKTABS:
4595 status.setOnOff(!tabular.use_booktabs);
4600 status.setEnabled(false);
4606 // These are only enabled inside tabular
4607 case LFUN_CELL_BACKWARD:
4608 case LFUN_CELL_FORWARD:
4609 status.setEnabled(true);
4612 // disable these with multiple cells selected
4613 case LFUN_INSET_INSERT:
4614 case LFUN_TABULAR_INSERT:
4615 case LFUN_FLEX_INSERT:
4616 case LFUN_FLOAT_INSERT:
4617 case LFUN_FLOAT_WIDE_INSERT:
4618 case LFUN_FOOTNOTE_INSERT:
4619 case LFUN_MARGINALNOTE_INSERT:
4620 case LFUN_MATH_INSERT:
4621 case LFUN_MATH_MODE:
4622 case LFUN_MATH_MUTATE:
4623 case LFUN_MATH_DISPLAY:
4624 case LFUN_NOTE_INSERT:
4625 case LFUN_ARGUMENT_INSERT:
4626 case LFUN_BOX_INSERT:
4627 case LFUN_BRANCH_INSERT:
4628 case LFUN_PHANTOM_INSERT:
4629 case LFUN_WRAP_INSERT:
4630 case LFUN_PREVIEW_INSERT:
4631 case LFUN_ERT_INSERT: {
4632 if (cur.selIsMultiCell()) {
4633 status.setEnabled(false);
4636 return cell(cur.idx())->getStatus(cur, cmd, status);
4639 // disable in non-fixed-width cells
4640 case LFUN_BREAK_PARAGRAPH:
4641 // multirow does not allow paragraph breaks
4642 if (tabular.isMultiRow(cur.idx())) {
4643 status.setEnabled(false);
4646 case LFUN_NEWLINE_INSERT: {
4647 if (tabular.getPWidth(cur.idx()).zero()) {
4648 status.setEnabled(false);
4651 return cell(cur.idx())->getStatus(cur, cmd, status);
4654 case LFUN_NEWPAGE_INSERT:
4655 status.setEnabled(false);
4659 if (tabularStackDirty() && theClipboard().isInternal()) {
4660 if (cur.selIsMultiCell()) {
4663 getSelection(cur, rs, re, cs, ce);
4664 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4665 && paste_tabular->nrows() == re - rs + 1)
4666 status.setEnabled(true);
4668 status.setEnabled(false);
4669 status.message(_("Selection size should match clipboard content."));
4672 status.setEnabled(true);
4675 return cell(cur.idx())->getStatus(cur, cmd, status);
4677 case LFUN_INSET_SETTINGS:
4678 // relay this lfun to Inset, not to the cell.
4679 return Inset::getStatus(cur, cmd, status);
4682 // we try to handle this event in the insets dispatch function.
4683 return cell(cur.idx())->getStatus(cur, cmd, status);
4689 Inset::DisplayType InsetTabular::display() const
4691 if (tabular.is_long_tabular) {
4692 switch (tabular.longtabular_alignment) {
4693 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4695 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4697 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4707 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4709 tabular.latex(os, runparams);
4713 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4715 os << '\n'; // output table on a new line
4716 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4717 tabular.plaintext(os, runparams, dp, false, 0);
4718 return PLAINTEXT_NEWLINE;
4722 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4727 // FIXME: Why not pass a proper DocIterator here?
4729 // if the table is inside a float it doesn't need the informaltable
4730 // wrapper. Search for it.
4731 for (master = owner(); master; master = master->owner())
4732 if (master->lyxCode() == FLOAT_CODE)
4737 os << "<informaltable>";
4740 ret += tabular.docbook(os, runparams);
4742 os << "</informaltable>";
4749 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4751 return tabular.xhtml(xs, rp);
4755 void InsetTabular::validate(LaTeXFeatures & features) const
4757 tabular.validate(features);
4759 // It'd be better to be able to get this from an InsetLayout, but at present
4760 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4761 if (features.runparams().flavor == OutputParams::HTML)
4762 features.addPreambleSnippet("<style type=\"text/css\">\n"
4763 "table { border: 1px solid black; display: inline-block; }\n"
4764 "td { border: 1px solid black; padding: 0.5ex; }\n"
4769 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4771 return tabular.cellInset(idx);
4775 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4777 return tabular.cellInset(idx);
4781 void InsetTabular::cursorPos(BufferView const & bv,
4782 CursorSlice const & sl, bool boundary, int & x, int & y) const
4784 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4786 // y offset correction
4787 y += cellYPos(sl.idx());
4788 y += tabular.textVOffset(sl.idx());
4789 y += offset_valign_;
4791 // x offset correction
4792 x += cellXPos(sl.idx());
4793 x += tabular.textHOffset(sl.idx());
4794 x += ADD_TO_TABULAR_WIDTH;
4799 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4803 Inset const & inset = *tabular.cellInset(cell);
4804 Point o = bv.coordCache().getInsets().xy(&inset);
4805 int const xbeg = o.x_ - tabular.textHOffset(cell);
4806 int const xend = xbeg + tabular.cellWidth(cell);
4807 row_type const row = tabular.cellRow(cell);
4808 int const ybeg = o.y_ - tabular.rowAscent(row)
4809 - tabular.interRowSpace(row);
4810 int const yend = ybeg + tabular.cellHeight(cell);
4822 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4823 // << " ybeg=" << ybeg << " yend=" << yend
4824 // << " xx=" << xx << " yy=" << yy
4825 // << " dist=" << xx + yy << endl;
4830 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4832 //lyxerr << "InsetTabular::editXY: " << this << endl;
4833 cur.setSelection(false);
4835 cur.idx() = getNearestCell(cur.bv(), x, y);
4837 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4841 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4843 cur.idx() = getNearestCell(cur.bv(), x, y);
4844 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4848 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4850 idx_type idx_min = 0;
4851 int dist_min = numeric_limits<int>::max();
4852 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4853 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4854 int const d = dist(bv, i, x, y);
4865 int InsetTabular::cellYPos(idx_type const cell) const
4867 row_type row = tabular.cellRow(cell);
4869 for (row_type r = 0; r < row; ++r)
4870 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4871 + tabular.interRowSpace(r + 1);
4876 int InsetTabular::cellXPos(idx_type const cell) const
4878 col_type col = tabular.cellColumn(cell);
4880 for (col_type c = 0; c < col; ++c)
4881 lx += tabular.column_info[c].width;
4886 void InsetTabular::resetPos(Cursor & cur) const
4888 BufferView & bv = cur.bv();
4889 int const maxwidth = bv.workWidth();
4891 int const scx_old = scx_;
4892 int const i = cur.find(this);
4897 int const X2 = maxwidth;
4898 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4899 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4900 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4903 scx_ = X1 + 20 - x1;
4905 scx_ = X2 - 20 - x2;
4910 // only update if offset changed
4911 if (scx_ != scx_old)
4912 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4916 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4918 row_type const row = tabular.cellRow(cur.idx());
4919 col_type const col = tabular.cellColumn(cur.idx());
4921 if (isRightToLeft(cur)) {
4922 if (tabular.cellColumn(cur.idx()) == 0) {
4923 if (row == tabular.nrows() - 1)
4925 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4930 cur.idx() = tabular.getLastCellInRow(row - 1);
4932 cur.idx() = tabular.cellIndex(row, col - 1);
4935 if (tabular.isLastCell(cur.idx()))
4937 if (cur.idx() == tabular.getLastCellInRow(row))
4938 cur.idx() = tabular.cellIndex(row + 1, 0);
4940 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4941 cur.idx() = tabular.cellIndex(row, colnextcell);
4945 cur.boundary(false);
4947 if (cur.selIsMultiCell()) {
4948 cur.pit() = cur.lastpit();
4949 cur.pos() = cur.lastpos();
4957 // in visual mode, place cursor at extreme left or right
4959 switch(entry_from) {
4961 case ENTRY_DIRECTION_RIGHT:
4962 cur.posVisToRowExtremity(false /* !left */);
4964 case ENTRY_DIRECTION_LEFT:
4965 cur.posVisToRowExtremity(true /* left */);
4967 case ENTRY_DIRECTION_IGNORE:
4968 // nothing to do in this case
4972 cur.setCurrentFont();
4977 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4979 row_type const row = tabular.cellRow(cur.idx());
4980 col_type const col = tabular.cellColumn(cur.idx());
4982 if (isRightToLeft(cur)) {
4983 if (cur.idx() == tabular.getLastCellInRow(row)) {
4986 cur.idx() = tabular.getFirstCellInRow(row);
4987 cur.idx() = tabular.cellAbove(cur.idx());
4989 if (tabular.isLastCell(cur.idx()))
4991 if (cur.idx() == tabular.getLastCellInRow(row))
4992 cur.idx() = tabular.cellIndex(row + 1, 0);
4994 cur.idx() = tabular.cellIndex(row, col + 1);
4997 if (cur.idx() == 0) // first cell
5000 cur.idx() = tabular.getLastCellInRow(row - 1);
5002 cur.idx() = tabular.cellIndex(row, col - 1);
5005 if (cur.selIsMultiCell()) {
5006 cur.pit() = cur.lastpit();
5007 cur.pos() = cur.lastpos();
5012 cur.pit() = cur.lastpit();
5013 cur.pos() = cur.lastpos();
5015 // in visual mode, place cursor at extreme left or right
5017 switch(entry_from) {
5019 case ENTRY_DIRECTION_RIGHT:
5020 cur.posVisToRowExtremity(false /* !left */);
5022 case ENTRY_DIRECTION_LEFT:
5023 cur.posVisToRowExtremity(true /* left */);
5025 case ENTRY_DIRECTION_IGNORE:
5026 // nothing to do in this case
5030 cur.setCurrentFont();
5035 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5037 istringstream is(argument);
5040 if (insetCode(s) != TABULAR_CODE)
5044 size_t safe_guard = 0;
5049 if (safe_guard > 1000) {
5050 LYXERR0("parameter max count reached!");
5054 Tabular::Feature action = Tabular::LAST_ACTION;
5057 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5058 if (s != tabularFeature[i].feature)
5061 action = tabularFeature[i].action;
5064 if (action == Tabular::LAST_ACTION) {
5065 LYXERR0("Feature not found " << s);
5069 if (tabularFeature[i].need_value)
5071 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5072 tabularFeatures(cur, action, val);
5078 static void checkLongtableSpecial(Tabular::ltType & ltt,
5079 string const & special, bool & flag)
5081 if (special == "dl_above") {
5084 } else if (special == "dl_below") {
5085 ltt.bottomDL = flag;
5087 } else if (special == "empty") {
5097 bool InsetTabular::oneCellHasRotationState(bool rotated,
5098 row_type row_start, row_type row_end,
5099 col_type col_start, col_type col_end) const
5101 for (row_type r = row_start; r <= row_end; ++r)
5102 for (col_type c = col_start; c <= col_end; ++c)
5103 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5109 void InsetTabular::tabularFeatures(Cursor & cur,
5110 Tabular::Feature feature, string const & value)
5112 col_type sel_col_start;
5113 col_type sel_col_end;
5114 row_type sel_row_start;
5115 row_type sel_row_end;
5116 bool setLines = false;
5117 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5118 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5122 case Tabular::M_ALIGN_LEFT:
5123 case Tabular::ALIGN_LEFT:
5124 setAlign = LYX_ALIGN_LEFT;
5127 case Tabular::M_ALIGN_RIGHT:
5128 case Tabular::ALIGN_RIGHT:
5129 setAlign = LYX_ALIGN_RIGHT;
5132 case Tabular::M_ALIGN_CENTER:
5133 case Tabular::ALIGN_CENTER:
5134 setAlign = LYX_ALIGN_CENTER;
5137 case Tabular::ALIGN_BLOCK:
5138 setAlign = LYX_ALIGN_BLOCK;
5141 case Tabular::ALIGN_DECIMAL:
5142 setAlign = LYX_ALIGN_DECIMAL;
5145 case Tabular::M_VALIGN_TOP:
5146 case Tabular::VALIGN_TOP:
5147 setVAlign = Tabular::LYX_VALIGN_TOP;
5150 case Tabular::M_VALIGN_BOTTOM:
5151 case Tabular::VALIGN_BOTTOM:
5152 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5155 case Tabular::M_VALIGN_MIDDLE:
5156 case Tabular::VALIGN_MIDDLE:
5157 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5164 cur.recordUndoInset(ATOMIC_UNDO, this);
5166 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5167 row_type const row = tabular.cellRow(cur.idx());
5168 col_type const column = tabular.cellColumn(cur.idx());
5170 Tabular::ltType ltt;
5174 case Tabular::SET_TABULAR_WIDTH:
5175 tabular.setTabularWidth(Length(value));
5178 case Tabular::SET_PWIDTH: {
5179 Length const len(value);
5180 tabular.setColumnPWidth(cur, cur.idx(), len);
5182 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5183 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5187 case Tabular::SET_MPWIDTH:
5188 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5191 case Tabular::SET_MROFFSET:
5192 tabular.setMROffset(cur, cur.idx(), Length(value));
5195 case Tabular::SET_SPECIAL_COLUMN:
5196 case Tabular::SET_SPECIAL_MULTICOLUMN:
5197 if (value == "none")
5198 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5200 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5203 case Tabular::APPEND_ROW:
5204 // append the row into the tabular
5205 tabular.appendRow(cur.idx());
5208 case Tabular::APPEND_COLUMN:
5209 // append the column into the tabular
5210 tabular.appendColumn(cur.idx());
5211 cur.idx() = tabular.cellIndex(row, column);
5214 case Tabular::DELETE_ROW:
5215 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5216 tabular.deleteRow(sel_row_start);
5217 if (sel_row_start >= tabular.nrows())
5219 cur.idx() = tabular.cellIndex(sel_row_start, column);
5222 cur.setSelection(false);
5225 case Tabular::DELETE_COLUMN:
5226 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5227 tabular.deleteColumn(sel_col_start);
5228 if (sel_col_start >= tabular.ncols())
5230 cur.idx() = tabular.cellIndex(row, sel_col_start);
5233 cur.setSelection(false);
5236 case Tabular::COPY_ROW:
5237 tabular.copyRow(row);
5240 case Tabular::COPY_COLUMN:
5241 tabular.copyColumn(column);
5242 cur.idx() = tabular.cellIndex(row, column);
5245 case Tabular::SET_LINE_TOP:
5246 case Tabular::TOGGLE_LINE_TOP: {
5247 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5248 ? (value == "true") : !tabular.topLine(cur.idx());
5249 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5250 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5251 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5255 case Tabular::SET_LINE_BOTTOM:
5256 case Tabular::TOGGLE_LINE_BOTTOM: {
5257 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5258 ? (value == "true") : !tabular.bottomLine(cur.idx());
5259 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5260 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5261 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5265 case Tabular::SET_LINE_LEFT:
5266 case Tabular::TOGGLE_LINE_LEFT: {
5267 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5268 ? (value == "true") : !tabular.leftLine(cur.idx());
5269 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5270 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5271 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5275 case Tabular::SET_LINE_RIGHT:
5276 case Tabular::TOGGLE_LINE_RIGHT: {
5277 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5278 ? (value == "true") : !tabular.rightLine(cur.idx());
5279 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5280 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5281 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5285 case Tabular::M_ALIGN_LEFT:
5286 case Tabular::M_ALIGN_RIGHT:
5287 case Tabular::M_ALIGN_CENTER:
5288 case Tabular::ALIGN_LEFT:
5289 case Tabular::ALIGN_RIGHT:
5290 case Tabular::ALIGN_CENTER:
5291 case Tabular::ALIGN_BLOCK:
5292 case Tabular::ALIGN_DECIMAL:
5293 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5294 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5295 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5296 !tabular.getPWidth(c).zero());
5299 case Tabular::M_VALIGN_TOP:
5300 case Tabular::M_VALIGN_BOTTOM:
5301 case Tabular::M_VALIGN_MIDDLE:
5303 case Tabular::VALIGN_TOP:
5304 case Tabular::VALIGN_BOTTOM:
5305 case Tabular::VALIGN_MIDDLE:
5306 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5307 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5308 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5311 case Tabular::SET_MULTICOLUMN: {
5312 if (!cur.selection()) {
5313 // just multicol for one single cell
5314 // check whether we are completely in a multicol
5315 if (!tabular.isMultiColumn(cur.idx()))
5316 tabular.setMultiColumn(cur.idx(), 1);
5319 // we have a selection so this means we just add all this
5320 // cells to form a multicolumn cell
5321 idx_type const s_start = cur.selBegin().idx();
5322 row_type const col_start = tabular.cellColumn(s_start);
5323 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5324 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5327 cur.setSelection(false);
5331 case Tabular::UNSET_MULTICOLUMN: {
5332 if (!cur.selection()) {
5333 if (tabular.isMultiColumn(cur.idx()))
5334 tabular.unsetMultiColumn(cur.idx());
5339 case Tabular::MULTICOLUMN: {
5340 if (tabular.isMultiColumn(cur.idx()))
5341 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5343 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5347 case Tabular::SET_MULTIROW: {
5348 if (!cur.selection()) {
5349 // just multirow for one single cell
5350 // check whether we are completely in a multirow
5351 if (!tabular.isMultiRow(cur.idx()))
5352 tabular.setMultiRow(cur.idx(), 1);
5355 // we have a selection so this means we just add all this
5356 // cells to form a multirow cell
5357 idx_type const s_start = cur.selBegin().idx();
5358 row_type const row_start = tabular.cellRow(s_start);
5359 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5360 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5363 cur.setSelection(false);
5367 case Tabular::UNSET_MULTIROW: {
5368 if (!cur.selection()) {
5369 if (tabular.isMultiRow(cur.idx()))
5370 tabular.unsetMultiRow(cur.idx());
5375 case Tabular::MULTIROW: {
5376 if (tabular.isMultiRow(cur.idx()))
5377 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5379 tabularFeatures(cur, Tabular::SET_MULTIROW);
5383 case Tabular::SET_ALL_LINES:
5385 case Tabular::UNSET_ALL_LINES:
5386 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5387 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5388 idx_type const cell = tabular.cellIndex(r, c);
5389 tabular.setTopLine(cell, setLines);
5390 tabular.setBottomLine(cell, setLines);
5391 tabular.setRightLine(cell, setLines);
5392 tabular.setLeftLine(cell, setLines);
5396 case Tabular::SET_BORDER_LINES:
5397 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5398 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5399 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5401 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5402 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5403 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5407 case Tabular::SET_LONGTABULAR:
5408 tabular.is_long_tabular = true;
5411 case Tabular::UNSET_LONGTABULAR:
5412 for (row_type r = 0; r < tabular.nrows(); ++r) {
5413 if (tabular.ltCaption(r)) {
5414 cur.idx() = tabular.cellIndex(r, 0);
5417 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5420 tabular.is_long_tabular = false;
5423 case Tabular::SET_ROTATE_TABULAR:
5424 tabular.rotate = true;
5427 case Tabular::UNSET_ROTATE_TABULAR:
5428 tabular.rotate = false;
5431 case Tabular::TOGGLE_ROTATE_TABULAR:
5432 tabular.rotate = !tabular.rotate;
5435 case Tabular::TABULAR_VALIGN_TOP:
5436 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5439 case Tabular::TABULAR_VALIGN_MIDDLE:
5440 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5443 case Tabular::TABULAR_VALIGN_BOTTOM:
5444 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5447 case Tabular::LONGTABULAR_ALIGN_LEFT:
5448 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5451 case Tabular::LONGTABULAR_ALIGN_CENTER:
5452 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5455 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5456 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5461 case Tabular::SET_ROTATE_CELL:
5462 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5463 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5464 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5467 case Tabular::UNSET_ROTATE_CELL:
5468 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5469 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5470 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5473 case Tabular::TOGGLE_ROTATE_CELL:
5475 bool oneNotRotated = oneCellHasRotationState(false,
5476 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5478 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5479 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5480 tabular.setRotateCell(tabular.cellIndex(r, c),
5485 case Tabular::SET_USEBOX: {
5486 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5487 if (val == tabular.getUsebox(cur.idx()))
5488 val = Tabular::BOX_NONE;
5489 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5490 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5491 tabular.setUsebox(tabular.cellIndex(r, c), val);
5495 case Tabular::UNSET_LTFIRSTHEAD:
5497 case Tabular::SET_LTFIRSTHEAD:
5498 tabular.getRowOfLTFirstHead(row, ltt);
5499 checkLongtableSpecial(ltt, value, flag);
5500 tabular.setLTHead(row, flag, ltt, true);
5503 case Tabular::UNSET_LTHEAD:
5505 case Tabular::SET_LTHEAD:
5506 tabular.getRowOfLTHead(row, ltt);
5507 checkLongtableSpecial(ltt, value, flag);
5508 tabular.setLTHead(row, flag, ltt, false);
5511 case Tabular::UNSET_LTFOOT:
5513 case Tabular::SET_LTFOOT:
5514 tabular.getRowOfLTFoot(row, ltt);
5515 checkLongtableSpecial(ltt, value, flag);
5516 tabular.setLTFoot(row, flag, ltt, false);
5519 case Tabular::UNSET_LTLASTFOOT:
5521 case Tabular::SET_LTLASTFOOT:
5522 tabular.getRowOfLTLastFoot(row, ltt);
5523 checkLongtableSpecial(ltt, value, flag);
5524 tabular.setLTFoot(row, flag, ltt, true);
5527 case Tabular::SET_LTNEWPAGE:
5528 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5531 case Tabular::SET_LTCAPTION: {
5532 if (tabular.ltCaption(row))
5534 cur.idx() = tabular.setLTCaption(row, true);
5537 cur.setSelection(false);
5538 // If a row is set as caption, then also insert
5539 // a caption. Otherwise the LaTeX output is broken.
5540 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5541 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5545 case Tabular::UNSET_LTCAPTION: {
5546 if (!tabular.ltCaption(row))
5548 cur.idx() = tabular.setLTCaption(row, false);
5551 cur.setSelection(false);
5552 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5553 if (lyx::getStatus(fr).enabled())
5558 case Tabular::TOGGLE_LTCAPTION: {
5559 if (tabular.ltCaption(row))
5560 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5562 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5566 case Tabular::SET_BOOKTABS:
5567 tabular.use_booktabs = true;
5570 case Tabular::UNSET_BOOKTABS:
5571 tabular.use_booktabs = false;
5574 case Tabular::SET_TOP_SPACE: {
5576 if (value == "default")
5577 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5578 tabular.row_info[r].top_space_default = true;
5579 else if (value == "none")
5580 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5581 tabular.row_info[r].top_space_default = false;
5582 tabular.row_info[r].top_space = len;
5584 else if (isValidLength(value, &len))
5585 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5586 tabular.row_info[r].top_space_default = false;
5587 tabular.row_info[r].top_space = len;
5592 case Tabular::SET_BOTTOM_SPACE: {
5594 if (value == "default")
5595 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5596 tabular.row_info[r].bottom_space_default = true;
5597 else if (value == "none")
5598 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5599 tabular.row_info[r].bottom_space_default = false;
5600 tabular.row_info[r].bottom_space = len;
5602 else if (isValidLength(value, &len))
5603 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5604 tabular.row_info[r].bottom_space_default = false;
5605 tabular.row_info[r].bottom_space = len;
5610 case Tabular::SET_INTERLINE_SPACE: {
5612 if (value == "default")
5613 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5614 tabular.row_info[r].interline_space_default = true;
5615 else if (value == "none")
5616 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5617 tabular.row_info[r].interline_space_default = false;
5618 tabular.row_info[r].interline_space = len;
5620 else if (isValidLength(value, &len))
5621 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5622 tabular.row_info[r].interline_space_default = false;
5623 tabular.row_info[r].interline_space = len;
5628 case Tabular::SET_DECIMAL_POINT:
5629 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5630 tabular.column_info[c].decimal_point = from_utf8(value);
5633 // dummy stuff just to avoid warnings
5634 case Tabular::LAST_ACTION:
5640 bool InsetTabular::copySelection(Cursor & cur)
5642 if (!cur.selection())
5647 getSelection(cur, rs, re, cs, ce);
5649 paste_tabular.reset(new Tabular(tabular));
5651 for (row_type r = 0; r < rs; ++r)
5652 paste_tabular->deleteRow(0);
5654 row_type const rows = re - rs + 1;
5655 while (paste_tabular->nrows() > rows)
5656 paste_tabular->deleteRow(rows);
5658 for (col_type c = 0; c < cs; ++c)
5659 paste_tabular->deleteColumn(0);
5661 col_type const columns = ce - cs + 1;
5662 while (paste_tabular->ncols() > columns)
5663 paste_tabular->deleteColumn(columns);
5665 paste_tabular->setBuffer(tabular.buffer());
5667 odocstringstream os;
5668 OutputParams const runparams(0);
5669 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5670 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5671 cap::copySelection(cur, os.str());
5673 // mark tabular stack dirty
5674 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5675 // when we (hopefully) have a one-for-all paste mechanism.
5676 // This must be called after cap::copySelection.
5677 dirtyTabularStack(true);
5683 bool InsetTabular::pasteClipboard(Cursor & cur)
5687 col_type actcol = tabular.cellColumn(cur.idx());
5688 row_type actrow = tabular.cellRow(cur.idx());
5690 if (cur.selIsMultiCell()) {
5693 getSelection(cur, actrow, re, actcol, ce);
5696 for (row_type r1 = 0, r2 = actrow;
5697 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5699 for (col_type c1 = 0, c2 = actcol;
5700 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5702 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5703 tabular.isPartOfMultiColumn(r2, c2))
5705 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5709 if (tabular.isPartOfMultiColumn(r2, c2)) {
5713 shared_ptr<InsetTableCell> inset(
5714 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5715 tabular.setCellInset(r2, c2, inset);
5716 // FIXME?: why do we need to do this explicitly? (EL)
5717 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5719 // FIXME: change tracking (MG)
5720 inset->setChange(Change(buffer().params().trackChanges ?
5721 Change::INSERTED : Change::UNCHANGED));
5729 void InsetTabular::cutSelection(Cursor & cur)
5731 if (!cur.selection())
5736 getSelection(cur, rs, re, cs, ce);
5737 for (row_type r = rs; r <= re; ++r) {
5738 for (col_type c = cs; c <= ce; ++c) {
5739 shared_ptr<InsetTableCell> t
5740 = cell(tabular.cellIndex(r, c));
5741 if (buffer().params().trackChanges)
5742 // FIXME: Change tracking (MG)
5743 t->setChange(Change(Change::DELETED));
5749 // cursor position might be invalid now
5750 if (cur.pit() > cur.lastpit())
5751 cur.pit() = cur.lastpit();
5752 if (cur.pos() > cur.lastpos())
5753 cur.pos() = cur.lastpos();
5754 cur.clearSelection();
5758 bool InsetTabular::isRightToLeft(Cursor & cur) const
5760 LASSERT(cur.depth() > 1, /**/);
5761 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5762 pos_type const parentpos = cur[cur.depth() - 2].pos();
5763 return parentpar.getFontSettings(buffer().params(),
5764 parentpos).language()->rightToLeft();
5768 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5771 LASSERT(stidx <= enidx, return docstring());
5773 col_type const col1 = tabular.cellColumn(stidx);
5774 col_type const col2 = tabular.cellColumn(enidx);
5775 row_type const row1 = tabular.cellRow(stidx);
5776 row_type const row2 = tabular.cellRow(enidx);
5778 for (col_type col = col1; col <= col2; col++)
5779 for (row_type row = row1; row <= row2; row++) {
5784 retval += tabular.cellInset(row, col)->asString(intoInsets);
5790 void InsetTabular::getSelection(Cursor & cur,
5791 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5793 CursorSlice const & beg = cur.selBegin();
5794 CursorSlice const & end = cur.selEnd();
5795 cs = tabular.cellColumn(beg.idx());
5796 ce = tabular.cellColumn(end.idx());
5800 rs = tabular.cellRow(beg.idx());
5801 re = tabular.cellRow(end.idx());
5807 Text * InsetTabular::getText(int idx) const
5809 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5813 void InsetTabular::setChange(Change const & change)
5815 for (idx_type idx = 0; idx < nargs(); ++idx)
5816 cell(idx)->setChange(change);
5820 void InsetTabular::acceptChanges()
5822 for (idx_type idx = 0; idx < nargs(); ++idx)
5823 cell(idx)->acceptChanges();
5827 void InsetTabular::rejectChanges()
5829 for (idx_type idx = 0; idx < nargs(); ++idx)
5830 cell(idx)->rejectChanges();
5834 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5836 return tabular.getPWidth(cell).zero();
5840 bool InsetTabular::forcePlainLayout(idx_type cell) const
5842 return !tabular.getPWidth(cell).zero();
5846 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5849 if (buf.length() <= 0)
5854 col_type maxCols = 1;
5855 size_t const len = buf.length();
5859 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5867 maxCols = max(cols, maxCols);
5873 maxCols = max(cols, maxCols);
5879 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5880 loctab = paste_tabular.get();
5882 dirtyTabularStack(true);
5885 cell = bv.cursor().idx();
5886 ocol = tabular.cellColumn(cell);
5887 row = tabular.cellRow(cell);
5891 idx_type const cells = loctab->numberofcells;
5894 rows = loctab->nrows();
5895 col_type const columns = loctab->ncols();
5897 while (cell < cells && p < len && row < rows &&
5898 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5904 // we can only set this if we are not too far right
5905 if (cols < columns) {
5906 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5907 Font const font = bv.textMetrics(&inset->text()).
5909 inset->setText(buf.substr(op, p - op), font,
5910 buffer().params().trackChanges);
5916 // we can only set this if we are not too far right
5917 if (cols < columns) {
5918 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5919 Font const font = bv.textMetrics(&inset->text()).
5921 inset->setText(buf.substr(op, p - op), font,
5922 buffer().params().trackChanges);
5927 cell = loctab->cellIndex(row, cols);
5933 // check for the last cell if there is no trailing '\n'
5934 if (cell < cells && op < len) {
5935 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5936 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5937 inset->setText(buf.substr(op, len - op), font,
5938 buffer().params().trackChanges);
5944 void InsetTabular::addPreview(DocIterator const & inset_pos,
5945 PreviewLoader & loader) const
5947 DocIterator cell_pos = inset_pos;
5949 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5950 for (row_type r = 0; r < tabular.nrows(); ++r) {
5951 for (col_type c = 0; c < tabular.ncols(); ++c) {
5952 cell_pos.top().idx() = tabular.cellIndex(r, c);
5953 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5959 bool InsetTabular::completionSupported(Cursor const & cur) const
5961 Cursor const & bvCur = cur.bv().cursor();
5962 if (&bvCur.inset() != this)
5964 return cur.text()->completionSupported(cur);
5968 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5970 return completionSupported(cur);
5974 bool InsetTabular::automaticInlineCompletion() const
5976 return lyxrc.completion_inline_text;
5980 bool InsetTabular::automaticPopupCompletion() const
5982 return lyxrc.completion_popup_text;
5986 bool InsetTabular::showCompletionCursor() const
5988 return lyxrc.completion_cursor_text;
5992 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5994 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5998 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6000 if (!completionSupported(cur))
6002 return cur.text()->completionPrefix(cur);
6006 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6008 if (!completionSupported(cur))
6011 return cur.text()->insertCompletion(cur, s, finished);
6015 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6016 Dimension & dim) const
6018 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6019 tm.completionPosAndDim(cur, x, y, dim);
6023 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6025 istringstream data(in);
6027 lex.setStream(data);
6034 if (!lex || token != "tabular") {
6035 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6039 // This is part of the inset proper that is usually swallowed
6040 // by Buffer::readInset
6042 if (!lex || token != "Tabular") {
6043 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6051 string InsetTabular::params2string(InsetTabular const & inset)
6054 data << "tabular" << ' ';
6056 data << "\\end_inset\n";