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 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lassert.h"
59 #include "support/lstrings.h"
61 #include <boost/scoped_ptr.hpp>
69 using namespace lyx::support;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
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; // horiz 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;
106 TabularFeature tabularFeature[] =
108 { Tabular::APPEND_ROW, "append-row" },
109 { Tabular::APPEND_COLUMN, "append-column" },
110 { Tabular::DELETE_ROW, "delete-row" },
111 { Tabular::DELETE_COLUMN, "delete-column" },
112 { Tabular::COPY_ROW, "copy-row" },
113 { Tabular::COPY_COLUMN, "copy-column" },
114 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
115 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
116 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
117 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
118 { Tabular::ALIGN_LEFT, "align-left" },
119 { Tabular::ALIGN_RIGHT, "align-right" },
120 { Tabular::ALIGN_CENTER, "align-center" },
121 { Tabular::ALIGN_BLOCK, "align-block" },
122 { Tabular::VALIGN_TOP, "valign-top" },
123 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
124 { Tabular::VALIGN_MIDDLE, "valign-middle" },
125 { Tabular::M_ALIGN_LEFT, "m-align-left" },
126 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
127 { Tabular::M_ALIGN_CENTER, "m-align-center" },
128 { Tabular::M_VALIGN_TOP, "m-valign-top" },
129 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
130 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
131 { Tabular::MULTICOLUMN, "multicolumn" },
132 { Tabular::SET_ALL_LINES, "set-all-lines" },
133 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
134 { Tabular::SET_LONGTABULAR, "set-longtabular" },
135 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
136 { Tabular::SET_PWIDTH, "set-pwidth" },
137 { Tabular::SET_MPWIDTH, "set-mpwidth" },
138 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
139 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
140 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
141 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
142 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
143 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
144 { Tabular::SET_USEBOX, "set-usebox" },
145 { Tabular::SET_LTHEAD, "set-lthead" },
146 { Tabular::UNSET_LTHEAD, "unset-lthead" },
147 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
148 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
149 { Tabular::SET_LTFOOT, "set-ltfoot" },
150 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
151 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
152 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
153 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
154 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::SET_BORDER_LINES, "set-border-lines" },
163 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top"},
164 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle"},
165 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom"},
166 { Tabular::LAST_ACTION, "" }
170 class FeatureEqual : public unary_function<TabularFeature, bool> {
172 FeatureEqual(Tabular::Feature feature)
173 : feature_(feature) {}
174 bool operator()(TabularFeature const & tf) const {
175 return tf.action == feature_;
178 Tabular::Feature feature_;
183 string const write_attribute(string const & name, T const & t)
185 string const s = tostr(t);
186 return s.empty() ? s : " " + name + "=\"" + s + "\"";
190 string const write_attribute(string const & name, string const & t)
192 return t.empty() ? t : " " + name + "=\"" + t + "\"";
197 string const write_attribute(string const & name, docstring const & t)
199 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
204 string const write_attribute(string const & name, bool const & b)
206 // we write only true attribute values so we remove a bit of the
207 // file format bloat for tabulars.
208 return b ? write_attribute(name, convert<string>(b)) : string();
213 string const write_attribute(string const & name, int const & i)
215 // we write only true attribute values so we remove a bit of the
216 // file format bloat for tabulars.
217 return i ? write_attribute(name, convert<string>(i)) : string();
222 string const write_attribute(string const & name, Tabular::idx_type 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, Length const & value)
233 // we write only the value if we really have one same reson as above.
234 return value.zero() ? string() : write_attribute(name, value.asString());
238 string const tostr(LyXAlignment const & num)
243 case LYX_ALIGN_BLOCK:
247 case LYX_ALIGN_CENTER:
249 case LYX_ALIGN_RIGHT:
251 case LYX_ALIGN_LAYOUT:
253 case LYX_ALIGN_SPECIAL:
260 string const tostr(Tabular::VAlignment const & num)
263 case Tabular::LYX_VALIGN_TOP:
265 case Tabular::LYX_VALIGN_MIDDLE:
267 case Tabular::LYX_VALIGN_BOTTOM:
274 string const tostr(Tabular::BoxType const & num)
277 case Tabular::BOX_NONE:
279 case Tabular::BOX_PARBOX:
281 case Tabular::BOX_MINIPAGE:
288 // I would have liked a fromstr template a lot better. (Lgb)
289 bool string2type(string const str, LyXAlignment & num)
292 num = LYX_ALIGN_NONE;
293 else if (str == "block")
294 num = LYX_ALIGN_BLOCK;
295 else if (str == "left")
296 num = LYX_ALIGN_LEFT;
297 else if (str == "center")
298 num = LYX_ALIGN_CENTER;
299 else if (str == "right")
300 num = LYX_ALIGN_RIGHT;
307 bool string2type(string const str, Tabular::VAlignment & num)
310 num = Tabular::LYX_VALIGN_TOP;
311 else if (str == "middle" )
312 num = Tabular::LYX_VALIGN_MIDDLE;
313 else if (str == "bottom")
314 num = Tabular::LYX_VALIGN_BOTTOM;
321 bool string2type(string const str, Tabular::BoxType & num)
324 num = Tabular::BOX_NONE;
325 else if (str == "parbox")
326 num = Tabular::BOX_PARBOX;
327 else if (str == "minipage")
328 num = Tabular::BOX_MINIPAGE;
335 bool string2type(string const str, bool & num)
339 else if (str == "false")
347 bool getTokenValue(string const & str, char const * token, string & ret)
350 size_t token_length = strlen(token);
351 size_t pos = str.find(token);
353 if (pos == string::npos || pos + token_length + 1 >= str.length()
354 || str[pos + token_length] != '=')
356 pos += token_length + 1;
358 if (ch != '"' && ch != '\'') { // only read till next space
362 while (pos < str.length() - 1 && str[++pos] != ch)
369 bool getTokenValue(string const & str, char const * token, docstring & ret)
372 bool const success = getTokenValue(str, token, tmp);
373 ret = from_utf8(tmp);
378 bool getTokenValue(string const & str, char const * token, int & num)
382 if (!getTokenValue(str, token, tmp))
384 num = convert<int>(tmp);
389 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
392 return getTokenValue(str, token, tmp) && string2type(tmp, num);
396 bool getTokenValue(string const & str, char const * token,
397 Tabular::VAlignment & num)
400 return getTokenValue(str, token, tmp) && string2type(tmp, num);
404 bool getTokenValue(string const & str, char const * token,
405 Tabular::BoxType & num)
408 return getTokenValue(str, token, tmp) && string2type(tmp, num);
412 bool getTokenValue(string const & str, char const * token, bool & flag)
414 // set the flag always to false as this should be the default for bools
415 // not in the file-format.
418 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
422 bool getTokenValue(string const & str, char const * token, Length & len)
424 // set the length to be zero() as default as this it should be if not
425 // in the file format.
428 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
432 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
437 if (!getTokenValue(str, token, tmp))
439 if (tmp == "default") {
443 return isValidLength(tmp, &len);
447 void l_getline(istream & is, string & str)
450 while (str.empty()) {
452 if (!str.empty() && str[str.length() - 1] == '\r')
453 str.erase(str.length() - 1);
460 string const featureAsString(Tabular::Feature feature)
462 TabularFeature * end = tabularFeature +
463 sizeof(tabularFeature) / sizeof(TabularFeature);
464 TabularFeature * it = find_if(tabularFeature, end,
465 FeatureEqual(feature));
466 return (it == end) ? string() : it->feature;
471 /////////////////////////////////////////////////////////////////////
475 /////////////////////////////////////////////////////////////////////
478 Tabular::CellData::CellData(Buffer & buf)
481 multicolumn(Tabular::CELL_NORMAL),
482 alignment(LYX_ALIGN_CENTER),
483 valignment(LYX_VALIGN_TOP),
490 inset(new InsetTableCell(buf))
492 inset->setBuffer(const_cast<Buffer &>(buf));
493 inset->paragraphs().back().setLayout(buf.params().documentClass().plainLayout());
497 Tabular::CellData::CellData(CellData const & cs)
500 multicolumn(cs.multicolumn),
501 alignment(cs.alignment),
502 valignment(cs.valignment),
503 top_line(cs.top_line),
504 bottom_line(cs.bottom_line),
505 left_line(cs.left_line),
506 right_line(cs.right_line),
509 align_special(cs.align_special),
511 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
515 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
521 void Tabular::CellData::swap(CellData & rhs)
523 std::swap(cellno, rhs.cellno);
524 std::swap(width, rhs.width);
525 std::swap(multicolumn, rhs.multicolumn);
526 std::swap(alignment, rhs.alignment);
527 std::swap(valignment, rhs.valignment);
528 std::swap(top_line, rhs.top_line);
529 std::swap(bottom_line, rhs.bottom_line);
530 std::swap(left_line, rhs.left_line);
531 std::swap(right_line, rhs.right_line);
532 std::swap(usebox, rhs.usebox);
533 std::swap(rotate, rhs.rotate);
534 std::swap(align_special, rhs.align_special);
535 p_width.swap(rhs.p_width);
536 inset.swap(rhs.inset);
540 Tabular::RowData::RowData()
543 top_space_default(false),
544 bottom_space_default(false),
545 interline_space_default(false),
555 Tabular::ColumnData::ColumnData()
556 : alignment(LYX_ALIGN_CENTER),
557 valignment(LYX_VALIGN_TOP),
563 Tabular::ltType::ltType()
570 Tabular::Tabular(Buffer & buffer, row_type rows_arg, col_type columns_arg)
572 init(buffer, rows_arg, columns_arg);
576 void Tabular::setBuffer(Buffer & buffer)
579 size_t row_count = row_info.size();
580 size_t column_count = column_info.size();
581 // set silly default lines
582 for (row_type i = 0; i < row_count; ++i)
583 for (col_type j = 0; j < column_count; ++j)
584 cell_info[i][j].inset->setBuffer(*buffer_);
588 // activates all lines and sets all widths to 0
589 void Tabular::init(Buffer & buf, row_type rows_arg,
590 col_type columns_arg)
593 row_info = row_vector(rows_arg);
594 column_info = column_vector(columns_arg);
595 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
596 row_info.reserve(10);
597 column_info.reserve(10);
598 cell_info.reserve(100);
600 is_long_tabular = false;
601 tabular_valignment = LYX_VALIGN_MIDDLE;
603 use_booktabs = false;
604 size_t row_count = row_info.size();
605 size_t column_count = column_info.size();
606 // set silly default lines
607 for (row_type i = 0; i < row_count; ++i)
608 for (col_type j = 0; j < column_count; ++j) {
609 cell_info[i][j].inset->setBuffer(*buffer_);
610 cell_info[i][j].top_line = true;
611 cell_info[i][j].left_line = true;
612 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
613 cell_info[i][j].right_line = j == column_count - 1;
618 void Tabular::appendRow(idx_type const cell)
620 BufferParams const & bp = buffer().params();
621 row_type const row = cellRow(cell);
623 row_vector::iterator rit = row_info.begin() + row;
624 row_info.insert(rit, RowData());
625 // now set the values of the row before
626 row_info[row] = row_info[row + 1];
628 row_type const nrows = row_info.size();
629 col_type const ncols = column_info.size();
631 cell_vvector old(nrows - 1);
632 for (row_type i = 0; i < nrows - 1; ++i)
633 swap(cell_info[i], old[i]);
635 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer())));
637 for (row_type i = 0; i <= row; ++i)
638 swap(cell_info[i], old[i]);
639 for (row_type i = row + 2; i < nrows; ++i)
640 swap(cell_info[i], old[i - 1]);
643 for (col_type c = 0; c < ncols; ++c) {
644 // inherit line settings
645 idx_type const i = cellIndex(row + 1, c);
646 idx_type const j = cellIndex(row, c);
647 setLeftLine(i, leftLine(j));
648 setRightLine(i, rightLine(j));
649 setTopLine(i, topLine(j));
650 if (topLine(j) && bottomLine(j)) {
651 setBottomLine(i, true);
652 setBottomLine(j, false);
654 // mark track changes
656 cellInfo(i).inset->setChange(Change(Change::INSERTED));
661 void Tabular::deleteRow(row_type const row)
663 // Not allowed to delete last row
664 if (row_info.size() == 1)
667 row_info.erase(row_info.begin() + row);
668 cell_info.erase(cell_info.begin() + row);
673 void Tabular::copyRow(row_type const row)
675 row_info.insert(row_info.begin() + row, row_info[row]);
676 cell_info.insert(cell_info.begin() + row, cell_info[row]);
678 if (buffer().params().trackChanges)
679 for (col_type j = 0; j < column_info.size(); ++j)
680 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
686 void Tabular::appendColumn(idx_type const cell)
688 col_type const c = cellColumn(cell);
689 column_vector::iterator cit = column_info.begin() + c + 1;
690 column_info.insert(cit, ColumnData());
691 row_type const nrows = row_info.size();
692 // set the column values of the column before
693 column_info[c + 1] = column_info[c];
695 for (row_type r = 0; r < nrows; ++r) {
696 cell_info[r].insert(cell_info[r].begin() + c + 1,
699 // FIXME: This code does not work. It deletes the cell's content and
700 // it triggers an assertion if the cursor is at pos > 0.
701 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
702 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
704 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
708 for (row_type r = 0; r < nrows; ++r) {
709 // inherit line settings
710 idx_type const i = cellIndex(r, c + 1);
711 idx_type const j = cellIndex(r, c);
712 setBottomLine(i, bottomLine(j));
713 setTopLine(i, topLine(j));
714 setLeftLine(i, leftLine(j));
715 if (rightLine(j) && rightLine(j)) {
716 setRightLine(i, true);
717 setRightLine(j, false);
720 cellInfo(i).inset->clear();
721 if (buffer().params().trackChanges)
722 cellInfo(i).inset->setChange(Change(Change::INSERTED));
727 void Tabular::deleteColumn(col_type const column)
729 // Not allowed to delete last column
730 if (column_info.size() == 1)
733 size_t const row_count = row_info.size();
734 for (row_type i = 0; i < row_count; ++i) {
735 // Care about multicolumn cells
736 if (column + 1 < column_info.size() &&
737 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
738 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
739 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
741 cell_info[i].erase(cell_info[i].begin() + column);
743 column_info.erase(column_info.begin() + column);
748 void Tabular::copyColumn(col_type const column)
750 BufferParams const & bp = buffer().params();
751 column_info.insert(column_info.begin() + column, column_info[column]);
753 size_t row_count = row_info.size();
754 for (row_type i = 0; i < row_count; ++i)
755 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
758 for (row_type i = 0; i < row_count; ++i)
759 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
764 void Tabular::updateIndexes()
766 col_type ncols = column_info.size();
767 row_type nrows = row_info.size();
769 for (row_type row = 0; row < nrows; ++row)
770 for (col_type column = 0; column < ncols; ++column) {
771 if (!isPartOfMultiColumn(row, column))
773 cell_info[row][column].cellno = numberofcells - 1;
776 rowofcell.resize(numberofcells);
777 columnofcell.resize(numberofcells);
779 for (row_type row = 0; row < nrows; ++row)
780 for (col_type column = 0; column < ncols; ++column) {
781 if (isPartOfMultiColumn(row, column))
784 columnofcell[i] = column;
785 setFixedWidth(row, column);
786 updateContentAlignment(row, column);
792 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
794 row_type const row = cellRow(cell);
796 for (col_type i = 0; i < column_info.size(); ++i)
797 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
803 bool Tabular::topLine(idx_type const cell) const
805 return cellInfo(cell).top_line;
809 bool Tabular::bottomLine(idx_type const cell) const
811 return cellInfo(cell).bottom_line;
815 bool Tabular::leftLine(idx_type cell) const
819 return cellInfo(cell).left_line;
823 bool Tabular::rightLine(idx_type cell) const
827 return cellInfo(cell).right_line;
831 bool Tabular::topAlreadyDrawn(idx_type cell) const
833 row_type row = cellRow(cell);
836 idx_type i = cellIndex(row - 1, cellColumn(cell));
837 return !rowTopLine(row) && bottomLine(i);
841 bool Tabular::leftAlreadyDrawn(idx_type cell) const
843 col_type col = cellColumn(cell);
846 idx_type i = cellIndex(cellRow(cell), col - 1);
847 return rightLine(i) && !leftLine(cell);
851 bool Tabular::isLastRow(idx_type cell) const
853 return cellRow(cell) == row_info.size() - 1;
857 int Tabular::getAdditionalHeight(row_type row) const
859 if (!row || row >= row_info.size())
862 int const interline_space = row_info[row - 1].interline_space_default ?
864 row_info[row - 1].interline_space.inPixels(width());
865 if (rowTopLine(row) && rowBottomLine(row - 1))
866 return interline_space + WIDTH_OF_LINE;
867 return interline_space;
871 int Tabular::getAdditionalWidth(idx_type cell) const
873 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
875 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
876 return WIDTH_OF_LINE;
881 int Tabular::columnWidth(idx_type cell) const
884 col_type const span = columnSpan(cell);
885 col_type const col = cellColumn(cell);
886 for(col_type c = col; c < col + span ; ++c)
887 w += column_info[c].width;
892 bool Tabular::updateColumnWidths()
894 col_type const ncols = column_info.size();
895 row_type const nrows = row_info.size();
897 // for each col get max of single col cells
898 for(col_type c = 0; c < ncols; ++c) {
900 for(row_type r = 0; r < nrows; ++r) {
901 idx_type const i = cellIndex(r, c);
902 if (columnSpan(i) == 1)
903 new_width = max(new_width, cellInfo(i).width);
906 if (column_info[c].width != new_width) {
907 column_info[c].width = new_width;
911 // update col widths to fit merged cells
912 for(col_type c = 0; c < ncols; ++c)
913 for(row_type r = 0; r < nrows; ++r) {
914 idx_type const i = cellIndex(r, c);
915 int const span = columnSpan(i);
916 if (span == 1 || c > cellColumn(i))
920 for(col_type j = c; j < c + span ; ++j)
921 old_width += column_info[j].width;
923 if (cellInfo(i).width > old_width) {
924 column_info[c + span - 1].width += cellInfo(i).width - old_width;
933 int Tabular::width() const
935 col_type const ncols = column_info.size();
937 for (col_type i = 0; i < ncols; ++i)
938 width += column_info[i].width;
943 void Tabular::setCellWidth(idx_type cell, int new_width)
945 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
946 + getAdditionalWidth(cell);
950 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
953 if (!isMultiColumn(cell) || onlycolumn)
954 column_info[cellColumn(cell)].alignment = align;
956 cellInfo(cell).alignment = align;
957 cellInset(cell).get()->setContentAlignment(align);
961 void Tabular::setVAlignment(idx_type cell, VAlignment align,
964 if (!isMultiColumn(cell) || onlycolumn)
965 column_info[cellColumn(cell)].valignment = align;
967 cellInfo(cell).valignment = align;
974 * Allow line and paragraph breaks for fixed width cells or disallow them,
975 * merge cell paragraphs and reset layout to standard for variable width
978 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
980 inset->setAutoBreakRows(fixedWidth);
981 inset->toggleFixedWidth(fixedWidth);
985 // merge all paragraphs to one
986 BufferParams const & bp = cur.bv().buffer().params();
987 while (inset->paragraphs().size() > 1)
988 mergeParagraph(bp, inset->paragraphs(), 0);
992 // undo information has already been recorded
993 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
994 bp.documentClass().plainLayoutName());
1001 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1002 Length const & width)
1004 col_type const j = cellColumn(cell);
1006 column_info[j].p_width = width;
1007 for (row_type i = 0; i < row_info.size(); ++i) {
1008 idx_type const cell = cellIndex(i, j);
1009 // because of multicolumns
1010 toggleFixedWidth(cur, cellInset(cell).get(),
1011 !getPWidth(cell).zero());
1013 // cur paragraph can become invalid after paragraphs were merged
1014 if (cur.pit() > cur.lastpit())
1015 cur.pit() = cur.lastpit();
1016 // cur position can become invalid after newlines were removed
1017 if (cur.pos() > cur.lastpos())
1018 cur.pos() = cur.lastpos();
1022 bool Tabular::setFixedWidth(row_type r, col_type c)
1024 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1025 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1026 || (multicol && !cell_info[r][c].p_width.zero());
1027 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1032 void Tabular::updateContentAlignment(row_type r, col_type c)
1034 cell_info[r][c].inset->setContentAlignment(
1035 getAlignment(cellIndex(r, c)));
1039 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1040 Length const & width)
1042 if (!isMultiColumn(cell))
1045 cellInfo(cell).p_width = width;
1046 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1047 // cur paragraph can become invalid after paragraphs were merged
1048 if (cur.pit() > cur.lastpit())
1049 cur.pit() = cur.lastpit();
1050 // cur position can become invalid after newlines were removed
1051 if (cur.pos() > cur.lastpos())
1052 cur.pos() = cur.lastpos();
1057 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1058 Tabular::Feature what)
1060 if (what == SET_SPECIAL_MULTI)
1061 cellInfo(cell).align_special = special;
1063 column_info[cellColumn(cell)].align_special = special;
1067 void Tabular::setAllLines(idx_type cell, bool line)
1069 setTopLine(cell, line);
1070 setBottomLine(cell, line);
1071 setRightLine(cell, line);
1072 setLeftLine(cell, line);
1076 void Tabular::setTopLine(idx_type i, bool line)
1078 cellInfo(i).top_line = line;
1082 void Tabular::setBottomLine(idx_type i, bool line)
1084 cellInfo(i).bottom_line = line;
1088 void Tabular::setLeftLine(idx_type cell, bool line)
1090 cellInfo(cell).left_line = line;
1094 void Tabular::setRightLine(idx_type cell, bool line)
1096 cellInfo(cell).right_line = line;
1099 bool Tabular::rowTopLine(row_type r) const
1101 idx_type i0 = getFirstCellInRow(r);
1102 idx_type i1 = getLastCellInRow(r);
1103 bool all_rows_set = true;
1104 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1105 all_rows_set = cellInfo(j).top_line;
1106 return all_rows_set;
1110 bool Tabular::rowBottomLine(row_type r) const
1112 idx_type i0 = getFirstCellInRow(r);
1113 idx_type i1 = getLastCellInRow(r);
1114 bool all_rows_set = true;
1115 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1116 all_rows_set = cellInfo(j).bottom_line;
1117 return all_rows_set;
1121 bool Tabular::columnLeftLine(col_type c) const
1128 row_type const nrows = row_info.size();
1129 for (row_type r = 0; r < nrows; ++r) {
1130 idx_type i = cellIndex(r, c);
1131 if (c == cellColumn(i)) {
1133 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1134 if (cellInfo(i).left_line || right)
1138 return 2 * nrows_left >= total;
1142 bool Tabular::columnRightLine(col_type c) const
1147 int nrows_right = 0;
1149 row_type const nrows = row_info.size();
1150 for (row_type r = 0; r < nrows; ++r) {
1151 idx_type i = cellIndex(r, c);
1152 if (c == cellColumn(i) + columnSpan(i) - 1) {
1154 bool left = (c + 1 < column_info.size()
1155 && cellInfo(cellIndex(r, c + 1)).left_line)
1156 || c + 1 == column_info.size();
1157 if (cellInfo(i).right_line && left)
1161 return 2 * nrows_right >= total;
1165 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1167 if (!onlycolumn && isMultiColumn(cell))
1168 return cellInfo(cell).alignment;
1169 return column_info[cellColumn(cell)].alignment;
1174 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1176 if (!onlycolumn && isMultiColumn(cell))
1177 return cellInfo(cell).valignment;
1178 return column_info[cellColumn(cell)].valignment;
1182 Length const Tabular::getPWidth(idx_type cell) const
1184 if (isMultiColumn(cell))
1185 return cellInfo(cell).p_width;
1186 return column_info[cellColumn(cell)].p_width;
1190 int Tabular::cellWidth(idx_type cell) const
1192 return cellInfo(cell).width;
1196 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1200 switch (getAlignment(cell)) {
1201 case LYX_ALIGN_CENTER:
1202 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1204 case LYX_ALIGN_RIGHT:
1205 x += columnWidth(cell) - cellWidth(cell);
1206 // + getAdditionalWidth(cell);
1209 // LYX_ALIGN_LEFT: nothing :-)
1213 // the LaTeX Way :-(
1219 bool Tabular::isFirstCellInRow(idx_type cell) const
1221 return cellColumn(cell) == 0;
1225 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1227 if (row > row_info.size() - 1)
1228 row = row_info.size() - 1;
1229 return cell_info[row][0].cellno;
1233 bool Tabular::isLastCellInRow(idx_type cell) const
1235 return cellRightColumn(cell) == column_info.size() - 1;
1239 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1241 if (row > row_info.size() - 1)
1242 row = row_info.size() - 1;
1243 return cell_info[row][column_info.size() - 1].cellno;
1247 Tabular::row_type Tabular::cellRow(idx_type cell) const
1249 if (cell >= numberofcells)
1250 return row_info.size() - 1;
1253 return rowofcell[cell];
1257 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1259 if (cell >= numberofcells)
1260 return column_info.size() - 1;
1263 return columnofcell[cell];
1267 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1269 row_type const row = cellRow(cell);
1270 col_type column = cellColumn(cell);
1271 while (column < column_info.size() - 1 &&
1272 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1278 void Tabular::write(ostream & os) const
1282 << write_attribute("version", 3)
1283 << write_attribute("rows", row_info.size())
1284 << write_attribute("columns", column_info.size())
1286 // global longtable options
1288 << write_attribute("rotate", rotate)
1289 << write_attribute("booktabs", use_booktabs)
1290 << write_attribute("islongtable", is_long_tabular)
1291 << write_attribute("tabularvalignment", tabular_valignment)
1292 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1293 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1294 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1295 << write_attribute("headTopDL", endhead.topDL)
1296 << write_attribute("headBottomDL", endhead.bottomDL)
1297 << write_attribute("footTopDL", endfoot.topDL)
1298 << write_attribute("footBottomDL", endfoot.bottomDL)
1299 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1300 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1301 << write_attribute("lastFootEmpty", endlastfoot.empty)
1303 for (col_type j = 0; j < column_info.size(); ++j) {
1305 << write_attribute("alignment", column_info[j].alignment)
1306 << write_attribute("valignment", column_info[j].valignment)
1307 << write_attribute("width", column_info[j].p_width.asString())
1308 << write_attribute("special", column_info[j].align_special)
1311 for (row_type i = 0; i < row_info.size(); ++i) {
1312 static const string def("default");
1314 if (row_info[i].top_space_default)
1315 os << write_attribute("topspace", def);
1317 os << write_attribute("topspace", row_info[i].top_space);
1318 if (row_info[i].bottom_space_default)
1319 os << write_attribute("bottomspace", def);
1321 os << write_attribute("bottomspace", row_info[i].bottom_space);
1322 if (row_info[i].interline_space_default)
1323 os << write_attribute("interlinespace", def);
1325 os << write_attribute("interlinespace", row_info[i].interline_space);
1326 os << write_attribute("endhead", row_info[i].endhead)
1327 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1328 << write_attribute("endfoot", row_info[i].endfoot)
1329 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1330 << write_attribute("newpage", row_info[i].newpage)
1331 << write_attribute("caption", row_info[i].caption)
1333 for (col_type j = 0; j < column_info.size(); ++j) {
1335 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1336 << write_attribute("alignment", cell_info[i][j].alignment)
1337 << write_attribute("valignment", cell_info[i][j].valignment)
1338 << write_attribute("topline", cell_info[i][j].top_line)
1339 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1340 << write_attribute("leftline", cell_info[i][j].left_line)
1341 << write_attribute("rightline", cell_info[i][j].right_line)
1342 << write_attribute("rotate", cell_info[i][j].rotate)
1343 << write_attribute("usebox", cell_info[i][j].usebox)
1344 << write_attribute("width", cell_info[i][j].p_width)
1345 << write_attribute("special", cell_info[i][j].align_special)
1347 os << "\\begin_inset ";
1348 cell_info[i][j].inset->write(os);
1349 os << "\n\\end_inset\n"
1354 os << "</lyxtabular>\n";
1358 void Tabular::read(Lexer & lex)
1361 istream & is = lex.getStream();
1363 l_getline(is, line);
1364 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1365 LASSERT(false, /**/);
1370 if (!getTokenValue(line, "version", version))
1372 LASSERT(version >= 2, /**/);
1375 if (!getTokenValue(line, "rows", rows_arg))
1378 if (!getTokenValue(line, "columns", columns_arg))
1380 init(buffer(), rows_arg, columns_arg);
1381 l_getline(is, line);
1382 if (!prefixIs(line, "<features")) {
1383 lyxerr << "Wrong tabular format (expected <features ...> got"
1384 << line << ')' << endl;
1387 getTokenValue(line, "rotate", rotate);
1388 getTokenValue(line, "booktabs", use_booktabs);
1389 getTokenValue(line, "islongtable", is_long_tabular);
1390 getTokenValue(line, "tabularvalignment", tabular_valignment);
1391 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1392 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1393 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1394 getTokenValue(line, "headTopDL", endhead.topDL);
1395 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1396 getTokenValue(line, "footTopDL", endfoot.topDL);
1397 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1398 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1399 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1400 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1402 for (col_type j = 0; j < column_info.size(); ++j) {
1404 if (!prefixIs(line,"<column")) {
1405 lyxerr << "Wrong tabular format (expected <column ...> got"
1406 << line << ')' << endl;
1409 getTokenValue(line, "alignment", column_info[j].alignment);
1410 getTokenValue(line, "valignment", column_info[j].valignment);
1411 getTokenValue(line, "width", column_info[j].p_width);
1412 getTokenValue(line, "special", column_info[j].align_special);
1415 for (row_type i = 0; i < row_info.size(); ++i) {
1416 l_getline(is, line);
1417 if (!prefixIs(line, "<row")) {
1418 lyxerr << "Wrong tabular format (expected <row ...> got"
1419 << line << ')' << endl;
1422 getTokenValue(line, "topspace", row_info[i].top_space,
1423 row_info[i].top_space_default);
1424 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1425 row_info[i].bottom_space_default);
1426 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1427 row_info[i].interline_space_default);
1428 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1429 getTokenValue(line, "endhead", row_info[i].endhead);
1430 getTokenValue(line, "endfoot", row_info[i].endfoot);
1431 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1432 getTokenValue(line, "newpage", row_info[i].newpage);
1433 getTokenValue(line, "caption", row_info[i].caption);
1434 for (col_type j = 0; j < column_info.size(); ++j) {
1435 l_getline(is, line);
1436 if (!prefixIs(line, "<cell")) {
1437 lyxerr << "Wrong tabular format (expected <cell ...> got"
1438 << line << ')' << endl;
1441 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1442 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1443 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1444 getTokenValue(line, "topline", cell_info[i][j].top_line);
1445 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1446 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1447 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1448 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1449 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1450 getTokenValue(line, "width", cell_info[i][j].p_width);
1452 getTokenValue(line, "special", cell_info[i][j].align_special);
1453 l_getline(is, line);
1454 if (prefixIs(line, "\\begin_inset")) {
1455 cell_info[i][j].inset->setBuffer(*buffer_);
1456 cell_info[i][j].inset->read(lex);
1457 l_getline(is, line);
1459 if (!prefixIs(line, "</cell>")) {
1460 lyxerr << "Wrong tabular format (expected </cell> got"
1461 << line << ')' << endl;
1465 l_getline(is, line);
1466 if (!prefixIs(line, "</row>")) {
1467 lyxerr << "Wrong tabular format (expected </row> got"
1468 << line << ')' << endl;
1472 while (!prefixIs(line, "</lyxtabular>")) {
1473 l_getline(is, line);
1479 bool Tabular::isMultiColumn(idx_type cell) const
1481 return cellInfo(cell).multicolumn != CELL_NORMAL;
1485 bool Tabular::isMultiColumnReal(idx_type cell) const
1487 return cellColumn(cell) != cellRightColumn(cell) &&
1488 cellInfo(cell).multicolumn != CELL_NORMAL;
1492 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1494 return cell_info[cellRow(cell)][cellColumn(cell)];
1498 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1500 CellData & cs = cellInfo(cell);
1501 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1502 cs.alignment = column_info[cellColumn(cell)].alignment;
1503 setRightLine(cell, rightLine(cell + number - 1));
1504 for (idx_type i = 1; i < number; ++i) {
1505 CellData & cs1 = cellInfo(cell + i);
1506 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1507 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1514 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1516 row_type const row = cellRow(cell);
1517 col_type const ncols = column_info.size();
1518 idx_type result = 1;
1519 col_type column = cellColumn(cell) + 1;
1520 while (column < ncols && isPartOfMultiColumn(row, column)) {
1528 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1530 row_type const row = cellRow(cell);
1531 col_type column = cellColumn(cell);
1533 idx_type result = 0;
1535 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1536 cell_info[row][column].multicolumn = CELL_NORMAL;
1538 while (column < column_info.size() &&
1539 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1541 cell_info[row][column].multicolumn = CELL_NORMAL;
1551 void Tabular::setRotateCell(idx_type cell, bool flag)
1553 cellInfo(cell).rotate = flag;
1557 bool Tabular::getRotateCell(idx_type cell) const
1559 return cellInfo(cell).rotate;
1563 bool Tabular::needRotating() const
1567 for (row_type i = 0; i < row_info.size(); ++i)
1568 for (col_type j = 0; j < column_info.size(); ++j)
1569 if (cell_info[i][j].rotate)
1575 bool Tabular::isLastCell(idx_type cell) const
1577 if (cell + 1 < numberofcells)
1583 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1585 if (cellRow(cell) > 0)
1586 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1591 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1593 if (cellRow(cell) + 1 < row_info.size())
1594 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1599 Tabular::idx_type Tabular::cellIndex(row_type row,
1600 col_type column) const
1602 BOOST_ASSERT(column != npos && column < column_info.size()
1603 && row != npos && row < row_info.size());
1604 return cell_info[row][column].cellno;
1608 void Tabular::setUsebox(idx_type cell, BoxType type)
1610 cellInfo(cell).usebox = type;
1614 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1615 // adds line breaks, see bug 4886.
1616 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1618 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1619 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1621 if (cellInfo(cell).usebox > 1)
1622 return cellInfo(cell).usebox;
1623 return useParbox(cell);
1628 // This are functions used for the longtable support
1630 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1636 row_info[row].endfirsthead = flag;
1640 row_info[row].endhead = flag;
1645 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1648 hd.set = haveLTHead();
1649 return row_info[row].endhead;
1653 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1656 hd.set = haveLTFirstHead();
1657 return row_info[row].endfirsthead;
1661 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1667 row_info[row].endlastfoot = flag;
1671 row_info[row].endfoot = flag;
1676 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1679 fd.set = haveLTFoot();
1680 return row_info[row].endfoot;
1684 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1687 fd.set = haveLTLastFoot();
1688 return row_info[row].endlastfoot;
1692 void Tabular::setLTNewPage(row_type row, bool what)
1694 row_info[row].newpage = what;
1698 bool Tabular::getLTNewPage(row_type row) const
1700 return row_info[row].newpage;
1704 bool Tabular::haveLTHead() const
1706 for (row_type i = 0; i < row_info.size(); ++i)
1707 if (row_info[i].endhead)
1713 bool Tabular::haveLTFirstHead() const
1715 if (endfirsthead.empty)
1717 for (row_type i = 0; i < row_info.size(); ++i)
1718 if (row_info[i].endfirsthead)
1724 bool Tabular::haveLTFoot() const
1726 for (row_type i = 0; i < row_info.size(); ++i)
1727 if (row_info[i].endfoot)
1733 bool Tabular::haveLTLastFoot() const
1735 if (endlastfoot.empty)
1737 for (row_type i = 0; i < row_info.size(); ++i)
1738 if (row_info[i].endlastfoot)
1744 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1746 idx_type i = getFirstCellInRow(row);
1748 setMultiColumn(i, numberOfCellsInRow(i));
1749 setTopLine(i, false);
1750 setBottomLine(i, false);
1751 setLeftLine(i, false);
1752 setRightLine(i, false);
1753 // When a row is set as caption, then also insert a caption. Otherwise
1754 // the LaTeX output is broken, when the user don't add a caption.
1755 dispatch(FuncRequest(LFUN_CAPTION_INSERT));
1757 unsetMultiColumn(i);
1758 // FIXME: when unsetting a caption row, also all existing captions
1759 // in this row must be dissolved, see (bug 5754)
1760 // dispatch(FuncRequest(LFUN_INSET_DISSOLVE, "caption-insert"));
1762 row_info[row].caption = what;
1767 bool Tabular::ltCaption(row_type row) const
1769 return row_info[row].caption;
1773 bool Tabular::haveLTCaption() const
1775 for (row_type i = 0; i < row_info.size(); ++i)
1776 if (row_info[i].caption)
1782 // end longtable support functions
1784 void Tabular::setRowAscent(row_type row, int height)
1786 if (row >= row_info.size() || row_info[row].ascent == height)
1788 row_info[row].ascent = height;
1792 void Tabular::setRowDescent(row_type row, int height)
1794 if (row >= row_info.size() || row_info[row].descent == height)
1796 row_info[row].descent = height;
1800 int Tabular::rowAscent(row_type row) const
1802 if (row >= row_info.size())
1804 return row_info[row].ascent;
1808 int Tabular::rowDescent(row_type row) const
1810 LASSERT(row < row_info.size(), /**/);
1811 return row_info[row].descent;
1815 int Tabular::height() const
1818 for (row_type row = 0; row < row_info.size(); ++row)
1819 height += rowAscent(row) + rowDescent(row) +
1820 getAdditionalHeight(row);
1825 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1827 LASSERT(row < row_info.size(), /**/);
1828 LASSERT(column < column_info.size(), /**/);
1829 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1833 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1835 // we only output complete row lines and the 1st row here, the rest
1836 // is done in Tabular::TeXBottomHLine(...)
1838 // get for each column the topline (if any)
1839 col_type const ncols = column_info.size();
1840 vector<bool> topline;
1842 for (col_type c = 0; c < ncols; ++c) {
1843 topline.push_back(topLine(cellIndex(row, c)));
1848 // do nothing if empty first row, or incomplete row line after
1849 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1852 // only output complete row lines and the 1st row's clines
1853 if (nset == ncols) {
1855 os << (row == 0 ? "\\toprule " : "\\midrule ");
1859 } else if (row == 0) {
1860 for (col_type c = 0; c < ncols; ++c) {
1862 //babel makes the "-" character an active one, so we have to suppress this here
1863 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1864 if (lang == "slovak" || lang == "czech")
1865 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1866 "\\expandafter\\cline\\expandafter{\\expandafter")
1867 << c + 1 << "\\string-";
1869 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1870 // get to last column of line span
1871 while (c < ncols && topline[c])
1882 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1884 // we output bottomlines of row r and the toplines of row r+1
1885 // if the latter do not span the whole tabular
1887 // get the bottomlines of row r, and toplines in next row
1888 bool lastrow = row == row_info.size() - 1;
1889 col_type const ncols = column_info.size();
1890 vector<bool> bottomline, topline;
1891 bool nextrowset = true;
1892 for (col_type c = 0; c < ncols; ++c) {
1893 bottomline.push_back(bottomLine(cellIndex(row, c)));
1894 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1895 nextrowset &= topline[c];
1898 // combine this row's bottom lines and next row's toplines if necessary
1900 for (col_type c = 0; c < ncols; ++c) {
1902 bottomline[c] = bottomline[c] || topline[c];
1907 // do nothing if empty, OR incomplete row line with a topline in next row
1908 if (nset == 0 || (nextrowset && nset != ncols))
1911 if (nset == ncols) {
1913 os << (lastrow ? "\\bottomrule" : "\\midrule");
1917 for (col_type c = 0; c < ncols; ++c) {
1918 if (bottomline[c]) {
1919 //babel makes the "-" character an active one, so we have to suppress this here
1920 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1921 if (lang == "slovak" || lang == "czech")
1922 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1923 "\\expandafter\\cline\\expandafter{\\expandafter")
1924 << c + 1 << "\\string-";
1926 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1927 // get to last column of line span
1928 while (c < ncols && bottomline[c])
1939 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1942 row_type const r = cellRow(cell);
1943 if (is_long_tabular && row_info[r].caption)
1946 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1947 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1948 // figure out how to set the lines
1949 // we always set double lines to the right of the cell
1950 col_type const c = cellColumn(cell);
1951 col_type const nextcol = c + columnSpan(cell);
1952 bool colright = columnRightLine(c);
1953 bool colleft = columnLeftLine(c);
1954 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1955 bool nextcellleft = nextcol < column_info.size()
1956 && leftLine(cellIndex(r, nextcol));
1957 bool coldouble = colright && nextcolleft;
1958 bool celldouble = rightLine(cell) && nextcellleft;
1959 ismulticol = isMultiColumn(cell)
1960 || (c == 0 && colleft != leftLine(cell))
1961 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1962 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1963 || (coldouble != celldouble);
1965 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1966 if (c ==0 && leftLine(cell))
1968 if (!cellInfo(cell).align_special.empty()) {
1969 os << cellInfo(cell).align_special;
1971 if (!getPWidth(cell).zero()) {
1973 case LYX_ALIGN_LEFT:
1974 os << ">{\\raggedright}";
1976 case LYX_ALIGN_RIGHT:
1977 os << ">{\\raggedleft}";
1979 case LYX_ALIGN_CENTER:
1980 os << ">{\\centering}";
1986 case LYX_VALIGN_TOP:
1989 case LYX_VALIGN_MIDDLE:
1992 case LYX_VALIGN_BOTTOM:
1997 << from_ascii(getPWidth(cell).asLatexString())
2001 case LYX_ALIGN_LEFT:
2004 case LYX_ALIGN_RIGHT:
2011 } // end if else !getPWidth
2012 } // end if else !cellinfo_of_cell
2013 if (rightLine(cell) || nextcellleft)
2016 // add extra vertical line if we want a double one
2020 if (getRotateCell(cell)) {
2021 os << "\\begin{sideways}\n";
2024 if (getUsebox(cell) == BOX_PARBOX) {
2027 case LYX_VALIGN_TOP:
2030 case LYX_VALIGN_MIDDLE:
2033 case LYX_VALIGN_BOTTOM:
2037 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2039 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2040 os << "\\begin{minipage}[";
2042 case LYX_VALIGN_TOP:
2045 case LYX_VALIGN_MIDDLE:
2048 case LYX_VALIGN_BOTTOM:
2052 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2060 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2063 row_type const r = cellRow(cell);
2064 if (is_long_tabular && row_info[r].caption)
2068 if (getUsebox(cell) == BOX_PARBOX)
2070 else if (getUsebox(cell) == BOX_MINIPAGE) {
2071 os << "%\n\\end{minipage}";
2074 if (getRotateCell(cell)) {
2075 os << "%\n\\end{sideways}";
2085 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2086 OutputParams const & runparams) const
2088 if (!is_long_tabular)
2093 // the caption must be output befrore the headers
2094 if (haveLTCaption()) {
2095 for (row_type i = 0; i < row_info.size(); ++i) {
2096 if (row_info[i].caption) {
2097 ret += TeXRow(os, i, runparams);
2101 // output first header info
2102 // first header must be output before the header, otherwise the
2103 // correct caption placement becomes really wierd
2104 if (haveLTFirstHead()) {
2105 if (endfirsthead.topDL) {
2109 for (row_type i = 0; i < row_info.size(); ++i) {
2110 if (row_info[i].endfirsthead) {
2111 ret += TeXRow(os, i, runparams);
2114 if (endfirsthead.bottomDL) {
2118 os << "\\endfirsthead\n";
2121 // output header info
2123 if (endfirsthead.empty && !haveLTFirstHead()) {
2124 os << "\\endfirsthead\n";
2127 if (endhead.topDL) {
2131 for (row_type i = 0; i < row_info.size(); ++i) {
2132 if (row_info[i].endhead) {
2133 ret += TeXRow(os, i, runparams);
2136 if (endhead.bottomDL) {
2140 os << "\\endhead\n";
2143 // output footer info
2145 if (endfoot.topDL) {
2149 for (row_type i = 0; i < row_info.size(); ++i) {
2150 if (row_info[i].endfoot) {
2151 ret += TeXRow(os, i, runparams);
2154 if (endfoot.bottomDL) {
2158 os << "\\endfoot\n";
2160 if (endlastfoot.empty && !haveLTLastFoot()) {
2161 os << "\\endlastfoot\n";
2165 // output lastfooter info
2166 if (haveLTLastFoot()) {
2167 if (endlastfoot.topDL) {
2171 for (row_type i = 0; i < row_info.size(); ++i) {
2172 if (row_info[i].endlastfoot) {
2173 ret += TeXRow(os, i, runparams);
2176 if (endlastfoot.bottomDL) {
2180 os << "\\endlastfoot\n";
2187 bool Tabular::isValidRow(row_type row) const
2189 if (!is_long_tabular)
2191 return !row_info[row].endhead && !row_info[row].endfirsthead
2192 && !row_info[row].endfoot && !row_info[row].endlastfoot
2193 && !row_info[row].caption;
2197 int Tabular::TeXRow(odocstream & os, row_type i,
2198 OutputParams const & runparams) const
2200 idx_type cell = cellIndex(i, 0);
2201 shared_ptr<InsetTableCell> inset = cellInset(cell);
2202 Paragraph const & par = inset->paragraphs().front();
2203 string const lang = par.getParLanguage(buffer().params())->lang();
2205 //output the top line
2206 int ret = TeXTopHLine(os, i, lang);
2208 if (row_info[i].top_space_default) {
2210 os << "\\addlinespace\n";
2212 os << "\\noalign{\\vskip\\doublerulesep}\n";
2214 } else if(!row_info[i].top_space.zero()) {
2216 os << "\\addlinespace["
2217 << from_ascii(row_info[i].top_space.asLatexString())
2220 os << "\\noalign{\\vskip"
2221 << from_ascii(row_info[i].top_space.asLatexString())
2226 bool ismulticol = false;
2227 for (col_type j = 0; j < column_info.size(); ++j) {
2228 if (isPartOfMultiColumn(i, j))
2230 ret += TeXCellPreamble(os, cell, ismulticol);
2231 shared_ptr<InsetTableCell> inset = cellInset(cell);
2233 Paragraph const & par = inset->paragraphs().front();
2234 bool rtl = par.isRTL(buffer().params())
2236 && getPWidth(cell).zero();
2240 par.getParLanguage(buffer().params())->lang();
2241 if (lang == "farsi")
2243 else if (lang == "arabic_arabi")
2245 // currently, remaning RTL languages are
2246 // arabic_arabtex and hebrew
2250 // pass to the OutputParams that we are in a cell and
2251 // which alignment we have set.
2252 // InsetNewline needs this context information.
2253 OutputParams newrp(runparams);
2254 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2255 ? OutputParams::PLAIN
2256 : OutputParams::ALIGNED;
2257 ret += inset->latex(os, newrp);
2258 runparams.encoding = newrp.encoding;
2262 ret += TeXCellPostamble(os, cell, ismulticol);
2263 if (!isLastCellInRow(cell)) { // not last cell in row
2268 os << "\\tabularnewline";
2269 if (row_info[i].bottom_space_default) {
2271 os << "\\addlinespace";
2273 os << "[\\doublerulesep]";
2274 } else if (!row_info[i].bottom_space.zero()) {
2276 os << "\\addlinespace";
2278 << from_ascii(row_info[i].bottom_space.asLatexString())
2284 //output the bottom line
2285 ret += TeXBottomHLine(os, i, lang);
2287 if (row_info[i].interline_space_default) {
2289 os << "\\addlinespace\n";
2291 os << "\\noalign{\\vskip\\doublerulesep}\n";
2293 } else if (!row_info[i].interline_space.zero()) {
2295 os << "\\addlinespace["
2296 << from_ascii(row_info[i].interline_space.asLatexString())
2299 os << "\\noalign{\\vskip"
2300 << from_ascii(row_info[i].interline_space.asLatexString())
2308 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2312 //+---------------------------------------------------------------------
2313 //+ first the opening preamble +
2314 //+---------------------------------------------------------------------
2317 os << "\\begin{sideways}\n";
2320 if (is_long_tabular)
2321 os << "\\begin{longtable}";
2323 os << "\\begin{tabular}";
2325 switch (tabular_valignment) {
2326 case LYX_VALIGN_TOP:
2329 case LYX_VALIGN_BOTTOM:
2332 case LYX_VALIGN_MIDDLE:
2338 for (col_type i = 0; i < column_info.size(); ++i) {
2339 if (columnLeftLine(i))
2341 if (!column_info[i].align_special.empty()) {
2342 os << column_info[i].align_special;
2344 if (!column_info[i].p_width.zero()) {
2345 switch (column_info[i].alignment) {
2346 case LYX_ALIGN_LEFT:
2347 os << ">{\\raggedright}";
2349 case LYX_ALIGN_RIGHT:
2350 os << ">{\\raggedleft}";
2352 case LYX_ALIGN_CENTER:
2353 os << ">{\\centering}";
2355 case LYX_ALIGN_NONE:
2356 case LYX_ALIGN_BLOCK:
2357 case LYX_ALIGN_LAYOUT:
2358 case LYX_ALIGN_SPECIAL:
2362 switch (column_info[i].valignment) {
2363 case LYX_VALIGN_TOP:
2366 case LYX_VALIGN_MIDDLE:
2369 case LYX_VALIGN_BOTTOM:
2374 << from_ascii(column_info[i].p_width.asLatexString())
2377 switch (column_info[i].alignment) {
2378 case LYX_ALIGN_LEFT:
2381 case LYX_ALIGN_RIGHT:
2388 } // end if else !column_info[i].p_width
2389 } // end if else !column_info[i].align_special
2390 if (columnRightLine(i))
2396 ret += TeXLongtableHeaderFooter(os, runparams);
2398 //+---------------------------------------------------------------------
2399 //+ the single row and columns (cells) +
2400 //+---------------------------------------------------------------------
2402 for (row_type i = 0; i < row_info.size(); ++i) {
2403 if (isValidRow(i)) {
2404 ret += TeXRow(os, i, runparams);
2405 if (is_long_tabular && row_info[i].newpage) {
2406 os << "\\newpage\n";
2412 //+---------------------------------------------------------------------
2413 //+ the closing of the tabular +
2414 //+---------------------------------------------------------------------
2416 if (is_long_tabular)
2417 os << "\\end{longtable}";
2419 os << "\\end{tabular}";
2421 os << "\n\\end{sideways}";
2429 int Tabular::docbookRow(odocstream & os, row_type row,
2430 OutputParams const & runparams) const
2433 idx_type cell = getFirstCellInRow(row);
2436 for (col_type j = 0; j < column_info.size(); ++j) {
2437 if (isPartOfMultiColumn(row, j))
2440 os << "<entry align=\"";
2441 switch (getAlignment(cell)) {
2442 case LYX_ALIGN_LEFT:
2445 case LYX_ALIGN_RIGHT:
2453 os << "\" valign=\"";
2454 switch (getVAlignment(cell)) {
2455 case LYX_VALIGN_TOP:
2458 case LYX_VALIGN_BOTTOM:
2461 case LYX_VALIGN_MIDDLE:
2466 if (isMultiColumn(cell)) {
2467 os << " namest=\"col" << j << "\" ";
2468 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2472 ret += cellInset(cell)->docbook(os, runparams);
2481 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2485 //+---------------------------------------------------------------------
2486 //+ first the opening preamble +
2487 //+---------------------------------------------------------------------
2489 os << "<tgroup cols=\"" << column_info.size()
2490 << "\" colsep=\"1\" rowsep=\"1\">\n";
2492 for (col_type i = 0; i < column_info.size(); ++i) {
2493 os << "<colspec colname=\"col" << i << "\" align=\"";
2494 switch (column_info[i].alignment) {
2495 case LYX_ALIGN_LEFT:
2498 case LYX_ALIGN_RIGHT:
2506 if (runparams.flavor == OutputParams::XML)
2512 //+---------------------------------------------------------------------
2513 //+ Long Tabular case +
2514 //+---------------------------------------------------------------------
2516 // output caption info
2517 if (haveLTCaption()) {
2518 os << "<caption>\n";
2520 for (row_type i = 0; i < row_info.size(); ++i) {
2521 if (row_info[i].caption) {
2522 ret += docbookRow(os, i, runparams);
2525 os << "</caption>\n";
2528 // output header info
2529 if (haveLTHead() || haveLTFirstHead()) {
2532 for (row_type i = 0; i < row_info.size(); ++i) {
2533 if (row_info[i].endhead || row_info[i].endfirsthead) {
2534 ret += docbookRow(os, i, runparams);
2540 // output footer info
2541 if (haveLTFoot() || haveLTLastFoot()) {
2544 for (row_type i = 0; i < row_info.size(); ++i) {
2545 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2546 ret += docbookRow(os, i, runparams);
2553 //+---------------------------------------------------------------------
2554 //+ the single row and columns (cells) +
2555 //+---------------------------------------------------------------------
2559 for (row_type i = 0; i < row_info.size(); ++i) {
2560 if (isValidRow(i)) {
2561 ret += docbookRow(os, i, runparams);
2566 //+---------------------------------------------------------------------
2567 //+ the closing of the tabular +
2568 //+---------------------------------------------------------------------
2577 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2578 vector<unsigned int> const & clen) const
2580 idx_type const fcell = getFirstCellInRow(row);
2581 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2584 for (idx_type i = fcell; i < n; ++i) {
2594 for (idx_type i = fcell; i < n; ++i) {
2605 col_type column = cellColumn(i);
2606 int len = clen[column];
2607 while (column < column_info.size() - 1
2608 && isPartOfMultiColumn(row, ++column))
2609 len += clen[column] + 4;
2610 os << docstring(len, ch);
2625 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2626 vector<unsigned int> const & clen) const
2628 idx_type const fcell = getFirstCellInRow(row);
2629 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2632 for (idx_type i = fcell; i < n; ++i) {
2633 if (bottomLine(i)) {
2642 for (idx_type i = fcell; i < n; ++i) {
2643 if (bottomLine(i)) {
2653 col_type column = cellColumn(i);
2654 int len = clen[column];
2655 while (column < column_info.size() -1
2656 && isPartOfMultiColumn(row, ++column))
2657 len += clen[column] + 4;
2658 os << docstring(len, ch);
2659 if (bottomLine(i)) {
2673 void Tabular::plaintextPrintCell(odocstream & os,
2674 OutputParams const & runparams,
2675 idx_type cell, row_type row, col_type column,
2676 vector<unsigned int> const & clen,
2677 bool onlydata) const
2679 odocstringstream sstr;
2680 cellInset(cell)->plaintext(sstr, runparams);
2692 unsigned int len1 = sstr.str().length();
2693 unsigned int len2 = clen[column];
2694 while (column < column_info.size() -1
2695 && isPartOfMultiColumn(row, ++column))
2696 len2 += clen[column] + 4;
2699 switch (getAlignment(cell)) {
2701 case LYX_ALIGN_LEFT:
2704 case LYX_ALIGN_RIGHT:
2708 case LYX_ALIGN_CENTER:
2714 os << docstring(len1, ' ') << sstr.str()
2715 << docstring(len2, ' ');
2717 if (rightLine(cell))
2724 void Tabular::plaintext(odocstream & os,
2725 OutputParams const & runparams, int const depth,
2726 bool onlydata, char_type delim) const
2728 // first calculate the width of the single columns
2729 vector<unsigned int> clen(column_info.size());
2732 // first all non (real) multicolumn cells!
2733 for (col_type j = 0; j < column_info.size(); ++j) {
2735 for (row_type i = 0; i < row_info.size(); ++i) {
2736 idx_type cell = cellIndex(i, j);
2737 if (isMultiColumnReal(cell))
2739 odocstringstream sstr;
2740 cellInset(cell)->plaintext(sstr, runparams);
2741 if (clen[j] < sstr.str().length())
2742 clen[j] = sstr.str().length();
2745 // then all (real) multicolumn cells!
2746 for (col_type j = 0; j < column_info.size(); ++j) {
2747 for (row_type i = 0; i < row_info.size(); ++i) {
2748 idx_type cell = cellIndex(i, j);
2749 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2751 odocstringstream sstr;
2752 cellInset(cell)->plaintext(sstr, runparams);
2753 int len = int(sstr.str().length());
2754 idx_type const n = columnSpan(cell);
2755 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2757 if (len > int(clen[j + n - 1]))
2758 clen[j + n - 1] = len;
2763 for (row_type i = 0; i < row_info.size(); ++i) {
2764 if (!onlydata && plaintextTopHLine(os, i, clen))
2765 os << docstring(depth * 2, ' ');
2766 for (col_type j = 0; j < column_info.size(); ++j) {
2767 if (isPartOfMultiColumn(i, j))
2769 if (onlydata && j > 0)
2770 // we don't use operator<< for single UCS4 character.
2771 // see explanation in docstream.h
2773 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2778 os << docstring(depth * 2, ' ');
2779 if (plaintextBottomHLine(os, i, clen))
2780 os << docstring(depth * 2, ' ');
2786 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2788 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2792 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2793 col_type column) const
2795 return cell_info[row][column].inset;
2799 void Tabular::setCellInset(row_type row, col_type column,
2800 shared_ptr<InsetTableCell> ins) const
2802 CellData & cd = cell_info[row][column];
2807 void Tabular::validate(LaTeXFeatures & features) const
2809 features.require("NeedTabularnewline");
2811 features.require("booktabs");
2812 if (is_long_tabular)
2813 features.require("longtable");
2815 features.require("rotating");
2816 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2817 if (getVAlignment(cell) != LYX_VALIGN_TOP
2818 || !getPWidth(cell).zero())
2819 features.require("array");
2820 cellInset(cell)->validate(features);
2825 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2827 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2828 ParagraphList::const_iterator cit = parlist.begin();
2829 ParagraphList::const_iterator end = parlist.end();
2831 for (; cit != end; ++cit)
2832 for (int i = 0; i < cit->size(); ++i)
2833 if (cit->isNewline(i))
2840 /////////////////////////////////////////////////////////////////////
2844 /////////////////////////////////////////////////////////////////////
2846 InsetTableCell::InsetTableCell(Buffer & buf)
2847 : InsetText(buf), isFixedWidth(false),
2848 contentAlign(LYX_ALIGN_CENTER)
2852 bool InsetTableCell::forcePlainLayout(idx_type) const
2854 return !isFixedWidth;
2858 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2860 return isFixedWidth;
2864 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2865 FuncStatus & status) const
2868 switch (cmd.action) {
2870 enabled = !forcePlainLayout();
2872 case LFUN_LAYOUT_PARAGRAPH:
2873 enabled = allowParagraphCustomization();
2876 return InsetText::getStatus(cur, cmd, status);
2878 status.setEnabled(enabled);
2882 docstring InsetTableCell::asString(bool intoInsets)
2885 if (paragraphs().empty())
2887 ParagraphList::const_iterator it = paragraphs().begin();
2888 ParagraphList::const_iterator en = paragraphs().end();
2890 for (; it != en; ++it) {
2895 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2902 /////////////////////////////////////////////////////////////////////
2906 /////////////////////////////////////////////////////////////////////
2908 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2910 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2911 rowselect_(false), colselect_(false)
2913 setBuffer(buf); // FIXME: remove later
2917 InsetTabular::InsetTabular(InsetTabular const & tab)
2918 : Inset(tab), tabular(tab.tabular), scx_(0)
2923 InsetTabular::~InsetTabular()
2925 hideDialogs("tabular", this);
2929 void InsetTabular::setBuffer(Buffer & buf)
2931 tabular.setBuffer(buf);
2932 Inset::setBuffer(buf);
2936 bool InsetTabular::insetAllowed(InsetCode code) const
2938 if (code == MATHMACRO_CODE
2939 || (code == CAPTION_CODE && !tabular.is_long_tabular))
2946 void InsetTabular::write(ostream & os) const
2948 os << "Tabular" << endl;
2953 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2955 // FIXME: depending on the selection state, we could offer a different menu.
2956 return from_ascii("context-tabular");
2960 void InsetTabular::read(Lexer & lex)
2962 //bool const old_format = (lex.getString() == "\\LyXTable");
2970 string token = lex.getString();
2971 while (lex && token != "\\end_inset") {
2973 token = lex.getString();
2976 lex.printError("Missing \\end_inset at this point. ");
2980 int InsetTabular::rowFromY(Cursor & cur, int y) const
2982 // top y coordinate of tabular
2983 int h = yo(cur.bv()) - tabular.rowAscent(0);
2984 size_t nrows = tabular.row_info.size();
2986 for (; r < nrows && y > h; ++r) {
2987 h += tabular.rowAscent(r);
2988 h += tabular.rowDescent(r);
2989 h += tabular.getAdditionalHeight(r);
2995 int InsetTabular::columnFromX(Cursor & cur, int x) const
2997 // left x coordinate of tabular
2998 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2999 size_t ncols = tabular.column_info.size();
3001 for (; c < ncols && x > w; ++c)
3002 w += tabular.columnWidth(c);
3007 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3009 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3010 // mi.base.textwidth << "\n";
3013 LASSERT(false, /**/);
3017 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
3020 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3021 if (tabular.isPartOfMultiColumn(i, j))
3022 // Multicolumn cell, but not first one
3027 if (tabular.cell_info[i][j].multicolumn ==
3028 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3029 p_width = tabular.cellInfo(cell).p_width;
3031 p_width = tabular.column_info[j].p_width;
3032 if (!p_width.zero())
3033 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3034 tabular.cellInset(cell)->metrics(m, dim);
3035 if (!p_width.zero())
3036 dim.wid = m.base.textwidth;
3037 tabular.setCellWidth(cell, dim.wid);
3038 maxAsc = max(maxAsc, dim.asc);
3039 maxDesc = max(maxDesc, dim.des);
3042 int const top_space = tabular.row_info[i].top_space_default ?
3043 default_line_space :
3044 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3045 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3046 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3047 default_line_space :
3048 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3049 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3051 tabular.updateColumnWidths();
3052 dim.asc = tabular.rowAscent(0);
3053 dim.des = tabular.height() - dim.asc;
3054 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3057 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3060 if (&cur.inset() == this && cur.selection()) {
3061 if (cur.selIsMultiCell()) {
3064 getSelection(cur, rs, re, cs, ce);
3066 if (col >= cs && col <= ce && row >= rs && row <= re)
3069 if (col == tabular.cellColumn(cur.idx())
3070 && row == tabular.cellRow(cur.idx())) {
3071 CursorSlice const & beg = cur.selBegin();
3072 CursorSlice const & end = cur.selEnd();
3074 if ((end.lastpos() > 0 || end.lastpit() > 0)
3075 && end.pos() == end.lastpos() && beg.pos() == 0
3076 && end.pit() == end.lastpit() && beg.pit() == 0)
3084 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3086 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3087 BufferView * bv = pi.base.bv;
3088 Cursor & cur = pi.base.bv->cursor();
3090 // FIXME: As the full backrgound is painted in drawSelection(),
3091 // we have no choice but to do a full repaint for the Text cells.
3092 pi.full_repaint = true;
3094 resetPos(bv->cursor());
3097 x += ADD_TO_TABULAR_WIDTH;
3099 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3100 bool const original_selection_state = pi.selected;
3103 first_visible_cell = Tabular::npos;
3104 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3106 int const a = tabular.rowAscent(i);
3107 int const d = tabular.rowDescent(i);
3108 idx = tabular.cellIndex(i, 0);
3109 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3110 if (tabular.isPartOfMultiColumn(i, j))
3112 if (first_visible_cell == Tabular::npos)
3113 first_visible_cell = idx;
3115 pi.selected |= isCellSelected(cur, i, j);
3116 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3117 // Cache the Inset position.
3118 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3119 if (nx + tabular.columnWidth(idx) < 0
3120 || nx > bv->workWidth()
3122 || y - a > bv->workHeight()) {
3123 pi.pain.setDrawingEnabled(false);
3124 cell(idx)->draw(pi, cx, y);
3125 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3126 pi.pain.setDrawingEnabled(original_drawing_state);
3128 cell(idx)->draw(pi, cx, y);
3129 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3131 nx += tabular.columnWidth(idx);
3133 pi.selected = original_selection_state;
3136 if (i + 1 < tabular.row_info.size())
3137 y += d + tabular.rowAscent(i + 1) +
3138 tabular.getAdditionalHeight(i + 1);
3143 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3145 Cursor & cur = pi.base.bv->cursor();
3148 x += scx_ + ADD_TO_TABULAR_WIDTH;
3150 // FIXME: it is wrong to completely paint the background
3151 // if we want to do single row painting.
3153 // Paint background of current tabular
3154 int const w = tabular.width();
3155 int const h = tabular.height();
3156 int yy = y - tabular.rowAscent(0);
3157 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3159 if (!cur.selection())
3161 if (&cur.inset() != this)
3166 bool const full_cell_selected = isCellSelected(cur,
3167 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3169 if (cur.selIsMultiCell() || full_cell_selected) {
3170 y -= tabular.rowAscent(0);
3171 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3172 int const a = tabular.rowAscent(j);
3173 int const h = a + tabular.rowDescent(j);
3175 y += tabular.getAdditionalHeight(j);
3176 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3177 if (tabular.isPartOfMultiColumn(j, i))
3179 idx_type const cell =
3180 tabular.cellIndex(j, i);
3181 int const w = tabular.columnWidth(cell);
3182 if (isCellSelected(cur, j, i))
3183 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3190 x += cellXPos(cur.idx());
3191 x += tabular.getBeginningOfTextInCell(cur.idx());
3192 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3197 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3198 row_type row, idx_type cell, Change const & change) const
3200 int x2 = x + tabular.columnWidth(cell);
3201 bool on_off = false;
3202 Color col = Color_tabularline;
3203 Color onoffcol = Color_tabularonoffline;
3205 if (change.changed()) {
3206 col = change.color();
3207 onoffcol = change.color();
3210 if (!tabular.topAlreadyDrawn(cell)) {
3211 on_off = !tabular.topLine(cell);
3212 pain.line(x, y - tabular.rowAscent(row),
3213 x2, y - tabular.rowAscent(row),
3214 on_off ? onoffcol : col,
3215 on_off ? Painter::line_onoffdash : Painter::line_solid);
3217 on_off = !tabular.bottomLine(cell);
3218 pain.line(x, y + tabular.rowDescent(row),
3219 x2, y + tabular.rowDescent(row),
3220 on_off ? onoffcol : col,
3221 on_off ? Painter::line_onoffdash : Painter::line_solid);
3222 if (!tabular.leftAlreadyDrawn(cell)) {
3223 on_off = !tabular.leftLine(cell);
3224 pain.line(x, y - tabular.rowAscent(row),
3225 x, y + tabular.rowDescent(row),
3226 on_off ? onoffcol : col,
3227 on_off ? Painter::line_onoffdash : Painter::line_solid);
3229 on_off = !tabular.rightLine(cell);
3230 pain.line(x2 - tabular.getAdditionalWidth(cell),
3231 y - tabular.rowAscent(row),
3232 x2 - tabular.getAdditionalWidth(cell),
3233 y + tabular.rowDescent(row),
3234 on_off ? onoffcol : col,
3235 on_off ? Painter::line_onoffdash : Painter::line_solid);
3239 docstring InsetTabular::editMessage() const
3241 return _("Opened table");
3245 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3247 //lyxerr << "InsetTabular::edit: " << this << endl;
3249 cur.setSelection(false);
3252 if (isRightToLeft(cur))
3253 cur.idx() = tabular.getLastCellInRow(0);
3259 if (isRightToLeft(cur))
3260 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3262 cur.idx() = tabular.numberofcells - 1;
3264 cur.pos() = cur.lastpos(); // FIXME crude guess
3266 // FIXME: this accesses the position cache before it is initialized
3268 //cur.bv().fitCursor();
3272 void InsetTabular::updateLabels(ParIterator const & it)
3274 // In a longtable, tell captions what the current float is
3275 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3276 string const saveflt = cnts.current_float();
3277 if (tabular.is_long_tabular)
3278 cnts.current_float("table");
3280 ParIterator it2 = it;
3282 size_t const end = it2.nargs();
3283 for ( ; it2.idx() < end; it2.top().forwardIdx())
3284 buffer().updateLabels(it2);
3287 if (tabular.is_long_tabular)
3288 cnts.current_float(saveflt);
3292 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3294 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3295 << "\n cur:" << cur);
3296 CursorSlice sl = cur.top();
3297 Cursor & bvcur = cur.bv().cursor();
3299 switch (cmd.action) {
3301 case LFUN_MOUSE_PRESS: {
3302 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3304 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3305 || cmd.x > xo(cur.bv()) + tabular.width()) {
3306 row_type r = rowFromY(cur, cmd.y);
3307 cur.idx() = tabular.getFirstCellInRow(r);
3310 cur.idx() = tabular.getLastCellInRow(r);
3311 cur.pos() = cur.lastpos();
3312 cur.setSelection(true);
3318 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3319 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3320 || cmd.y > y0 + tabular.height()) {
3321 col_type c = columnFromX(cur, cmd.x);
3322 cur.idx() = tabular.cellIndex(0, c);
3325 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3326 cur.pos() = cur.lastpos();
3327 cur.setSelection(true);
3332 // do not reset cursor/selection if we have selected
3333 // some cells (bug 2715).
3334 if (cmd.button() == mouse_button::button3
3335 && &bvcur.selBegin().inset() == this
3336 && bvcur.selIsMultiCell())
3339 // Let InsetTableCell do it
3340 cell(cur.idx())->dispatch(cur, cmd);
3343 case LFUN_MOUSE_MOTION:
3344 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3345 if (cmd.button() == mouse_button::button1) {
3346 // only accept motions to places not deeper nested than the real anchor
3347 if (!bvcur.anchor_.hasPart(cur)) {
3351 // select (additional) row
3353 row_type r = rowFromY(cur, cmd.y);
3354 cur.idx() = tabular.getLastCellInRow(r);
3355 // we need to reset the cursor's pit and pos now, as the old ones
3356 // may no longer be valid.
3359 bvcur.setCursor(cur);
3360 bvcur.setSelection(true);
3363 // select (additional) column
3365 col_type c = columnFromX(cur, cmd.x);
3366 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3367 // we need to reset the cursor's pit and pos now, as the old ones
3368 // may no longer be valid.
3371 bvcur.setCursor(cur);
3372 bvcur.setSelection(true);
3375 // only update if selection changes
3376 if (bvcur.idx() == cur.idx() &&
3377 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3379 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3380 bvcur.setCursor(cur);
3381 bvcur.setSelection(true);
3382 // if this is a multicell selection, we just set the cursor to
3383 // the beginning of the cell's text.
3384 if (bvcur.selIsMultiCell()) {
3385 bvcur.pit() = bvcur.lastpit();
3386 bvcur.pos() = bvcur.lastpos();
3391 case LFUN_MOUSE_RELEASE:
3396 case LFUN_CELL_BACKWARD:
3398 cur.setSelection(false);
3401 case LFUN_CELL_FORWARD:
3403 cur.setSelection(false);
3406 case LFUN_CHAR_FORWARD_SELECT:
3407 case LFUN_CHAR_FORWARD:
3408 case LFUN_CHAR_BACKWARD_SELECT:
3409 case LFUN_CHAR_BACKWARD:
3410 case LFUN_CHAR_RIGHT_SELECT:
3411 case LFUN_CHAR_RIGHT:
3412 case LFUN_CHAR_LEFT_SELECT:
3413 case LFUN_CHAR_LEFT: {
3414 // determine whether we move to next or previous cell, where to enter
3415 // the new cell from, and which command to "finish" (i.e., exit the
3418 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3419 FuncCode finish_lfun;
3421 if (cmd.action == LFUN_CHAR_FORWARD
3422 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3424 finish_lfun = LFUN_FINISHED_FORWARD;
3426 else if (cmd.action == LFUN_CHAR_BACKWARD
3427 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3429 finish_lfun = LFUN_FINISHED_BACKWARD;
3431 // LEFT or RIGHT commands --- the interpretation will depend on the
3432 // table's direction.
3434 bool const right = cmd.action == LFUN_CHAR_RIGHT
3435 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3436 next_cell = isRightToLeft(cur) != right;
3438 if (lyxrc.visual_cursor)
3439 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3441 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3444 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3445 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3446 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3447 cmd.action == LFUN_CHAR_LEFT_SELECT;
3449 // If we have a multicell selection or we're
3450 // not doing some LFUN_*_SELECT thing anyway...
3451 if (!cur.selIsMultiCell() || !select) {
3452 col_type const c = tabular.cellColumn(cur.idx());
3453 row_type const r = tabular.cellRow(cur.idx());
3454 // Are we trying to select the whole cell and is the whole cell
3455 // not yet selected?
3456 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3457 ((next_cell && cur.pit() == cur.lastpit()
3458 && cur.pos() == cur.lastpos())
3459 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3461 // ...try to dispatch to the cell's inset.
3462 cell(cur.idx())->dispatch(cur, cmd);
3464 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3466 // When we already have a selection we want to select the whole cell
3467 // before going to the next cell.
3468 if (select_whole && !empty_cell){
3469 getText(cur.idx())->selectAll(cur);
3474 // FIXME: When we support the selection of an empty cell, remove
3475 // the !empty_cell from this condition. For now we jump to the next
3476 // cell if the current cell is empty.
3477 if (cur.result().dispatched() && !empty_cell)
3481 // move to next/prev cell, as appropriate
3482 // note that we will always do this if we're selecting and we have
3483 // a multicell selection
3484 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3485 << " cell from: " << int(entry_from));
3487 moveNextCell(cur, entry_from);
3489 movePrevCell(cur, entry_from);
3490 // if we're exiting the table, call the appropriate FINISHED lfun
3491 if (sl == cur.top())
3492 cmd = FuncRequest(finish_lfun);
3499 case LFUN_DOWN_SELECT:
3501 if (!(cur.selection() && cur.selIsMultiCell()))
3502 cell(cur.idx())->dispatch(cur, cmd);
3504 cur.dispatched(); // override the cell's decision
3505 if (sl == cur.top()) {
3506 // if our Text didn't do anything to the cursor
3507 // then we try to put the cursor into the cell below
3508 // setting also the right targetX.
3509 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3510 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3511 cur.idx() = tabular.cellBelow(cur.idx());
3513 TextMetrics const & tm =
3514 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3515 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3518 if (sl == cur.top()) {
3519 // we trick it to go to forward after leaving the
3521 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3524 if (cur.selIsMultiCell()) {
3525 cur.pit() = cur.lastpit();
3526 cur.pos() = cur.lastpos();
3531 case LFUN_UP_SELECT:
3533 if (!(cur.selection() && cur.selIsMultiCell()))
3534 cell(cur.idx())->dispatch(cur, cmd);
3535 cur.dispatched(); // override the cell's decision
3536 if (sl == cur.top()) {
3537 // if our Text didn't do anything to the cursor
3538 // then we try to put the cursor into the cell above
3539 // setting also the right targetX.
3540 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3541 if (tabular.cellRow(cur.idx()) != 0) {
3542 cur.idx() = tabular.cellAbove(cur.idx());
3543 cur.pit() = cur.lastpit();
3544 Text const * text = cell(cur.idx())->getText(0);
3545 TextMetrics const & tm = cur.bv().textMetrics(text);
3546 ParagraphMetrics const & pm =
3547 tm.parMetrics(cur.lastpit());
3548 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3551 if (sl == cur.top()) {
3552 cmd = FuncRequest(LFUN_UP);
3555 if (cur.selIsMultiCell()) {
3557 cur.pos() = cur.lastpos();
3562 // case LFUN_SCREEN_DOWN: {
3563 // //if (hasSelection())
3564 // // cur.selection() = false;
3565 // col_type const col = tabular.cellColumn(cur.idx());
3566 // int const t = cur.bv().top_y() + cur.bv().height();
3567 // if (t < yo() + tabular.getHeightOfTabular()) {
3568 // cur.bv().scrollDocView(t);
3569 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3571 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3578 // case LFUN_SCREEN_UP: {
3579 // //if (hasSelection())
3580 // // cur.selection() = false;
3581 // col_type const col = tabular.cellColumn(cur.idx());
3582 // int const t = cur.bv().top_y() + cur.bv().height();
3584 // cur.bv().scrollDocView(t);
3588 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3592 // cur.par() = cur.lastpar();
3593 // cur.pos() = cur.lastpos();
3597 case LFUN_LAYOUT_TABULAR:
3598 cur.bv().showDialog("tabular", params2string(*this), this);
3601 case LFUN_INSET_DIALOG_UPDATE:
3602 cur.bv().updateDialog("tabular", params2string(*this));
3605 case LFUN_TABULAR_FEATURE:
3606 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3610 // insert file functions
3611 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3612 case LFUN_FILE_INSERT_PLAINTEXT: {
3614 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3615 FileName(to_utf8(cmd.argument())));
3618 cur.recordUndoInset(INSERT_UNDO);
3619 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3620 // content has been replaced,
3621 // so cursor might be invalid
3622 cur.pos() = cur.lastpos();
3623 cur.pit() = cur.lastpit();
3624 bvcur.setCursor(cur);
3631 if (cur.selIsMultiCell()) {
3632 if (copySelection(cur)) {
3633 cur.recordUndoInset(DELETE_UNDO);
3638 cell(cur.idx())->dispatch(cur, cmd);
3641 case LFUN_SELF_INSERT:
3642 if (cur.selIsMultiCell()) {
3643 cur.recordUndoInset(DELETE_UNDO);
3646 cell(cur.idx())->dispatch(cur, cmd);
3649 case LFUN_CHAR_DELETE_BACKWARD:
3650 case LFUN_CHAR_DELETE_FORWARD:
3651 if (cur.selIsMultiCell()) {
3652 cur.recordUndoInset(DELETE_UNDO);
3656 cell(cur.idx())->dispatch(cur, cmd);
3660 if (!cur.selection())
3662 if (cur.selIsMultiCell()) {
3666 cell(cur.idx())->dispatch(cur, cmd);
3669 case LFUN_CLIPBOARD_PASTE:
3670 case LFUN_PRIMARY_SELECTION_PASTE: {
3671 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3672 theClipboard().getAsText() :
3673 theSelection().get();
3676 // pass to InsertPlaintextString, but
3677 // only if we have multi-cell content
3678 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3679 cur.recordUndoInset(INSERT_UNDO);
3680 if (insertPlaintextString(cur.bv(), clip, false)) {
3681 // content has been replaced,
3682 // so cursor might be invalid
3683 cur.pos() = cur.lastpos();
3684 cur.pit() = cur.lastpit();
3685 bvcur.setCursor(cur);
3689 // Let the cell handle normal text
3690 cell(cur.idx())->dispatch(cur, cmd);
3695 if (!tabularStackDirty()) {
3696 cell(cur.idx())->dispatch(cur, cmd);
3699 if (theClipboard().isInternal() ||
3700 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3701 cur.recordUndoInset(INSERT_UNDO);
3702 pasteClipboard(cur);
3706 case LFUN_FONT_EMPH:
3707 case LFUN_FONT_BOLD:
3708 case LFUN_FONT_BOLDSYMBOL:
3709 case LFUN_FONT_ROMAN:
3710 case LFUN_FONT_NOUN:
3711 case LFUN_FONT_ITAL:
3712 case LFUN_FONT_FRAK:
3713 case LFUN_FONT_TYPEWRITER:
3714 case LFUN_FONT_SANS:
3715 case LFUN_TEXTSTYLE_APPLY:
3716 case LFUN_TEXTSTYLE_UPDATE:
3717 case LFUN_FONT_SIZE:
3718 case LFUN_FONT_UNDERLINE:
3720 case LFUN_WORD_CAPITALIZE:
3721 case LFUN_WORD_UPCASE:
3722 case LFUN_WORD_LOWCASE:
3723 case LFUN_CHARS_TRANSPOSE:
3724 if (cur.selIsMultiCell()) {
3727 getSelection(cur, rs, re, cs, ce);
3728 Cursor tmpcur = cur;
3729 for (row_type i = rs; i <= re; ++i) {
3730 for (col_type j = cs; j <= ce; ++j) {
3731 // cursor follows cell:
3732 tmpcur.idx() = tabular.cellIndex(i, j);
3733 // select this cell only:
3736 tmpcur.resetAnchor();
3737 tmpcur.pit() = tmpcur.lastpit();
3738 tmpcur.pos() = tmpcur.top().lastpos();
3739 tmpcur.setCursor(tmpcur);
3740 tmpcur.setSelection();
3741 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3746 cell(cur.idx())->dispatch(cur, cmd);
3750 // we try to handle this event in the insets dispatch function.
3751 cell(cur.idx())->dispatch(cur, cmd);
3757 // function sets an object as defined in func_status.h:
3758 // states OK, Unknown, Disabled, On, Off.
3759 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3760 FuncStatus & status) const
3762 switch (cmd.action) {
3763 case LFUN_TABULAR_FEATURE: {
3764 int action = Tabular::LAST_ACTION;
3766 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3767 string const tmp = tabularFeature[i].feature;
3768 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3769 action = tabularFeature[i].action;
3773 if (action == Tabular::LAST_ACTION) {
3775 status.unknown(true);
3779 string const argument
3780 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3782 row_type sel_row_start = 0;
3783 row_type sel_row_end = 0;
3784 col_type sel_col_start = 0;
3785 col_type sel_col_end = 0;
3786 Tabular::ltType dummyltt;
3789 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3792 case Tabular::SET_PWIDTH:
3793 case Tabular::SET_MPWIDTH:
3794 case Tabular::SET_SPECIAL_COLUMN:
3795 case Tabular::SET_SPECIAL_MULTI:
3796 case Tabular::APPEND_ROW:
3797 case Tabular::APPEND_COLUMN:
3798 case Tabular::DELETE_ROW:
3799 case Tabular::DELETE_COLUMN:
3800 case Tabular::COPY_ROW:
3801 case Tabular::COPY_COLUMN:
3802 case Tabular::SET_TOP_SPACE:
3803 case Tabular::SET_BOTTOM_SPACE:
3804 case Tabular::SET_INTERLINE_SPACE:
3808 case Tabular::MULTICOLUMN:
3809 // When a row is set as longtable caption, it must not be allowed
3810 // to unset that this row is a multicolumn.
3811 status.setEnabled(sel_row_start == sel_row_end
3812 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
3813 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3816 case Tabular::SET_ALL_LINES:
3817 case Tabular::UNSET_ALL_LINES:
3818 case Tabular::SET_BORDER_LINES:
3819 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3822 case Tabular::TOGGLE_LINE_TOP:
3823 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3824 status.setOnOff(tabular.topLine(cur.idx()));
3827 case Tabular::TOGGLE_LINE_BOTTOM:
3828 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3829 status.setOnOff(tabular.bottomLine(cur.idx()));
3832 case Tabular::TOGGLE_LINE_LEFT:
3833 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3834 status.setOnOff(tabular.leftLine(cur.idx()));
3837 case Tabular::TOGGLE_LINE_RIGHT:
3838 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3839 status.setOnOff(tabular.rightLine(cur.idx()));
3842 case Tabular::M_ALIGN_LEFT:
3844 case Tabular::ALIGN_LEFT:
3845 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3848 case Tabular::M_ALIGN_RIGHT:
3850 case Tabular::ALIGN_RIGHT:
3851 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3854 case Tabular::M_ALIGN_CENTER:
3856 case Tabular::ALIGN_CENTER:
3857 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3860 case Tabular::ALIGN_BLOCK:
3861 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3862 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3865 case Tabular::M_VALIGN_TOP:
3867 case Tabular::VALIGN_TOP:
3869 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3872 case Tabular::M_VALIGN_BOTTOM:
3874 case Tabular::VALIGN_BOTTOM:
3876 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3879 case Tabular::M_VALIGN_MIDDLE:
3881 case Tabular::VALIGN_MIDDLE:
3883 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3886 case Tabular::SET_LONGTABULAR:
3887 status.setOnOff(tabular.is_long_tabular);
3890 case Tabular::UNSET_LONGTABULAR:
3891 status.setOnOff(!tabular.is_long_tabular);
3894 case Tabular::TOGGLE_ROTATE_TABULAR:
3895 case Tabular::SET_ROTATE_TABULAR:
3896 status.setOnOff(tabular.rotate);
3899 case Tabular::TABULAR_VALIGN_TOP:
3900 status.setOnOff(tabular.tabular_valignment
3901 == Tabular::LYX_VALIGN_TOP);
3903 case Tabular::TABULAR_VALIGN_MIDDLE:
3904 status.setOnOff(tabular.tabular_valignment
3905 == Tabular::LYX_VALIGN_MIDDLE);
3907 case Tabular::TABULAR_VALIGN_BOTTOM:
3908 status.setOnOff(tabular.tabular_valignment
3909 == Tabular::LYX_VALIGN_BOTTOM);
3912 case Tabular::UNSET_ROTATE_TABULAR:
3913 status.setOnOff(!tabular.rotate);
3916 case Tabular::TOGGLE_ROTATE_CELL:
3917 case Tabular::SET_ROTATE_CELL:
3918 status.setOnOff(!oneCellHasRotationState(false,
3919 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3922 case Tabular::UNSET_ROTATE_CELL:
3923 status.setOnOff(!oneCellHasRotationState(true,
3924 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3927 case Tabular::SET_USEBOX:
3928 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3931 // every row can only be one thing:
3932 // either a footer or header or caption
3933 case Tabular::SET_LTFIRSTHEAD:
3934 status.setEnabled(sel_row_start == sel_row_end
3935 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
3936 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
3937 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
3938 && !tabular.ltCaption(sel_row_start));
3939 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
3942 case Tabular::UNSET_LTFIRSTHEAD:
3943 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
3946 case Tabular::SET_LTHEAD:
3947 status.setEnabled(sel_row_start == sel_row_end
3948 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
3949 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
3950 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
3951 && !tabular.ltCaption(sel_row_start));
3952 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3955 case Tabular::UNSET_LTHEAD:
3956 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3959 case Tabular::SET_LTFOOT:
3960 status.setEnabled(sel_row_start == sel_row_end
3961 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
3962 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
3963 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
3964 && !tabular.ltCaption(sel_row_start));
3965 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3968 case Tabular::UNSET_LTFOOT:
3969 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3972 case Tabular::SET_LTLASTFOOT:
3973 status.setEnabled(sel_row_start == sel_row_end
3974 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
3975 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
3976 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
3977 && !tabular.ltCaption(sel_row_start));
3978 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
3981 case Tabular::UNSET_LTLASTFOOT:
3982 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
3985 case Tabular::SET_LTNEWPAGE:
3986 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3989 // only one row can be the caption
3990 case Tabular::TOGGLE_LTCAPTION:
3991 status.setEnabled(sel_row_start == sel_row_end
3992 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
3993 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
3994 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
3995 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
3996 && (!tabular.haveLTCaption()
3997 || tabular.ltCaption(sel_row_start)));
3998 status.setOnOff(tabular.ltCaption(sel_row_start));
4001 case Tabular::SET_BOOKTABS:
4002 status.setOnOff(tabular.use_booktabs);
4005 case Tabular::UNSET_BOOKTABS:
4006 status.setOnOff(!tabular.use_booktabs);
4011 status.setEnabled(false);
4017 // These are only enabled inside tabular
4018 case LFUN_CELL_BACKWARD:
4019 case LFUN_CELL_FORWARD:
4020 status.setEnabled(true);
4023 // disable these with multiple cells selected
4024 case LFUN_INSET_INSERT:
4025 case LFUN_TABULAR_INSERT:
4026 case LFUN_FLEX_INSERT:
4027 case LFUN_FLOAT_INSERT:
4028 case LFUN_FLOAT_WIDE_INSERT:
4029 case LFUN_FOOTNOTE_INSERT:
4030 case LFUN_MARGINALNOTE_INSERT:
4031 case LFUN_MATH_INSERT:
4032 case LFUN_MATH_MODE:
4033 case LFUN_MATH_MUTATE:
4034 case LFUN_MATH_DISPLAY:
4035 case LFUN_NOTE_INSERT:
4036 case LFUN_OPTIONAL_INSERT:
4037 case LFUN_BOX_INSERT:
4038 case LFUN_BRANCH_INSERT:
4039 case LFUN_PHANTOM_INSERT:
4040 case LFUN_WRAP_INSERT:
4041 case LFUN_ERT_INSERT: {
4042 if (cur.selIsMultiCell()) {
4043 status.setEnabled(false);
4046 return cell(cur.idx())->getStatus(cur, cmd, status);
4049 // disable in non-fixed-width cells
4050 case LFUN_NEWLINE_INSERT:
4051 case LFUN_BREAK_PARAGRAPH: {
4052 if (tabular.getPWidth(cur.idx()).zero()) {
4053 status.setEnabled(false);
4056 return cell(cur.idx())->getStatus(cur, cmd, status);
4059 case LFUN_NEWPAGE_INSERT:
4060 status.setEnabled(false);
4064 if (cur.selIsMultiCell()) {
4065 status.setEnabled(false);
4066 status.message(_("You cannot paste into a multicell selection."));
4069 if (tabularStackDirty() && theClipboard().isInternal()) {
4070 status.setEnabled(true);
4073 return cell(cur.idx())->getStatus(cur, cmd, status);
4075 case LFUN_INSET_MODIFY:
4076 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
4077 status.setEnabled(true);
4083 // we try to handle this event in the insets dispatch function.
4084 return cell(cur.idx())->getStatus(cur, cmd, status);
4089 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4091 return tabular.latex(os, runparams);
4095 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4097 os << '\n'; // output table on a new line
4098 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4099 tabular.plaintext(os, runparams, dp, false, 0);
4100 return PLAINTEXT_NEWLINE;
4104 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4109 // FIXME: Why not pass a proper DocIterator here?
4111 // if the table is inside a float it doesn't need the informaltable
4112 // wrapper. Search for it.
4113 for (master = owner(); master; master = master->owner())
4114 if (master->lyxCode() == FLOAT_CODE)
4119 os << "<informaltable>";
4122 ret += tabular.docbook(os, runparams);
4124 os << "</informaltable>";
4131 void InsetTabular::validate(LaTeXFeatures & features) const
4133 tabular.validate(features);
4137 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4139 return tabular.cellInset(idx);
4143 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4145 return tabular.cellInset(idx);
4149 void InsetTabular::cursorPos(BufferView const & bv,
4150 CursorSlice const & sl, bool boundary, int & x, int & y) const
4152 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4154 // y offset correction
4155 int const row = tabular.cellRow(sl.idx());
4156 for (int i = 0; i <= row; ++i) {
4158 y += tabular.rowAscent(i);
4159 y += tabular.getAdditionalHeight(i);
4162 y += tabular.rowDescent(i);
4165 // x offset correction
4166 int const col = tabular.cellColumn(sl.idx());
4167 int idx = tabular.cellIndex(row, 0);
4168 for (int j = 0; j < col; ++j) {
4169 if (tabular.isPartOfMultiColumn(row, j))
4171 x += tabular.columnWidth(idx);
4174 x += tabular.getBeginningOfTextInCell(idx);
4175 x += ADD_TO_TABULAR_WIDTH;
4180 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4184 Inset const & inset = *tabular.cellInset(cell);
4185 Point o = bv.coordCache().getInsets().xy(&inset);
4186 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
4187 int const xend = xbeg + tabular.columnWidth(cell);
4188 row_type const row = tabular.cellRow(cell);
4189 int const ybeg = o.y_ - tabular.rowAscent(row) -
4190 tabular.getAdditionalHeight(row);
4191 int const yend = o.y_ + tabular.rowDescent(row);
4203 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4204 // << " ybeg=" << ybeg << " yend=" << yend
4205 // << " xx=" << xx << " yy=" << yy
4206 // << " dist=" << xx + yy << endl;
4211 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4213 //lyxerr << "InsetTabular::editXY: " << this << endl;
4214 cur.setSelection(false);
4216 cur.idx() = getNearestCell(cur.bv(), x, y);
4218 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4222 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4224 cur.idx() = getNearestCell(cur.bv(), x, y);
4225 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4229 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4231 idx_type idx_min = 0;
4232 int dist_min = numeric_limits<int>::max();
4233 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4234 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4235 int const d = dist(bv, i, x, y);
4246 int InsetTabular::cellXPos(idx_type const cell) const
4250 for (; !tabular.isFirstCellInRow(c); --c)
4253 for (; c < cell; ++c)
4254 lx += tabular.columnWidth(c);
4260 void InsetTabular::resetPos(Cursor & cur) const
4262 BufferView & bv = cur.bv();
4263 int const maxwidth = bv.workWidth();
4265 int const scx_old = scx_;
4266 int const i = cur.find(this);
4271 int const X2 = maxwidth;
4272 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4273 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4274 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4277 scx_ = X1 + 20 - x1;
4279 scx_ = X2 - 20 - x2;
4284 // only update if offset changed
4285 if (scx_ != scx_old)
4286 cur.updateFlags(Update::Force | Update::FitCursor);
4290 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4292 if (isRightToLeft(cur)) {
4293 if (tabular.isFirstCellInRow(cur.idx())) {
4294 row_type const row = tabular.cellRow(cur.idx());
4295 if (row == tabular.row_info.size() - 1)
4297 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4304 if (tabular.isLastCell(cur.idx()))
4309 cur.boundary(false);
4311 if (cur.selIsMultiCell()) {
4312 cur.pit() = cur.lastpit();
4313 cur.pos() = cur.lastpos();
4321 // in visual mode, place cursor at extreme left or right
4323 switch(entry_from) {
4325 case ENTRY_DIRECTION_RIGHT:
4326 cur.posVisToRowExtremity(false /* !left */);
4328 case ENTRY_DIRECTION_LEFT:
4329 cur.posVisToRowExtremity(true /* left */);
4331 case ENTRY_DIRECTION_IGNORE:
4332 // nothing to do in this case
4341 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4343 if (isRightToLeft(cur)) {
4344 if (tabular.isLastCellInRow(cur.idx())) {
4345 row_type const row = tabular.cellRow(cur.idx());
4348 cur.idx() = tabular.getFirstCellInRow(row);
4349 cur.idx() = tabular.cellAbove(cur.idx());
4351 if (tabular.isLastCell(cur.idx()))
4356 if (cur.idx() == 0) // first cell
4361 if (cur.selIsMultiCell()) {
4362 cur.pit() = cur.lastpit();
4363 cur.pos() = cur.lastpos();
4368 cur.pit() = cur.lastpit();
4369 cur.pos() = cur.lastpos();
4371 // in visual mode, place cursor at extreme left or right
4373 switch(entry_from) {
4375 case ENTRY_DIRECTION_RIGHT:
4376 cur.posVisToRowExtremity(false /* !left */);
4378 case ENTRY_DIRECTION_LEFT:
4379 cur.posVisToRowExtremity(true /* left */);
4381 case ENTRY_DIRECTION_IGNORE:
4382 // nothing to do in this case
4391 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4393 Tabular::Feature action = Tabular::LAST_ACTION;
4396 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4397 string const tmp = tabularFeature[i].feature;
4399 if (tmp == what.substr(0, tmp.length())) {
4400 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4401 //tabularFeatures[i].feature.length()))
4402 action = tabularFeature[i].action;
4406 if (action == Tabular::LAST_ACTION)
4410 ltrim(what.substr(tabularFeature[i].feature.length()));
4411 tabularFeatures(cur, action, val);
4416 static void checkLongtableSpecial(Tabular::ltType & ltt,
4417 string const & special, bool & flag)
4419 if (special == "dl_above") {
4422 } else if (special == "dl_below") {
4423 ltt.bottomDL = flag;
4425 } else if (special == "empty") {
4434 bool InsetTabular::oneCellHasRotationState(bool rotated,
4435 row_type row_start, row_type row_end,
4436 col_type col_start, col_type col_end) const {
4438 for (row_type i = row_start; i <= row_end; ++i) {
4439 for (col_type j = col_start; j <= col_end; ++j) {
4440 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4449 void InsetTabular::tabularFeatures(Cursor & cur,
4450 Tabular::Feature feature, string const & value)
4452 col_type sel_col_start;
4453 col_type sel_col_end;
4454 row_type sel_row_start;
4455 row_type sel_row_end;
4456 bool setLines = false;
4457 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4458 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4462 case Tabular::M_ALIGN_LEFT:
4463 case Tabular::ALIGN_LEFT:
4464 setAlign = LYX_ALIGN_LEFT;
4467 case Tabular::M_ALIGN_RIGHT:
4468 case Tabular::ALIGN_RIGHT:
4469 setAlign = LYX_ALIGN_RIGHT;
4472 case Tabular::M_ALIGN_CENTER:
4473 case Tabular::ALIGN_CENTER:
4474 setAlign = LYX_ALIGN_CENTER;
4477 case Tabular::ALIGN_BLOCK:
4478 setAlign = LYX_ALIGN_BLOCK;
4481 case Tabular::M_VALIGN_TOP:
4482 case Tabular::VALIGN_TOP:
4483 setVAlign = Tabular::LYX_VALIGN_TOP;
4486 case Tabular::M_VALIGN_BOTTOM:
4487 case Tabular::VALIGN_BOTTOM:
4488 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4491 case Tabular::M_VALIGN_MIDDLE:
4492 case Tabular::VALIGN_MIDDLE:
4493 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4500 cur.recordUndoInset(ATOMIC_UNDO);
4502 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4503 row_type const row = tabular.cellRow(cur.idx());
4504 col_type const column = tabular.cellColumn(cur.idx());
4506 Tabular::ltType ltt;
4510 case Tabular::SET_PWIDTH: {
4511 Length const len(value);
4512 tabular.setColumnPWidth(cur, cur.idx(), len);
4514 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4515 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4519 case Tabular::SET_MPWIDTH:
4520 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4523 case Tabular::SET_SPECIAL_COLUMN:
4524 case Tabular::SET_SPECIAL_MULTI:
4525 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4528 case Tabular::APPEND_ROW:
4529 // append the row into the tabular
4530 tabular.appendRow(cur.idx());
4533 case Tabular::APPEND_COLUMN:
4534 // append the column into the tabular
4535 tabular.appendColumn(cur.idx());
4536 cur.idx() = tabular.cellIndex(row, column);
4539 case Tabular::DELETE_ROW:
4540 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4541 tabular.deleteRow(sel_row_start);
4542 if (sel_row_start >= tabular.row_info.size())
4544 cur.idx() = tabular.cellIndex(sel_row_start, column);
4547 cur.setSelection(false);
4550 case Tabular::DELETE_COLUMN:
4551 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4552 tabular.deleteColumn(sel_col_start);
4553 if (sel_col_start >= tabular.column_info.size())
4555 cur.idx() = tabular.cellIndex(row, sel_col_start);
4558 cur.setSelection(false);
4561 case Tabular::COPY_ROW:
4562 tabular.copyRow(row);
4565 case Tabular::COPY_COLUMN:
4566 tabular.copyColumn(column);
4567 cur.idx() = tabular.cellIndex(row, column);
4570 case Tabular::TOGGLE_LINE_TOP: {
4571 bool lineSet = !tabular.topLine(cur.idx());
4572 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4573 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4574 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4578 case Tabular::TOGGLE_LINE_BOTTOM: {
4579 bool lineSet = !tabular.bottomLine(cur.idx());
4580 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4581 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4582 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4586 case Tabular::TOGGLE_LINE_LEFT: {
4587 bool lineSet = !tabular.leftLine(cur.idx());
4588 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4589 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4590 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4594 case Tabular::TOGGLE_LINE_RIGHT: {
4595 bool lineSet = !tabular.rightLine(cur.idx());
4596 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4597 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4598 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4602 case Tabular::M_ALIGN_LEFT:
4603 case Tabular::M_ALIGN_RIGHT:
4604 case Tabular::M_ALIGN_CENTER:
4606 case Tabular::ALIGN_LEFT:
4607 case Tabular::ALIGN_RIGHT:
4608 case Tabular::ALIGN_CENTER:
4609 case Tabular::ALIGN_BLOCK:
4610 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4611 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4612 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4615 case Tabular::M_VALIGN_TOP:
4616 case Tabular::M_VALIGN_BOTTOM:
4617 case Tabular::M_VALIGN_MIDDLE:
4619 case Tabular::VALIGN_TOP:
4620 case Tabular::VALIGN_BOTTOM:
4621 case Tabular::VALIGN_MIDDLE:
4622 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4623 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4624 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4627 case Tabular::MULTICOLUMN: {
4628 if (!cur.selection()) {
4629 // just multicol for one single cell
4630 // check whether we are completely in a multicol
4631 if (tabular.isMultiColumn(cur.idx()))
4632 tabular.unsetMultiColumn(cur.idx());
4634 tabular.setMultiColumn(cur.idx(), 1);
4637 // we have a selection so this means we just add all this
4638 // cells to form a multicolumn cell
4639 idx_type const s_start = cur.selBegin().idx();
4640 idx_type const s_end = cur.selEnd().idx();
4641 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4642 cur.idx() = s_start;
4645 cur.setSelection(false);
4649 case Tabular::SET_ALL_LINES:
4651 case Tabular::UNSET_ALL_LINES:
4652 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4653 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4654 tabular.setAllLines(
4655 tabular.cellIndex(i,j), setLines);
4658 case Tabular::SET_BORDER_LINES:
4659 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4660 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4661 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4663 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4664 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4665 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4669 case Tabular::SET_LONGTABULAR:
4670 tabular.is_long_tabular = true;
4673 case Tabular::UNSET_LONGTABULAR:
4674 tabular.is_long_tabular = false;
4677 case Tabular::SET_ROTATE_TABULAR:
4678 tabular.rotate = true;
4681 case Tabular::UNSET_ROTATE_TABULAR:
4682 tabular.rotate = false;
4685 case Tabular::TOGGLE_ROTATE_TABULAR:
4686 tabular.rotate = !tabular.rotate;
4689 case Tabular::TABULAR_VALIGN_TOP:
4690 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
4693 case Tabular::TABULAR_VALIGN_MIDDLE:
4694 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
4697 case Tabular::TABULAR_VALIGN_BOTTOM:
4698 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
4701 case Tabular::SET_ROTATE_CELL:
4702 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4703 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4704 tabular.setRotateCell(
4705 tabular.cellIndex(i, j), true);
4708 case Tabular::UNSET_ROTATE_CELL:
4709 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4710 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4711 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4714 case Tabular::TOGGLE_ROTATE_CELL:
4716 bool oneNotRotated = oneCellHasRotationState(false,
4717 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4719 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4720 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4721 tabular.setRotateCell(tabular.cellIndex(i, j),
4726 case Tabular::SET_USEBOX: {
4727 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4728 if (val == tabular.getUsebox(cur.idx()))
4729 val = Tabular::BOX_NONE;
4730 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4731 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4732 tabular.setUsebox(tabular.cellIndex(i, j), val);
4736 case Tabular::UNSET_LTFIRSTHEAD:
4738 case Tabular::SET_LTFIRSTHEAD:
4739 tabular.getRowOfLTFirstHead(row, ltt);
4740 checkLongtableSpecial(ltt, value, flag);
4741 tabular.setLTHead(row, flag, ltt, true);
4744 case Tabular::UNSET_LTHEAD:
4746 case Tabular::SET_LTHEAD:
4747 tabular.getRowOfLTHead(row, ltt);
4748 checkLongtableSpecial(ltt, value, flag);
4749 tabular.setLTHead(row, flag, ltt, false);
4752 case Tabular::UNSET_LTFOOT:
4754 case Tabular::SET_LTFOOT:
4755 tabular.getRowOfLTFoot(row, ltt);
4756 checkLongtableSpecial(ltt, value, flag);
4757 tabular.setLTFoot(row, flag, ltt, false);
4760 case Tabular::UNSET_LTLASTFOOT:
4762 case Tabular::SET_LTLASTFOOT:
4763 tabular.getRowOfLTLastFoot(row, ltt);
4764 checkLongtableSpecial(ltt, value, flag);
4765 tabular.setLTFoot(row, flag, ltt, true);
4768 case Tabular::SET_LTNEWPAGE:
4769 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4772 case Tabular::TOGGLE_LTCAPTION:
4773 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4776 cur.setSelection(false);
4779 case Tabular::SET_BOOKTABS:
4780 tabular.use_booktabs = true;
4783 case Tabular::UNSET_BOOKTABS:
4784 tabular.use_booktabs = false;
4787 case Tabular::SET_TOP_SPACE: {
4789 if (value == "default")
4790 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4791 tabular.row_info[i].top_space_default = true;
4792 else if (isValidLength(value, &len))
4793 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4794 tabular.row_info[i].top_space_default = false;
4795 tabular.row_info[i].top_space = len;
4798 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4799 tabular.row_info[i].top_space_default = false;
4800 tabular.row_info[i].top_space = len;
4805 case Tabular::SET_BOTTOM_SPACE: {
4807 if (value == "default")
4808 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4809 tabular.row_info[i].bottom_space_default = true;
4810 else if (isValidLength(value, &len))
4811 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4812 tabular.row_info[i].bottom_space_default = false;
4813 tabular.row_info[i].bottom_space = len;
4816 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4817 tabular.row_info[i].bottom_space_default = false;
4818 tabular.row_info[i].bottom_space = len;
4823 case Tabular::SET_INTERLINE_SPACE: {
4825 if (value == "default")
4826 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4827 tabular.row_info[i].interline_space_default = true;
4828 else if (isValidLength(value, &len))
4829 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4830 tabular.row_info[i].interline_space_default = false;
4831 tabular.row_info[i].interline_space = len;
4834 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4835 tabular.row_info[i].interline_space_default = false;
4836 tabular.row_info[i].interline_space = len;
4841 // dummy stuff just to avoid warnings
4842 case Tabular::LAST_ACTION:
4848 bool InsetTabular::showInsetDialog(BufferView * bv) const
4850 bv->showDialog("tabular", params2string(*this),
4851 const_cast<InsetTabular *>(this));
4856 void InsetTabular::openLayoutDialog(BufferView * bv) const
4858 bv->showDialog("tabular", params2string(*this),
4859 const_cast<InsetTabular *>(this));
4863 bool InsetTabular::copySelection(Cursor & cur)
4865 if (!cur.selection())
4870 getSelection(cur, rs, re, cs, ce);
4872 paste_tabular.reset(new Tabular(tabular));
4874 for (row_type i = 0; i < rs; ++i)
4875 paste_tabular->deleteRow(0);
4877 row_type const rows = re - rs + 1;
4878 while (paste_tabular->row_info.size() > rows)
4879 paste_tabular->deleteRow(rows);
4881 for (col_type i = 0; i < cs; ++i)
4882 paste_tabular->deleteColumn(0);
4884 col_type const columns = ce - cs + 1;
4885 while (paste_tabular->column_info.size() > columns)
4886 paste_tabular->deleteColumn(columns);
4888 paste_tabular->setBuffer(tabular.buffer());
4890 odocstringstream os;
4891 OutputParams const runparams(0);
4892 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4893 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4894 cap::copySelection(cur, os.str());
4896 // mark tabular stack dirty
4897 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4898 // when we (hopefully) have a one-for-all paste mechanism.
4899 // This must be called after cap::copySelection.
4900 dirtyTabularStack(true);
4906 bool InsetTabular::pasteClipboard(Cursor & cur)
4910 col_type const actcol = tabular.cellColumn(cur.idx());
4911 row_type const actrow = tabular.cellRow(cur.idx());
4912 for (row_type r1 = 0, r2 = actrow;
4913 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4915 for (col_type c1 = 0, c2 = actcol;
4916 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4918 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4919 tabular.isPartOfMultiColumn(r2, c2))
4921 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4925 if (tabular.isPartOfMultiColumn(r2, c2)) {
4929 shared_ptr<InsetTableCell> inset(
4930 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4931 tabular.setCellInset(r2, c2, inset);
4932 // FIXME: change tracking (MG)
4933 inset->setChange(Change(buffer().params().trackChanges ?
4934 Change::INSERTED : Change::UNCHANGED));
4942 void InsetTabular::cutSelection(Cursor & cur)
4944 if (!cur.selection())
4949 getSelection(cur, rs, re, cs, ce);
4950 for (row_type i = rs; i <= re; ++i) {
4951 for (col_type j = cs; j <= ce; ++j) {
4952 shared_ptr<InsetTableCell> t
4953 = cell(tabular.cellIndex(i, j));
4954 if (buffer().params().trackChanges)
4955 // FIXME: Change tracking (MG)
4956 t->setChange(Change(Change::DELETED));
4962 // cursor position might be invalid now
4963 if (cur.pit() > cur.lastpit())
4964 cur.pit() = cur.lastpit();
4965 if (cur.pos() > cur.lastpos())
4966 cur.pos() = cur.lastpos();
4967 cur.clearSelection();
4971 bool InsetTabular::isRightToLeft(Cursor & cur) const
4973 LASSERT(cur.depth() > 1, /**/);
4974 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4975 pos_type const parentpos = cur[cur.depth() - 2].pos();
4976 return parentpar.getFontSettings(buffer().params(),
4977 parentpos).language()->rightToLeft();
4980 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4983 LASSERT(stidx <= enidx, return docstring());
4985 col_type const col1 = tabular.cellColumn(stidx);
4986 col_type const col2 = tabular.cellColumn(enidx);
4987 row_type const row1 = tabular.cellRow(stidx);
4988 row_type const row2 = tabular.cellRow(enidx);
4990 for (col_type col = col1; col <= col2; col++)
4991 for (row_type row = row1; row <= row2; row++) {
4996 retval += tabular.cellInset(row, col)->asString(intoInsets);
5002 void InsetTabular::getSelection(Cursor & cur,
5003 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5005 CursorSlice const & beg = cur.selBegin();
5006 CursorSlice const & end = cur.selEnd();
5007 cs = tabular.cellColumn(beg.idx());
5008 ce = tabular.cellColumn(end.idx());
5011 cs = tabular.cellColumn(end.idx());
5013 ce = tabular.cellRightColumn(end.idx());
5016 rs = tabular.cellRow(beg.idx());
5017 re = tabular.cellRow(end.idx());
5023 Text * InsetTabular::getText(int idx) const
5025 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5029 void InsetTabular::setChange(Change const & change)
5031 for (idx_type idx = 0; idx < nargs(); ++idx)
5032 cell(idx)->setChange(change);
5036 void InsetTabular::acceptChanges(BufferParams const & bparams)
5038 for (idx_type idx = 0; idx < nargs(); ++idx)
5039 cell(idx)->acceptChanges(bparams);
5043 void InsetTabular::rejectChanges(BufferParams const & bparams)
5045 for (idx_type idx = 0; idx < nargs(); ++idx)
5046 cell(idx)->rejectChanges(bparams);
5050 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5052 return tabular.getPWidth(cell).zero();
5056 bool InsetTabular::forcePlainLayout(idx_type cell) const
5058 return !tabular.getPWidth(cell).zero();
5062 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5065 if (buf.length() <= 0)
5070 col_type maxCols = 1;
5071 size_t const len = buf.length();
5075 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5083 maxCols = max(cols, maxCols);
5089 maxCols = max(cols, maxCols);
5095 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
5096 loctab = paste_tabular.get();
5098 dirtyTabularStack(true);
5101 cell = bv.cursor().idx();
5102 ocol = tabular.cellColumn(cell);
5103 row = tabular.cellRow(cell);
5107 idx_type const cells = loctab->numberofcells;
5110 rows = loctab->row_info.size();
5111 col_type const columns = loctab->column_info.size();
5113 while (cell < cells && p < len && row < rows &&
5114 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5120 // we can only set this if we are not too far right
5121 if (cols < columns) {
5122 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5123 Font const font = bv.textMetrics(&inset->text()).
5125 inset->setText(buf.substr(op, p - op), font,
5126 buffer().params().trackChanges);
5132 // we can only set this if we are not too far right
5133 if (cols < columns) {
5134 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5135 Font const font = bv.textMetrics(&inset->text()).
5137 inset->setText(buf.substr(op, p - op), font,
5138 buffer().params().trackChanges);
5143 cell = loctab->cellIndex(row, cols);
5149 // check for the last cell if there is no trailing '\n'
5150 if (cell < cells && op < len) {
5151 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5152 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5153 inset->setText(buf.substr(op, len - op), font,
5154 buffer().params().trackChanges);
5160 void InsetTabular::addPreview(PreviewLoader & loader) const
5162 row_type const rows = tabular.row_info.size();
5163 col_type const columns = tabular.column_info.size();
5164 for (row_type i = 0; i < rows; ++i) {
5165 for (col_type j = 0; j < columns; ++j)
5166 tabular.cellInset(i, j)->addPreview(loader);
5171 bool InsetTabular::completionSupported(Cursor const & cur) const
5173 Cursor const & bvCur = cur.bv().cursor();
5174 if (&bvCur.inset() != this)
5176 return cur.text()->completionSupported(cur);
5180 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5182 return completionSupported(cur);
5186 bool InsetTabular::automaticInlineCompletion() const
5188 return lyxrc.completion_inline_text;
5192 bool InsetTabular::automaticPopupCompletion() const
5194 return lyxrc.completion_popup_text;
5198 bool InsetTabular::showCompletionCursor() const
5200 return lyxrc.completion_cursor_text;
5204 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5206 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5210 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5212 if (!completionSupported(cur))
5214 return cur.text()->completionPrefix(cur);
5218 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5220 if (!completionSupported(cur))
5223 return cur.text()->insertCompletion(cur, s, finished);
5227 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5228 Dimension & dim) const
5230 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5231 tm.completionPosAndDim(cur, x, y, dim);
5235 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5237 istringstream data(in);
5239 lex.setStream(data);
5246 if (!lex || token != "tabular") {
5247 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5251 // This is part of the inset proper that is usually swallowed
5252 // by Buffer::readInset
5254 if (!lex || token != "Tabular") {
5255 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5263 string InsetTabular::params2string(InsetTabular const & inset)
5266 data << "tabular" << ' ';
5268 data << "\\end_inset\n";