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,
1619 bool const right_border)
1621 idx_type const col = cellColumn(cell);
1622 idx_type const row = cellRow(cell);
1623 for (idx_type i = 0; i < number; ++i)
1624 unsetMultiRow(cellIndex(row, col + i));
1626 // unsetting of multirow may have invalidated cell index
1627 cell = cellIndex(row, col);
1628 CellData & cs = cellInfo(cell);
1629 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1630 if (column_info[col].alignment != LYX_ALIGN_DECIMAL)
1631 cs.alignment = column_info[col].alignment;
1633 setRightLine(cell, right_border);
1635 for (idx_type i = 1; i < number; ++i) {
1636 CellData & cs1 = cellInfo(cell + i);
1637 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1638 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1646 bool Tabular::isMultiRow(idx_type cell) const
1648 return (cellInfo(cell).multirow == CELL_BEGIN_OF_MULTIROW
1649 || cellInfo(cell).multirow == CELL_PART_OF_MULTIROW);
1653 Tabular::idx_type Tabular::setMultiRow(idx_type cell, idx_type number,
1654 bool const bottom_border)
1656 idx_type const col = cellColumn(cell);
1657 idx_type const row = cellRow(cell);
1658 for (idx_type i = 0; i < number; ++i)
1659 unsetMultiColumn(cellIndex(row + i, col));
1661 // unsetting of multirow may have invalidated cell index
1662 cell = cellIndex(row, col);
1663 CellData & cs = cellInfo(cell);
1664 cs.multirow = CELL_BEGIN_OF_MULTIROW;
1665 cs.valignment = LYX_VALIGN_MIDDLE;
1666 // the horizontal alignment of multirow cells can only
1667 // be changed for the whole table row,
1668 // support changing this only for the multirow cell can be done via
1670 // this feature would be a fileformat change
1671 // until LyX supports this, use the deault alignment of multirow
1673 cs.alignment = LYX_ALIGN_LEFT;
1675 // set the bottom line of the last selected cell
1676 setBottomLine(cell, bottom_border);
1678 for (idx_type i = 1; i < number; ++i) {
1679 CellData & cs1 = cell_info[row + i][col];
1680 cs1.multirow = CELL_PART_OF_MULTIROW;
1681 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1689 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1691 row_type const row = cellRow(cell);
1692 col_type const col = cellColumn(cell);
1694 while (col + span < ncols() && isPartOfMultiColumn(row, col + span))
1701 Tabular::idx_type Tabular::rowSpan(idx_type cell) const
1703 col_type const column = cellColumn(cell);
1704 col_type row = cellRow(cell) + 1;
1705 while (row < nrows() && isPartOfMultiRow(row, column))
1708 return row - cellRow(cell);
1712 void Tabular::unsetMultiColumn(idx_type cell)
1714 if (!isMultiColumn(cell))
1717 row_type const row = cellRow(cell);
1718 col_type const col = cellColumn(cell);
1719 row_type const span = columnSpan(cell);
1720 for (col_type c = 0; c < span; ++c) {
1721 // in the table dialog the lines are set in every case
1722 // when unsetting a multicolumn this leads to an additional right
1723 // line for every cell that was part of the former multicolumn cell,
1724 // except if the cell is in the last column
1725 // therefore remove this line
1726 if (cell_info[row][col + c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN
1727 && (col + c) < (col + span - 1))
1728 cell_info[row][col + c].right_line = false;
1729 cell_info[row][col + c].multicolumn = CELL_NORMAL;
1735 void Tabular::unsetMultiRow(idx_type cell)
1737 if (!isMultiRow(cell))
1740 cellInfo(cell).valignment = LYX_VALIGN_TOP;
1741 cellInfo(cell).alignment = LYX_ALIGN_CENTER;
1742 row_type const row = cellRow(cell);
1743 col_type const col = cellColumn(cell);
1744 row_type const span = rowSpan(cell);
1745 for (row_type r = 0; r < span; ++r)
1746 cell_info[row + r][col].multirow = CELL_NORMAL;
1751 void Tabular::setRotateCell(idx_type cell, bool flag)
1753 cellInfo(cell).rotate = flag;
1757 bool Tabular::getRotateCell(idx_type cell) const
1759 return cellInfo(cell).rotate;
1763 bool Tabular::needRotating() const
1767 for (row_type r = 0; r < nrows(); ++r)
1768 for (col_type c = 0; c < ncols(); ++c)
1769 if (cell_info[r][c].rotate)
1775 bool Tabular::isLastCell(idx_type cell) const
1777 if (cell + 1 < numberofcells)
1783 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1785 if (cellRow(cell) == 0)
1788 col_type const col = cellColumn(cell);
1789 row_type r = cellRow(cell) - 1;
1790 while (r > 0 && cell_info[r][col].multirow == CELL_PART_OF_MULTIROW)
1793 return cell_info[r][col].cellno;
1797 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1799 row_type const nextrow = cellRow(cell) + rowSpan(cell);
1800 if (nextrow < nrows())
1801 return cell_info[nextrow][cellColumn(cell)].cellno;
1806 Tabular::idx_type Tabular::cellIndex(row_type row, col_type column) const
1808 LASSERT(column != npos && column < ncols()
1809 && row != npos && row < nrows(), /**/);
1810 return cell_info[row][column].cellno;
1814 void Tabular::setUsebox(idx_type cell, BoxType type)
1816 cellInfo(cell).usebox = type;
1820 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1821 // adds line breaks, see bug 4886.
1822 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1824 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1825 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1827 if (cellInfo(cell).usebox > 1)
1828 return cellInfo(cell).usebox;
1829 return useParbox(cell);
1834 // This are functions used for the longtable support
1836 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1842 row_info[row].endfirsthead = flag;
1846 row_info[row].endhead = flag;
1851 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1854 hd.set = haveLTHead();
1855 return row_info[row].endhead;
1859 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1862 hd.set = haveLTFirstHead();
1863 return row_info[row].endfirsthead;
1867 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1873 row_info[row].endlastfoot = flag;
1877 row_info[row].endfoot = flag;
1882 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1885 fd.set = haveLTFoot();
1886 return row_info[row].endfoot;
1890 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1893 fd.set = haveLTLastFoot();
1894 return row_info[row].endlastfoot;
1898 void Tabular::setLTNewPage(row_type row, bool what)
1900 row_info[row].newpage = what;
1904 bool Tabular::getLTNewPage(row_type row) const
1906 return row_info[row].newpage;
1910 bool Tabular::haveLTHead() const
1912 if (!is_long_tabular)
1914 for (row_type i = 0; i < nrows(); ++i)
1915 if (row_info[i].endhead)
1921 bool Tabular::haveLTFirstHead() const
1923 if (!is_long_tabular || endfirsthead.empty)
1925 for (row_type r = 0; r < nrows(); ++r)
1926 if (row_info[r].endfirsthead)
1932 bool Tabular::haveLTFoot() const
1934 if (!is_long_tabular)
1936 for (row_type r = 0; r < nrows(); ++r)
1937 if (row_info[r].endfoot)
1943 bool Tabular::haveLTLastFoot() const
1945 if (!is_long_tabular || endlastfoot.empty)
1947 for (row_type r = 0; r < nrows(); ++r)
1948 if (row_info[r].endlastfoot)
1954 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1956 idx_type i = getFirstCellInRow(row);
1958 setMultiColumn(i, numberOfCellsInRow(row), false);
1959 setTopLine(i, false);
1960 setBottomLine(i, false);
1961 setLeftLine(i, false);
1962 setRightLine(i, false);
1964 unsetMultiColumn(i);
1965 // When unsetting a caption row, also all existing
1966 // captions in this row must be dissolved.
1968 row_info[row].caption = what;
1973 bool Tabular::ltCaption(row_type row) const
1975 return row_info[row].caption;
1979 bool Tabular::haveLTCaption() const
1981 if (!is_long_tabular)
1983 for (row_type r = 0; r < nrows(); ++r)
1984 if (row_info[r].caption)
1990 // end longtable support functions
1992 void Tabular::setRowAscent(row_type row, int height)
1994 if (row >= nrows() || row_info[row].ascent == height)
1996 row_info[row].ascent = height;
2000 void Tabular::setRowDescent(row_type row, int height)
2002 if (row >= nrows() || row_info[row].descent == height)
2004 row_info[row].descent = height;
2008 int Tabular::rowAscent(row_type row) const
2010 LASSERT(row < nrows(), /**/);
2011 return row_info[row].ascent;
2015 int Tabular::rowDescent(row_type row) const
2017 LASSERT(row < nrows(), /**/);
2018 return row_info[row].descent;
2022 int Tabular::height() const
2025 for (row_type row = 0; row < nrows(); ++row)
2026 height += rowAscent(row) + rowDescent(row) +
2032 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
2034 LASSERT(row < nrows(), /**/);
2035 LASSERT(column < ncols(), /**/);
2036 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
2040 bool Tabular::isPartOfMultiRow(row_type row, col_type column) const
2042 LASSERT(row < nrows(), /**/);
2043 LASSERT(column < ncols(), /**/);
2044 return cell_info[row][column].multirow == CELL_PART_OF_MULTIROW;
2048 void Tabular::TeXTopHLine(otexstream & os, row_type row, string const lang) const
2050 // we only output complete row lines and the 1st row here, the rest
2051 // is done in Tabular::TeXBottomHLine(...)
2053 // get for each column the topline (if any)
2054 vector<bool> topline;
2056 for (col_type c = 0; c < ncols(); ++c) {
2057 topline.push_back(topLine(cellIndex(row, c)));
2058 // If cell is part of a multirow and not the first cell of the
2059 // multirow, no line must be drawn.
2061 if (isMultiRow(cellIndex(row, c))
2062 && cell_info[row][c].multirow != CELL_BEGIN_OF_MULTIROW)
2068 // do nothing if empty first row, or incomplete row line after
2069 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols()))
2072 // only output complete row lines and the 1st row's clines
2073 if (nset == ncols()) {
2075 os << (row == 0 ? "\\toprule " : "\\midrule ");
2079 } else if (row == 0) {
2080 for (col_type c = 0; c < ncols(); ++c) {
2082 col_type offset = 0;
2083 for (col_type j = 0 ; j < c; ++j)
2084 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2087 //babel makes the "-" character an active one, so we have to suppress this here
2088 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2089 if (lang == "slovak" || lang == "czech")
2090 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2091 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2093 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2095 col_type cstart = c;
2096 for ( ; c < ncols() && topline[c]; ++c) {}
2098 for (col_type j = cstart ; j < c ; ++j)
2099 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2102 os << c + offset << "} ";
2110 void Tabular::TeXBottomHLine(otexstream & os, row_type row, string const lang) const
2112 // we output bottomlines of row r and the toplines of row r+1
2113 // if the latter do not span the whole tabular
2115 // get the bottomlines of row r, and toplines in next row
2116 bool lastrow = row == nrows() - 1;
2117 vector<bool> bottomline, topline;
2118 bool nextrowset = true;
2119 for (col_type c = 0; c < ncols(); ++c) {
2120 bottomline.push_back(bottomLine(cellIndex(row, c)));
2121 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
2122 // If cell is part of a multirow and not the last cell of the
2123 // multirow, no line must be drawn.
2125 if (isMultiRow(cellIndex(row, c))
2126 && isMultiRow(cellIndex(row + 1, c))
2127 && cell_info[row + 1][c].multirow != CELL_BEGIN_OF_MULTIROW) {
2128 bottomline[c] = false;
2131 nextrowset &= topline[c];
2134 // combine this row's bottom lines and next row's toplines if necessary
2136 for (col_type c = 0; c < ncols(); ++c) {
2138 bottomline[c] = bottomline[c] || topline[c];
2143 // do nothing if empty, OR incomplete row line with a topline in next row
2144 if (nset == 0 || (nextrowset && nset != ncols()))
2147 if (nset == ncols()) {
2149 os << (lastrow ? "\\bottomrule" : "\\midrule");
2153 for (col_type c = 0; c < ncols(); ++c) {
2154 if (bottomline[c]) {
2155 col_type offset = 0;
2156 for (col_type j = 0 ; j < c; ++j)
2157 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2160 //babel makes the "-" character an active one, so we have to suppress this here
2161 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
2162 if (lang == "slovak" || lang == "czech")
2163 os << "\\expandafter" << (use_booktabs ? "\\cmidrule" : "\\cline")
2164 << "\\expandafter{\\expandafter" << c + 1 + offset << "\\string-";
2166 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 + offset << '-';
2168 col_type cstart = c;
2169 for ( ; c < ncols() && bottomline[c]; ++c) {}
2171 for (col_type j = cstart ; j < c ; ++j)
2172 if (column_info[j].alignment == LYX_ALIGN_DECIMAL)
2175 os << c + offset << "} ";
2183 void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
2184 bool & ismulticol, bool & ismultirow) const
2186 row_type const r = cellRow(cell);
2187 if (is_long_tabular && row_info[r].caption)
2190 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
2191 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
2192 // figure out how to set the lines
2193 // we always set double lines to the right of the cell
2194 col_type const c = cellColumn(cell);
2195 col_type const nextcol = c + columnSpan(cell);
2196 bool colright = columnRightLine(c);
2197 bool colleft = columnLeftLine(c);
2198 bool nextcolleft = nextcol < ncols() && columnLeftLine(nextcol);
2199 bool nextcellleft = nextcol < ncols()
2200 && leftLine(cellIndex(r, nextcol));
2201 bool coldouble = colright && nextcolleft;
2202 bool celldouble = rightLine(cell) && nextcellleft;
2204 ismulticol = isMultiColumn(cell)
2205 || (c == 0 && colleft != leftLine(cell))
2206 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2207 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2208 || (coldouble != celldouble);
2210 // we center in multicol when no decimal point
2211 if (column_info[c].alignment == LYX_ALIGN_DECIMAL) {
2212 docstring const align_d = column_info[c].decimal_point;
2213 DocIterator const dit = separatorPos(cellInset(cell).get(), align_d);
2217 // up counter by 1 for each decimally aligned col since they use 2 latex cols
2218 int latexcolspan = columnSpan(cell);
2219 for(col_type col = c; col < c + columnSpan(cell); ++col)
2220 if (column_info[col].alignment == LYX_ALIGN_DECIMAL)
2224 os << "\\multicolumn{" << latexcolspan << "}{";
2225 if (c ==0 && leftLine(cell))
2227 if (!cellInfo(cell).align_special.empty()) {
2228 os << cellInfo(cell).align_special;
2230 if (!getPWidth(cell).zero()) {
2232 case LYX_ALIGN_LEFT:
2233 os << ">{\\raggedright}";
2235 case LYX_ALIGN_RIGHT:
2236 os << ">{\\raggedleft}";
2238 case LYX_ALIGN_CENTER:
2239 os << ">{\\centering}";
2245 case LYX_VALIGN_TOP:
2248 case LYX_VALIGN_MIDDLE:
2251 case LYX_VALIGN_BOTTOM:
2256 << from_ascii(getPWidth(cell).asLatexString())
2260 case LYX_ALIGN_LEFT:
2263 case LYX_ALIGN_RIGHT:
2270 } // end if else !getPWidth
2271 } // end if else !cellinfo_of_cell
2272 if (rightLine(cell) || nextcellleft)
2275 // add extra vertical line if we want a double one
2278 } // end if ismulticol
2280 // we only need code for the first multirow cell
2281 ismultirow = isMultiRow(cell);
2283 os << "\\multirow{" << rowSpan(cell) << "}{";
2284 if (!getPWidth(cell).zero())
2285 os << from_ascii(getPWidth(cell).asLatexString());
2287 // we need to set a default value
2290 if (!getMROffset(cell).zero())
2291 os << "[" << from_ascii(getMROffset(cell).asLatexString()) << "]";
2293 } // end if ismultirow
2295 if (getRotateCell(cell)) {
2296 os << "\\begin{sideways}\n";
2298 if (getUsebox(cell) == BOX_PARBOX) {
2301 case LYX_VALIGN_TOP:
2304 case LYX_VALIGN_MIDDLE:
2307 case LYX_VALIGN_BOTTOM:
2311 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2313 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2314 os << "\\begin{minipage}[";
2316 case LYX_VALIGN_TOP:
2319 case LYX_VALIGN_MIDDLE:
2322 case LYX_VALIGN_BOTTOM:
2326 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2332 void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
2333 bool ismulticol, bool ismultirow) const
2335 row_type const r = cellRow(cell);
2336 if (is_long_tabular && row_info[r].caption)
2340 if (getUsebox(cell) == BOX_PARBOX)
2342 else if (getUsebox(cell) == BOX_MINIPAGE)
2343 os << breakln << "\\end{minipage}";
2344 if (getRotateCell(cell))
2345 os << breakln << "\\end{sideways}";
2353 void Tabular::TeXLongtableHeaderFooter(otexstream & os,
2354 OutputParams const & runparams) const
2356 if (!is_long_tabular)
2360 // the caption must be output before the headers
2361 if (haveLTCaption()) {
2362 for (row_type r = 0; r < nrows(); ++r) {
2363 if (row_info[r].caption)
2364 TeXRow(os, r, runparams);
2367 // output first header info
2368 // first header must be output before the header, otherwise the
2369 // correct caption placement becomes really weird
2370 if (haveLTFirstHead()) {
2371 if (endfirsthead.topDL)
2373 for (row_type r = 0; r < nrows(); ++r) {
2374 if (row_info[r].endfirsthead)
2375 TeXRow(os, r, runparams);
2377 if (endfirsthead.bottomDL)
2379 os << "\\endfirsthead\n";
2381 // output header info
2383 if (endfirsthead.empty && !haveLTFirstHead())
2384 os << "\\endfirsthead\n";
2387 for (row_type r = 0; r < nrows(); ++r) {
2388 if (row_info[r].endhead)
2389 TeXRow(os, r, runparams);
2391 if (endhead.bottomDL)
2393 os << "\\endhead\n";
2395 // output footer info
2399 for (row_type r = 0; r < nrows(); ++r) {
2400 if (row_info[r].endfoot)
2401 TeXRow(os, r, runparams);
2403 if (endfoot.bottomDL)
2405 os << "\\endfoot\n";
2406 if (endlastfoot.empty && !haveLTLastFoot())
2407 os << "\\endlastfoot\n";
2409 // output lastfooter info
2410 if (haveLTLastFoot()) {
2411 if (endlastfoot.topDL)
2413 for (row_type r = 0; r < nrows(); ++r) {
2414 if (row_info[r].endlastfoot)
2415 TeXRow(os, r, runparams);
2417 if (endlastfoot.bottomDL)
2419 os << "\\endlastfoot\n";
2424 bool Tabular::isValidRow(row_type row) const
2426 if (!is_long_tabular)
2428 return !row_info[row].endhead && !row_info[row].endfirsthead
2429 && !row_info[row].endfoot && !row_info[row].endlastfoot
2430 && !row_info[row].caption;
2434 void Tabular::TeXRow(otexstream & os, row_type row,
2435 OutputParams const & runparams) const
2437 idx_type cell = cellIndex(row, 0);
2438 shared_ptr<InsetTableCell> inset = cellInset(cell);
2439 Paragraph const & par = inset->paragraphs().front();
2440 string const lang = par.getParLanguage(buffer().params())->lang();
2442 //output the top line
2443 TeXTopHLine(os, row, lang);
2445 if (row_info[row].top_space_default) {
2447 os << "\\addlinespace\n";
2449 os << "\\noalign{\\vskip\\doublerulesep}\n";
2450 } else if(!row_info[row].top_space.zero()) {
2452 os << "\\addlinespace["
2453 << from_ascii(row_info[row].top_space.asLatexString())
2456 os << "\\noalign{\\vskip"
2457 << from_ascii(row_info[row].top_space.asLatexString())
2461 bool ismulticol = false;
2462 bool ismultirow = false;
2463 for (col_type c = 0; c < ncols(); ++c) {
2464 if (isPartOfMultiColumn(row, c))
2467 cell = cellIndex(row, c);
2469 if (isPartOfMultiRow(row, c)
2470 && column_info[c].alignment != LYX_ALIGN_DECIMAL) {
2471 if (cell != getLastCellInRow(row))
2476 TeXCellPreamble(os, cell, ismulticol, ismultirow);
2477 shared_ptr<InsetTableCell> inset = cellInset(cell);
2479 Paragraph const & par = inset->paragraphs().front();
2480 bool rtl = par.isRTL(buffer().params())
2482 && getPWidth(cell).zero();
2486 par.getParLanguage(buffer().params())->lang();
2487 if (lang == "farsi")
2489 else if (lang == "arabic_arabi")
2491 // currently, remaning RTL languages are
2492 // arabic_arabtex and hebrew
2496 // pass to the OutputParams that we are in a cell and
2497 // which alignment we have set.
2498 // InsetNewline needs this context information.
2499 OutputParams newrp(runparams);
2500 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2501 ? OutputParams::PLAIN
2502 : OutputParams::ALIGNED;
2504 if (getAlignment(cell) == LYX_ALIGN_DECIMAL) {
2505 // copy cell and split in 2
2506 InsetTableCell head = InsetTableCell(*cellInset(cell).get());
2507 head.setBuffer(buffer());
2508 DocIterator dit = cellInset(cell)->getText(0)->macrocontextPosition();
2510 dit.push_back(CursorSlice(head));
2511 head.setMacrocontextPositionRecursive(dit);
2512 bool hassep = false;
2513 InsetTableCell tail = splitCell(head, column_info[c].decimal_point, hassep);
2514 head.latex(os, newrp);
2517 tail.setBuffer(head.buffer());
2519 dit.push_back(CursorSlice(tail));
2520 tail.setMacrocontextPositionRecursive(dit);
2521 tail.latex(os, newrp);
2523 } else if (!isPartOfMultiRow(row, c)) {
2524 if (!runparams.nice)
2525 os.texrow().start(par.id(), 0);
2526 inset->latex(os, newrp);
2529 runparams.encoding = newrp.encoding;
2533 TeXCellPostamble(os, cell, ismulticol, ismultirow);
2534 if (cell != getLastCellInRow(row)) { // not last cell in row
2541 if (row_info[row].caption && !endfirsthead.empty && !haveLTFirstHead())
2542 // if no first header and no empty first header is used,
2543 // the caption needs to be terminated by \endfirsthead
2545 os << "\\endfirsthead";
2547 os << "\\tabularnewline";
2548 if (row_info[row].bottom_space_default) {
2550 os << "\\addlinespace";
2552 os << "[\\doublerulesep]";
2553 } else if (!row_info[row].bottom_space.zero()) {
2555 os << "\\addlinespace";
2557 << from_ascii(row_info[row].bottom_space.asLatexString())
2562 //output the bottom line
2563 TeXBottomHLine(os, row, lang);
2565 if (row_info[row].interline_space_default) {
2567 os << "\\addlinespace\n";
2569 os << "\\noalign{\\vskip\\doublerulesep}\n";
2570 } else if (!row_info[row].interline_space.zero()) {
2572 os << "\\addlinespace["
2573 << from_ascii(row_info[row].interline_space.asLatexString())
2576 os << "\\noalign{\\vskip"
2577 << from_ascii(row_info[row].interline_space.asLatexString())
2583 void Tabular::latex(otexstream & os, OutputParams const & runparams) const
2585 bool const is_tabular_star = !tabular_width.zero();
2587 //+---------------------------------------------------------------------
2588 //+ first the opening preamble +
2589 //+---------------------------------------------------------------------
2592 if (runparams.lastid != -1)
2593 os.texrow().start(runparams.lastid, runparams.lastpos);
2596 os << "\\begin{sideways}\n";
2598 if (is_long_tabular) {
2599 os << "\\begin{longtable}";
2600 switch (longtabular_alignment) {
2601 case LYX_LONGTABULAR_ALIGN_LEFT:
2604 case LYX_LONGTABULAR_ALIGN_CENTER:
2606 case LYX_LONGTABULAR_ALIGN_RIGHT:
2611 if (is_tabular_star)
2612 os << "\\begin{tabular*}{" << from_ascii(tabular_width.asLatexString()) << "}";
2614 os << "\\begin{tabular}";
2615 switch (tabular_valignment) {
2616 case LYX_VALIGN_TOP:
2619 case LYX_VALIGN_MIDDLE:
2621 case LYX_VALIGN_BOTTOM:
2629 if (is_tabular_star)
2630 os << "@{\\extracolsep{\\fill}}";
2632 for (col_type c = 0; c < ncols(); ++c) {
2633 if (columnLeftLine(c))
2635 if (!column_info[c].align_special.empty()) {
2636 os << column_info[c].align_special;
2638 if (!column_info[c].p_width.zero()) {
2639 switch (column_info[c].alignment) {
2640 case LYX_ALIGN_LEFT:
2641 os << ">{\\raggedright}";
2643 case LYX_ALIGN_RIGHT:
2644 os << ">{\\raggedleft}";
2646 case LYX_ALIGN_CENTER:
2647 os << ">{\\centering}";
2649 case LYX_ALIGN_NONE:
2650 case LYX_ALIGN_BLOCK:
2651 case LYX_ALIGN_LAYOUT:
2652 case LYX_ALIGN_SPECIAL:
2653 case LYX_ALIGN_DECIMAL:
2657 switch (column_info[c].valignment) {
2658 case LYX_VALIGN_TOP:
2661 case LYX_VALIGN_MIDDLE:
2664 case LYX_VALIGN_BOTTOM:
2669 << from_ascii(column_info[c].p_width.asLatexString())
2672 switch (column_info[c].alignment) {
2673 case LYX_ALIGN_LEFT:
2676 case LYX_ALIGN_RIGHT:
2679 case LYX_ALIGN_DECIMAL:
2680 os << "r@{\\extracolsep{0pt}" << column_info[c].decimal_point << "}l";
2686 } // end if else !column_info[i].p_width
2687 } // end if else !column_info[i].align_special
2688 if (columnRightLine(c))
2693 TeXLongtableHeaderFooter(os, runparams);
2695 //+---------------------------------------------------------------------
2696 //+ the single row and columns (cells) +
2697 //+---------------------------------------------------------------------
2699 for (row_type r = 0; r < nrows(); ++r) {
2700 if (isValidRow(r)) {
2701 TeXRow(os, r, runparams);
2702 if (is_long_tabular && row_info[r].newpage)
2703 os << "\\newpage\n";
2707 //+---------------------------------------------------------------------
2708 //+ the closing of the tabular +
2709 //+---------------------------------------------------------------------
2711 if (is_long_tabular)
2712 os << "\\end{longtable}";
2714 if (is_tabular_star)
2715 os << "\\end{tabular*}";
2717 os << "\\end{tabular}";
2721 os << breakln << "\\end{sideways}";
2725 int Tabular::docbookRow(odocstream & os, row_type row,
2726 OutputParams const & runparams) const
2729 idx_type cell = getFirstCellInRow(row);
2732 for (col_type c = 0; c < ncols(); ++c) {
2733 if (isPartOfMultiColumn(row, c))
2736 os << "<entry align=\"";
2737 switch (getAlignment(cell)) {
2738 case LYX_ALIGN_LEFT:
2741 case LYX_ALIGN_RIGHT:
2749 os << "\" valign=\"";
2750 switch (getVAlignment(cell)) {
2751 case LYX_VALIGN_TOP:
2754 case LYX_VALIGN_BOTTOM:
2757 case LYX_VALIGN_MIDDLE:
2762 if (isMultiColumn(cell)) {
2763 os << " namest=\"col" << c << "\" ";
2764 os << "nameend=\"col" << c + columnSpan(cell) - 1 << '"';
2768 ret += cellInset(cell)->docbook(os, runparams);
2777 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2781 //+---------------------------------------------------------------------
2782 //+ first the opening preamble +
2783 //+---------------------------------------------------------------------
2785 os << "<tgroup cols=\"" << ncols()
2786 << "\" colsep=\"1\" rowsep=\"1\">\n";
2788 for (col_type c = 0; c < ncols(); ++c) {
2789 os << "<colspec colname=\"col" << c << "\" align=\"";
2790 switch (column_info[c].alignment) {
2791 case LYX_ALIGN_LEFT:
2794 case LYX_ALIGN_RIGHT:
2802 if (runparams.flavor == OutputParams::XML)
2808 //+---------------------------------------------------------------------
2809 //+ Long Tabular case +
2810 //+---------------------------------------------------------------------
2812 // output caption info
2813 if (haveLTCaption()) {
2814 os << "<caption>\n";
2816 for (row_type r = 0; r < nrows(); ++r) {
2817 if (row_info[r].caption) {
2818 ret += docbookRow(os, r, runparams);
2821 os << "</caption>\n";
2824 // output header info
2825 if (haveLTHead() || haveLTFirstHead()) {
2828 for (row_type r = 0; r < nrows(); ++r) {
2829 if (row_info[r].endhead || row_info[r].endfirsthead) {
2830 ret += docbookRow(os, r, runparams);
2836 // output footer info
2837 if (haveLTFoot() || haveLTLastFoot()) {
2840 for (row_type r = 0; r < nrows(); ++r) {
2841 if (row_info[r].endfoot || row_info[r].endlastfoot) {
2842 ret += docbookRow(os, r, runparams);
2849 //+---------------------------------------------------------------------
2850 //+ the single row and columns (cells) +
2851 //+---------------------------------------------------------------------
2855 for (row_type r = 0; r < nrows(); ++r) {
2856 if (isValidRow(r)) {
2857 ret += docbookRow(os, r, runparams);
2862 //+---------------------------------------------------------------------
2863 //+ the closing of the tabular +
2864 //+---------------------------------------------------------------------
2873 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2874 OutputParams const & runparams, bool header) const
2877 string const celltag = header ? "th" : "td";
2878 idx_type cell = getFirstCellInRow(row);
2880 xs << html::StartTag("tr");
2881 for (col_type c = 0; c < ncols(); ++c) {
2882 if (isPartOfMultiColumn(row, c) || isPartOfMultiRow(row, c))
2887 switch (getAlignment(cell)) {
2888 case LYX_ALIGN_LEFT:
2891 case LYX_ALIGN_RIGHT:
2899 attr << " valign='";
2900 switch (getVAlignment(cell)) {
2901 case LYX_VALIGN_TOP:
2904 case LYX_VALIGN_BOTTOM:
2907 case LYX_VALIGN_MIDDLE:
2912 if (isMultiColumn(cell))
2913 attr << " colspan='" << columnSpan(cell) << "'";
2914 else if (isMultiRow(cell))
2915 attr << " rowspan='" << rowSpan(cell) << "'";
2917 xs << html::StartTag(celltag, attr.str()) << html::CR();
2918 ret += cellInset(cell)->xhtml(xs, runparams);
2919 xs << html::EndTag(celltag) << html::CR();
2922 xs << html::EndTag("tr");
2927 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2931 if (is_long_tabular) {
2932 // we'll wrap it in a div, so as to deal with alignment
2934 switch (longtabular_alignment) {
2935 case LYX_LONGTABULAR_ALIGN_LEFT:
2938 case LYX_LONGTABULAR_ALIGN_CENTER:
2941 case LYX_LONGTABULAR_ALIGN_RIGHT:
2945 xs << html::StartTag("div", "class='longtable' style='text-align: " + align + ";'")
2947 if (haveLTCaption()) {
2948 xs << html::StartTag("div", "class='longtable-caption' style='text-align: " + align + ";'")
2950 for (row_type r = 0; r < nrows(); ++r)
2951 if (row_info[r].caption)
2952 ret += xhtmlRow(xs, r, runparams);
2953 xs << html::EndTag("div") << html::CR();
2957 xs << html::StartTag("table") << html::CR();
2959 // output header info
2960 bool const havefirsthead = haveLTFirstHead();
2961 // if we have a first head, then we are going to ignore the
2962 // headers for the additional pages, since there aren't any
2963 // in XHTML. this test accomplishes that.
2964 bool const havehead = !havefirsthead && haveLTHead();
2965 if (havehead || havefirsthead) {
2966 xs << html::StartTag("thead") << html::CR();
2967 for (row_type r = 0; r < nrows(); ++r) {
2968 if ((havefirsthead && row_info[r].endfirsthead)
2969 || (havehead && row_info[r].endhead)) {
2970 ret += xhtmlRow(xs, r, runparams, true);
2973 xs << html::EndTag("thead") << html::CR();
2975 // output footer info
2976 bool const havelastfoot = haveLTLastFoot();
2978 bool const havefoot = !havelastfoot && haveLTFoot();
2979 if (havefoot || havelastfoot) {
2980 xs << html::StartTag("tfoot") << html::CR();
2981 for (row_type r = 0; r < nrows(); ++r) {
2982 if ((havelastfoot && row_info[r].endlastfoot)
2983 || (havefoot && row_info[r].endfoot)) {
2984 ret += xhtmlRow(xs, r, runparams);
2987 xs << html::EndTag("tfoot") << html::CR();
2990 xs << html::StartTag("tbody") << html::CR();
2991 for (row_type r = 0; r < nrows(); ++r) {
2992 if (isValidRow(r)) {
2993 ret += xhtmlRow(xs, r, runparams);
2996 xs << html::EndTag("tbody")
2998 << html::EndTag("table")
3000 if (is_long_tabular)
3001 xs << html::EndTag("div") << html::CR();
3006 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
3007 vector<unsigned int> const & clen) const
3009 idx_type const fcell = getFirstCellInRow(row);
3010 idx_type const n = numberOfCellsInRow(row) + fcell;
3013 for (idx_type i = fcell; i < n; ++i) {
3023 for (idx_type i = fcell; i < n; ++i) {
3034 col_type column = cellColumn(i);
3035 int len = clen[column];
3036 while (column < ncols() - 1
3037 && isPartOfMultiColumn(row, ++column))
3038 len += clen[column] + 4;
3039 os << docstring(len, ch);
3054 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
3055 vector<unsigned int> const & clen) const
3057 idx_type const fcell = getFirstCellInRow(row);
3058 idx_type const n = numberOfCellsInRow(row) + fcell;
3061 for (idx_type i = fcell; i < n; ++i) {
3062 if (bottomLine(i)) {
3071 for (idx_type i = fcell; i < n; ++i) {
3072 if (bottomLine(i)) {
3082 col_type column = cellColumn(i);
3083 int len = clen[column];
3084 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3085 len += clen[column] + 4;
3086 os << docstring(len, ch);
3087 if (bottomLine(i)) {
3101 void Tabular::plaintextPrintCell(odocstream & os,
3102 OutputParams const & runparams,
3103 idx_type cell, row_type row, col_type column,
3104 vector<unsigned int> const & clen,
3105 bool onlydata) const
3107 odocstringstream sstr;
3108 cellInset(cell)->plaintext(sstr, runparams);
3120 unsigned int len1 = sstr.str().length();
3121 unsigned int len2 = clen[column];
3122 while (column < ncols() - 1 && isPartOfMultiColumn(row, ++column))
3123 len2 += clen[column] + 4;
3126 switch (getAlignment(cell)) {
3128 case LYX_ALIGN_LEFT:
3131 case LYX_ALIGN_RIGHT:
3135 case LYX_ALIGN_CENTER:
3141 os << docstring(len1, ' ') << sstr.str()
3142 << docstring(len2, ' ');
3144 if (rightLine(cell))
3151 void Tabular::plaintext(odocstream & os,
3152 OutputParams const & runparams, int const depth,
3153 bool onlydata, char_type delim) const
3155 // first calculate the width of the single columns
3156 vector<unsigned int> clen(ncols());
3159 // first all non multicolumn cells!
3160 for (col_type c = 0; c < ncols(); ++c) {
3162 for (row_type r = 0; r < nrows(); ++r) {
3163 idx_type cell = cellIndex(r, c);
3164 if (isMultiColumn(cell))
3166 odocstringstream sstr;
3167 cellInset(cell)->plaintext(sstr, runparams);
3168 if (clen[c] < sstr.str().length())
3169 clen[c] = sstr.str().length();
3172 // then all multicolumn cells!
3173 for (col_type c = 0; c < ncols(); ++c) {
3174 for (row_type r = 0; r < nrows(); ++r) {
3175 idx_type cell = cellIndex(r, c);
3176 if (cell_info[r][c].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
3178 odocstringstream sstr;
3179 cellInset(cell)->plaintext(sstr, runparams);
3180 int len = int(sstr.str().length());
3181 idx_type const n = columnSpan(cell);
3182 for (col_type k = c; len > 0 && k < c + n - 1; ++k)
3184 if (len > int(clen[c + n - 1]))
3185 clen[c + n - 1] = len;
3190 for (row_type r = 0; r < nrows(); ++r) {
3191 if (!onlydata && plaintextTopHLine(os, r, clen))
3192 os << docstring(depth * 2, ' ');
3193 for (col_type c = 0; c < ncols(); ++c) {
3194 if (isPartOfMultiColumn(r, c) || isPartOfMultiRow(r,c))
3196 if (onlydata && c > 0)
3197 // we don't use operator<< for single UCS4 character.
3198 // see explanation in docstream.h
3200 plaintextPrintCell(os, runparams, cell, r, c, clen, onlydata);
3205 os << docstring(depth * 2, ' ');
3206 if (plaintextBottomHLine(os, r, clen))
3207 os << docstring(depth * 2, ' ');
3213 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
3215 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
3219 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
3220 col_type column) const
3222 return cell_info[row][column].inset;
3226 void Tabular::setCellInset(row_type row, col_type column,
3227 shared_ptr<InsetTableCell> ins) const
3229 CellData & cd = cell_info[row][column];
3234 void Tabular::validate(LaTeXFeatures & features) const
3236 features.require("NeedTabularnewline");
3238 features.require("booktabs");
3239 if (is_long_tabular)
3240 features.require("longtable");
3242 features.require("rotating");
3243 for (idx_type cell = 0; cell < numberofcells; ++cell) {
3244 if (isMultiRow(cell))
3245 features.require("multirow");
3246 if (getVAlignment(cell) != LYX_VALIGN_TOP
3247 || !getPWidth(cell).zero())
3248 features.require("array");
3249 cellInset(cell)->validate(features);
3254 Tabular::BoxType Tabular::useParbox(idx_type cell) const
3256 ParagraphList const & parlist = cellInset(cell)->paragraphs();
3257 ParagraphList::const_iterator cit = parlist.begin();
3258 ParagraphList::const_iterator end = parlist.end();
3260 for (; cit != end; ++cit)
3261 for (int i = 0; i < cit->size(); ++i)
3262 if (cit->isNewline(i))
3269 /////////////////////////////////////////////////////////////////////
3273 /////////////////////////////////////////////////////////////////////
3275 InsetTableCell::InsetTableCell(Buffer * buf)
3276 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
3277 contentAlign(LYX_ALIGN_CENTER)
3281 bool InsetTableCell::forcePlainLayout(idx_type) const
3283 return !isFixedWidth;
3287 bool InsetTableCell::allowParagraphCustomization(idx_type) const
3289 return isFixedWidth;
3293 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
3294 FuncStatus & status) const
3296 bool enabled = true;
3297 switch (cmd.action()) {
3299 enabled = !forcePlainLayout();
3301 case LFUN_LAYOUT_PARAGRAPH:
3302 enabled = allowParagraphCustomization();
3305 case LFUN_MATH_DISPLAY:
3306 if (!hasFixedWidth()) {
3311 return InsetText::getStatus(cur, cmd, status);
3313 status.setEnabled(enabled);
3317 docstring InsetTableCell::asString(bool intoInsets)
3320 if (paragraphs().empty())
3322 ParagraphList::const_iterator it = paragraphs().begin();
3323 ParagraphList::const_iterator en = paragraphs().end();
3325 for (; it != en; ++it) {
3330 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3336 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3339 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3340 return InsetText::xhtml(xs, rp);
3345 /////////////////////////////////////////////////////////////////////
3349 /////////////////////////////////////////////////////////////////////
3351 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3353 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3354 rowselect_(false), colselect_(false)
3359 InsetTabular::InsetTabular(InsetTabular const & tab)
3360 : Inset(tab), tabular(tab.tabular), scx_(0)
3365 InsetTabular::~InsetTabular()
3367 hideDialogs("tabular", this);
3371 void InsetTabular::setBuffer(Buffer & buf)
3373 tabular.setBuffer(buf);
3374 Inset::setBuffer(buf);
3378 bool InsetTabular::insetAllowed(InsetCode code) const
3383 case MATHMACRO_CODE:
3388 return tabular.is_long_tabular;
3396 void InsetTabular::write(ostream & os) const
3398 os << "Tabular" << endl;
3403 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3405 // FIXME: depending on the selection state,
3406 // we could offer a different menu.
3407 return cell(0)->contextMenuName() + ";" + contextMenuName();
3411 docstring InsetTabular::contextMenuName() const
3413 return from_ascii("context-tabular");
3417 void InsetTabular::read(Lexer & lex)
3419 //bool const old_format = (lex.getString() == "\\LyXTable");
3427 string token = lex.getString();
3428 while (lex && token != "\\end_inset") {
3430 token = lex.getString();
3433 lex.printError("Missing \\end_inset at this point. ");
3437 int InsetTabular::rowFromY(Cursor & cur, int y) const
3439 // top y coordinate of tabular
3440 int h = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3442 for (; r < tabular.nrows() && y > h; ++r)
3443 h += tabular.rowAscent(r) + tabular.rowDescent(r)
3444 + tabular.interRowSpace(r);
3450 int InsetTabular::columnFromX(Cursor & cur, int x) const
3452 // left x coordinate of tabular
3453 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3455 for (; c < tabular.ncols() && x > w; ++c)
3456 w += tabular.cellWidth(c);
3461 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3463 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3464 // mi.base.textwidth << "\n";
3467 LASSERT(false, /**/);
3470 for (row_type r = 0; r < tabular.nrows(); ++r) {
3473 for (col_type c = 0; c < tabular.ncols(); ++c) {
3474 if (tabular.isPartOfMultiColumn(r, c)
3475 || tabular.isPartOfMultiRow(r, c))
3476 // multicolumn or multirow cell, but not first one
3478 idx_type const cell = tabular.cellIndex(r, c);
3481 Length const p_width = tabular.getPWidth(cell);
3482 if (!p_width.zero())
3483 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3484 tabular.cellInset(cell)->metrics(m, dim);
3485 if (!p_width.zero())
3486 dim.wid = m.base.textwidth;
3487 tabular.cellInfo(cell).width = dim.wid + 2 * WIDTH_OF_LINE
3488 + tabular.interColumnSpace(cell);
3490 // FIXME(?): do we need a second metrics call?
3491 TextMetrics const & tm =
3492 mi.base.bv->textMetrics(tabular.cellInset(cell)->getText(0));
3494 // determine horizontal offset because of decimal align (if necessary)
3495 int decimal_width = 0;
3496 if (tabular.getAlignment(cell) == LYX_ALIGN_DECIMAL) {
3497 InsetTableCell tail = InsetTableCell(*tabular.cellInset(cell).get());
3498 tail.setBuffer(tabular.buffer());
3499 // we need to set macrocontext position everywhere
3500 // otherwise we crash with nested insets (e.g. footnotes)
3501 // after decimal point
3502 DocIterator dit = tabular.cellInset(cell)->getText(0)->macrocontextPosition();
3504 dit.push_back(CursorSlice(tail));
3505 tail.setMacrocontextPositionRecursive(dit);
3507 // remove text leading decimal point
3508 docstring const align_d = tabular.column_info[c].decimal_point;
3509 dit = separatorPos(&tail, align_d);
3511 pit_type const psize = tail.paragraphs().front().size();
3513 tail.paragraphs().front().eraseChars(0,
3514 dit.pos() < psize ? dit.pos() + 1 : psize, false);
3516 tail.metrics(m, dim1);
3517 decimal_width = dim1.width();
3520 tabular.cell_info[r][c].decimal_hoffset = tm.width() - decimal_width;
3521 tabular.cell_info[r][c].decimal_width = decimal_width;
3523 // with LYX_VALIGN_BOTTOM the descent is relative to the last par
3524 // = descent of text in last par + TEXT_TO_INSET_OFFSET:
3525 int const lastpardes = tm.last().second->descent()
3526 + TEXT_TO_INSET_OFFSET;
3528 switch (tabular.getVAlignment(cell)) {
3529 case Tabular::LYX_VALIGN_TOP:
3531 case Tabular::LYX_VALIGN_MIDDLE:
3532 offset = -(dim.des - lastpardes)/2;
3534 case Tabular::LYX_VALIGN_BOTTOM:
3535 offset = -(dim.des - lastpardes);
3538 tabular.cell_info[r][c].voffset = offset;
3539 maxasc = max(maxasc, dim.asc - offset);
3540 maxdes = max(maxdes, dim.des + offset);
3542 int const top_space = tabular.row_info[r].top_space_default ?
3543 default_line_space :
3544 tabular.row_info[r].top_space.inPixels(mi.base.textwidth);
3545 tabular.setRowAscent(r, maxasc + ADD_TO_HEIGHT + top_space);
3546 int const bottom_space = tabular.row_info[r].bottom_space_default ?
3547 default_line_space :
3548 tabular.row_info[r].bottom_space.inPixels(mi.base.textwidth);
3549 tabular.setRowDescent(r, maxdes + ADD_TO_HEIGHT + bottom_space);
3552 // for top-alignment the first horizontal table line must be exactly at
3553 // the position of the base line of the surrounding text line
3554 // for bottom alignment, the same is for the last table line
3555 switch (tabular.tabular_valignment) {
3556 case Tabular::LYX_VALIGN_BOTTOM:
3557 offset_valign_ = tabular.rowAscent(0) - tabular.height();
3559 case Tabular::LYX_VALIGN_MIDDLE:
3560 offset_valign_ = (- tabular.height()) / 2 + tabular.rowAscent(0);
3562 case Tabular::LYX_VALIGN_TOP:
3563 offset_valign_ = tabular.rowAscent(0);
3567 tabular.updateColumnWidths();
3568 dim.asc = tabular.rowAscent(0) - offset_valign_;
3569 dim.des = tabular.height() - dim.asc;
3570 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3574 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3577 if (&cur.inset() == this && cur.selection()) {
3578 if (cur.selIsMultiCell()) {
3581 getSelection(cur, rs, re, cs, ce);
3583 idx_type const cell = tabular.cellIndex(row, col);
3584 col_type const cspan = tabular.columnSpan(cell);
3585 row_type const rspan = tabular.rowSpan(cell);
3586 if (col + cspan - 1 >= cs && col <= ce
3587 && row + rspan - 1 >= rs && row <= re)
3590 if (col == tabular.cellColumn(cur.idx())
3591 && row == tabular.cellRow(cur.idx())) {
3592 CursorSlice const & beg = cur.selBegin();
3593 CursorSlice const & end = cur.selEnd();
3595 if ((end.lastpos() > 0 || end.lastpit() > 0)
3596 && end.pos() == end.lastpos() && beg.pos() == 0
3597 && end.pit() == end.lastpit() && beg.pit() == 0)
3605 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3607 x += scx_ + ADD_TO_TABULAR_WIDTH;
3609 BufferView * bv = pi.base.bv;
3610 Cursor & cur = pi.base.bv->cursor();
3613 // FIXME: As the full background is painted in drawBackground(),
3614 // we have no choice but to do a full repaint for the Text cells.
3615 pi.full_repaint = true;
3617 bool const original_selection_state = pi.selected;
3620 first_visible_cell = Tabular::npos;
3622 int yy = y + offset_valign_;
3623 for (row_type r = 0; r < tabular.nrows(); ++r) {
3625 for (col_type c = 0; c < tabular.ncols(); ++c) {
3626 if (tabular.isPartOfMultiColumn(r, c))
3629 idx = tabular.cellIndex(r, c);
3631 if (tabular.isPartOfMultiRow(r, c)) {
3632 nx += tabular.cellWidth(idx);
3636 if (first_visible_cell == Tabular::npos)
3637 first_visible_cell = idx;
3639 pi.selected |= isCellSelected(cur, r, c);
3640 int const cx = nx + tabular.textHOffset(idx);
3641 int const cy = yy + tabular.textVOffset(idx);
3642 // Cache the Inset position.
3643 bv->coordCache().insets().add(cell(idx).get(), cx, cy);
3644 cell(idx)->draw(pi, cx, cy);
3645 drawCellLines(pi, nx, yy, r, idx);
3646 nx += tabular.cellWidth(idx);
3647 pi.selected = original_selection_state;
3650 if (r + 1 < tabular.nrows())
3651 yy += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3652 + tabular.interRowSpace(r + 1);
3657 void InsetTabular::drawBackground(PainterInfo & pi, int x, int y) const
3659 x += scx_ + ADD_TO_TABULAR_WIDTH;
3660 y += offset_valign_ - tabular.rowAscent(0);
3661 pi.pain.fillRectangle(x, y, tabular.width(), tabular.height(),
3662 pi.backgroundColor(this));
3666 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3668 Cursor & cur = pi.base.bv->cursor();
3671 x += scx_ + ADD_TO_TABULAR_WIDTH;
3673 if (!cur.selection())
3675 if (&cur.inset() != this)
3680 bool const full_cell_selected = isCellSelected(cur,
3681 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3683 if (cur.selIsMultiCell() || full_cell_selected) {
3684 for (row_type r = 0; r < tabular.nrows(); ++r) {
3686 for (col_type c = 0; c < tabular.ncols(); ++c) {
3687 if (tabular.isPartOfMultiColumn(r, c))
3690 idx_type const cell = tabular.cellIndex(r, c);
3692 if (tabular.isPartOfMultiRow(r, c)) {
3693 xx += tabular.cellWidth(cell);
3696 int const w = tabular.cellWidth(cell);
3697 int const h = tabular.cellHeight(cell);
3698 int const yy = y - tabular.rowAscent(r) + offset_valign_;
3699 if (isCellSelected(cur, r, c))
3700 pi.pain.fillRectangle(xx, yy, w, h, Color_selection);
3703 if (r + 1 < tabular.nrows())
3704 y += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
3705 + tabular.interRowSpace(r + 1);
3709 // FIXME: This code has no effect because InsetTableCell does not handle
3710 // drawSelection other than the trivial implementation in Inset.
3712 // x += cellXPos(cur.idx());
3713 // x += tabular.textHOffset(cur.idx());
3714 // cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3719 void InsetTabular::drawCellLines(PainterInfo & pi, int x, int y,
3720 row_type row, idx_type cell) const
3722 y -= tabular.rowAscent(row);
3723 int const w = tabular.cellWidth(cell);
3724 int const h = tabular.cellHeight(cell);
3725 Color const linecolor = pi.textColor(Color_tabularline);
3726 Color const gridcolor = pi.textColor(Color_tabularonoffline);
3729 bool drawline = tabular.topLine(cell)
3730 || (row > 0 && tabular.bottomLine(tabular.cellAbove(cell)));
3731 pi.pain.line(x, y, x + w, y,
3732 drawline ? linecolor : gridcolor,
3733 drawline ? Painter::line_solid : Painter::line_onoffdash);
3736 drawline = tabular.bottomLine(cell);
3737 pi.pain.line(x, y + h, x + w, y + h,
3738 drawline ? linecolor : gridcolor,
3739 drawline ? Painter::line_solid : Painter::line_onoffdash);
3742 col_type const col = tabular.cellColumn(cell);
3743 drawline = tabular.leftLine(cell)
3744 || (col > 0 && tabular.rightLine(tabular.cellIndex(row, col - 1)));
3745 pi.pain.line(x, y, x, y + h,
3746 drawline ? linecolor : gridcolor,
3747 drawline ? Painter::line_solid : Painter::line_onoffdash);
3750 x -= tabular.interColumnSpace(cell);
3751 drawline = tabular.rightLine(cell)
3752 || (col + 1 < tabular.ncols()
3753 && tabular.leftLine(tabular.cellIndex(row, col + 1)));
3754 pi.pain.line(x + w, y, x + w, y + h,
3755 drawline ? linecolor : gridcolor,
3756 drawline ? Painter::line_solid : Painter::line_onoffdash);
3760 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3762 //lyxerr << "InsetTabular::edit: " << this << endl;
3764 cur.setSelection(false);
3767 if (isRightToLeft(cur))
3768 cur.idx() = tabular.getLastCellInRow(0);
3774 if (isRightToLeft(cur))
3775 cur.idx() = tabular.getFirstCellInRow(tabular.nrows() - 1);
3777 cur.idx() = tabular.numberofcells - 1;
3779 cur.pos() = cur.lastpos(); // FIXME crude guess
3781 cur.setCurrentFont();
3782 // FIXME: this accesses the position cache before it is initialized
3784 //cur.bv().fitCursor();
3788 void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
3790 // In a longtable, tell captions what the current float is
3791 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3792 string const saveflt = cnts.current_float();
3793 if (tabular.is_long_tabular)
3794 cnts.current_float("table");
3796 ParIterator it2 = it;
3798 size_t const end = it2.nargs();
3799 for ( ; it2.idx() < end; it2.top().forwardIdx())
3800 buffer().updateBuffer(it2, utype);
3803 if (tabular.is_long_tabular)
3804 cnts.current_float(saveflt);
3808 void InsetTabular::addToToc(DocIterator const & cpit) const
3810 DocIterator dit = cpit;
3812 size_t const end = dit.nargs();
3813 for ( ; dit.idx() < end; dit.top().forwardIdx())
3814 cell(dit.idx())->addToToc(dit);
3818 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3820 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3821 << "\n cur:" << cur);
3822 CursorSlice sl = cur.top();
3823 Cursor & bvcur = cur.bv().cursor();
3825 FuncCode const act = cmd.action();
3829 case LFUN_MOUSE_PRESS: {
3830 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3832 if (cmd.x() < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3833 || cmd.x() > xo(cur.bv()) + tabular.width()) {
3834 row_type r = rowFromY(cur, cmd.y());
3835 cur.idx() = tabular.getFirstCellInRow(r);
3838 cur.idx() = tabular.getLastCellInRow(r);
3839 cur.pos() = cur.lastpos();
3840 cur.setSelection(true);
3846 int const y0 = yo(cur.bv()) - tabular.rowAscent(0) + offset_valign_;
3847 if (cmd.y() < y0 + ADD_TO_TABULAR_WIDTH
3848 || cmd.y() > y0 + tabular.height()) {
3849 col_type c = columnFromX(cur, cmd.x());
3850 cur.idx() = tabular.cellIndex(0, c);
3853 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3854 cur.pos() = cur.lastpos();
3855 cur.setSelection(true);
3860 // do not reset cursor/selection if we have selected
3861 // some cells (bug 2715).
3862 if (cmd.button() == mouse_button::button3
3863 && &bvcur.selBegin().inset() == this
3864 && bvcur.selIsMultiCell())
3867 // Let InsetTableCell do it
3868 cell(cur.idx())->dispatch(cur, cmd);
3871 case LFUN_MOUSE_MOTION:
3872 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3873 if (cmd.button() == mouse_button::button1) {
3874 // only accept motions to places not deeper nested than the real anchor
3875 if (!bvcur.realAnchor().hasPart(cur)) {
3879 // select (additional) row
3881 row_type r = rowFromY(cur, cmd.y());
3882 cur.idx() = tabular.getLastCellInRow(r);
3883 // we need to reset the cursor's pit and pos now, as the old ones
3884 // may no longer be valid.
3887 bvcur.setCursor(cur);
3888 bvcur.setSelection(true);
3891 // select (additional) column
3893 col_type c = columnFromX(cur, cmd.x());
3894 cur.idx() = tabular.cellIndex(tabular.nrows() - 1, c);
3895 // we need to reset the cursor's pit and pos now, as the old ones
3896 // may no longer be valid.
3899 bvcur.setCursor(cur);
3900 bvcur.setSelection(true);
3903 // only update if selection changes
3904 if (bvcur.idx() == cur.idx() &&
3905 !(bvcur.realAnchor().idx() == cur.idx() && bvcur.pos() != cur.pos()))
3906 cur.noScreenUpdate();
3907 setCursorFromCoordinates(cur, cmd.x(), cmd.y());
3908 bvcur.setCursor(cur);
3909 bvcur.setSelection(true);
3910 // if this is a multicell selection, we just set the cursor to
3911 // the beginning of the cell's text.
3912 if (bvcur.selIsMultiCell()) {
3913 bvcur.pit() = bvcur.lastpit();
3914 bvcur.pos() = bvcur.lastpos();
3919 case LFUN_MOUSE_RELEASE:
3924 case LFUN_CELL_BACKWARD:
3926 cur.setSelection(false);
3929 case LFUN_CELL_FORWARD:
3931 cur.setSelection(false);
3934 case LFUN_CHAR_FORWARD_SELECT:
3935 case LFUN_CHAR_FORWARD:
3936 case LFUN_CHAR_BACKWARD_SELECT:
3937 case LFUN_CHAR_BACKWARD:
3938 case LFUN_CHAR_RIGHT_SELECT:
3939 case LFUN_CHAR_RIGHT:
3940 case LFUN_CHAR_LEFT_SELECT:
3941 case LFUN_CHAR_LEFT:
3942 case LFUN_WORD_FORWARD:
3943 case LFUN_WORD_FORWARD_SELECT:
3944 case LFUN_WORD_BACKWARD:
3945 case LFUN_WORD_BACKWARD_SELECT:
3946 case LFUN_WORD_RIGHT:
3947 case LFUN_WORD_RIGHT_SELECT:
3948 case LFUN_WORD_LEFT:
3949 case LFUN_WORD_LEFT_SELECT: {
3950 // determine whether we move to next or previous cell, where to enter
3951 // the new cell from, and which command to "finish" (i.e., exit the
3954 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3955 FuncCode finish_lfun;
3957 if (act == LFUN_CHAR_FORWARD
3958 || act == LFUN_CHAR_FORWARD_SELECT
3959 || act == LFUN_WORD_FORWARD
3960 || act == LFUN_WORD_FORWARD_SELECT) {
3962 finish_lfun = LFUN_FINISHED_FORWARD;
3964 else if (act == LFUN_CHAR_BACKWARD
3965 || act == LFUN_CHAR_BACKWARD_SELECT
3966 || act == LFUN_WORD_BACKWARD
3967 || act == LFUN_WORD_BACKWARD_SELECT) {
3969 finish_lfun = LFUN_FINISHED_BACKWARD;
3971 // LEFT or RIGHT commands --- the interpretation will depend on the
3972 // table's direction.
3974 bool const right = act == LFUN_CHAR_RIGHT
3975 || act == LFUN_CHAR_RIGHT_SELECT
3976 || act == LFUN_WORD_RIGHT
3977 || act == LFUN_WORD_RIGHT_SELECT;
3978 next_cell = isRightToLeft(cur) != right;
3980 if (lyxrc.visual_cursor)
3981 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3983 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3986 bool const select = act == LFUN_CHAR_FORWARD_SELECT
3987 || act == LFUN_CHAR_BACKWARD_SELECT
3988 || act == LFUN_CHAR_RIGHT_SELECT
3989 || act == LFUN_CHAR_LEFT_SELECT
3990 || act == LFUN_WORD_FORWARD_SELECT
3991 || act == LFUN_WORD_RIGHT_SELECT
3992 || act == LFUN_WORD_BACKWARD_SELECT
3993 || act == LFUN_WORD_LEFT_SELECT;
3995 // If we have a multicell selection or we're
3996 // not doing some LFUN_*_SELECT thing anyway...
3997 if (!cur.selIsMultiCell() || !select) {
3998 col_type const c = tabular.cellColumn(cur.idx());
3999 row_type const r = tabular.cellRow(cur.idx());
4000 // Are we trying to select the whole cell and is the whole cell
4001 // not yet selected?
4002 bool const select_whole = select && !isCellSelected(cur, r, c) &&
4003 ((next_cell && cur.pit() == cur.lastpit()
4004 && cur.pos() == cur.lastpos())
4005 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
4007 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
4009 // ...try to dispatch to the cell's inset.
4010 cell(cur.idx())->dispatch(cur, cmd);
4012 // When we already have a selection we want to select the whole cell
4013 // before going to the next cell.
4014 if (select_whole && !empty_cell){
4015 getText(cur.idx())->selectAll(cur);
4020 // FIXME: When we support the selection of an empty cell, remove
4021 // the !empty_cell from this condition. For now we jump to the next
4022 // cell if the current cell is empty.
4023 if (cur.result().dispatched() && !empty_cell)
4027 // move to next/prev cell, as appropriate
4028 // note that we will always do this if we're selecting and we have
4029 // a multicell selection
4030 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
4031 << " cell from: " << int(entry_from));
4033 moveNextCell(cur, entry_from);
4035 movePrevCell(cur, entry_from);
4036 // if we're exiting the table, call the appropriate FINISHED lfun
4037 if (sl == cur.top())
4038 cmd = FuncRequest(finish_lfun);
4042 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4047 case LFUN_DOWN_SELECT:
4049 if (!(cur.selection() && cur.selIsMultiCell()))
4050 cell(cur.idx())->dispatch(cur, cmd);
4052 cur.dispatched(); // override the cell's decision
4053 if (sl == cur.top()) {
4054 // if our Text didn't do anything to the cursor
4055 // then we try to put the cursor into the cell below
4056 // setting also the right targetX.
4057 cur.selHandle(act == LFUN_DOWN_SELECT);
4058 if (tabular.cellRow(cur.idx()) != tabular.nrows() - 1) {
4059 int const xtarget = cur.targetX();
4060 // WARNING: Once cur.idx() has been reset, the cursor is in
4061 // an inconsistent state until pos() has been set. Be careful
4062 // what you do with it!
4063 cur.idx() = tabular.cellBelow(cur.idx());
4065 TextMetrics const & tm =
4066 cur.bv().textMetrics(cell(cur.idx())->getText(0));
4067 cur.pos() = tm.x2pos(cur.pit(), 0, xtarget);
4068 cur.setCurrentFont();
4071 if (sl == cur.top()) {
4072 // we trick it to go to forward after leaving the
4074 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
4077 if (cur.selIsMultiCell()) {
4078 cur.pit() = cur.lastpit();
4079 cur.pos() = cur.lastpos();
4080 cur.setCurrentFont();
4083 cur.screenUpdateFlags(Update::FitCursor);
4086 case LFUN_UP_SELECT:
4088 if (!(cur.selection() && cur.selIsMultiCell()))
4089 cell(cur.idx())->dispatch(cur, cmd);
4090 cur.dispatched(); // override the cell's decision
4091 if (sl == cur.top()) {
4092 // if our Text didn't do anything to the cursor
4093 // then we try to put the cursor into the cell above
4094 // setting also the right targetX.
4095 cur.selHandle(act == LFUN_UP_SELECT);
4096 if (tabular.cellRow(cur.idx()) != 0) {
4097 int const xtarget = cur.targetX();
4098 // WARNING: Once cur.idx() has been reset, the cursor is in
4099 // an inconsistent state until pos() has been set. Be careful
4100 // what you do with it!
4101 cur.idx() = tabular.cellAbove(cur.idx());
4102 cur.pit() = cur.lastpit();
4103 Text const * text = cell(cur.idx())->getText(0);
4104 TextMetrics const & tm = cur.bv().textMetrics(text);
4105 ParagraphMetrics const & pm =
4106 tm.parMetrics(cur.lastpit());
4107 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, xtarget);
4108 cur.setCurrentFont();
4111 if (sl == cur.top()) {
4112 cmd = FuncRequest(LFUN_UP);
4115 if (cur.selIsMultiCell()) {
4117 cur.pos() = cur.lastpos();
4118 cur.setCurrentFont();
4121 cur.screenUpdateFlags(Update::FitCursor);
4124 // case LFUN_SCREEN_DOWN: {
4125 // //if (hasSelection())
4126 // // cur.selection() = false;
4127 // col_type const col = tabular.cellColumn(cur.idx());
4128 // int const t = cur.bv().top_y() + cur.bv().height();
4129 // if (t < yo() + tabular.getHeightOfTabular()) {
4130 // cur.bv().scrollDocView(t, true);
4131 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4133 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
4140 // case LFUN_SCREEN_UP: {
4141 // //if (hasSelection())
4142 // // cur.selection() = false;
4143 // col_type const col = tabular.cellColumn(cur.idx());
4144 // int const t = cur.bv().top_y() + cur.bv().height();
4146 // cur.bv().scrollDocView(t, true);
4150 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
4154 // cur.par() = cur.lastpar();
4155 // cur.pos() = cur.lastpos();
4159 case LFUN_LAYOUT_TABULAR:
4160 cur.bv().showDialog("tabular");
4163 case LFUN_INSET_MODIFY:
4164 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
4168 // insert file functions
4169 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
4170 case LFUN_FILE_INSERT_PLAINTEXT:
4172 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
4173 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
4174 FileName(to_utf8(cmd.argument())));
4177 cur.recordUndoInset(INSERT_UNDO);
4178 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
4179 // content has been replaced,
4180 // so cursor might be invalid
4181 cur.pos() = cur.lastpos();
4182 cur.pit() = cur.lastpit();
4183 bvcur.setCursor(cur);
4190 if (cur.selIsMultiCell()) {
4191 if (copySelection(cur)) {
4192 cur.recordUndoInset(DELETE_UNDO);
4196 cell(cur.idx())->dispatch(cur, cmd);
4199 case LFUN_SELF_INSERT:
4200 if (cur.selIsMultiCell()) {
4201 cur.recordUndoInset(DELETE_UNDO);
4204 cell(cur.idx())->dispatch(cur, cmd);
4207 case LFUN_CHAR_DELETE_BACKWARD:
4208 case LFUN_CHAR_DELETE_FORWARD:
4209 if (cur.selIsMultiCell()) {
4210 cur.recordUndoInset(DELETE_UNDO);
4213 cell(cur.idx())->dispatch(cur, cmd);
4217 if (!cur.selection())
4219 if (cur.selIsMultiCell()) {
4223 cell(cur.idx())->dispatch(cur, cmd);
4226 case LFUN_CLIPBOARD_PASTE:
4227 case LFUN_PRIMARY_SELECTION_PASTE: {
4228 docstring const clip = (act == LFUN_CLIPBOARD_PASTE) ?
4229 theClipboard().getAsText() :
4230 theSelection().get();
4233 // pass to InsertPlaintextString, but
4234 // only if we have multi-cell content
4235 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
4236 cur.recordUndoInset(INSERT_UNDO);
4237 if (insertPlaintextString(cur.bv(), clip, false)) {
4238 // content has been replaced,
4239 // so cursor might be invalid
4240 cur.pos() = cur.lastpos();
4241 cur.pit() = cur.lastpit();
4242 bvcur.setCursor(cur);
4246 // Let the cell handle normal text
4247 cell(cur.idx())->dispatch(cur, cmd);
4252 if (!tabularStackDirty()) {
4253 if (!cur.selIsMultiCell())
4254 cell(cur.idx())->dispatch(cur, cmd);
4257 if (theClipboard().isInternal()) {
4258 cur.recordUndoInset(INSERT_UNDO);
4259 pasteClipboard(cur);
4263 case LFUN_FONT_EMPH:
4264 case LFUN_FONT_BOLD:
4265 case LFUN_FONT_BOLDSYMBOL:
4266 case LFUN_FONT_ROMAN:
4267 case LFUN_FONT_NOUN:
4268 case LFUN_FONT_ITAL:
4269 case LFUN_FONT_FRAK:
4270 case LFUN_FONT_TYPEWRITER:
4271 case LFUN_FONT_SANS:
4272 case LFUN_TEXTSTYLE_APPLY:
4273 case LFUN_TEXTSTYLE_UPDATE:
4274 case LFUN_FONT_SIZE:
4275 case LFUN_FONT_UNDERLINE:
4276 case LFUN_FONT_STRIKEOUT:
4277 case LFUN_FONT_UULINE:
4278 case LFUN_FONT_UWAVE:
4280 case LFUN_WORD_CAPITALIZE:
4281 case LFUN_WORD_UPCASE:
4282 case LFUN_WORD_LOWCASE:
4283 case LFUN_CHARS_TRANSPOSE:
4284 if (cur.selIsMultiCell()) {
4287 getSelection(cur, rs, re, cs, ce);
4288 Cursor tmpcur = cur;
4289 for (row_type r = rs; r <= re; ++r) {
4290 for (col_type c = cs; c <= ce; ++c) {
4291 // cursor follows cell:
4292 tmpcur.idx() = tabular.cellIndex(r, c);
4293 // select this cell only:
4296 tmpcur.resetAnchor();
4297 tmpcur.pit() = tmpcur.lastpit();
4298 tmpcur.pos() = tmpcur.top().lastpos();
4299 tmpcur.setCursor(tmpcur);
4300 tmpcur.setSelection();
4301 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
4306 cell(cur.idx())->dispatch(cur, cmd);
4310 case LFUN_INSET_SETTINGS:
4311 // relay this lfun to Inset, not to the cell.
4312 Inset::doDispatch(cur, cmd);
4316 // we try to handle this event in the insets dispatch function.
4317 cell(cur.idx())->dispatch(cur, cmd);
4323 // function sets an object as defined in func_status.h:
4324 // states OK, Unknown, Disabled, On, Off.
4325 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
4326 FuncStatus & status) const
4328 switch (cmd.action()) {
4329 case LFUN_INSET_MODIFY: {
4330 if (&cur.inset() != this || cmd.getArg(0) != "tabular")
4333 // FIXME: We only check for the very first argument...
4334 string const s = cmd.getArg(1);
4335 // We always enable the lfun if it is coming from the dialog
4336 // because the dialog makes sure all the settings are valid,
4337 // even though the first argument might not be valid now.
4338 if (s == "from-dialog") {
4339 status.setEnabled(true);
4343 int action = Tabular::LAST_ACTION;
4345 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4346 if (tabularFeature[i].feature == s) {
4347 action = tabularFeature[i].action;
4351 if (action == Tabular::LAST_ACTION) {
4353 status.setUnknown(true);
4357 string const argument = cmd.getLongArg(2);
4359 row_type sel_row_start = 0;
4360 row_type sel_row_end = 0;
4361 col_type sel_col_start = 0;
4362 col_type sel_col_end = 0;
4363 Tabular::ltType dummyltt;
4366 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4369 case Tabular::SET_PWIDTH:
4370 case Tabular::SET_MPWIDTH:
4371 case Tabular::SET_SPECIAL_COLUMN:
4372 case Tabular::SET_SPECIAL_MULTICOLUMN:
4373 case Tabular::APPEND_ROW:
4374 case Tabular::APPEND_COLUMN:
4375 case Tabular::DELETE_ROW:
4376 case Tabular::DELETE_COLUMN:
4377 case Tabular::COPY_ROW:
4378 case Tabular::COPY_COLUMN:
4379 case Tabular::SET_TOP_SPACE:
4380 case Tabular::SET_BOTTOM_SPACE:
4381 case Tabular::SET_INTERLINE_SPACE:
4385 case Tabular::SET_TABULAR_WIDTH:
4386 status.setEnabled(!tabular.rotate && !tabular.is_long_tabular
4387 && tabular.tabular_valignment == Tabular::LYX_VALIGN_MIDDLE);
4390 case Tabular::SET_DECIMAL_POINT:
4392 tabular.getAlignment(cur.idx()) == LYX_ALIGN_DECIMAL);
4395 case Tabular::SET_MULTICOLUMN:
4396 case Tabular::UNSET_MULTICOLUMN:
4397 case Tabular::MULTICOLUMN:
4398 // If a row is set as longtable caption, it must not be allowed
4399 // to unset that this row is a multicolumn.
4400 status.setEnabled(sel_row_start == sel_row_end
4401 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4402 status.setOnOff(tabular.isMultiColumn(cur.idx()));
4405 case Tabular::SET_MULTIROW:
4406 case Tabular::UNSET_MULTIROW:
4407 case Tabular::MULTIROW:
4408 // If a row is set as longtable caption, it must not be allowed
4409 // to unset that this row is a multirow.
4410 status.setEnabled(sel_col_start == sel_col_end
4411 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
4412 status.setOnOff(tabular.isMultiRow(cur.idx()));
4415 case Tabular::SET_ALL_LINES:
4416 case Tabular::UNSET_ALL_LINES:
4417 case Tabular::SET_BORDER_LINES:
4418 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4421 case Tabular::SET_LINE_TOP:
4422 case Tabular::SET_LINE_BOTTOM:
4423 case Tabular::SET_LINE_LEFT:
4424 case Tabular::SET_LINE_RIGHT:
4425 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4428 case Tabular::TOGGLE_LINE_TOP:
4429 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4430 status.setOnOff(tabular.topLine(cur.idx()));
4433 case Tabular::TOGGLE_LINE_BOTTOM:
4434 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4435 status.setOnOff(tabular.bottomLine(cur.idx()));
4438 case Tabular::TOGGLE_LINE_LEFT:
4439 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4440 status.setOnOff(tabular.leftLine(cur.idx()));
4443 case Tabular::TOGGLE_LINE_RIGHT:
4444 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
4445 status.setOnOff(tabular.rightLine(cur.idx()));
4448 // multirow cells only inherit the alignment of the column if the column has
4449 // no width, otherwise they are left-aligned
4450 // therefore allow always left but right and center only if there is no width
4451 case Tabular::M_ALIGN_LEFT:
4453 case Tabular::ALIGN_LEFT:
4454 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4457 case Tabular::M_ALIGN_RIGHT:
4459 case Tabular::ALIGN_RIGHT:
4460 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4461 && !tabular.getPWidth(cur.idx()).zero()));
4462 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4465 case Tabular::M_ALIGN_CENTER:
4467 case Tabular::ALIGN_CENTER:
4468 status.setEnabled(!(tabular.isMultiRow(cur.idx())
4469 && !tabular.getPWidth(cur.idx()).zero()));
4470 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4473 case Tabular::ALIGN_BLOCK:
4474 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4475 && !tabular.isMultiRow(cur.idx()));
4476 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4479 case Tabular::ALIGN_DECIMAL:
4480 status.setEnabled(!tabular.isMultiRow(cur.idx())
4481 && !tabular.isMultiColumn(cur.idx()));
4482 status.setOnOff(tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_DECIMAL);
4485 case Tabular::M_VALIGN_TOP:
4487 case Tabular::VALIGN_TOP:
4488 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4489 && !tabular.isMultiRow(cur.idx()));
4491 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4494 case Tabular::M_VALIGN_BOTTOM:
4496 case Tabular::VALIGN_BOTTOM:
4497 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4498 && !tabular.isMultiRow(cur.idx()));
4500 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4503 case Tabular::M_VALIGN_MIDDLE:
4505 case Tabular::VALIGN_MIDDLE:
4506 status.setEnabled(!tabular.getPWidth(cur.idx()).zero()
4507 && !tabular.isMultiRow(cur.idx()));
4509 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4512 case Tabular::SET_LONGTABULAR:
4513 // setting as longtable is not allowed when table is inside a float
4514 if (cur.innerInsetOfType(FLOAT_CODE) != 0
4515 || cur.innerInsetOfType(WRAP_CODE) != 0)
4516 status.setEnabled(false);
4518 status.setEnabled(true);
4519 status.setOnOff(tabular.is_long_tabular);
4522 case Tabular::UNSET_LONGTABULAR:
4523 status.setOnOff(!tabular.is_long_tabular);
4526 case Tabular::TOGGLE_ROTATE_TABULAR:
4527 case Tabular::SET_ROTATE_TABULAR:
4528 status.setEnabled(tabular.tabular_width.zero());
4529 status.setOnOff(tabular.rotate);
4532 case Tabular::TABULAR_VALIGN_TOP:
4533 status.setEnabled(tabular.tabular_width.zero());
4534 status.setOnOff(tabular.tabular_valignment
4535 == Tabular::LYX_VALIGN_TOP);
4537 case Tabular::TABULAR_VALIGN_MIDDLE:
4538 status.setEnabled(tabular.tabular_width.zero());
4539 status.setOnOff(tabular.tabular_valignment
4540 == Tabular::LYX_VALIGN_MIDDLE);
4542 case Tabular::TABULAR_VALIGN_BOTTOM:
4543 status.setEnabled(tabular.tabular_width.zero());
4544 status.setOnOff(tabular.tabular_valignment
4545 == Tabular::LYX_VALIGN_BOTTOM);
4548 case Tabular::LONGTABULAR_ALIGN_LEFT:
4549 status.setOnOff(tabular.longtabular_alignment
4550 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4552 case Tabular::LONGTABULAR_ALIGN_CENTER:
4553 status.setOnOff(tabular.longtabular_alignment
4554 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4556 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4557 status.setOnOff(tabular.longtabular_alignment
4558 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4561 case Tabular::UNSET_ROTATE_TABULAR:
4562 status.setOnOff(!tabular.rotate);
4565 case Tabular::TOGGLE_ROTATE_CELL:
4566 case Tabular::SET_ROTATE_CELL:
4567 status.setOnOff(!oneCellHasRotationState(false,
4568 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4571 case Tabular::UNSET_ROTATE_CELL:
4572 status.setOnOff(!oneCellHasRotationState(true,
4573 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4576 case Tabular::SET_USEBOX:
4577 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4580 // every row can only be one thing:
4581 // either a footer or header or caption
4582 case Tabular::SET_LTFIRSTHEAD:
4583 status.setEnabled(sel_row_start == sel_row_end
4584 && !tabular.ltCaption(sel_row_start));
4585 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4588 case Tabular::UNSET_LTFIRSTHEAD:
4589 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4592 case Tabular::SET_LTHEAD:
4593 status.setEnabled(sel_row_start == sel_row_end
4594 && !tabular.ltCaption(sel_row_start));
4595 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4598 case Tabular::UNSET_LTHEAD:
4599 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4602 case Tabular::SET_LTFOOT:
4603 status.setEnabled(sel_row_start == sel_row_end
4604 && !tabular.ltCaption(sel_row_start));
4605 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4608 case Tabular::UNSET_LTFOOT:
4609 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4612 case Tabular::SET_LTLASTFOOT:
4613 status.setEnabled(sel_row_start == sel_row_end
4614 && !tabular.ltCaption(sel_row_start));
4615 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4618 case Tabular::UNSET_LTLASTFOOT:
4619 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4622 case Tabular::SET_LTNEWPAGE:
4623 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4626 // only one row can be the caption
4627 // and a multirow cannot be set as caption
4628 case Tabular::SET_LTCAPTION:
4629 case Tabular::UNSET_LTCAPTION:
4630 case Tabular::TOGGLE_LTCAPTION:
4631 status.setEnabled(sel_row_start == sel_row_end
4632 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4633 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4634 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4635 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4636 && (!tabular.haveLTCaption()
4637 || tabular.ltCaption(sel_row_start))
4638 && !tabular.isMultiRow(sel_row_start));
4639 status.setOnOff(tabular.ltCaption(sel_row_start));
4642 case Tabular::SET_BOOKTABS:
4643 status.setOnOff(tabular.use_booktabs);
4646 case Tabular::UNSET_BOOKTABS:
4647 status.setOnOff(!tabular.use_booktabs);
4652 status.setEnabled(false);
4658 // These are only enabled inside tabular
4659 case LFUN_CELL_BACKWARD:
4660 case LFUN_CELL_FORWARD:
4661 status.setEnabled(true);
4664 // disable these with multiple cells selected
4665 case LFUN_INSET_INSERT:
4666 case LFUN_TABULAR_INSERT:
4667 case LFUN_FLEX_INSERT:
4668 case LFUN_FLOAT_INSERT:
4669 case LFUN_FLOAT_WIDE_INSERT:
4670 case LFUN_FOOTNOTE_INSERT:
4671 case LFUN_MARGINALNOTE_INSERT:
4672 case LFUN_MATH_INSERT:
4673 case LFUN_MATH_MODE:
4674 case LFUN_MATH_MUTATE:
4675 case LFUN_MATH_DISPLAY:
4676 case LFUN_NOTE_INSERT:
4677 case LFUN_ARGUMENT_INSERT:
4678 case LFUN_BOX_INSERT:
4679 case LFUN_BRANCH_INSERT:
4680 case LFUN_PHANTOM_INSERT:
4681 case LFUN_WRAP_INSERT:
4682 case LFUN_PREVIEW_INSERT:
4683 case LFUN_ERT_INSERT: {
4684 if (cur.selIsMultiCell()) {
4685 status.setEnabled(false);
4688 return cell(cur.idx())->getStatus(cur, cmd, status);
4691 // disable in non-fixed-width cells
4692 case LFUN_BREAK_PARAGRAPH:
4693 // multirow does not allow paragraph breaks
4694 if (tabular.isMultiRow(cur.idx())) {
4695 status.setEnabled(false);
4698 case LFUN_NEWLINE_INSERT: {
4699 if (tabular.getPWidth(cur.idx()).zero()) {
4700 status.setEnabled(false);
4703 return cell(cur.idx())->getStatus(cur, cmd, status);
4706 case LFUN_NEWPAGE_INSERT:
4707 status.setEnabled(false);
4711 if (tabularStackDirty() && theClipboard().isInternal()) {
4712 if (cur.selIsMultiCell()) {
4715 getSelection(cur, rs, re, cs, ce);
4716 if (paste_tabular && paste_tabular->ncols() == ce - cs + 1
4717 && paste_tabular->nrows() == re - rs + 1)
4718 status.setEnabled(true);
4720 status.setEnabled(false);
4721 status.message(_("Selection size should match clipboard content."));
4724 status.setEnabled(true);
4727 return cell(cur.idx())->getStatus(cur, cmd, status);
4729 case LFUN_INSET_SETTINGS:
4730 // relay this lfun to Inset, not to the cell.
4731 return Inset::getStatus(cur, cmd, status);
4734 // we try to handle this event in the insets dispatch function.
4735 return cell(cur.idx())->getStatus(cur, cmd, status);
4741 Inset::DisplayType InsetTabular::display() const
4743 if (tabular.is_long_tabular) {
4744 switch (tabular.longtabular_alignment) {
4745 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4747 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4749 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4759 void InsetTabular::latex(otexstream & os, OutputParams const & runparams) const
4761 tabular.latex(os, runparams);
4765 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4767 os << '\n'; // output table on a new line
4768 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4769 tabular.plaintext(os, runparams, dp, false, 0);
4770 return PLAINTEXT_NEWLINE;
4774 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4779 // FIXME: Why not pass a proper DocIterator here?
4781 // if the table is inside a float it doesn't need the informaltable
4782 // wrapper. Search for it.
4783 for (master = owner(); master; master = master->owner())
4784 if (master->lyxCode() == FLOAT_CODE)
4789 os << "<informaltable>";
4792 ret += tabular.docbook(os, runparams);
4794 os << "</informaltable>";
4801 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4803 return tabular.xhtml(xs, rp);
4807 void InsetTabular::validate(LaTeXFeatures & features) const
4809 tabular.validate(features);
4811 // It'd be better to be able to get this from an InsetLayout, but at present
4812 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4813 if (features.runparams().flavor == OutputParams::HTML)
4814 features.addPreambleSnippet("<style type=\"text/css\">\n"
4815 "table { border: 1px solid black; display: inline-block; }\n"
4816 "td { border: 1px solid black; padding: 0.5ex; }\n"
4821 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4823 return tabular.cellInset(idx);
4827 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4829 return tabular.cellInset(idx);
4833 void InsetTabular::cursorPos(BufferView const & bv,
4834 CursorSlice const & sl, bool boundary, int & x, int & y) const
4836 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4838 // y offset correction
4839 y += cellYPos(sl.idx());
4840 y += tabular.textVOffset(sl.idx());
4841 y += offset_valign_;
4843 // x offset correction
4844 x += cellXPos(sl.idx());
4845 x += tabular.textHOffset(sl.idx());
4846 x += ADD_TO_TABULAR_WIDTH;
4851 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4855 Inset const & inset = *tabular.cellInset(cell);
4856 Point o = bv.coordCache().getInsets().xy(&inset);
4857 int const xbeg = o.x_ - tabular.textHOffset(cell);
4858 int const xend = xbeg + tabular.cellWidth(cell);
4859 row_type const row = tabular.cellRow(cell);
4860 int const ybeg = o.y_ - tabular.rowAscent(row)
4861 - tabular.interRowSpace(row);
4862 int const yend = ybeg + tabular.cellHeight(cell);
4874 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4875 // << " ybeg=" << ybeg << " yend=" << yend
4876 // << " xx=" << xx << " yy=" << yy
4877 // << " dist=" << xx + yy << endl;
4882 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4884 //lyxerr << "InsetTabular::editXY: " << this << endl;
4885 cur.setSelection(false);
4887 cur.idx() = getNearestCell(cur.bv(), x, y);
4889 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4893 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4895 cur.idx() = getNearestCell(cur.bv(), x, y);
4896 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4900 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4902 idx_type idx_min = 0;
4903 int dist_min = numeric_limits<int>::max();
4904 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4905 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4906 int const d = dist(bv, i, x, y);
4917 int InsetTabular::cellYPos(idx_type const cell) const
4919 row_type row = tabular.cellRow(cell);
4921 for (row_type r = 0; r < row; ++r)
4922 ly += tabular.rowDescent(r) + tabular.rowAscent(r + 1)
4923 + tabular.interRowSpace(r + 1);
4928 int InsetTabular::cellXPos(idx_type const cell) const
4930 col_type col = tabular.cellColumn(cell);
4932 for (col_type c = 0; c < col; ++c)
4933 lx += tabular.column_info[c].width;
4938 void InsetTabular::resetPos(Cursor & cur) const
4940 BufferView & bv = cur.bv();
4941 int const maxwidth = bv.workWidth();
4943 int const scx_old = scx_;
4944 int const i = cur.find(this);
4949 int const X2 = maxwidth;
4950 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4951 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4952 int const x2 = x1 + tabular.cellWidth(cur[i].idx());
4955 scx_ = X1 + 20 - x1;
4957 scx_ = X2 - 20 - x2;
4962 // only update if offset changed
4963 if (scx_ != scx_old)
4964 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
4968 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4970 row_type const row = tabular.cellRow(cur.idx());
4971 col_type const col = tabular.cellColumn(cur.idx());
4973 if (isRightToLeft(cur)) {
4974 if (tabular.cellColumn(cur.idx()) == 0) {
4975 if (row == tabular.nrows() - 1)
4977 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4982 cur.idx() = tabular.getLastCellInRow(row - 1);
4984 cur.idx() = tabular.cellIndex(row, col - 1);
4987 if (tabular.isLastCell(cur.idx()))
4989 if (cur.idx() == tabular.getLastCellInRow(row))
4990 cur.idx() = tabular.cellIndex(row + 1, 0);
4992 col_type const colnextcell = col + tabular.columnSpan(cur.idx());
4993 cur.idx() = tabular.cellIndex(row, colnextcell);
4997 cur.boundary(false);
4999 if (cur.selIsMultiCell()) {
5000 cur.pit() = cur.lastpit();
5001 cur.pos() = cur.lastpos();
5009 // in visual mode, place cursor at extreme left or right
5011 switch(entry_from) {
5013 case ENTRY_DIRECTION_RIGHT:
5014 cur.posVisToRowExtremity(false /* !left */);
5016 case ENTRY_DIRECTION_LEFT:
5017 cur.posVisToRowExtremity(true /* left */);
5019 case ENTRY_DIRECTION_IGNORE:
5020 // nothing to do in this case
5024 cur.setCurrentFont();
5029 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
5031 row_type const row = tabular.cellRow(cur.idx());
5032 col_type const col = tabular.cellColumn(cur.idx());
5034 if (isRightToLeft(cur)) {
5035 if (cur.idx() == tabular.getLastCellInRow(row)) {
5038 cur.idx() = tabular.getFirstCellInRow(row);
5039 cur.idx() = tabular.cellAbove(cur.idx());
5041 if (tabular.isLastCell(cur.idx()))
5043 if (cur.idx() == tabular.getLastCellInRow(row))
5044 cur.idx() = tabular.cellIndex(row + 1, 0);
5046 cur.idx() = tabular.cellIndex(row, col + 1);
5049 if (cur.idx() == 0) // first cell
5052 cur.idx() = tabular.getLastCellInRow(row - 1);
5054 cur.idx() = tabular.cellIndex(row, col - 1);
5057 if (cur.selIsMultiCell()) {
5058 cur.pit() = cur.lastpit();
5059 cur.pos() = cur.lastpos();
5064 cur.pit() = cur.lastpit();
5065 cur.pos() = cur.lastpos();
5067 // in visual mode, place cursor at extreme left or right
5069 switch(entry_from) {
5071 case ENTRY_DIRECTION_RIGHT:
5072 cur.posVisToRowExtremity(false /* !left */);
5074 case ENTRY_DIRECTION_LEFT:
5075 cur.posVisToRowExtremity(true /* left */);
5077 case ENTRY_DIRECTION_IGNORE:
5078 // nothing to do in this case
5082 cur.setCurrentFont();
5087 bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
5089 istringstream is(argument);
5092 if (insetCode(s) != TABULAR_CODE)
5096 size_t safe_guard = 0;
5101 if (safe_guard > 1000) {
5102 LYXERR0("parameter max count reached!");
5106 Tabular::Feature action = Tabular::LAST_ACTION;
5109 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
5110 if (s != tabularFeature[i].feature)
5113 action = tabularFeature[i].action;
5116 if (action == Tabular::LAST_ACTION) {
5117 LYXERR0("Feature not found " << s);
5121 if (tabularFeature[i].need_value)
5123 LYXERR(Debug::DEBUG, "Feature: " << s << "\t\tvalue: " << val);
5124 tabularFeatures(cur, action, val);
5130 static void checkLongtableSpecial(Tabular::ltType & ltt,
5131 string const & special, bool & flag)
5133 if (special == "dl_above") {
5136 } else if (special == "dl_below") {
5137 ltt.bottomDL = flag;
5139 } else if (special == "empty") {
5149 bool InsetTabular::oneCellHasRotationState(bool rotated,
5150 row_type row_start, row_type row_end,
5151 col_type col_start, col_type col_end) const
5153 for (row_type r = row_start; r <= row_end; ++r)
5154 for (col_type c = col_start; c <= col_end; ++c)
5155 if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
5161 void InsetTabular::tabularFeatures(Cursor & cur,
5162 Tabular::Feature feature, string const & value)
5164 col_type sel_col_start;
5165 col_type sel_col_end;
5166 row_type sel_row_start;
5167 row_type sel_row_end;
5168 bool setLines = false;
5169 LyXAlignment setAlign = LYX_ALIGN_LEFT;
5170 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
5174 case Tabular::M_ALIGN_LEFT:
5175 case Tabular::ALIGN_LEFT:
5176 setAlign = LYX_ALIGN_LEFT;
5179 case Tabular::M_ALIGN_RIGHT:
5180 case Tabular::ALIGN_RIGHT:
5181 setAlign = LYX_ALIGN_RIGHT;
5184 case Tabular::M_ALIGN_CENTER:
5185 case Tabular::ALIGN_CENTER:
5186 setAlign = LYX_ALIGN_CENTER;
5189 case Tabular::ALIGN_BLOCK:
5190 setAlign = LYX_ALIGN_BLOCK;
5193 case Tabular::ALIGN_DECIMAL:
5194 setAlign = LYX_ALIGN_DECIMAL;
5197 case Tabular::M_VALIGN_TOP:
5198 case Tabular::VALIGN_TOP:
5199 setVAlign = Tabular::LYX_VALIGN_TOP;
5202 case Tabular::M_VALIGN_BOTTOM:
5203 case Tabular::VALIGN_BOTTOM:
5204 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
5207 case Tabular::M_VALIGN_MIDDLE:
5208 case Tabular::VALIGN_MIDDLE:
5209 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
5216 cur.recordUndoInset(ATOMIC_UNDO, this);
5218 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5219 row_type const row = tabular.cellRow(cur.idx());
5220 col_type const column = tabular.cellColumn(cur.idx());
5222 Tabular::ltType ltt;
5226 case Tabular::SET_TABULAR_WIDTH:
5227 tabular.setTabularWidth(Length(value));
5230 case Tabular::SET_PWIDTH: {
5231 Length const len(value);
5232 tabular.setColumnPWidth(cur, cur.idx(), len);
5234 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
5235 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
5239 case Tabular::SET_MPWIDTH:
5240 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
5243 case Tabular::SET_MROFFSET:
5244 tabular.setMROffset(cur, cur.idx(), Length(value));
5247 case Tabular::SET_SPECIAL_COLUMN:
5248 case Tabular::SET_SPECIAL_MULTICOLUMN:
5249 if (value == "none")
5250 tabular.setAlignSpecial(cur.idx(), docstring(), feature);
5252 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
5255 case Tabular::APPEND_ROW:
5256 // append the row into the tabular
5257 tabular.appendRow(cur.idx());
5260 case Tabular::APPEND_COLUMN:
5261 // append the column into the tabular
5262 tabular.appendColumn(cur.idx());
5263 cur.idx() = tabular.cellIndex(row, column);
5266 case Tabular::DELETE_ROW:
5267 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5268 tabular.deleteRow(sel_row_start);
5269 if (sel_row_start >= tabular.nrows())
5271 cur.idx() = tabular.cellIndex(sel_row_start, column);
5274 cur.setSelection(false);
5277 case Tabular::DELETE_COLUMN:
5278 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5279 tabular.deleteColumn(sel_col_start);
5280 if (sel_col_start >= tabular.ncols())
5282 cur.idx() = tabular.cellIndex(row, sel_col_start);
5285 cur.setSelection(false);
5288 case Tabular::COPY_ROW:
5289 tabular.copyRow(row);
5292 case Tabular::COPY_COLUMN:
5293 tabular.copyColumn(column);
5294 cur.idx() = tabular.cellIndex(row, column);
5297 case Tabular::SET_LINE_TOP:
5298 case Tabular::TOGGLE_LINE_TOP: {
5299 bool lineSet = (feature == Tabular::SET_LINE_TOP)
5300 ? (value == "true") : !tabular.topLine(cur.idx());
5301 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5302 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5303 tabular.setTopLine(tabular.cellIndex(r, c), lineSet);
5307 case Tabular::SET_LINE_BOTTOM:
5308 case Tabular::TOGGLE_LINE_BOTTOM: {
5309 bool lineSet = (feature == Tabular::SET_LINE_BOTTOM)
5310 ? (value == "true") : !tabular.bottomLine(cur.idx());
5311 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5312 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5313 tabular.setBottomLine(tabular.cellIndex(r, c), lineSet);
5317 case Tabular::SET_LINE_LEFT:
5318 case Tabular::TOGGLE_LINE_LEFT: {
5319 bool lineSet = (feature == Tabular::SET_LINE_LEFT)
5320 ? (value == "true") : !tabular.leftLine(cur.idx());
5321 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5322 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5323 tabular.setLeftLine(tabular.cellIndex(r, c), lineSet);
5327 case Tabular::SET_LINE_RIGHT:
5328 case Tabular::TOGGLE_LINE_RIGHT: {
5329 bool lineSet = (feature == Tabular::SET_LINE_RIGHT)
5330 ? (value == "true") : !tabular.rightLine(cur.idx());
5331 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5332 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5333 tabular.setRightLine(tabular.cellIndex(r, c), lineSet);
5337 case Tabular::M_ALIGN_LEFT:
5338 case Tabular::M_ALIGN_RIGHT:
5339 case Tabular::M_ALIGN_CENTER:
5340 case Tabular::ALIGN_LEFT:
5341 case Tabular::ALIGN_RIGHT:
5342 case Tabular::ALIGN_CENTER:
5343 case Tabular::ALIGN_BLOCK:
5344 case Tabular::ALIGN_DECIMAL:
5345 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5346 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5347 tabular.setAlignment(tabular.cellIndex(r, c), setAlign,
5348 !tabular.getPWidth(c).zero());
5351 case Tabular::M_VALIGN_TOP:
5352 case Tabular::M_VALIGN_BOTTOM:
5353 case Tabular::M_VALIGN_MIDDLE:
5355 case Tabular::VALIGN_TOP:
5356 case Tabular::VALIGN_BOTTOM:
5357 case Tabular::VALIGN_MIDDLE:
5358 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5359 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5360 tabular.setVAlignment(tabular.cellIndex(r, c), setVAlign, flag);
5363 case Tabular::SET_MULTICOLUMN: {
5364 if (!cur.selection()) {
5365 // just multicol for one single cell
5366 // check whether we are completely in a multicol
5367 if (!tabular.isMultiColumn(cur.idx()))
5368 tabular.setMultiColumn(cur.idx(), 1,
5369 tabular.rightLine(cur.idx()));
5372 // we have a selection so this means we just add all this
5373 // cells to form a multicolumn cell
5374 idx_type const s_start = cur.selBegin().idx();
5375 row_type const col_start = tabular.cellColumn(s_start);
5376 row_type const col_end = tabular.cellColumn(cur.selEnd().idx());
5377 cur.idx() = tabular.setMultiColumn(s_start, col_end - col_start + 1,
5378 tabular.rightLine(cur.selEnd().idx()));
5381 cur.setSelection(false);
5385 case Tabular::UNSET_MULTICOLUMN: {
5386 if (!cur.selection()) {
5387 if (tabular.isMultiColumn(cur.idx()))
5388 tabular.unsetMultiColumn(cur.idx());
5393 case Tabular::MULTICOLUMN: {
5394 if (!cur.selection()) {
5395 if (tabular.isMultiColumn(cur.idx()))
5396 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5398 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5402 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5403 row_type const r = sel_row_start;
5404 if (!tabular.isMultiColumn(tabular.cellIndex(r, c))
5405 || (r > sel_row_start && !tabular.isPartOfMultiColumn(r, c)))
5408 // If the selection contains at least one singlecol cell
5409 // or multiple multicol cells,
5410 // we assume the user will merge is to a single multicol
5412 tabularFeatures(cur, Tabular::SET_MULTICOLUMN);
5414 tabularFeatures(cur, Tabular::UNSET_MULTICOLUMN);
5418 case Tabular::SET_MULTIROW: {
5419 if (!cur.selection()) {
5420 // just multirow for one single cell
5421 // check whether we are completely in a multirow
5422 if (!tabular.isMultiRow(cur.idx()))
5423 tabular.setMultiRow(cur.idx(), 1,
5424 tabular.bottomLine(cur.idx()));
5427 // we have a selection so this means we just add all this
5428 // cells to form a multirow cell
5429 idx_type const s_start = cur.selBegin().idx();
5430 row_type const row_start = tabular.cellRow(s_start);
5431 row_type const row_end = tabular.cellRow(cur.selEnd().idx());
5432 cur.idx() = tabular.setMultiRow(s_start, row_end - row_start + 1,
5433 tabular.bottomLine(cur.selEnd().idx()));
5436 cur.setSelection(false);
5440 case Tabular::UNSET_MULTIROW: {
5441 if (!cur.selection()) {
5442 if (tabular.isMultiRow(cur.idx()))
5443 tabular.unsetMultiRow(cur.idx());
5448 case Tabular::MULTIROW: {
5449 if (!cur.selection()) {
5450 if (tabular.isMultiRow(cur.idx()))
5451 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5453 tabularFeatures(cur, Tabular::SET_MULTIROW);
5457 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5458 col_type const c = sel_col_start;
5459 if (!tabular.isMultiRow(tabular.cellIndex(r, c))
5460 || (r > sel_row_start && !tabular.isPartOfMultiRow(r, c)))
5463 // If the selection contains at least one singlerow cell
5464 // or multiple multirow cells,
5465 // we assume the user will merge is to a single multirow
5467 tabularFeatures(cur, Tabular::SET_MULTIROW);
5469 tabularFeatures(cur, Tabular::UNSET_MULTIROW);
5473 case Tabular::SET_ALL_LINES:
5475 case Tabular::UNSET_ALL_LINES:
5476 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5477 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5478 idx_type const cell = tabular.cellIndex(r, c);
5479 tabular.setTopLine(cell, setLines);
5480 tabular.setBottomLine(cell, setLines);
5481 tabular.setRightLine(cell, setLines);
5482 tabular.setLeftLine(cell, setLines);
5486 case Tabular::SET_BORDER_LINES:
5487 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5488 tabular.setLeftLine(tabular.cellIndex(r, sel_col_start), true);
5489 tabular.setRightLine(tabular.cellIndex(r, sel_col_end), true);
5491 for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
5492 tabular.setTopLine(tabular.cellIndex(sel_row_start, c), true);
5493 tabular.setBottomLine(tabular.cellIndex(sel_row_end, c), true);
5497 case Tabular::SET_LONGTABULAR:
5498 tabular.is_long_tabular = true;
5501 case Tabular::UNSET_LONGTABULAR:
5502 for (row_type r = 0; r < tabular.nrows(); ++r) {
5503 if (tabular.ltCaption(r)) {
5504 cur.idx() = tabular.cellIndex(r, 0);
5507 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
5510 tabular.is_long_tabular = false;
5513 case Tabular::SET_ROTATE_TABULAR:
5514 tabular.rotate = true;
5517 case Tabular::UNSET_ROTATE_TABULAR:
5518 tabular.rotate = false;
5521 case Tabular::TOGGLE_ROTATE_TABULAR:
5522 tabular.rotate = !tabular.rotate;
5525 case Tabular::TABULAR_VALIGN_TOP:
5526 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
5529 case Tabular::TABULAR_VALIGN_MIDDLE:
5530 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
5533 case Tabular::TABULAR_VALIGN_BOTTOM:
5534 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
5537 case Tabular::LONGTABULAR_ALIGN_LEFT:
5538 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
5541 case Tabular::LONGTABULAR_ALIGN_CENTER:
5542 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
5545 case Tabular::LONGTABULAR_ALIGN_RIGHT:
5546 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
5551 case Tabular::SET_ROTATE_CELL:
5552 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5553 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5554 tabular.setRotateCell(tabular.cellIndex(r, c), true);
5557 case Tabular::UNSET_ROTATE_CELL:
5558 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5559 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5560 tabular.setRotateCell(tabular.cellIndex(r, c), false);
5563 case Tabular::TOGGLE_ROTATE_CELL:
5565 bool oneNotRotated = oneCellHasRotationState(false,
5566 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
5568 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5569 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5570 tabular.setRotateCell(tabular.cellIndex(r, c),
5575 case Tabular::SET_USEBOX: {
5576 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
5577 if (val == tabular.getUsebox(cur.idx()))
5578 val = Tabular::BOX_NONE;
5579 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5580 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5581 tabular.setUsebox(tabular.cellIndex(r, c), val);
5585 case Tabular::UNSET_LTFIRSTHEAD:
5587 case Tabular::SET_LTFIRSTHEAD:
5588 tabular.getRowOfLTFirstHead(row, ltt);
5589 checkLongtableSpecial(ltt, value, flag);
5590 tabular.setLTHead(row, flag, ltt, true);
5593 case Tabular::UNSET_LTHEAD:
5595 case Tabular::SET_LTHEAD:
5596 tabular.getRowOfLTHead(row, ltt);
5597 checkLongtableSpecial(ltt, value, flag);
5598 tabular.setLTHead(row, flag, ltt, false);
5601 case Tabular::UNSET_LTFOOT:
5603 case Tabular::SET_LTFOOT:
5604 tabular.getRowOfLTFoot(row, ltt);
5605 checkLongtableSpecial(ltt, value, flag);
5606 tabular.setLTFoot(row, flag, ltt, false);
5609 case Tabular::UNSET_LTLASTFOOT:
5611 case Tabular::SET_LTLASTFOOT:
5612 tabular.getRowOfLTLastFoot(row, ltt);
5613 checkLongtableSpecial(ltt, value, flag);
5614 tabular.setLTFoot(row, flag, ltt, true);
5617 case Tabular::SET_LTNEWPAGE:
5618 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
5621 case Tabular::SET_LTCAPTION: {
5622 if (tabular.ltCaption(row))
5624 cur.idx() = tabular.setLTCaption(row, true);
5627 cur.setSelection(false);
5628 // If a row is set as caption, then also insert
5629 // a caption. Otherwise the LaTeX output is broken.
5630 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5631 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5635 case Tabular::UNSET_LTCAPTION: {
5636 if (!tabular.ltCaption(row))
5638 cur.idx() = tabular.setLTCaption(row, false);
5641 cur.setSelection(false);
5642 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5643 if (lyx::getStatus(fr).enabled())
5648 case Tabular::TOGGLE_LTCAPTION: {
5649 if (tabular.ltCaption(row))
5650 tabularFeatures(cur, Tabular::UNSET_LTCAPTION);
5652 tabularFeatures(cur, Tabular::SET_LTCAPTION);
5656 case Tabular::SET_BOOKTABS:
5657 tabular.use_booktabs = true;
5660 case Tabular::UNSET_BOOKTABS:
5661 tabular.use_booktabs = false;
5664 case Tabular::SET_TOP_SPACE: {
5666 if (value == "default")
5667 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5668 tabular.row_info[r].top_space_default = true;
5669 else if (value == "none")
5670 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5671 tabular.row_info[r].top_space_default = false;
5672 tabular.row_info[r].top_space = len;
5674 else if (isValidLength(value, &len))
5675 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5676 tabular.row_info[r].top_space_default = false;
5677 tabular.row_info[r].top_space = len;
5682 case Tabular::SET_BOTTOM_SPACE: {
5684 if (value == "default")
5685 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5686 tabular.row_info[r].bottom_space_default = true;
5687 else if (value == "none")
5688 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5689 tabular.row_info[r].bottom_space_default = false;
5690 tabular.row_info[r].bottom_space = len;
5692 else if (isValidLength(value, &len))
5693 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5694 tabular.row_info[r].bottom_space_default = false;
5695 tabular.row_info[r].bottom_space = len;
5700 case Tabular::SET_INTERLINE_SPACE: {
5702 if (value == "default")
5703 for (row_type r = sel_row_start; r <= sel_row_end; ++r)
5704 tabular.row_info[r].interline_space_default = true;
5705 else if (value == "none")
5706 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5707 tabular.row_info[r].interline_space_default = false;
5708 tabular.row_info[r].interline_space = len;
5710 else if (isValidLength(value, &len))
5711 for (row_type r = sel_row_start; r <= sel_row_end; ++r) {
5712 tabular.row_info[r].interline_space_default = false;
5713 tabular.row_info[r].interline_space = len;
5718 case Tabular::SET_DECIMAL_POINT:
5719 for (col_type c = sel_col_start; c <= sel_col_end; ++c)
5720 tabular.column_info[c].decimal_point = from_utf8(value);
5723 // dummy stuff just to avoid warnings
5724 case Tabular::LAST_ACTION:
5730 bool InsetTabular::copySelection(Cursor & cur)
5732 if (!cur.selection())
5737 getSelection(cur, rs, re, cs, ce);
5739 paste_tabular.reset(new Tabular(tabular));
5741 for (row_type r = 0; r < rs; ++r)
5742 paste_tabular->deleteRow(0);
5744 row_type const rows = re - rs + 1;
5745 while (paste_tabular->nrows() > rows)
5746 paste_tabular->deleteRow(rows);
5748 for (col_type c = 0; c < cs; ++c)
5749 paste_tabular->deleteColumn(0);
5751 col_type const columns = ce - cs + 1;
5752 while (paste_tabular->ncols() > columns)
5753 paste_tabular->deleteColumn(columns);
5755 paste_tabular->setBuffer(tabular.buffer());
5757 odocstringstream os;
5758 OutputParams const runparams(0);
5759 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5760 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5761 cap::copySelection(cur, os.str());
5763 // mark tabular stack dirty
5764 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5765 // when we (hopefully) have a one-for-all paste mechanism.
5766 // This must be called after cap::copySelection.
5767 dirtyTabularStack(true);
5773 bool InsetTabular::pasteClipboard(Cursor & cur)
5777 col_type actcol = tabular.cellColumn(cur.idx());
5778 row_type actrow = tabular.cellRow(cur.idx());
5780 if (cur.selIsMultiCell()) {
5783 getSelection(cur, actrow, re, actcol, ce);
5786 for (row_type r1 = 0, r2 = actrow;
5787 r1 < paste_tabular->nrows() && r2 < tabular.nrows();
5789 for (col_type c1 = 0, c2 = actcol;
5790 c1 < paste_tabular->ncols() && c2 < tabular.ncols();
5792 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5793 tabular.isPartOfMultiColumn(r2, c2))
5795 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5799 if (tabular.isPartOfMultiColumn(r2, c2)) {
5803 shared_ptr<InsetTableCell> inset(
5804 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5805 tabular.setCellInset(r2, c2, inset);
5806 // FIXME?: why do we need to do this explicitly? (EL)
5807 tabular.cellInset(r2, c2)->setBuffer(tabular.buffer());
5809 // FIXME: change tracking (MG)
5810 inset->setChange(Change(buffer().params().trackChanges ?
5811 Change::INSERTED : Change::UNCHANGED));
5819 void InsetTabular::cutSelection(Cursor & cur)
5821 if (!cur.selection())
5826 getSelection(cur, rs, re, cs, ce);
5827 for (row_type r = rs; r <= re; ++r) {
5828 for (col_type c = cs; c <= ce; ++c) {
5829 shared_ptr<InsetTableCell> t
5830 = cell(tabular.cellIndex(r, c));
5831 if (buffer().params().trackChanges)
5832 // FIXME: Change tracking (MG)
5833 t->setChange(Change(Change::DELETED));
5839 // cursor position might be invalid now
5840 if (cur.pit() > cur.lastpit())
5841 cur.pit() = cur.lastpit();
5842 if (cur.pos() > cur.lastpos())
5843 cur.pos() = cur.lastpos();
5844 cur.clearSelection();
5848 bool InsetTabular::isRightToLeft(Cursor & cur) const
5850 LASSERT(cur.depth() > 1, /**/);
5851 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5852 pos_type const parentpos = cur[cur.depth() - 2].pos();
5853 return parentpar.getFontSettings(buffer().params(),
5854 parentpos).language()->rightToLeft();
5858 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5861 LASSERT(stidx <= enidx, return docstring());
5863 col_type const col1 = tabular.cellColumn(stidx);
5864 col_type const col2 = tabular.cellColumn(enidx);
5865 row_type const row1 = tabular.cellRow(stidx);
5866 row_type const row2 = tabular.cellRow(enidx);
5868 for (col_type col = col1; col <= col2; col++)
5869 for (row_type row = row1; row <= row2; row++) {
5874 retval += tabular.cellInset(row, col)->asString(intoInsets);
5880 void InsetTabular::getSelection(Cursor & cur,
5881 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5883 CursorSlice const & beg = cur.selBegin();
5884 CursorSlice const & end = cur.selEnd();
5885 cs = tabular.cellColumn(beg.idx());
5886 ce = tabular.cellColumn(end.idx());
5890 rs = tabular.cellRow(beg.idx());
5891 re = tabular.cellRow(end.idx());
5897 Text * InsetTabular::getText(int idx) const
5899 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5903 void InsetTabular::setChange(Change const & change)
5905 for (idx_type idx = 0; idx < nargs(); ++idx)
5906 cell(idx)->setChange(change);
5910 void InsetTabular::acceptChanges()
5912 for (idx_type idx = 0; idx < nargs(); ++idx)
5913 cell(idx)->acceptChanges();
5917 void InsetTabular::rejectChanges()
5919 for (idx_type idx = 0; idx < nargs(); ++idx)
5920 cell(idx)->rejectChanges();
5924 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5926 return tabular.getPWidth(cell).zero();
5930 bool InsetTabular::forcePlainLayout(idx_type cell) const
5932 return !tabular.getPWidth(cell).zero();
5936 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5939 if (buf.length() <= 0)
5944 col_type maxCols = 1;
5945 size_t const len = buf.length();
5949 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5957 maxCols = max(cols, maxCols);
5963 maxCols = max(cols, maxCols);
5969 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5970 loctab = paste_tabular.get();
5972 dirtyTabularStack(true);
5975 cell = bv.cursor().idx();
5976 ocol = tabular.cellColumn(cell);
5977 row = tabular.cellRow(cell);
5981 idx_type const cells = loctab->numberofcells;
5984 rows = loctab->nrows();
5985 col_type const columns = loctab->ncols();
5987 while (cell < cells && p < len && row < rows &&
5988 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5994 // we can only set this if we are not too far right
5995 if (cols < columns) {
5996 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5997 Font const font = bv.textMetrics(&inset->text()).
5999 inset->setText(buf.substr(op, p - op), font,
6000 buffer().params().trackChanges);
6006 // we can only set this if we are not too far right
6007 if (cols < columns) {
6008 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
6009 Font const font = bv.textMetrics(&inset->text()).
6011 inset->setText(buf.substr(op, p - op), font,
6012 buffer().params().trackChanges);
6017 cell = loctab->cellIndex(row, cols);
6023 // check for the last cell if there is no trailing '\n'
6024 if (cell < cells && op < len) {
6025 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
6026 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
6027 inset->setText(buf.substr(op, len - op), font,
6028 buffer().params().trackChanges);
6034 void InsetTabular::addPreview(DocIterator const & inset_pos,
6035 PreviewLoader & loader) const
6037 DocIterator cell_pos = inset_pos;
6039 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
6040 for (row_type r = 0; r < tabular.nrows(); ++r) {
6041 for (col_type c = 0; c < tabular.ncols(); ++c) {
6042 cell_pos.top().idx() = tabular.cellIndex(r, c);
6043 tabular.cellInset(r, c)->addPreview(cell_pos, loader);
6049 bool InsetTabular::completionSupported(Cursor const & cur) const
6051 Cursor const & bvCur = cur.bv().cursor();
6052 if (&bvCur.inset() != this)
6054 return cur.text()->completionSupported(cur);
6058 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
6060 return completionSupported(cur);
6064 bool InsetTabular::automaticInlineCompletion() const
6066 return lyxrc.completion_inline_text;
6070 bool InsetTabular::automaticPopupCompletion() const
6072 return lyxrc.completion_popup_text;
6076 bool InsetTabular::showCompletionCursor() const
6078 return lyxrc.completion_cursor_text;
6082 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
6084 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
6088 docstring InsetTabular::completionPrefix(Cursor const & cur) const
6090 if (!completionSupported(cur))
6092 return cur.text()->completionPrefix(cur);
6096 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
6098 if (!completionSupported(cur))
6101 return cur.text()->insertCompletion(cur, s, finished);
6105 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
6106 Dimension & dim) const
6108 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
6109 tm.completionPosAndDim(cur, x, y, dim);
6113 void InsetTabular::string2params(string const & in, InsetTabular & inset)
6115 istringstream data(in);
6117 lex.setStream(data);
6124 if (!lex || token != "tabular") {
6125 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
6129 // This is part of the inset proper that is usually swallowed
6130 // by Buffer::readInset
6132 if (!lex || token != "Tabular") {
6133 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
6141 string InsetTabular::params2string(InsetTabular const & inset)
6144 data << "tabular" << ' ';
6146 data << "\\end_inset\n";