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;
1346 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1347 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1349 return cell_info[row][c].cellno;
1353 Tabular::row_type Tabular::cellRow(idx_type cell) const
1355 if (cell >= numberofcells)
1359 return rowofcell[cell];
1363 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1365 if (cell >= numberofcells)
1369 return columnofcell[cell];
1373 void Tabular::write(ostream & os) const
1377 << write_attribute("version", 3)
1378 << write_attribute("rows", nrows())
1379 << write_attribute("columns", ncols())
1381 // global longtable options
1383 << write_attribute("rotate", rotate)
1384 << write_attribute("booktabs", use_booktabs)
1385 << write_attribute("islongtable", is_long_tabular)
1386 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1387 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1388 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1389 << write_attribute("headTopDL", endhead.topDL)
1390 << write_attribute("headBottomDL", endhead.bottomDL)
1391 << write_attribute("footTopDL", endfoot.topDL)
1392 << write_attribute("footBottomDL", endfoot.bottomDL)
1393 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1394 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1395 << write_attribute("lastFootEmpty", endlastfoot.empty);
1396 // longtables cannot be aligned vertically
1397 if (!is_long_tabular) {
1398 os << write_attribute("tabularvalignment", tabular_valignment);
1399 os << write_attribute("tabularwidth", tabular_width);
1401 if (is_long_tabular)
1402 os << write_attribute("longtabularalignment",
1403 longtabular_alignment);
1405 for (col_type c = 0; c < ncols(); ++c) {
1407 << write_attribute("alignment", column_info[c].alignment);
1408 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1409 os << write_attribute("decimal_point", column_info[c].decimal_point);
1410 os << write_attribute("valignment", column_info[c].valignment)
1411 << write_attribute("width", column_info[c].p_width.asString())
1412 << write_attribute("special", column_info[c].align_special)
1415 for (row_type r = 0; r < nrows(); ++r) {
1416 static const string def("default");
1418 if (row_info[r].top_space_default)
1419 os << write_attribute("topspace", def);
1421 os << write_attribute("topspace", row_info[r].top_space);
1422 if (row_info[r].bottom_space_default)
1423 os << write_attribute("bottomspace", def);
1425 os << write_attribute("bottomspace", row_info[r].bottom_space);
1426 if (row_info[r].interline_space_default)
1427 os << write_attribute("interlinespace", def);
1429 os << write_attribute("interlinespace", row_info[r].interline_space);
1430 os << write_attribute("endhead", row_info[r].endhead)
1431 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1432 << write_attribute("endfoot", row_info[r].endfoot)
1433 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1434 << write_attribute("newpage", row_info[r].newpage)
1435 << write_attribute("caption", row_info[r].caption)
1437 for (col_type c = 0; c < ncols(); ++c) {
1439 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1440 << write_attribute("multirow", cell_info[r][c].multirow)
1441 << write_attribute("mroffset", cell_info[r][c].mroffset)
1442 << write_attribute("alignment", cell_info[r][c].alignment)
1443 << write_attribute("valignment", cell_info[r][c].valignment)
1444 << write_attribute("topline", cell_info[r][c].top_line)
1445 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1446 << write_attribute("leftline", cell_info[r][c].left_line)
1447 << write_attribute("rightline", cell_info[r][c].right_line)
1448 << write_attribute("rotate", cell_info[r][c].rotate)
1449 << write_attribute("usebox", cell_info[r][c].usebox)
1450 << write_attribute("width", cell_info[r][c].p_width)
1451 << write_attribute("special", cell_info[r][c].align_special)
1453 os << "\\begin_inset ";
1454 cell_info[r][c].inset->write(os);
1455 os << "\n\\end_inset\n"
1460 os << "</lyxtabular>\n";
1464 void Tabular::read(Lexer & lex)
1467 istream & is = lex.getStream();
1469 l_getline(is, line);
1470 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1471 LASSERT(false, /**/);
1476 if (!getTokenValue(line, "version", version))
1478 LASSERT(version >= 2, /**/);
1481 if (!getTokenValue(line, "rows", rows_arg))
1484 if (!getTokenValue(line, "columns", columns_arg))
1486 init(buffer_, rows_arg, columns_arg);
1487 l_getline(is, line);
1488 if (!prefixIs(line, "<features")) {
1489 lyxerr << "Wrong tabular format (expected <features ...> got"
1490 << line << ')' << endl;
1493 getTokenValue(line, "rotate", rotate);
1494 getTokenValue(line, "booktabs", use_booktabs);
1495 getTokenValue(line, "islongtable", is_long_tabular);
1496 getTokenValue(line, "tabularvalignment", tabular_valignment);
1497 getTokenValue(line, "tabularwidth", tabular_width);
1498 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1499 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1500 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1501 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1502 getTokenValue(line, "headTopDL", endhead.topDL);
1503 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1504 getTokenValue(line, "footTopDL", endfoot.topDL);
1505 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1506 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1507 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1508 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1510 for (col_type c = 0; c < ncols(); ++c) {
1512 if (!prefixIs(line,"<column")) {
1513 lyxerr << "Wrong tabular format (expected <column ...> got"
1514 << line << ')' << endl;
1517 getTokenValue(line, "alignment", column_info[c].alignment);
1518 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1519 getTokenValue(line, "valignment", column_info[c].valignment);
1520 getTokenValue(line, "width", column_info[c].p_width);
1521 getTokenValue(line, "special", column_info[c].align_special);
1524 for (row_type i = 0; i < nrows(); ++i) {
1525 l_getline(is, line);
1526 if (!prefixIs(line, "<row")) {
1527 lyxerr << "Wrong tabular format (expected <row ...> got"
1528 << line << ')' << endl;
1531 getTokenValue(line, "topspace", row_info[i].top_space,
1532 row_info[i].top_space_default);
1533 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1534 row_info[i].bottom_space_default);
1535 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1536 row_info[i].interline_space_default);
1537 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1538 getTokenValue(line, "endhead", row_info[i].endhead);
1539 getTokenValue(line, "endfoot", row_info[i].endfoot);
1540 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1541 getTokenValue(line, "newpage", row_info[i].newpage);
1542 getTokenValue(line, "caption", row_info[i].caption);
1543 for (col_type j = 0; j < ncols(); ++j) {
1544 l_getline(is, line);
1545 if (!prefixIs(line, "<cell")) {
1546 lyxerr << "Wrong tabular format (expected <cell ...> got"
1547 << line << ')' << endl;
1550 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1551 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1552 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1553 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1554 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1555 getTokenValue(line, "topline", cell_info[i][j].top_line);
1556 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1557 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1558 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1559 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1560 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1561 getTokenValue(line, "width", cell_info[i][j].p_width);
1563 getTokenValue(line, "special", cell_info[i][j].align_special);
1564 l_getline(is, line);
1565 if (prefixIs(line, "\\begin_inset")) {
1566 cell_info[i][j].inset->setBuffer(*buffer_);
1567 cell_info[i][j].inset->read(lex);
1568 l_getline(is, line);
1570 if (!prefixIs(line, "</cell>")) {
1571 lyxerr << "Wrong tabular format (expected </cell> got"
1572 << line << ')' << endl;
1576 l_getline(is, line);
1577 if (!prefixIs(line, "</row>")) {
1578 lyxerr << "Wrong tabular format (expected </row> got"
1579 << line << ')' << endl;
1583 while (!prefixIs(line, "</lyxtabular>")) {
1584 l_getline(is, line);
1590 bool Tabular::isMultiColumn(idx_type cell) const
1592 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1593 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1597 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1599 return cell_info[cellRow(cell)][cellColumn(cell)];
1603 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1605 idx_type const col = cellColumn(cell);
1606 idx_type const row = cellRow(cell);
1607 for (idx_type i = 0; i < number; ++i)
1608 unsetMultiRow(cellIndex(row, col + i));
1610 // unsetting of multirow may have invalidated cell index
1611 cell = cellIndex(row, col);
1612 CellData & cs = cellInfo(cell);
1613 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1614 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1615 cs.alignment = column_info[col].alignment;
1617 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1619 for (idx_type i = 1; i < number; ++i) {
1620 CellData & cs1 = cellInfo(cell + i);
1621 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1622 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1630 bool Tabular::isMultiRow(idx_type cell) const
1632 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1633 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1637 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1639 idx_type const col = cellColumn(cell);
1640 idx_type const row = cellRow(cell);
1641 for (idx_type i = 0; i < number; ++i)
1642 unsetMultiColumn(cellIndex(row + i, col));
1644 // unsetting of multirow may have invalidated cell index
1645 cell = cellIndex(row, col);
1646 CellData & cs = cellInfo(cell);
1647 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1648 cs.valignment = LYX_VALIGN_MIDDLE;
1649 // the horizontal alignment of multirow cells can only
1650 // be changed for the whole table row,
1651 // support changing this only for the multirow cell can be done via
1653 // this feature would be a fileformat change
1654 // until LyX supports this, use the deault alignment of multirow
1656 cs.alignment = LYX_ALIGN_LEFT;
1658 // set the bottom row of the last selected cell
1659 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1661 for (idx_type i = 1; i < number; ++i) {
1662 CellData & cs1 = cell_info[row + i][col];
1663 cs1.multirow = CELL_PART_OF_MULTIROW;
1664 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1672 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1674 row_type const row = cellRow(cell);
1675 col_type const col = cellColumn(cell);
1677 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1684 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1686 col_type const column = cellColumn(cell);
1687 col_type row = cellRow(cell) + 1;
1688 while (row < nrows() && isPartOfMultiRow(row, column))
1691 return row - cellRow(cell);
1695 void Tabular::unsetMultiColumn(idx_type cell)
1697 if (!isMultiColumn(cell))
1700 row_type const row = cellRow(cell);
1701 col_type const col = cellColumn(cell);
1702 row_type const span = columnSpan(cell);
1703 for (col_type c = 0; c < span; ++c) {
1704 // in the table dialog the lines are set in every case
1705 // when unsetting a multicolumn this leads to an additional right
1706 // line for every cell that was part of the former multicolumn cell,
1707 // except if the cell is in the last column
1708 // therefore remove this line
1709 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1710 && (col + c) < (col + span - 1))
1711 cell_info[row][col + c].right_line = false;
1712 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1718 void Tabular::unsetMultiRow(idx_type cell)
1720 if (!isMultiRow(cell))
1723 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1724 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1725 row_type const row = cellRow(cell);
1726 col_type const col = cellColumn(cell);
1727 row_type const span = rowSpan(cell);
1728 for (row_type r = 0; r < span; ++r)
1729 cell_info[row + r][col].multirow = CELL_NORMAL;
1734 void Tabular::setRotateCell(idx_type cell, bool flag)
1736 cellInfo(cell).rotate = flag;
1740 bool Tabular::getRotateCell(idx_type cell) const
1742 return cellInfo(cell).rotate;
1746 bool Tabular::needRotating() const
1750 for (row_type r = 0; r < nrows(); ++r)
1751 for (col_type c = 0; c < ncols(); ++c)
1752 if (cell_info[r][c].rotate)
1758 bool Tabular::isLastCell(idx_type cell) const
1760 if (cell + 1 < numberofcells)
1766 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1768 if (cellRow(cell) == 0)
1771 col_type const col = cellColumn(cell);
1772 row_type r = cellRow(cell) - 1;
1773 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1776 return cell_info[r][col].cellno;
1780 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1782 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1783 if (nextrow < nrows())
1784 return cell_info[nextrow][cellColumn(cell)].cellno;
1789 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1791 LASSERT(column != npos && column < ncols()
1792 && row != npos && row < nrows(), /**/);
1793 return cell_info[row][column].cellno;
1797 void Tabular::setUsebox(idx_type cell, BoxType type)
1799 cellInfo(cell).usebox = type;
1803 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1804 // adds line breaks, see bug 4886.
1805 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1807 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1808 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1810 if (cellInfo(cell).usebox > 1)
1811 return cellInfo(cell).usebox;
1812 return useParbox(cell);
1817 // This are functions used for the longtable support
1819 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1825 row_info[row].endfirsthead = flag;
1829 row_info[row].endhead = flag;
1834 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1837 hd.set = haveLTHead();
1838 return row_info[row].endhead;
1842 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1845 hd.set = haveLTFirstHead();
1846 return row_info[row].endfirsthead;
1850 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1856 row_info[row].endlastfoot = flag;
1860 row_info[row].endfoot = flag;
1865 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1868 fd.set = haveLTFoot();
1869 return row_info[row].endfoot;
1873 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1876 fd.set = haveLTLastFoot();
1877 return row_info[row].endlastfoot;
1881 void Tabular::setLTNewPage(row_type row, bool what)
1883 row_info[row].newpage = what;
1887 bool Tabular::getLTNewPage(row_type row) const
1889 return row_info[row].newpage;
1893 bool Tabular::haveLTHead() const
1895 if (!is_long_tabular)
1897 for (row_type i = 0; i < nrows(); ++i)
1898 if (row_info[i].endhead)
1904 bool Tabular::haveLTFirstHead() const
1906 if (!is_long_tabular || endfirsthead.empty)
1908 for (row_type r = 0; r < nrows(); ++r)
1909 if (row_info[r].endfirsthead)
1915 bool Tabular::haveLTFoot() const
1917 if (!is_long_tabular)
1919 for (row_type r = 0; r < nrows(); ++r)
1920 if (row_info[r].endfoot)
1926 bool Tabular::haveLTLastFoot() const
1928 if (!is_long_tabular || endlastfoot.empty)
1930 for (row_type r = 0; r < nrows(); ++r)
1931 if (row_info[r].endlastfoot)
1937 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1939 idx_type i = getFirstCellInRow(row);
1941 setMultiColumn(i, numberOfCellsInRow(row));
1942 setTopLine(i, false);
1943 setBottomLine(i, false);
1944 setLeftLine(i, false);
1945 setRightLine(i, false);
1947 unsetMultiColumn(i);
1948 // When unsetting a caption row, also all existing
1949 // captions in this row must be dissolved.
1951 row_info[row].caption = what;
1956 bool Tabular::ltCaption(row_type row) const
1958 return row_info[row].caption;
1962 bool Tabular::haveLTCaption() const
1964 if (!is_long_tabular)
1966 for (row_type r = 0; r < nrows(); ++r)
1967 if (row_info[r].caption)
1973 // end longtable support functions
1975 void Tabular::setRowAscent(row_type row, int height)
1977 if (row >= nrows() || row_info[row].ascent == height)
1979 row_info[row].ascent = height;
1983 void Tabular::setRowDescent(row_type row, int height)
1985 if (row >= nrows() || row_info[row].descent == height)
1987 row_info[row].descent = height;
1991 int Tabular::rowAscent(row_type row) const
1993 LASSERT(row < nrows(), /**/);
1994 return row_info[row].ascent;
1998 int Tabular::rowDescent(row_type row) const
2000 LASSERT(row < nrows(), /**/);
2001 return row_info[row].descent;
2005 int Tabular::height() const
2008 for (row_type row = 0; row < nrows(); ++row)
2009 height += rowAscent(row) + rowDescent(row) +
2015 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2017 LASSERT(row < nrows(), /**/);
2018 LASSERT(column < ncols(), /**/);
2019 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2023 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2025 LASSERT(row < nrows(), /**/);
2026 LASSERT(column < ncols(), /**/);
2027 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2031 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2033 // we only output complete row lines and the 1st row here, the rest
2034 // is done in Tabular::TeXBottomHLine(...)
2036 // get for each column the topline (if any)
2037 vector<bool> topline;
2039 for (col_type c = 0; c < ncols(); ++c) {
2040 topline.push_back(topLine(cellIndex(row, c)));
2041 // If cell is part of a multirow and not the first cell of the
2042 // multirow, no line must be drawn.
2044 if (isMultiRow(cellIndex(row, c))
2045 && isMultiRow(cellIndex(row - 1, c)))
2051 // do nothing if empty first row, or incomplete row line after
2052 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2055 // only output complete row lines and the 1st row's clines
2056 if (nset == ncols()) {
2058 os << (row == 0 ? "\\toprule " : "\\midrule ");
2062 } else if (row == 0) {
2063 for (col_type c = 0; c < ncols(); ++c) {
2065 col_type offset = 0;
2066 for (col_type j = 0 ; j < c; ++j)
2067 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2070 //babel makes the "-" character an active one, so we have to suppress this here
2071 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2072 if (lang == "slovak" || lang == "czech")
2073 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2074 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2076 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2078 col_type cstart = c;
2079 for ( ; c < ncols() && topline[c]; ++c) {}
2081 for (col_type j = cstart ; j < c ; ++j)
2082 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2085 os << c + offset << "} ";
2093 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2095 // we output bottomlines of row r and the toplines of row r+1
2096 // if the latter do not span the whole tabular
2098 // get the bottomlines of row r, and toplines in next row
2099 bool lastrow = row == nrows() - 1;
2100 vector<bool> bottomline, topline;
2101 bool nextrowset = true;
2102 for (col_type c = 0; c < ncols(); ++c) {
2103 bottomline.push_back(bottomLine(cellIndex(row, c)));
2104 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2105 // If cell is part of a multirow and not the last or first cell of the
2106 // multirow, no line must be drawn.
2108 if (isMultiRow(cellIndex(row, c))
2109 && isMultiRow(cellIndex(row + 1, c))) {
2110 bottomline[c] = false;
2113 nextrowset &= topline[c];
2116 // combine this row's bottom lines and next row's toplines if necessary
2118 for (col_type c = 0; c < ncols(); ++c) {
2120 bottomline[c] = bottomline[c] || topline[c];
2125 // do nothing if empty, OR incomplete row line with a topline in next row
2126 if (nset == 0 || (nextrowset && nset != ncols()))
2129 if (nset == ncols()) {
2131 os << (lastrow ? "\\bottomrule" : "\\midrule");
2135 for (col_type c = 0; c < ncols(); ++c) {
2136 if (bottomline[c]) {
2137 col_type offset = 0;
2138 for (col_type j = 0 ; j < c; ++j)
2139 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2142 //babel makes the "-" character an active one, so we have to suppress this here
2143 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2144 if (lang == "slovak" || lang == "czech")
2145 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2146 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2148 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2150 col_type cstart = c;
2151 for ( ; c < ncols() && bottomline[c]; ++c) {}
2153 for (col_type j = cstart ; j < c ; ++j)
2154 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2157 os << c + offset << "} ";
2165 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2166 bool & ismulticol, bool & ismultirow) const
2168 row_type const r = cellRow(cell);
2169 if (is_long_tabular && row_info[r].caption)
2172 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2173 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2174 // figure out how to set the lines
2175 // we always set double lines to the right of the cell
2176 col_type const c = cellColumn(cell);
2177 col_type const nextcol = c + columnSpan(cell);
2178 bool colright = columnRightLine(c);
2179 bool colleft = columnLeftLine(c);
2180 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2181 bool nextcellleft = nextcol < ncols()
2182 && leftLine(cellIndex(r, nextcol));
2183 bool coldouble = colright && nextcolleft;
2184 bool celldouble = rightLine(cell) && nextcellleft;
2186 ismulticol = isMultiColumn(cell)
2187 || (c == 0 && colleft != leftLine(cell))
2188 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2189 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2190 || (coldouble != celldouble);
2192 // we center in multicol when no decimal point
2193 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2194 && (cellInfo(cell).decimal_width == 0));
2196 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2197 int latexcolspan = columnSpan(cell);
2198 for(col_type col = c; col < c + columnSpan(cell); ++col)
2199 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2203 os << "\\multicolumn{" << latexcolspan << "}{";
2204 if (c ==0 && leftLine(cell))
2206 if (!cellInfo(cell).align_special.empty()) {
2207 os << cellInfo(cell).align_special;
2209 if (!getPWidth(cell).zero()) {
2211 case LYX_ALIGN_LEFT:
2212 os << ">{\\raggedright}";
2214 case LYX_ALIGN_RIGHT:
2215 os << ">{\\raggedleft}";
2217 case LYX_ALIGN_CENTER:
2218 os << ">{\\centering}";
2224 case LYX_VALIGN_TOP:
2227 case LYX_VALIGN_MIDDLE:
2230 case LYX_VALIGN_BOTTOM:
2235 << from_ascii(getPWidth(cell).asLatexString())
2239 case LYX_ALIGN_LEFT:
2242 case LYX_ALIGN_RIGHT:
2249 } // end if else !getPWidth
2250 } // end if else !cellinfo_of_cell
2251 if (rightLine(cell) || nextcellleft)
2254 // add extra vertical line if we want a double one
2257 } // end if ismulticol
2259 // we only need code for the first multirow cell
2260 ismultirow = isMultiRow(cell);
2262 os << "\\multirow{" << rowSpan(cell) << "}{";
2263 if (!getPWidth(cell).zero())
2264 os << from_ascii(getPWidth(cell).asLatexString());
2266 // we need to set a default value
2269 if (!getMROffset(cell).zero())
2270 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2272 } // end if ismultirow
2274 if (getRotateCell(cell)) {
2275 os << "\\begin{sideways}\n";
2277 if (getUsebox(cell) == BOX_PARBOX) {
2280 case LYX_VALIGN_TOP:
2283 case LYX_VALIGN_MIDDLE:
2286 case LYX_VALIGN_BOTTOM:
2290 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2292 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2293 os << "\\begin{minipage}[";
2295 case LYX_VALIGN_TOP:
2298 case LYX_VALIGN_MIDDLE:
2301 case LYX_VALIGN_BOTTOM:
2305 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2311 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2312 bool ismulticol, bool ismultirow) const
2314 row_type const r = cellRow(cell);
2315 if (is_long_tabular && row_info[r].caption)
2319 if (getUsebox(cell) == BOX_PARBOX)
2321 else if (getUsebox(cell) == BOX_MINIPAGE)
2322 os << breakln << "\\end{minipage}";
2323 if (getRotateCell(cell))
2324 os << breakln << "\\end{sideways}";
2332 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2333 OutputParams const & runparams) const
2335 if (!is_long_tabular)
2339 // the caption must be output before the headers
2340 if (haveLTCaption()) {
2341 for (row_type r = 0; r < nrows(); ++r) {
2342 if (row_info[r].caption)
2343 TeXRow(os, r, runparams);
2346 // output first header info
2347 // first header must be output before the header, otherwise the
2348 // correct caption placement becomes really weird
2349 if (haveLTFirstHead()) {
2350 if (endfirsthead.topDL)
2352 for (row_type r = 0; r < nrows(); ++r) {
2353 if (row_info[r].endfirsthead)
2354 TeXRow(os, r, runparams);
2356 if (endfirsthead.bottomDL)
2358 os << "\\endfirsthead\n";
2360 // output header info
2362 if (endfirsthead.empty && !haveLTFirstHead())
2363 os << "\\endfirsthead\n";
2366 for (row_type r = 0; r < nrows(); ++r) {
2367 if (row_info[r].endhead)
2368 TeXRow(os, r, runparams);
2370 if (endhead.bottomDL)
2372 os << "\\endhead\n";
2374 // output footer info
2378 for (row_type r = 0; r < nrows(); ++r) {
2379 if (row_info[r].endfoot)
2380 TeXRow(os, r, runparams);
2382 if (endfoot.bottomDL)
2384 os << "\\endfoot\n";
2385 if (endlastfoot.empty && !haveLTLastFoot())
2386 os << "\\endlastfoot\n";
2388 // output lastfooter info
2389 if (haveLTLastFoot()) {
2390 if (endlastfoot.topDL)
2392 for (row_type r = 0; r < nrows(); ++r) {
2393 if (row_info[r].endlastfoot)
2394 TeXRow(os, r, runparams);
2396 if (endlastfoot.bottomDL)
2398 os << "\\endlastfoot\n";
2403 bool Tabular::isValidRow(row_type row) const
2405 if (!is_long_tabular)
2407 return !row_info[row].endhead && !row_info[row].endfirsthead
2408 && !row_info[row].endfoot && !row_info[row].endlastfoot
2409 && !row_info[row].caption;
2413 void Tabular::TeXRow(otexstream & os, row_type row,
2414 OutputParams const & runparams) const
2416 idx_type cell = cellIndex(row, 0);
2417 shared_ptr<InsetTableCell> inset = cellInset(cell);
2418 Paragraph const & par = inset->paragraphs().front();
2419 string const lang = par.getParLanguage(buffer().params())->lang();
2421 //output the top line
2422 TeXTopHLine(os, row, lang);
2424 if (row_info[row].top_space_default) {
2426 os << "\\addlinespace\n";
2428 os << "\\noalign{\\vskip\\doublerulesep}\n";
2429 } else if(!row_info[row].top_space.zero()) {
2431 os << "\\addlinespace["
2432 << from_ascii(row_info[row].top_space.asLatexString())
2435 os << "\\noalign{\\vskip"
2436 << from_ascii(row_info[row].top_space.asLatexString())
2440 bool ismulticol = false;
2441 bool ismultirow = false;
2442 for (col_type c = 0; c < ncols(); ++c) {
2443 if (isPartOfMultiColumn(row, c))
2446 cell = cellIndex(row, c);
2448 if (isPartOfMultiRow(row, c)
2449 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2450 if (cell != getLastCellInRow(row))
2455 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2456 shared_ptr<InsetTableCell> inset = cellInset(cell);
2458 Paragraph const & par = inset->paragraphs().front();
2459 bool rtl = par.isRTL(buffer().params())
2461 && getPWidth(cell).zero();
2465 par.getParLanguage(buffer().params())->lang();
2466 if (lang == "farsi")
2468 else if (lang == "arabic_arabi")
2470 // currently, remaning RTL languages are
2471 // arabic_arabtex and hebrew
2475 // pass to the OutputParams that we are in a cell and
2476 // which alignment we have set.
2477 // InsetNewline needs this context information.
2478 OutputParams newrp(runparams);
2479 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2480 ? OutputParams::PLAIN
2481 : OutputParams::ALIGNED;
2483 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2484 && cellInfo(cell).decimal_width != 0) {
2485 // copy cell and split in 2
2486 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2487 head.getText(0)->setMacrocontextPosition(
2488 cellInset(cell)->getText(0)->macrocontextPosition());
2489 head.setBuffer(buffer());
2490 bool hassep = false;
2491 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2492 tail.getText(0)->setMacrocontextPosition(
2493 head.getText(0)->macrocontextPosition());
2494 tail.setBuffer(head.buffer());
2495 head.latex(os, newrp);
2497 tail.latex(os, newrp);
2498 } else if (!isPartOfMultiRow(row, c)) {
2499 if (!runparams.nice)
2500 os.texrow().start(par.id(), 0);
2501 inset->latex(os, newrp);
2504 runparams.encoding = newrp.encoding;
2508 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2509 if (cell != getLastCellInRow(row)) { // not last cell in row
2516 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2517 // if no first header and no empty first header is used,
2518 // the caption needs to be terminated by \endfirsthead
2520 os << "\\endfirsthead";
2522 os << "\\tabularnewline";
2523 if (row_info[row].bottom_space_default) {
2525 os << "\\addlinespace";
2527 os << "[\\doublerulesep]";
2528 } else if (!row_info[row].bottom_space.zero()) {
2530 os << "\\addlinespace";
2532 << from_ascii(row_info[row].bottom_space.asLatexString())
2537 //output the bottom line
2538 TeXBottomHLine(os, row, lang);
2540 if (row_info[row].interline_space_default) {
2542 os << "\\addlinespace\n";
2544 os << "\\noalign{\\vskip\\doublerulesep}\n";
2545 } else if (!row_info[row].interline_space.zero()) {
2547 os << "\\addlinespace["
2548 << from_ascii(row_info[row].interline_space.asLatexString())
2551 os << "\\noalign{\\vskip"
2552 << from_ascii(row_info[row].interline_space.asLatexString())
2558 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2560 bool const is_tabular_star = !tabular_width.zero();
2562 //+---------------------------------------------------------------------
2563 //+ first the opening preamble +
2564 //+---------------------------------------------------------------------
2567 if (runparams.lastid != -1)
2568 os.texrow().start(runparams.lastid, runparams.lastpos);
2571 os << "\\begin{sideways}\n";
2573 if (is_long_tabular) {
2574 os << "\\begin{longtable}";
2575 switch (longtabular_alignment) {
2576 case LYX_LONGTABULAR_ALIGN_LEFT:
2579 case LYX_LONGTABULAR_ALIGN_CENTER:
2581 case LYX_LONGTABULAR_ALIGN_RIGHT:
2586 if (is_tabular_star)
2587 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2589 os << "\\begin{tabular}";
2590 switch (tabular_valignment) {
2591 case LYX_VALIGN_TOP:
2594 case LYX_VALIGN_MIDDLE:
2596 case LYX_VALIGN_BOTTOM:
2604 if (is_tabular_star)
2605 os << "@{\\extracolsep{\\fill}}";
2607 for (col_type c = 0; c < ncols(); ++c) {
2608 if (columnLeftLine(c))
2610 if (!column_info[c].align_special.empty()) {
2611 os << column_info[c].align_special;
2613 if (!column_info[c].p_width.zero()) {
2614 switch (column_info[c].alignment) {
2615 case LYX_ALIGN_LEFT:
2616 os << ">{\\raggedright}";
2618 case LYX_ALIGN_RIGHT:
2619 os << ">{\\raggedleft}";
2621 case LYX_ALIGN_CENTER:
2622 os << ">{\\centering}";
2624 case LYX_ALIGN_NONE:
2625 case LYX_ALIGN_BLOCK:
2626 case LYX_ALIGN_LAYOUT:
2627 case LYX_ALIGN_SPECIAL:
2628 case LYX_ALIGN_DECIMAL:
2632 switch (column_info[c].valignment) {
2633 case LYX_VALIGN_TOP:
2636 case LYX_VALIGN_MIDDLE:
2639 case LYX_VALIGN_BOTTOM:
2644 << from_ascii(column_info[c].p_width.asLatexString())
2647 switch (column_info[c].alignment) {
2648 case LYX_ALIGN_LEFT:
2651 case LYX_ALIGN_RIGHT:
2654 case LYX_ALIGN_DECIMAL:
2655 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2661 } // end if else !column_info[i].p_width
2662 } // end if else !column_info[i].align_special
2663 if (columnRightLine(c))
2668 TeXLongtableHeaderFooter(os, runparams);
2670 //+---------------------------------------------------------------------
2671 //+ the single row and columns (cells) +
2672 //+---------------------------------------------------------------------
2674 for (row_type r = 0; r < nrows(); ++r) {
2675 if (isValidRow(r)) {
2676 TeXRow(os, r, runparams);
2677 if (is_long_tabular && row_info[r].newpage)
2678 os << "\\newpage\n";
2682 //+---------------------------------------------------------------------
2683 //+ the closing of the tabular +
2684 //+---------------------------------------------------------------------
2686 if (is_long_tabular)
2687 os << "\\end{longtable}";
2689 if (is_tabular_star)
2690 os << "\\end{tabular*}";
2692 os << "\\end{tabular}";
2696 os << breakln << "\\end{sideways}";
2700 int Tabular::docbookRow(odocstream & os, row_type row,
2701 OutputParams const & runparams) const
2704 idx_type cell = getFirstCellInRow(row);
2707 for (col_type c = 0; c < ncols(); ++c) {
2708 if (isPartOfMultiColumn(row, c))
2711 os << "<entry align=\"";
2712 switch (getAlignment(cell)) {
2713 case LYX_ALIGN_LEFT:
2716 case LYX_ALIGN_RIGHT:
2724 os << "\" valign=\"";
2725 switch (getVAlignment(cell)) {
2726 case LYX_VALIGN_TOP:
2729 case LYX_VALIGN_BOTTOM:
2732 case LYX_VALIGN_MIDDLE:
2737 if (isMultiColumn(cell)) {
2738 os << " namest=\"col" << c << "\" ";
2739 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2743 ret += cellInset(cell)->docbook(os, runparams);
2752 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2756 //+---------------------------------------------------------------------
2757 //+ first the opening preamble +
2758 //+---------------------------------------------------------------------
2760 os << "<tgroup cols=\"" << ncols()
2761 << "\" colsep=\"1\" rowsep=\"1\">\n";
2763 for (col_type c = 0; c < ncols(); ++c) {
2764 os << "<colspec colname=\"col" << c << "\" align=\"";
2765 switch (column_info[c].alignment) {
2766 case LYX_ALIGN_LEFT:
2769 case LYX_ALIGN_RIGHT:
2777 if (runparams.flavor == OutputParams::XML)
2783 //+---------------------------------------------------------------------
2784 //+ Long Tabular case +
2785 //+---------------------------------------------------------------------
2787 // output caption info
2788 if (haveLTCaption()) {
2789 os << "<caption>\n";
2791 for (row_type r = 0; r < nrows(); ++r) {
2792 if (row_info[r].caption) {
2793 ret += docbookRow(os, r, runparams);
2796 os << "</caption>\n";
2799 // output header info
2800 if (haveLTHead() || haveLTFirstHead()) {
2803 for (row_type r = 0; r < nrows(); ++r) {
2804 if (row_info[r].endhead || row_info[r].endfirsthead) {
2805 ret += docbookRow(os, r, runparams);
2811 // output footer info
2812 if (haveLTFoot() || haveLTLastFoot()) {
2815 for (row_type r = 0; r < nrows(); ++r) {
2816 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2817 ret += docbookRow(os, r, runparams);
2824 //+---------------------------------------------------------------------
2825 //+ the single row and columns (cells) +
2826 //+---------------------------------------------------------------------
2830 for (row_type r = 0; r < nrows(); ++r) {
2831 if (isValidRow(r)) {
2832 ret += docbookRow(os, r, runparams);
2837 //+---------------------------------------------------------------------
2838 //+ the closing of the tabular +
2839 //+---------------------------------------------------------------------
2848 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2849 OutputParams const & runparams, bool header) const
2852 string const celltag = header ? "th" : "td";
2853 idx_type cell = getFirstCellInRow(row);
2855 xs << html::StartTag("tr");
2856 for (col_type c = 0; c < ncols(); ++c) {
2857 if (isPartOfMultiColumn(row, c))
2862 switch (getAlignment(cell)) {
2863 case LYX_ALIGN_LEFT:
2866 case LYX_ALIGN_RIGHT:
2874 attr << " valign='";
2875 switch (getVAlignment(cell)) {
2876 case LYX_VALIGN_TOP:
2879 case LYX_VALIGN_BOTTOM:
2882 case LYX_VALIGN_MIDDLE:
2887 if (isMultiColumn(cell))
2888 attr << " colspan='" << columnSpan(cell) << "'";
2890 xs << html::StartTag(celltag, attr.str());
2891 ret += cellInset(cell)->xhtml(xs, runparams);
2892 xs << html::EndTag(celltag);
2895 xs << html::EndTag("tr");
2900 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2904 if (is_long_tabular) {
2905 // we'll wrap it in a div, so as to deal with alignment
2907 switch (longtabular_alignment) {
2908 case LYX_LONGTABULAR_ALIGN_LEFT:
2911 case LYX_LONGTABULAR_ALIGN_CENTER:
2914 case LYX_LONGTABULAR_ALIGN_RIGHT:
2918 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'");
2919 if (haveLTCaption()) {
2920 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'");
2921 for (row_type r = 0; r < nrows(); ++r)
2922 if (row_info[r].caption)
2923 ret += xhtmlRow(xs, r, runparams);
2924 xs << html::EndTag("div");
2928 xs << html::StartTag("table");
2930 // output header info
2931 bool const havefirsthead = haveLTFirstHead();
2932 // if we have a first head, then we are going to ignore the
2933 // headers for the additional pages, since there aren't any
2934 // in XHTML. this test accomplishes that.
2935 bool const havehead = !havefirsthead && haveLTHead();
2936 if (havehead || havefirsthead) {
2937 xs << html::StartTag("thead");
2938 for (row_type r = 0; r < nrows(); ++r) {
2939 if ((havefirsthead && row_info[r].endfirsthead)
2940 || (havehead && row_info[r].endhead)) {
2941 ret += xhtmlRow(xs, r, runparams, true);
2944 xs << html::EndTag("thead");
2946 // output footer info
2947 bool const havelastfoot = haveLTLastFoot();
2949 bool const havefoot = !havelastfoot && haveLTFoot();
2950 if (havefoot || havelastfoot) {
2951 xs << html::StartTag("tfoot");
2952 for (row_type r = 0; r < nrows(); ++r) {
2953 if ((havelastfoot && row_info[r].endlastfoot)
2954 || (havefoot && row_info[r].endfoot)) {
2955 ret += xhtmlRow(xs, r, runparams);
2958 xs << html::EndTag("tfoot");
2961 xs << html::StartTag("tbody");
2962 for (row_type r = 0; r < nrows(); ++r) {
2963 if (isValidRow(r)) {
2964 ret += xhtmlRow(xs, r, runparams);
2967 xs << html::EndTag("tbody")
2968 << html::EndTag("table");
2969 if (is_long_tabular)
2970 xs << html::EndTag("div");
2975 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2976 vector<unsigned int> const & clen) const
2978 idx_type const fcell = getFirstCellInRow(row);
2979 idx_type const n = numberOfCellsInRow(row) + fcell;
2982 for (idx_type i = fcell; i < n; ++i) {
2992 for (idx_type i = fcell; i < n; ++i) {
3003 col_type column = cellColumn(i);
3004 int len = clen[column];
3005 while (column < ncols() - 1
3006 && isPartOfMultiColumn(row, ++column))
3007 len += clen[column] + 4;
3008 os << docstring(len, ch);
3023 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3024 vector<unsigned int> const & clen) const
3026 idx_type const fcell = getFirstCellInRow(row);
3027 idx_type const n = numberOfCellsInRow(row) + fcell;
3030 for (idx_type i = fcell; i < n; ++i) {
3031 if (bottomLine(i)) {
3040 for (idx_type i = fcell; i < n; ++i) {
3041 if (bottomLine(i)) {
3051 col_type column = cellColumn(i);
3052 int len = clen[column];
3053 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3054 len += clen[column] + 4;
3055 os << docstring(len, ch);
3056 if (bottomLine(i)) {
3070 void Tabular::plaintextPrintCell(odocstream & os,
3071 OutputParams const & runparams,
3072 idx_type cell, row_type row, col_type column,
3073 vector<unsigned int> const & clen,
3074 bool onlydata) const
3076 odocstringstream sstr;
3077 cellInset(cell)->plaintext(sstr, runparams);
3089 unsigned int len1 = sstr.str().length();
3090 unsigned int len2 = clen[column];
3091 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3092 len2 += clen[column] + 4;
3095 switch (getAlignment(cell)) {
3097 case LYX_ALIGN_LEFT:
3100 case LYX_ALIGN_RIGHT:
3104 case LYX_ALIGN_CENTER:
3110 os << docstring(len1, ' ') << sstr.str()
3111 << docstring(len2, ' ');
3113 if (rightLine(cell))
3120 void Tabular::plaintext(odocstream & os,
3121 OutputParams const & runparams, int const depth,
3122 bool onlydata, char_type delim) const
3124 // first calculate the width of the single columns
3125 vector<unsigned int> clen(ncols());
3128 // first all non multicolumn cells!
3129 for (col_type c = 0; c < ncols(); ++c) {
3131 for (row_type r = 0; r < nrows(); ++r) {
3132 idx_type cell = cellIndex(r, c);
3133 if (isMultiColumn(cell))
3135 odocstringstream sstr;
3136 cellInset(cell)->plaintext(sstr, runparams);
3137 if (clen[c] < sstr.str().length())
3138 clen[c] = sstr.str().length();
3141 // then all multicolumn cells!
3142 for (col_type c = 0; c < ncols(); ++c) {
3143 for (row_type r = 0; r < nrows(); ++r) {
3144 idx_type cell = cellIndex(r, c);
3145 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3147 odocstringstream sstr;
3148 cellInset(cell)->plaintext(sstr, runparams);
3149 int len = int(sstr.str().length());
3150 idx_type const n = columnSpan(cell);
3151 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3153 if (len > int(clen[c + n - 1]))
3154 clen[c + n - 1] = len;
3159 for (row_type r = 0; r < nrows(); ++r) {
3160 if (!onlydata && plaintextTopHLine(os, r, clen))
3161 os << docstring(depth * 2, ' ');
3162 for (col_type c = 0; c < ncols(); ++c) {
3163 if (isPartOfMultiColumn(r, c))
3165 if (onlydata && c > 0)
3166 // we don't use operator<< for single UCS4 character.
3167 // see explanation in docstream.h
3169 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3174 os << docstring(depth * 2, ' ');
3175 if (plaintextBottomHLine(os, r, clen))
3176 os << docstring(depth * 2, ' ');
3182 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3184 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3188 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3189 col_type column) const
3191 return cell_info[row][column].inset;
3195 void Tabular::setCellInset(row_type row, col_type column,
3196 shared_ptr<InsetTableCell> ins) const
3198 CellData & cd = cell_info[row][column];
3203 void Tabular::validate(LaTeXFeatures & features) const
3205 features.require("NeedTabularnewline");
3207 features.require("booktabs");
3208 if (is_long_tabular)
3209 features.require("longtable");
3211 features.require("rotating");
3212 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3213 if (isMultiRow(cell))
3214 features.require("multirow");
3215 if (getVAlignment(cell) != LYX_VALIGN_TOP
3216 || !getPWidth(cell).zero())
3217 features.require("array");
3218 cellInset(cell)->validate(features);
3223 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3225 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3226 ParagraphList::const_iterator cit = parlist.begin();
3227 ParagraphList::const_iterator end = parlist.end();
3229 for (; cit != end; ++cit)
3230 for (int i = 0; i < cit->size(); ++i)
3231 if (cit->isNewline(i))
3238 /////////////////////////////////////////////////////////////////////
3242 /////////////////////////////////////////////////////////////////////
3244 InsetTableCell::InsetTableCell(Buffer * buf)
3245 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3246 contentAlign(LYX_ALIGN_CENTER)
3250 bool InsetTableCell::forcePlainLayout(idx_type) const
3252 return !isFixedWidth;
3256 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3258 return isFixedWidth;
3262 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3263 FuncStatus & status) const
3265 bool enabled = true;
3266 switch (cmd.action()) {
3268 enabled = !forcePlainLayout();
3270 case LFUN_LAYOUT_PARAGRAPH:
3271 enabled = allowParagraphCustomization();
3274 case LFUN_MATH_DISPLAY:
3275 if (!hasFixedWidth()) {
3280 return InsetText::getStatus(cur, cmd, status);
3282 status.setEnabled(enabled);
3286 docstring InsetTableCell::asString(bool intoInsets)
3289 if (paragraphs().empty())
3291 ParagraphList::const_iterator it = paragraphs().begin();
3292 ParagraphList::const_iterator en = paragraphs().end();
3294 for (; it != en; ++it) {
3299 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3305 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3308 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3309 return InsetText::xhtml(xs, rp);
3314 /////////////////////////////////////////////////////////////////////
3318 /////////////////////////////////////////////////////////////////////
3320 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3322 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3323 rowselect_(false), colselect_(false)
3328 InsetTabular::InsetTabular(InsetTabular const & tab)
3329 : Inset(tab), tabular(tab.tabular), scx_(0)
3334 InsetTabular::~InsetTabular()
3336 hideDialogs("tabular", this);
3340 void InsetTabular::setBuffer(Buffer & buf)
3342 tabular.setBuffer(buf);
3343 Inset::setBuffer(buf);
3347 bool InsetTabular::insetAllowed(InsetCode code) const
3352 case MATHMACRO_CODE:
3357 return tabular.is_long_tabular;
3365 void InsetTabular::write(ostream & os) const
3367 os << "Tabular" << endl;
3372 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3374 // FIXME: depending on the selection state,
3375 // we could offer a different menu.
3376 return cell(0)->contextMenuName() + ";" + contextMenuName();
3380 docstring InsetTabular::contextMenuName() const
3382 return from_ascii("context-tabular");
3386 void InsetTabular::read(Lexer & lex)
3388 //bool const old_format = (lex.getString() == "\\LyXTable");
3396 string token = lex.getString();
3397 while (lex && token != "\\end_inset") {
3399 token = lex.getString();
3402 lex.printError("Missing \\end_inset at this point. ");
3406 int InsetTabular::rowFromY(Cursor & cur, int y) const
3408 // top y coordinate of tabular
3409 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3411 for (; r < tabular.nrows() && y > h; ++r)
3412 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3413 + tabular.interRowSpace(r);
3419 int InsetTabular::columnFromX(Cursor & cur, int x) const
3421 // left x coordinate of tabular
3422 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3424 for (; c < tabular.ncols() && x > w; ++c)
3425 w += tabular.cellWidth(c);
3430 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3432 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3433 // mi.base.textwidth << "\n";
3436 LASSERT(false, /**/);
3439 for (row_type r = 0; r < tabular.nrows(); ++r) {
3442 for (col_type c = 0; c < tabular.ncols(); ++c) {
3443 if (tabular.isPartOfMultiColumn(r, c)
3444 || tabular.isPartOfMultiRow(r, c))
3445 // multicolumn or multirow cell, but not first one
3447 idx_type const cell = tabular.cellIndex(r, c);
3450 Length const p_width = tabular.getPWidth(cell);
3451 if (!p_width.zero())
3452 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3453 tabular.cellInset(cell)->metrics(m, dim);
3454 if (!p_width.zero())
3455 dim.wid = m.base.textwidth;
3456 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3457 + tabular.interColumnSpace(cell);
3459 // FIXME(?): do we need a second metrics call?
3460 TextMetrics const & tm =
3461 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3463 // determine horizontal offset because of decimal align (if necessary)
3464 int decimal_hoffset = 0;
3465 int decimal_width = 0;
3466 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3467 // make a copy which we will split in 2
3468 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3469 head.getText(0)->setMacrocontextPosition(
3470 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3471 head.setBuffer(tabular.buffer());
3472 // split in 2 and calculate width of each part
3473 bool hassep = false;
3474 InsetTableCell tail =
3475 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3476 tail.getText(0)->setMacrocontextPosition(
3477 head.getText(0)->macrocontextPosition());
3478 tail.setBuffer(head.buffer());
3480 head.metrics(m, dim1);
3481 decimal_hoffset = dim1.width();
3483 tail.metrics(m, dim1);
3484 decimal_width = dim1.width();
3487 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3488 tabular.cell_info[r][c].decimal_width = decimal_width;
3490 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3491 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3492 int const lastpardes = tm.last().second->descent()
3493 + TEXT_TO_INSET_OFFSET;
3495 switch (tabular.getVAlignment(cell)) {
3496 case Tabular::LYX_VALIGN_TOP:
3498 case Tabular::LYX_VALIGN_MIDDLE:
3499 offset = -(dim.des - lastpardes)/2;
3501 case Tabular::LYX_VALIGN_BOTTOM:
3502 offset = -(dim.des - lastpardes);
3505 tabular.cell_info[r][c].voffset = offset;
3506 maxasc = max(maxasc, dim.asc - offset);
3507 maxdes = max(maxdes, dim.des + offset);
3509 int const top_space = tabular.row_info[r].top_space_default ?
3510 default_line_space :
3511 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3512 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3513 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3514 default_line_space :
3515 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3516 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3519 // for top-alignment the first horizontal table line must be exactly at
3520 // the position of the base line of the surrounding text line
3521 // for bottom alignment, the same is for the last table line
3522 switch (tabular.tabular_valignment) {
3523 case Tabular::LYX_VALIGN_BOTTOM:
3524 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3526 case Tabular::LYX_VALIGN_MIDDLE:
3527 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3529 case Tabular::LYX_VALIGN_TOP:
3530 offset_valign_ = tabular.rowAscent(0);
3534 tabular.updateColumnWidths();
3535 dim.asc = tabular.rowAscent(0) - offset_valign_;
3536 dim.des = tabular.height() - dim.asc;
3537 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3541 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3544 if (&cur.inset() == this && cur.selection()) {
3545 if (cur.selIsMultiCell()) {
3548 getSelection(cur, rs, re, cs, ce);
3550 idx_type const cell = tabular.cellIndex(row, col);
3551 col_type const cspan = tabular.columnSpan(cell);
3552 row_type const rspan = tabular.rowSpan(cell);
3553 if (col + cspan - 1 >= cs && col <= ce
3554 && row + rspan - 1 >= rs && row <= re)
3557 if (col == tabular.cellColumn(cur.idx())
3558 && row == tabular.cellRow(cur.idx())) {
3559 CursorSlice const & beg = cur.selBegin();
3560 CursorSlice const & end = cur.selEnd();
3562 if ((end.lastpos() > 0 || end.lastpit() > 0)
3563 && end.pos() == end.lastpos() && beg.pos() == 0
3564 && end.pit() == end.lastpit() && beg.pit() == 0)
3572 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3574 x += scx_ + ADD_TO_TABULAR_WIDTH;
3576 BufferView * bv = pi.base.bv;
3577 Cursor & cur = pi.base.bv->cursor();
3580 // FIXME: As the full background is painted in drawBackground(),
3581 // we have no choice but to do a full repaint for the Text cells.
3582 pi.full_repaint = true;
3584 bool const original_selection_state = pi.selected;
3587 first_visible_cell = Tabular::npos;
3589 int yy = y + offset_valign_;
3590 for (row_type r = 0; r < tabular.nrows(); ++r) {
3592 for (col_type c = 0; c < tabular.ncols(); ++c) {
3593 if (tabular.isPartOfMultiColumn(r, c))
3596 idx = tabular.cellIndex(r, c);
3598 if (tabular.isPartOfMultiRow(r, c)) {
3599 nx += tabular.cellWidth(idx);
3603 if (first_visible_cell == Tabular::npos)
3604 first_visible_cell = idx;
3606 pi.selected |= isCellSelected(cur, r, c);
3607 int const cx = nx + tabular.textHOffset(idx);
3608 int const cy = yy + tabular.textVOffset(idx);
3609 // Cache the Inset position.
3610 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3611 cell(idx)->draw(pi, cx, cy);
3612 drawCellLines(pi, nx, yy, r, idx);
3613 nx += tabular.cellWidth(idx);
3614 pi.selected = original_selection_state;
3617 if (r + 1 < tabular.nrows())
3618 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3619 + tabular.interRowSpace(r + 1);
3624 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3626 x += scx_ + ADD_TO_TABULAR_WIDTH;
3627 y += offset_valign_ - tabular.rowAscent(0);
3628 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3629 pi.backgroundColor(this));
3633 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3635 Cursor & cur = pi.base.bv->cursor();
3638 x += scx_ + ADD_TO_TABULAR_WIDTH;
3640 if (!cur.selection())
3642 if (&cur.inset() != this)
3647 bool const full_cell_selected = isCellSelected(cur,
3648 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3650 if (cur.selIsMultiCell() || full_cell_selected) {
3651 for (row_type r = 0; r < tabular.nrows(); ++r) {
3653 for (col_type c = 0; c < tabular.ncols(); ++c) {
3654 if (tabular.isPartOfMultiColumn(r, c))
3657 idx_type const cell = tabular.cellIndex(r, c);
3659 if (tabular.isPartOfMultiRow(r, c)) {
3660 xx += tabular.cellWidth(cell);
3663 int const w = tabular.cellWidth(cell);
3664 int const h = tabular.cellHeight(cell);
3665 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3666 if (isCellSelected(cur, r, c))
3667 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3670 if (r + 1 < tabular.nrows())
3671 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3672 + tabular.interRowSpace(r + 1);
3676 // FIXME: This code has no effect because InsetTableCell does not handle
3677 // drawSelection other than the trivial implementation in Inset.
3679 // x += cellXPos(cur.idx());
3680 // x += tabular.textHOffset(cur.idx());
3681 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3686 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3687 row_type row, idx_type cell) const
3689 y -= tabular.rowAscent(row);
3690 int const w = tabular.cellWidth(cell);
3691 int const h = tabular.cellHeight(cell);
3692 Color const linecolor = pi.textColor(Color_tabularline);
3693 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3696 bool drawline = tabular.topLine(cell)
3697 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3698 pi.pain.line(x, y, x + w, y,
3699 drawline ? linecolor : gridcolor,
3700 drawline ? Painter::line_solid : Painter::line_onoffdash);
3703 drawline = tabular.bottomLine(cell);
3704 pi.pain.line(x, y + h, x + w, y + h,
3705 drawline ? linecolor : gridcolor,
3706 drawline ? Painter::line_solid : Painter::line_onoffdash);
3709 col_type const col = tabular.cellColumn(cell);
3710 drawline = tabular.leftLine(cell)
3711 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3712 pi.pain.line(x, y, x, y + h,
3713 drawline ? linecolor : gridcolor,
3714 drawline ? Painter::line_solid : Painter::line_onoffdash);
3717 x -= tabular.interColumnSpace(cell);
3718 drawline = tabular.rightLine(cell)
3719 || (col + 1 < tabular.ncols()
3720 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3721 pi.pain.line(x + w, y, x + w, y + h,
3722 drawline ? linecolor : gridcolor,
3723 drawline ? Painter::line_solid : Painter::line_onoffdash);
3727 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3729 //lyxerr << "InsetTabular::edit: " << this << endl;
3731 cur.setSelection(false);
3734 if (isRightToLeft(cur))
3735 cur.idx() = tabular.getLastCellInRow(0);
3741 if (isRightToLeft(cur))
3742 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3744 cur.idx() = tabular.numberofcells - 1;
3746 cur.pos() = cur.lastpos(); // FIXME crude guess
3748 cur.setCurrentFont();
3749 // FIXME: this accesses the position cache before it is initialized
3751 //cur.bv().fitCursor();
3755 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3757 // In a longtable, tell captions what the current float is
3758 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3759 string const saveflt = cnts.current_float();
3760 if (tabular.is_long_tabular)
3761 cnts.current_float("table");
3763 ParIterator it2 = it;
3765 size_t const end = it2.nargs();
3766 for ( ; it2.idx() < end; it2.top().forwardIdx())
3767 buffer().updateBuffer(it2, utype);
3770 if (tabular.is_long_tabular)
3771 cnts.current_float(saveflt);
3775 void InsetTabular::addToToc(DocIterator const & cpit) const
3777 DocIterator dit = cpit;
3779 size_t const end = dit.nargs();
3780 for ( ; dit.idx() < end; dit.top().forwardIdx())
3781 cell(dit.idx())->addToToc(dit);
3785 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3787 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3788 << "\n cur:" << cur);
3789 CursorSlice sl = cur.top();
3790 Cursor & bvcur = cur.bv().cursor();
3792 FuncCode const act = cmd.action();
3796 case LFUN_MOUSE_PRESS: {
3797 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3799 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3800 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3801 row_type r = rowFromY(cur, cmd.y());
3802 cur.idx() = tabular.getFirstCellInRow(r);
3805 cur.idx() = tabular.getLastCellInRow(r);
3806 cur.pos() = cur.lastpos();
3807 cur.setSelection(true);
3813 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3814 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3815 || cmd.y() > y0 + tabular.height()) {
3816 col_type c = columnFromX(cur, cmd.x());
3817 cur.idx() = tabular.cellIndex(0, c);
3820 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3821 cur.pos() = cur.lastpos();
3822 cur.setSelection(true);
3827 // do not reset cursor/selection if we have selected
3828 // some cells (bug 2715).
3829 if (cmd.button() == mouse_button::button3
3830 && &bvcur.selBegin().inset() == this
3831 && bvcur.selIsMultiCell())
3834 // Let InsetTableCell do it
3835 cell(cur.idx())->dispatch(cur, cmd);
3838 case LFUN_MOUSE_MOTION:
3839 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3840 if (cmd.button() == mouse_button::button1) {
3841 // only accept motions to places not deeper nested than the real anchor
3842 if (!bvcur.realAnchor().hasPart(cur)) {
3846 // select (additional) row
3848 row_type r = rowFromY(cur, cmd.y());
3849 cur.idx() = tabular.getLastCellInRow(r);
3850 // we need to reset the cursor's pit and pos now, as the old ones
3851 // may no longer be valid.
3854 bvcur.setCursor(cur);
3855 bvcur.setSelection(true);
3858 // select (additional) column
3860 col_type c = columnFromX(cur, cmd.x());
3861 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3862 // we need to reset the cursor's pit and pos now, as the old ones
3863 // may no longer be valid.
3866 bvcur.setCursor(cur);
3867 bvcur.setSelection(true);
3870 // only update if selection changes
3871 if (bvcur.idx() == cur.idx() &&
3872 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3873 cur.noScreenUpdate();
3874 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3875 bvcur.setCursor(cur);
3876 bvcur.setSelection(true);
3877 // if this is a multicell selection, we just set the cursor to
3878 // the beginning of the cell's text.
3879 if (bvcur.selIsMultiCell()) {
3880 bvcur.pit() = bvcur.lastpit();
3881 bvcur.pos() = bvcur.lastpos();
3886 case LFUN_MOUSE_RELEASE:
3891 case LFUN_CELL_BACKWARD:
3893 cur.setSelection(false);
3896 case LFUN_CELL_FORWARD:
3898 cur.setSelection(false);
3901 case LFUN_CHAR_FORWARD_SELECT:
3902 case LFUN_CHAR_FORWARD:
3903 case LFUN_CHAR_BACKWARD_SELECT:
3904 case LFUN_CHAR_BACKWARD:
3905 case LFUN_CHAR_RIGHT_SELECT:
3906 case LFUN_CHAR_RIGHT:
3907 case LFUN_CHAR_LEFT_SELECT:
3908 case LFUN_CHAR_LEFT:
3909 case LFUN_WORD_FORWARD:
3910 case LFUN_WORD_FORWARD_SELECT:
3911 case LFUN_WORD_BACKWARD:
3912 case LFUN_WORD_BACKWARD_SELECT:
3913 case LFUN_WORD_RIGHT:
3914 case LFUN_WORD_RIGHT_SELECT:
3915 case LFUN_WORD_LEFT:
3916 case LFUN_WORD_LEFT_SELECT: {
3917 // determine whether we move to next or previous cell, where to enter
3918 // the new cell from, and which command to "finish" (i.e., exit the
3921 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3922 FuncCode finish_lfun;
3924 if (act == LFUN_CHAR_FORWARD
3925 || act == LFUN_CHAR_FORWARD_SELECT
3926 || act == LFUN_WORD_FORWARD
3927 || act == LFUN_WORD_FORWARD_SELECT) {
3929 finish_lfun = LFUN_FINISHED_FORWARD;
3931 else if (act == LFUN_CHAR_BACKWARD
3932 || act == LFUN_CHAR_BACKWARD_SELECT
3933 || act == LFUN_WORD_BACKWARD
3934 || act == LFUN_WORD_BACKWARD_SELECT) {
3936 finish_lfun = LFUN_FINISHED_BACKWARD;
3938 // LEFT or RIGHT commands --- the interpretation will depend on the
3939 // table's direction.
3941 bool const right = act == LFUN_CHAR_RIGHT
3942 || act == LFUN_CHAR_RIGHT_SELECT
3943 || act == LFUN_WORD_RIGHT
3944 || act == LFUN_WORD_RIGHT_SELECT;
3945 next_cell = isRightToLeft(cur) != right;
3947 if (lyxrc.visual_cursor)
3948 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3950 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3953 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3954 || act == LFUN_CHAR_BACKWARD_SELECT
3955 || act == LFUN_CHAR_RIGHT_SELECT
3956 || act == LFUN_CHAR_LEFT_SELECT
3957 || act == LFUN_WORD_FORWARD_SELECT
3958 || act == LFUN_WORD_RIGHT_SELECT
3959 || act == LFUN_WORD_BACKWARD_SELECT
3960 || act == LFUN_WORD_LEFT_SELECT;
3962 // If we have a multicell selection or we're
3963 // not doing some LFUN_*_SELECT thing anyway...
3964 if (!cur.selIsMultiCell() || !select) {
3965 col_type const c = tabular.cellColumn(cur.idx());
3966 row_type const r = tabular.cellRow(cur.idx());
3967 // Are we trying to select the whole cell and is the whole cell
3968 // not yet selected?
3969 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3970 ((next_cell && cur.pit() == cur.lastpit()
3971 && cur.pos() == cur.lastpos())
3972 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3974 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3976 // ...try to dispatch to the cell's inset.
3977 cell(cur.idx())->dispatch(cur, cmd);
3979 // When we already have a selection we want to select the whole cell
3980 // before going to the next cell.
3981 if (select_whole && !empty_cell){
3982 getText(cur.idx())->selectAll(cur);
3987 // FIXME: When we support the selection of an empty cell, remove
3988 // the !empty_cell from this condition. For now we jump to the next
3989 // cell if the current cell is empty.
3990 if (cur.result().dispatched() && !empty_cell)
3994 // move to next/prev cell, as appropriate
3995 // note that we will always do this if we're selecting and we have
3996 // a multicell selection
3997 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3998 << " cell from: " << int(entry_from));
4000 moveNextCell(cur, entry_from);
4002 movePrevCell(cur, entry_from);
4003 // if we're exiting the table, call the appropriate FINISHED lfun
4004 if (sl == cur.top())
4005 cmd = FuncRequest(finish_lfun);
4009 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4014 case LFUN_DOWN_SELECT:
4016 if (!(cur.selection() && cur.selIsMultiCell()))
4017 cell(cur.idx())->dispatch(cur, cmd);
4019 cur.dispatched(); // override the cell's decision
4020 if (sl == cur.top()) {
4021 // if our Text didn't do anything to the cursor
4022 // then we try to put the cursor into the cell below
4023 // setting also the right targetX.
4024 cur.selHandle(act == LFUN_DOWN_SELECT);
4025 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4026 cur.idx() = tabular.cellBelow(cur.idx());
4028 TextMetrics const & tm =
4029 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4030 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
4031 cur.setCurrentFont();
4034 if (sl == cur.top()) {
4035 // we trick it to go to forward after leaving the
4037 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4040 if (cur.selIsMultiCell()) {
4041 cur.pit() = cur.lastpit();
4042 cur.pos() = cur.lastpos();
4043 cur.setCurrentFont();
4046 cur.screenUpdateFlags(Update::FitCursor);
4049 case LFUN_UP_SELECT:
4051 if (!(cur.selection() && cur.selIsMultiCell()))
4052 cell(cur.idx())->dispatch(cur, cmd);
4053 cur.dispatched(); // override the cell's decision
4054 if (sl == cur.top()) {
4055 // if our Text didn't do anything to the cursor
4056 // then we try to put the cursor into the cell above
4057 // setting also the right targetX.
4058 cur.selHandle(act == LFUN_UP_SELECT);
4059 if (tabular.cellRow(cur.idx()) != 0) {
4060 cur.idx() = tabular.cellAbove(cur.idx());
4061 cur.pit() = cur.lastpit();
4062 Text const * text = cell(cur.idx())->getText(0);
4063 TextMetrics const & tm = cur.bv().textMetrics(text);
4064 ParagraphMetrics const & pm =
4065 tm.parMetrics(cur.lastpit());
4066 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
4067 cur.setCurrentFont();
4070 if (sl == cur.top()) {
4071 cmd = FuncRequest(LFUN_UP);
4074 if (cur.selIsMultiCell()) {
4076 cur.pos() = cur.lastpos();
4077 cur.setCurrentFont();
4080 cur.screenUpdateFlags(Update::FitCursor);
4083 // case LFUN_SCREEN_DOWN: {
4084 // //if (hasSelection())
4085 // // cur.selection() = false;
4086 // col_type const col = tabular.cellColumn(cur.idx());
4087 // int const t = cur.bv().top_y() + cur.bv().height();
4088 // if (t < yo() + tabular.getHeightOfTabular()) {
4089 // cur.bv().scrollDocView(t, true);
4090 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4092 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4099 // case LFUN_SCREEN_UP: {
4100 // //if (hasSelection())
4101 // // cur.selection() = false;
4102 // col_type const col = tabular.cellColumn(cur.idx());
4103 // int const t = cur.bv().top_y() + cur.bv().height();
4105 // cur.bv().scrollDocView(t, true);
4109 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4113 // cur.par() = cur.lastpar();
4114 // cur.pos() = cur.lastpos();
4118 case LFUN_LAYOUT_TABULAR:
4119 cur.bv().showDialog("tabular");
4122 case LFUN_INSET_MODIFY:
4123 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4127 // insert file functions
4128 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4129 case LFUN_FILE_INSERT_PLAINTEXT:
4131 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4132 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4133 FileName(to_utf8(cmd.argument())));
4136 cur.recordUndoInset(INSERT_UNDO);
4137 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4138 // content has been replaced,
4139 // so cursor might be invalid
4140 cur.pos() = cur.lastpos();
4141 cur.pit() = cur.lastpit();
4142 bvcur.setCursor(cur);
4149 if (cur.selIsMultiCell()) {
4150 if (copySelection(cur)) {
4151 cur.recordUndoInset(DELETE_UNDO);
4155 cell(cur.idx())->dispatch(cur, cmd);
4158 case LFUN_SELF_INSERT:
4159 if (cur.selIsMultiCell()) {
4160 cur.recordUndoInset(DELETE_UNDO);
4163 cell(cur.idx())->dispatch(cur, cmd);
4166 case LFUN_CHAR_DELETE_BACKWARD:
4167 case LFUN_CHAR_DELETE_FORWARD:
4168 if (cur.selIsMultiCell()) {
4169 cur.recordUndoInset(DELETE_UNDO);
4172 cell(cur.idx())->dispatch(cur, cmd);
4176 if (!cur.selection())
4178 if (cur.selIsMultiCell()) {
4182 cell(cur.idx())->dispatch(cur, cmd);
4185 case LFUN_CLIPBOARD_PASTE:
4186 case LFUN_PRIMARY_SELECTION_PASTE: {
4187 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4188 theClipboard().getAsText() :
4189 theSelection().get();
4192 // pass to InsertPlaintextString, but
4193 // only if we have multi-cell content
4194 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4195 cur.recordUndoInset(INSERT_UNDO);
4196 if (insertPlaintextString(cur.bv(), clip, false)) {
4197 // content has been replaced,
4198 // so cursor might be invalid
4199 cur.pos() = cur.lastpos();
4200 cur.pit() = cur.lastpit();
4201 bvcur.setCursor(cur);
4205 // Let the cell handle normal text
4206 cell(cur.idx())->dispatch(cur, cmd);
4211 if (!tabularStackDirty()) {
4212 if (!cur.selIsMultiCell())
4213 cell(cur.idx())->dispatch(cur, cmd);
4216 if (theClipboard().isInternal()) {
4217 cur.recordUndoInset(INSERT_UNDO);
4218 pasteClipboard(cur);
4222 case LFUN_FONT_EMPH:
4223 case LFUN_FONT_BOLD:
4224 case LFUN_FONT_BOLDSYMBOL:
4225 case LFUN_FONT_ROMAN:
4226 case LFUN_FONT_NOUN:
4227 case LFUN_FONT_ITAL:
4228 case LFUN_FONT_FRAK:
4229 case LFUN_FONT_TYPEWRITER:
4230 case LFUN_FONT_SANS:
4231 case LFUN_TEXTSTYLE_APPLY:
4232 case LFUN_TEXTSTYLE_UPDATE:
4233 case LFUN_FONT_SIZE:
4234 case LFUN_FONT_UNDERLINE:
4235 case LFUN_FONT_STRIKEOUT:
4236 case LFUN_FONT_UULINE:
4237 case LFUN_FONT_UWAVE:
4239 case LFUN_WORD_CAPITALIZE:
4240 case LFUN_WORD_UPCASE:
4241 case LFUN_WORD_LOWCASE:
4242 case LFUN_CHARS_TRANSPOSE:
4243 if (cur.selIsMultiCell()) {
4246 getSelection(cur, rs, re, cs, ce);
4247 Cursor tmpcur = cur;
4248 for (row_type r = rs; r <= re; ++r) {
4249 for (col_type c = cs; c <= ce; ++c) {
4250 // cursor follows cell:
4251 tmpcur.idx() = tabular.cellIndex(r, c);
4252 // select this cell only:
4255 tmpcur.resetAnchor();
4256 tmpcur.pit() = tmpcur.lastpit();
4257 tmpcur.pos() = tmpcur.top().lastpos();
4258 tmpcur.setCursor(tmpcur);
4259 tmpcur.setSelection();
4260 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4265 cell(cur.idx())->dispatch(cur, cmd);
4269 case LFUN_INSET_SETTINGS:
4270 // relay this lfun to Inset, not to the cell.
4271 Inset::doDispatch(cur, cmd);
4275 // we try to handle this event in the insets dispatch function.
4276 cell(cur.idx())->dispatch(cur, cmd);
4282 // function sets an object as defined in func_status.h:
4283 // states OK, Unknown, Disabled, On, Off.
4284 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4285 FuncStatus & status) const
4287 switch (cmd.action()) {
4288 case LFUN_INSET_MODIFY: {
4289 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4292 // FIXME: We only check for the very first argument...
4293 string const s = cmd.getArg(1);
4294 // We always enable the lfun if it is coming from the dialog
4295 // because the dialog makes sure all the settings are valid,
4296 // even though the first argument might not be valid now.
4297 if (s == "from-dialog") {
4298 status.setEnabled(true);
4302 int action = Tabular::LAST_ACTION;
4304 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4305 if (tabularFeature[i].feature == s) {
4306 action = tabularFeature[i].action;
4310 if (action == Tabular::LAST_ACTION) {
4312 status.setUnknown(true);
4316 string const argument = cmd.getLongArg(2);
4318 row_type sel_row_start = 0;
4319 row_type sel_row_end = 0;
4320 col_type sel_col_start = 0;
4321 col_type sel_col_end = 0;
4322 Tabular::ltType dummyltt;
4325 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4328 case Tabular::SET_PWIDTH:
4329 case Tabular::SET_MPWIDTH:
4330 case Tabular::SET_SPECIAL_COLUMN:
4331 case Tabular::SET_SPECIAL_MULTICOLUMN:
4332 case Tabular::APPEND_ROW:
4333 case Tabular::APPEND_COLUMN:
4334 case Tabular::DELETE_ROW:
4335 case Tabular::DELETE_COLUMN:
4336 case Tabular::COPY_ROW:
4337 case Tabular::COPY_COLUMN:
4338 case Tabular::SET_TOP_SPACE:
4339 case Tabular::SET_BOTTOM_SPACE:
4340 case Tabular::SET_INTERLINE_SPACE:
4344 case Tabular::SET_TABULAR_WIDTH:
4345 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4346 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4349 case Tabular::SET_DECIMAL_POINT:
4351 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4354 case Tabular::SET_MULTICOLUMN:
4355 case Tabular::UNSET_MULTICOLUMN:
4356 case Tabular::MULTICOLUMN:
4357 // If a row is set as longtable caption, it must not be allowed
4358 // to unset that this row is a multicolumn.
4359 status.setEnabled(sel_row_start == sel_row_end
4360 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4361 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4364 case Tabular::SET_MULTIROW:
4365 case Tabular::UNSET_MULTIROW:
4366 case Tabular::MULTIROW:
4367 // If a row is set as longtable caption, it must not be allowed
4368 // to unset that this row is a multirow.
4369 status.setEnabled(sel_col_start == sel_col_end
4370 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4371 status.setOnOff(tabular.isMultiRow(cur.idx()));
4374 case Tabular::SET_ALL_LINES:
4375 case Tabular::UNSET_ALL_LINES:
4376 case Tabular::SET_BORDER_LINES:
4377 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4380 case Tabular::SET_LINE_TOP:
4381 case Tabular::SET_LINE_BOTTOM:
4382 case Tabular::SET_LINE_LEFT:
4383 case Tabular::SET_LINE_RIGHT:
4384 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4387 case Tabular::TOGGLE_LINE_TOP:
4388 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4389 status.setOnOff(tabular.topLine(cur.idx()));
4392 case Tabular::TOGGLE_LINE_BOTTOM:
4393 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4394 status.setOnOff(tabular.bottomLine(cur.idx()));
4397 case Tabular::TOGGLE_LINE_LEFT:
4398 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4399 status.setOnOff(tabular.leftLine(cur.idx()));
4402 case Tabular::TOGGLE_LINE_RIGHT:
4403 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4404 status.setOnOff(tabular.rightLine(cur.idx()));
4407 // multirow cells only inherit the alignment of the column if the column has
4408 // no width, otherwise they are left-aligned
4409 // therefore allow always left but right and center only if there is no width
4410 case Tabular::M_ALIGN_LEFT:
4412 case Tabular::ALIGN_LEFT:
4413 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4416 case Tabular::M_ALIGN_RIGHT:
4418 case Tabular::ALIGN_RIGHT:
4419 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4420 && !tabular.getPWidth(cur.idx()).zero()));
4421 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4424 case Tabular::M_ALIGN_CENTER:
4426 case Tabular::ALIGN_CENTER:
4427 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4428 && !tabular.getPWidth(cur.idx()).zero()));
4429 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4432 case Tabular::ALIGN_BLOCK:
4433 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4434 && !tabular.isMultiRow(cur.idx()));
4435 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4438 case Tabular::ALIGN_DECIMAL:
4439 status.setEnabled(!tabular.isMultiRow(cur.idx())
4440 && !tabular.isMultiColumn(cur.idx()));
4441 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4444 case Tabular::M_VALIGN_TOP:
4446 case Tabular::VALIGN_TOP:
4447 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4448 && !tabular.isMultiRow(cur.idx()));
4450 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4453 case Tabular::M_VALIGN_BOTTOM:
4455 case Tabular::VALIGN_BOTTOM:
4456 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4457 && !tabular.isMultiRow(cur.idx()));
4459 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4462 case Tabular::M_VALIGN_MIDDLE:
4464 case Tabular::VALIGN_MIDDLE:
4465 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4466 && !tabular.isMultiRow(cur.idx()));
4468 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4471 case Tabular::SET_LONGTABULAR:
4472 // setting as longtable is not allowed when table is inside a float
4473 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4474 || cur.innerInsetOfType(WRAP_CODE) != 0)
4475 status.setEnabled(false);
4477 status.setEnabled(true);
4478 status.setOnOff(tabular.is_long_tabular);
4481 case Tabular::UNSET_LONGTABULAR:
4482 status.setOnOff(!tabular.is_long_tabular);
4485 case Tabular::TOGGLE_ROTATE_TABULAR:
4486 case Tabular::SET_ROTATE_TABULAR:
4487 status.setEnabled(tabular.tabular_width.zero());
4488 status.setOnOff(tabular.rotate);
4491 case Tabular::TABULAR_VALIGN_TOP:
4492 status.setEnabled(tabular.tabular_width.zero());
4493 status.setOnOff(tabular.tabular_valignment
4494 == Tabular::LYX_VALIGN_TOP);
4496 case Tabular::TABULAR_VALIGN_MIDDLE:
4497 status.setEnabled(tabular.tabular_width.zero());
4498 status.setOnOff(tabular.tabular_valignment
4499 == Tabular::LYX_VALIGN_MIDDLE);
4501 case Tabular::TABULAR_VALIGN_BOTTOM:
4502 status.setEnabled(tabular.tabular_width.zero());
4503 status.setOnOff(tabular.tabular_valignment
4504 == Tabular::LYX_VALIGN_BOTTOM);
4507 case Tabular::LONGTABULAR_ALIGN_LEFT:
4508 status.setOnOff(tabular.longtabular_alignment
4509 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4511 case Tabular::LONGTABULAR_ALIGN_CENTER:
4512 status.setOnOff(tabular.longtabular_alignment
4513 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4515 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4516 status.setOnOff(tabular.longtabular_alignment
4517 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4520 case Tabular::UNSET_ROTATE_TABULAR:
4521 status.setOnOff(!tabular.rotate);
4524 case Tabular::TOGGLE_ROTATE_CELL:
4525 case Tabular::SET_ROTATE_CELL:
4526 status.setOnOff(!oneCellHasRotationState(false,
4527 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4530 case Tabular::UNSET_ROTATE_CELL:
4531 status.setOnOff(!oneCellHasRotationState(true,
4532 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4535 case Tabular::SET_USEBOX:
4536 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4539 // every row can only be one thing:
4540 // either a footer or header or caption
4541 case Tabular::SET_LTFIRSTHEAD:
4542 status.setEnabled(sel_row_start == sel_row_end
4543 && !tabular.ltCaption(sel_row_start));
4544 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4547 case Tabular::UNSET_LTFIRSTHEAD:
4548 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4551 case Tabular::SET_LTHEAD:
4552 status.setEnabled(sel_row_start == sel_row_end
4553 && !tabular.ltCaption(sel_row_start));
4554 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4557 case Tabular::UNSET_LTHEAD:
4558 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4561 case Tabular::SET_LTFOOT:
4562 status.setEnabled(sel_row_start == sel_row_end
4563 && !tabular.ltCaption(sel_row_start));
4564 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4567 case Tabular::UNSET_LTFOOT:
4568 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4571 case Tabular::SET_LTLASTFOOT:
4572 status.setEnabled(sel_row_start == sel_row_end
4573 && !tabular.ltCaption(sel_row_start));
4574 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4577 case Tabular::UNSET_LTLASTFOOT:
4578 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4581 case Tabular::SET_LTNEWPAGE:
4582 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4585 // only one row can be the caption
4586 // and a multirow cannot be set as caption
4587 case Tabular::SET_LTCAPTION:
4588 case Tabular::UNSET_LTCAPTION:
4589 case Tabular::TOGGLE_LTCAPTION:
4590 status.setEnabled(sel_row_start == sel_row_end
4591 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4592 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4593 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4594 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4595 && (!tabular.haveLTCaption()
4596 || tabular.ltCaption(sel_row_start))
4597 && !tabular.isMultiRow(sel_row_start));
4598 status.setOnOff(tabular.ltCaption(sel_row_start));
4601 case Tabular::SET_BOOKTABS:
4602 status.setOnOff(tabular.use_booktabs);
4605 case Tabular::UNSET_BOOKTABS:
4606 status.setOnOff(!tabular.use_booktabs);
4611 status.setEnabled(false);
4617 // These are only enabled inside tabular
4618 case LFUN_CELL_BACKWARD:
4619 case LFUN_CELL_FORWARD:
4620 status.setEnabled(true);
4623 // disable these with multiple cells selected
4624 case LFUN_INSET_INSERT:
4625 case LFUN_TABULAR_INSERT:
4626 case LFUN_FLEX_INSERT:
4627 case LFUN_FLOAT_INSERT:
4628 case LFUN_FLOAT_WIDE_INSERT:
4629 case LFUN_FOOTNOTE_INSERT:
4630 case LFUN_MARGINALNOTE_INSERT:
4631 case LFUN_MATH_INSERT:
4632 case LFUN_MATH_MODE:
4633 case LFUN_MATH_MUTATE:
4634 case LFUN_MATH_DISPLAY:
4635 case LFUN_NOTE_INSERT:
4636 case LFUN_ARGUMENT_INSERT:
4637 case LFUN_BOX_INSERT:
4638 case LFUN_BRANCH_INSERT:
4639 case LFUN_PHANTOM_INSERT:
4640 case LFUN_WRAP_INSERT:
4641 case LFUN_PREVIEW_INSERT:
4642 case LFUN_ERT_INSERT: {
4643 if (cur.selIsMultiCell()) {
4644 status.setEnabled(false);
4647 return cell(cur.idx())->getStatus(cur, cmd, status);
4650 // disable in non-fixed-width cells
4651 case LFUN_BREAK_PARAGRAPH:
4652 // multirow does not allow paragraph breaks
4653 if (tabular.isMultiRow(cur.idx())) {
4654 status.setEnabled(false);
4657 case LFUN_NEWLINE_INSERT: {
4658 if (tabular.getPWidth(cur.idx()).zero()) {
4659 status.setEnabled(false);
4662 return cell(cur.idx())->getStatus(cur, cmd, status);
4665 case LFUN_NEWPAGE_INSERT:
4666 status.setEnabled(false);
4670 if (tabularStackDirty() && theClipboard().isInternal()) {
4671 if (cur.selIsMultiCell()) {
4674 getSelection(cur, rs, re, cs, ce);
4675 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4676 && paste_tabular->nrows() == re - rs + 1)
4677 status.setEnabled(true);
4679 status.setEnabled(false);
4680 status.message(_("Selection size should match clipboard content."));
4683 status.setEnabled(true);
4686 return cell(cur.idx())->getStatus(cur, cmd, status);
4688 case LFUN_INSET_SETTINGS:
4689 // relay this lfun to Inset, not to the cell.
4690 return Inset::getStatus(cur, cmd, status);
4693 // we try to handle this event in the insets dispatch function.
4694 return cell(cur.idx())->getStatus(cur, cmd, status);
4700 Inset::DisplayType InsetTabular::display() const
4702 if (tabular.is_long_tabular) {
4703 switch (tabular.longtabular_alignment) {
4704 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4706 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4708 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4718 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4720 tabular.latex(os, runparams);
4724 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4726 os << '\n'; // output table on a new line
4727 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4728 tabular.plaintext(os, runparams, dp, false, 0);
4729 return PLAINTEXT_NEWLINE;
4733 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4738 // FIXME: Why not pass a proper DocIterator here?
4740 // if the table is inside a float it doesn't need the informaltable
4741 // wrapper. Search for it.
4742 for (master = owner(); master; master = master->owner())
4743 if (master->lyxCode() == FLOAT_CODE)
4748 os << "<informaltable>";
4751 ret += tabular.docbook(os, runparams);
4753 os << "</informaltable>";
4760 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4762 return tabular.xhtml(xs, rp);
4766 void InsetTabular::validate(LaTeXFeatures & features) const
4768 tabular.validate(features);
4770 // It'd be better to be able to get this from an InsetLayout, but at present
4771 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4772 if (features.runparams().flavor == OutputParams::HTML)
4773 features.addPreambleSnippet("<style type=\"text/css\">\n"
4774 "table { border: 1px solid black; display: inline-block; }\n"
4775 "td { border: 1px solid black; padding: 0.5ex; }\n"
4780 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4782 return tabular.cellInset(idx);
4786 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4788 return tabular.cellInset(idx);
4792 void InsetTabular::cursorPos(BufferView const & bv,
4793 CursorSlice const & sl, bool boundary, int & x, int & y) const
4795 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4797 // y offset correction
4798 y += cellYPos(sl.idx());
4799 y += tabular.textVOffset(sl.idx());
4800 y += offset_valign_;
4802 // x offset correction
4803 x += cellXPos(sl.idx());
4804 x += tabular.textHOffset(sl.idx());
4805 x += ADD_TO_TABULAR_WIDTH;
4810 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4814 Inset const & inset = *tabular.cellInset(cell);
4815 Point o = bv.coordCache().getInsets().xy(&inset);
4816 int const xbeg = o.x_ - tabular.textHOffset(cell);
4817 int const xend = xbeg + tabular.cellWidth(cell);
4818 row_type const row = tabular.cellRow(cell);
4819 int const ybeg = o.y_ - tabular.rowAscent(row)
4820 - tabular.interRowSpace(row);
4821 int const yend = ybeg + tabular.cellHeight(cell);
4833 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4834 // << " ybeg=" << ybeg << " yend=" << yend
4835 // << " xx=" << xx << " yy=" << yy
4836 // << " dist=" << xx + yy << endl;
4841 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4843 //lyxerr << "InsetTabular::editXY: " << this << endl;
4844 cur.setSelection(false);
4846 cur.idx() = getNearestCell(cur.bv(), x, y);
4848 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4852 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4854 cur.idx() = getNearestCell(cur.bv(), x, y);
4855 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4859 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4861 idx_type idx_min = 0;
4862 int dist_min = numeric_limits<int>::max();
4863 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4864 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4865 int const d = dist(bv, i, x, y);
4876 int InsetTabular::cellYPos(idx_type const cell) const
4878 row_type row = tabular.cellRow(cell);
4880 for (row_type r = 0; r < row; ++r)
4881 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4882 + tabular.interRowSpace(r + 1);
4887 int InsetTabular::cellXPos(idx_type const cell) const
4889 col_type col = tabular.cellColumn(cell);
4891 for (col_type c = 0; c < col; ++c)
4892 lx += tabular.column_info[c].width;
4897 void InsetTabular::resetPos(Cursor & cur) const
4899 BufferView & bv = cur.bv();
4900 int const maxwidth = bv.workWidth();
4902 int const scx_old = scx_;
4903 int const i = cur.find(this);
4908 int const X2 = maxwidth;
4909 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4910 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4911 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4914 scx_ = X1 + 20 - x1;
4916 scx_ = X2 - 20 - x2;
4921 // only update if offset changed
4922 if (scx_ != scx_old)
4923 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4927 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4929 row_type const row = tabular.cellRow(cur.idx());
4930 col_type const col = tabular.cellColumn(cur.idx());
4932 if (isRightToLeft(cur)) {
4933 if (tabular.cellColumn(cur.idx()) == 0) {
4934 if (row == tabular.nrows() - 1)
4936 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4941 cur.idx() = tabular.getLastCellInRow(row - 1);
4943 cur.idx() = tabular.cellIndex(row, col - 1);
4946 if (tabular.isLastCell(cur.idx()))
4948 if (cur.idx() == tabular.getLastCellInRow(row))
4949 cur.idx() = tabular.cellIndex(row + 1, 0);
4951 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4952 cur.idx() = tabular.cellIndex(row, colnextcell);
4956 cur.boundary(false);
4958 if (cur.selIsMultiCell()) {
4959 cur.pit() = cur.lastpit();
4960 cur.pos() = cur.lastpos();
4968 // in visual mode, place cursor at extreme left or right
4970 switch(entry_from) {
4972 case ENTRY_DIRECTION_RIGHT:
4973 cur.posVisToRowExtremity(false /* !left */);
4975 case ENTRY_DIRECTION_LEFT:
4976 cur.posVisToRowExtremity(true /* left */);
4978 case ENTRY_DIRECTION_IGNORE:
4979 // nothing to do in this case
4983 cur.setCurrentFont();
4988 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4990 row_type const row = tabular.cellRow(cur.idx());
4991 col_type const col = tabular.cellColumn(cur.idx());
4993 if (isRightToLeft(cur)) {
4994 if (cur.idx() == tabular.getLastCellInRow(row)) {
4997 cur.idx() = tabular.getFirstCellInRow(row);
4998 cur.idx() = tabular.cellAbove(cur.idx());
5000 if (tabular.isLastCell(cur.idx()))
5002 if (cur.idx() == tabular.getLastCellInRow(row))
5003 cur.idx() = tabular.cellIndex(row + 1, 0);
5005 cur.idx() = tabular.cellIndex(row, col + 1);
5008 if (cur.idx() == 0) // first cell
5011 cur.idx() = tabular.getLastCellInRow(row - 1);
5013 cur.idx() = tabular.cellIndex(row, col - 1);
5016 if (cur.selIsMultiCell()) {
5017 cur.pit() = cur.lastpit();
5018 cur.pos() = cur.lastpos();
5023 cur.pit() = cur.lastpit();
5024 cur.pos() = cur.lastpos();
5026 // in visual mode, place cursor at extreme left or right
5028 switch(entry_from) {
5030 case ENTRY_DIRECTION_RIGHT:
5031 cur.posVisToRowExtremity(false /* !left */);
5033 case ENTRY_DIRECTION_LEFT:
5034 cur.posVisToRowExtremity(true /* left */);
5036 case ENTRY_DIRECTION_IGNORE:
5037 // nothing to do in this case
5041 cur.setCurrentFont();
5046 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5048 istringstream is(argument);
5051 if (insetCode(s) != TABULAR_CODE)
5055 size_t safe_guard = 0;
5060 if (safe_guard > 1000) {
5061 LYXERR0("parameter max count reached!");
5065 Tabular::Feature action = Tabular::LAST_ACTION;
5068 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5069 if (s != tabularFeature[i].feature)
5072 action = tabularFeature[i].action;
5075 if (action == Tabular::LAST_ACTION) {
5076 LYXERR0("Feature not found " << s);
5080 if (tabularFeature[i].need_value)
5082 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5083 tabularFeatures(cur, action, val);
5089 static void checkLongtableSpecial(Tabular::ltType & ltt,
5090 string const & special, bool & flag)
5092 if (special == "dl_above") {
5095 } else if (special == "dl_below") {
5096 ltt.bottomDL = flag;
5098 } else if (special == "empty") {
5108 bool InsetTabular::oneCellHasRotationState(bool rotated,
5109 row_type row_start, row_type row_end,
5110 col_type col_start, col_type col_end) const
5112 for (row_type r = row_start; r <= row_end; ++r)
5113 for (col_type c = col_start; c <= col_end; ++c)
5114 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5120 void InsetTabular::tabularFeatures(Cursor & cur,
5121 Tabular::Feature feature, string const & value)
5123 col_type sel_col_start;
5124 col_type sel_col_end;
5125 row_type sel_row_start;
5126 row_type sel_row_end;
5127 bool setLines = false;
5128 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5129 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5133 case Tabular::M_ALIGN_LEFT:
5134 case Tabular::ALIGN_LEFT:
5135 setAlign = LYX_ALIGN_LEFT;
5138 case Tabular::M_ALIGN_RIGHT:
5139 case Tabular::ALIGN_RIGHT:
5140 setAlign = LYX_ALIGN_RIGHT;
5143 case Tabular::M_ALIGN_CENTER:
5144 case Tabular::ALIGN_CENTER:
5145 setAlign = LYX_ALIGN_CENTER;
5148 case Tabular::ALIGN_BLOCK:
5149 setAlign = LYX_ALIGN_BLOCK;
5152 case Tabular::ALIGN_DECIMAL:
5153 setAlign = LYX_ALIGN_DECIMAL;
5156 case Tabular::M_VALIGN_TOP:
5157 case Tabular::VALIGN_TOP:
5158 setVAlign = Tabular::LYX_VALIGN_TOP;
5161 case Tabular::M_VALIGN_BOTTOM:
5162 case Tabular::VALIGN_BOTTOM:
5163 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5166 case Tabular::M_VALIGN_MIDDLE:
5167 case Tabular::VALIGN_MIDDLE:
5168 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5175 cur.recordUndoInset(ATOMIC_UNDO, this);
5177 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5178 row_type const row = tabular.cellRow(cur.idx());
5179 col_type const column = tabular.cellColumn(cur.idx());
5181 Tabular::ltType ltt;
5185 case Tabular::SET_TABULAR_WIDTH:
5186 tabular.setTabularWidth(Length(value));
5189 case Tabular::SET_PWIDTH: {
5190 Length const len(value);
5191 tabular.setColumnPWidth(cur, cur.idx(), len);
5193 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5194 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5198 case Tabular::SET_MPWIDTH:
5199 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5202 case Tabular::SET_MROFFSET:
5203 tabular.setMROffset(cur, cur.idx(), Length(value));
5206 case Tabular::SET_SPECIAL_COLUMN:
5207 case Tabular::SET_SPECIAL_MULTICOLUMN:
5208 if (value == "none")
5209 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5211 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5214 case Tabular::APPEND_ROW:
5215 // append the row into the tabular
5216 tabular.appendRow(cur.idx());
5219 case Tabular::APPEND_COLUMN:
5220 // append the column into the tabular
5221 tabular.appendColumn(cur.idx());
5222 cur.idx() = tabular.cellIndex(row, column);
5225 case Tabular::DELETE_ROW:
5226 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5227 tabular.deleteRow(sel_row_start);
5228 if (sel_row_start >= tabular.nrows())
5230 cur.idx() = tabular.cellIndex(sel_row_start, column);
5233 cur.setSelection(false);
5236 case Tabular::DELETE_COLUMN:
5237 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5238 tabular.deleteColumn(sel_col_start);
5239 if (sel_col_start >= tabular.ncols())
5241 cur.idx() = tabular.cellIndex(row, sel_col_start);
5244 cur.setSelection(false);
5247 case Tabular::COPY_ROW:
5248 tabular.copyRow(row);
5251 case Tabular::COPY_COLUMN:
5252 tabular.copyColumn(column);
5253 cur.idx() = tabular.cellIndex(row, column);
5256 case Tabular::SET_LINE_TOP:
5257 case Tabular::TOGGLE_LINE_TOP: {
5258 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5259 ? (value == "true") : !tabular.topLine(cur.idx());
5260 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5261 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5262 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5266 case Tabular::SET_LINE_BOTTOM:
5267 case Tabular::TOGGLE_LINE_BOTTOM: {
5268 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5269 ? (value == "true") : !tabular.bottomLine(cur.idx());
5270 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5271 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5272 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5276 case Tabular::SET_LINE_LEFT:
5277 case Tabular::TOGGLE_LINE_LEFT: {
5278 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5279 ? (value == "true") : !tabular.leftLine(cur.idx());
5280 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5281 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5282 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5286 case Tabular::SET_LINE_RIGHT:
5287 case Tabular::TOGGLE_LINE_RIGHT: {
5288 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5289 ? (value == "true") : !tabular.rightLine(cur.idx());
5290 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5291 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5292 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5296 case Tabular::M_ALIGN_LEFT:
5297 case Tabular::M_ALIGN_RIGHT:
5298 case Tabular::M_ALIGN_CENTER:
5299 case Tabular::ALIGN_LEFT:
5300 case Tabular::ALIGN_RIGHT:
5301 case Tabular::ALIGN_CENTER:
5302 case Tabular::ALIGN_BLOCK:
5303 case Tabular::ALIGN_DECIMAL:
5304 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5305 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5306 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5307 !tabular.getPWidth(c).zero());
5310 case Tabular::M_VALIGN_TOP:
5311 case Tabular::M_VALIGN_BOTTOM:
5312 case Tabular::M_VALIGN_MIDDLE:
5314 case Tabular::VALIGN_TOP:
5315 case Tabular::VALIGN_BOTTOM:
5316 case Tabular::VALIGN_MIDDLE:
5317 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5318 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5319 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5322 case Tabular::SET_MULTICOLUMN: {
5323 if (!cur.selection()) {
5324 // just multicol for one single cell
5325 // check whether we are completely in a multicol
5326 if (!tabular.isMultiColumn(cur.idx()))
5327 tabular.setMultiColumn(cur.idx(), 1);
5330 // we have a selection so this means we just add all this
5331 // cells to form a multicolumn cell
5332 idx_type const s_start = cur.selBegin().idx();
5333 row_type const col_start = tabular.cellColumn(s_start);
5334 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5335 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5338 cur.setSelection(false);
5342 case Tabular::UNSET_MULTICOLUMN: {
5343 if (!cur.selection()) {
5344 if (tabular.isMultiColumn(cur.idx()))
5345 tabular.unsetMultiColumn(cur.idx());
5350 case Tabular::MULTICOLUMN: {
5351 if (tabular.isMultiColumn(cur.idx()))
5352 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5354 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5358 case Tabular::SET_MULTIROW: {
5359 if (!cur.selection()) {
5360 // just multirow for one single cell
5361 // check whether we are completely in a multirow
5362 if (!tabular.isMultiRow(cur.idx()))
5363 tabular.setMultiRow(cur.idx(), 1);
5366 // we have a selection so this means we just add all this
5367 // cells to form a multirow cell
5368 idx_type const s_start = cur.selBegin().idx();
5369 row_type const row_start = tabular.cellRow(s_start);
5370 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5371 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5374 cur.setSelection(false);
5378 case Tabular::UNSET_MULTIROW: {
5379 if (!cur.selection()) {
5380 if (tabular.isMultiRow(cur.idx()))
5381 tabular.unsetMultiRow(cur.idx());
5386 case Tabular::MULTIROW: {
5387 if (tabular.isMultiRow(cur.idx()))
5388 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5390 tabularFeatures(cur, Tabular::SET_MULTIROW);
5394 case Tabular::SET_ALL_LINES:
5396 case Tabular::UNSET_ALL_LINES:
5397 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5398 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5399 idx_type const cell = tabular.cellIndex(r, c);
5400 tabular.setTopLine(cell, setLines);
5401 tabular.setBottomLine(cell, setLines);
5402 tabular.setRightLine(cell, setLines);
5403 tabular.setLeftLine(cell, setLines);
5407 case Tabular::SET_BORDER_LINES:
5408 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5409 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5410 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5412 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5413 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5414 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5418 case Tabular::SET_LONGTABULAR:
5419 tabular.is_long_tabular = true;
5422 case Tabular::UNSET_LONGTABULAR:
5423 for (row_type r = 0; r < tabular.nrows(); ++r) {
5424 if (tabular.ltCaption(r)) {
5425 cur.idx() = tabular.cellIndex(r, 0);
5428 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5431 tabular.is_long_tabular = false;
5434 case Tabular::SET_ROTATE_TABULAR:
5435 tabular.rotate = true;
5438 case Tabular::UNSET_ROTATE_TABULAR:
5439 tabular.rotate = false;
5442 case Tabular::TOGGLE_ROTATE_TABULAR:
5443 tabular.rotate = !tabular.rotate;
5446 case Tabular::TABULAR_VALIGN_TOP:
5447 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5450 case Tabular::TABULAR_VALIGN_MIDDLE:
5451 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5454 case Tabular::TABULAR_VALIGN_BOTTOM:
5455 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5458 case Tabular::LONGTABULAR_ALIGN_LEFT:
5459 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5462 case Tabular::LONGTABULAR_ALIGN_CENTER:
5463 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5466 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5467 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5472 case Tabular::SET_ROTATE_CELL:
5473 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5474 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5475 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5478 case Tabular::UNSET_ROTATE_CELL:
5479 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5480 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5481 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5484 case Tabular::TOGGLE_ROTATE_CELL:
5486 bool oneNotRotated = oneCellHasRotationState(false,
5487 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
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.setRotateCell(tabular.cellIndex(r, c),
5496 case Tabular::SET_USEBOX: {
5497 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5498 if (val == tabular.getUsebox(cur.idx()))
5499 val = Tabular::BOX_NONE;
5500 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5501 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5502 tabular.setUsebox(tabular.cellIndex(r, c), val);
5506 case Tabular::UNSET_LTFIRSTHEAD:
5508 case Tabular::SET_LTFIRSTHEAD:
5509 tabular.getRowOfLTFirstHead(row, ltt);
5510 checkLongtableSpecial(ltt, value, flag);
5511 tabular.setLTHead(row, flag, ltt, true);
5514 case Tabular::UNSET_LTHEAD:
5516 case Tabular::SET_LTHEAD:
5517 tabular.getRowOfLTHead(row, ltt);
5518 checkLongtableSpecial(ltt, value, flag);
5519 tabular.setLTHead(row, flag, ltt, false);
5522 case Tabular::UNSET_LTFOOT:
5524 case Tabular::SET_LTFOOT:
5525 tabular.getRowOfLTFoot(row, ltt);
5526 checkLongtableSpecial(ltt, value, flag);
5527 tabular.setLTFoot(row, flag, ltt, false);
5530 case Tabular::UNSET_LTLASTFOOT:
5532 case Tabular::SET_LTLASTFOOT:
5533 tabular.getRowOfLTLastFoot(row, ltt);
5534 checkLongtableSpecial(ltt, value, flag);
5535 tabular.setLTFoot(row, flag, ltt, true);
5538 case Tabular::SET_LTNEWPAGE:
5539 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5542 case Tabular::SET_LTCAPTION: {
5543 if (tabular.ltCaption(row))
5545 cur.idx() = tabular.setLTCaption(row, true);
5548 cur.setSelection(false);
5549 // If a row is set as caption, then also insert
5550 // a caption. Otherwise the LaTeX output is broken.
5551 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5552 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5556 case Tabular::UNSET_LTCAPTION: {
5557 if (!tabular.ltCaption(row))
5559 cur.idx() = tabular.setLTCaption(row, false);
5562 cur.setSelection(false);
5563 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5564 if (lyx::getStatus(fr).enabled())
5569 case Tabular::TOGGLE_LTCAPTION: {
5570 if (tabular.ltCaption(row))
5571 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5573 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5577 case Tabular::SET_BOOKTABS:
5578 tabular.use_booktabs = true;
5581 case Tabular::UNSET_BOOKTABS:
5582 tabular.use_booktabs = false;
5585 case Tabular::SET_TOP_SPACE: {
5587 if (value == "default")
5588 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5589 tabular.row_info[r].top_space_default = true;
5590 else if (value == "none")
5591 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5592 tabular.row_info[r].top_space_default = false;
5593 tabular.row_info[r].top_space = len;
5595 else if (isValidLength(value, &len))
5596 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5597 tabular.row_info[r].top_space_default = false;
5598 tabular.row_info[r].top_space = len;
5603 case Tabular::SET_BOTTOM_SPACE: {
5605 if (value == "default")
5606 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5607 tabular.row_info[r].bottom_space_default = true;
5608 else if (value == "none")
5609 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5610 tabular.row_info[r].bottom_space_default = false;
5611 tabular.row_info[r].bottom_space = len;
5613 else if (isValidLength(value, &len))
5614 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5615 tabular.row_info[r].bottom_space_default = false;
5616 tabular.row_info[r].bottom_space = len;
5621 case Tabular::SET_INTERLINE_SPACE: {
5623 if (value == "default")
5624 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5625 tabular.row_info[r].interline_space_default = true;
5626 else if (value == "none")
5627 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5628 tabular.row_info[r].interline_space_default = false;
5629 tabular.row_info[r].interline_space = len;
5631 else if (isValidLength(value, &len))
5632 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5633 tabular.row_info[r].interline_space_default = false;
5634 tabular.row_info[r].interline_space = len;
5639 case Tabular::SET_DECIMAL_POINT:
5640 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5641 tabular.column_info[c].decimal_point = from_utf8(value);
5644 // dummy stuff just to avoid warnings
5645 case Tabular::LAST_ACTION:
5651 bool InsetTabular::copySelection(Cursor & cur)
5653 if (!cur.selection())
5658 getSelection(cur, rs, re, cs, ce);
5660 paste_tabular.reset(new Tabular(tabular));
5662 for (row_type r = 0; r < rs; ++r)
5663 paste_tabular->deleteRow(0);
5665 row_type const rows = re - rs + 1;
5666 while (paste_tabular->nrows() > rows)
5667 paste_tabular->deleteRow(rows);
5669 for (col_type c = 0; c < cs; ++c)
5670 paste_tabular->deleteColumn(0);
5672 col_type const columns = ce - cs + 1;
5673 while (paste_tabular->ncols() > columns)
5674 paste_tabular->deleteColumn(columns);
5676 paste_tabular->setBuffer(tabular.buffer());
5678 odocstringstream os;
5679 OutputParams const runparams(0);
5680 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5681 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5682 cap::copySelection(cur, os.str());
5684 // mark tabular stack dirty
5685 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5686 // when we (hopefully) have a one-for-all paste mechanism.
5687 // This must be called after cap::copySelection.
5688 dirtyTabularStack(true);
5694 bool InsetTabular::pasteClipboard(Cursor & cur)
5698 col_type actcol = tabular.cellColumn(cur.idx());
5699 row_type actrow = tabular.cellRow(cur.idx());
5701 if (cur.selIsMultiCell()) {
5704 getSelection(cur, actrow, re, actcol, ce);
5707 for (row_type r1 = 0, r2 = actrow;
5708 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5710 for (col_type c1 = 0, c2 = actcol;
5711 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5713 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5714 tabular.isPartOfMultiColumn(r2, c2))
5716 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5720 if (tabular.isPartOfMultiColumn(r2, c2)) {
5724 shared_ptr<InsetTableCell> inset(
5725 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5726 tabular.setCellInset(r2, c2, inset);
5727 // FIXME?: why do we need to do this explicitly? (EL)
5728 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5730 // FIXME: change tracking (MG)
5731 inset->setChange(Change(buffer().params().trackChanges ?
5732 Change::INSERTED : Change::UNCHANGED));
5740 void InsetTabular::cutSelection(Cursor & cur)
5742 if (!cur.selection())
5747 getSelection(cur, rs, re, cs, ce);
5748 for (row_type r = rs; r <= re; ++r) {
5749 for (col_type c = cs; c <= ce; ++c) {
5750 shared_ptr<InsetTableCell> t
5751 = cell(tabular.cellIndex(r, c));
5752 if (buffer().params().trackChanges)
5753 // FIXME: Change tracking (MG)
5754 t->setChange(Change(Change::DELETED));
5760 // cursor position might be invalid now
5761 if (cur.pit() > cur.lastpit())
5762 cur.pit() = cur.lastpit();
5763 if (cur.pos() > cur.lastpos())
5764 cur.pos() = cur.lastpos();
5765 cur.clearSelection();
5769 bool InsetTabular::isRightToLeft(Cursor & cur) const
5771 LASSERT(cur.depth() > 1, /**/);
5772 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5773 pos_type const parentpos = cur[cur.depth() - 2].pos();
5774 return parentpar.getFontSettings(buffer().params(),
5775 parentpos).language()->rightToLeft();
5779 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5782 LASSERT(stidx <= enidx, return docstring());
5784 col_type const col1 = tabular.cellColumn(stidx);
5785 col_type const col2 = tabular.cellColumn(enidx);
5786 row_type const row1 = tabular.cellRow(stidx);
5787 row_type const row2 = tabular.cellRow(enidx);
5789 for (col_type col = col1; col <= col2; col++)
5790 for (row_type row = row1; row <= row2; row++) {
5795 retval += tabular.cellInset(row, col)->asString(intoInsets);
5801 void InsetTabular::getSelection(Cursor & cur,
5802 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5804 CursorSlice const & beg = cur.selBegin();
5805 CursorSlice const & end = cur.selEnd();
5806 cs = tabular.cellColumn(beg.idx());
5807 ce = tabular.cellColumn(end.idx());
5811 rs = tabular.cellRow(beg.idx());
5812 re = tabular.cellRow(end.idx());
5818 Text * InsetTabular::getText(int idx) const
5820 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5824 void InsetTabular::setChange(Change const & change)
5826 for (idx_type idx = 0; idx < nargs(); ++idx)
5827 cell(idx)->setChange(change);
5831 void InsetTabular::acceptChanges()
5833 for (idx_type idx = 0; idx < nargs(); ++idx)
5834 cell(idx)->acceptChanges();
5838 void InsetTabular::rejectChanges()
5840 for (idx_type idx = 0; idx < nargs(); ++idx)
5841 cell(idx)->rejectChanges();
5845 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5847 return tabular.getPWidth(cell).zero();
5851 bool InsetTabular::forcePlainLayout(idx_type cell) const
5853 return !tabular.getPWidth(cell).zero();
5857 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5860 if (buf.length() <= 0)
5865 col_type maxCols = 1;
5866 size_t const len = buf.length();
5870 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5878 maxCols = max(cols, maxCols);
5884 maxCols = max(cols, maxCols);
5890 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5891 loctab = paste_tabular.get();
5893 dirtyTabularStack(true);
5896 cell = bv.cursor().idx();
5897 ocol = tabular.cellColumn(cell);
5898 row = tabular.cellRow(cell);
5902 idx_type const cells = loctab->numberofcells;
5905 rows = loctab->nrows();
5906 col_type const columns = loctab->ncols();
5908 while (cell < cells && p < len && row < rows &&
5909 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5915 // we can only set this if we are not too far right
5916 if (cols < columns) {
5917 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5918 Font const font = bv.textMetrics(&inset->text()).
5920 inset->setText(buf.substr(op, p - op), font,
5921 buffer().params().trackChanges);
5927 // we can only set this if we are not too far right
5928 if (cols < columns) {
5929 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5930 Font const font = bv.textMetrics(&inset->text()).
5932 inset->setText(buf.substr(op, p - op), font,
5933 buffer().params().trackChanges);
5938 cell = loctab->cellIndex(row, cols);
5944 // check for the last cell if there is no trailing '\n'
5945 if (cell < cells && op < len) {
5946 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5947 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5948 inset->setText(buf.substr(op, len - op), font,
5949 buffer().params().trackChanges);
5955 void InsetTabular::addPreview(DocIterator const & inset_pos,
5956 PreviewLoader & loader) const
5958 DocIterator cell_pos = inset_pos;
5960 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5961 for (row_type r = 0; r < tabular.nrows(); ++r) {
5962 for (col_type c = 0; c < tabular.ncols(); ++c) {
5963 cell_pos.top().idx() = tabular.cellIndex(r, c);
5964 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5970 bool InsetTabular::completionSupported(Cursor const & cur) const
5972 Cursor const & bvCur = cur.bv().cursor();
5973 if (&bvCur.inset() != this)
5975 return cur.text()->completionSupported(cur);
5979 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5981 return completionSupported(cur);
5985 bool InsetTabular::automaticInlineCompletion() const
5987 return lyxrc.completion_inline_text;
5991 bool InsetTabular::automaticPopupCompletion() const
5993 return lyxrc.completion_popup_text;
5997 bool InsetTabular::showCompletionCursor() const
5999 return lyxrc.completion_cursor_text;
6003 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6005 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6009 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6011 if (!completionSupported(cur))
6013 return cur.text()->completionPrefix(cur);
6017 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6019 if (!completionSupported(cur))
6022 return cur.text()->insertCompletion(cur, s, finished);
6026 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6027 Dimension & dim) const
6029 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6030 tm.completionPosAndDim(cur, x, y, dim);
6034 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6036 istringstream data(in);
6038 lex.setStream(data);
6045 if (!lex || token != "tabular") {
6046 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6050 // This is part of the inset proper that is usually swallowed
6051 // by Buffer::readInset
6053 if (!lex || token != "Tabular") {
6054 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6062 string InsetTabular::params2string(InsetTabular const & inset)
6065 data << "tabular" << ' ';
6067 data << "\\end_inset\n";