2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
169 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
170 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
172 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
173 { Tabular::SET_BOOKTABS, "set-booktabs", false },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
175 { Tabular::SET_TOP_SPACE, "set-top-space", true },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
178 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
179 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
180 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
181 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
182 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
183 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
184 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
185 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
186 { Tabular::LAST_ACTION, "", false }
191 string const write_attribute(string const & name, T const & t)
193 string const s = tostr(t);
194 return s.empty() ? s : " " + name + "=\"" + s + "\"";
198 string const write_attribute(string const & name, string const & t)
200 return t.empty() ? t : " " + name + "=\"" + t + "\"";
205 string const write_attribute(string const & name, docstring const & t)
207 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
212 string const write_attribute(string const & name, bool const & b)
214 // we write only true attribute values so we remove a bit of the
215 // file format bloat for tabulars.
216 return b ? write_attribute(name, convert<string>(b)) : string();
221 string const write_attribute(string const & name, int const & i)
223 // we write only true attribute values so we remove a bit of the
224 // file format bloat for tabulars.
225 return i ? write_attribute(name, convert<string>(i)) : string();
230 string const write_attribute(string const & name, Tabular::idx_type const & i)
232 // we write only true attribute values so we remove a bit of the
233 // file format bloat for tabulars.
234 return i ? write_attribute(name, convert<string>(i)) : string();
239 string const write_attribute(string const & name, Length const & value)
241 // we write only the value if we really have one same reson as above.
242 return value.zero() ? string() : write_attribute(name, value.asString());
246 string const tostr(LyXAlignment const & num)
251 case LYX_ALIGN_BLOCK:
255 case LYX_ALIGN_CENTER:
257 case LYX_ALIGN_RIGHT:
259 case LYX_ALIGN_LAYOUT:
261 case LYX_ALIGN_SPECIAL:
263 case LYX_ALIGN_DECIMAL:
270 string const tostr(Tabular::HAlignment const & num)
273 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
275 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
277 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
284 string const tostr(Tabular::VAlignment const & num)
287 case Tabular::LYX_VALIGN_TOP:
289 case Tabular::LYX_VALIGN_MIDDLE:
291 case Tabular::LYX_VALIGN_BOTTOM:
298 string const tostr(Tabular::BoxType const & num)
301 case Tabular::BOX_NONE:
303 case Tabular::BOX_PARBOX:
305 case Tabular::BOX_MINIPAGE:
312 // I would have liked a fromstr template a lot better. (Lgb)
313 bool string2type(string const str, LyXAlignment & num)
316 num = LYX_ALIGN_NONE;
317 else if (str == "block")
318 num = LYX_ALIGN_BLOCK;
319 else if (str == "left")
320 num = LYX_ALIGN_LEFT;
321 else if (str == "center")
322 num = LYX_ALIGN_CENTER;
323 else if (str == "right")
324 num = LYX_ALIGN_RIGHT;
325 else if (str == "decimal")
326 num = LYX_ALIGN_DECIMAL;
333 bool string2type(string const str, Tabular::HAlignment & num)
336 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
337 else if (str == "center" )
338 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
339 else if (str == "right")
340 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
347 bool string2type(string const str, Tabular::VAlignment & num)
350 num = Tabular::LYX_VALIGN_TOP;
351 else if (str == "middle" )
352 num = Tabular::LYX_VALIGN_MIDDLE;
353 else if (str == "bottom")
354 num = Tabular::LYX_VALIGN_BOTTOM;
361 bool string2type(string const str, Tabular::BoxType & num)
364 num = Tabular::BOX_NONE;
365 else if (str == "parbox")
366 num = Tabular::BOX_PARBOX;
367 else if (str == "minipage")
368 num = Tabular::BOX_MINIPAGE;
375 bool string2type(string const str, bool & num)
379 else if (str == "false")
387 bool getTokenValue(string const & str, char const * token, string & ret)
390 size_t token_length = strlen(token);
391 size_t pos = str.find(token);
393 if (pos == string::npos || pos + token_length + 1 >= str.length()
394 || str[pos + token_length] != '=')
396 pos += token_length + 1;
398 if (ch != '"' && ch != '\'') { // only read till next space
402 while (pos < str.length() - 1 && str[++pos] != ch)
409 bool getTokenValue(string const & str, char const * token, docstring & ret)
412 bool const success = getTokenValue(str, token, tmp);
413 ret = from_utf8(tmp);
418 bool getTokenValue(string const & str, char const * token, int & num)
422 if (!getTokenValue(str, token, tmp))
424 num = convert<int>(tmp);
429 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
432 return getTokenValue(str, token, tmp) && string2type(tmp, num);
436 bool getTokenValue(string const & str, char const * token,
437 Tabular::HAlignment & num)
440 return getTokenValue(str, token, tmp) && string2type(tmp, num);
444 bool getTokenValue(string const & str, char const * token,
445 Tabular::VAlignment & num)
448 return getTokenValue(str, token, tmp) && string2type(tmp, num);
452 bool getTokenValue(string const & str, char const * token,
453 Tabular::BoxType & num)
456 return getTokenValue(str, token, tmp) && string2type(tmp, num);
460 bool getTokenValue(string const & str, char const * token, bool & flag)
462 // set the flag always to false as this should be the default for bools
463 // not in the file-format.
466 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
470 bool getTokenValue(string const & str, char const * token, Length & len)
472 // set the length to be zero() as default as this it should be if not
473 // in the file format.
476 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
480 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
485 if (!getTokenValue(str, token, tmp))
487 if (tmp == "default") {
491 return isValidLength(tmp, &len);
495 void l_getline(istream & is, string & str)
498 while (str.empty()) {
500 if (!str.empty() && str[str.length() - 1] == '\r')
501 str.erase(str.length() - 1);
508 string const featureAsString(Tabular::Feature action)
510 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
511 if (tabularFeature[i].action == action)
512 return tabularFeature[i].feature;
518 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
520 InsetTableCell tail = InsetTableCell(head);
521 tail.getText(0)->setMacrocontextPosition(
522 head.getText(0)->macrocontextPosition());
523 tail.setBuffer(head.buffer());
525 DocIterator dit = doc_iterator_begin(&head.buffer(), &head);
526 for (; dit; dit.forwardChar())
527 if (dit.inTexted() && dit.depth()==1
528 && dit.paragraph().find(align_d, false, false, dit.pos()))
531 pit_type const psize = head.paragraphs().front().size();
534 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
535 tail.paragraphs().front().eraseChars(0,
536 dit.pos() < psize ? dit.pos() + 1 : psize, false);
543 /////////////////////////////////////////////////////////////////////
547 /////////////////////////////////////////////////////////////////////
550 Tabular::CellData::CellData(Buffer * buf)
553 multicolumn(Tabular::CELL_NORMAL),
554 multirow(Tabular::CELL_NORMAL),
555 alignment(LYX_ALIGN_CENTER),
556 valignment(LYX_VALIGN_TOP),
566 inset(new InsetTableCell(buf))
568 inset->setBuffer(*buf);
572 Tabular::CellData::CellData(CellData const & cs)
575 multicolumn(cs.multicolumn),
576 multirow(cs.multirow),
577 mroffset(cs.mroffset),
578 alignment(cs.alignment),
579 valignment(cs.valignment),
580 decimal_hoffset(cs.decimal_hoffset),
581 decimal_width(cs.decimal_width),
583 top_line(cs.top_line),
584 bottom_line(cs.bottom_line),
585 left_line(cs.left_line),
586 right_line(cs.right_line),
589 align_special(cs.align_special),
591 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
595 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
601 void Tabular::CellData::swap(CellData & rhs)
603 std::swap(cellno, rhs.cellno);
604 std::swap(width, rhs.width);
605 std::swap(multicolumn, rhs.multicolumn);
606 std::swap(multirow, rhs.multirow);
607 std::swap(mroffset, rhs.mroffset);
608 std::swap(alignment, rhs.alignment);
609 std::swap(valignment, rhs.valignment);
610 std::swap(decimal_hoffset, rhs.decimal_hoffset);
611 std::swap(decimal_width, rhs.decimal_width);
612 std::swap(voffset, rhs.voffset);
613 std::swap(top_line, rhs.top_line);
614 std::swap(bottom_line, rhs.bottom_line);
615 std::swap(left_line, rhs.left_line);
616 std::swap(right_line, rhs.right_line);
617 std::swap(usebox, rhs.usebox);
618 std::swap(rotate, rhs.rotate);
619 std::swap(align_special, rhs.align_special);
620 p_width.swap(rhs.p_width);
621 inset.swap(rhs.inset);
625 Tabular::RowData::RowData()
628 top_space_default(false),
629 bottom_space_default(false),
630 interline_space_default(false),
640 Tabular::ColumnData::ColumnData()
641 : alignment(LYX_ALIGN_CENTER),
642 valignment(LYX_VALIGN_TOP),
648 Tabular::ltType::ltType()
655 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
657 init(buffer, rows_arg, columns_arg);
661 void Tabular::setBuffer(Buffer & buffer)
664 for (row_type i = 0; i < nrows(); ++i)
665 for (col_type j = 0; j < ncols(); ++j)
666 cell_info[i][j].inset->setBuffer(*buffer_);
670 // activates all lines and sets all widths to 0
671 void Tabular::init(Buffer * buf, row_type rows_arg,
672 col_type columns_arg)
675 row_info = row_vector(rows_arg);
676 column_info = column_vector(columns_arg);
677 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
678 row_info.reserve(10);
679 column_info.reserve(10);
680 cell_info.reserve(100);
682 is_long_tabular = false;
683 tabular_valignment = LYX_VALIGN_MIDDLE;
684 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
686 use_booktabs = false;
687 // set silly default lines
688 for (row_type r = 0; r < nrows(); ++r)
689 for (col_type c = 0; c < ncols(); ++c) {
690 cell_info[r][c].inset->setBuffer(*buffer_);
691 cell_info[r][c].top_line = true;
692 cell_info[r][c].left_line = true;
693 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
694 cell_info[r][c].right_line = c == ncols() - 1;
699 void Tabular::appendRow(idx_type const cell)
701 row_type const row = cellRow(cell);
703 row_info.insert(row_info.begin() + row + 1, RowData());
704 row_info[row + 1] = row_info[row];
706 cell_info.insert(cell_info.begin() + row + 1,
707 cell_vector(ncols(), CellData(buffer_)));
708 for (col_type c = 0; c < ncols(); ++c) {
709 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
710 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
712 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
716 for (col_type c = 0; c < ncols(); ++c) {
717 if (isPartOfMultiRow(row, c))
719 // inherit line settings
720 idx_type const i = cellIndex(row + 1, c);
721 idx_type const j = cellIndex(row, c);
722 setLeftLine(i, leftLine(j));
723 setRightLine(i, rightLine(j));
724 setTopLine(i, topLine(j));
725 if (topLine(j) && bottomLine(j)) {
726 setBottomLine(i, true);
727 setBottomLine(j, false);
729 // mark track changes
730 if (buffer().params().trackChanges)
731 cellInfo(i).inset->setChange(Change(Change::INSERTED));
736 void Tabular::deleteRow(row_type const row)
738 // Not allowed to delete last row
742 for (col_type c = 0; c < ncols(); ++c) {
743 // Care about multirow cells
744 if (row + 1 < nrows() &&
745 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
746 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
747 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
750 row_info.erase(row_info.begin() + row);
751 cell_info.erase(cell_info.begin() + row);
756 void Tabular::copyRow(row_type const row)
758 row_info.insert(row_info.begin() + row, row_info[row]);
759 cell_info.insert(cell_info.begin() + row, cell_info[row]);
761 if (buffer().params().trackChanges)
762 for (col_type c = 0; c < ncols(); ++c)
763 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
769 void Tabular::appendColumn(idx_type const cell)
771 col_type const c = cellColumn(cell);
773 column_info.insert(column_info.begin() + c + 1, ColumnData());
774 column_info[c + 1] = column_info[c];
776 for (row_type r = 0; r < nrows(); ++r) {
777 cell_info[r].insert(cell_info[r].begin() + c + 1,
779 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
780 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
782 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
785 for (row_type r = 0; r < nrows(); ++r) {
786 // inherit line settings
787 idx_type const i = cellIndex(r, c + 1);
788 idx_type const j = cellIndex(r, c);
789 setBottomLine(i, bottomLine(j));
790 setTopLine(i, topLine(j));
791 setLeftLine(i, leftLine(j));
792 if (rightLine(j) && rightLine(j)) {
793 setRightLine(i, true);
794 setRightLine(j, false);
796 if (buffer().params().trackChanges)
797 cellInfo(i).inset->setChange(Change(Change::INSERTED));
802 void Tabular::deleteColumn(col_type const col)
804 // Not allowed to delete last column
808 for (row_type r = 0; r < nrows(); ++r) {
809 // Care about multicolumn cells
810 if (col + 1 < ncols() &&
811 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
812 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
813 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
815 cell_info[r].erase(cell_info[r].begin() + col);
817 column_info.erase(column_info.begin() + col);
822 void Tabular::copyColumn(col_type const col)
824 BufferParams const & bp = buffer().params();
825 column_info.insert(column_info.begin() + col, column_info[col]);
827 for (row_type r = 0; r < nrows(); ++r) {
828 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
830 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
836 void Tabular::updateIndexes()
841 for (row_type row = 0; row < nrows(); ++row)
842 for (col_type column = 0; column < ncols(); ++column) {
843 if (!isPartOfMultiColumn(row, column)
844 && !isPartOfMultiRow(row, column))
846 if (isPartOfMultiRow(row, column))
847 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
849 cell_info[row][column].cellno = numberofcells - 1;
852 rowofcell.resize(numberofcells);
853 columnofcell.resize(numberofcells);
855 // reset column and row of cells and update their width and alignment
856 for (row_type row = 0; row < nrows(); ++row)
857 for (col_type column = 0; column < ncols(); ++column) {
858 if (isPartOfMultiColumn(row, column))
860 // columnofcell needs to be called before setting width and aligment
861 // multirow cells inherit the width from the column width
862 if (!isPartOfMultiRow(row, column)) {
863 columnofcell[i] = column;
866 setFixedWidth(row, column);
867 if (isPartOfMultiRow(row, column))
869 cell_info[row][column].inset->setContentAlignment(
870 getAlignment(cellIndex(row, column)));
876 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
879 for (col_type c = 0; c < ncols(); ++c)
880 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
886 bool Tabular::topLine(idx_type const cell) const
888 return cellInfo(cell).top_line;
892 bool Tabular::bottomLine(idx_type const cell) const
894 return cellInfo(cell).bottom_line;
898 bool Tabular::leftLine(idx_type cell) const
902 return cellInfo(cell).left_line;
906 bool Tabular::rightLine(idx_type cell) const
910 return cellInfo(cell).right_line;
914 int Tabular::interRowSpace(row_type row) const
916 if (!row || row >= nrows())
919 int const interline_space = row_info[row - 1].interline_space_default ?
921 row_info[row - 1].interline_space.inPixels(width());
922 if (rowTopLine(row) && rowBottomLine(row - 1))
923 return interline_space + WIDTH_OF_LINE;
924 return interline_space;
928 int Tabular::interColumnSpace(idx_type cell) const
930 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
931 if (rightLine(cell) && nextcol < ncols()
932 && leftLine(cellIndex(cellRow(cell), nextcol)))
933 return WIDTH_OF_LINE;
938 int Tabular::cellWidth(idx_type cell) const
941 col_type const span = columnSpan(cell);
942 col_type const col = cellColumn(cell);
943 for(col_type c = col; c < col + span ; ++c)
944 w += column_info[c].width;
949 int Tabular::cellHeight(idx_type cell) const
951 row_type const span = rowSpan(cell);
952 row_type const row = cellRow(cell);
954 for(row_type r = row; r < row + span ; ++r) {
955 h += rowAscent(r) + rowDescent(r);
956 if (r != row + span - 1)
957 h += interRowSpace(r + 1);
964 bool Tabular::updateColumnWidths()
966 vector<int> max_dwidth(ncols(), 0);
967 for(col_type c = 0; c < ncols(); ++c)
968 for(row_type r = 0; r < nrows(); ++r) {
969 idx_type const i = cellIndex(r, c);
970 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
971 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
975 // for each col get max of single col cells
976 for(col_type c = 0; c < ncols(); ++c) {
978 for(row_type r = 0; r < nrows(); ++r) {
979 idx_type const i = cellIndex(r, c);
980 if (columnSpan(i) == 1) {
981 if (getAlignment(i) == LYX_ALIGN_DECIMAL
982 && cell_info[r][c].decimal_width!=0)
983 new_width = max(new_width, cellInfo(i).width
984 + max_dwidth[c] - cellInfo(i).decimal_width);
986 new_width = max(new_width, cellInfo(i).width);
990 if (column_info[c].width != new_width) {
991 column_info[c].width = new_width;
995 // update col widths to fit merged cells
996 for(col_type c = 0; c < ncols(); ++c)
997 for(row_type r = 0; r < nrows(); ++r) {
998 idx_type const i = cellIndex(r, c);
999 int const span = columnSpan(i);
1000 if (span == 1 || c > cellColumn(i))
1004 for(col_type j = c; j < c + span ; ++j)
1005 old_width += column_info[j].width;
1007 if (cellInfo(i).width > old_width) {
1008 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1017 int Tabular::width() const
1020 for (col_type c = 0; c < ncols(); ++c)
1021 width += column_info[c].width;
1026 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1029 col_type const col = cellColumn(cell);
1030 // set alignment for the whole row if we are not in a multicolumn cell,
1031 // exclude possible multicolumn cells in the row
1032 if (!isMultiColumn(cell)) {
1033 for (row_type r = 0; r < nrows(); ++r) {
1034 // only if the column has no width the multirow inherits the
1035 // alignment of the column, otherwise it is left aligned
1036 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1037 && !isMultiColumn(cellIndex(r, col))) {
1038 cell_info[r][col].alignment = align;
1039 cell_info[r][col].inset->setContentAlignment(align);
1041 if ((isMultiRow(cellIndex(r, col)) && has_width)
1042 && !isMultiColumn(cellIndex(r, col))) {
1043 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1044 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1047 column_info[col].alignment = align;
1048 docstring & dpoint = column_info[col].decimal_point;
1049 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1050 dpoint = from_utf8(lyxrc.default_decimal_point);
1052 cellInfo(cell).alignment = align;
1053 cellInset(cell).get()->setContentAlignment(align);
1058 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1061 if (!isMultiColumn(cell) || onlycolumn)
1062 column_info[cellColumn(cell)].valignment = align;
1064 cellInfo(cell).valignment = align;
1071 * Allow line and paragraph breaks for fixed width cells or disallow them,
1072 * merge cell paragraphs and reset layout to standard for variable width
1075 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1077 inset->setAutoBreakRows(fixedWidth);
1078 inset->toggleFixedWidth(fixedWidth);
1082 // merge all paragraphs to one
1083 BufferParams const & bp = cur.bv().buffer().params();
1084 while (inset->paragraphs().size() > 1)
1085 mergeParagraph(bp, inset->paragraphs(), 0);
1089 // undo information has already been recorded
1090 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1091 bp.documentClass().plainLayoutName());
1098 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1099 Length const & width)
1101 col_type const c = cellColumn(cell);
1103 column_info[c].p_width = width;
1104 // reset the vertical alignment to top if the fixed with
1105 // is removed or zero because only fixed width columns can
1106 // have a vertical alignment
1107 if (column_info[c].p_width.zero())
1108 column_info[c].valignment = LYX_VALIGN_TOP;
1109 for (row_type r = 0; r < nrows(); ++r) {
1110 idx_type const cell = cellIndex(r, c);
1111 // because of multicolumns
1112 toggleFixedWidth(cur, cellInset(cell).get(),
1113 !getPWidth(cell).zero());
1115 // cur paragraph can become invalid after paragraphs were merged
1116 if (cur.pit() > cur.lastpit())
1117 cur.pit() = cur.lastpit();
1118 // cur position can become invalid after newlines were removed
1119 if (cur.pos() > cur.lastpos())
1120 cur.pos() = cur.lastpos();
1124 bool Tabular::setFixedWidth(row_type r, col_type c)
1126 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1127 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1128 || (multicol && !cell_info[r][c].p_width.zero());
1129 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1134 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1135 Length const & width)
1137 if (!isMultiColumn(cell))
1140 cellInfo(cell).p_width = width;
1141 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1142 // cur paragraph can become invalid after paragraphs were merged
1143 if (cur.pit() > cur.lastpit())
1144 cur.pit() = cur.lastpit();
1145 // cur position can become invalid after newlines were removed
1146 if (cur.pos() > cur.lastpos())
1147 cur.pos() = cur.lastpos();
1152 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1154 cellInfo(cell).mroffset = mroffset;
1159 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1160 Tabular::Feature what)
1162 if (what == SET_SPECIAL_MULTICOLUMN)
1163 cellInfo(cell).align_special = special;
1165 column_info[cellColumn(cell)].align_special = special;
1169 void Tabular::setTopLine(idx_type i, bool line)
1171 cellInfo(i).top_line = line;
1175 void Tabular::setBottomLine(idx_type i, bool line)
1177 cellInfo(i).bottom_line = line;
1181 void Tabular::setLeftLine(idx_type cell, bool line)
1183 cellInfo(cell).left_line = line;
1187 void Tabular::setRightLine(idx_type cell, bool line)
1189 cellInfo(cell).right_line = line;
1192 bool Tabular::rowTopLine(row_type r) const
1194 bool all_rows_set = true;
1195 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1196 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1197 return all_rows_set;
1201 bool Tabular::rowBottomLine(row_type r) const
1203 bool all_rows_set = true;
1204 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1205 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1206 return all_rows_set;
1210 bool Tabular::columnLeftLine(col_type c) const
1217 for (row_type r = 0; r < nrows(); ++r) {
1218 idx_type const i = cellIndex(r, c);
1219 if (c == cellColumn(i)) {
1221 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1222 if (cellInfo(i).left_line || right)
1226 return 2 * nrows_left >= total;
1230 bool Tabular::columnRightLine(col_type c) const
1235 int nrows_right = 0;
1237 for (row_type r = 0; r < nrows(); ++r) {
1238 idx_type i = cellIndex(r, c);
1239 if (c == cellColumn(i) + columnSpan(i) - 1) {
1241 bool left = (c + 1 < ncols()
1242 && cellInfo(cellIndex(r, c + 1)).left_line)
1243 || c + 1 == ncols();
1244 if (cellInfo(i).right_line && left)
1248 return 2 * nrows_right >= total;
1252 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1254 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1255 return cellInfo(cell).alignment;
1257 return column_info[cellColumn(cell)].alignment;
1262 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1264 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1265 return cellInfo(cell).valignment;
1266 return column_info[cellColumn(cell)].valignment;
1270 Length const Tabular::getPWidth(idx_type cell) const
1272 if (isMultiColumn(cell))
1273 return cellInfo(cell).p_width;
1274 return column_info[cellColumn(cell)].p_width;
1278 Length const Tabular::getMROffset(idx_type cell) const
1280 return cellInfo(cell).mroffset;
1284 int Tabular::textHOffset(idx_type cell) const
1286 // the LaTeX Way :-(
1287 int x = WIDTH_OF_LINE;
1289 int const w = cellWidth(cell) - cellInfo(cell).width;
1291 switch (getAlignment(cell)) {
1292 case LYX_ALIGN_CENTER:
1295 case LYX_ALIGN_RIGHT:
1298 case LYX_ALIGN_DECIMAL: {
1299 // we center when no decimal point
1300 if (cellInfo(cell).decimal_width == 0) {
1304 col_type const c = cellColumn(cell);
1305 int max_dhoffset = 0;
1306 for(row_type r = 0; r < row_info.size() ; ++r) {
1307 idx_type const i = cellIndex(r, c);
1308 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1309 && cellInfo(i).decimal_width != 0)
1310 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1312 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1315 // LYX_ALIGN_LEFT: nothing :-)
1323 int Tabular::textVOffset(idx_type cell) const
1325 int voffset = cellInfo(cell).voffset;
1326 if (isMultiRow(cell)) {
1327 row_type const row = cellRow(cell);
1328 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1334 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1337 while (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1339 return cell_info[row][c].cellno;
1343 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1345 col_type c = ncols() - 1;
1346 while (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 if (is_long_tabular)
1400 os << write_attribute("longtabularalignment",
1401 longtabular_alignment);
1403 for (col_type c = 0; c < ncols(); ++c) {
1405 << write_attribute("alignment", column_info[c].alignment);
1406 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1407 os << write_attribute("decimal_point", column_info[c].decimal_point);
1408 os << write_attribute("valignment", column_info[c].valignment)
1409 << write_attribute("width", column_info[c].p_width.asString())
1410 << write_attribute("special", column_info[c].align_special)
1413 for (row_type r = 0; r < nrows(); ++r) {
1414 static const string def("default");
1416 if (row_info[r].top_space_default)
1417 os << write_attribute("topspace", def);
1419 os << write_attribute("topspace", row_info[r].top_space);
1420 if (row_info[r].bottom_space_default)
1421 os << write_attribute("bottomspace", def);
1423 os << write_attribute("bottomspace", row_info[r].bottom_space);
1424 if (row_info[r].interline_space_default)
1425 os << write_attribute("interlinespace", def);
1427 os << write_attribute("interlinespace", row_info[r].interline_space);
1428 os << write_attribute("endhead", row_info[r].endhead)
1429 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1430 << write_attribute("endfoot", row_info[r].endfoot)
1431 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1432 << write_attribute("newpage", row_info[r].newpage)
1433 << write_attribute("caption", row_info[r].caption)
1435 for (col_type c = 0; c < ncols(); ++c) {
1437 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1438 << write_attribute("multirow", cell_info[r][c].multirow)
1439 << write_attribute("mroffset", cell_info[r][c].mroffset)
1440 << write_attribute("alignment", cell_info[r][c].alignment)
1441 << write_attribute("valignment", cell_info[r][c].valignment)
1442 << write_attribute("topline", cell_info[r][c].top_line)
1443 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1444 << write_attribute("leftline", cell_info[r][c].left_line)
1445 << write_attribute("rightline", cell_info[r][c].right_line)
1446 << write_attribute("rotate", cell_info[r][c].rotate)
1447 << write_attribute("usebox", cell_info[r][c].usebox)
1448 << write_attribute("width", cell_info[r][c].p_width)
1449 << write_attribute("special", cell_info[r][c].align_special)
1451 os << "\\begin_inset ";
1452 cell_info[r][c].inset->write(os);
1453 os << "\n\\end_inset\n"
1458 os << "</lyxtabular>\n";
1462 void Tabular::read(Lexer & lex)
1465 istream & is = lex.getStream();
1467 l_getline(is, line);
1468 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1469 LASSERT(false, /**/);
1474 if (!getTokenValue(line, "version", version))
1476 LASSERT(version >= 2, /**/);
1479 if (!getTokenValue(line, "rows", rows_arg))
1482 if (!getTokenValue(line, "columns", columns_arg))
1484 init(buffer_, rows_arg, columns_arg);
1485 l_getline(is, line);
1486 if (!prefixIs(line, "<features")) {
1487 lyxerr << "Wrong tabular format (expected <features ...> got"
1488 << line << ')' << endl;
1491 getTokenValue(line, "rotate", rotate);
1492 getTokenValue(line, "booktabs", use_booktabs);
1493 getTokenValue(line, "islongtable", is_long_tabular);
1494 getTokenValue(line, "tabularvalignment", tabular_valignment);
1495 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1496 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1497 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1498 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1499 getTokenValue(line, "headTopDL", endhead.topDL);
1500 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1501 getTokenValue(line, "footTopDL", endfoot.topDL);
1502 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1503 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1504 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1505 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1507 for (col_type c = 0; c < ncols(); ++c) {
1509 if (!prefixIs(line,"<column")) {
1510 lyxerr << "Wrong tabular format (expected <column ...> got"
1511 << line << ')' << endl;
1514 getTokenValue(line, "alignment", column_info[c].alignment);
1515 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1516 getTokenValue(line, "valignment", column_info[c].valignment);
1517 getTokenValue(line, "width", column_info[c].p_width);
1518 getTokenValue(line, "special", column_info[c].align_special);
1521 for (row_type i = 0; i < nrows(); ++i) {
1522 l_getline(is, line);
1523 if (!prefixIs(line, "<row")) {
1524 lyxerr << "Wrong tabular format (expected <row ...> got"
1525 << line << ')' << endl;
1528 getTokenValue(line, "topspace", row_info[i].top_space,
1529 row_info[i].top_space_default);
1530 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1531 row_info[i].bottom_space_default);
1532 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1533 row_info[i].interline_space_default);
1534 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1535 getTokenValue(line, "endhead", row_info[i].endhead);
1536 getTokenValue(line, "endfoot", row_info[i].endfoot);
1537 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1538 getTokenValue(line, "newpage", row_info[i].newpage);
1539 getTokenValue(line, "caption", row_info[i].caption);
1540 for (col_type j = 0; j < ncols(); ++j) {
1541 l_getline(is, line);
1542 if (!prefixIs(line, "<cell")) {
1543 lyxerr << "Wrong tabular format (expected <cell ...> got"
1544 << line << ')' << endl;
1547 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1548 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1549 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1550 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1551 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1552 getTokenValue(line, "topline", cell_info[i][j].top_line);
1553 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1554 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1555 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1556 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1557 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1558 getTokenValue(line, "width", cell_info[i][j].p_width);
1560 getTokenValue(line, "special", cell_info[i][j].align_special);
1561 l_getline(is, line);
1562 if (prefixIs(line, "\\begin_inset")) {
1563 cell_info[i][j].inset->setBuffer(*buffer_);
1564 cell_info[i][j].inset->read(lex);
1565 l_getline(is, line);
1567 if (!prefixIs(line, "</cell>")) {
1568 lyxerr << "Wrong tabular format (expected </cell> got"
1569 << line << ')' << endl;
1573 l_getline(is, line);
1574 if (!prefixIs(line, "</row>")) {
1575 lyxerr << "Wrong tabular format (expected </row> got"
1576 << line << ')' << endl;
1580 while (!prefixIs(line, "</lyxtabular>")) {
1581 l_getline(is, line);
1587 bool Tabular::isMultiColumn(idx_type cell) const
1589 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1590 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1594 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1596 return cell_info[cellRow(cell)][cellColumn(cell)];
1600 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1602 idx_type const col = cellColumn(cell);
1603 idx_type const row = cellRow(cell);
1604 for (idx_type i = 0; i < number; ++i)
1605 unsetMultiRow(cellIndex(row, col + i));
1607 // unsetting of multirow may have invalidated cell index
1608 cell = cellIndex(row, col);
1609 CellData & cs = cellInfo(cell);
1610 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1611 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1612 cs.alignment = column_info[col].alignment;
1614 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1616 for (idx_type i = 1; i < number; ++i) {
1617 CellData & cs1 = cellInfo(cell + i);
1618 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1619 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1627 bool Tabular::isMultiRow(idx_type cell) const
1629 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1630 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1634 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1636 idx_type const col = cellColumn(cell);
1637 idx_type const row = cellRow(cell);
1638 for (idx_type i = 0; i < number; ++i)
1639 unsetMultiColumn(cellIndex(row + i, col));
1641 // unsetting of multirow may have invalidated cell index
1642 cell = cellIndex(row, col);
1643 CellData & cs = cellInfo(cell);
1644 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1645 cs.valignment = LYX_VALIGN_MIDDLE;
1646 // the horizontal alignment of multirow cells can only
1647 // be changed for the whole table row,
1648 // support changing this only for the multirow cell can be done via
1650 // this feature would be a fileformat change
1651 // until LyX supports this, use the deault alignment of multirow
1653 cs.alignment = LYX_ALIGN_LEFT;
1655 // set the bottom row of the last selected cell
1656 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1658 for (idx_type i = 1; i < number; ++i) {
1659 CellData & cs1 = cell_info[row + i][col];
1660 cs1.multirow = CELL_PART_OF_MULTIROW;
1661 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1669 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1671 row_type const row = cellRow(cell);
1672 col_type const col = cellColumn(cell);
1674 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1681 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1683 col_type const column = cellColumn(cell);
1684 col_type row = cellRow(cell) + 1;
1685 while (row < nrows() && isPartOfMultiRow(row, column))
1688 return row - cellRow(cell);
1692 void Tabular::unsetMultiColumn(idx_type cell)
1694 if (!isMultiColumn(cell))
1697 row_type const row = cellRow(cell);
1698 col_type const col = cellColumn(cell);
1699 row_type const span = columnSpan(cell);
1700 for (col_type c = 0; c < span; ++c) {
1701 // in the table dialog the lines are set in every case
1702 // when unsetting a multicolumn this leads to an additional right
1703 // line for every cell that was part of the former multicolumn cell,
1704 // except if the cell is in the last column
1705 // therefore remove this line
1706 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1707 && (col + c) < (col + span - 1))
1708 cell_info[row][col + c].right_line = false;
1709 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1715 void Tabular::unsetMultiRow(idx_type cell)
1717 if (!isMultiRow(cell))
1720 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1721 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1722 row_type const row = cellRow(cell);
1723 col_type const col = cellColumn(cell);
1724 row_type const span = rowSpan(cell);
1725 for (row_type r = 0; r < span; ++r)
1726 cell_info[row + r][col].multirow = CELL_NORMAL;
1731 void Tabular::setRotateCell(idx_type cell, bool flag)
1733 cellInfo(cell).rotate = flag;
1737 bool Tabular::getRotateCell(idx_type cell) const
1739 return cellInfo(cell).rotate;
1743 bool Tabular::needRotating() const
1747 for (row_type r = 0; r < nrows(); ++r)
1748 for (col_type c = 0; c < ncols(); ++c)
1749 if (cell_info[r][c].rotate)
1755 bool Tabular::isLastCell(idx_type cell) const
1757 if (cell + 1 < numberofcells)
1763 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1765 if (cellRow(cell) == 0)
1768 col_type const col = cellColumn(cell);
1769 row_type r = cellRow(cell) - 1;
1770 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1773 return cell_info[r][col].cellno;
1777 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1779 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1780 if (nextrow < nrows())
1781 return cell_info[nextrow][cellColumn(cell)].cellno;
1786 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1788 LASSERT(column != npos && column < ncols()
1789 && row != npos && row < nrows(), /**/);
1790 return cell_info[row][column].cellno;
1794 void Tabular::setUsebox(idx_type cell, BoxType type)
1796 cellInfo(cell).usebox = type;
1800 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1801 // adds line breaks, see bug 4886.
1802 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1804 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1805 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1807 if (cellInfo(cell).usebox > 1)
1808 return cellInfo(cell).usebox;
1809 return useParbox(cell);
1814 // This are functions used for the longtable support
1816 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1822 row_info[row].endfirsthead = flag;
1826 row_info[row].endhead = flag;
1831 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1834 hd.set = haveLTHead();
1835 return row_info[row].endhead;
1839 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1842 hd.set = haveLTFirstHead();
1843 return row_info[row].endfirsthead;
1847 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1853 row_info[row].endlastfoot = flag;
1857 row_info[row].endfoot = flag;
1862 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1865 fd.set = haveLTFoot();
1866 return row_info[row].endfoot;
1870 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1873 fd.set = haveLTLastFoot();
1874 return row_info[row].endlastfoot;
1878 void Tabular::setLTNewPage(row_type row, bool what)
1880 row_info[row].newpage = what;
1884 bool Tabular::getLTNewPage(row_type row) const
1886 return row_info[row].newpage;
1890 bool Tabular::haveLTHead() const
1892 for (row_type i = 0; i < nrows(); ++i)
1893 if (row_info[i].endhead)
1899 bool Tabular::haveLTFirstHead() const
1901 if (endfirsthead.empty)
1903 for (row_type r = 0; r < nrows(); ++r)
1904 if (row_info[r].endfirsthead)
1910 bool Tabular::haveLTFoot() const
1912 for (row_type r = 0; r < nrows(); ++r)
1913 if (row_info[r].endfoot)
1919 bool Tabular::haveLTLastFoot() const
1921 if (endlastfoot.empty)
1923 for (row_type r = 0; r < nrows(); ++r)
1924 if (row_info[r].endlastfoot)
1930 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1932 idx_type i = getFirstCellInRow(row);
1934 setMultiColumn(i, numberOfCellsInRow(row));
1935 setTopLine(i, false);
1936 setBottomLine(i, false);
1937 setLeftLine(i, false);
1938 setRightLine(i, false);
1940 unsetMultiColumn(i);
1941 // When unsetting a caption row, also all existing
1942 // captions in this row must be dissolved.
1944 row_info[row].caption = what;
1949 bool Tabular::ltCaption(row_type row) const
1951 return row_info[row].caption;
1955 bool Tabular::haveLTCaption() const
1957 for (row_type r = 0; r < nrows(); ++r)
1958 if (row_info[r].caption)
1964 // end longtable support functions
1966 void Tabular::setRowAscent(row_type row, int height)
1968 if (row >= nrows() || row_info[row].ascent == height)
1970 row_info[row].ascent = height;
1974 void Tabular::setRowDescent(row_type row, int height)
1976 if (row >= nrows() || row_info[row].descent == height)
1978 row_info[row].descent = height;
1982 int Tabular::rowAscent(row_type row) const
1984 LASSERT(row < nrows(), /**/);
1985 return row_info[row].ascent;
1989 int Tabular::rowDescent(row_type row) const
1991 LASSERT(row < nrows(), /**/);
1992 return row_info[row].descent;
1996 int Tabular::height() const
1999 for (row_type row = 0; row < nrows(); ++row)
2000 height += rowAscent(row) + rowDescent(row) +
2006 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2008 LASSERT(row < nrows(), /**/);
2009 LASSERT(column < ncols(), /**/);
2010 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2014 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2016 LASSERT(row < nrows(), /**/);
2017 LASSERT(column < ncols(), /**/);
2018 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2022 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
2024 // we only output complete row lines and the 1st row here, the rest
2025 // is done in Tabular::TeXBottomHLine(...)
2027 // get for each column the topline (if any)
2028 vector<bool> topline;
2030 for (col_type c = 0; c < ncols(); ++c) {
2031 topline.push_back(topLine(cellIndex(row, c)));
2032 // If cell is part of a multirow and not the first cell of the
2033 // multirow, no line must be drawn.
2035 if (isMultiRow(cellIndex(row, c))
2036 && isMultiRow(cellIndex(row - 1, c)))
2042 // do nothing if empty first row, or incomplete row line after
2043 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2046 // only output complete row lines and the 1st row's clines
2047 if (nset == ncols()) {
2049 os << (row == 0 ? "\\toprule " : "\\midrule ");
2053 } else if (row == 0) {
2054 for (col_type c = 0; c < ncols(); ++c) {
2056 col_type offset = 0;
2057 for (col_type j = 0 ; j < c; ++j)
2058 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2061 //babel makes the "-" character an active one, so we have to suppress this here
2062 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2063 if (lang == "slovak" || lang == "czech")
2064 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2065 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2067 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2069 col_type cstart = c;
2070 for ( ; c < ncols() && topline[c]; ++c) {}
2072 for (col_type j = cstart ; j < c ; ++j)
2073 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2076 os << c + offset << "} ";
2085 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
2087 // we output bottomlines of row r and the toplines of row r+1
2088 // if the latter do not span the whole tabular
2090 // get the bottomlines of row r, and toplines in next row
2091 bool lastrow = row == nrows() - 1;
2092 vector<bool> bottomline, topline;
2093 bool nextrowset = true;
2094 for (col_type c = 0; c < ncols(); ++c) {
2095 bottomline.push_back(bottomLine(cellIndex(row, c)));
2096 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2097 // If cell is part of a multirow and not the last or first cell of the
2098 // multirow, no line must be drawn.
2100 if (isMultiRow(cellIndex(row, c))
2101 && isMultiRow(cellIndex(row + 1, c))) {
2102 bottomline[c] = false;
2105 nextrowset &= topline[c];
2108 // combine this row's bottom lines and next row's toplines if necessary
2110 for (col_type c = 0; c < ncols(); ++c) {
2112 bottomline[c] = bottomline[c] || topline[c];
2117 // do nothing if empty, OR incomplete row line with a topline in next row
2118 if (nset == 0 || (nextrowset && nset != ncols()))
2121 if (nset == ncols()) {
2123 os << (lastrow ? "\\bottomrule" : "\\midrule");
2127 for (col_type c = 0; c < ncols(); ++c) {
2128 if (bottomline[c]) {
2129 col_type offset = 0;
2130 for (col_type j = 0 ; j < c; ++j)
2131 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2134 //babel makes the "-" character an active one, so we have to suppress this here
2135 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2136 if (lang == "slovak" || lang == "czech")
2137 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2138 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2140 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2142 col_type cstart = c;
2143 for ( ; c < ncols() && bottomline[c]; ++c) {}
2145 for (col_type j = cstart ; j < c ; ++j)
2146 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2149 os << c + offset << "} ";
2158 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell,
2159 bool & ismulticol, bool & ismultirow) const
2162 row_type const r = cellRow(cell);
2163 if (is_long_tabular && row_info[r].caption)
2166 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2167 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2168 // figure out how to set the lines
2169 // we always set double lines to the right of the cell
2170 col_type const c = cellColumn(cell);
2171 col_type const nextcol = c + columnSpan(cell);
2172 bool colright = columnRightLine(c);
2173 bool colleft = columnLeftLine(c);
2174 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2175 bool nextcellleft = nextcol < ncols()
2176 && leftLine(cellIndex(r, nextcol));
2177 bool coldouble = colright && nextcolleft;
2178 bool celldouble = rightLine(cell) && nextcellleft;
2180 ismulticol = isMultiColumn(cell)
2181 || (c == 0 && colleft != leftLine(cell))
2182 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2183 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2184 || (coldouble != celldouble);
2186 // we center in multicol when no decimal point
2187 ismulticol |= ((column_info[c].alignment == LYX_ALIGN_DECIMAL)
2188 && (cellInfo(cell).decimal_width == 0));
2190 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2191 int latexcolspan = columnSpan(cell);
2192 for(col_type col = c; col < c + columnSpan(cell); ++col)
2193 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2197 os << "\\multicolumn{" << latexcolspan << "}{";
2198 if (c ==0 && leftLine(cell))
2200 if (!cellInfo(cell).align_special.empty()) {
2201 os << cellInfo(cell).align_special;
2203 if (!getPWidth(cell).zero()) {
2205 case LYX_ALIGN_LEFT:
2206 os << ">{\\raggedright}";
2208 case LYX_ALIGN_RIGHT:
2209 os << ">{\\raggedleft}";
2211 case LYX_ALIGN_CENTER:
2212 os << ">{\\centering}";
2218 case LYX_VALIGN_TOP:
2221 case LYX_VALIGN_MIDDLE:
2224 case LYX_VALIGN_BOTTOM:
2229 << from_ascii(getPWidth(cell).asLatexString())
2233 case LYX_ALIGN_LEFT:
2236 case LYX_ALIGN_RIGHT:
2243 } // end if else !getPWidth
2244 } // end if else !cellinfo_of_cell
2245 if (rightLine(cell) || nextcellleft)
2248 // add extra vertical line if we want a double one
2251 } // end if ismulticol
2253 // we only need code for the first multirow cell
2254 ismultirow = isMultiRow(cell);
2256 os << "\\multirow{" << rowSpan(cell) << "}{";
2257 if (!getPWidth(cell).zero())
2258 os << from_ascii(getPWidth(cell).asLatexString());
2260 // we need to set a default value
2263 if (!getMROffset(cell).zero())
2264 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2266 } // end if ismultirow
2268 if (getRotateCell(cell)) {
2269 os << "\\begin{sideways}\n";
2272 if (getUsebox(cell) == BOX_PARBOX) {
2275 case LYX_VALIGN_TOP:
2278 case LYX_VALIGN_MIDDLE:
2281 case LYX_VALIGN_BOTTOM:
2285 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2287 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2288 os << "\\begin{minipage}[";
2290 case LYX_VALIGN_TOP:
2293 case LYX_VALIGN_MIDDLE:
2296 case LYX_VALIGN_BOTTOM:
2300 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2308 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell,
2309 bool ismulticol, bool ismultirow) const
2312 row_type const r = cellRow(cell);
2313 if (is_long_tabular && row_info[r].caption)
2317 if (getUsebox(cell) == BOX_PARBOX)
2319 else if (getUsebox(cell) == BOX_MINIPAGE) {
2320 os << "%\n\\end{minipage}";
2323 if (getRotateCell(cell)) {
2324 os << "%\n\\end{sideways}";
2336 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2337 OutputParams const & runparams) const
2339 if (!is_long_tabular)
2344 // the caption must be output before the headers
2345 if (haveLTCaption()) {
2346 for (row_type r = 0; r < nrows(); ++r) {
2347 if (row_info[r].caption) {
2348 ret += TeXRow(os, r, runparams);
2352 // output first header info
2353 // first header must be output before the header, otherwise the
2354 // correct caption placement becomes really weird
2355 if (haveLTFirstHead()) {
2356 if (endfirsthead.topDL) {
2360 for (row_type r = 0; r < nrows(); ++r) {
2361 if (row_info[r].endfirsthead) {
2362 ret += TeXRow(os, r, runparams);
2365 if (endfirsthead.bottomDL) {
2369 os << "\\endfirsthead\n";
2372 // output header info
2374 if (endfirsthead.empty && !haveLTFirstHead()) {
2375 os << "\\endfirsthead\n";
2378 if (endhead.topDL) {
2382 for (row_type r = 0; r < nrows(); ++r) {
2383 if (row_info[r].endhead) {
2384 ret += TeXRow(os, r, runparams);
2387 if (endhead.bottomDL) {
2391 os << "\\endhead\n";
2394 // output footer info
2396 if (endfoot.topDL) {
2400 for (row_type r = 0; r < nrows(); ++r) {
2401 if (row_info[r].endfoot) {
2402 ret += TeXRow(os, r, runparams);
2405 if (endfoot.bottomDL) {
2409 os << "\\endfoot\n";
2411 if (endlastfoot.empty && !haveLTLastFoot()) {
2412 os << "\\endlastfoot\n";
2416 // output lastfooter info
2417 if (haveLTLastFoot()) {
2418 if (endlastfoot.topDL) {
2422 for (row_type r = 0; r < nrows(); ++r) {
2423 if (row_info[r].endlastfoot) {
2424 ret += TeXRow(os, r, runparams);
2427 if (endlastfoot.bottomDL) {
2431 os << "\\endlastfoot\n";
2438 bool Tabular::isValidRow(row_type row) const
2440 if (!is_long_tabular)
2442 return !row_info[row].endhead && !row_info[row].endfirsthead
2443 && !row_info[row].endfoot && !row_info[row].endlastfoot
2444 && !row_info[row].caption;
2448 int Tabular::TeXRow(odocstream & os, row_type row,
2449 OutputParams const & runparams) const
2451 idx_type cell = cellIndex(row, 0);
2452 shared_ptr<InsetTableCell> inset = cellInset(cell);
2453 Paragraph const & par = inset->paragraphs().front();
2454 string const lang = par.getParLanguage(buffer().params())->lang();
2456 //output the top line
2457 int ret = TeXTopHLine(os, row, lang);
2459 if (row_info[row].top_space_default) {
2461 os << "\\addlinespace\n";
2463 os << "\\noalign{\\vskip\\doublerulesep}\n";
2465 } else if(!row_info[row].top_space.zero()) {
2467 os << "\\addlinespace["
2468 << from_ascii(row_info[row].top_space.asLatexString())
2471 os << "\\noalign{\\vskip"
2472 << from_ascii(row_info[row].top_space.asLatexString())
2477 bool ismulticol = false;
2478 bool ismultirow = false;
2479 for (col_type c = 0; c < ncols(); ++c) {
2480 if (isPartOfMultiColumn(row, c))
2483 if (isPartOfMultiRow(row, c) &&
2484 column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2489 cell = cellIndex(row, c);
2490 ret += TeXCellPreamble(os, cell, ismulticol, ismultirow);
2491 shared_ptr<InsetTableCell> inset = cellInset(cell);
2493 Paragraph const & par = inset->paragraphs().front();
2494 bool rtl = par.isRTL(buffer().params())
2496 && getPWidth(cell).zero();
2500 par.getParLanguage(buffer().params())->lang();
2501 if (lang == "farsi")
2503 else if (lang == "arabic_arabi")
2505 // currently, remaning RTL languages are
2506 // arabic_arabtex and hebrew
2510 // pass to the OutputParams that we are in a cell and
2511 // which alignment we have set.
2512 // InsetNewline needs this context information.
2513 OutputParams newrp(runparams);
2514 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2515 ? OutputParams::PLAIN
2516 : OutputParams::ALIGNED;
2518 if (getAlignment(cell) == LYX_ALIGN_DECIMAL
2519 && cellInfo(cell).decimal_width != 0) {
2520 // copy cell and split in 2
2521 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2522 head.getText(0)->setMacrocontextPosition(
2523 cellInset(cell)->getText(0)->macrocontextPosition());
2524 head.setBuffer(buffer());
2525 bool hassep = false;
2526 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2527 head.latex(os, newrp);
2529 ret += tail.latex(os, newrp);
2530 } else if (!isPartOfMultiRow(row, c))
2531 ret += inset->latex(os, newrp);
2533 runparams.encoding = newrp.encoding;
2537 ret += TeXCellPostamble(os, cell, ismulticol, ismultirow);
2538 if (cell != getLastCellInRow(row)) { // not last cell in row
2542 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2543 // if no first header and no empty first header is used,
2544 // the caption needs to be terminated by \endfirsthead
2546 os << "\\endfirsthead";
2548 os << "\\tabularnewline";
2549 if (row_info[row].bottom_space_default) {
2551 os << "\\addlinespace";
2553 os << "[\\doublerulesep]";
2554 } else if (!row_info[row].bottom_space.zero()) {
2556 os << "\\addlinespace";
2558 << from_ascii(row_info[row].bottom_space.asLatexString())
2564 //output the bottom line
2565 ret += TeXBottomHLine(os, row, lang);
2567 if (row_info[row].interline_space_default) {
2569 os << "\\addlinespace\n";
2571 os << "\\noalign{\\vskip\\doublerulesep}\n";
2573 } else if (!row_info[row].interline_space.zero()) {
2575 os << "\\addlinespace["
2576 << from_ascii(row_info[row].interline_space.asLatexString())
2579 os << "\\noalign{\\vskip"
2580 << from_ascii(row_info[row].interline_space.asLatexString())
2588 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2592 //+---------------------------------------------------------------------
2593 //+ first the opening preamble +
2594 //+---------------------------------------------------------------------
2597 os << "\\begin{sideways}\n";
2600 if (is_long_tabular) {
2601 os << "\\begin{longtable}";
2602 switch (longtabular_alignment) {
2603 case LYX_LONGTABULAR_ALIGN_LEFT:
2606 case LYX_LONGTABULAR_ALIGN_CENTER:
2608 case LYX_LONGTABULAR_ALIGN_RIGHT:
2613 os << "\\begin{tabular}";
2614 switch (tabular_valignment) {
2615 case LYX_VALIGN_TOP:
2618 case LYX_VALIGN_MIDDLE:
2620 case LYX_VALIGN_BOTTOM:
2628 for (col_type c = 0; c < ncols(); ++c) {
2629 if (columnLeftLine(c))
2631 if (!column_info[c].align_special.empty()) {
2632 os << column_info[c].align_special;
2634 if (!column_info[c].p_width.zero()) {
2635 switch (column_info[c].alignment) {
2636 case LYX_ALIGN_LEFT:
2637 os << ">{\\raggedright}";
2639 case LYX_ALIGN_RIGHT:
2640 os << ">{\\raggedleft}";
2642 case LYX_ALIGN_CENTER:
2643 os << ">{\\centering}";
2645 case LYX_ALIGN_NONE:
2646 case LYX_ALIGN_BLOCK:
2647 case LYX_ALIGN_LAYOUT:
2648 case LYX_ALIGN_SPECIAL:
2649 case LYX_ALIGN_DECIMAL:
2653 switch (column_info[c].valignment) {
2654 case LYX_VALIGN_TOP:
2657 case LYX_VALIGN_MIDDLE:
2660 case LYX_VALIGN_BOTTOM:
2665 << from_ascii(column_info[c].p_width.asLatexString())
2668 switch (column_info[c].alignment) {
2669 case LYX_ALIGN_LEFT:
2672 case LYX_ALIGN_RIGHT:
2675 case LYX_ALIGN_DECIMAL:
2676 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2682 } // end if else !column_info[i].p_width
2683 } // end if else !column_info[i].align_special
2684 if (columnRightLine(c))
2690 ret += TeXLongtableHeaderFooter(os, runparams);
2692 //+---------------------------------------------------------------------
2693 //+ the single row and columns (cells) +
2694 //+---------------------------------------------------------------------
2696 for (row_type r = 0; r < nrows(); ++r) {
2697 if (isValidRow(r)) {
2698 ret += TeXRow(os, r, runparams);
2699 if (is_long_tabular && row_info[r].newpage) {
2700 os << "\\newpage\n";
2706 //+---------------------------------------------------------------------
2707 //+ the closing of the tabular +
2708 //+---------------------------------------------------------------------
2710 if (is_long_tabular)
2711 os << "\\end{longtable}";
2713 os << "\\end{tabular}";
2715 os << "\n\\end{sideways}";
2723 int Tabular::docbookRow(odocstream & os, row_type row,
2724 OutputParams const & runparams) const
2727 idx_type cell = getFirstCellInRow(row);
2730 for (col_type c = 0; c < ncols(); ++c) {
2731 if (isPartOfMultiColumn(row, c))
2734 os << "<entry align=\"";
2735 switch (getAlignment(cell)) {
2736 case LYX_ALIGN_LEFT:
2739 case LYX_ALIGN_RIGHT:
2747 os << "\" valign=\"";
2748 switch (getVAlignment(cell)) {
2749 case LYX_VALIGN_TOP:
2752 case LYX_VALIGN_BOTTOM:
2755 case LYX_VALIGN_MIDDLE:
2760 if (isMultiColumn(cell)) {
2761 os << " namest=\"col" << c << "\" ";
2762 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2766 ret += cellInset(cell)->docbook(os, runparams);
2775 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2779 //+---------------------------------------------------------------------
2780 //+ first the opening preamble +
2781 //+---------------------------------------------------------------------
2783 os << "<tgroup cols=\"" << ncols()
2784 << "\" colsep=\"1\" rowsep=\"1\">\n";
2786 for (col_type c = 0; c < ncols(); ++c) {
2787 os << "<colspec colname=\"col" << c << "\" align=\"";
2788 switch (column_info[c].alignment) {
2789 case LYX_ALIGN_LEFT:
2792 case LYX_ALIGN_RIGHT:
2800 if (runparams.flavor == OutputParams::XML)
2806 //+---------------------------------------------------------------------
2807 //+ Long Tabular case +
2808 //+---------------------------------------------------------------------
2810 // output caption info
2811 if (haveLTCaption()) {
2812 os << "<caption>\n";
2814 for (row_type r = 0; r < nrows(); ++r) {
2815 if (row_info[r].caption) {
2816 ret += docbookRow(os, r, runparams);
2819 os << "</caption>\n";
2822 // output header info
2823 if (haveLTHead() || haveLTFirstHead()) {
2826 for (row_type r = 0; r < nrows(); ++r) {
2827 if (row_info[r].endhead || row_info[r].endfirsthead) {
2828 ret += docbookRow(os, r, runparams);
2834 // output footer info
2835 if (haveLTFoot() || haveLTLastFoot()) {
2838 for (row_type r = 0; r < nrows(); ++r) {
2839 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2840 ret += docbookRow(os, r, runparams);
2847 //+---------------------------------------------------------------------
2848 //+ the single row and columns (cells) +
2849 //+---------------------------------------------------------------------
2853 for (row_type r = 0; r < nrows(); ++r) {
2854 if (isValidRow(r)) {
2855 ret += docbookRow(os, r, runparams);
2860 //+---------------------------------------------------------------------
2861 //+ the closing of the tabular +
2862 //+---------------------------------------------------------------------
2871 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2872 OutputParams const & runparams) const
2875 idx_type cell = getFirstCellInRow(row);
2877 xs << html::StartTag("tr");
2878 for (col_type c = 0; c < ncols(); ++c) {
2879 if (isPartOfMultiColumn(row, c))
2884 switch (getAlignment(cell)) {
2885 case LYX_ALIGN_LEFT:
2888 case LYX_ALIGN_RIGHT:
2896 attr << " valign='";
2897 switch (getVAlignment(cell)) {
2898 case LYX_VALIGN_TOP:
2901 case LYX_VALIGN_BOTTOM:
2904 case LYX_VALIGN_MIDDLE:
2909 if (isMultiColumn(cell))
2910 attr << " colspan='" << columnSpan(cell) << "'";
2912 xs << html::StartTag("td", attr.str());
2913 ret += cellInset(cell)->xhtml(xs, runparams);
2914 xs << html::EndTag("td");
2917 xs << html::EndTag("tr");
2922 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2925 // It's unclear to me if we need to mess with the long table stuff.
2926 // We can borrow that too from docbook, if so.
2928 xs << html::StartTag("tbody");
2929 for (row_type r = 0; r < nrows(); ++r) {
2930 if (isValidRow(r)) {
2931 ret += xhtmlRow(xs, r, runparams);
2934 xs << html::EndTag("tbody");
2939 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2940 vector<unsigned int> const & clen) const
2942 idx_type const fcell = getFirstCellInRow(row);
2943 idx_type const n = numberOfCellsInRow(row) + fcell;
2946 for (idx_type i = fcell; i < n; ++i) {
2956 for (idx_type i = fcell; i < n; ++i) {
2967 col_type column = cellColumn(i);
2968 int len = clen[column];
2969 while (column < ncols() - 1
2970 && isPartOfMultiColumn(row, ++column))
2971 len += clen[column] + 4;
2972 os << docstring(len, ch);
2987 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2988 vector<unsigned int> const & clen) const
2990 idx_type const fcell = getFirstCellInRow(row);
2991 idx_type const n = numberOfCellsInRow(row) + fcell;
2994 for (idx_type i = fcell; i < n; ++i) {
2995 if (bottomLine(i)) {
3004 for (idx_type i = fcell; i < n; ++i) {
3005 if (bottomLine(i)) {
3015 col_type column = cellColumn(i);
3016 int len = clen[column];
3017 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3018 len += clen[column] + 4;
3019 os << docstring(len, ch);
3020 if (bottomLine(i)) {
3034 void Tabular::plaintextPrintCell(odocstream & os,
3035 OutputParams const & runparams,
3036 idx_type cell, row_type row, col_type column,
3037 vector<unsigned int> const & clen,
3038 bool onlydata) const
3040 odocstringstream sstr;
3041 cellInset(cell)->plaintext(sstr, runparams);
3053 unsigned int len1 = sstr.str().length();
3054 unsigned int len2 = clen[column];
3055 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3056 len2 += clen[column] + 4;
3059 switch (getAlignment(cell)) {
3061 case LYX_ALIGN_LEFT:
3064 case LYX_ALIGN_RIGHT:
3068 case LYX_ALIGN_CENTER:
3074 os << docstring(len1, ' ') << sstr.str()
3075 << docstring(len2, ' ');
3077 if (rightLine(cell))
3084 void Tabular::plaintext(odocstream & os,
3085 OutputParams const & runparams, int const depth,
3086 bool onlydata, char_type delim) const
3088 // first calculate the width of the single columns
3089 vector<unsigned int> clen(ncols());
3092 // first all non multicolumn cells!
3093 for (col_type c = 0; c < ncols(); ++c) {
3095 for (row_type r = 0; r < nrows(); ++r) {
3096 idx_type cell = cellIndex(r, c);
3097 if (isMultiColumn(cell))
3099 odocstringstream sstr;
3100 cellInset(cell)->plaintext(sstr, runparams);
3101 if (clen[c] < sstr.str().length())
3102 clen[c] = sstr.str().length();
3105 // then all multicolumn cells!
3106 for (col_type c = 0; c < ncols(); ++c) {
3107 for (row_type r = 0; r < nrows(); ++r) {
3108 idx_type cell = cellIndex(r, c);
3109 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3111 odocstringstream sstr;
3112 cellInset(cell)->plaintext(sstr, runparams);
3113 int len = int(sstr.str().length());
3114 idx_type const n = columnSpan(cell);
3115 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3117 if (len > int(clen[c + n - 1]))
3118 clen[c + n - 1] = len;
3123 for (row_type r = 0; r < nrows(); ++r) {
3124 if (!onlydata && plaintextTopHLine(os, r, clen))
3125 os << docstring(depth * 2, ' ');
3126 for (col_type c = 0; c < ncols(); ++c) {
3127 if (isPartOfMultiColumn(r, c))
3129 if (onlydata && c > 0)
3130 // we don't use operator<< for single UCS4 character.
3131 // see explanation in docstream.h
3133 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3138 os << docstring(depth * 2, ' ');
3139 if (plaintextBottomHLine(os, r, clen))
3140 os << docstring(depth * 2, ' ');
3146 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3148 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3152 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3153 col_type column) const
3155 return cell_info[row][column].inset;
3159 void Tabular::setCellInset(row_type row, col_type column,
3160 shared_ptr<InsetTableCell> ins) const
3162 CellData & cd = cell_info[row][column];
3167 void Tabular::validate(LaTeXFeatures & features) const
3169 features.require("NeedTabularnewline");
3171 features.require("booktabs");
3172 if (is_long_tabular)
3173 features.require("longtable");
3175 features.require("rotating");
3176 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3177 if (isMultiRow(cell))
3178 features.require("multirow");
3179 if (getVAlignment(cell) != LYX_VALIGN_TOP
3180 || !getPWidth(cell).zero())
3181 features.require("array");
3182 cellInset(cell)->validate(features);
3187 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3189 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3190 ParagraphList::const_iterator cit = parlist.begin();
3191 ParagraphList::const_iterator end = parlist.end();
3193 for (; cit != end; ++cit)
3194 for (int i = 0; i < cit->size(); ++i)
3195 if (cit->isNewline(i))
3202 /////////////////////////////////////////////////////////////////////
3206 /////////////////////////////////////////////////////////////////////
3208 InsetTableCell::InsetTableCell(Buffer * buf)
3209 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3210 contentAlign(LYX_ALIGN_CENTER)
3214 bool InsetTableCell::forcePlainLayout(idx_type) const
3216 return !isFixedWidth;
3220 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3222 return isFixedWidth;
3226 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3227 FuncStatus & status) const
3230 switch (cmd.action()) {
3232 enabled = !forcePlainLayout();
3234 case LFUN_LAYOUT_PARAGRAPH:
3235 enabled = allowParagraphCustomization();
3238 return InsetText::getStatus(cur, cmd, status);
3240 status.setEnabled(enabled);
3244 docstring InsetTableCell::asString(bool intoInsets)
3247 if (paragraphs().empty())
3249 ParagraphList::const_iterator it = paragraphs().begin();
3250 ParagraphList::const_iterator en = paragraphs().end();
3252 for (; it != en; ++it) {
3257 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3263 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3266 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3267 return InsetText::xhtml(xs, rp);
3272 /////////////////////////////////////////////////////////////////////
3276 /////////////////////////////////////////////////////////////////////
3278 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3280 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3281 rowselect_(false), colselect_(false)
3286 InsetTabular::InsetTabular(InsetTabular const & tab)
3287 : Inset(tab), tabular(tab.tabular), scx_(0)
3292 InsetTabular::~InsetTabular()
3294 hideDialogs("tabular", this);
3298 void InsetTabular::setBuffer(Buffer & buf)
3300 tabular.setBuffer(buf);
3301 Inset::setBuffer(buf);
3305 bool InsetTabular::insetAllowed(InsetCode code) const
3310 case MATHMACRO_CODE:
3315 return tabular.is_long_tabular;
3323 void InsetTabular::write(ostream & os) const
3325 os << "Tabular" << endl;
3330 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3332 // FIXME: depending on the selection state,
3333 // we could offer a different menu.
3334 return cell(0)->contextMenuName() + ";" + contextMenuName();
3338 docstring InsetTabular::contextMenuName() const
3340 return from_ascii("context-tabular");
3344 void InsetTabular::read(Lexer & lex)
3346 //bool const old_format = (lex.getString() == "\\LyXTable");
3354 string token = lex.getString();
3355 while (lex && token != "\\end_inset") {
3357 token = lex.getString();
3360 lex.printError("Missing \\end_inset at this point. ");
3364 int InsetTabular::rowFromY(Cursor & cur, int y) const
3366 // top y coordinate of tabular
3367 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3369 for (; r < tabular.nrows() && y > h; ++r)
3370 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3371 + tabular.interRowSpace(r);
3377 int InsetTabular::columnFromX(Cursor & cur, int x) const
3379 // left x coordinate of tabular
3380 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3382 for (; c < tabular.ncols() && x > w; ++c)
3383 w += tabular.cellWidth(c);
3388 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3390 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3391 // mi.base.textwidth << "\n";
3394 LASSERT(false, /**/);
3397 for (row_type r = 0; r < tabular.nrows(); ++r) {
3400 for (col_type c = 0; c < tabular.ncols(); ++c) {
3401 if (tabular.isPartOfMultiColumn(r, c)
3402 || tabular.isPartOfMultiRow(r, c))
3403 // multicolumn or multirow cell, but not first one
3405 idx_type const cell = tabular.cellIndex(r, c);
3408 Length const p_width = tabular.getPWidth(cell);
3409 if (!p_width.zero())
3410 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3411 tabular.cellInset(cell)->metrics(m, dim);
3412 if (!p_width.zero())
3413 dim.wid = m.base.textwidth;
3414 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3415 + tabular.interColumnSpace(cell);
3417 // FIXME(?): do we need a second metrics call?
3418 TextMetrics const & tm =
3419 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3421 // determine horizontal offset because of decimal align (if necessary)
3422 int decimal_hoffset = 0;
3423 int decimal_width = 0;
3424 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3425 // make a copy which we will split in 2
3426 InsetTableCell head = InsetTableCell(*tabular.cellInset(cell).get());
3427 head.getText(0)->setMacrocontextPosition(
3428 tabular.cellInset(cell)->getText(0)->macrocontextPosition());
3429 head.setBuffer(tabular.buffer());
3430 // split in 2 and calculate width of each part
3431 bool hassep = false;
3432 InsetTableCell tail =
3433 splitCell(head, tabular.column_info[c].decimal_point, hassep);
3435 head.metrics(m, dim1);
3436 decimal_hoffset = dim1.width();
3438 tail.metrics(m, dim1);
3439 decimal_width = dim1.width();
3442 tabular.cell_info[r][c].decimal_hoffset = decimal_hoffset;
3443 tabular.cell_info[r][c].decimal_width = decimal_width;
3445 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3446 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3447 int const lastpardes = tm.last().second->descent()
3448 + TEXT_TO_INSET_OFFSET;
3450 switch (tabular.getVAlignment(cell)) {
3451 case Tabular::LYX_VALIGN_TOP:
3453 case Tabular::LYX_VALIGN_MIDDLE:
3454 offset = -(dim.des - lastpardes)/2;
3456 case Tabular::LYX_VALIGN_BOTTOM:
3457 offset = -(dim.des - lastpardes);
3460 tabular.cell_info[r][c].voffset = offset;
3461 maxasc = max(maxasc, dim.asc - offset);
3462 maxdes = max(maxdes, dim.des + offset);
3464 int const top_space = tabular.row_info[r].top_space_default ?
3465 default_line_space :
3466 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3467 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3468 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3469 default_line_space :
3470 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3471 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3474 // for top-alignment the first horizontal table line must be exactly at
3475 // the position of the base line of the surrounding text line
3476 // for bottom alignment, the same is for the last table line
3477 switch (tabular.tabular_valignment) {
3478 case Tabular::LYX_VALIGN_BOTTOM:
3479 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3481 case Tabular::LYX_VALIGN_MIDDLE:
3482 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3484 case Tabular::LYX_VALIGN_TOP:
3485 offset_valign_ = tabular.rowAscent(0);
3489 tabular.updateColumnWidths();
3490 dim.asc = tabular.rowAscent(0) - offset_valign_;
3491 dim.des = tabular.height() - dim.asc;
3492 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3496 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3499 if (&cur.inset() == this && cur.selection()) {
3500 if (cur.selIsMultiCell()) {
3503 getSelection(cur, rs, re, cs, ce);
3505 idx_type const cell = tabular.cellIndex(row, col);
3506 col_type const cspan = tabular.columnSpan(cell);
3507 row_type const rspan = tabular.rowSpan(cell);
3508 if (col + cspan - 1 >= cs && col <= ce
3509 && row + rspan - 1 >= rs && row <= re)
3512 if (col == tabular.cellColumn(cur.idx())
3513 && row == tabular.cellRow(cur.idx())) {
3514 CursorSlice const & beg = cur.selBegin();
3515 CursorSlice const & end = cur.selEnd();
3517 if ((end.lastpos() > 0 || end.lastpit() > 0)
3518 && end.pos() == end.lastpos() && beg.pos() == 0
3519 && end.pit() == end.lastpit() && beg.pit() == 0)
3527 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3529 x += scx_ + ADD_TO_TABULAR_WIDTH;
3531 BufferView * bv = pi.base.bv;
3532 Cursor & cur = pi.base.bv->cursor();
3535 // FIXME: As the full background is painted in drawBackground(),
3536 // we have no choice but to do a full repaint for the Text cells.
3537 pi.full_repaint = true;
3539 bool const original_selection_state = pi.selected;
3542 first_visible_cell = Tabular::npos;
3544 int yy = y + offset_valign_;
3545 for (row_type r = 0; r < tabular.nrows(); ++r) {
3547 for (col_type c = 0; c < tabular.ncols(); ++c) {
3548 if (tabular.isPartOfMultiColumn(r, c))
3551 idx = tabular.cellIndex(r, c);
3553 if (tabular.isPartOfMultiRow(r, c)) {
3554 nx += tabular.cellWidth(idx);
3558 if (first_visible_cell == Tabular::npos)
3559 first_visible_cell = idx;
3561 pi.selected |= isCellSelected(cur, r, c);
3562 int const cx = nx + tabular.textHOffset(idx);
3563 int const cy = yy + tabular.textVOffset(idx);
3564 // Cache the Inset position.
3565 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3566 cell(idx)->draw(pi, cx, cy);
3567 drawCellLines(pi, nx, yy, r, idx);
3568 nx += tabular.cellWidth(idx);
3569 pi.selected = original_selection_state;
3572 if (r + 1 < tabular.nrows())
3573 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3574 + tabular.interRowSpace(r + 1);
3579 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3581 x += scx_ + ADD_TO_TABULAR_WIDTH;
3582 y += offset_valign_ - tabular.rowAscent(0);
3583 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3584 pi.backgroundColor(this));
3588 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3590 Cursor & cur = pi.base.bv->cursor();
3593 x += scx_ + ADD_TO_TABULAR_WIDTH;
3595 if (!cur.selection())
3597 if (&cur.inset() != this)
3602 bool const full_cell_selected = isCellSelected(cur,
3603 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3605 if (cur.selIsMultiCell() || full_cell_selected) {
3606 for (row_type r = 0; r < tabular.nrows(); ++r) {
3608 for (col_type c = 0; c < tabular.ncols(); ++c) {
3609 if (tabular.isPartOfMultiColumn(r, c))
3612 idx_type const cell = tabular.cellIndex(r, c);
3614 if (tabular.isPartOfMultiRow(r, c)) {
3615 xx += tabular.cellWidth(cell);
3618 int const w = tabular.cellWidth(cell);
3619 int const h = tabular.cellHeight(cell);
3620 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3621 if (isCellSelected(cur, r, c))
3622 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3625 if (r + 1 < tabular.nrows())
3626 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3627 + tabular.interRowSpace(r + 1);
3631 // FIXME: This code has no effect because InsetTableCell does not handle
3632 // drawSelection other than the trivial implementation in Inset.
3634 // x += cellXPos(cur.idx());
3635 // x += tabular.textHOffset(cur.idx());
3636 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3641 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3642 row_type row, idx_type cell) const
3644 y -= tabular.rowAscent(row);
3645 int const w = tabular.cellWidth(cell);
3646 int const h = tabular.cellHeight(cell);
3647 Color const linecolor = pi.textColor(Color_tabularline);
3648 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3651 bool drawline = tabular.topLine(cell)
3652 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3653 pi.pain.line(x, y, x + w, y,
3654 drawline ? linecolor : gridcolor,
3655 drawline ? Painter::line_solid : Painter::line_onoffdash);
3658 drawline = tabular.bottomLine(cell);
3659 pi.pain.line(x, y + h, x + w, y + h,
3660 drawline ? linecolor : gridcolor,
3661 drawline ? Painter::line_solid : Painter::line_onoffdash);
3664 col_type const col = tabular.cellColumn(cell);
3665 drawline = tabular.leftLine(cell)
3666 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3667 pi.pain.line(x, y, x, y + h,
3668 drawline ? linecolor : gridcolor,
3669 drawline ? Painter::line_solid : Painter::line_onoffdash);
3672 x -= tabular.interColumnSpace(cell);
3673 drawline = tabular.rightLine(cell)
3674 || (col + 1 < tabular.ncols()
3675 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3676 pi.pain.line(x + w, y, x + w, y + h,
3677 drawline ? linecolor : gridcolor,
3678 drawline ? Painter::line_solid : Painter::line_onoffdash);
3682 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3684 //lyxerr << "InsetTabular::edit: " << this << endl;
3686 cur.setSelection(false);
3689 if (isRightToLeft(cur))
3690 cur.idx() = tabular.getLastCellInRow(0);
3696 if (isRightToLeft(cur))
3697 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3699 cur.idx() = tabular.numberofcells - 1;
3701 cur.pos() = cur.lastpos(); // FIXME crude guess
3703 cur.setCurrentFont();
3704 // FIXME: this accesses the position cache before it is initialized
3706 //cur.bv().fitCursor();
3710 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3712 // In a longtable, tell captions what the current float is
3713 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3714 string const saveflt = cnts.current_float();
3715 if (tabular.is_long_tabular)
3716 cnts.current_float("table");
3718 ParIterator it2 = it;
3720 size_t const end = it2.nargs();
3721 for ( ; it2.idx() < end; it2.top().forwardIdx())
3722 buffer().updateBuffer(it2, utype);
3725 if (tabular.is_long_tabular)
3726 cnts.current_float(saveflt);
3730 void InsetTabular::addToToc(DocIterator const & cpit)
3732 DocIterator dit = cpit;
3734 size_t const end = dit.nargs();
3735 for ( ; dit.idx() < end; dit.top().forwardIdx())
3736 cell(dit.idx())->addToToc(dit);
3740 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3742 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3743 << "\n cur:" << cur);
3744 CursorSlice sl = cur.top();
3745 Cursor & bvcur = cur.bv().cursor();
3747 FuncCode const act = cmd.action();
3751 case LFUN_MOUSE_PRESS: {
3752 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3754 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3755 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3756 row_type r = rowFromY(cur, cmd.y());
3757 cur.idx() = tabular.getFirstCellInRow(r);
3760 cur.idx() = tabular.getLastCellInRow(r);
3761 cur.pos() = cur.lastpos();
3762 cur.setSelection(true);
3768 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3769 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3770 || cmd.y() > y0 + tabular.height()) {
3771 col_type c = columnFromX(cur, cmd.x());
3772 cur.idx() = tabular.cellIndex(0, c);
3775 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3776 cur.pos() = cur.lastpos();
3777 cur.setSelection(true);
3782 // do not reset cursor/selection if we have selected
3783 // some cells (bug 2715).
3784 if (cmd.button() == mouse_button::button3
3785 && &bvcur.selBegin().inset() == this
3786 && bvcur.selIsMultiCell())
3789 // Let InsetTableCell do it
3790 cell(cur.idx())->dispatch(cur, cmd);
3793 case LFUN_MOUSE_MOTION:
3794 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3795 if (cmd.button() == mouse_button::button1) {
3796 // only accept motions to places not deeper nested than the real anchor
3797 if (!bvcur.realAnchor().hasPart(cur)) {
3801 // select (additional) row
3803 row_type r = rowFromY(cur, cmd.y());
3804 cur.idx() = tabular.getLastCellInRow(r);
3805 // we need to reset the cursor's pit and pos now, as the old ones
3806 // may no longer be valid.
3809 bvcur.setCursor(cur);
3810 bvcur.setSelection(true);
3813 // select (additional) column
3815 col_type c = columnFromX(cur, cmd.x());
3816 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3817 // we need to reset the cursor's pit and pos now, as the old ones
3818 // may no longer be valid.
3821 bvcur.setCursor(cur);
3822 bvcur.setSelection(true);
3825 // only update if selection changes
3826 if (bvcur.idx() == cur.idx() &&
3827 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3828 cur.noScreenUpdate();
3829 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3830 bvcur.setCursor(cur);
3831 bvcur.setSelection(true);
3832 // if this is a multicell selection, we just set the cursor to
3833 // the beginning of the cell's text.
3834 if (bvcur.selIsMultiCell()) {
3835 bvcur.pit() = bvcur.lastpit();
3836 bvcur.pos() = bvcur.lastpos();
3841 case LFUN_MOUSE_RELEASE:
3846 case LFUN_CELL_BACKWARD:
3848 cur.setSelection(false);
3851 case LFUN_CELL_FORWARD:
3853 cur.setSelection(false);
3856 case LFUN_CHAR_FORWARD_SELECT:
3857 case LFUN_CHAR_FORWARD:
3858 case LFUN_CHAR_BACKWARD_SELECT:
3859 case LFUN_CHAR_BACKWARD:
3860 case LFUN_CHAR_RIGHT_SELECT:
3861 case LFUN_CHAR_RIGHT:
3862 case LFUN_CHAR_LEFT_SELECT:
3863 case LFUN_CHAR_LEFT:
3864 case LFUN_WORD_FORWARD:
3865 case LFUN_WORD_FORWARD_SELECT:
3866 case LFUN_WORD_BACKWARD:
3867 case LFUN_WORD_BACKWARD_SELECT:
3868 case LFUN_WORD_RIGHT:
3869 case LFUN_WORD_RIGHT_SELECT:
3870 case LFUN_WORD_LEFT:
3871 case LFUN_WORD_LEFT_SELECT: {
3872 // determine whether we move to next or previous cell, where to enter
3873 // the new cell from, and which command to "finish" (i.e., exit the
3876 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3877 FuncCode finish_lfun;
3879 if (act == LFUN_CHAR_FORWARD
3880 || act == LFUN_CHAR_FORWARD_SELECT
3881 || act == LFUN_WORD_FORWARD
3882 || act == LFUN_WORD_FORWARD_SELECT) {
3884 finish_lfun = LFUN_FINISHED_FORWARD;
3886 else if (act == LFUN_CHAR_BACKWARD
3887 || act == LFUN_CHAR_BACKWARD_SELECT
3888 || act == LFUN_WORD_BACKWARD
3889 || act == LFUN_WORD_BACKWARD_SELECT) {
3891 finish_lfun = LFUN_FINISHED_BACKWARD;
3893 // LEFT or RIGHT commands --- the interpretation will depend on the
3894 // table's direction.
3896 bool const right = act == LFUN_CHAR_RIGHT
3897 || act == LFUN_CHAR_RIGHT_SELECT
3898 || act == LFUN_WORD_RIGHT
3899 || act == LFUN_WORD_RIGHT_SELECT;
3900 next_cell = isRightToLeft(cur) != right;
3902 if (lyxrc.visual_cursor)
3903 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3905 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3908 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3909 || act == LFUN_CHAR_BACKWARD_SELECT
3910 || act == LFUN_CHAR_RIGHT_SELECT
3911 || act == LFUN_CHAR_LEFT_SELECT
3912 || act == LFUN_WORD_FORWARD_SELECT
3913 || act == LFUN_WORD_RIGHT_SELECT
3914 || act == LFUN_WORD_BACKWARD_SELECT
3915 || act == LFUN_WORD_LEFT_SELECT;
3917 // If we have a multicell selection or we're
3918 // not doing some LFUN_*_SELECT thing anyway...
3919 if (!cur.selIsMultiCell() || !select) {
3920 col_type const c = tabular.cellColumn(cur.idx());
3921 row_type const r = tabular.cellRow(cur.idx());
3922 // Are we trying to select the whole cell and is the whole cell
3923 // not yet selected?
3924 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3925 ((next_cell && cur.pit() == cur.lastpit()
3926 && cur.pos() == cur.lastpos())
3927 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3929 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3931 // ...try to dispatch to the cell's inset.
3932 cell(cur.idx())->dispatch(cur, cmd);
3934 // When we already have a selection we want to select the whole cell
3935 // before going to the next cell.
3936 if (select_whole && !empty_cell){
3937 getText(cur.idx())->selectAll(cur);
3942 // FIXME: When we support the selection of an empty cell, remove
3943 // the !empty_cell from this condition. For now we jump to the next
3944 // cell if the current cell is empty.
3945 if (cur.result().dispatched() && !empty_cell)
3949 // move to next/prev cell, as appropriate
3950 // note that we will always do this if we're selecting and we have
3951 // a multicell selection
3952 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3953 << " cell from: " << int(entry_from));
3955 moveNextCell(cur, entry_from);
3957 movePrevCell(cur, entry_from);
3958 // if we're exiting the table, call the appropriate FINISHED lfun
3959 if (sl == cur.top())
3960 cmd = FuncRequest(finish_lfun);
3967 case LFUN_DOWN_SELECT:
3969 if (!(cur.selection() && cur.selIsMultiCell()))
3970 cell(cur.idx())->dispatch(cur, cmd);
3972 cur.dispatched(); // override the cell's decision
3973 if (sl == cur.top()) {
3974 // if our Text didn't do anything to the cursor
3975 // then we try to put the cursor into the cell below
3976 // setting also the right targetX.
3977 cur.selHandle(act == LFUN_DOWN_SELECT);
3978 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
3979 cur.idx() = tabular.cellBelow(cur.idx());
3981 TextMetrics const & tm =
3982 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3983 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3984 cur.setCurrentFont();
3987 if (sl == cur.top()) {
3988 // we trick it to go to forward after leaving the
3990 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3993 if (cur.selIsMultiCell()) {
3994 cur.pit() = cur.lastpit();
3995 cur.pos() = cur.lastpos();
3996 cur.setCurrentFont();
4001 case LFUN_UP_SELECT:
4003 if (!(cur.selection() && cur.selIsMultiCell()))
4004 cell(cur.idx())->dispatch(cur, cmd);
4005 cur.dispatched(); // override the cell's decision
4006 if (sl == cur.top()) {
4007 // if our Text didn't do anything to the cursor
4008 // then we try to put the cursor into the cell above
4009 // setting also the right targetX.
4010 cur.selHandle(act == LFUN_UP_SELECT);
4011 if (tabular.cellRow(cur.idx()) != 0) {
4012 cur.idx() = tabular.cellAbove(cur.idx());
4013 cur.pit() = cur.lastpit();
4014 Text const * text = cell(cur.idx())->getText(0);
4015 TextMetrics const & tm = cur.bv().textMetrics(text);
4016 ParagraphMetrics const & pm =
4017 tm.parMetrics(cur.lastpit());
4018 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
4019 cur.setCurrentFont();
4022 if (sl == cur.top()) {
4023 cmd = FuncRequest(LFUN_UP);
4026 if (cur.selIsMultiCell()) {
4028 cur.pos() = cur.lastpos();
4029 cur.setCurrentFont();
4034 // case LFUN_SCREEN_DOWN: {
4035 // //if (hasSelection())
4036 // // cur.selection() = false;
4037 // col_type const col = tabular.cellColumn(cur.idx());
4038 // int const t = cur.bv().top_y() + cur.bv().height();
4039 // if (t < yo() + tabular.getHeightOfTabular()) {
4040 // cur.bv().scrollDocView(t, true);
4041 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4043 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4050 // case LFUN_SCREEN_UP: {
4051 // //if (hasSelection())
4052 // // cur.selection() = false;
4053 // col_type const col = tabular.cellColumn(cur.idx());
4054 // int const t = cur.bv().top_y() + cur.bv().height();
4056 // cur.bv().scrollDocView(t, true);
4060 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4064 // cur.par() = cur.lastpar();
4065 // cur.pos() = cur.lastpos();
4069 case LFUN_LAYOUT_TABULAR:
4070 cur.bv().showDialog("tabular");
4073 case LFUN_INSET_MODIFY:
4074 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4078 // insert file functions
4079 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4080 case LFUN_FILE_INSERT_PLAINTEXT:
4082 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4083 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4084 FileName(to_utf8(cmd.argument())));
4087 cur.recordUndoInset(INSERT_UNDO);
4088 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4089 // content has been replaced,
4090 // so cursor might be invalid
4091 cur.pos() = cur.lastpos();
4092 cur.pit() = cur.lastpit();
4093 bvcur.setCursor(cur);
4100 if (cur.selIsMultiCell()) {
4101 if (copySelection(cur)) {
4102 cur.recordUndoInset(DELETE_UNDO);
4106 cell(cur.idx())->dispatch(cur, cmd);
4109 case LFUN_SELF_INSERT:
4110 if (cur.selIsMultiCell()) {
4111 cur.recordUndoInset(DELETE_UNDO);
4114 cell(cur.idx())->dispatch(cur, cmd);
4117 case LFUN_CHAR_DELETE_BACKWARD:
4118 case LFUN_CHAR_DELETE_FORWARD:
4119 if (cur.selIsMultiCell()) {
4120 cur.recordUndoInset(DELETE_UNDO);
4123 cell(cur.idx())->dispatch(cur, cmd);
4127 if (!cur.selection())
4129 if (cur.selIsMultiCell()) {
4133 cell(cur.idx())->dispatch(cur, cmd);
4136 case LFUN_CLIPBOARD_PASTE:
4137 case LFUN_PRIMARY_SELECTION_PASTE: {
4138 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4139 theClipboard().getAsText() :
4140 theSelection().get();
4143 // pass to InsertPlaintextString, but
4144 // only if we have multi-cell content
4145 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4146 cur.recordUndoInset(INSERT_UNDO);
4147 if (insertPlaintextString(cur.bv(), clip, false)) {
4148 // content has been replaced,
4149 // so cursor might be invalid
4150 cur.pos() = cur.lastpos();
4151 cur.pit() = cur.lastpit();
4152 bvcur.setCursor(cur);
4156 // Let the cell handle normal text
4157 cell(cur.idx())->dispatch(cur, cmd);
4162 if (!tabularStackDirty()) {
4163 if (!cur.selIsMultiCell())
4164 cell(cur.idx())->dispatch(cur, cmd);
4167 if (theClipboard().isInternal() ||
4168 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
4169 cur.recordUndoInset(INSERT_UNDO);
4170 pasteClipboard(cur);
4174 case LFUN_FONT_EMPH:
4175 case LFUN_FONT_BOLD:
4176 case LFUN_FONT_BOLDSYMBOL:
4177 case LFUN_FONT_ROMAN:
4178 case LFUN_FONT_NOUN:
4179 case LFUN_FONT_ITAL:
4180 case LFUN_FONT_FRAK:
4181 case LFUN_FONT_TYPEWRITER:
4182 case LFUN_FONT_SANS:
4183 case LFUN_TEXTSTYLE_APPLY:
4184 case LFUN_TEXTSTYLE_UPDATE:
4185 case LFUN_FONT_SIZE:
4186 case LFUN_FONT_UNDERLINE:
4187 case LFUN_FONT_STRIKEOUT:
4188 case LFUN_FONT_UULINE:
4189 case LFUN_FONT_UWAVE:
4191 case LFUN_WORD_CAPITALIZE:
4192 case LFUN_WORD_UPCASE:
4193 case LFUN_WORD_LOWCASE:
4194 case LFUN_CHARS_TRANSPOSE:
4195 if (cur.selIsMultiCell()) {
4198 getSelection(cur, rs, re, cs, ce);
4199 Cursor tmpcur = cur;
4200 for (row_type r = rs; r <= re; ++r) {
4201 for (col_type c = cs; c <= ce; ++c) {
4202 // cursor follows cell:
4203 tmpcur.idx() = tabular.cellIndex(r, c);
4204 // select this cell only:
4207 tmpcur.resetAnchor();
4208 tmpcur.pit() = tmpcur.lastpit();
4209 tmpcur.pos() = tmpcur.top().lastpos();
4210 tmpcur.setCursor(tmpcur);
4211 tmpcur.setSelection();
4212 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4217 cell(cur.idx())->dispatch(cur, cmd);
4221 case LFUN_INSET_SETTINGS:
4222 // relay this lfun to Inset, not to the cell.
4223 Inset::doDispatch(cur, cmd);
4227 // we try to handle this event in the insets dispatch function.
4228 cell(cur.idx())->dispatch(cur, cmd);
4234 // function sets an object as defined in func_status.h:
4235 // states OK, Unknown, Disabled, On, Off.
4236 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4237 FuncStatus & status) const
4239 switch (cmd.action()) {
4240 case LFUN_INSET_MODIFY: {
4241 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4244 string const s = cmd.getArg(1);
4245 // FIXME: We only check for the very first argument...
4246 int action = Tabular::LAST_ACTION;
4248 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4249 if (tabularFeature[i].feature == s) {
4250 action = tabularFeature[i].action;
4254 if (action == Tabular::LAST_ACTION) {
4256 status.setUnknown(true);
4260 string const argument = cmd.getLongArg(2);
4262 row_type sel_row_start = 0;
4263 row_type sel_row_end = 0;
4264 col_type sel_col_start = 0;
4265 col_type sel_col_end = 0;
4266 Tabular::ltType dummyltt;
4269 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4272 case Tabular::SET_PWIDTH:
4273 case Tabular::SET_MPWIDTH:
4274 case Tabular::SET_SPECIAL_COLUMN:
4275 case Tabular::SET_SPECIAL_MULTICOLUMN:
4276 case Tabular::APPEND_ROW:
4277 case Tabular::APPEND_COLUMN:
4278 case Tabular::DELETE_ROW:
4279 case Tabular::DELETE_COLUMN:
4280 case Tabular::COPY_ROW:
4281 case Tabular::COPY_COLUMN:
4282 case Tabular::SET_TOP_SPACE:
4283 case Tabular::SET_BOTTOM_SPACE:
4284 case Tabular::SET_INTERLINE_SPACE:
4288 case Tabular::SET_DECIMAL_POINT:
4290 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4293 case Tabular::SET_MULTICOLUMN:
4294 case Tabular::UNSET_MULTICOLUMN:
4295 case Tabular::MULTICOLUMN:
4296 // If a row is set as longtable caption, it must not be allowed
4297 // to unset that this row is a multicolumn.
4298 status.setEnabled(sel_row_start == sel_row_end
4299 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4300 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4303 case Tabular::SET_MULTIROW:
4304 case Tabular::UNSET_MULTIROW:
4305 case Tabular::MULTIROW:
4306 // If a row is set as longtable caption, it must not be allowed
4307 // to unset that this row is a multirow.
4308 status.setEnabled(sel_col_start == sel_col_end
4309 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4310 status.setOnOff(tabular.isMultiRow(cur.idx()));
4313 case Tabular::SET_ALL_LINES:
4314 case Tabular::UNSET_ALL_LINES:
4315 case Tabular::SET_BORDER_LINES:
4316 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4319 case Tabular::SET_LINE_TOP:
4320 case Tabular::SET_LINE_BOTTOM:
4321 case Tabular::SET_LINE_LEFT:
4322 case Tabular::SET_LINE_RIGHT:
4323 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4326 case Tabular::TOGGLE_LINE_TOP:
4327 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4328 status.setOnOff(tabular.topLine(cur.idx()));
4331 case Tabular::TOGGLE_LINE_BOTTOM:
4332 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4333 status.setOnOff(tabular.bottomLine(cur.idx()));
4336 case Tabular::TOGGLE_LINE_LEFT:
4337 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4338 status.setOnOff(tabular.leftLine(cur.idx()));
4341 case Tabular::TOGGLE_LINE_RIGHT:
4342 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4343 status.setOnOff(tabular.rightLine(cur.idx()));
4346 // multirow cells only inherit the alignment of the column if the column has
4347 // no width, otherwise they are left-aligned
4348 // therefore allow always left but right and center only if there is no width
4349 case Tabular::M_ALIGN_LEFT:
4351 case Tabular::ALIGN_LEFT:
4352 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4355 case Tabular::M_ALIGN_RIGHT:
4357 case Tabular::ALIGN_RIGHT:
4358 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4359 && !tabular.getPWidth(cur.idx()).zero()));
4360 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4363 case Tabular::M_ALIGN_CENTER:
4365 case Tabular::ALIGN_CENTER:
4366 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4367 && !tabular.getPWidth(cur.idx()).zero()));
4368 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4371 case Tabular::ALIGN_BLOCK:
4372 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4373 && !tabular.isMultiRow(cur.idx()));
4374 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4377 case Tabular::ALIGN_DECIMAL:
4378 status.setEnabled(!tabular.isMultiRow(cur.idx())
4379 && !tabular.isMultiColumn(cur.idx()));
4380 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4383 case Tabular::M_VALIGN_TOP:
4385 case Tabular::VALIGN_TOP:
4386 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4387 && !tabular.isMultiRow(cur.idx()));
4389 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4392 case Tabular::M_VALIGN_BOTTOM:
4394 case Tabular::VALIGN_BOTTOM:
4395 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4396 && !tabular.isMultiRow(cur.idx()));
4398 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4401 case Tabular::M_VALIGN_MIDDLE:
4403 case Tabular::VALIGN_MIDDLE:
4404 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4405 && !tabular.isMultiRow(cur.idx()));
4407 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4410 case Tabular::SET_LONGTABULAR:
4411 // setting as longtable is not allowed when table is inside a float
4412 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4413 || cur.innerInsetOfType(WRAP_CODE) != 0)
4414 status.setEnabled(false);
4416 status.setEnabled(true);
4417 status.setOnOff(tabular.is_long_tabular);
4420 case Tabular::UNSET_LONGTABULAR:
4421 status.setOnOff(!tabular.is_long_tabular);
4424 case Tabular::TOGGLE_ROTATE_TABULAR:
4425 case Tabular::SET_ROTATE_TABULAR:
4426 status.setOnOff(tabular.rotate);
4429 case Tabular::TABULAR_VALIGN_TOP:
4430 status.setOnOff(tabular.tabular_valignment
4431 == Tabular::LYX_VALIGN_TOP);
4433 case Tabular::TABULAR_VALIGN_MIDDLE:
4434 status.setOnOff(tabular.tabular_valignment
4435 == Tabular::LYX_VALIGN_MIDDLE);
4437 case Tabular::TABULAR_VALIGN_BOTTOM:
4438 status.setOnOff(tabular.tabular_valignment
4439 == Tabular::LYX_VALIGN_BOTTOM);
4442 case Tabular::LONGTABULAR_ALIGN_LEFT:
4443 status.setOnOff(tabular.longtabular_alignment
4444 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4446 case Tabular::LONGTABULAR_ALIGN_CENTER:
4447 status.setOnOff(tabular.longtabular_alignment
4448 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4450 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4451 status.setOnOff(tabular.longtabular_alignment
4452 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4455 case Tabular::UNSET_ROTATE_TABULAR:
4456 status.setOnOff(!tabular.rotate);
4459 case Tabular::TOGGLE_ROTATE_CELL:
4460 case Tabular::SET_ROTATE_CELL:
4461 status.setOnOff(!oneCellHasRotationState(false,
4462 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4465 case Tabular::UNSET_ROTATE_CELL:
4466 status.setOnOff(!oneCellHasRotationState(true,
4467 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4470 case Tabular::SET_USEBOX:
4471 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4474 // every row can only be one thing:
4475 // either a footer or header or caption
4476 case Tabular::SET_LTFIRSTHEAD:
4477 status.setEnabled(sel_row_start == sel_row_end
4478 && !tabular.ltCaption(sel_row_start));
4479 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4482 case Tabular::UNSET_LTFIRSTHEAD:
4483 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4486 case Tabular::SET_LTHEAD:
4487 status.setEnabled(sel_row_start == sel_row_end
4488 && !tabular.ltCaption(sel_row_start));
4489 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4492 case Tabular::UNSET_LTHEAD:
4493 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4496 case Tabular::SET_LTFOOT:
4497 status.setEnabled(sel_row_start == sel_row_end
4498 && !tabular.ltCaption(sel_row_start));
4499 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4502 case Tabular::UNSET_LTFOOT:
4503 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4506 case Tabular::SET_LTLASTFOOT:
4507 status.setEnabled(sel_row_start == sel_row_end
4508 && !tabular.ltCaption(sel_row_start));
4509 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4512 case Tabular::UNSET_LTLASTFOOT:
4513 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4516 case Tabular::SET_LTNEWPAGE:
4517 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4520 // only one row can be the caption
4521 // and a multirow cannot be set as caption
4522 case Tabular::SET_LTCAPTION:
4523 case Tabular::UNSET_LTCAPTION:
4524 case Tabular::TOGGLE_LTCAPTION:
4525 status.setEnabled(sel_row_start == sel_row_end
4526 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4527 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4528 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4529 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4530 && (!tabular.haveLTCaption()
4531 || tabular.ltCaption(sel_row_start))
4532 && !tabular.isMultiRow(sel_row_start));
4533 status.setOnOff(tabular.ltCaption(sel_row_start));
4536 case Tabular::SET_BOOKTABS:
4537 status.setOnOff(tabular.use_booktabs);
4540 case Tabular::UNSET_BOOKTABS:
4541 status.setOnOff(!tabular.use_booktabs);
4546 status.setEnabled(false);
4552 // These are only enabled inside tabular
4553 case LFUN_CELL_BACKWARD:
4554 case LFUN_CELL_FORWARD:
4555 status.setEnabled(true);
4558 // disable these with multiple cells selected
4559 case LFUN_INSET_INSERT:
4560 case LFUN_TABULAR_INSERT:
4561 case LFUN_FLEX_INSERT:
4562 case LFUN_FLOAT_INSERT:
4563 case LFUN_FLOAT_WIDE_INSERT:
4564 case LFUN_FOOTNOTE_INSERT:
4565 case LFUN_MARGINALNOTE_INSERT:
4566 case LFUN_MATH_INSERT:
4567 case LFUN_MATH_MODE:
4568 case LFUN_MATH_MUTATE:
4569 case LFUN_MATH_DISPLAY:
4570 case LFUN_NOTE_INSERT:
4571 case LFUN_ARGUMENT_INSERT:
4572 case LFUN_BOX_INSERT:
4573 case LFUN_BRANCH_INSERT:
4574 case LFUN_PHANTOM_INSERT:
4575 case LFUN_WRAP_INSERT:
4576 case LFUN_PREVIEW_INSERT:
4577 case LFUN_ERT_INSERT: {
4578 if (cur.selIsMultiCell()) {
4579 status.setEnabled(false);
4582 return cell(cur.idx())->getStatus(cur, cmd, status);
4585 // disable in non-fixed-width cells
4586 case LFUN_NEWLINE_INSERT:
4587 case LFUN_BREAK_PARAGRAPH: {
4588 if (tabular.getPWidth(cur.idx()).zero()) {
4589 status.setEnabled(false);
4592 return cell(cur.idx())->getStatus(cur, cmd, status);
4595 case LFUN_NEWPAGE_INSERT:
4596 status.setEnabled(false);
4600 if (tabularStackDirty() && theClipboard().isInternal()) {
4601 if (cur.selIsMultiCell()) {
4604 getSelection(cur, rs, re, cs, ce);
4605 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4606 && paste_tabular->nrows() == re - rs + 1)
4607 status.setEnabled(true);
4609 status.setEnabled(false);
4610 status.message(_("Selection size should match clipboard content."));
4613 status.setEnabled(true);
4616 return cell(cur.idx())->getStatus(cur, cmd, status);
4618 case LFUN_INSET_SETTINGS:
4619 // relay this lfun to Inset, not to the cell.
4620 return Inset::getStatus(cur, cmd, status);
4623 // we try to handle this event in the insets dispatch function.
4624 return cell(cur.idx())->getStatus(cur, cmd, status);
4630 Inset::DisplayType InsetTabular::display() const
4632 if (tabular.is_long_tabular) {
4633 switch (tabular.longtabular_alignment) {
4634 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4636 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4638 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4648 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4650 return tabular.latex(os, runparams);
4654 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4656 os << '\n'; // output table on a new line
4657 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4658 tabular.plaintext(os, runparams, dp, false, 0);
4659 return PLAINTEXT_NEWLINE;
4663 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4668 // FIXME: Why not pass a proper DocIterator here?
4670 // if the table is inside a float it doesn't need the informaltable
4671 // wrapper. Search for it.
4672 for (master = owner(); master; master = master->owner())
4673 if (master->lyxCode() == FLOAT_CODE)
4678 os << "<informaltable>";
4681 ret += tabular.docbook(os, runparams);
4683 os << "</informaltable>";
4690 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4693 // It'd be better to be able to get this from an InsetLayout, but at present
4694 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4695 xs << html::StartTag("table");
4696 docstring ret = tabular.xhtml(xs, rp);
4697 xs << html::EndTag("table");
4702 void InsetTabular::validate(LaTeXFeatures & features) const
4704 tabular.validate(features);
4706 // It'd be better to be able to get this from an InsetLayout, but at present
4707 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4708 if (features.runparams().flavor == OutputParams::HTML)
4709 features.addPreambleSnippet("<style type=\"text/css\">\n"
4710 "table { border: 1px solid black; display: inline-block; }\n"
4711 "td { border: 1px solid black; padding: 0.5ex; }\n"
4716 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4718 return tabular.cellInset(idx);
4722 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4724 return tabular.cellInset(idx);
4728 void InsetTabular::cursorPos(BufferView const & bv,
4729 CursorSlice const & sl, bool boundary, int & x, int & y) const
4731 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4733 // y offset correction
4734 y += cellYPos(sl.idx());
4735 y += tabular.textVOffset(sl.idx());
4736 y += offset_valign_;
4738 // x offset correction
4739 x += cellXPos(sl.idx());
4740 x += tabular.textHOffset(sl.idx());
4741 x += ADD_TO_TABULAR_WIDTH;
4746 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4750 Inset const & inset = *tabular.cellInset(cell);
4751 Point o = bv.coordCache().getInsets().xy(&inset);
4752 int const xbeg = o.x_ - tabular.textHOffset(cell);
4753 int const xend = xbeg + tabular.cellWidth(cell);
4754 row_type const row = tabular.cellRow(cell);
4755 int const ybeg = o.y_ - tabular.rowAscent(row)
4756 - tabular.interRowSpace(row);
4757 int const yend = ybeg + tabular.cellHeight(cell);
4769 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4770 // << " ybeg=" << ybeg << " yend=" << yend
4771 // << " xx=" << xx << " yy=" << yy
4772 // << " dist=" << xx + yy << endl;
4777 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4779 //lyxerr << "InsetTabular::editXY: " << this << endl;
4780 cur.setSelection(false);
4782 cur.idx() = getNearestCell(cur.bv(), x, y);
4784 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4788 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4790 cur.idx() = getNearestCell(cur.bv(), x, y);
4791 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4795 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4797 idx_type idx_min = 0;
4798 int dist_min = numeric_limits<int>::max();
4799 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4800 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4801 int const d = dist(bv, i, x, y);
4812 int InsetTabular::cellYPos(idx_type const cell) const
4814 row_type row = tabular.cellRow(cell);
4816 for (row_type r = 0; r < row; ++r)
4817 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4818 + tabular.interRowSpace(r + 1);
4823 int InsetTabular::cellXPos(idx_type const cell) const
4825 col_type col = tabular.cellColumn(cell);
4827 for (col_type c = 0; c < col; ++c)
4828 lx += tabular.column_info[c].width;
4833 void InsetTabular::resetPos(Cursor & cur) const
4835 BufferView & bv = cur.bv();
4836 int const maxwidth = bv.workWidth();
4838 int const scx_old = scx_;
4839 int const i = cur.find(this);
4844 int const X2 = maxwidth;
4845 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4846 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4847 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4850 scx_ = X1 + 20 - x1;
4852 scx_ = X2 - 20 - x2;
4857 // only update if offset changed
4858 if (scx_ != scx_old)
4859 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4863 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4865 row_type const row = tabular.cellRow(cur.idx());
4866 col_type const col = tabular.cellColumn(cur.idx());
4868 if (isRightToLeft(cur)) {
4869 if (tabular.cellColumn(cur.idx()) == 0) {
4870 if (row == tabular.nrows() - 1)
4872 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4877 cur.idx() = tabular.getLastCellInRow(row - 1);
4879 cur.idx() = tabular.cellIndex(row, col - 1);
4882 if (tabular.isLastCell(cur.idx()))
4884 if (cur.idx() == tabular.getLastCellInRow(row))
4885 cur.idx() = tabular.cellIndex(row + 1, 0);
4887 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4888 cur.idx() = tabular.cellIndex(row, colnextcell);
4892 cur.boundary(false);
4894 if (cur.selIsMultiCell()) {
4895 cur.pit() = cur.lastpit();
4896 cur.pos() = cur.lastpos();
4904 // in visual mode, place cursor at extreme left or right
4906 switch(entry_from) {
4908 case ENTRY_DIRECTION_RIGHT:
4909 cur.posVisToRowExtremity(false /* !left */);
4911 case ENTRY_DIRECTION_LEFT:
4912 cur.posVisToRowExtremity(true /* left */);
4914 case ENTRY_DIRECTION_IGNORE:
4915 // nothing to do in this case
4919 cur.setCurrentFont();
4924 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4926 row_type const row = tabular.cellRow(cur.idx());
4927 col_type const col = tabular.cellColumn(cur.idx());
4929 if (isRightToLeft(cur)) {
4930 if (cur.idx() == tabular.getLastCellInRow(row)) {
4933 cur.idx() = tabular.getFirstCellInRow(row);
4934 cur.idx() = tabular.cellAbove(cur.idx());
4936 if (tabular.isLastCell(cur.idx()))
4938 if (cur.idx() == tabular.getLastCellInRow(row))
4939 cur.idx() = tabular.cellIndex(row + 1, 0);
4941 cur.idx() = tabular.cellIndex(row, col + 1);
4944 if (cur.idx() == 0) // first cell
4947 cur.idx() = tabular.getLastCellInRow(row - 1);
4949 cur.idx() = tabular.cellIndex(row, col - 1);
4952 if (cur.selIsMultiCell()) {
4953 cur.pit() = cur.lastpit();
4954 cur.pos() = cur.lastpos();
4959 cur.pit() = cur.lastpit();
4960 cur.pos() = cur.lastpos();
4962 // in visual mode, place cursor at extreme left or right
4964 switch(entry_from) {
4966 case ENTRY_DIRECTION_RIGHT:
4967 cur.posVisToRowExtremity(false /* !left */);
4969 case ENTRY_DIRECTION_LEFT:
4970 cur.posVisToRowExtremity(true /* left */);
4972 case ENTRY_DIRECTION_IGNORE:
4973 // nothing to do in this case
4977 cur.setCurrentFont();
4982 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
4984 istringstream is(argument);
4987 if (insetCode(s) != TABULAR_CODE)
4991 size_t safe_guard = 0;
4996 if (safe_guard > 1000) {
4997 LYXERR0("parameter max count reached!");
5001 Tabular::Feature action = Tabular::LAST_ACTION;
5004 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5005 if (s != tabularFeature[i].feature)
5008 action = tabularFeature[i].action;
5011 if (action == Tabular::LAST_ACTION) {
5012 LYXERR0("Feature not found " << s);
5016 if (tabularFeature[i].need_value)
5018 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5019 tabularFeatures(cur, action, val);
5025 static void checkLongtableSpecial(Tabular::ltType & ltt,
5026 string const & special, bool & flag)
5028 if (special == "dl_above") {
5031 } else if (special == "dl_below") {
5032 ltt.bottomDL = flag;
5034 } else if (special == "empty") {
5044 bool InsetTabular::oneCellHasRotationState(bool rotated,
5045 row_type row_start, row_type row_end,
5046 col_type col_start, col_type col_end) const
5048 for (row_type r = row_start; r <= row_end; ++r)
5049 for (col_type c = col_start; c <= col_end; ++c)
5050 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5056 void InsetTabular::tabularFeatures(Cursor & cur,
5057 Tabular::Feature feature, string const & value)
5059 col_type sel_col_start;
5060 col_type sel_col_end;
5061 row_type sel_row_start;
5062 row_type sel_row_end;
5063 bool setLines = false;
5064 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5065 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5069 case Tabular::M_ALIGN_LEFT:
5070 case Tabular::ALIGN_LEFT:
5071 setAlign = LYX_ALIGN_LEFT;
5074 case Tabular::M_ALIGN_RIGHT:
5075 case Tabular::ALIGN_RIGHT:
5076 setAlign = LYX_ALIGN_RIGHT;
5079 case Tabular::M_ALIGN_CENTER:
5080 case Tabular::ALIGN_CENTER:
5081 setAlign = LYX_ALIGN_CENTER;
5084 case Tabular::ALIGN_BLOCK:
5085 setAlign = LYX_ALIGN_BLOCK;
5088 case Tabular::ALIGN_DECIMAL:
5089 setAlign = LYX_ALIGN_DECIMAL;
5092 case Tabular::M_VALIGN_TOP:
5093 case Tabular::VALIGN_TOP:
5094 setVAlign = Tabular::LYX_VALIGN_TOP;
5097 case Tabular::M_VALIGN_BOTTOM:
5098 case Tabular::VALIGN_BOTTOM:
5099 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5102 case Tabular::M_VALIGN_MIDDLE:
5103 case Tabular::VALIGN_MIDDLE:
5104 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5111 cur.recordUndoInset(ATOMIC_UNDO);
5113 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5114 row_type const row = tabular.cellRow(cur.idx());
5115 col_type const column = tabular.cellColumn(cur.idx());
5117 Tabular::ltType ltt;
5121 case Tabular::SET_PWIDTH: {
5122 Length const len(value);
5123 tabular.setColumnPWidth(cur, cur.idx(), len);
5125 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5126 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5127 // check if there is a 1-column multicolumn cell
5128 // if so it must get the same width
5129 for (row_type r = 0; r < tabular.nrows(); ++r) {
5130 if (tabular.isMultiColumn(tabular.cellIndex(r, column))
5131 && tabular.columnSpan(tabular.cellIndex(r, column)) == 1)
5132 tabular.setMColumnPWidth(cur, tabular.cellIndex(r, column), len);
5137 case Tabular::SET_MPWIDTH:
5138 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5139 // if the multicolumn only spans 1 column, the width of the whole column
5140 // must have the same width, see bug #7055
5141 if (tabular.columnSpan(cur.idx()) == 1)
5142 for (row_type r = 0; r < tabular.nrows(); ++r) {
5143 if (!tabular.isMultiColumn(tabular.cellIndex(r, column)))
5144 tabular.setColumnPWidth(cur, tabular.cellIndex(r, column), Length(value));
5145 else if (tabular.isMultiColumn(tabular.cellIndex(r, column))
5146 && tabular.columnSpan(tabular.cellIndex(r, column)) == 1)
5147 tabular.setMColumnPWidth(cur, tabular.cellIndex(r, column), Length(value));
5151 case Tabular::SET_MROFFSET:
5152 tabular.setMROffset(cur, cur.idx(), Length(value));
5155 case Tabular::SET_SPECIAL_COLUMN:
5156 case Tabular::SET_SPECIAL_MULTICOLUMN:
5157 if (value == "none")
5158 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5160 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5163 case Tabular::APPEND_ROW:
5164 // append the row into the tabular
5165 tabular.appendRow(cur.idx());
5168 case Tabular::APPEND_COLUMN:
5169 // append the column into the tabular
5170 tabular.appendColumn(cur.idx());
5171 cur.idx() = tabular.cellIndex(row, column);
5174 case Tabular::DELETE_ROW:
5175 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5176 tabular.deleteRow(sel_row_start);
5177 if (sel_row_start >= tabular.nrows())
5179 cur.idx() = tabular.cellIndex(sel_row_start, column);
5182 cur.setSelection(false);
5185 case Tabular::DELETE_COLUMN:
5186 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5187 tabular.deleteColumn(sel_col_start);
5188 if (sel_col_start >= tabular.ncols())
5190 cur.idx() = tabular.cellIndex(row, sel_col_start);
5193 cur.setSelection(false);
5196 case Tabular::COPY_ROW:
5197 tabular.copyRow(row);
5200 case Tabular::COPY_COLUMN:
5201 tabular.copyColumn(column);
5202 cur.idx() = tabular.cellIndex(row, column);
5205 case Tabular::SET_LINE_TOP:
5206 case Tabular::TOGGLE_LINE_TOP: {
5207 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5208 ? (value == "true") : !tabular.topLine(cur.idx());
5209 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5210 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5211 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5215 case Tabular::SET_LINE_BOTTOM:
5216 case Tabular::TOGGLE_LINE_BOTTOM: {
5217 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5218 ? (value == "true") : !tabular.bottomLine(cur.idx());
5219 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5220 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5221 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5225 case Tabular::SET_LINE_LEFT:
5226 case Tabular::TOGGLE_LINE_LEFT: {
5227 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5228 ? (value == "true") : !tabular.leftLine(cur.idx());
5229 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5230 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5231 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5235 case Tabular::SET_LINE_RIGHT:
5236 case Tabular::TOGGLE_LINE_RIGHT: {
5237 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5238 ? (value == "true") : !tabular.rightLine(cur.idx());
5239 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5240 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5241 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5245 case Tabular::M_ALIGN_LEFT:
5246 case Tabular::M_ALIGN_RIGHT:
5247 case Tabular::M_ALIGN_CENTER:
5248 case Tabular::ALIGN_LEFT:
5249 case Tabular::ALIGN_RIGHT:
5250 case Tabular::ALIGN_CENTER:
5251 case Tabular::ALIGN_BLOCK:
5252 case Tabular::ALIGN_DECIMAL:
5253 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5254 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5255 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5256 !tabular.getPWidth(c).zero());
5259 case Tabular::M_VALIGN_TOP:
5260 case Tabular::M_VALIGN_BOTTOM:
5261 case Tabular::M_VALIGN_MIDDLE:
5263 case Tabular::VALIGN_TOP:
5264 case Tabular::VALIGN_BOTTOM:
5265 case Tabular::VALIGN_MIDDLE:
5266 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5267 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5268 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5271 case Tabular::SET_MULTICOLUMN: {
5272 if (!cur.selection()) {
5273 // just multicol for one single cell
5274 // check whether we are completely in a multicol
5275 if (!tabular.isMultiColumn(cur.idx()))
5276 tabular.setMultiColumn(cur.idx(), 1);
5279 // we have a selection so this means we just add all this
5280 // cells to form a multicolumn cell
5281 idx_type const s_start = cur.selBegin().idx();
5282 row_type const col_start = tabular.cellColumn(s_start);
5283 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5284 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5287 cur.setSelection(false);
5291 case Tabular::UNSET_MULTICOLUMN: {
5292 if (!cur.selection()) {
5293 if (tabular.isMultiColumn(cur.idx()))
5294 tabular.unsetMultiColumn(cur.idx());
5299 case Tabular::MULTICOLUMN: {
5300 if (tabular.isMultiColumn(cur.idx()))
5301 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5303 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5307 case Tabular::SET_MULTIROW: {
5308 if (!cur.selection()) {
5309 // just multirow for one single cell
5310 // check whether we are completely in a multirow
5311 if (!tabular.isMultiRow(cur.idx()))
5312 tabular.setMultiRow(cur.idx(), 1);
5315 // we have a selection so this means we just add all this
5316 // cells to form a multirow cell
5317 idx_type const s_start = cur.selBegin().idx();
5318 row_type const row_start = tabular.cellRow(s_start);
5319 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5320 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5323 cur.setSelection(false);
5327 case Tabular::UNSET_MULTIROW: {
5328 if (!cur.selection()) {
5329 if (tabular.isMultiRow(cur.idx()))
5330 tabular.unsetMultiRow(cur.idx());
5335 case Tabular::MULTIROW: {
5336 if (tabular.isMultiRow(cur.idx()))
5337 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5339 tabularFeatures(cur, Tabular::SET_MULTIROW);
5343 case Tabular::SET_ALL_LINES:
5345 case Tabular::UNSET_ALL_LINES:
5346 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5347 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5348 idx_type const cell = tabular.cellIndex(r, c);
5349 tabular.setTopLine(cell, setLines);
5350 tabular.setBottomLine(cell, setLines);
5351 tabular.setRightLine(cell, setLines);
5352 tabular.setLeftLine(cell, setLines);
5356 case Tabular::SET_BORDER_LINES:
5357 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5358 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5359 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5361 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5362 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5363 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5367 case Tabular::SET_LONGTABULAR:
5368 tabular.is_long_tabular = true;
5371 case Tabular::UNSET_LONGTABULAR:
5372 for (row_type r = 0; r < tabular.nrows(); ++r) {
5373 if (tabular.ltCaption(r)) {
5374 cur.idx() = tabular.cellIndex(r, 0);
5377 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5380 tabular.is_long_tabular = false;
5383 case Tabular::SET_ROTATE_TABULAR:
5384 tabular.rotate = true;
5387 case Tabular::UNSET_ROTATE_TABULAR:
5388 tabular.rotate = false;
5391 case Tabular::TOGGLE_ROTATE_TABULAR:
5392 tabular.rotate = !tabular.rotate;
5395 case Tabular::TABULAR_VALIGN_TOP:
5396 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5399 case Tabular::TABULAR_VALIGN_MIDDLE:
5400 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5403 case Tabular::TABULAR_VALIGN_BOTTOM:
5404 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5407 case Tabular::LONGTABULAR_ALIGN_LEFT:
5408 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5411 case Tabular::LONGTABULAR_ALIGN_CENTER:
5412 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5415 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5416 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5421 case Tabular::SET_ROTATE_CELL:
5422 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5423 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5424 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5427 case Tabular::UNSET_ROTATE_CELL:
5428 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5429 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5430 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5433 case Tabular::TOGGLE_ROTATE_CELL:
5435 bool oneNotRotated = oneCellHasRotationState(false,
5436 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5438 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5439 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5440 tabular.setRotateCell(tabular.cellIndex(r, c),
5445 case Tabular::SET_USEBOX: {
5446 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5447 if (val == tabular.getUsebox(cur.idx()))
5448 val = Tabular::BOX_NONE;
5449 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5450 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5451 tabular.setUsebox(tabular.cellIndex(r, c), val);
5455 case Tabular::UNSET_LTFIRSTHEAD:
5457 case Tabular::SET_LTFIRSTHEAD:
5458 tabular.getRowOfLTFirstHead(row, ltt);
5459 checkLongtableSpecial(ltt, value, flag);
5460 tabular.setLTHead(row, flag, ltt, true);
5463 case Tabular::UNSET_LTHEAD:
5465 case Tabular::SET_LTHEAD:
5466 tabular.getRowOfLTHead(row, ltt);
5467 checkLongtableSpecial(ltt, value, flag);
5468 tabular.setLTHead(row, flag, ltt, false);
5471 case Tabular::UNSET_LTFOOT:
5473 case Tabular::SET_LTFOOT:
5474 tabular.getRowOfLTFoot(row, ltt);
5475 checkLongtableSpecial(ltt, value, flag);
5476 tabular.setLTFoot(row, flag, ltt, false);
5479 case Tabular::UNSET_LTLASTFOOT:
5481 case Tabular::SET_LTLASTFOOT:
5482 tabular.getRowOfLTLastFoot(row, ltt);
5483 checkLongtableSpecial(ltt, value, flag);
5484 tabular.setLTFoot(row, flag, ltt, true);
5487 case Tabular::SET_LTNEWPAGE:
5488 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5491 case Tabular::SET_LTCAPTION: {
5492 if (tabular.ltCaption(row))
5494 cur.idx() = tabular.setLTCaption(row, true);
5497 cur.setSelection(false);
5498 // If a row is set as caption, then also insert
5499 // a caption. Otherwise the LaTeX output is broken.
5500 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5501 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5505 case Tabular::UNSET_LTCAPTION: {
5506 if (!tabular.ltCaption(row))
5508 cur.idx() = tabular.setLTCaption(row, false);
5511 cur.setSelection(false);
5512 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5513 if (lyx::getStatus(fr).enabled())
5518 case Tabular::TOGGLE_LTCAPTION: {
5519 if (tabular.ltCaption(row))
5520 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5522 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5526 case Tabular::SET_BOOKTABS:
5527 tabular.use_booktabs = true;
5530 case Tabular::UNSET_BOOKTABS:
5531 tabular.use_booktabs = false;
5534 case Tabular::SET_TOP_SPACE: {
5536 if (value == "default")
5537 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5538 tabular.row_info[r].top_space_default = true;
5539 else if (value == "none")
5540 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5541 tabular.row_info[r].top_space_default = false;
5542 tabular.row_info[r].top_space = len;
5544 else if (isValidLength(value, &len))
5545 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5546 tabular.row_info[r].top_space_default = false;
5547 tabular.row_info[r].top_space = len;
5552 case Tabular::SET_BOTTOM_SPACE: {
5554 if (value == "default")
5555 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5556 tabular.row_info[r].bottom_space_default = true;
5557 else if (value == "none")
5558 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5559 tabular.row_info[r].bottom_space_default = false;
5560 tabular.row_info[r].bottom_space = len;
5562 else if (isValidLength(value, &len))
5563 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5564 tabular.row_info[r].bottom_space_default = false;
5565 tabular.row_info[r].bottom_space = len;
5570 case Tabular::SET_INTERLINE_SPACE: {
5572 if (value == "default")
5573 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5574 tabular.row_info[r].interline_space_default = true;
5575 else if (value == "none")
5576 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5577 tabular.row_info[r].interline_space_default = false;
5578 tabular.row_info[r].interline_space = len;
5580 else if (isValidLength(value, &len))
5581 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5582 tabular.row_info[r].interline_space_default = false;
5583 tabular.row_info[r].interline_space = len;
5588 case Tabular::SET_DECIMAL_POINT:
5589 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5590 tabular.column_info[c].decimal_point = from_utf8(value);
5593 // dummy stuff just to avoid warnings
5594 case Tabular::LAST_ACTION:
5600 bool InsetTabular::copySelection(Cursor & cur)
5602 if (!cur.selection())
5607 getSelection(cur, rs, re, cs, ce);
5609 paste_tabular.reset(new Tabular(tabular));
5611 for (row_type r = 0; r < rs; ++r)
5612 paste_tabular->deleteRow(0);
5614 row_type const rows = re - rs + 1;
5615 while (paste_tabular->nrows() > rows)
5616 paste_tabular->deleteRow(rows);
5618 for (col_type c = 0; c < cs; ++c)
5619 paste_tabular->deleteColumn(0);
5621 col_type const columns = ce - cs + 1;
5622 while (paste_tabular->ncols() > columns)
5623 paste_tabular->deleteColumn(columns);
5625 paste_tabular->setBuffer(tabular.buffer());
5627 odocstringstream os;
5628 OutputParams const runparams(0);
5629 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5630 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5631 cap::copySelection(cur, os.str());
5633 // mark tabular stack dirty
5634 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5635 // when we (hopefully) have a one-for-all paste mechanism.
5636 // This must be called after cap::copySelection.
5637 dirtyTabularStack(true);
5643 bool InsetTabular::pasteClipboard(Cursor & cur)
5647 col_type actcol = tabular.cellColumn(cur.idx());
5648 row_type actrow = tabular.cellRow(cur.idx());
5650 if (cur.selIsMultiCell()) {
5653 getSelection(cur, actrow, re, actcol, ce);
5656 for (row_type r1 = 0, r2 = actrow;
5657 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5659 for (col_type c1 = 0, c2 = actcol;
5660 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5662 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5663 tabular.isPartOfMultiColumn(r2, c2))
5665 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5669 if (tabular.isPartOfMultiColumn(r2, c2)) {
5673 shared_ptr<InsetTableCell> inset(
5674 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5675 tabular.setCellInset(r2, c2, inset);
5676 // FIXME?: why do we need to do this explicitly? (EL)
5677 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5679 // FIXME: change tracking (MG)
5680 inset->setChange(Change(buffer().params().trackChanges ?
5681 Change::INSERTED : Change::UNCHANGED));
5689 void InsetTabular::cutSelection(Cursor & cur)
5691 if (!cur.selection())
5696 getSelection(cur, rs, re, cs, ce);
5697 for (row_type r = rs; r <= re; ++r) {
5698 for (col_type c = cs; c <= ce; ++c) {
5699 shared_ptr<InsetTableCell> t
5700 = cell(tabular.cellIndex(r, c));
5701 if (buffer().params().trackChanges)
5702 // FIXME: Change tracking (MG)
5703 t->setChange(Change(Change::DELETED));
5709 // cursor position might be invalid now
5710 if (cur.pit() > cur.lastpit())
5711 cur.pit() = cur.lastpit();
5712 if (cur.pos() > cur.lastpos())
5713 cur.pos() = cur.lastpos();
5714 cur.clearSelection();
5718 bool InsetTabular::isRightToLeft(Cursor & cur) const
5720 LASSERT(cur.depth() > 1, /**/);
5721 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5722 pos_type const parentpos = cur[cur.depth() - 2].pos();
5723 return parentpar.getFontSettings(buffer().params(),
5724 parentpos).language()->rightToLeft();
5728 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5731 LASSERT(stidx <= enidx, return docstring());
5733 col_type const col1 = tabular.cellColumn(stidx);
5734 col_type const col2 = tabular.cellColumn(enidx);
5735 row_type const row1 = tabular.cellRow(stidx);
5736 row_type const row2 = tabular.cellRow(enidx);
5738 for (col_type col = col1; col <= col2; col++)
5739 for (row_type row = row1; row <= row2; row++) {
5744 retval += tabular.cellInset(row, col)->asString(intoInsets);
5750 void InsetTabular::getSelection(Cursor & cur,
5751 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5753 CursorSlice const & beg = cur.selBegin();
5754 CursorSlice const & end = cur.selEnd();
5755 cs = tabular.cellColumn(beg.idx());
5756 ce = tabular.cellColumn(end.idx());
5760 rs = tabular.cellRow(beg.idx());
5761 re = tabular.cellRow(end.idx());
5767 Text * InsetTabular::getText(int idx) const
5769 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5773 void InsetTabular::setChange(Change const & change)
5775 for (idx_type idx = 0; idx < nargs(); ++idx)
5776 cell(idx)->setChange(change);
5780 void InsetTabular::acceptChanges()
5782 for (idx_type idx = 0; idx < nargs(); ++idx)
5783 cell(idx)->acceptChanges();
5787 void InsetTabular::rejectChanges()
5789 for (idx_type idx = 0; idx < nargs(); ++idx)
5790 cell(idx)->rejectChanges();
5794 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5796 return tabular.getPWidth(cell).zero();
5800 bool InsetTabular::forcePlainLayout(idx_type cell) const
5802 return !tabular.getPWidth(cell).zero();
5806 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5809 if (buf.length() <= 0)
5814 col_type maxCols = 1;
5815 size_t const len = buf.length();
5819 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5827 maxCols = max(cols, maxCols);
5833 maxCols = max(cols, maxCols);
5839 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5840 loctab = paste_tabular.get();
5842 dirtyTabularStack(true);
5845 cell = bv.cursor().idx();
5846 ocol = tabular.cellColumn(cell);
5847 row = tabular.cellRow(cell);
5851 idx_type const cells = loctab->numberofcells;
5854 rows = loctab->nrows();
5855 col_type const columns = loctab->ncols();
5857 while (cell < cells && p < len && row < rows &&
5858 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5864 // we can only set this if we are not too far right
5865 if (cols < columns) {
5866 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5867 Font const font = bv.textMetrics(&inset->text()).
5869 inset->setText(buf.substr(op, p - op), font,
5870 buffer().params().trackChanges);
5876 // we can only set this if we are not too far right
5877 if (cols < columns) {
5878 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5879 Font const font = bv.textMetrics(&inset->text()).
5881 inset->setText(buf.substr(op, p - op), font,
5882 buffer().params().trackChanges);
5887 cell = loctab->cellIndex(row, cols);
5893 // check for the last cell if there is no trailing '\n'
5894 if (cell < cells && op < len) {
5895 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5896 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5897 inset->setText(buf.substr(op, len - op), font,
5898 buffer().params().trackChanges);
5904 void InsetTabular::addPreview(DocIterator const & inset_pos,
5905 PreviewLoader & loader) const
5907 DocIterator cell_pos = inset_pos;
5909 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5910 for (row_type r = 0; r < tabular.nrows(); ++r) {
5911 for (col_type c = 0; c < tabular.ncols(); ++c) {
5912 cell_pos.top().idx() = tabular.cellIndex(r, c);
5913 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
5919 bool InsetTabular::completionSupported(Cursor const & cur) const
5921 Cursor const & bvCur = cur.bv().cursor();
5922 if (&bvCur.inset() != this)
5924 return cur.text()->completionSupported(cur);
5928 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5930 return completionSupported(cur);
5934 bool InsetTabular::automaticInlineCompletion() const
5936 return lyxrc.completion_inline_text;
5940 bool InsetTabular::automaticPopupCompletion() const
5942 return lyxrc.completion_popup_text;
5946 bool InsetTabular::showCompletionCursor() const
5948 return lyxrc.completion_cursor_text;
5952 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5954 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5958 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5960 if (!completionSupported(cur))
5962 return cur.text()->completionPrefix(cur);
5966 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5968 if (!completionSupported(cur))
5971 return cur.text()->insertCompletion(cur, s, finished);
5975 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5976 Dimension & dim) const
5978 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5979 tm.completionPosAndDim(cur, x, y, dim);
5983 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5985 istringstream data(in);
5987 lex.setStream(data);
5994 if (!lex || token != "tabular") {
5995 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5999 // This is part of the inset proper that is usually swallowed
6000 // by Buffer::readInset
6002 if (!lex || token != "Tabular") {
6003 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6011 string InsetTabular::params2string(InsetTabular const & inset)
6014 data << "tabular" << ' ';
6016 data << "\\end_inset\n";