2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * \author Edwin Leuven
17 * Full author contact details are available in file CREDITS.
22 #include "InsetTabular.h"
24 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "FuncRequest.h"
34 #include "FuncStatus.h"
36 #include "LaTeXFeatures.h"
40 #include "MetricsInfo.h"
41 #include "OutputParams.h"
42 #include "output_xhtml.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/alert.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/convert.h"
56 #include "support/debug.h"
57 #include "support/docstream.h"
58 #include "support/FileName.h"
59 #include "support/gettext.h"
60 #include "support/lassert.h"
61 #include "support/lstrings.h"
63 #include <boost/scoped_ptr.hpp>
71 using namespace lyx::support;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horizontal space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 // the SET/UNSET actions are used by the table dialog,
110 // the TOGGLE actions by the table toolbar buttons
111 { Tabular::APPEND_ROW, "append-row", false },
112 { Tabular::APPEND_COLUMN, "append-column", false },
113 { Tabular::DELETE_ROW, "delete-row", false },
114 { Tabular::DELETE_COLUMN, "delete-column", false },
115 { Tabular::COPY_ROW, "copy-row", false },
116 { Tabular::COPY_COLUMN, "copy-column", false },
117 { Tabular::SET_LINE_TOP, "set-line-top", true },
118 { Tabular::SET_LINE_BOTTOM, "set-line-bottom", true },
119 { Tabular::SET_LINE_LEFT, "set-line-left", true },
120 { Tabular::SET_LINE_RIGHT, "set-line-right", true },
121 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top", false },
122 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom", false },
123 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left", false },
124 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right", false },
125 { Tabular::ALIGN_LEFT, "align-left", false },
126 { Tabular::ALIGN_RIGHT, "align-right", false },
127 { Tabular::ALIGN_CENTER, "align-center", false },
128 { Tabular::ALIGN_BLOCK, "align-block", false },
129 { Tabular::ALIGN_DECIMAL, "align-decimal", false },
130 { Tabular::VALIGN_TOP, "valign-top", false },
131 { Tabular::VALIGN_BOTTOM, "valign-bottom", false },
132 { Tabular::VALIGN_MIDDLE, "valign-middle", false },
133 { Tabular::M_ALIGN_LEFT, "m-align-left", false },
134 { Tabular::M_ALIGN_RIGHT, "m-align-right", false },
135 { Tabular::M_ALIGN_CENTER, "m-align-center", false },
136 { Tabular::M_VALIGN_TOP, "m-valign-top", false },
137 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom", false },
138 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle", false },
139 { Tabular::MULTICOLUMN, "multicolumn", false },
140 { Tabular::SET_MULTICOLUMN, "set-multicolumn", false },
141 { Tabular::UNSET_MULTICOLUMN, "unset-multicolumn", false },
142 { Tabular::MULTIROW, "multirow", false },
143 { Tabular::SET_MULTIROW, "set-multirow", false },
144 { Tabular::UNSET_MULTIROW, "unset-multirow", false },
145 { Tabular::SET_MROFFSET, "set-mroffset", true },
146 { Tabular::SET_ALL_LINES, "set-all-lines", false },
147 { Tabular::UNSET_ALL_LINES, "unset-all-lines", false },
148 { Tabular::SET_LONGTABULAR, "set-longtabular", false },
149 { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
150 { Tabular::SET_PWIDTH, "set-pwidth", true },
151 { Tabular::SET_MPWIDTH, "set-mpwidth", true },
152 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
153 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
154 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
155 { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
156 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
157 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
158 { Tabular::SET_USEBOX, "set-usebox", true },
159 { Tabular::SET_LTHEAD, "set-lthead", true },
160 { Tabular::UNSET_LTHEAD, "unset-lthead", true },
161 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead", true },
162 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead", true },
163 { Tabular::SET_LTFOOT, "set-ltfoot", true },
164 { Tabular::UNSET_LTFOOT, "unset-ltfoot", true },
165 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
166 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
167 { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
168 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
169 { Tabular::SET_LTCAPTION, "set-ltcaption", false },
170 { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
171 { Tabular::SET_SPECIAL_COLUMN, "set-special-column", true },
172 { Tabular::SET_SPECIAL_MULTICOLUMN, "set-special-multicolumn", true },
173 { Tabular::SET_BOOKTABS, "set-booktabs", false },
174 { Tabular::UNSET_BOOKTABS, "unset-booktabs", false },
175 { Tabular::SET_TOP_SPACE, "set-top-space", true },
176 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space", true },
177 { Tabular::SET_INTERLINE_SPACE, "set-interline-space", true },
178 { Tabular::SET_BORDER_LINES, "set-border-lines", false },
179 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top", false},
180 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle", false},
181 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom", false},
182 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left", false },
183 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center", false },
184 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right", false },
185 { Tabular::SET_DECIMAL_POINT, "set-decimal-point", true },
186 { Tabular::SET_TABULAR_WIDTH, "set-tabular-width", true },
187 { Tabular::LAST_ACTION, "", false }
192 string const write_attribute(string const & name, T const & t)
194 string const s = tostr(t);
195 return s.empty() ? s : " " + name + "=\"" + s + "\"";
199 string const write_attribute(string const & name, string const & t)
201 return t.empty() ? t : " " + name + "=\"" + t + "\"";
206 string const write_attribute(string const & name, docstring const & t)
208 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
213 string const write_attribute(string const & name, bool const & b)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return b ? write_attribute(name, convert<string>(b)) : string();
222 string const write_attribute(string const & name, int const & i)
224 // we write only true attribute values so we remove a bit of the
225 // file format bloat for tabulars.
226 return i ? write_attribute(name, convert<string>(i)) : string();
231 string const write_attribute(string const & name, Tabular::idx_type const & i)
233 // we write only true attribute values so we remove a bit of the
234 // file format bloat for tabulars.
235 return i ? write_attribute(name, convert<string>(i)) : string();
240 string const write_attribute(string const & name, Length const & value)
242 // we write only the value if we really have one same reson as above.
243 return value.zero() ? string() : write_attribute(name, value.asString());
247 string const tostr(LyXAlignment const & num)
252 case LYX_ALIGN_BLOCK:
256 case LYX_ALIGN_CENTER:
258 case LYX_ALIGN_RIGHT:
260 case LYX_ALIGN_LAYOUT:
262 case LYX_ALIGN_SPECIAL:
264 case LYX_ALIGN_DECIMAL:
271 string const tostr(Tabular::HAlignment const & num)
274 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
276 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
278 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
285 string const tostr(Tabular::VAlignment const & num)
288 case Tabular::LYX_VALIGN_TOP:
290 case Tabular::LYX_VALIGN_MIDDLE:
292 case Tabular::LYX_VALIGN_BOTTOM:
299 string const tostr(Tabular::BoxType const & num)
302 case Tabular::BOX_NONE:
304 case Tabular::BOX_PARBOX:
306 case Tabular::BOX_MINIPAGE:
313 // I would have liked a fromstr template a lot better. (Lgb)
314 bool string2type(string const str, LyXAlignment & num)
317 num = LYX_ALIGN_NONE;
318 else if (str == "block")
319 num = LYX_ALIGN_BLOCK;
320 else if (str == "left")
321 num = LYX_ALIGN_LEFT;
322 else if (str == "center")
323 num = LYX_ALIGN_CENTER;
324 else if (str == "right")
325 num = LYX_ALIGN_RIGHT;
326 else if (str == "decimal")
327 num = LYX_ALIGN_DECIMAL;
334 bool string2type(string const str, Tabular::HAlignment & num)
337 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
338 else if (str == "center" )
339 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
340 else if (str == "right")
341 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
348 bool string2type(string const str, Tabular::VAlignment & num)
351 num = Tabular::LYX_VALIGN_TOP;
352 else if (str == "middle" )
353 num = Tabular::LYX_VALIGN_MIDDLE;
354 else if (str == "bottom")
355 num = Tabular::LYX_VALIGN_BOTTOM;
362 bool string2type(string const str, Tabular::BoxType & num)
365 num = Tabular::BOX_NONE;
366 else if (str == "parbox")
367 num = Tabular::BOX_PARBOX;
368 else if (str == "minipage")
369 num = Tabular::BOX_MINIPAGE;
376 bool string2type(string const str, bool & num)
380 else if (str == "false")
388 bool getTokenValue(string const & str, char const * token, string & ret)
391 size_t token_length = strlen(token);
392 size_t pos = str.find(token);
394 if (pos == string::npos || pos + token_length + 1 >= str.length()
395 || str[pos + token_length] != '=')
397 pos += token_length + 1;
399 if (ch != '"' && ch != '\'') { // only read till next space
403 while (pos < str.length() - 1 && str[++pos] != ch)
410 bool getTokenValue(string const & str, char const * token, docstring & ret)
413 bool const success = getTokenValue(str, token, tmp);
414 ret = from_utf8(tmp);
419 bool getTokenValue(string const & str, char const * token, int & num)
423 if (!getTokenValue(str, token, tmp))
425 num = convert<int>(tmp);
430 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
433 return getTokenValue(str, token, tmp) && string2type(tmp, num);
437 bool getTokenValue(string const & str, char const * token,
438 Tabular::HAlignment & num)
441 return getTokenValue(str, token, tmp) && string2type(tmp, num);
445 bool getTokenValue(string const & str, char const * token,
446 Tabular::VAlignment & num)
449 return getTokenValue(str, token, tmp) && string2type(tmp, num);
453 bool getTokenValue(string const & str, char const * token,
454 Tabular::BoxType & num)
457 return getTokenValue(str, token, tmp) && string2type(tmp, num);
461 bool getTokenValue(string const & str, char const * token, bool & flag)
463 // set the flag always to false as this should be the default for bools
464 // not in the file-format.
467 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
471 bool getTokenValue(string const & str, char const * token, Length & len)
473 // set the length to be zero() as default as this it should be if not
474 // in the file format.
477 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
481 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
486 if (!getTokenValue(str, token, tmp))
488 if (tmp == "default") {
492 return isValidLength(tmp, &len);
496 void l_getline(istream & is, string & str)
499 while (str.empty()) {
501 if (!str.empty() && str[str.length() - 1] == '\r')
502 str.erase(str.length() - 1);
509 string const featureAsString(Tabular::Feature action)
511 for (size_t i = 0; i != Tabular::LAST_ACTION; ++i) {
512 if (tabularFeature[i].action == action)
513 return tabularFeature[i].feature;
519 DocIterator separatorPos(InsetTableCell * cell, docstring const & align_d)
521 DocIterator dit = doc_iterator_begin(&(cell->buffer()), cell);
522 for (; dit; dit.forwardChar())
523 if (dit.inTexted() && dit.depth() == 1
524 && dit.paragraph().find(align_d, false, false, dit.pos()))
531 InsetTableCell splitCell(InsetTableCell & head, docstring const align_d, bool & hassep)
533 InsetTableCell tail = InsetTableCell(head);
534 DocIterator const dit = separatorPos(&head, align_d);
537 pit_type const psize = head.paragraphs().front().size();
538 head.paragraphs().front().eraseChars(dit.pos(), psize, false);
539 tail.paragraphs().front().eraseChars(0,
540 dit.pos() < psize ? dit.pos() + 1 : psize, false);
547 /////////////////////////////////////////////////////////////////////
551 /////////////////////////////////////////////////////////////////////
554 Tabular::CellData::CellData(Buffer * buf)
557 multicolumn(Tabular::CELL_NORMAL),
558 multirow(Tabular::CELL_NORMAL),
559 alignment(LYX_ALIGN_CENTER),
560 valignment(LYX_VALIGN_TOP),
570 inset(new InsetTableCell(buf))
572 inset->setBuffer(*buf);
576 Tabular::CellData::CellData(CellData const & cs)
579 multicolumn(cs.multicolumn),
580 multirow(cs.multirow),
581 mroffset(cs.mroffset),
582 alignment(cs.alignment),
583 valignment(cs.valignment),
584 decimal_hoffset(cs.decimal_hoffset),
585 decimal_width(cs.decimal_width),
587 top_line(cs.top_line),
588 bottom_line(cs.bottom_line),
589 left_line(cs.left_line),
590 right_line(cs.right_line),
593 align_special(cs.align_special),
595 inset(static_cast<InsetTableCell *>(cs.inset->clone()))
599 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
605 void Tabular::CellData::swap(CellData & rhs)
607 std::swap(cellno, rhs.cellno);
608 std::swap(width, rhs.width);
609 std::swap(multicolumn, rhs.multicolumn);
610 std::swap(multirow, rhs.multirow);
611 std::swap(mroffset, rhs.mroffset);
612 std::swap(alignment, rhs.alignment);
613 std::swap(valignment, rhs.valignment);
614 std::swap(decimal_hoffset, rhs.decimal_hoffset);
615 std::swap(decimal_width, rhs.decimal_width);
616 std::swap(voffset, rhs.voffset);
617 std::swap(top_line, rhs.top_line);
618 std::swap(bottom_line, rhs.bottom_line);
619 std::swap(left_line, rhs.left_line);
620 std::swap(right_line, rhs.right_line);
621 std::swap(usebox, rhs.usebox);
622 std::swap(rotate, rhs.rotate);
623 std::swap(align_special, rhs.align_special);
624 p_width.swap(rhs.p_width);
625 inset.swap(rhs.inset);
629 Tabular::RowData::RowData()
632 top_space_default(false),
633 bottom_space_default(false),
634 interline_space_default(false),
644 Tabular::ColumnData::ColumnData()
645 : alignment(LYX_ALIGN_CENTER),
646 valignment(LYX_VALIGN_TOP),
652 Tabular::ltType::ltType()
659 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
661 init(buffer, rows_arg, columns_arg);
665 void Tabular::setBuffer(Buffer & buffer)
668 for (row_type i = 0; i < nrows(); ++i)
669 for (col_type j = 0; j < ncols(); ++j)
670 cell_info[i][j].inset->setBuffer(*buffer_);
674 // activates all lines and sets all widths to 0
675 void Tabular::init(Buffer * buf, row_type rows_arg,
676 col_type columns_arg)
679 row_info = row_vector(rows_arg);
680 column_info = column_vector(columns_arg);
681 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
682 row_info.reserve(10);
683 column_info.reserve(10);
684 cell_info.reserve(100);
686 is_long_tabular = false;
687 tabular_valignment = LYX_VALIGN_MIDDLE;
688 tabular_width = Length();
689 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
691 use_booktabs = false;
692 // set silly default lines
693 for (row_type r = 0; r < nrows(); ++r)
694 for (col_type c = 0; c < ncols(); ++c) {
695 cell_info[r][c].inset->setBuffer(*buffer_);
696 cell_info[r][c].top_line = true;
697 cell_info[r][c].left_line = true;
698 cell_info[r][c].bottom_line = r == 0 || r == nrows() - 1;
699 cell_info[r][c].right_line = c == ncols() - 1;
704 void Tabular::appendRow(idx_type const cell)
706 row_type const row = cellRow(cell);
708 row_info.insert(row_info.begin() + row + 1, RowData());
709 row_info[row + 1] = row_info[row];
711 cell_info.insert(cell_info.begin() + row + 1,
712 cell_vector(ncols(), CellData(buffer_)));
713 for (col_type c = 0; c < ncols(); ++c) {
714 if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
715 cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
717 cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
721 for (col_type c = 0; c < ncols(); ++c) {
722 if (isPartOfMultiRow(row, c))
724 // inherit line settings
725 idx_type const i = cellIndex(row + 1, c);
726 idx_type const j = cellIndex(row, c);
727 setLeftLine(i, leftLine(j));
728 setRightLine(i, rightLine(j));
729 setTopLine(i, topLine(j));
730 if (topLine(j) && bottomLine(j)) {
731 setBottomLine(i, true);
732 setBottomLine(j, false);
734 // mark track changes
735 if (buffer().params().trackChanges)
736 cellInfo(i).inset->setChange(Change(Change::INSERTED));
741 void Tabular::deleteRow(row_type const row)
743 // Not allowed to delete last row
747 for (col_type c = 0; c < ncols(); ++c) {
748 // Care about multirow cells
749 if (row + 1 < nrows() &&
750 cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW &&
751 cell_info[row + 1][c].multirow == CELL_PART_OF_MULTIROW) {
752 cell_info[row + 1][c].multirow = CELL_BEGIN_OF_MULTIROW;
755 row_info.erase(row_info.begin() + row);
756 cell_info.erase(cell_info.begin() + row);
761 void Tabular::copyRow(row_type const row)
763 row_info.insert(row_info.begin() + row, row_info[row]);
764 cell_info.insert(cell_info.begin() + row, cell_info[row]);
766 if (buffer().params().trackChanges)
767 for (col_type c = 0; c < ncols(); ++c)
768 cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
774 void Tabular::appendColumn(idx_type const cell)
776 col_type const c = cellColumn(cell);
778 column_info.insert(column_info.begin() + c + 1, ColumnData());
779 column_info[c + 1] = column_info[c];
781 for (row_type r = 0; r < nrows(); ++r) {
782 cell_info[r].insert(cell_info[r].begin() + c + 1,
784 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
785 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
787 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
790 for (row_type r = 0; r < nrows(); ++r) {
791 // inherit line settings
792 idx_type const i = cellIndex(r, c + 1);
793 idx_type const j = cellIndex(r, c);
794 setBottomLine(i, bottomLine(j));
795 setTopLine(i, topLine(j));
796 setLeftLine(i, leftLine(j));
797 if (rightLine(j) && rightLine(j)) {
798 setRightLine(i, true);
799 setRightLine(j, false);
801 if (buffer().params().trackChanges)
802 cellInfo(i).inset->setChange(Change(Change::INSERTED));
807 void Tabular::deleteColumn(col_type const col)
809 // Not allowed to delete last column
813 for (row_type r = 0; r < nrows(); ++r) {
814 // Care about multicolumn cells
815 if (col + 1 < ncols() &&
816 cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
817 cell_info[r][col + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
818 cell_info[r][col + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
820 cell_info[r].erase(cell_info[r].begin() + col);
822 column_info.erase(column_info.begin() + col);
827 void Tabular::copyColumn(col_type const col)
829 BufferParams const & bp = buffer().params();
830 column_info.insert(column_info.begin() + col, column_info[col]);
832 for (row_type r = 0; r < nrows(); ++r) {
833 cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
835 cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
841 void Tabular::updateIndexes()
846 for (row_type row = 0; row < nrows(); ++row)
847 for (col_type column = 0; column < ncols(); ++column) {
848 if (!isPartOfMultiColumn(row, column)
849 && !isPartOfMultiRow(row, column))
851 if (isPartOfMultiRow(row, column))
852 cell_info[row][column].cellno = cell_info[row - 1][column].cellno;
854 cell_info[row][column].cellno = numberofcells - 1;
857 rowofcell.resize(numberofcells);
858 columnofcell.resize(numberofcells);
860 // reset column and row of cells and update their width and alignment
861 for (row_type row = 0; row < nrows(); ++row)
862 for (col_type column = 0; column < ncols(); ++column) {
863 if (isPartOfMultiColumn(row, column))
865 // columnofcell needs to be called before setting width and aligment
866 // multirow cells inherit the width from the column width
867 if (!isPartOfMultiRow(row, column)) {
868 columnofcell[i] = column;
871 setFixedWidth(row, column);
872 if (isPartOfMultiRow(row, column))
874 cell_info[row][column].inset->setContentAlignment(
875 getAlignment(cellIndex(row, column)));
881 Tabular::idx_type Tabular::numberOfCellsInRow(row_type const row) const
884 for (col_type c = 0; c < ncols(); ++c)
885 if (cell_info[row][c].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
891 bool Tabular::topLine(idx_type const cell) const
893 return cellInfo(cell).top_line;
897 bool Tabular::bottomLine(idx_type const cell) const
899 return cellInfo(cell).bottom_line;
903 bool Tabular::leftLine(idx_type cell) const
907 return cellInfo(cell).left_line;
911 bool Tabular::rightLine(idx_type cell) const
915 return cellInfo(cell).right_line;
919 int Tabular::interRowSpace(row_type row) const
921 if (!row || row >= nrows())
924 int const interline_space = row_info[row - 1].interline_space_default ?
926 row_info[row - 1].interline_space.inPixels(width());
927 if (rowTopLine(row) && rowBottomLine(row - 1))
928 return interline_space + WIDTH_OF_LINE;
929 return interline_space;
933 int Tabular::interColumnSpace(idx_type cell) const
935 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
936 if (rightLine(cell) && nextcol < ncols()
937 && leftLine(cellIndex(cellRow(cell), nextcol)))
938 return WIDTH_OF_LINE;
943 int Tabular::cellWidth(idx_type cell) const
946 col_type const span = columnSpan(cell);
947 col_type const col = cellColumn(cell);
948 for(col_type c = col; c < col + span ; ++c)
949 w += column_info[c].width;
954 int Tabular::cellHeight(idx_type cell) const
956 row_type const span = rowSpan(cell);
957 row_type const row = cellRow(cell);
959 for(row_type r = row; r < row + span ; ++r) {
960 h += rowAscent(r) + rowDescent(r);
961 if (r != row + span - 1)
962 h += interRowSpace(r + 1);
969 bool Tabular::updateColumnWidths()
971 vector<int> max_dwidth(ncols(), 0);
972 for(col_type c = 0; c < ncols(); ++c)
973 for(row_type r = 0; r < nrows(); ++r) {
974 idx_type const i = cellIndex(r, c);
975 if (getAlignment(i) == LYX_ALIGN_DECIMAL)
976 max_dwidth[c] = max(max_dwidth[c], cell_info[r][c].decimal_width);
980 // for each col get max of single col cells
981 for(col_type c = 0; c < ncols(); ++c) {
983 for(row_type r = 0; r < nrows(); ++r) {
984 idx_type const i = cellIndex(r, c);
985 if (columnSpan(i) == 1) {
986 if (getAlignment(i) == LYX_ALIGN_DECIMAL
987 && cell_info[r][c].decimal_width!=0)
988 new_width = max(new_width, cellInfo(i).width
989 + max_dwidth[c] - cellInfo(i).decimal_width);
991 new_width = max(new_width, cellInfo(i).width);
995 if (column_info[c].width != new_width) {
996 column_info[c].width = new_width;
1000 // update col widths to fit merged cells
1001 for(col_type c = 0; c < ncols(); ++c)
1002 for(row_type r = 0; r < nrows(); ++r) {
1003 idx_type const i = cellIndex(r, c);
1004 int const span = columnSpan(i);
1005 if (span == 1 || c > cellColumn(i))
1009 for(col_type j = c; j < c + span ; ++j)
1010 old_width += column_info[j].width;
1012 if (cellInfo(i).width > old_width) {
1013 column_info[c + span - 1].width += cellInfo(i).width - old_width;
1022 int Tabular::width() const
1025 for (col_type c = 0; c < ncols(); ++c)
1026 width += column_info[c].width;
1031 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
1034 col_type const col = cellColumn(cell);
1035 // set alignment for the whole row if we are not in a multicolumn cell,
1036 // exclude possible multicolumn cells in the row
1037 if (!isMultiColumn(cell)) {
1038 for (row_type r = 0; r < nrows(); ++r) {
1039 // only if the column has no width the multirow inherits the
1040 // alignment of the column, otherwise it is left aligned
1041 if (!(isMultiRow(cellIndex(r, col)) && has_width)
1042 && !isMultiColumn(cellIndex(r, col))) {
1043 cell_info[r][col].alignment = align;
1044 cell_info[r][col].inset->setContentAlignment(align);
1046 if ((isMultiRow(cellIndex(r, col)) && has_width)
1047 && !isMultiColumn(cellIndex(r, col))) {
1048 cell_info[r][col].alignment = LYX_ALIGN_LEFT;
1049 cell_info[r][col].inset->setContentAlignment(LYX_ALIGN_LEFT);
1052 column_info[col].alignment = align;
1053 docstring & dpoint = column_info[col].decimal_point;
1054 if (align == LYX_ALIGN_DECIMAL && dpoint.empty())
1055 dpoint = from_utf8(lyxrc.default_decimal_point);
1057 cellInfo(cell).alignment = align;
1058 cellInset(cell).get()->setContentAlignment(align);
1063 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1066 if (!isMultiColumn(cell) || onlycolumn)
1067 column_info[cellColumn(cell)].valignment = align;
1069 cellInfo(cell).valignment = align;
1076 * Allow line and paragraph breaks for fixed width cells or disallow them,
1077 * merge cell paragraphs and reset layout to standard for variable width
1080 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1082 inset->setAutoBreakRows(fixedWidth);
1083 inset->toggleFixedWidth(fixedWidth);
1087 // merge all paragraphs to one
1088 BufferParams const & bp = cur.bv().buffer().params();
1089 while (inset->paragraphs().size() > 1)
1090 mergeParagraph(bp, inset->paragraphs(), 0);
1094 // undo information has already been recorded
1095 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1096 bp.documentClass().plainLayoutName());
1103 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1104 Length const & width)
1106 col_type const c = cellColumn(cell);
1108 column_info[c].p_width = width;
1109 // reset the vertical alignment to top if the fixed with
1110 // is removed or zero because only fixed width columns can
1111 // have a vertical alignment
1112 if (column_info[c].p_width.zero())
1113 column_info[c].valignment = LYX_VALIGN_TOP;
1114 for (row_type r = 0; r < nrows(); ++r) {
1115 idx_type const cell = cellIndex(r, c);
1116 // because of multicolumns
1117 toggleFixedWidth(cur, cellInset(cell).get(),
1118 !getPWidth(cell).zero());
1120 // cur paragraph can become invalid after paragraphs were merged
1121 if (cur.pit() > cur.lastpit())
1122 cur.pit() = cur.lastpit();
1123 // cur position can become invalid after newlines were removed
1124 if (cur.pos() > cur.lastpos())
1125 cur.pos() = cur.lastpos();
1129 bool Tabular::setFixedWidth(row_type r, col_type c)
1131 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1132 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1133 || (multicol && !cell_info[r][c].p_width.zero());
1134 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1139 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1140 Length const & width)
1142 if (!isMultiColumn(cell))
1145 cellInfo(cell).p_width = width;
1146 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1147 // cur paragraph can become invalid after paragraphs were merged
1148 if (cur.pit() > cur.lastpit())
1149 cur.pit() = cur.lastpit();
1150 // cur position can become invalid after newlines were removed
1151 if (cur.pos() > cur.lastpos())
1152 cur.pos() = cur.lastpos();
1157 bool Tabular::setMROffset(Cursor &, idx_type cell, Length const & mroffset)
1159 cellInfo(cell).mroffset = mroffset;
1164 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1165 Tabular::Feature what)
1167 if (what == SET_SPECIAL_MULTICOLUMN)
1168 cellInfo(cell).align_special = special;
1170 column_info[cellColumn(cell)].align_special = special;
1174 void Tabular::setTopLine(idx_type i, bool line)
1176 cellInfo(i).top_line = line;
1180 void Tabular::setBottomLine(idx_type i, bool line)
1182 cellInfo(i).bottom_line = line;
1186 void Tabular::setLeftLine(idx_type cell, bool line)
1188 cellInfo(cell).left_line = line;
1192 void Tabular::setRightLine(idx_type cell, bool line)
1194 cellInfo(cell).right_line = line;
1197 bool Tabular::rowTopLine(row_type r) const
1199 bool all_rows_set = true;
1200 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1201 all_rows_set = cellInfo(cellIndex(r, c)).top_line;
1202 return all_rows_set;
1206 bool Tabular::rowBottomLine(row_type r) const
1208 bool all_rows_set = true;
1209 for (col_type c = 0; all_rows_set && c < ncols(); ++c)
1210 all_rows_set = cellInfo(cellIndex(r, c)).bottom_line;
1211 return all_rows_set;
1215 bool Tabular::columnLeftLine(col_type c) const
1222 for (row_type r = 0; r < nrows(); ++r) {
1223 idx_type const i = cellIndex(r, c);
1224 if (c == cellColumn(i)) {
1226 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1227 if (cellInfo(i).left_line || right)
1231 return 2 * nrows_left >= total;
1235 bool Tabular::columnRightLine(col_type c) const
1240 int nrows_right = 0;
1242 for (row_type r = 0; r < nrows(); ++r) {
1243 idx_type i = cellIndex(r, c);
1244 if (c == cellColumn(i) + columnSpan(i) - 1) {
1246 bool left = (c + 1 < ncols()
1247 && cellInfo(cellIndex(r, c + 1)).left_line)
1248 || c + 1 == ncols();
1249 if (cellInfo(i).right_line && left)
1253 return 2 * nrows_right >= total;
1257 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1259 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1260 return cellInfo(cell).alignment;
1262 return column_info[cellColumn(cell)].alignment;
1267 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1269 if (!onlycolumn && (isMultiColumn(cell) || isMultiRow(cell)))
1270 return cellInfo(cell).valignment;
1271 return column_info[cellColumn(cell)].valignment;
1275 Length const Tabular::getPWidth(idx_type cell) const
1277 if (isMultiColumn(cell))
1278 return cellInfo(cell).p_width;
1279 return column_info[cellColumn(cell)].p_width;
1283 Length const Tabular::getMROffset(idx_type cell) const
1285 return cellInfo(cell).mroffset;
1289 int Tabular::textHOffset(idx_type cell) const
1291 // the LaTeX Way :-(
1292 int x = WIDTH_OF_LINE;
1294 int const w = cellWidth(cell) - cellInfo(cell).width;
1296 switch (getAlignment(cell)) {
1297 case LYX_ALIGN_CENTER:
1300 case LYX_ALIGN_RIGHT:
1303 case LYX_ALIGN_DECIMAL: {
1304 // we center when no decimal point
1305 if (cellInfo(cell).decimal_width == 0) {
1309 col_type const c = cellColumn(cell);
1310 int max_dhoffset = 0;
1311 for(row_type r = 0; r < row_info.size() ; ++r) {
1312 idx_type const i = cellIndex(r, c);
1313 if (getAlignment(i) == LYX_ALIGN_DECIMAL
1314 && cellInfo(i).decimal_width != 0)
1315 max_dhoffset = max(max_dhoffset, cellInfo(i).decimal_hoffset);
1317 x += max_dhoffset - cellInfo(cell).decimal_hoffset;
1320 // LYX_ALIGN_LEFT: nothing :-)
1328 int Tabular::textVOffset(idx_type cell) const
1330 int voffset = cellInfo(cell).voffset;
1331 if (isMultiRow(cell)) {
1332 row_type const row = cellRow(cell);
1333 voffset += (cellHeight(cell) - rowAscent(row) - rowDescent(row))/2;
1339 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1342 idx_type const numcells = numberOfCellsInRow(row);
1343 // we check against numcells to make sure we do not crash if all the
1344 // cells are multirow (bug #7535), but in that case our return value
1345 // is really invalid, i.e., it is NOT the first cell in the row. but
1346 // i do not know what to do here. (rgh)
1347 while (c < numcells - 1
1348 && cell_info[row][c].multirow == CELL_PART_OF_MULTIROW)
1350 return cell_info[row][c].cellno;
1354 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1356 col_type c = ncols() - 1;
1357 // of course we check against 0 so we don't crash. but we have the same
1358 // problem as in the previous routine: if all the cells are part of a
1359 // multirow or part of a multi column, then our return value is invalid.
1361 && (cell_info[row][c].multirow == CELL_PART_OF_MULTIROW
1362 || cell_info[row][c].multicolumn == CELL_PART_OF_MULTICOLUMN))
1364 return cell_info[row][c].cellno;
1368 Tabular::row_type Tabular::cellRow(idx_type cell) const
1370 if (cell >= numberofcells)
1374 return rowofcell[cell];
1378 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1380 if (cell >= numberofcells)
1384 return columnofcell[cell];
1388 void Tabular::write(ostream & os) const
1392 << write_attribute("version", 3)
1393 << write_attribute("rows", nrows())
1394 << write_attribute("columns", ncols())
1396 // global longtable options
1398 << write_attribute("rotate", rotate)
1399 << write_attribute("booktabs", use_booktabs)
1400 << write_attribute("islongtable", is_long_tabular)
1401 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1402 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1403 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1404 << write_attribute("headTopDL", endhead.topDL)
1405 << write_attribute("headBottomDL", endhead.bottomDL)
1406 << write_attribute("footTopDL", endfoot.topDL)
1407 << write_attribute("footBottomDL", endfoot.bottomDL)
1408 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1409 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1410 << write_attribute("lastFootEmpty", endlastfoot.empty);
1411 // longtables cannot be aligned vertically
1412 if (!is_long_tabular) {
1413 os << write_attribute("tabularvalignment", tabular_valignment);
1414 os << write_attribute("tabularwidth", tabular_width);
1416 if (is_long_tabular)
1417 os << write_attribute("longtabularalignment",
1418 longtabular_alignment);
1420 for (col_type c = 0; c < ncols(); ++c) {
1422 << write_attribute("alignment", column_info[c].alignment);
1423 if (column_info[c].alignment == LYX_ALIGN_DECIMAL)
1424 os << write_attribute("decimal_point", column_info[c].decimal_point);
1425 os << write_attribute("valignment", column_info[c].valignment)
1426 << write_attribute("width", column_info[c].p_width.asString())
1427 << write_attribute("special", column_info[c].align_special)
1430 for (row_type r = 0; r < nrows(); ++r) {
1431 static const string def("default");
1433 if (row_info[r].top_space_default)
1434 os << write_attribute("topspace", def);
1436 os << write_attribute("topspace", row_info[r].top_space);
1437 if (row_info[r].bottom_space_default)
1438 os << write_attribute("bottomspace", def);
1440 os << write_attribute("bottomspace", row_info[r].bottom_space);
1441 if (row_info[r].interline_space_default)
1442 os << write_attribute("interlinespace", def);
1444 os << write_attribute("interlinespace", row_info[r].interline_space);
1445 os << write_attribute("endhead", row_info[r].endhead)
1446 << write_attribute("endfirsthead", row_info[r].endfirsthead)
1447 << write_attribute("endfoot", row_info[r].endfoot)
1448 << write_attribute("endlastfoot", row_info[r].endlastfoot)
1449 << write_attribute("newpage", row_info[r].newpage)
1450 << write_attribute("caption", row_info[r].caption)
1452 for (col_type c = 0; c < ncols(); ++c) {
1454 << write_attribute("multicolumn", cell_info[r][c].multicolumn)
1455 << write_attribute("multirow", cell_info[r][c].multirow)
1456 << write_attribute("mroffset", cell_info[r][c].mroffset)
1457 << write_attribute("alignment", cell_info[r][c].alignment)
1458 << write_attribute("valignment", cell_info[r][c].valignment)
1459 << write_attribute("topline", cell_info[r][c].top_line)
1460 << write_attribute("bottomline", cell_info[r][c].bottom_line)
1461 << write_attribute("leftline", cell_info[r][c].left_line)
1462 << write_attribute("rightline", cell_info[r][c].right_line)
1463 << write_attribute("rotate", cell_info[r][c].rotate)
1464 << write_attribute("usebox", cell_info[r][c].usebox)
1465 << write_attribute("width", cell_info[r][c].p_width)
1466 << write_attribute("special", cell_info[r][c].align_special)
1468 os << "\\begin_inset ";
1469 cell_info[r][c].inset->write(os);
1470 os << "\n\\end_inset\n"
1475 os << "</lyxtabular>\n";
1479 void Tabular::read(Lexer & lex)
1482 istream & is = lex.getStream();
1484 l_getline(is, line);
1485 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1486 LASSERT(false, /**/);
1491 if (!getTokenValue(line, "version", version))
1493 LASSERT(version >= 2, /**/);
1496 if (!getTokenValue(line, "rows", rows_arg))
1499 if (!getTokenValue(line, "columns", columns_arg))
1501 init(buffer_, rows_arg, columns_arg);
1502 l_getline(is, line);
1503 if (!prefixIs(line, "<features")) {
1504 lyxerr << "Wrong tabular format (expected <features ...> got"
1505 << line << ')' << endl;
1508 getTokenValue(line, "rotate", rotate);
1509 getTokenValue(line, "booktabs", use_booktabs);
1510 getTokenValue(line, "islongtable", is_long_tabular);
1511 getTokenValue(line, "tabularvalignment", tabular_valignment);
1512 getTokenValue(line, "tabularwidth", tabular_width);
1513 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1514 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1515 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1516 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1517 getTokenValue(line, "headTopDL", endhead.topDL);
1518 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1519 getTokenValue(line, "footTopDL", endfoot.topDL);
1520 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1521 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1522 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1523 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1525 for (col_type c = 0; c < ncols(); ++c) {
1527 if (!prefixIs(line,"<column")) {
1528 lyxerr << "Wrong tabular format (expected <column ...> got"
1529 << line << ')' << endl;
1532 getTokenValue(line, "alignment", column_info[c].alignment);
1533 getTokenValue(line, "decimal_point", column_info[c].decimal_point);
1534 getTokenValue(line, "valignment", column_info[c].valignment);
1535 getTokenValue(line, "width", column_info[c].p_width);
1536 getTokenValue(line, "special", column_info[c].align_special);
1539 for (row_type i = 0; i < nrows(); ++i) {
1540 l_getline(is, line);
1541 if (!prefixIs(line, "<row")) {
1542 lyxerr << "Wrong tabular format (expected <row ...> got"
1543 << line << ')' << endl;
1546 getTokenValue(line, "topspace", row_info[i].top_space,
1547 row_info[i].top_space_default);
1548 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1549 row_info[i].bottom_space_default);
1550 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1551 row_info[i].interline_space_default);
1552 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1553 getTokenValue(line, "endhead", row_info[i].endhead);
1554 getTokenValue(line, "endfoot", row_info[i].endfoot);
1555 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1556 getTokenValue(line, "newpage", row_info[i].newpage);
1557 getTokenValue(line, "caption", row_info[i].caption);
1558 for (col_type j = 0; j < ncols(); ++j) {
1559 l_getline(is, line);
1560 if (!prefixIs(line, "<cell")) {
1561 lyxerr << "Wrong tabular format (expected <cell ...> got"
1562 << line << ')' << endl;
1565 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1566 getTokenValue(line, "multirow", cell_info[i][j].multirow);
1567 getTokenValue(line, "mroffset", cell_info[i][j].mroffset);
1568 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1569 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1570 getTokenValue(line, "topline", cell_info[i][j].top_line);
1571 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1572 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1573 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1574 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1575 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1576 getTokenValue(line, "width", cell_info[i][j].p_width);
1578 getTokenValue(line, "special", cell_info[i][j].align_special);
1579 l_getline(is, line);
1580 if (prefixIs(line, "\\begin_inset")) {
1581 cell_info[i][j].inset->setBuffer(*buffer_);
1582 cell_info[i][j].inset->read(lex);
1583 l_getline(is, line);
1585 if (!prefixIs(line, "</cell>")) {
1586 lyxerr << "Wrong tabular format (expected </cell> got"
1587 << line << ')' << endl;
1591 l_getline(is, line);
1592 if (!prefixIs(line, "</row>")) {
1593 lyxerr << "Wrong tabular format (expected </row> got"
1594 << line << ')' << endl;
1598 while (!prefixIs(line, "</lyxtabular>")) {
1599 l_getline(is, line);
1605 bool Tabular::isMultiColumn(idx_type cell) const
1607 return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1608 || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
1612 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1614 return cell_info[cellRow(cell)][cellColumn(cell)];
1618 Tabular::idx_type Tabular::setMultiColumn(idx_type cell, idx_type number)
1620 idx_type const col = cellColumn(cell);
1621 idx_type const row = cellRow(cell);
1622 for (idx_type i = 0; i < number; ++i)
1623 unsetMultiRow(cellIndex(row, col + i));
1625 // unsetting of multirow may have invalidated cell index
1626 cell = cellIndex(row, col);
1627 CellData & cs = cellInfo(cell);
1628 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1629 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1630 cs.alignment = column_info[col].alignment;
1632 setRightLine(cell, rightLine(cellIndex(row, col - 1)));
1634 for (idx_type i = 1; i < number; ++i) {
1635 CellData & cs1 = cellInfo(cell + i);
1636 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1637 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1645 bool Tabular::isMultiRow(idx_type cell) const
1647 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1648 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1652 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number)
1654 idx_type const col = cellColumn(cell);
1655 idx_type const row = cellRow(cell);
1656 for (idx_type i = 0; i < number; ++i)
1657 unsetMultiColumn(cellIndex(row + i, col));
1659 // unsetting of multirow may have invalidated cell index
1660 cell = cellIndex(row, col);
1661 CellData & cs = cellInfo(cell);
1662 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1663 cs.valignment = LYX_VALIGN_MIDDLE;
1664 // the horizontal alignment of multirow cells can only
1665 // be changed for the whole table row,
1666 // support changing this only for the multirow cell can be done via
1668 // this feature would be a fileformat change
1669 // until LyX supports this, use the deault alignment of multirow
1671 cs.alignment = LYX_ALIGN_LEFT;
1673 // set the bottom row of the last selected cell
1674 setBottomLine(cell, bottomLine(cell + (number - 1)*ncols()));
1676 for (idx_type i = 1; i < number; ++i) {
1677 CellData & cs1 = cell_info[row + i][col];
1678 cs1.multirow = CELL_PART_OF_MULTIROW;
1679 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1687 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1689 row_type const row = cellRow(cell);
1690 col_type const col = cellColumn(cell);
1692 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1699 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1701 col_type const column = cellColumn(cell);
1702 col_type row = cellRow(cell) + 1;
1703 while (row < nrows() && isPartOfMultiRow(row, column))
1706 return row - cellRow(cell);
1710 void Tabular::unsetMultiColumn(idx_type cell)
1712 if (!isMultiColumn(cell))
1715 row_type const row = cellRow(cell);
1716 col_type const col = cellColumn(cell);
1717 row_type const span = columnSpan(cell);
1718 for (col_type c = 0; c < span; ++c) {
1719 // in the table dialog the lines are set in every case
1720 // when unsetting a multicolumn this leads to an additional right
1721 // line for every cell that was part of the former multicolumn cell,
1722 // except if the cell is in the last column
1723 // therefore remove this line
1724 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1725 && (col + c) < (col + span - 1))
1726 cell_info[row][col + c].right_line = false;
1727 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1733 void Tabular::unsetMultiRow(idx_type cell)
1735 if (!isMultiRow(cell))
1738 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1739 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1740 row_type const row = cellRow(cell);
1741 col_type const col = cellColumn(cell);
1742 row_type const span = rowSpan(cell);
1743 for (row_type r = 0; r < span; ++r)
1744 cell_info[row + r][col].multirow = CELL_NORMAL;
1749 void Tabular::setRotateCell(idx_type cell, bool flag)
1751 cellInfo(cell).rotate = flag;
1755 bool Tabular::getRotateCell(idx_type cell) const
1757 return cellInfo(cell).rotate;
1761 bool Tabular::needRotating() const
1765 for (row_type r = 0; r < nrows(); ++r)
1766 for (col_type c = 0; c < ncols(); ++c)
1767 if (cell_info[r][c].rotate)
1773 bool Tabular::isLastCell(idx_type cell) const
1775 if (cell + 1 < numberofcells)
1781 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1783 if (cellRow(cell) == 0)
1786 col_type const col = cellColumn(cell);
1787 row_type r = cellRow(cell) - 1;
1788 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1791 return cell_info[r][col].cellno;
1795 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1797 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1798 if (nextrow < nrows())
1799 return cell_info[nextrow][cellColumn(cell)].cellno;
1804 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1806 LASSERT(column != npos && column < ncols()
1807 && row != npos && row < nrows(), /**/);
1808 return cell_info[row][column].cellno;
1812 void Tabular::setUsebox(idx_type cell, BoxType type)
1814 cellInfo(cell).usebox = type;
1818 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1819 // adds line breaks, see bug 4886.
1820 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1822 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1823 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1825 if (cellInfo(cell).usebox > 1)
1826 return cellInfo(cell).usebox;
1827 return useParbox(cell);
1832 // This are functions used for the longtable support
1834 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1840 row_info[row].endfirsthead = flag;
1844 row_info[row].endhead = flag;
1849 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1852 hd.set = haveLTHead();
1853 return row_info[row].endhead;
1857 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1860 hd.set = haveLTFirstHead();
1861 return row_info[row].endfirsthead;
1865 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1871 row_info[row].endlastfoot = flag;
1875 row_info[row].endfoot = flag;
1880 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1883 fd.set = haveLTFoot();
1884 return row_info[row].endfoot;
1888 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1891 fd.set = haveLTLastFoot();
1892 return row_info[row].endlastfoot;
1896 void Tabular::setLTNewPage(row_type row, bool what)
1898 row_info[row].newpage = what;
1902 bool Tabular::getLTNewPage(row_type row) const
1904 return row_info[row].newpage;
1908 bool Tabular::haveLTHead() const
1910 if (!is_long_tabular)
1912 for (row_type i = 0; i < nrows(); ++i)
1913 if (row_info[i].endhead)
1919 bool Tabular::haveLTFirstHead() const
1921 if (!is_long_tabular || endfirsthead.empty)
1923 for (row_type r = 0; r < nrows(); ++r)
1924 if (row_info[r].endfirsthead)
1930 bool Tabular::haveLTFoot() const
1932 if (!is_long_tabular)
1934 for (row_type r = 0; r < nrows(); ++r)
1935 if (row_info[r].endfoot)
1941 bool Tabular::haveLTLastFoot() const
1943 if (!is_long_tabular || endlastfoot.empty)
1945 for (row_type r = 0; r < nrows(); ++r)
1946 if (row_info[r].endlastfoot)
1952 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1954 idx_type i = getFirstCellInRow(row);
1956 setMultiColumn(i, numberOfCellsInRow(row));
1957 setTopLine(i, false);
1958 setBottomLine(i, false);
1959 setLeftLine(i, false);
1960 setRightLine(i, false);
1962 unsetMultiColumn(i);
1963 // When unsetting a caption row, also all existing
1964 // captions in this row must be dissolved.
1966 row_info[row].caption = what;
1971 bool Tabular::ltCaption(row_type row) const
1973 return row_info[row].caption;
1977 bool Tabular::haveLTCaption() const
1979 if (!is_long_tabular)
1981 for (row_type r = 0; r < nrows(); ++r)
1982 if (row_info[r].caption)
1988 // end longtable support functions
1990 void Tabular::setRowAscent(row_type row, int height)
1992 if (row >= nrows() || row_info[row].ascent == height)
1994 row_info[row].ascent = height;
1998 void Tabular::setRowDescent(row_type row, int height)
2000 if (row >= nrows() || row_info[row].descent == height)
2002 row_info[row].descent = height;
2006 int Tabular::rowAscent(row_type row) const
2008 LASSERT(row < nrows(), /**/);
2009 return row_info[row].ascent;
2013 int Tabular::rowDescent(row_type row) const
2015 LASSERT(row < nrows(), /**/);
2016 return row_info[row].descent;
2020 int Tabular::height() const
2023 for (row_type row = 0; row < nrows(); ++row)
2024 height += rowAscent(row) + rowDescent(row) +
2030 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2032 LASSERT(row < nrows(), /**/);
2033 LASSERT(column < ncols(), /**/);
2034 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2038 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2040 LASSERT(row < nrows(), /**/);
2041 LASSERT(column < ncols(), /**/);
2042 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2046 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2048 // we only output complete row lines and the 1st row here, the rest
2049 // is done in Tabular::TeXBottomHLine(...)
2051 // get for each column the topline (if any)
2052 vector<bool> topline;
2054 for (col_type c = 0; c < ncols(); ++c) {
2055 topline.push_back(topLine(cellIndex(row, c)));
2056 // If cell is part of a multirow and not the first cell of the
2057 // multirow, no line must be drawn.
2059 if (isMultiRow(cellIndex(row, c))
2060 && isMultiRow(cellIndex(row - 1, c)))
2066 // do nothing if empty first row, or incomplete row line after
2067 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2070 // only output complete row lines and the 1st row's clines
2071 if (nset == ncols()) {
2073 os << (row == 0 ? "\\toprule " : "\\midrule ");
2077 } else if (row == 0) {
2078 for (col_type c = 0; c < ncols(); ++c) {
2080 col_type offset = 0;
2081 for (col_type j = 0 ; j < c; ++j)
2082 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2085 //babel makes the "-" character an active one, so we have to suppress this here
2086 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2087 if (lang == "slovak" || lang == "czech")
2088 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2089 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2091 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2093 col_type cstart = c;
2094 for ( ; c < ncols() && topline[c]; ++c) {}
2096 for (col_type j = cstart ; j < c ; ++j)
2097 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2100 os << c + offset << "} ";
2108 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2110 // we output bottomlines of row r and the toplines of row r+1
2111 // if the latter do not span the whole tabular
2113 // get the bottomlines of row r, and toplines in next row
2114 bool lastrow = row == nrows() - 1;
2115 vector<bool> bottomline, topline;
2116 bool nextrowset = true;
2117 for (col_type c = 0; c < ncols(); ++c) {
2118 bottomline.push_back(bottomLine(cellIndex(row, c)));
2119 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2120 // If cell is part of a multirow and not the last or first cell of the
2121 // multirow, no line must be drawn.
2123 if (isMultiRow(cellIndex(row, c))
2124 && isMultiRow(cellIndex(row + 1, c))) {
2125 bottomline[c] = false;
2128 nextrowset &= topline[c];
2131 // combine this row's bottom lines and next row's toplines if necessary
2133 for (col_type c = 0; c < ncols(); ++c) {
2135 bottomline[c] = bottomline[c] || topline[c];
2140 // do nothing if empty, OR incomplete row line with a topline in next row
2141 if (nset == 0 || (nextrowset && nset != ncols()))
2144 if (nset == ncols()) {
2146 os << (lastrow ? "\\bottomrule" : "\\midrule");
2150 for (col_type c = 0; c < ncols(); ++c) {
2151 if (bottomline[c]) {
2152 col_type offset = 0;
2153 for (col_type j = 0 ; j < c; ++j)
2154 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2157 //babel makes the "-" character an active one, so we have to suppress this here
2158 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2159 if (lang == "slovak" || lang == "czech")
2160 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2161 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2163 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2165 col_type cstart = c;
2166 for ( ; c < ncols() && bottomline[c]; ++c) {}
2168 for (col_type j = cstart ; j < c ; ++j)
2169 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2172 os << c + offset << "} ";
2180 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2181 bool & ismulticol, bool & ismultirow) const
2183 row_type const r = cellRow(cell);
2184 if (is_long_tabular && row_info[r].caption)
2187 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2188 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2189 // figure out how to set the lines
2190 // we always set double lines to the right of the cell
2191 col_type const c = cellColumn(cell);
2192 col_type const nextcol = c + columnSpan(cell);
2193 bool colright = columnRightLine(c);
2194 bool colleft = columnLeftLine(c);
2195 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2196 bool nextcellleft = nextcol < ncols()
2197 && leftLine(cellIndex(r, nextcol));
2198 bool coldouble = colright && nextcolleft;
2199 bool celldouble = rightLine(cell) && nextcellleft;
2201 ismulticol = isMultiColumn(cell)
2202 || (c == 0 && colleft != leftLine(cell))
2203 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2204 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2205 || (coldouble != celldouble);
2207 // we center in multicol when no decimal point
2208 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2209 docstring const align_d = column_info[c].decimal_point;
2210 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2214 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2215 int latexcolspan = columnSpan(cell);
2216 for(col_type col = c; col < c + columnSpan(cell); ++col)
2217 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2221 os << "\\multicolumn{" << latexcolspan << "}{";
2222 if (c ==0 && leftLine(cell))
2224 if (!cellInfo(cell).align_special.empty()) {
2225 os << cellInfo(cell).align_special;
2227 if (!getPWidth(cell).zero()) {
2229 case LYX_ALIGN_LEFT:
2230 os << ">{\\raggedright}";
2232 case LYX_ALIGN_RIGHT:
2233 os << ">{\\raggedleft}";
2235 case LYX_ALIGN_CENTER:
2236 os << ">{\\centering}";
2242 case LYX_VALIGN_TOP:
2245 case LYX_VALIGN_MIDDLE:
2248 case LYX_VALIGN_BOTTOM:
2253 << from_ascii(getPWidth(cell).asLatexString())
2257 case LYX_ALIGN_LEFT:
2260 case LYX_ALIGN_RIGHT:
2267 } // end if else !getPWidth
2268 } // end if else !cellinfo_of_cell
2269 if (rightLine(cell) || nextcellleft)
2272 // add extra vertical line if we want a double one
2275 } // end if ismulticol
2277 // we only need code for the first multirow cell
2278 ismultirow = isMultiRow(cell);
2280 os << "\\multirow{" << rowSpan(cell) << "}{";
2281 if (!getPWidth(cell).zero())
2282 os << from_ascii(getPWidth(cell).asLatexString());
2284 // we need to set a default value
2287 if (!getMROffset(cell).zero())
2288 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2290 } // end if ismultirow
2292 if (getRotateCell(cell)) {
2293 os << "\\begin{sideways}\n";
2295 if (getUsebox(cell) == BOX_PARBOX) {
2298 case LYX_VALIGN_TOP:
2301 case LYX_VALIGN_MIDDLE:
2304 case LYX_VALIGN_BOTTOM:
2308 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2310 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2311 os << "\\begin{minipage}[";
2313 case LYX_VALIGN_TOP:
2316 case LYX_VALIGN_MIDDLE:
2319 case LYX_VALIGN_BOTTOM:
2323 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2329 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2330 bool ismulticol, bool ismultirow) const
2332 row_type const r = cellRow(cell);
2333 if (is_long_tabular && row_info[r].caption)
2337 if (getUsebox(cell) == BOX_PARBOX)
2339 else if (getUsebox(cell) == BOX_MINIPAGE)
2340 os << breakln << "\\end{minipage}";
2341 if (getRotateCell(cell))
2342 os << breakln << "\\end{sideways}";
2350 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2351 OutputParams const & runparams) const
2353 if (!is_long_tabular)
2357 // the caption must be output before the headers
2358 if (haveLTCaption()) {
2359 for (row_type r = 0; r < nrows(); ++r) {
2360 if (row_info[r].caption)
2361 TeXRow(os, r, runparams);
2364 // output first header info
2365 // first header must be output before the header, otherwise the
2366 // correct caption placement becomes really weird
2367 if (haveLTFirstHead()) {
2368 if (endfirsthead.topDL)
2370 for (row_type r = 0; r < nrows(); ++r) {
2371 if (row_info[r].endfirsthead)
2372 TeXRow(os, r, runparams);
2374 if (endfirsthead.bottomDL)
2376 os << "\\endfirsthead\n";
2378 // output header info
2380 if (endfirsthead.empty && !haveLTFirstHead())
2381 os << "\\endfirsthead\n";
2384 for (row_type r = 0; r < nrows(); ++r) {
2385 if (row_info[r].endhead)
2386 TeXRow(os, r, runparams);
2388 if (endhead.bottomDL)
2390 os << "\\endhead\n";
2392 // output footer info
2396 for (row_type r = 0; r < nrows(); ++r) {
2397 if (row_info[r].endfoot)
2398 TeXRow(os, r, runparams);
2400 if (endfoot.bottomDL)
2402 os << "\\endfoot\n";
2403 if (endlastfoot.empty && !haveLTLastFoot())
2404 os << "\\endlastfoot\n";
2406 // output lastfooter info
2407 if (haveLTLastFoot()) {
2408 if (endlastfoot.topDL)
2410 for (row_type r = 0; r < nrows(); ++r) {
2411 if (row_info[r].endlastfoot)
2412 TeXRow(os, r, runparams);
2414 if (endlastfoot.bottomDL)
2416 os << "\\endlastfoot\n";
2421 bool Tabular::isValidRow(row_type row) const
2423 if (!is_long_tabular)
2425 return !row_info[row].endhead && !row_info[row].endfirsthead
2426 && !row_info[row].endfoot && !row_info[row].endlastfoot
2427 && !row_info[row].caption;
2431 void Tabular::TeXRow(otexstream & os, row_type row,
2432 OutputParams const & runparams) const
2434 idx_type cell = cellIndex(row, 0);
2435 shared_ptr<InsetTableCell> inset = cellInset(cell);
2436 Paragraph const & par = inset->paragraphs().front();
2437 string const lang = par.getParLanguage(buffer().params())->lang();
2439 //output the top line
2440 TeXTopHLine(os, row, lang);
2442 if (row_info[row].top_space_default) {
2444 os << "\\addlinespace\n";
2446 os << "\\noalign{\\vskip\\doublerulesep}\n";
2447 } else if(!row_info[row].top_space.zero()) {
2449 os << "\\addlinespace["
2450 << from_ascii(row_info[row].top_space.asLatexString())
2453 os << "\\noalign{\\vskip"
2454 << from_ascii(row_info[row].top_space.asLatexString())
2458 bool ismulticol = false;
2459 bool ismultirow = false;
2460 for (col_type c = 0; c < ncols(); ++c) {
2461 if (isPartOfMultiColumn(row, c))
2464 cell = cellIndex(row, c);
2466 if (isPartOfMultiRow(row, c)
2467 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2468 if (cell != getLastCellInRow(row))
2473 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2474 shared_ptr<InsetTableCell> inset = cellInset(cell);
2476 Paragraph const & par = inset->paragraphs().front();
2477 bool rtl = par.isRTL(buffer().params())
2479 && getPWidth(cell).zero();
2483 par.getParLanguage(buffer().params())->lang();
2484 if (lang == "farsi")
2486 else if (lang == "arabic_arabi")
2488 // currently, remaning RTL languages are
2489 // arabic_arabtex and hebrew
2493 // pass to the OutputParams that we are in a cell and
2494 // which alignment we have set.
2495 // InsetNewline needs this context information.
2496 OutputParams newrp(runparams);
2497 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2498 ? OutputParams::PLAIN
2499 : OutputParams::ALIGNED;
2501 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2502 // copy cell and split in 2
2503 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2504 head.setBuffer(buffer());
2505 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2507 dit.push_back(CursorSlice(head));
2508 head.setMacrocontextPositionRecursive(dit);
2509 bool hassep = false;
2510 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2511 head.latex(os, newrp);
2514 tail.setBuffer(head.buffer());
2516 dit.push_back(CursorSlice(tail));
2517 tail.setMacrocontextPositionRecursive(dit);
2518 tail.latex(os, newrp);
2520 } else if (!isPartOfMultiRow(row, c)) {
2521 if (!runparams.nice)
2522 os.texrow().start(par.id(), 0);
2523 inset->latex(os, newrp);
2526 runparams.encoding = newrp.encoding;
2530 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2531 if (cell != getLastCellInRow(row)) { // not last cell in row
2538 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2539 // if no first header and no empty first header is used,
2540 // the caption needs to be terminated by \endfirsthead
2542 os << "\\endfirsthead";
2544 os << "\\tabularnewline";
2545 if (row_info[row].bottom_space_default) {
2547 os << "\\addlinespace";
2549 os << "[\\doublerulesep]";
2550 } else if (!row_info[row].bottom_space.zero()) {
2552 os << "\\addlinespace";
2554 << from_ascii(row_info[row].bottom_space.asLatexString())
2559 //output the bottom line
2560 TeXBottomHLine(os, row, lang);
2562 if (row_info[row].interline_space_default) {
2564 os << "\\addlinespace\n";
2566 os << "\\noalign{\\vskip\\doublerulesep}\n";
2567 } else if (!row_info[row].interline_space.zero()) {
2569 os << "\\addlinespace["
2570 << from_ascii(row_info[row].interline_space.asLatexString())
2573 os << "\\noalign{\\vskip"
2574 << from_ascii(row_info[row].interline_space.asLatexString())
2580 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2582 bool const is_tabular_star = !tabular_width.zero();
2584 //+---------------------------------------------------------------------
2585 //+ first the opening preamble +
2586 //+---------------------------------------------------------------------
2589 if (runparams.lastid != -1)
2590 os.texrow().start(runparams.lastid, runparams.lastpos);
2593 os << "\\begin{sideways}\n";
2595 if (is_long_tabular) {
2596 os << "\\begin{longtable}";
2597 switch (longtabular_alignment) {
2598 case LYX_LONGTABULAR_ALIGN_LEFT:
2601 case LYX_LONGTABULAR_ALIGN_CENTER:
2603 case LYX_LONGTABULAR_ALIGN_RIGHT:
2608 if (is_tabular_star)
2609 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2611 os << "\\begin{tabular}";
2612 switch (tabular_valignment) {
2613 case LYX_VALIGN_TOP:
2616 case LYX_VALIGN_MIDDLE:
2618 case LYX_VALIGN_BOTTOM:
2626 if (is_tabular_star)
2627 os << "@{\\extracolsep{\\fill}}";
2629 for (col_type c = 0; c < ncols(); ++c) {
2630 if (columnLeftLine(c))
2632 if (!column_info[c].align_special.empty()) {
2633 os << column_info[c].align_special;
2635 if (!column_info[c].p_width.zero()) {
2636 switch (column_info[c].alignment) {
2637 case LYX_ALIGN_LEFT:
2638 os << ">{\\raggedright}";
2640 case LYX_ALIGN_RIGHT:
2641 os << ">{\\raggedleft}";
2643 case LYX_ALIGN_CENTER:
2644 os << ">{\\centering}";
2646 case LYX_ALIGN_NONE:
2647 case LYX_ALIGN_BLOCK:
2648 case LYX_ALIGN_LAYOUT:
2649 case LYX_ALIGN_SPECIAL:
2650 case LYX_ALIGN_DECIMAL:
2654 switch (column_info[c].valignment) {
2655 case LYX_VALIGN_TOP:
2658 case LYX_VALIGN_MIDDLE:
2661 case LYX_VALIGN_BOTTOM:
2666 << from_ascii(column_info[c].p_width.asLatexString())
2669 switch (column_info[c].alignment) {
2670 case LYX_ALIGN_LEFT:
2673 case LYX_ALIGN_RIGHT:
2676 case LYX_ALIGN_DECIMAL:
2677 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2683 } // end if else !column_info[i].p_width
2684 } // end if else !column_info[i].align_special
2685 if (columnRightLine(c))
2690 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 TeXRow(os, r, runparams);
2699 if (is_long_tabular && row_info[r].newpage)
2700 os << "\\newpage\n";
2704 //+---------------------------------------------------------------------
2705 //+ the closing of the tabular +
2706 //+---------------------------------------------------------------------
2708 if (is_long_tabular)
2709 os << "\\end{longtable}";
2711 if (is_tabular_star)
2712 os << "\\end{tabular*}";
2714 os << "\\end{tabular}";
2718 os << breakln << "\\end{sideways}";
2722 int Tabular::docbookRow(odocstream & os, row_type row,
2723 OutputParams const & runparams) const
2726 idx_type cell = getFirstCellInRow(row);
2729 for (col_type c = 0; c < ncols(); ++c) {
2730 if (isPartOfMultiColumn(row, c))
2733 os << "<entry align=\"";
2734 switch (getAlignment(cell)) {
2735 case LYX_ALIGN_LEFT:
2738 case LYX_ALIGN_RIGHT:
2746 os << "\" valign=\"";
2747 switch (getVAlignment(cell)) {
2748 case LYX_VALIGN_TOP:
2751 case LYX_VALIGN_BOTTOM:
2754 case LYX_VALIGN_MIDDLE:
2759 if (isMultiColumn(cell)) {
2760 os << " namest=\"col" << c << "\" ";
2761 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2765 ret += cellInset(cell)->docbook(os, runparams);
2774 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2778 //+---------------------------------------------------------------------
2779 //+ first the opening preamble +
2780 //+---------------------------------------------------------------------
2782 os << "<tgroup cols=\"" << ncols()
2783 << "\" colsep=\"1\" rowsep=\"1\">\n";
2785 for (col_type c = 0; c < ncols(); ++c) {
2786 os << "<colspec colname=\"col" << c << "\" align=\"";
2787 switch (column_info[c].alignment) {
2788 case LYX_ALIGN_LEFT:
2791 case LYX_ALIGN_RIGHT:
2799 if (runparams.flavor == OutputParams::XML)
2805 //+---------------------------------------------------------------------
2806 //+ Long Tabular case +
2807 //+---------------------------------------------------------------------
2809 // output caption info
2810 if (haveLTCaption()) {
2811 os << "<caption>\n";
2813 for (row_type r = 0; r < nrows(); ++r) {
2814 if (row_info[r].caption) {
2815 ret += docbookRow(os, r, runparams);
2818 os << "</caption>\n";
2821 // output header info
2822 if (haveLTHead() || haveLTFirstHead()) {
2825 for (row_type r = 0; r < nrows(); ++r) {
2826 if (row_info[r].endhead || row_info[r].endfirsthead) {
2827 ret += docbookRow(os, r, runparams);
2833 // output footer info
2834 if (haveLTFoot() || haveLTLastFoot()) {
2837 for (row_type r = 0; r < nrows(); ++r) {
2838 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2839 ret += docbookRow(os, r, runparams);
2846 //+---------------------------------------------------------------------
2847 //+ the single row and columns (cells) +
2848 //+---------------------------------------------------------------------
2852 for (row_type r = 0; r < nrows(); ++r) {
2853 if (isValidRow(r)) {
2854 ret += docbookRow(os, r, runparams);
2859 //+---------------------------------------------------------------------
2860 //+ the closing of the tabular +
2861 //+---------------------------------------------------------------------
2870 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2871 OutputParams const & runparams, bool header) const
2874 string const celltag = header ? "th" : "td";
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) || isPartOfMultiRow(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) << "'";
2911 else if (isMultiRow(cell))
2912 attr << " rowspan='" << rowSpan(cell) << "'";
2914 xs << html::StartTag(celltag, attr.str()) << html::CR();
2915 ret += cellInset(cell)->xhtml(xs, runparams);
2916 xs << html::EndTag(celltag) << html::CR();
2919 xs << html::EndTag("tr");
2924 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2928 if (is_long_tabular) {
2929 // we'll wrap it in a div, so as to deal with alignment
2931 switch (longtabular_alignment) {
2932 case LYX_LONGTABULAR_ALIGN_LEFT:
2935 case LYX_LONGTABULAR_ALIGN_CENTER:
2938 case LYX_LONGTABULAR_ALIGN_RIGHT:
2942 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2944 if (haveLTCaption()) {
2945 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2947 for (row_type r = 0; r < nrows(); ++r)
2948 if (row_info[r].caption)
2949 ret += xhtmlRow(xs, r, runparams);
2950 xs << html::EndTag("div") << html::CR();
2954 xs << html::StartTag("table") << html::CR();
2956 // output header info
2957 bool const havefirsthead = haveLTFirstHead();
2958 // if we have a first head, then we are going to ignore the
2959 // headers for the additional pages, since there aren't any
2960 // in XHTML. this test accomplishes that.
2961 bool const havehead = !havefirsthead && haveLTHead();
2962 if (havehead || havefirsthead) {
2963 xs << html::StartTag("thead") << html::CR();
2964 for (row_type r = 0; r < nrows(); ++r) {
2965 if ((havefirsthead && row_info[r].endfirsthead)
2966 || (havehead && row_info[r].endhead)) {
2967 ret += xhtmlRow(xs, r, runparams, true);
2970 xs << html::EndTag("thead") << html::CR();
2972 // output footer info
2973 bool const havelastfoot = haveLTLastFoot();
2975 bool const havefoot = !havelastfoot && haveLTFoot();
2976 if (havefoot || havelastfoot) {
2977 xs << html::StartTag("tfoot") << html::CR();
2978 for (row_type r = 0; r < nrows(); ++r) {
2979 if ((havelastfoot && row_info[r].endlastfoot)
2980 || (havefoot && row_info[r].endfoot)) {
2981 ret += xhtmlRow(xs, r, runparams);
2984 xs << html::EndTag("tfoot") << html::CR();
2987 xs << html::StartTag("tbody") << html::CR();
2988 for (row_type r = 0; r < nrows(); ++r) {
2989 if (isValidRow(r)) {
2990 ret += xhtmlRow(xs, r, runparams);
2993 xs << html::EndTag("tbody")
2995 << html::EndTag("table")
2997 if (is_long_tabular)
2998 xs << html::EndTag("div") << html::CR();
3003 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3004 vector<unsigned int> const & clen) const
3006 idx_type const fcell = getFirstCellInRow(row);
3007 idx_type const n = numberOfCellsInRow(row) + fcell;
3010 for (idx_type i = fcell; i < n; ++i) {
3020 for (idx_type i = fcell; i < n; ++i) {
3031 col_type column = cellColumn(i);
3032 int len = clen[column];
3033 while (column < ncols() - 1
3034 && isPartOfMultiColumn(row, ++column))
3035 len += clen[column] + 4;
3036 os << docstring(len, ch);
3051 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3052 vector<unsigned int> const & clen) const
3054 idx_type const fcell = getFirstCellInRow(row);
3055 idx_type const n = numberOfCellsInRow(row) + fcell;
3058 for (idx_type i = fcell; i < n; ++i) {
3059 if (bottomLine(i)) {
3068 for (idx_type i = fcell; i < n; ++i) {
3069 if (bottomLine(i)) {
3079 col_type column = cellColumn(i);
3080 int len = clen[column];
3081 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3082 len += clen[column] + 4;
3083 os << docstring(len, ch);
3084 if (bottomLine(i)) {
3098 void Tabular::plaintextPrintCell(odocstream & os,
3099 OutputParams const & runparams,
3100 idx_type cell, row_type row, col_type column,
3101 vector<unsigned int> const & clen,
3102 bool onlydata) const
3104 odocstringstream sstr;
3105 cellInset(cell)->plaintext(sstr, runparams);
3117 unsigned int len1 = sstr.str().length();
3118 unsigned int len2 = clen[column];
3119 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3120 len2 += clen[column] + 4;
3123 switch (getAlignment(cell)) {
3125 case LYX_ALIGN_LEFT:
3128 case LYX_ALIGN_RIGHT:
3132 case LYX_ALIGN_CENTER:
3138 os << docstring(len1, ' ') << sstr.str()
3139 << docstring(len2, ' ');
3141 if (rightLine(cell))
3148 void Tabular::plaintext(odocstream & os,
3149 OutputParams const & runparams, int const depth,
3150 bool onlydata, char_type delim) const
3152 // first calculate the width of the single columns
3153 vector<unsigned int> clen(ncols());
3156 // first all non multicolumn cells!
3157 for (col_type c = 0; c < ncols(); ++c) {
3159 for (row_type r = 0; r < nrows(); ++r) {
3160 idx_type cell = cellIndex(r, c);
3161 if (isMultiColumn(cell))
3163 odocstringstream sstr;
3164 cellInset(cell)->plaintext(sstr, runparams);
3165 if (clen[c] < sstr.str().length())
3166 clen[c] = sstr.str().length();
3169 // then all multicolumn cells!
3170 for (col_type c = 0; c < ncols(); ++c) {
3171 for (row_type r = 0; r < nrows(); ++r) {
3172 idx_type cell = cellIndex(r, c);
3173 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3175 odocstringstream sstr;
3176 cellInset(cell)->plaintext(sstr, runparams);
3177 int len = int(sstr.str().length());
3178 idx_type const n = columnSpan(cell);
3179 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3181 if (len > int(clen[c + n - 1]))
3182 clen[c + n - 1] = len;
3187 for (row_type r = 0; r < nrows(); ++r) {
3188 if (!onlydata && plaintextTopHLine(os, r, clen))
3189 os << docstring(depth * 2, ' ');
3190 for (col_type c = 0; c < ncols(); ++c) {
3191 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3193 if (onlydata && c > 0)
3194 // we don't use operator<< for single UCS4 character.
3195 // see explanation in docstream.h
3197 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3202 os << docstring(depth * 2, ' ');
3203 if (plaintextBottomHLine(os, r, clen))
3204 os << docstring(depth * 2, ' ');
3210 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3212 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3216 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3217 col_type column) const
3219 return cell_info[row][column].inset;
3223 void Tabular::setCellInset(row_type row, col_type column,
3224 shared_ptr<InsetTableCell> ins) const
3226 CellData & cd = cell_info[row][column];
3231 void Tabular::validate(LaTeXFeatures & features) const
3233 features.require("NeedTabularnewline");
3235 features.require("booktabs");
3236 if (is_long_tabular)
3237 features.require("longtable");
3239 features.require("rotating");
3240 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3241 if (isMultiRow(cell))
3242 features.require("multirow");
3243 if (getVAlignment(cell) != LYX_VALIGN_TOP
3244 || !getPWidth(cell).zero())
3245 features.require("array");
3246 cellInset(cell)->validate(features);
3251 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3253 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3254 ParagraphList::const_iterator cit = parlist.begin();
3255 ParagraphList::const_iterator end = parlist.end();
3257 for (; cit != end; ++cit)
3258 for (int i = 0; i < cit->size(); ++i)
3259 if (cit->isNewline(i))
3266 /////////////////////////////////////////////////////////////////////
3270 /////////////////////////////////////////////////////////////////////
3272 InsetTableCell::InsetTableCell(Buffer * buf)
3273 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3274 contentAlign(LYX_ALIGN_CENTER)
3278 bool InsetTableCell::forcePlainLayout(idx_type) const
3280 return !isFixedWidth;
3284 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3286 return isFixedWidth;
3290 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3291 FuncStatus & status) const
3293 bool enabled = true;
3294 switch (cmd.action()) {
3296 enabled = !forcePlainLayout();
3298 case LFUN_LAYOUT_PARAGRAPH:
3299 enabled = allowParagraphCustomization();
3302 case LFUN_MATH_DISPLAY:
3303 if (!hasFixedWidth()) {
3308 return InsetText::getStatus(cur, cmd, status);
3310 status.setEnabled(enabled);
3314 docstring InsetTableCell::asString(bool intoInsets)
3317 if (paragraphs().empty())
3319 ParagraphList::const_iterator it = paragraphs().begin();
3320 ParagraphList::const_iterator en = paragraphs().end();
3322 for (; it != en; ++it) {
3327 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3333 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3336 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3337 return InsetText::xhtml(xs, rp);
3342 /////////////////////////////////////////////////////////////////////
3346 /////////////////////////////////////////////////////////////////////
3348 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3350 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3351 rowselect_(false), colselect_(false)
3356 InsetTabular::InsetTabular(InsetTabular const & tab)
3357 : Inset(tab), tabular(tab.tabular), scx_(0)
3362 InsetTabular::~InsetTabular()
3364 hideDialogs("tabular", this);
3368 void InsetTabular::setBuffer(Buffer & buf)
3370 tabular.setBuffer(buf);
3371 Inset::setBuffer(buf);
3375 bool InsetTabular::insetAllowed(InsetCode code) const
3380 case MATHMACRO_CODE:
3385 return tabular.is_long_tabular;
3393 void InsetTabular::write(ostream & os) const
3395 os << "Tabular" << endl;
3400 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3402 // FIXME: depending on the selection state,
3403 // we could offer a different menu.
3404 return cell(0)->contextMenuName() + ";" + contextMenuName();
3408 docstring InsetTabular::contextMenuName() const
3410 return from_ascii("context-tabular");
3414 void InsetTabular::read(Lexer & lex)
3416 //bool const old_format = (lex.getString() == "\\LyXTable");
3424 string token = lex.getString();
3425 while (lex && token != "\\end_inset") {
3427 token = lex.getString();
3430 lex.printError("Missing \\end_inset at this point. ");
3434 int InsetTabular::rowFromY(Cursor & cur, int y) const
3436 // top y coordinate of tabular
3437 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3439 for (; r < tabular.nrows() && y > h; ++r)
3440 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3441 + tabular.interRowSpace(r);
3447 int InsetTabular::columnFromX(Cursor & cur, int x) const
3449 // left x coordinate of tabular
3450 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3452 for (; c < tabular.ncols() && x > w; ++c)
3453 w += tabular.cellWidth(c);
3458 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3460 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3461 // mi.base.textwidth << "\n";
3464 LASSERT(false, /**/);
3467 for (row_type r = 0; r < tabular.nrows(); ++r) {
3470 for (col_type c = 0; c < tabular.ncols(); ++c) {
3471 if (tabular.isPartOfMultiColumn(r, c)
3472 || tabular.isPartOfMultiRow(r, c))
3473 // multicolumn or multirow cell, but not first one
3475 idx_type const cell = tabular.cellIndex(r, c);
3478 Length const p_width = tabular.getPWidth(cell);
3479 if (!p_width.zero())
3480 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3481 tabular.cellInset(cell)->metrics(m, dim);
3482 if (!p_width.zero())
3483 dim.wid = m.base.textwidth;
3484 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3485 + tabular.interColumnSpace(cell);
3487 // FIXME(?): do we need a second metrics call?
3488 TextMetrics const & tm =
3489 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3491 // determine horizontal offset because of decimal align (if necessary)
3492 int decimal_width = 0;
3493 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3494 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3495 tail.setBuffer(tabular.buffer());
3496 // we need to set macrocontext position everywhere
3497 // otherwise we crash with nested insets (e.g. footnotes)
3498 // after decimal point
3499 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3501 dit.push_back(CursorSlice(tail));
3502 tail.setMacrocontextPositionRecursive(dit);
3504 // remove text leading decimal point
3505 docstring const align_d = tabular.column_info[c].decimal_point;
3506 dit = separatorPos(&tail, align_d);
3508 pit_type const psize = tail.paragraphs().front().size();
3510 tail.paragraphs().front().eraseChars(0,
3511 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3513 tail.metrics(m, dim1);
3514 decimal_width = dim1.width();
3517 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3518 tabular.cell_info[r][c].decimal_width = decimal_width;
3520 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3521 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3522 int const lastpardes = tm.last().second->descent()
3523 + TEXT_TO_INSET_OFFSET;
3525 switch (tabular.getVAlignment(cell)) {
3526 case Tabular::LYX_VALIGN_TOP:
3528 case Tabular::LYX_VALIGN_MIDDLE:
3529 offset = -(dim.des - lastpardes)/2;
3531 case Tabular::LYX_VALIGN_BOTTOM:
3532 offset = -(dim.des - lastpardes);
3535 tabular.cell_info[r][c].voffset = offset;
3536 maxasc = max(maxasc, dim.asc - offset);
3537 maxdes = max(maxdes, dim.des + offset);
3539 int const top_space = tabular.row_info[r].top_space_default ?
3540 default_line_space :
3541 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3542 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3543 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3544 default_line_space :
3545 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3546 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3549 // for top-alignment the first horizontal table line must be exactly at
3550 // the position of the base line of the surrounding text line
3551 // for bottom alignment, the same is for the last table line
3552 switch (tabular.tabular_valignment) {
3553 case Tabular::LYX_VALIGN_BOTTOM:
3554 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3556 case Tabular::LYX_VALIGN_MIDDLE:
3557 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3559 case Tabular::LYX_VALIGN_TOP:
3560 offset_valign_ = tabular.rowAscent(0);
3564 tabular.updateColumnWidths();
3565 dim.asc = tabular.rowAscent(0) - offset_valign_;
3566 dim.des = tabular.height() - dim.asc;
3567 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3571 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3574 if (&cur.inset() == this && cur.selection()) {
3575 if (cur.selIsMultiCell()) {
3578 getSelection(cur, rs, re, cs, ce);
3580 idx_type const cell = tabular.cellIndex(row, col);
3581 col_type const cspan = tabular.columnSpan(cell);
3582 row_type const rspan = tabular.rowSpan(cell);
3583 if (col + cspan - 1 >= cs && col <= ce
3584 && row + rspan - 1 >= rs && row <= re)
3587 if (col == tabular.cellColumn(cur.idx())
3588 && row == tabular.cellRow(cur.idx())) {
3589 CursorSlice const & beg = cur.selBegin();
3590 CursorSlice const & end = cur.selEnd();
3592 if ((end.lastpos() > 0 || end.lastpit() > 0)
3593 && end.pos() == end.lastpos() && beg.pos() == 0
3594 && end.pit() == end.lastpit() && beg.pit() == 0)
3602 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3604 x += scx_ + ADD_TO_TABULAR_WIDTH;
3606 BufferView * bv = pi.base.bv;
3607 Cursor & cur = pi.base.bv->cursor();
3610 // FIXME: As the full background is painted in drawBackground(),
3611 // we have no choice but to do a full repaint for the Text cells.
3612 pi.full_repaint = true;
3614 bool const original_selection_state = pi.selected;
3617 first_visible_cell = Tabular::npos;
3619 int yy = y + offset_valign_;
3620 for (row_type r = 0; r < tabular.nrows(); ++r) {
3622 for (col_type c = 0; c < tabular.ncols(); ++c) {
3623 if (tabular.isPartOfMultiColumn(r, c))
3626 idx = tabular.cellIndex(r, c);
3628 if (tabular.isPartOfMultiRow(r, c)) {
3629 nx += tabular.cellWidth(idx);
3633 if (first_visible_cell == Tabular::npos)
3634 first_visible_cell = idx;
3636 pi.selected |= isCellSelected(cur, r, c);
3637 int const cx = nx + tabular.textHOffset(idx);
3638 int const cy = yy + tabular.textVOffset(idx);
3639 // Cache the Inset position.
3640 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3641 cell(idx)->draw(pi, cx, cy);
3642 drawCellLines(pi, nx, yy, r, idx);
3643 nx += tabular.cellWidth(idx);
3644 pi.selected = original_selection_state;
3647 if (r + 1 < tabular.nrows())
3648 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3649 + tabular.interRowSpace(r + 1);
3654 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3656 x += scx_ + ADD_TO_TABULAR_WIDTH;
3657 y += offset_valign_ - tabular.rowAscent(0);
3658 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3659 pi.backgroundColor(this));
3663 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3665 Cursor & cur = pi.base.bv->cursor();
3668 x += scx_ + ADD_TO_TABULAR_WIDTH;
3670 if (!cur.selection())
3672 if (&cur.inset() != this)
3677 bool const full_cell_selected = isCellSelected(cur,
3678 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3680 if (cur.selIsMultiCell() || full_cell_selected) {
3681 for (row_type r = 0; r < tabular.nrows(); ++r) {
3683 for (col_type c = 0; c < tabular.ncols(); ++c) {
3684 if (tabular.isPartOfMultiColumn(r, c))
3687 idx_type const cell = tabular.cellIndex(r, c);
3689 if (tabular.isPartOfMultiRow(r, c)) {
3690 xx += tabular.cellWidth(cell);
3693 int const w = tabular.cellWidth(cell);
3694 int const h = tabular.cellHeight(cell);
3695 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3696 if (isCellSelected(cur, r, c))
3697 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3700 if (r + 1 < tabular.nrows())
3701 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3702 + tabular.interRowSpace(r + 1);
3706 // FIXME: This code has no effect because InsetTableCell does not handle
3707 // drawSelection other than the trivial implementation in Inset.
3709 // x += cellXPos(cur.idx());
3710 // x += tabular.textHOffset(cur.idx());
3711 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3716 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3717 row_type row, idx_type cell) const
3719 y -= tabular.rowAscent(row);
3720 int const w = tabular.cellWidth(cell);
3721 int const h = tabular.cellHeight(cell);
3722 Color const linecolor = pi.textColor(Color_tabularline);
3723 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3726 bool drawline = tabular.topLine(cell)
3727 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3728 pi.pain.line(x, y, x + w, y,
3729 drawline ? linecolor : gridcolor,
3730 drawline ? Painter::line_solid : Painter::line_onoffdash);
3733 drawline = tabular.bottomLine(cell);
3734 pi.pain.line(x, y + h, x + w, y + h,
3735 drawline ? linecolor : gridcolor,
3736 drawline ? Painter::line_solid : Painter::line_onoffdash);
3739 col_type const col = tabular.cellColumn(cell);
3740 drawline = tabular.leftLine(cell)
3741 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3742 pi.pain.line(x, y, x, y + h,
3743 drawline ? linecolor : gridcolor,
3744 drawline ? Painter::line_solid : Painter::line_onoffdash);
3747 x -= tabular.interColumnSpace(cell);
3748 drawline = tabular.rightLine(cell)
3749 || (col + 1 < tabular.ncols()
3750 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3751 pi.pain.line(x + w, y, x + w, y + h,
3752 drawline ? linecolor : gridcolor,
3753 drawline ? Painter::line_solid : Painter::line_onoffdash);
3757 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3759 //lyxerr << "InsetTabular::edit: " << this << endl;
3761 cur.setSelection(false);
3764 if (isRightToLeft(cur))
3765 cur.idx() = tabular.getLastCellInRow(0);
3771 if (isRightToLeft(cur))
3772 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3774 cur.idx() = tabular.numberofcells - 1;
3776 cur.pos() = cur.lastpos(); // FIXME crude guess
3778 cur.setCurrentFont();
3779 // FIXME: this accesses the position cache before it is initialized
3781 //cur.bv().fitCursor();
3785 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3787 // In a longtable, tell captions what the current float is
3788 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3789 string const saveflt = cnts.current_float();
3790 if (tabular.is_long_tabular)
3791 cnts.current_float("table");
3793 ParIterator it2 = it;
3795 size_t const end = it2.nargs();
3796 for ( ; it2.idx() < end; it2.top().forwardIdx())
3797 buffer().updateBuffer(it2, utype);
3800 if (tabular.is_long_tabular)
3801 cnts.current_float(saveflt);
3805 void InsetTabular::addToToc(DocIterator const & cpit) const
3807 DocIterator dit = cpit;
3809 size_t const end = dit.nargs();
3810 for ( ; dit.idx() < end; dit.top().forwardIdx())
3811 cell(dit.idx())->addToToc(dit);
3815 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3817 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3818 << "\n cur:" << cur);
3819 CursorSlice sl = cur.top();
3820 Cursor & bvcur = cur.bv().cursor();
3822 FuncCode const act = cmd.action();
3826 case LFUN_MOUSE_PRESS: {
3827 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3829 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3830 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3831 row_type r = rowFromY(cur, cmd.y());
3832 cur.idx() = tabular.getFirstCellInRow(r);
3835 cur.idx() = tabular.getLastCellInRow(r);
3836 cur.pos() = cur.lastpos();
3837 cur.setSelection(true);
3843 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3844 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3845 || cmd.y() > y0 + tabular.height()) {
3846 col_type c = columnFromX(cur, cmd.x());
3847 cur.idx() = tabular.cellIndex(0, c);
3850 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3851 cur.pos() = cur.lastpos();
3852 cur.setSelection(true);
3857 // do not reset cursor/selection if we have selected
3858 // some cells (bug 2715).
3859 if (cmd.button() == mouse_button::button3
3860 && &bvcur.selBegin().inset() == this
3861 && bvcur.selIsMultiCell())
3864 // Let InsetTableCell do it
3865 cell(cur.idx())->dispatch(cur, cmd);
3868 case LFUN_MOUSE_MOTION:
3869 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3870 if (cmd.button() == mouse_button::button1) {
3871 // only accept motions to places not deeper nested than the real anchor
3872 if (!bvcur.realAnchor().hasPart(cur)) {
3876 // select (additional) row
3878 row_type r = rowFromY(cur, cmd.y());
3879 cur.idx() = tabular.getLastCellInRow(r);
3880 // we need to reset the cursor's pit and pos now, as the old ones
3881 // may no longer be valid.
3884 bvcur.setCursor(cur);
3885 bvcur.setSelection(true);
3888 // select (additional) column
3890 col_type c = columnFromX(cur, cmd.x());
3891 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3892 // we need to reset the cursor's pit and pos now, as the old ones
3893 // may no longer be valid.
3896 bvcur.setCursor(cur);
3897 bvcur.setSelection(true);
3900 // only update if selection changes
3901 if (bvcur.idx() == cur.idx() &&
3902 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3903 cur.noScreenUpdate();
3904 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3905 bvcur.setCursor(cur);
3906 bvcur.setSelection(true);
3907 // if this is a multicell selection, we just set the cursor to
3908 // the beginning of the cell's text.
3909 if (bvcur.selIsMultiCell()) {
3910 bvcur.pit() = bvcur.lastpit();
3911 bvcur.pos() = bvcur.lastpos();
3916 case LFUN_MOUSE_RELEASE:
3921 case LFUN_CELL_BACKWARD:
3923 cur.setSelection(false);
3926 case LFUN_CELL_FORWARD:
3928 cur.setSelection(false);
3931 case LFUN_CHAR_FORWARD_SELECT:
3932 case LFUN_CHAR_FORWARD:
3933 case LFUN_CHAR_BACKWARD_SELECT:
3934 case LFUN_CHAR_BACKWARD:
3935 case LFUN_CHAR_RIGHT_SELECT:
3936 case LFUN_CHAR_RIGHT:
3937 case LFUN_CHAR_LEFT_SELECT:
3938 case LFUN_CHAR_LEFT:
3939 case LFUN_WORD_FORWARD:
3940 case LFUN_WORD_FORWARD_SELECT:
3941 case LFUN_WORD_BACKWARD:
3942 case LFUN_WORD_BACKWARD_SELECT:
3943 case LFUN_WORD_RIGHT:
3944 case LFUN_WORD_RIGHT_SELECT:
3945 case LFUN_WORD_LEFT:
3946 case LFUN_WORD_LEFT_SELECT: {
3947 // determine whether we move to next or previous cell, where to enter
3948 // the new cell from, and which command to "finish" (i.e., exit the
3951 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3952 FuncCode finish_lfun;
3954 if (act == LFUN_CHAR_FORWARD
3955 || act == LFUN_CHAR_FORWARD_SELECT
3956 || act == LFUN_WORD_FORWARD
3957 || act == LFUN_WORD_FORWARD_SELECT) {
3959 finish_lfun = LFUN_FINISHED_FORWARD;
3961 else if (act == LFUN_CHAR_BACKWARD
3962 || act == LFUN_CHAR_BACKWARD_SELECT
3963 || act == LFUN_WORD_BACKWARD
3964 || act == LFUN_WORD_BACKWARD_SELECT) {
3966 finish_lfun = LFUN_FINISHED_BACKWARD;
3968 // LEFT or RIGHT commands --- the interpretation will depend on the
3969 // table's direction.
3971 bool const right = act == LFUN_CHAR_RIGHT
3972 || act == LFUN_CHAR_RIGHT_SELECT
3973 || act == LFUN_WORD_RIGHT
3974 || act == LFUN_WORD_RIGHT_SELECT;
3975 next_cell = isRightToLeft(cur) != right;
3977 if (lyxrc.visual_cursor)
3978 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3980 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3983 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3984 || act == LFUN_CHAR_BACKWARD_SELECT
3985 || act == LFUN_CHAR_RIGHT_SELECT
3986 || act == LFUN_CHAR_LEFT_SELECT
3987 || act == LFUN_WORD_FORWARD_SELECT
3988 || act == LFUN_WORD_RIGHT_SELECT
3989 || act == LFUN_WORD_BACKWARD_SELECT
3990 || act == LFUN_WORD_LEFT_SELECT;
3992 // If we have a multicell selection or we're
3993 // not doing some LFUN_*_SELECT thing anyway...
3994 if (!cur.selIsMultiCell() || !select) {
3995 col_type const c = tabular.cellColumn(cur.idx());
3996 row_type const r = tabular.cellRow(cur.idx());
3997 // Are we trying to select the whole cell and is the whole cell
3998 // not yet selected?
3999 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4000 ((next_cell && cur.pit() == cur.lastpit()
4001 && cur.pos() == cur.lastpos())
4002 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4004 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4006 // ...try to dispatch to the cell's inset.
4007 cell(cur.idx())->dispatch(cur, cmd);
4009 // When we already have a selection we want to select the whole cell
4010 // before going to the next cell.
4011 if (select_whole && !empty_cell){
4012 getText(cur.idx())->selectAll(cur);
4017 // FIXME: When we support the selection of an empty cell, remove
4018 // the !empty_cell from this condition. For now we jump to the next
4019 // cell if the current cell is empty.
4020 if (cur.result().dispatched() && !empty_cell)
4024 // move to next/prev cell, as appropriate
4025 // note that we will always do this if we're selecting and we have
4026 // a multicell selection
4027 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4028 << " cell from: " << int(entry_from));
4030 moveNextCell(cur, entry_from);
4032 movePrevCell(cur, entry_from);
4033 // if we're exiting the table, call the appropriate FINISHED lfun
4034 if (sl == cur.top())
4035 cmd = FuncRequest(finish_lfun);
4039 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4044 case LFUN_DOWN_SELECT:
4046 if (!(cur.selection() && cur.selIsMultiCell()))
4047 cell(cur.idx())->dispatch(cur, cmd);
4049 cur.dispatched(); // override the cell's decision
4050 if (sl == cur.top()) {
4051 // if our Text didn't do anything to the cursor
4052 // then we try to put the cursor into the cell below
4053 // setting also the right targetX.
4054 cur.selHandle(act == LFUN_DOWN_SELECT);
4055 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4056 int const xtarget = cur.targetX();
4057 // WARNING: Once cur.idx() has been reset, the cursor is in
4058 // an inconsistent state until pos() has been set. Be careful
4059 // what you do with it!
4060 cur.idx() = tabular.cellBelow(cur.idx());
4062 TextMetrics const & tm =
4063 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4064 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4065 cur.setCurrentFont();
4068 if (sl == cur.top()) {
4069 // we trick it to go to forward after leaving the
4071 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4074 if (cur.selIsMultiCell()) {
4075 cur.pit() = cur.lastpit();
4076 cur.pos() = cur.lastpos();
4077 cur.setCurrentFont();
4080 cur.screenUpdateFlags(Update::FitCursor);
4083 case LFUN_UP_SELECT:
4085 if (!(cur.selection() && cur.selIsMultiCell()))
4086 cell(cur.idx())->dispatch(cur, cmd);
4087 cur.dispatched(); // override the cell's decision
4088 if (sl == cur.top()) {
4089 // if our Text didn't do anything to the cursor
4090 // then we try to put the cursor into the cell above
4091 // setting also the right targetX.
4092 cur.selHandle(act == LFUN_UP_SELECT);
4093 if (tabular.cellRow(cur.idx()) != 0) {
4094 int const xtarget = cur.targetX();
4095 // WARNING: Once cur.idx() has been reset, the cursor is in
4096 // an inconsistent state until pos() has been set. Be careful
4097 // what you do with it!
4098 cur.idx() = tabular.cellAbove(cur.idx());
4099 cur.pit() = cur.lastpit();
4100 Text const * text = cell(cur.idx())->getText(0);
4101 TextMetrics const & tm = cur.bv().textMetrics(text);
4102 ParagraphMetrics const & pm =
4103 tm.parMetrics(cur.lastpit());
4104 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4105 cur.setCurrentFont();
4108 if (sl == cur.top()) {
4109 cmd = FuncRequest(LFUN_UP);
4112 if (cur.selIsMultiCell()) {
4114 cur.pos() = cur.lastpos();
4115 cur.setCurrentFont();
4118 cur.screenUpdateFlags(Update::FitCursor);
4121 // case LFUN_SCREEN_DOWN: {
4122 // //if (hasSelection())
4123 // // cur.selection() = false;
4124 // col_type const col = tabular.cellColumn(cur.idx());
4125 // int const t = cur.bv().top_y() + cur.bv().height();
4126 // if (t < yo() + tabular.getHeightOfTabular()) {
4127 // cur.bv().scrollDocView(t, true);
4128 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4130 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4137 // case LFUN_SCREEN_UP: {
4138 // //if (hasSelection())
4139 // // cur.selection() = false;
4140 // col_type const col = tabular.cellColumn(cur.idx());
4141 // int const t = cur.bv().top_y() + cur.bv().height();
4143 // cur.bv().scrollDocView(t, true);
4147 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4151 // cur.par() = cur.lastpar();
4152 // cur.pos() = cur.lastpos();
4156 case LFUN_LAYOUT_TABULAR:
4157 cur.bv().showDialog("tabular");
4160 case LFUN_INSET_MODIFY:
4161 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4165 // insert file functions
4166 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4167 case LFUN_FILE_INSERT_PLAINTEXT:
4169 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4170 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4171 FileName(to_utf8(cmd.argument())));
4174 cur.recordUndoInset(INSERT_UNDO);
4175 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4176 // content has been replaced,
4177 // so cursor might be invalid
4178 cur.pos() = cur.lastpos();
4179 cur.pit() = cur.lastpit();
4180 bvcur.setCursor(cur);
4187 if (cur.selIsMultiCell()) {
4188 if (copySelection(cur)) {
4189 cur.recordUndoInset(DELETE_UNDO);
4193 cell(cur.idx())->dispatch(cur, cmd);
4196 case LFUN_SELF_INSERT:
4197 if (cur.selIsMultiCell()) {
4198 cur.recordUndoInset(DELETE_UNDO);
4201 cell(cur.idx())->dispatch(cur, cmd);
4204 case LFUN_CHAR_DELETE_BACKWARD:
4205 case LFUN_CHAR_DELETE_FORWARD:
4206 if (cur.selIsMultiCell()) {
4207 cur.recordUndoInset(DELETE_UNDO);
4210 cell(cur.idx())->dispatch(cur, cmd);
4214 if (!cur.selection())
4216 if (cur.selIsMultiCell()) {
4220 cell(cur.idx())->dispatch(cur, cmd);
4223 case LFUN_CLIPBOARD_PASTE:
4224 case LFUN_PRIMARY_SELECTION_PASTE: {
4225 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4226 theClipboard().getAsText() :
4227 theSelection().get();
4230 // pass to InsertPlaintextString, but
4231 // only if we have multi-cell content
4232 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4233 cur.recordUndoInset(INSERT_UNDO);
4234 if (insertPlaintextString(cur.bv(), clip, false)) {
4235 // content has been replaced,
4236 // so cursor might be invalid
4237 cur.pos() = cur.lastpos();
4238 cur.pit() = cur.lastpit();
4239 bvcur.setCursor(cur);
4243 // Let the cell handle normal text
4244 cell(cur.idx())->dispatch(cur, cmd);
4249 if (!tabularStackDirty()) {
4250 if (!cur.selIsMultiCell())
4251 cell(cur.idx())->dispatch(cur, cmd);
4254 if (theClipboard().isInternal()) {
4255 cur.recordUndoInset(INSERT_UNDO);
4256 pasteClipboard(cur);
4260 case LFUN_FONT_EMPH:
4261 case LFUN_FONT_BOLD:
4262 case LFUN_FONT_BOLDSYMBOL:
4263 case LFUN_FONT_ROMAN:
4264 case LFUN_FONT_NOUN:
4265 case LFUN_FONT_ITAL:
4266 case LFUN_FONT_FRAK:
4267 case LFUN_FONT_TYPEWRITER:
4268 case LFUN_FONT_SANS:
4269 case LFUN_TEXTSTYLE_APPLY:
4270 case LFUN_TEXTSTYLE_UPDATE:
4271 case LFUN_FONT_SIZE:
4272 case LFUN_FONT_UNDERLINE:
4273 case LFUN_FONT_STRIKEOUT:
4274 case LFUN_FONT_UULINE:
4275 case LFUN_FONT_UWAVE:
4277 case LFUN_WORD_CAPITALIZE:
4278 case LFUN_WORD_UPCASE:
4279 case LFUN_WORD_LOWCASE:
4280 case LFUN_CHARS_TRANSPOSE:
4281 if (cur.selIsMultiCell()) {
4284 getSelection(cur, rs, re, cs, ce);
4285 Cursor tmpcur = cur;
4286 for (row_type r = rs; r <= re; ++r) {
4287 for (col_type c = cs; c <= ce; ++c) {
4288 // cursor follows cell:
4289 tmpcur.idx() = tabular.cellIndex(r, c);
4290 // select this cell only:
4293 tmpcur.resetAnchor();
4294 tmpcur.pit() = tmpcur.lastpit();
4295 tmpcur.pos() = tmpcur.top().lastpos();
4296 tmpcur.setCursor(tmpcur);
4297 tmpcur.setSelection();
4298 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4303 cell(cur.idx())->dispatch(cur, cmd);
4307 case LFUN_INSET_SETTINGS:
4308 // relay this lfun to Inset, not to the cell.
4309 Inset::doDispatch(cur, cmd);
4313 // we try to handle this event in the insets dispatch function.
4314 cell(cur.idx())->dispatch(cur, cmd);
4320 // function sets an object as defined in func_status.h:
4321 // states OK, Unknown, Disabled, On, Off.
4322 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4323 FuncStatus & status) const
4325 switch (cmd.action()) {
4326 case LFUN_INSET_MODIFY: {
4327 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4330 // FIXME: We only check for the very first argument...
4331 string const s = cmd.getArg(1);
4332 // We always enable the lfun if it is coming from the dialog
4333 // because the dialog makes sure all the settings are valid,
4334 // even though the first argument might not be valid now.
4335 if (s == "from-dialog") {
4336 status.setEnabled(true);
4340 int action = Tabular::LAST_ACTION;
4342 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4343 if (tabularFeature[i].feature == s) {
4344 action = tabularFeature[i].action;
4348 if (action == Tabular::LAST_ACTION) {
4350 status.setUnknown(true);
4354 string const argument = cmd.getLongArg(2);
4356 row_type sel_row_start = 0;
4357 row_type sel_row_end = 0;
4358 col_type sel_col_start = 0;
4359 col_type sel_col_end = 0;
4360 Tabular::ltType dummyltt;
4363 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4366 case Tabular::SET_PWIDTH:
4367 case Tabular::SET_MPWIDTH:
4368 case Tabular::SET_SPECIAL_COLUMN:
4369 case Tabular::SET_SPECIAL_MULTICOLUMN:
4370 case Tabular::APPEND_ROW:
4371 case Tabular::APPEND_COLUMN:
4372 case Tabular::DELETE_ROW:
4373 case Tabular::DELETE_COLUMN:
4374 case Tabular::COPY_ROW:
4375 case Tabular::COPY_COLUMN:
4376 case Tabular::SET_TOP_SPACE:
4377 case Tabular::SET_BOTTOM_SPACE:
4378 case Tabular::SET_INTERLINE_SPACE:
4382 case Tabular::SET_TABULAR_WIDTH:
4383 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4384 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4387 case Tabular::SET_DECIMAL_POINT:
4389 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4392 case Tabular::SET_MULTICOLUMN:
4393 case Tabular::UNSET_MULTICOLUMN:
4394 case Tabular::MULTICOLUMN:
4395 // If a row is set as longtable caption, it must not be allowed
4396 // to unset that this row is a multicolumn.
4397 status.setEnabled(sel_row_start == sel_row_end
4398 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4399 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4402 case Tabular::SET_MULTIROW:
4403 case Tabular::UNSET_MULTIROW:
4404 case Tabular::MULTIROW:
4405 // If a row is set as longtable caption, it must not be allowed
4406 // to unset that this row is a multirow.
4407 status.setEnabled(sel_col_start == sel_col_end
4408 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4409 status.setOnOff(tabular.isMultiRow(cur.idx()));
4412 case Tabular::SET_ALL_LINES:
4413 case Tabular::UNSET_ALL_LINES:
4414 case Tabular::SET_BORDER_LINES:
4415 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4418 case Tabular::SET_LINE_TOP:
4419 case Tabular::SET_LINE_BOTTOM:
4420 case Tabular::SET_LINE_LEFT:
4421 case Tabular::SET_LINE_RIGHT:
4422 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4425 case Tabular::TOGGLE_LINE_TOP:
4426 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4427 status.setOnOff(tabular.topLine(cur.idx()));
4430 case Tabular::TOGGLE_LINE_BOTTOM:
4431 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4432 status.setOnOff(tabular.bottomLine(cur.idx()));
4435 case Tabular::TOGGLE_LINE_LEFT:
4436 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4437 status.setOnOff(tabular.leftLine(cur.idx()));
4440 case Tabular::TOGGLE_LINE_RIGHT:
4441 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4442 status.setOnOff(tabular.rightLine(cur.idx()));
4445 // multirow cells only inherit the alignment of the column if the column has
4446 // no width, otherwise they are left-aligned
4447 // therefore allow always left but right and center only if there is no width
4448 case Tabular::M_ALIGN_LEFT:
4450 case Tabular::ALIGN_LEFT:
4451 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4454 case Tabular::M_ALIGN_RIGHT:
4456 case Tabular::ALIGN_RIGHT:
4457 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4458 && !tabular.getPWidth(cur.idx()).zero()));
4459 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4462 case Tabular::M_ALIGN_CENTER:
4464 case Tabular::ALIGN_CENTER:
4465 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4466 && !tabular.getPWidth(cur.idx()).zero()));
4467 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4470 case Tabular::ALIGN_BLOCK:
4471 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4472 && !tabular.isMultiRow(cur.idx()));
4473 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4476 case Tabular::ALIGN_DECIMAL:
4477 status.setEnabled(!tabular.isMultiRow(cur.idx())
4478 && !tabular.isMultiColumn(cur.idx()));
4479 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4482 case Tabular::M_VALIGN_TOP:
4484 case Tabular::VALIGN_TOP:
4485 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4486 && !tabular.isMultiRow(cur.idx()));
4488 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4491 case Tabular::M_VALIGN_BOTTOM:
4493 case Tabular::VALIGN_BOTTOM:
4494 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4495 && !tabular.isMultiRow(cur.idx()));
4497 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4500 case Tabular::M_VALIGN_MIDDLE:
4502 case Tabular::VALIGN_MIDDLE:
4503 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4504 && !tabular.isMultiRow(cur.idx()));
4506 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4509 case Tabular::SET_LONGTABULAR:
4510 // setting as longtable is not allowed when table is inside a float
4511 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4512 || cur.innerInsetOfType(WRAP_CODE) != 0)
4513 status.setEnabled(false);
4515 status.setEnabled(true);
4516 status.setOnOff(tabular.is_long_tabular);
4519 case Tabular::UNSET_LONGTABULAR:
4520 status.setOnOff(!tabular.is_long_tabular);
4523 case Tabular::TOGGLE_ROTATE_TABULAR:
4524 case Tabular::SET_ROTATE_TABULAR:
4525 status.setEnabled(tabular.tabular_width.zero());
4526 status.setOnOff(tabular.rotate);
4529 case Tabular::TABULAR_VALIGN_TOP:
4530 status.setEnabled(tabular.tabular_width.zero());
4531 status.setOnOff(tabular.tabular_valignment
4532 == Tabular::LYX_VALIGN_TOP);
4534 case Tabular::TABULAR_VALIGN_MIDDLE:
4535 status.setEnabled(tabular.tabular_width.zero());
4536 status.setOnOff(tabular.tabular_valignment
4537 == Tabular::LYX_VALIGN_MIDDLE);
4539 case Tabular::TABULAR_VALIGN_BOTTOM:
4540 status.setEnabled(tabular.tabular_width.zero());
4541 status.setOnOff(tabular.tabular_valignment
4542 == Tabular::LYX_VALIGN_BOTTOM);
4545 case Tabular::LONGTABULAR_ALIGN_LEFT:
4546 status.setOnOff(tabular.longtabular_alignment
4547 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4549 case Tabular::LONGTABULAR_ALIGN_CENTER:
4550 status.setOnOff(tabular.longtabular_alignment
4551 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4553 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4554 status.setOnOff(tabular.longtabular_alignment
4555 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4558 case Tabular::UNSET_ROTATE_TABULAR:
4559 status.setOnOff(!tabular.rotate);
4562 case Tabular::TOGGLE_ROTATE_CELL:
4563 case Tabular::SET_ROTATE_CELL:
4564 status.setOnOff(!oneCellHasRotationState(false,
4565 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4568 case Tabular::UNSET_ROTATE_CELL:
4569 status.setOnOff(!oneCellHasRotationState(true,
4570 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4573 case Tabular::SET_USEBOX:
4574 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4577 // every row can only be one thing:
4578 // either a footer or header or caption
4579 case Tabular::SET_LTFIRSTHEAD:
4580 status.setEnabled(sel_row_start == sel_row_end
4581 && !tabular.ltCaption(sel_row_start));
4582 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4585 case Tabular::UNSET_LTFIRSTHEAD:
4586 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4589 case Tabular::SET_LTHEAD:
4590 status.setEnabled(sel_row_start == sel_row_end
4591 && !tabular.ltCaption(sel_row_start));
4592 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4595 case Tabular::UNSET_LTHEAD:
4596 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4599 case Tabular::SET_LTFOOT:
4600 status.setEnabled(sel_row_start == sel_row_end
4601 && !tabular.ltCaption(sel_row_start));
4602 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4605 case Tabular::UNSET_LTFOOT:
4606 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4609 case Tabular::SET_LTLASTFOOT:
4610 status.setEnabled(sel_row_start == sel_row_end
4611 && !tabular.ltCaption(sel_row_start));
4612 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4615 case Tabular::UNSET_LTLASTFOOT:
4616 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4619 case Tabular::SET_LTNEWPAGE:
4620 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4623 // only one row can be the caption
4624 // and a multirow cannot be set as caption
4625 case Tabular::SET_LTCAPTION:
4626 case Tabular::UNSET_LTCAPTION:
4627 case Tabular::TOGGLE_LTCAPTION:
4628 status.setEnabled(sel_row_start == sel_row_end
4629 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4630 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4631 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4632 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4633 && (!tabular.haveLTCaption()
4634 || tabular.ltCaption(sel_row_start))
4635 && !tabular.isMultiRow(sel_row_start));
4636 status.setOnOff(tabular.ltCaption(sel_row_start));
4639 case Tabular::SET_BOOKTABS:
4640 status.setOnOff(tabular.use_booktabs);
4643 case Tabular::UNSET_BOOKTABS:
4644 status.setOnOff(!tabular.use_booktabs);
4649 status.setEnabled(false);
4655 // These are only enabled inside tabular
4656 case LFUN_CELL_BACKWARD:
4657 case LFUN_CELL_FORWARD:
4658 status.setEnabled(true);
4661 // disable these with multiple cells selected
4662 case LFUN_INSET_INSERT:
4663 case LFUN_TABULAR_INSERT:
4664 case LFUN_FLEX_INSERT:
4665 case LFUN_FLOAT_INSERT:
4666 case LFUN_FLOAT_WIDE_INSERT:
4667 case LFUN_FOOTNOTE_INSERT:
4668 case LFUN_MARGINALNOTE_INSERT:
4669 case LFUN_MATH_INSERT:
4670 case LFUN_MATH_MODE:
4671 case LFUN_MATH_MUTATE:
4672 case LFUN_MATH_DISPLAY:
4673 case LFUN_NOTE_INSERT:
4674 case LFUN_ARGUMENT_INSERT:
4675 case LFUN_BOX_INSERT:
4676 case LFUN_BRANCH_INSERT:
4677 case LFUN_PHANTOM_INSERT:
4678 case LFUN_WRAP_INSERT:
4679 case LFUN_PREVIEW_INSERT:
4680 case LFUN_ERT_INSERT: {
4681 if (cur.selIsMultiCell()) {
4682 status.setEnabled(false);
4685 return cell(cur.idx())->getStatus(cur, cmd, status);
4688 // disable in non-fixed-width cells
4689 case LFUN_BREAK_PARAGRAPH:
4690 // multirow does not allow paragraph breaks
4691 if (tabular.isMultiRow(cur.idx())) {
4692 status.setEnabled(false);
4695 case LFUN_NEWLINE_INSERT: {
4696 if (tabular.getPWidth(cur.idx()).zero()) {
4697 status.setEnabled(false);
4700 return cell(cur.idx())->getStatus(cur, cmd, status);
4703 case LFUN_NEWPAGE_INSERT:
4704 status.setEnabled(false);
4708 if (tabularStackDirty() && theClipboard().isInternal()) {
4709 if (cur.selIsMultiCell()) {
4712 getSelection(cur, rs, re, cs, ce);
4713 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4714 && paste_tabular->nrows() == re - rs + 1)
4715 status.setEnabled(true);
4717 status.setEnabled(false);
4718 status.message(_("Selection size should match clipboard content."));
4721 status.setEnabled(true);
4724 return cell(cur.idx())->getStatus(cur, cmd, status);
4726 case LFUN_INSET_SETTINGS:
4727 // relay this lfun to Inset, not to the cell.
4728 return Inset::getStatus(cur, cmd, status);
4731 // we try to handle this event in the insets dispatch function.
4732 return cell(cur.idx())->getStatus(cur, cmd, status);
4738 Inset::DisplayType InsetTabular::display() const
4740 if (tabular.is_long_tabular) {
4741 switch (tabular.longtabular_alignment) {
4742 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4744 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4746 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4756 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4758 tabular.latex(os, runparams);
4762 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4764 os << '\n'; // output table on a new line
4765 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4766 tabular.plaintext(os, runparams, dp, false, 0);
4767 return PLAINTEXT_NEWLINE;
4771 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4776 // FIXME: Why not pass a proper DocIterator here?
4778 // if the table is inside a float it doesn't need the informaltable
4779 // wrapper. Search for it.
4780 for (master = owner(); master; master = master->owner())
4781 if (master->lyxCode() == FLOAT_CODE)
4786 os << "<informaltable>";
4789 ret += tabular.docbook(os, runparams);
4791 os << "</informaltable>";
4798 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4800 return tabular.xhtml(xs, rp);
4804 void InsetTabular::validate(LaTeXFeatures & features) const
4806 tabular.validate(features);
4808 // It'd be better to be able to get this from an InsetLayout, but at present
4809 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4810 if (features.runparams().flavor == OutputParams::HTML)
4811 features.addPreambleSnippet("<style type=\"text/css\">\n"
4812 "table { border: 1px solid black; display: inline-block; }\n"
4813 "td { border: 1px solid black; padding: 0.5ex; }\n"
4818 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4820 return tabular.cellInset(idx);
4824 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4826 return tabular.cellInset(idx);
4830 void InsetTabular::cursorPos(BufferView const & bv,
4831 CursorSlice const & sl, bool boundary, int & x, int & y) const
4833 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4835 // y offset correction
4836 y += cellYPos(sl.idx());
4837 y += tabular.textVOffset(sl.idx());
4838 y += offset_valign_;
4840 // x offset correction
4841 x += cellXPos(sl.idx());
4842 x += tabular.textHOffset(sl.idx());
4843 x += ADD_TO_TABULAR_WIDTH;
4848 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4852 Inset const & inset = *tabular.cellInset(cell);
4853 Point o = bv.coordCache().getInsets().xy(&inset);
4854 int const xbeg = o.x_ - tabular.textHOffset(cell);
4855 int const xend = xbeg + tabular.cellWidth(cell);
4856 row_type const row = tabular.cellRow(cell);
4857 int const ybeg = o.y_ - tabular.rowAscent(row)
4858 - tabular.interRowSpace(row);
4859 int const yend = ybeg + tabular.cellHeight(cell);
4871 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4872 // << " ybeg=" << ybeg << " yend=" << yend
4873 // << " xx=" << xx << " yy=" << yy
4874 // << " dist=" << xx + yy << endl;
4879 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4881 //lyxerr << "InsetTabular::editXY: " << this << endl;
4882 cur.setSelection(false);
4884 cur.idx() = getNearestCell(cur.bv(), x, y);
4886 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4890 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4892 cur.idx() = getNearestCell(cur.bv(), x, y);
4893 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4897 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4899 idx_type idx_min = 0;
4900 int dist_min = numeric_limits<int>::max();
4901 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4902 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4903 int const d = dist(bv, i, x, y);
4914 int InsetTabular::cellYPos(idx_type const cell) const
4916 row_type row = tabular.cellRow(cell);
4918 for (row_type r = 0; r < row; ++r)
4919 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4920 + tabular.interRowSpace(r + 1);
4925 int InsetTabular::cellXPos(idx_type const cell) const
4927 col_type col = tabular.cellColumn(cell);
4929 for (col_type c = 0; c < col; ++c)
4930 lx += tabular.column_info[c].width;
4935 void InsetTabular::resetPos(Cursor & cur) const
4937 BufferView & bv = cur.bv();
4938 int const maxwidth = bv.workWidth();
4940 int const scx_old = scx_;
4941 int const i = cur.find(this);
4946 int const X2 = maxwidth;
4947 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4948 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4949 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4952 scx_ = X1 + 20 - x1;
4954 scx_ = X2 - 20 - x2;
4959 // only update if offset changed
4960 if (scx_ != scx_old)
4961 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4965 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4967 row_type const row = tabular.cellRow(cur.idx());
4968 col_type const col = tabular.cellColumn(cur.idx());
4970 if (isRightToLeft(cur)) {
4971 if (tabular.cellColumn(cur.idx()) == 0) {
4972 if (row == tabular.nrows() - 1)
4974 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4979 cur.idx() = tabular.getLastCellInRow(row - 1);
4981 cur.idx() = tabular.cellIndex(row, col - 1);
4984 if (tabular.isLastCell(cur.idx()))
4986 if (cur.idx() == tabular.getLastCellInRow(row))
4987 cur.idx() = tabular.cellIndex(row + 1, 0);
4989 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4990 cur.idx() = tabular.cellIndex(row, colnextcell);
4994 cur.boundary(false);
4996 if (cur.selIsMultiCell()) {
4997 cur.pit() = cur.lastpit();
4998 cur.pos() = cur.lastpos();
5006 // in visual mode, place cursor at extreme left or right
5008 switch(entry_from) {
5010 case ENTRY_DIRECTION_RIGHT:
5011 cur.posVisToRowExtremity(false /* !left */);
5013 case ENTRY_DIRECTION_LEFT:
5014 cur.posVisToRowExtremity(true /* left */);
5016 case ENTRY_DIRECTION_IGNORE:
5017 // nothing to do in this case
5021 cur.setCurrentFont();
5026 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5028 row_type const row = tabular.cellRow(cur.idx());
5029 col_type const col = tabular.cellColumn(cur.idx());
5031 if (isRightToLeft(cur)) {
5032 if (cur.idx() == tabular.getLastCellInRow(row)) {
5035 cur.idx() = tabular.getFirstCellInRow(row);
5036 cur.idx() = tabular.cellAbove(cur.idx());
5038 if (tabular.isLastCell(cur.idx()))
5040 if (cur.idx() == tabular.getLastCellInRow(row))
5041 cur.idx() = tabular.cellIndex(row + 1, 0);
5043 cur.idx() = tabular.cellIndex(row, col + 1);
5046 if (cur.idx() == 0) // first cell
5049 cur.idx() = tabular.getLastCellInRow(row - 1);
5051 cur.idx() = tabular.cellIndex(row, col - 1);
5054 if (cur.selIsMultiCell()) {
5055 cur.pit() = cur.lastpit();
5056 cur.pos() = cur.lastpos();
5061 cur.pit() = cur.lastpit();
5062 cur.pos() = cur.lastpos();
5064 // in visual mode, place cursor at extreme left or right
5066 switch(entry_from) {
5068 case ENTRY_DIRECTION_RIGHT:
5069 cur.posVisToRowExtremity(false /* !left */);
5071 case ENTRY_DIRECTION_LEFT:
5072 cur.posVisToRowExtremity(true /* left */);
5074 case ENTRY_DIRECTION_IGNORE:
5075 // nothing to do in this case
5079 cur.setCurrentFont();
5084 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5086 istringstream is(argument);
5089 if (insetCode(s) != TABULAR_CODE)
5093 size_t safe_guard = 0;
5098 if (safe_guard > 1000) {
5099 LYXERR0("parameter max count reached!");
5103 Tabular::Feature action = Tabular::LAST_ACTION;
5106 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5107 if (s != tabularFeature[i].feature)
5110 action = tabularFeature[i].action;
5113 if (action == Tabular::LAST_ACTION) {
5114 LYXERR0("Feature not found " << s);
5118 if (tabularFeature[i].need_value)
5120 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5121 tabularFeatures(cur, action, val);
5127 static void checkLongtableSpecial(Tabular::ltType & ltt,
5128 string const & special, bool & flag)
5130 if (special == "dl_above") {
5133 } else if (special == "dl_below") {
5134 ltt.bottomDL = flag;
5136 } else if (special == "empty") {
5146 bool InsetTabular::oneCellHasRotationState(bool rotated,
5147 row_type row_start, row_type row_end,
5148 col_type col_start, col_type col_end) const
5150 for (row_type r = row_start; r <= row_end; ++r)
5151 for (col_type c = col_start; c <= col_end; ++c)
5152 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5158 void InsetTabular::tabularFeatures(Cursor & cur,
5159 Tabular::Feature feature, string const & value)
5161 col_type sel_col_start;
5162 col_type sel_col_end;
5163 row_type sel_row_start;
5164 row_type sel_row_end;
5165 bool setLines = false;
5166 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5167 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5171 case Tabular::M_ALIGN_LEFT:
5172 case Tabular::ALIGN_LEFT:
5173 setAlign = LYX_ALIGN_LEFT;
5176 case Tabular::M_ALIGN_RIGHT:
5177 case Tabular::ALIGN_RIGHT:
5178 setAlign = LYX_ALIGN_RIGHT;
5181 case Tabular::M_ALIGN_CENTER:
5182 case Tabular::ALIGN_CENTER:
5183 setAlign = LYX_ALIGN_CENTER;
5186 case Tabular::ALIGN_BLOCK:
5187 setAlign = LYX_ALIGN_BLOCK;
5190 case Tabular::ALIGN_DECIMAL:
5191 setAlign = LYX_ALIGN_DECIMAL;
5194 case Tabular::M_VALIGN_TOP:
5195 case Tabular::VALIGN_TOP:
5196 setVAlign = Tabular::LYX_VALIGN_TOP;
5199 case Tabular::M_VALIGN_BOTTOM:
5200 case Tabular::VALIGN_BOTTOM:
5201 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5204 case Tabular::M_VALIGN_MIDDLE:
5205 case Tabular::VALIGN_MIDDLE:
5206 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5213 cur.recordUndoInset(ATOMIC_UNDO, this);
5215 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5216 row_type const row = tabular.cellRow(cur.idx());
5217 col_type const column = tabular.cellColumn(cur.idx());
5219 Tabular::ltType ltt;
5223 case Tabular::SET_TABULAR_WIDTH:
5224 tabular.setTabularWidth(Length(value));
5227 case Tabular::SET_PWIDTH: {
5228 Length const len(value);
5229 tabular.setColumnPWidth(cur, cur.idx(), len);
5231 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5232 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5236 case Tabular::SET_MPWIDTH:
5237 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5240 case Tabular::SET_MROFFSET:
5241 tabular.setMROffset(cur, cur.idx(), Length(value));
5244 case Tabular::SET_SPECIAL_COLUMN:
5245 case Tabular::SET_SPECIAL_MULTICOLUMN:
5246 if (value == "none")
5247 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5249 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5252 case Tabular::APPEND_ROW:
5253 // append the row into the tabular
5254 tabular.appendRow(cur.idx());
5257 case Tabular::APPEND_COLUMN:
5258 // append the column into the tabular
5259 tabular.appendColumn(cur.idx());
5260 cur.idx() = tabular.cellIndex(row, column);
5263 case Tabular::DELETE_ROW:
5264 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5265 tabular.deleteRow(sel_row_start);
5266 if (sel_row_start >= tabular.nrows())
5268 cur.idx() = tabular.cellIndex(sel_row_start, column);
5271 cur.setSelection(false);
5274 case Tabular::DELETE_COLUMN:
5275 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5276 tabular.deleteColumn(sel_col_start);
5277 if (sel_col_start >= tabular.ncols())
5279 cur.idx() = tabular.cellIndex(row, sel_col_start);
5282 cur.setSelection(false);
5285 case Tabular::COPY_ROW:
5286 tabular.copyRow(row);
5289 case Tabular::COPY_COLUMN:
5290 tabular.copyColumn(column);
5291 cur.idx() = tabular.cellIndex(row, column);
5294 case Tabular::SET_LINE_TOP:
5295 case Tabular::TOGGLE_LINE_TOP: {
5296 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5297 ? (value == "true") : !tabular.topLine(cur.idx());
5298 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5299 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5300 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5304 case Tabular::SET_LINE_BOTTOM:
5305 case Tabular::TOGGLE_LINE_BOTTOM: {
5306 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5307 ? (value == "true") : !tabular.bottomLine(cur.idx());
5308 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5309 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5310 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5314 case Tabular::SET_LINE_LEFT:
5315 case Tabular::TOGGLE_LINE_LEFT: {
5316 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5317 ? (value == "true") : !tabular.leftLine(cur.idx());
5318 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5319 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5320 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5324 case Tabular::SET_LINE_RIGHT:
5325 case Tabular::TOGGLE_LINE_RIGHT: {
5326 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5327 ? (value == "true") : !tabular.rightLine(cur.idx());
5328 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5329 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5330 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5334 case Tabular::M_ALIGN_LEFT:
5335 case Tabular::M_ALIGN_RIGHT:
5336 case Tabular::M_ALIGN_CENTER:
5337 case Tabular::ALIGN_LEFT:
5338 case Tabular::ALIGN_RIGHT:
5339 case Tabular::ALIGN_CENTER:
5340 case Tabular::ALIGN_BLOCK:
5341 case Tabular::ALIGN_DECIMAL:
5342 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5343 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5344 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5345 !tabular.getPWidth(c).zero());
5348 case Tabular::M_VALIGN_TOP:
5349 case Tabular::M_VALIGN_BOTTOM:
5350 case Tabular::M_VALIGN_MIDDLE:
5352 case Tabular::VALIGN_TOP:
5353 case Tabular::VALIGN_BOTTOM:
5354 case Tabular::VALIGN_MIDDLE:
5355 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5356 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5357 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5360 case Tabular::SET_MULTICOLUMN: {
5361 if (!cur.selection()) {
5362 // just multicol for one single cell
5363 // check whether we are completely in a multicol
5364 if (!tabular.isMultiColumn(cur.idx()))
5365 tabular.setMultiColumn(cur.idx(), 1);
5368 // we have a selection so this means we just add all this
5369 // cells to form a multicolumn cell
5370 idx_type const s_start = cur.selBegin().idx();
5371 row_type const col_start = tabular.cellColumn(s_start);
5372 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5373 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1);
5376 cur.setSelection(false);
5380 case Tabular::UNSET_MULTICOLUMN: {
5381 if (!cur.selection()) {
5382 if (tabular.isMultiColumn(cur.idx()))
5383 tabular.unsetMultiColumn(cur.idx());
5388 case Tabular::MULTICOLUMN: {
5389 if (tabular.isMultiColumn(cur.idx()))
5390 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5392 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5396 case Tabular::SET_MULTIROW: {
5397 if (!cur.selection()) {
5398 // just multirow for one single cell
5399 // check whether we are completely in a multirow
5400 if (!tabular.isMultiRow(cur.idx()))
5401 tabular.setMultiRow(cur.idx(), 1);
5404 // we have a selection so this means we just add all this
5405 // cells to form a multirow cell
5406 idx_type const s_start = cur.selBegin().idx();
5407 row_type const row_start = tabular.cellRow(s_start);
5408 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5409 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1);
5412 cur.setSelection(false);
5416 case Tabular::UNSET_MULTIROW: {
5417 if (!cur.selection()) {
5418 if (tabular.isMultiRow(cur.idx()))
5419 tabular.unsetMultiRow(cur.idx());
5424 case Tabular::MULTIROW: {
5425 if (tabular.isMultiRow(cur.idx()))
5426 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5428 tabularFeatures(cur, Tabular::SET_MULTIROW);
5432 case Tabular::SET_ALL_LINES:
5434 case Tabular::UNSET_ALL_LINES:
5435 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5436 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5437 idx_type const cell = tabular.cellIndex(r, c);
5438 tabular.setTopLine(cell, setLines);
5439 tabular.setBottomLine(cell, setLines);
5440 tabular.setRightLine(cell, setLines);
5441 tabular.setLeftLine(cell, setLines);
5445 case Tabular::SET_BORDER_LINES:
5446 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5447 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5448 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5450 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5451 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5452 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5456 case Tabular::SET_LONGTABULAR:
5457 tabular.is_long_tabular = true;
5460 case Tabular::UNSET_LONGTABULAR:
5461 for (row_type r = 0; r < tabular.nrows(); ++r) {
5462 if (tabular.ltCaption(r)) {
5463 cur.idx() = tabular.cellIndex(r, 0);
5466 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5469 tabular.is_long_tabular = false;
5472 case Tabular::SET_ROTATE_TABULAR:
5473 tabular.rotate = true;
5476 case Tabular::UNSET_ROTATE_TABULAR:
5477 tabular.rotate = false;
5480 case Tabular::TOGGLE_ROTATE_TABULAR:
5481 tabular.rotate = !tabular.rotate;
5484 case Tabular::TABULAR_VALIGN_TOP:
5485 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5488 case Tabular::TABULAR_VALIGN_MIDDLE:
5489 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5492 case Tabular::TABULAR_VALIGN_BOTTOM:
5493 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5496 case Tabular::LONGTABULAR_ALIGN_LEFT:
5497 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5500 case Tabular::LONGTABULAR_ALIGN_CENTER:
5501 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5504 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5505 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5510 case Tabular::SET_ROTATE_CELL:
5511 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5512 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5513 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5516 case Tabular::UNSET_ROTATE_CELL:
5517 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5518 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5519 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5522 case Tabular::TOGGLE_ROTATE_CELL:
5524 bool oneNotRotated = oneCellHasRotationState(false,
5525 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5527 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5528 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5529 tabular.setRotateCell(tabular.cellIndex(r, c),
5534 case Tabular::SET_USEBOX: {
5535 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5536 if (val == tabular.getUsebox(cur.idx()))
5537 val = Tabular::BOX_NONE;
5538 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5539 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5540 tabular.setUsebox(tabular.cellIndex(r, c), val);
5544 case Tabular::UNSET_LTFIRSTHEAD:
5546 case Tabular::SET_LTFIRSTHEAD:
5547 tabular.getRowOfLTFirstHead(row, ltt);
5548 checkLongtableSpecial(ltt, value, flag);
5549 tabular.setLTHead(row, flag, ltt, true);
5552 case Tabular::UNSET_LTHEAD:
5554 case Tabular::SET_LTHEAD:
5555 tabular.getRowOfLTHead(row, ltt);
5556 checkLongtableSpecial(ltt, value, flag);
5557 tabular.setLTHead(row, flag, ltt, false);
5560 case Tabular::UNSET_LTFOOT:
5562 case Tabular::SET_LTFOOT:
5563 tabular.getRowOfLTFoot(row, ltt);
5564 checkLongtableSpecial(ltt, value, flag);
5565 tabular.setLTFoot(row, flag, ltt, false);
5568 case Tabular::UNSET_LTLASTFOOT:
5570 case Tabular::SET_LTLASTFOOT:
5571 tabular.getRowOfLTLastFoot(row, ltt);
5572 checkLongtableSpecial(ltt, value, flag);
5573 tabular.setLTFoot(row, flag, ltt, true);
5576 case Tabular::SET_LTNEWPAGE:
5577 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5580 case Tabular::SET_LTCAPTION: {
5581 if (tabular.ltCaption(row))
5583 cur.idx() = tabular.setLTCaption(row, true);
5586 cur.setSelection(false);
5587 // If a row is set as caption, then also insert
5588 // a caption. Otherwise the LaTeX output is broken.
5589 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5590 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5594 case Tabular::UNSET_LTCAPTION: {
5595 if (!tabular.ltCaption(row))
5597 cur.idx() = tabular.setLTCaption(row, false);
5600 cur.setSelection(false);
5601 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5602 if (lyx::getStatus(fr).enabled())
5607 case Tabular::TOGGLE_LTCAPTION: {
5608 if (tabular.ltCaption(row))
5609 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5611 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5615 case Tabular::SET_BOOKTABS:
5616 tabular.use_booktabs = true;
5619 case Tabular::UNSET_BOOKTABS:
5620 tabular.use_booktabs = false;
5623 case Tabular::SET_TOP_SPACE: {
5625 if (value == "default")
5626 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5627 tabular.row_info[r].top_space_default = true;
5628 else if (value == "none")
5629 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5630 tabular.row_info[r].top_space_default = false;
5631 tabular.row_info[r].top_space = len;
5633 else if (isValidLength(value, &len))
5634 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5635 tabular.row_info[r].top_space_default = false;
5636 tabular.row_info[r].top_space = len;
5641 case Tabular::SET_BOTTOM_SPACE: {
5643 if (value == "default")
5644 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5645 tabular.row_info[r].bottom_space_default = true;
5646 else if (value == "none")
5647 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5648 tabular.row_info[r].bottom_space_default = false;
5649 tabular.row_info[r].bottom_space = len;
5651 else if (isValidLength(value, &len))
5652 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5653 tabular.row_info[r].bottom_space_default = false;
5654 tabular.row_info[r].bottom_space = len;
5659 case Tabular::SET_INTERLINE_SPACE: {
5661 if (value == "default")
5662 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5663 tabular.row_info[r].interline_space_default = true;
5664 else if (value == "none")
5665 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5666 tabular.row_info[r].interline_space_default = false;
5667 tabular.row_info[r].interline_space = len;
5669 else if (isValidLength(value, &len))
5670 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5671 tabular.row_info[r].interline_space_default = false;
5672 tabular.row_info[r].interline_space = len;
5677 case Tabular::SET_DECIMAL_POINT:
5678 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5679 tabular.column_info[c].decimal_point = from_utf8(value);
5682 // dummy stuff just to avoid warnings
5683 case Tabular::LAST_ACTION:
5689 bool InsetTabular::copySelection(Cursor & cur)
5691 if (!cur.selection())
5696 getSelection(cur, rs, re, cs, ce);
5698 paste_tabular.reset(new Tabular(tabular));
5700 for (row_type r = 0; r < rs; ++r)
5701 paste_tabular->deleteRow(0);
5703 row_type const rows = re - rs + 1;
5704 while (paste_tabular->nrows() > rows)
5705 paste_tabular->deleteRow(rows);
5707 for (col_type c = 0; c < cs; ++c)
5708 paste_tabular->deleteColumn(0);
5710 col_type const columns = ce - cs + 1;
5711 while (paste_tabular->ncols() > columns)
5712 paste_tabular->deleteColumn(columns);
5714 paste_tabular->setBuffer(tabular.buffer());
5716 odocstringstream os;
5717 OutputParams const runparams(0);
5718 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5719 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5720 cap::copySelection(cur, os.str());
5722 // mark tabular stack dirty
5723 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5724 // when we (hopefully) have a one-for-all paste mechanism.
5725 // This must be called after cap::copySelection.
5726 dirtyTabularStack(true);
5732 bool InsetTabular::pasteClipboard(Cursor & cur)
5736 col_type actcol = tabular.cellColumn(cur.idx());
5737 row_type actrow = tabular.cellRow(cur.idx());
5739 if (cur.selIsMultiCell()) {
5742 getSelection(cur, actrow, re, actcol, ce);
5745 for (row_type r1 = 0, r2 = actrow;
5746 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5748 for (col_type c1 = 0, c2 = actcol;
5749 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5751 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5752 tabular.isPartOfMultiColumn(r2, c2))
5754 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5758 if (tabular.isPartOfMultiColumn(r2, c2)) {
5762 shared_ptr<InsetTableCell> inset(
5763 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5764 tabular.setCellInset(r2, c2, inset);
5765 // FIXME?: why do we need to do this explicitly? (EL)
5766 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5768 // FIXME: change tracking (MG)
5769 inset->setChange(Change(buffer().params().trackChanges ?
5770 Change::INSERTED : Change::UNCHANGED));
5778 void InsetTabular::cutSelection(Cursor & cur)
5780 if (!cur.selection())
5785 getSelection(cur, rs, re, cs, ce);
5786 for (row_type r = rs; r <= re; ++r) {
5787 for (col_type c = cs; c <= ce; ++c) {
5788 shared_ptr<InsetTableCell> t
5789 = cell(tabular.cellIndex(r, c));
5790 if (buffer().params().trackChanges)
5791 // FIXME: Change tracking (MG)
5792 t->setChange(Change(Change::DELETED));
5798 // cursor position might be invalid now
5799 if (cur.pit() > cur.lastpit())
5800 cur.pit() = cur.lastpit();
5801 if (cur.pos() > cur.lastpos())
5802 cur.pos() = cur.lastpos();
5803 cur.clearSelection();
5807 bool InsetTabular::isRightToLeft(Cursor & cur) const
5809 LASSERT(cur.depth() > 1, /**/);
5810 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5811 pos_type const parentpos = cur[cur.depth() - 2].pos();
5812 return parentpar.getFontSettings(buffer().params(),
5813 parentpos).language()->rightToLeft();
5817 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5820 LASSERT(stidx <= enidx, return docstring());
5822 col_type const col1 = tabular.cellColumn(stidx);
5823 col_type const col2 = tabular.cellColumn(enidx);
5824 row_type const row1 = tabular.cellRow(stidx);
5825 row_type const row2 = tabular.cellRow(enidx);
5827 for (col_type col = col1; col <= col2; col++)
5828 for (row_type row = row1; row <= row2; row++) {
5833 retval += tabular.cellInset(row, col)->asString(intoInsets);
5839 void InsetTabular::getSelection(Cursor & cur,
5840 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5842 CursorSlice const & beg = cur.selBegin();
5843 CursorSlice const & end = cur.selEnd();
5844 cs = tabular.cellColumn(beg.idx());
5845 ce = tabular.cellColumn(end.idx());
5849 rs = tabular.cellRow(beg.idx());
5850 re = tabular.cellRow(end.idx());
5856 Text * InsetTabular::getText(int idx) const
5858 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5862 void InsetTabular::setChange(Change const & change)
5864 for (idx_type idx = 0; idx < nargs(); ++idx)
5865 cell(idx)->setChange(change);
5869 void InsetTabular::acceptChanges()
5871 for (idx_type idx = 0; idx < nargs(); ++idx)
5872 cell(idx)->acceptChanges();
5876 void InsetTabular::rejectChanges()
5878 for (idx_type idx = 0; idx < nargs(); ++idx)
5879 cell(idx)->rejectChanges();
5883 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5885 return tabular.getPWidth(cell).zero();
5889 bool InsetTabular::forcePlainLayout(idx_type cell) const
5891 return !tabular.getPWidth(cell).zero();
5895 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5898 if (buf.length() <= 0)
5903 col_type maxCols = 1;
5904 size_t const len = buf.length();
5908 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5916 maxCols = max(cols, maxCols);
5922 maxCols = max(cols, maxCols);
5928 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5929 loctab = paste_tabular.get();
5931 dirtyTabularStack(true);
5934 cell = bv.cursor().idx();
5935 ocol = tabular.cellColumn(cell);
5936 row = tabular.cellRow(cell);
5940 idx_type const cells = loctab->numberofcells;
5943 rows = loctab->nrows();
5944 col_type const columns = loctab->ncols();
5946 while (cell < cells && p < len && row < rows &&
5947 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5953 // we can only set this if we are not too far right
5954 if (cols < columns) {
5955 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5956 Font const font = bv.textMetrics(&inset->text()).
5958 inset->setText(buf.substr(op, p - op), font,
5959 buffer().params().trackChanges);
5965 // we can only set this if we are not too far right
5966 if (cols < columns) {
5967 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5968 Font const font = bv.textMetrics(&inset->text()).
5970 inset->setText(buf.substr(op, p - op), font,
5971 buffer().params().trackChanges);
5976 cell = loctab->cellIndex(row, cols);
5982 // check for the last cell if there is no trailing '\n'
5983 if (cell < cells && op < len) {
5984 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5985 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5986 inset->setText(buf.substr(op, len - op), font,
5987 buffer().params().trackChanges);
5993 void InsetTabular::addPreview(DocIterator const & inset_pos,
5994 PreviewLoader & loader) const
5996 DocIterator cell_pos = inset_pos;
5998 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5999 for (row_type r = 0; r < tabular.nrows(); ++r) {
6000 for (col_type c = 0; c < tabular.ncols(); ++c) {
6001 cell_pos.top().idx() = tabular.cellIndex(r, c);
6002 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6008 bool InsetTabular::completionSupported(Cursor const & cur) const
6010 Cursor const & bvCur = cur.bv().cursor();
6011 if (&bvCur.inset() != this)
6013 return cur.text()->completionSupported(cur);
6017 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6019 return completionSupported(cur);
6023 bool InsetTabular::automaticInlineCompletion() const
6025 return lyxrc.completion_inline_text;
6029 bool InsetTabular::automaticPopupCompletion() const
6031 return lyxrc.completion_popup_text;
6035 bool InsetTabular::showCompletionCursor() const
6037 return lyxrc.completion_cursor_text;
6041 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6043 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6047 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6049 if (!completionSupported(cur))
6051 return cur.text()->completionPrefix(cur);
6055 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6057 if (!completionSupported(cur))
6060 return cur.text()->insertCompletion(cur, s, finished);
6064 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6065 Dimension & dim) const
6067 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6068 tm.completionPosAndDim(cur, x, y, dim);
6072 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6074 istringstream data(in);
6076 lex.setStream(data);
6083 if (!lex || token != "tabular") {
6084 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6088 // This is part of the inset proper that is usually swallowed
6089 // by Buffer::readInset
6091 if (!lex || token != "Tabular") {
6092 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6100 string InsetTabular::params2string(InsetTabular const & inset)
6103 data << "tabular" << ' ';
6105 data << "\\end_inset\n";