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 column_info.erase(column_info.begin() + column);
734 size_t row_count = row_info.size();
735 for (row_type i = 0; i < row_count; ++i) {
736 // Care about multicolumn cells
737 if (column + 1 < column_info.size() &&
738 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
739 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
740 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
742 cell_info[i].erase(cell_info[i].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 if ((!column_info[c].p_width.zero() && !multicol)
1026 || (multicol && !cell_info[r][c].p_width.zero())) {
1027 cell_info[r][c].inset->toggleFixedWidth(true);
1034 void Tabular::updateContentAlignment(row_type r, col_type c)
1036 cell_info[r][c].inset->setContentAlignment(
1037 getAlignment(cellIndex(r, c)));
1041 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1042 Length const & width)
1044 if (!isMultiColumn(cell))
1047 cellInfo(cell).p_width = width;
1048 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1049 // cur paragraph can become invalid after paragraphs were merged
1050 if (cur.pit() > cur.lastpit())
1051 cur.pit() = cur.lastpit();
1052 // cur position can become invalid after newlines were removed
1053 if (cur.pos() > cur.lastpos())
1054 cur.pos() = cur.lastpos();
1059 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1060 Tabular::Feature what)
1062 if (what == SET_SPECIAL_MULTI)
1063 cellInfo(cell).align_special = special;
1065 column_info[cellColumn(cell)].align_special = special;
1069 void Tabular::setAllLines(idx_type cell, bool line)
1071 setTopLine(cell, line);
1072 setBottomLine(cell, line);
1073 setRightLine(cell, line);
1074 setLeftLine(cell, line);
1078 void Tabular::setTopLine(idx_type i, bool line)
1080 cellInfo(i).top_line = line;
1084 void Tabular::setBottomLine(idx_type i, bool line)
1086 cellInfo(i).bottom_line = line;
1090 void Tabular::setLeftLine(idx_type cell, bool line)
1092 cellInfo(cell).left_line = line;
1096 void Tabular::setRightLine(idx_type cell, bool line)
1098 cellInfo(cell).right_line = line;
1101 bool Tabular::rowTopLine(row_type r) const
1103 idx_type i0 = getFirstCellInRow(r);
1104 idx_type i1 = getLastCellInRow(r);
1105 bool all_rows_set = true;
1106 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1107 all_rows_set = cellInfo(j).top_line;
1108 return all_rows_set;
1112 bool Tabular::rowBottomLine(row_type r) const
1114 idx_type i0 = getFirstCellInRow(r);
1115 idx_type i1 = getLastCellInRow(r);
1116 bool all_rows_set = true;
1117 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1118 all_rows_set = cellInfo(j).bottom_line;
1119 return all_rows_set;
1123 bool Tabular::columnLeftLine(col_type c) const
1130 row_type const nrows = row_info.size();
1131 for (row_type r = 0; r < nrows; ++r) {
1132 idx_type i = cellIndex(r, c);
1133 if (c == cellColumn(i)) {
1135 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1136 if (cellInfo(i).left_line || right)
1140 return 2 * nrows_left >= total;
1144 bool Tabular::columnRightLine(col_type c) const
1149 int nrows_right = 0;
1151 row_type const nrows = row_info.size();
1152 for (row_type r = 0; r < nrows; ++r) {
1153 idx_type i = cellIndex(r, c);
1154 if (c == cellColumn(i) + columnSpan(i) - 1) {
1156 bool left = (c + 1 < column_info.size()
1157 && cellInfo(cellIndex(r, c + 1)).left_line)
1158 || c + 1 == column_info.size();
1159 if (cellInfo(i).right_line && left)
1163 return 2 * nrows_right >= total;
1167 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1169 if (!onlycolumn && isMultiColumn(cell))
1170 return cellInfo(cell).alignment;
1171 return column_info[cellColumn(cell)].alignment;
1176 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1178 if (!onlycolumn && isMultiColumn(cell))
1179 return cellInfo(cell).valignment;
1180 return column_info[cellColumn(cell)].valignment;
1184 Length const Tabular::getPWidth(idx_type cell) const
1186 if (isMultiColumn(cell))
1187 return cellInfo(cell).p_width;
1188 return column_info[cellColumn(cell)].p_width;
1192 int Tabular::cellWidth(idx_type cell) const
1194 return cellInfo(cell).width;
1198 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1202 switch (getAlignment(cell)) {
1203 case LYX_ALIGN_CENTER:
1204 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1206 case LYX_ALIGN_RIGHT:
1207 x += columnWidth(cell) - cellWidth(cell);
1208 // + getAdditionalWidth(cell);
1211 // LYX_ALIGN_LEFT: nothing :-)
1215 // the LaTeX Way :-(
1221 bool Tabular::isFirstCellInRow(idx_type cell) const
1223 return cellColumn(cell) == 0;
1227 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1229 if (row > row_info.size() - 1)
1230 row = row_info.size() - 1;
1231 return cell_info[row][0].cellno;
1235 bool Tabular::isLastCellInRow(idx_type cell) const
1237 return cellRightColumn(cell) == column_info.size() - 1;
1241 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1243 if (row > row_info.size() - 1)
1244 row = row_info.size() - 1;
1245 return cell_info[row][column_info.size() - 1].cellno;
1249 Tabular::row_type Tabular::cellRow(idx_type cell) const
1251 if (cell >= numberofcells)
1252 return row_info.size() - 1;
1255 return rowofcell[cell];
1259 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1261 if (cell >= numberofcells)
1262 return column_info.size() - 1;
1265 return columnofcell[cell];
1269 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1271 row_type const row = cellRow(cell);
1272 col_type column = cellColumn(cell);
1273 while (column < column_info.size() - 1 &&
1274 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1280 void Tabular::write(ostream & os) const
1284 << write_attribute("version", 3)
1285 << write_attribute("rows", row_info.size())
1286 << write_attribute("columns", column_info.size())
1288 // global longtable options
1290 << write_attribute("rotate", rotate)
1291 << write_attribute("booktabs", use_booktabs)
1292 << write_attribute("islongtable", is_long_tabular)
1293 << write_attribute("tabularvalignment", tabular_valignment)
1294 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1295 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1296 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1297 << write_attribute("headTopDL", endhead.topDL)
1298 << write_attribute("headBottomDL", endhead.bottomDL)
1299 << write_attribute("footTopDL", endfoot.topDL)
1300 << write_attribute("footBottomDL", endfoot.bottomDL)
1301 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1302 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1303 << write_attribute("lastFootEmpty", endlastfoot.empty)
1305 for (col_type j = 0; j < column_info.size(); ++j) {
1307 << write_attribute("alignment", column_info[j].alignment)
1308 << write_attribute("valignment", column_info[j].valignment)
1309 << write_attribute("width", column_info[j].p_width.asString())
1310 << write_attribute("special", column_info[j].align_special)
1313 for (row_type i = 0; i < row_info.size(); ++i) {
1314 static const string def("default");
1316 if (row_info[i].top_space_default)
1317 os << write_attribute("topspace", def);
1319 os << write_attribute("topspace", row_info[i].top_space);
1320 if (row_info[i].bottom_space_default)
1321 os << write_attribute("bottomspace", def);
1323 os << write_attribute("bottomspace", row_info[i].bottom_space);
1324 if (row_info[i].interline_space_default)
1325 os << write_attribute("interlinespace", def);
1327 os << write_attribute("interlinespace", row_info[i].interline_space);
1328 os << write_attribute("endhead", row_info[i].endhead)
1329 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1330 << write_attribute("endfoot", row_info[i].endfoot)
1331 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1332 << write_attribute("newpage", row_info[i].newpage)
1333 << write_attribute("caption", row_info[i].caption)
1335 for (col_type j = 0; j < column_info.size(); ++j) {
1337 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1338 << write_attribute("alignment", cell_info[i][j].alignment)
1339 << write_attribute("valignment", cell_info[i][j].valignment)
1340 << write_attribute("topline", cell_info[i][j].top_line)
1341 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1342 << write_attribute("leftline", cell_info[i][j].left_line)
1343 << write_attribute("rightline", cell_info[i][j].right_line)
1344 << write_attribute("rotate", cell_info[i][j].rotate)
1345 << write_attribute("usebox", cell_info[i][j].usebox)
1346 << write_attribute("width", cell_info[i][j].p_width)
1347 << write_attribute("special", cell_info[i][j].align_special)
1349 os << "\\begin_inset ";
1350 cell_info[i][j].inset->write(os);
1351 os << "\n\\end_inset\n"
1356 os << "</lyxtabular>\n";
1360 void Tabular::read(Lexer & lex)
1363 istream & is = lex.getStream();
1365 l_getline(is, line);
1366 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1367 LASSERT(false, /**/);
1372 if (!getTokenValue(line, "version", version))
1374 LASSERT(version >= 2, /**/);
1377 if (!getTokenValue(line, "rows", rows_arg))
1380 if (!getTokenValue(line, "columns", columns_arg))
1382 init(buffer(), rows_arg, columns_arg);
1383 l_getline(is, line);
1384 if (!prefixIs(line, "<features")) {
1385 lyxerr << "Wrong tabular format (expected <features ...> got"
1386 << line << ')' << endl;
1389 getTokenValue(line, "rotate", rotate);
1390 getTokenValue(line, "booktabs", use_booktabs);
1391 getTokenValue(line, "islongtable", is_long_tabular);
1392 getTokenValue(line, "tabularvalignment", tabular_valignment);
1393 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1394 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1395 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1396 getTokenValue(line, "headTopDL", endhead.topDL);
1397 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1398 getTokenValue(line, "footTopDL", endfoot.topDL);
1399 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1400 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1401 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1402 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1404 for (col_type j = 0; j < column_info.size(); ++j) {
1406 if (!prefixIs(line,"<column")) {
1407 lyxerr << "Wrong tabular format (expected <column ...> got"
1408 << line << ')' << endl;
1411 getTokenValue(line, "alignment", column_info[j].alignment);
1412 getTokenValue(line, "valignment", column_info[j].valignment);
1413 getTokenValue(line, "width", column_info[j].p_width);
1414 getTokenValue(line, "special", column_info[j].align_special);
1417 for (row_type i = 0; i < row_info.size(); ++i) {
1418 l_getline(is, line);
1419 if (!prefixIs(line, "<row")) {
1420 lyxerr << "Wrong tabular format (expected <row ...> got"
1421 << line << ')' << endl;
1424 getTokenValue(line, "topspace", row_info[i].top_space,
1425 row_info[i].top_space_default);
1426 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1427 row_info[i].bottom_space_default);
1428 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1429 row_info[i].interline_space_default);
1430 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1431 getTokenValue(line, "endhead", row_info[i].endhead);
1432 getTokenValue(line, "endfoot", row_info[i].endfoot);
1433 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1434 getTokenValue(line, "newpage", row_info[i].newpage);
1435 getTokenValue(line, "caption", row_info[i].caption);
1436 for (col_type j = 0; j < column_info.size(); ++j) {
1437 l_getline(is, line);
1438 if (!prefixIs(line, "<cell")) {
1439 lyxerr << "Wrong tabular format (expected <cell ...> got"
1440 << line << ')' << endl;
1443 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1444 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1445 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1446 getTokenValue(line, "topline", cell_info[i][j].top_line);
1447 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1448 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1449 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1450 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1451 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1452 getTokenValue(line, "width", cell_info[i][j].p_width);
1454 getTokenValue(line, "special", cell_info[i][j].align_special);
1455 l_getline(is, line);
1456 if (prefixIs(line, "\\begin_inset")) {
1457 cell_info[i][j].inset->setBuffer(*buffer_);
1458 cell_info[i][j].inset->read(lex);
1459 l_getline(is, line);
1461 if (!prefixIs(line, "</cell>")) {
1462 lyxerr << "Wrong tabular format (expected </cell> got"
1463 << line << ')' << endl;
1467 l_getline(is, line);
1468 if (!prefixIs(line, "</row>")) {
1469 lyxerr << "Wrong tabular format (expected </row> got"
1470 << line << ')' << endl;
1474 while (!prefixIs(line, "</lyxtabular>")) {
1475 l_getline(is, line);
1481 bool Tabular::isMultiColumn(idx_type cell) const
1483 return cellInfo(cell).multicolumn != CELL_NORMAL;
1487 bool Tabular::isMultiColumnReal(idx_type cell) const
1489 return cellColumn(cell) != cellRightColumn(cell) &&
1490 cellInfo(cell).multicolumn != CELL_NORMAL;
1494 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1496 return cell_info[cellRow(cell)][cellColumn(cell)];
1500 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1502 CellData & cs = cellInfo(cell);
1503 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1504 cs.alignment = column_info[cellColumn(cell)].alignment;
1505 setRightLine(cell, rightLine(cell + number - 1));
1506 for (idx_type i = 1; i < number; ++i) {
1507 CellData & cs1 = cellInfo(cell + i);
1508 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1509 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1516 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1518 row_type const row = cellRow(cell);
1519 col_type const ncols = column_info.size();
1520 idx_type result = 1;
1521 col_type column = cellColumn(cell) + 1;
1522 while (column < ncols && isPartOfMultiColumn(row, column)) {
1530 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1532 row_type const row = cellRow(cell);
1533 col_type column = cellColumn(cell);
1535 idx_type result = 0;
1537 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1538 cell_info[row][column].multicolumn = CELL_NORMAL;
1540 while (column < column_info.size() &&
1541 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1543 cell_info[row][column].multicolumn = CELL_NORMAL;
1553 void Tabular::setRotateCell(idx_type cell, bool flag)
1555 cellInfo(cell).rotate = flag;
1559 bool Tabular::getRotateCell(idx_type cell) const
1561 return cellInfo(cell).rotate;
1565 bool Tabular::needRotating() const
1569 for (row_type i = 0; i < row_info.size(); ++i)
1570 for (col_type j = 0; j < column_info.size(); ++j)
1571 if (cell_info[i][j].rotate)
1577 bool Tabular::isLastCell(idx_type cell) const
1579 if (cell + 1 < numberofcells)
1585 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1587 if (cellRow(cell) > 0)
1588 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1593 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1595 if (cellRow(cell) + 1 < row_info.size())
1596 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1601 Tabular::idx_type Tabular::cellIndex(row_type row,
1602 col_type column) const
1604 BOOST_ASSERT(column != npos && column < column_info.size()
1605 && row != npos && row < row_info.size());
1606 return cell_info[row][column].cellno;
1610 void Tabular::setUsebox(idx_type cell, BoxType type)
1612 cellInfo(cell).usebox = type;
1616 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1617 // adds line breaks, see bug 4886.
1618 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1620 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1621 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1623 if (cellInfo(cell).usebox > 1)
1624 return cellInfo(cell).usebox;
1625 return useParbox(cell);
1630 // This are functions used for the longtable support
1632 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1638 row_info[row].endfirsthead = flag;
1642 row_info[row].endhead = flag;
1647 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1650 hd.set = haveLTHead();
1651 return row_info[row].endhead;
1655 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1658 hd.set = haveLTFirstHead();
1659 return row_info[row].endfirsthead;
1663 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1669 row_info[row].endlastfoot = flag;
1673 row_info[row].endfoot = flag;
1678 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1681 fd.set = haveLTFoot();
1682 return row_info[row].endfoot;
1686 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1689 fd.set = haveLTLastFoot();
1690 return row_info[row].endlastfoot;
1694 void Tabular::setLTNewPage(row_type row, bool what)
1696 row_info[row].newpage = what;
1700 bool Tabular::getLTNewPage(row_type row) const
1702 return row_info[row].newpage;
1706 bool Tabular::haveLTHead() const
1708 for (row_type i = 0; i < row_info.size(); ++i)
1709 if (row_info[i].endhead)
1715 bool Tabular::haveLTFirstHead() const
1717 if (endfirsthead.empty)
1719 for (row_type i = 0; i < row_info.size(); ++i)
1720 if (row_info[i].endfirsthead)
1726 bool Tabular::haveLTFoot() const
1728 for (row_type i = 0; i < row_info.size(); ++i)
1729 if (row_info[i].endfoot)
1735 bool Tabular::haveLTLastFoot() const
1737 if (endlastfoot.empty)
1739 for (row_type i = 0; i < row_info.size(); ++i)
1740 if (row_info[i].endlastfoot)
1746 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1748 idx_type i = getFirstCellInRow(row);
1750 setMultiColumn(i, column_info.size());
1751 setTopLine(i, false);
1752 setBottomLine(i, false);
1753 setLeftLine(i, false);
1754 setRightLine(i, false);
1756 unsetMultiColumn(i);
1757 row_info[row].caption = what;
1762 bool Tabular::ltCaption(row_type row) const
1764 return row_info[row].caption;
1768 // end longtable support functions
1770 void Tabular::setRowAscent(row_type row, int height)
1772 if (row >= row_info.size() || row_info[row].ascent == height)
1774 row_info[row].ascent = height;
1778 void Tabular::setRowDescent(row_type row, int height)
1780 if (row >= row_info.size() || row_info[row].descent == height)
1782 row_info[row].descent = height;
1786 int Tabular::rowAscent(row_type row) const
1788 if (row >= row_info.size())
1790 return row_info[row].ascent;
1794 int Tabular::rowDescent(row_type row) const
1796 LASSERT(row < row_info.size(), /**/);
1797 return row_info[row].descent;
1801 int Tabular::height() const
1804 for (row_type row = 0; row < row_info.size(); ++row)
1805 height += rowAscent(row) + rowDescent(row) +
1806 getAdditionalHeight(row);
1811 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1813 LASSERT(row < row_info.size(), /**/);
1814 LASSERT(column < column_info.size(), /**/);
1815 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1819 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1821 // we only output complete row lines and the 1st row here, the rest
1822 // is done in Tabular::TeXBottomHLine(...)
1824 // get for each column the topline (if any)
1825 col_type const ncols = column_info.size();
1826 vector<bool> topline;
1828 for (col_type c = 0; c < ncols; ++c) {
1829 topline.push_back(topLine(cellIndex(row, c)));
1834 // do nothing if empty first row, or incomplete row line after
1835 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1838 // only output complete row lines and the 1st row's clines
1839 if (nset == ncols) {
1841 os << (row == 0 ? "\\toprule " : "\\midrule ");
1845 } else if (row == 0) {
1846 for (col_type c = 0; c < ncols; ++c) {
1848 //babel makes the "-" character an active one, so we have to suppress this here
1849 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1850 if (lang == "slovak" || lang == "czech")
1851 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1852 "\\expandafter\\cline\\expandafter{\\expandafter")
1853 << c + 1 << "\\string-";
1855 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1856 // get to last column of line span
1857 while (c < ncols && topline[c])
1868 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1870 // we output bottomlines of row r and the toplines of row r+1
1871 // if the latter do not span the whole tabular
1873 // get the bottomlines of row r, and toplines in next row
1874 bool lastrow = row == row_info.size() - 1;
1875 col_type const ncols = column_info.size();
1876 vector<bool> bottomline, topline;
1877 bool nextrowset = true;
1878 for (col_type c = 0; c < ncols; ++c) {
1879 bottomline.push_back(bottomLine(cellIndex(row, c)));
1880 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1881 nextrowset &= topline[c];
1884 // combine this row's bottom lines and next row's toplines if necessary
1886 for (col_type c = 0; c < ncols; ++c) {
1888 bottomline[c] = bottomline[c] || topline[c];
1893 // do nothing if empty, OR incomplete row line with a topline in next row
1894 if (nset == 0 || (nextrowset && nset != ncols))
1897 if (nset == ncols) {
1899 os << (lastrow ? "\\bottomrule" : "\\midrule");
1903 for (col_type c = 0; c < ncols; ++c) {
1904 if (bottomline[c]) {
1905 //babel makes the "-" character an active one, so we have to suppress this here
1906 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1907 if (lang == "slovak" || lang == "czech")
1908 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1909 "\\expandafter\\cline\\expandafter{\\expandafter")
1910 << c + 1 << "\\string-";
1912 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1913 // get to last column of line span
1914 while (c < ncols && bottomline[c])
1925 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1928 row_type const r = cellRow(cell);
1929 if (is_long_tabular && row_info[r].caption)
1932 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1933 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1934 // figure out how to set the lines
1935 // we always set double lines to the right of the cell
1936 col_type const c = cellColumn(cell);
1937 col_type const nextcol = c + columnSpan(cell);
1938 bool colright = columnRightLine(c);
1939 bool colleft = columnLeftLine(c);
1940 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1941 bool nextcellleft = nextcol < column_info.size()
1942 && leftLine(cellIndex(r, nextcol));
1943 bool coldouble = colright && nextcolleft;
1944 bool celldouble = rightLine(cell) && nextcellleft;
1945 ismulticol = isMultiColumn(cell)
1946 || (c == 0 && colleft != leftLine(cell))
1947 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1948 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1949 || (coldouble != celldouble);
1951 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1952 if (c ==0 && leftLine(cell))
1954 if (!cellInfo(cell).align_special.empty()) {
1955 os << cellInfo(cell).align_special;
1957 if (!getPWidth(cell).zero()) {
1959 case LYX_ALIGN_LEFT:
1960 os << ">{\\raggedright}";
1962 case LYX_ALIGN_RIGHT:
1963 os << ">{\\raggedleft}";
1965 case LYX_ALIGN_CENTER:
1966 os << ">{\\centering}";
1972 case LYX_VALIGN_TOP:
1975 case LYX_VALIGN_MIDDLE:
1978 case LYX_VALIGN_BOTTOM:
1983 << from_ascii(getPWidth(cell).asLatexString())
1987 case LYX_ALIGN_LEFT:
1990 case LYX_ALIGN_RIGHT:
1997 } // end if else !getPWidth
1998 } // end if else !cellinfo_of_cell
1999 if (rightLine(cell) || nextcellleft)
2002 // add extra vertical line if we want a double one
2006 if (getRotateCell(cell)) {
2007 os << "\\begin{sideways}\n";
2010 if (getUsebox(cell) == BOX_PARBOX) {
2013 case LYX_VALIGN_TOP:
2016 case LYX_VALIGN_MIDDLE:
2019 case LYX_VALIGN_BOTTOM:
2023 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2025 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2026 os << "\\begin{minipage}[";
2028 case LYX_VALIGN_TOP:
2031 case LYX_VALIGN_MIDDLE:
2034 case LYX_VALIGN_BOTTOM:
2038 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2046 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2049 row_type const r = cellRow(cell);
2050 if (is_long_tabular && row_info[r].caption)
2054 if (getUsebox(cell) == BOX_PARBOX)
2056 else if (getUsebox(cell) == BOX_MINIPAGE) {
2057 os << "%\n\\end{minipage}";
2060 if (getRotateCell(cell)) {
2061 os << "%\n\\end{sideways}";
2071 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2072 OutputParams const & runparams) const
2074 if (!is_long_tabular)
2078 // output header info
2080 if (endhead.topDL) {
2084 for (row_type i = 0; i < row_info.size(); ++i) {
2085 if (row_info[i].endhead) {
2086 ret += TeXRow(os, i, runparams);
2089 if (endhead.bottomDL) {
2093 os << "\\endhead\n";
2095 if (endfirsthead.empty) {
2096 os << "\\endfirsthead\n";
2100 // output firstheader info
2101 if (haveLTFirstHead()) {
2102 if (endfirsthead.topDL) {
2106 for (row_type i = 0; i < row_info.size(); ++i) {
2107 if (row_info[i].endfirsthead) {
2108 ret += TeXRow(os, i, runparams);
2111 if (endfirsthead.bottomDL) {
2115 os << "\\endfirsthead\n";
2118 // output footer info
2120 if (endfoot.topDL) {
2124 for (row_type i = 0; i < row_info.size(); ++i) {
2125 if (row_info[i].endfoot) {
2126 ret += TeXRow(os, i, runparams);
2129 if (endfoot.bottomDL) {
2133 os << "\\endfoot\n";
2135 if (endlastfoot.empty) {
2136 os << "\\endlastfoot\n";
2140 // output lastfooter info
2141 if (haveLTLastFoot()) {
2142 if (endlastfoot.topDL) {
2146 for (row_type i = 0; i < row_info.size(); ++i) {
2147 if (row_info[i].endlastfoot) {
2148 ret += TeXRow(os, i, runparams);
2151 if (endlastfoot.bottomDL) {
2155 os << "\\endlastfoot\n";
2162 bool Tabular::isValidRow(row_type row) const
2164 if (!is_long_tabular)
2166 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2167 !row_info[row].endfoot && !row_info[row].endlastfoot;
2171 int Tabular::TeXRow(odocstream & os, row_type i,
2172 OutputParams const & runparams) const
2174 idx_type cell = cellIndex(i, 0);
2175 shared_ptr<InsetTableCell> inset = cellInset(cell);
2176 Paragraph const & par = inset->paragraphs().front();
2177 string const lang = par.getParLanguage(buffer().params())->lang();
2179 //output the top line
2180 int ret = TeXTopHLine(os, i, lang);
2182 if (row_info[i].top_space_default) {
2184 os << "\\addlinespace\n";
2186 os << "\\noalign{\\vskip\\doublerulesep}\n";
2188 } else if(!row_info[i].top_space.zero()) {
2190 os << "\\addlinespace["
2191 << from_ascii(row_info[i].top_space.asLatexString())
2194 os << "\\noalign{\\vskip"
2195 << from_ascii(row_info[i].top_space.asLatexString())
2200 bool ismulticol = false;
2201 for (col_type j = 0; j < column_info.size(); ++j) {
2202 if (isPartOfMultiColumn(i, j))
2204 ret += TeXCellPreamble(os, cell, ismulticol);
2205 shared_ptr<InsetTableCell> inset = cellInset(cell);
2207 Paragraph const & par = inset->paragraphs().front();
2208 bool rtl = par.isRTL(buffer().params())
2210 && getPWidth(cell).zero();
2214 par.getParLanguage(buffer().params())->lang();
2215 if (lang == "farsi")
2217 else if (lang == "arabic_arabi")
2219 // currently, remaning RTL languages are
2220 // arabic_arabtex and hebrew
2224 // pass to the OutputParams that we are in a cell and
2225 // which alignment we have set.
2226 // InsetNewline needs this context information.
2227 OutputParams newrp(runparams);
2228 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2229 ? OutputParams::PLAIN
2230 : OutputParams::ALIGNED;
2231 ret += inset->latex(os, newrp);
2232 runparams.encoding = newrp.encoding;
2236 ret += TeXCellPostamble(os, cell, ismulticol);
2237 if (!isLastCellInRow(cell)) { // not last cell in row
2242 os << "\\tabularnewline";
2243 if (row_info[i].bottom_space_default) {
2245 os << "\\addlinespace";
2247 os << "[\\doublerulesep]";
2248 } else if (!row_info[i].bottom_space.zero()) {
2250 os << "\\addlinespace";
2252 << from_ascii(row_info[i].bottom_space.asLatexString())
2258 //output the bottom line
2259 ret += TeXBottomHLine(os, i, lang);
2261 if (row_info[i].interline_space_default) {
2263 os << "\\addlinespace\n";
2265 os << "\\noalign{\\vskip\\doublerulesep}\n";
2267 } else if (!row_info[i].interline_space.zero()) {
2269 os << "\\addlinespace["
2270 << from_ascii(row_info[i].interline_space.asLatexString())
2273 os << "\\noalign{\\vskip"
2274 << from_ascii(row_info[i].interline_space.asLatexString())
2282 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2286 //+---------------------------------------------------------------------
2287 //+ first the opening preamble +
2288 //+---------------------------------------------------------------------
2291 os << "\\begin{sideways}\n";
2294 if (is_long_tabular)
2295 os << "\\begin{longtable}";
2297 os << "\\begin{tabular}";
2299 switch (tabular_valignment) {
2300 case LYX_VALIGN_TOP:
2303 case LYX_VALIGN_BOTTOM:
2306 case LYX_VALIGN_MIDDLE:
2312 for (col_type i = 0; i < column_info.size(); ++i) {
2313 if (columnLeftLine(i))
2315 if (!column_info[i].align_special.empty()) {
2316 os << column_info[i].align_special;
2318 if (!column_info[i].p_width.zero()) {
2319 switch (column_info[i].alignment) {
2320 case LYX_ALIGN_LEFT:
2321 os << ">{\\raggedright}";
2323 case LYX_ALIGN_RIGHT:
2324 os << ">{\\raggedleft}";
2326 case LYX_ALIGN_CENTER:
2327 os << ">{\\centering}";
2329 case LYX_ALIGN_NONE:
2330 case LYX_ALIGN_BLOCK:
2331 case LYX_ALIGN_LAYOUT:
2332 case LYX_ALIGN_SPECIAL:
2336 switch (column_info[i].valignment) {
2337 case LYX_VALIGN_TOP:
2340 case LYX_VALIGN_MIDDLE:
2343 case LYX_VALIGN_BOTTOM:
2348 << from_ascii(column_info[i].p_width.asLatexString())
2351 switch (column_info[i].alignment) {
2352 case LYX_ALIGN_LEFT:
2355 case LYX_ALIGN_RIGHT:
2362 } // end if else !column_info[i].p_width
2363 } // end if else !column_info[i].align_special
2364 if (columnRightLine(i))
2370 ret += TeXLongtableHeaderFooter(os, runparams);
2372 //+---------------------------------------------------------------------
2373 //+ the single row and columns (cells) +
2374 //+---------------------------------------------------------------------
2376 for (row_type i = 0; i < row_info.size(); ++i) {
2377 if (isValidRow(i)) {
2378 ret += TeXRow(os, i, runparams);
2379 if (is_long_tabular && row_info[i].newpage) {
2380 os << "\\newpage\n";
2386 //+---------------------------------------------------------------------
2387 //+ the closing of the tabular +
2388 //+---------------------------------------------------------------------
2390 if (is_long_tabular)
2391 os << "\\end{longtable}";
2393 os << "\\end{tabular}";
2395 os << "\n\\end{sideways}";
2403 int Tabular::docbookRow(odocstream & os, row_type row,
2404 OutputParams const & runparams) const
2407 idx_type cell = getFirstCellInRow(row);
2410 for (col_type j = 0; j < column_info.size(); ++j) {
2411 if (isPartOfMultiColumn(row, j))
2414 os << "<entry align=\"";
2415 switch (getAlignment(cell)) {
2416 case LYX_ALIGN_LEFT:
2419 case LYX_ALIGN_RIGHT:
2427 os << "\" valign=\"";
2428 switch (getVAlignment(cell)) {
2429 case LYX_VALIGN_TOP:
2432 case LYX_VALIGN_BOTTOM:
2435 case LYX_VALIGN_MIDDLE:
2440 if (isMultiColumn(cell)) {
2441 os << " namest=\"col" << j << "\" ";
2442 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2446 ret += cellInset(cell)->docbook(os, runparams);
2455 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2459 //+---------------------------------------------------------------------
2460 //+ first the opening preamble +
2461 //+---------------------------------------------------------------------
2463 os << "<tgroup cols=\"" << column_info.size()
2464 << "\" colsep=\"1\" rowsep=\"1\">\n";
2466 for (col_type i = 0; i < column_info.size(); ++i) {
2467 os << "<colspec colname=\"col" << i << "\" align=\"";
2468 switch (column_info[i].alignment) {
2469 case LYX_ALIGN_LEFT:
2472 case LYX_ALIGN_RIGHT:
2480 if (runparams.flavor == OutputParams::XML)
2486 //+---------------------------------------------------------------------
2487 //+ Long Tabular case +
2488 //+---------------------------------------------------------------------
2490 // output header info
2491 if (haveLTHead() || haveLTFirstHead()) {
2494 for (row_type i = 0; i < row_info.size(); ++i) {
2495 if (row_info[i].endhead || row_info[i].endfirsthead) {
2496 ret += docbookRow(os, i, runparams);
2502 // output footer info
2503 if (haveLTFoot() || haveLTLastFoot()) {
2506 for (row_type i = 0; i < row_info.size(); ++i) {
2507 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2508 ret += docbookRow(os, i, runparams);
2515 //+---------------------------------------------------------------------
2516 //+ the single row and columns (cells) +
2517 //+---------------------------------------------------------------------
2521 for (row_type i = 0; i < row_info.size(); ++i) {
2522 if (isValidRow(i)) {
2523 ret += docbookRow(os, i, runparams);
2528 //+---------------------------------------------------------------------
2529 //+ the closing of the tabular +
2530 //+---------------------------------------------------------------------
2539 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2540 vector<unsigned int> const & clen) const
2542 idx_type const fcell = getFirstCellInRow(row);
2543 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2546 for (idx_type i = fcell; i < n; ++i) {
2556 for (idx_type i = fcell; i < n; ++i) {
2567 col_type column = cellColumn(i);
2568 int len = clen[column];
2569 while (column < column_info.size() - 1
2570 && isPartOfMultiColumn(row, ++column))
2571 len += clen[column] + 4;
2572 os << docstring(len, ch);
2587 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2588 vector<unsigned int> const & clen) const
2590 idx_type const fcell = getFirstCellInRow(row);
2591 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2594 for (idx_type i = fcell; i < n; ++i) {
2595 if (bottomLine(i)) {
2604 for (idx_type i = fcell; i < n; ++i) {
2605 if (bottomLine(i)) {
2615 col_type column = cellColumn(i);
2616 int len = clen[column];
2617 while (column < column_info.size() -1
2618 && isPartOfMultiColumn(row, ++column))
2619 len += clen[column] + 4;
2620 os << docstring(len, ch);
2621 if (bottomLine(i)) {
2635 void Tabular::plaintextPrintCell(odocstream & os,
2636 OutputParams const & runparams,
2637 idx_type cell, row_type row, col_type column,
2638 vector<unsigned int> const & clen,
2639 bool onlydata) const
2641 odocstringstream sstr;
2642 cellInset(cell)->plaintext(sstr, runparams);
2654 unsigned int len1 = sstr.str().length();
2655 unsigned int len2 = clen[column];
2656 while (column < column_info.size() -1
2657 && isPartOfMultiColumn(row, ++column))
2658 len2 += clen[column] + 4;
2661 switch (getAlignment(cell)) {
2663 case LYX_ALIGN_LEFT:
2666 case LYX_ALIGN_RIGHT:
2670 case LYX_ALIGN_CENTER:
2676 os << docstring(len1, ' ') << sstr.str()
2677 << docstring(len2, ' ');
2679 if (rightLine(cell))
2686 void Tabular::plaintext(odocstream & os,
2687 OutputParams const & runparams, int const depth,
2688 bool onlydata, char_type delim) const
2690 // first calculate the width of the single columns
2691 vector<unsigned int> clen(column_info.size());
2694 // first all non (real) multicolumn cells!
2695 for (col_type j = 0; j < column_info.size(); ++j) {
2697 for (row_type i = 0; i < row_info.size(); ++i) {
2698 idx_type cell = cellIndex(i, j);
2699 if (isMultiColumnReal(cell))
2701 odocstringstream sstr;
2702 cellInset(cell)->plaintext(sstr, runparams);
2703 if (clen[j] < sstr.str().length())
2704 clen[j] = sstr.str().length();
2707 // then all (real) multicolumn cells!
2708 for (col_type j = 0; j < column_info.size(); ++j) {
2709 for (row_type i = 0; i < row_info.size(); ++i) {
2710 idx_type cell = cellIndex(i, j);
2711 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2713 odocstringstream sstr;
2714 cellInset(cell)->plaintext(sstr, runparams);
2715 int len = int(sstr.str().length());
2716 idx_type const n = columnSpan(cell);
2717 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2719 if (len > int(clen[j + n - 1]))
2720 clen[j + n - 1] = len;
2725 for (row_type i = 0; i < row_info.size(); ++i) {
2726 if (!onlydata && plaintextTopHLine(os, i, clen))
2727 os << docstring(depth * 2, ' ');
2728 for (col_type j = 0; j < column_info.size(); ++j) {
2729 if (isPartOfMultiColumn(i, j))
2731 if (onlydata && j > 0)
2732 // we don't use operator<< for single UCS4 character.
2733 // see explanation in docstream.h
2735 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2740 os << docstring(depth * 2, ' ');
2741 if (plaintextBottomHLine(os, i, clen))
2742 os << docstring(depth * 2, ' ');
2748 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2750 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2754 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2755 col_type column) const
2757 return cell_info[row][column].inset;
2761 void Tabular::setCellInset(row_type row, col_type column,
2762 shared_ptr<InsetTableCell> ins) const
2764 CellData & cd = cell_info[row][column];
2769 void Tabular::validate(LaTeXFeatures & features) const
2771 features.require("NeedTabularnewline");
2773 features.require("booktabs");
2774 if (is_long_tabular)
2775 features.require("longtable");
2777 features.require("rotating");
2778 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2779 if (getVAlignment(cell) != LYX_VALIGN_TOP
2780 || !getPWidth(cell).zero())
2781 features.require("array");
2782 cellInset(cell)->validate(features);
2787 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2789 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2790 ParagraphList::const_iterator cit = parlist.begin();
2791 ParagraphList::const_iterator end = parlist.end();
2793 for (; cit != end; ++cit)
2794 for (int i = 0; i < cit->size(); ++i)
2795 if (cit->isNewline(i))
2802 /////////////////////////////////////////////////////////////////////
2806 /////////////////////////////////////////////////////////////////////
2808 InsetTableCell::InsetTableCell(Buffer & buf)
2809 : InsetText(buf), isFixedWidth(false),
2810 contentAlign(LYX_ALIGN_CENTER)
2814 bool InsetTableCell::forcePlainLayout(idx_type) const
2816 return !isFixedWidth;
2820 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2822 return isFixedWidth;
2826 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2827 FuncStatus & status) const
2830 switch (cmd.action) {
2832 enabled = !forcePlainLayout();
2834 case LFUN_LAYOUT_PARAGRAPH:
2835 enabled = allowParagraphCustomization();
2838 return InsetText::getStatus(cur, cmd, status);
2840 status.setEnabled(enabled);
2844 docstring InsetTableCell::asString(bool intoInsets)
2847 if (paragraphs().empty())
2849 ParagraphList::const_iterator it = paragraphs().begin();
2850 ParagraphList::const_iterator en = paragraphs().end();
2852 for (; it != en; ++it) {
2857 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2864 /////////////////////////////////////////////////////////////////////
2868 /////////////////////////////////////////////////////////////////////
2870 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2872 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2873 rowselect_(false), colselect_(false)
2875 setBuffer(buf); // FIXME: remove later
2879 InsetTabular::InsetTabular(InsetTabular const & tab)
2880 : Inset(tab), tabular(tab.tabular), scx_(0)
2885 InsetTabular::~InsetTabular()
2887 hideDialogs("tabular", this);
2891 void InsetTabular::setBuffer(Buffer & buf)
2893 tabular.setBuffer(buf);
2894 Inset::setBuffer(buf);
2898 bool InsetTabular::insetAllowed(InsetCode code) const
2900 if (code == MATHMACRO_CODE)
2907 void InsetTabular::write(ostream & os) const
2909 os << "Tabular" << endl;
2914 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2916 // FIXME: depending on the selection state, we could offer a different menu.
2917 return from_ascii("context-tabular");
2921 void InsetTabular::read(Lexer & lex)
2923 //bool const old_format = (lex.getString() == "\\LyXTable");
2931 string token = lex.getString();
2932 while (lex && token != "\\end_inset") {
2934 token = lex.getString();
2937 lex.printError("Missing \\end_inset at this point. ");
2941 int InsetTabular::rowFromY(Cursor & cur, int y) const
2943 // top y coordinate of tabular
2944 int h = yo(cur.bv()) - tabular.rowAscent(0);
2945 size_t nrows = tabular.row_info.size();
2947 for (; r < nrows && y > h; ++r) {
2948 h += tabular.rowAscent(r);
2949 h += tabular.rowDescent(r);
2950 h += tabular.getAdditionalHeight(r);
2956 int InsetTabular::columnFromX(Cursor & cur, int x) const
2958 // left x coordinate of tabular
2959 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2960 size_t ncols = tabular.column_info.size();
2962 for (; c < ncols && x > w; ++c)
2963 w += tabular.columnWidth(c);
2968 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2970 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2971 // mi.base.textwidth << "\n";
2974 LASSERT(false, /**/);
2978 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2981 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2982 if (tabular.isPartOfMultiColumn(i, j))
2983 // Multicolumn cell, but not first one
2988 if (tabular.cell_info[i][j].multicolumn ==
2989 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2990 p_width = tabular.cellInfo(cell).p_width;
2992 p_width = tabular.column_info[j].p_width;
2993 if (!p_width.zero())
2994 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2995 tabular.cellInset(cell)->metrics(m, dim);
2996 if (!p_width.zero())
2997 dim.wid = m.base.textwidth;
2998 tabular.setCellWidth(cell, dim.wid);
2999 maxAsc = max(maxAsc, dim.asc);
3000 maxDesc = max(maxDesc, dim.des);
3003 int const top_space = tabular.row_info[i].top_space_default ?
3004 default_line_space :
3005 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3006 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3007 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3008 default_line_space :
3009 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3010 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3012 tabular.updateColumnWidths();
3013 dim.asc = tabular.rowAscent(0);
3014 dim.des = tabular.height() - dim.asc;
3015 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3018 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3021 if (&cur.inset() == this && cur.selection()) {
3022 if (cur.selIsMultiCell()) {
3025 getSelection(cur, rs, re, cs, ce);
3027 if (col >= cs && col <= ce && row >= rs && row <= re)
3030 if (col == tabular.cellColumn(cur.idx())
3031 && row == tabular.cellRow(cur.idx())) {
3032 CursorSlice const & beg = cur.selBegin();
3033 CursorSlice const & end = cur.selEnd();
3035 if ((end.lastpos() > 0 || end.lastpit() > 0)
3036 && end.pos() == end.lastpos() && beg.pos() == 0
3037 && end.pit() == end.lastpit() && beg.pit() == 0)
3045 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3047 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3048 BufferView * bv = pi.base.bv;
3049 Cursor & cur = pi.base.bv->cursor();
3051 // FIXME: As the full backrgound is painted in drawSelection(),
3052 // we have no choice but to do a full repaint for the Text cells.
3053 pi.full_repaint = true;
3055 resetPos(bv->cursor());
3058 x += ADD_TO_TABULAR_WIDTH;
3060 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3061 bool const original_selection_state = pi.selected;
3064 first_visible_cell = Tabular::npos;
3065 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3067 int const a = tabular.rowAscent(i);
3068 int const d = tabular.rowDescent(i);
3069 idx = tabular.cellIndex(i, 0);
3070 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3071 if (tabular.isPartOfMultiColumn(i, j))
3073 if (first_visible_cell == Tabular::npos)
3074 first_visible_cell = idx;
3076 pi.selected |= isCellSelected(cur, i, j);
3077 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3078 // Cache the Inset position.
3079 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3080 if (nx + tabular.columnWidth(idx) < 0
3081 || nx > bv->workWidth()
3083 || y - a > bv->workHeight()) {
3084 pi.pain.setDrawingEnabled(false);
3085 cell(idx)->draw(pi, cx, y);
3086 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3087 pi.pain.setDrawingEnabled(original_drawing_state);
3089 cell(idx)->draw(pi, cx, y);
3090 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
3092 nx += tabular.columnWidth(idx);
3094 pi.selected = original_selection_state;
3097 if (i + 1 < tabular.row_info.size())
3098 y += d + tabular.rowAscent(i + 1) +
3099 tabular.getAdditionalHeight(i + 1);
3104 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3106 Cursor & cur = pi.base.bv->cursor();
3109 x += scx_ + ADD_TO_TABULAR_WIDTH;
3111 // FIXME: it is wrong to completely paint the background
3112 // if we want to do single row painting.
3114 // Paint background of current tabular
3115 int const w = tabular.width();
3116 int const h = tabular.height();
3117 int yy = y - tabular.rowAscent(0);
3118 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3120 if (!cur.selection())
3122 if (&cur.inset() != this)
3127 bool const full_cell_selected = isCellSelected(cur,
3128 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3130 if (cur.selIsMultiCell() || full_cell_selected) {
3131 y -= tabular.rowAscent(0);
3132 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3133 int const a = tabular.rowAscent(j);
3134 int const h = a + tabular.rowDescent(j);
3136 y += tabular.getAdditionalHeight(j);
3137 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3138 if (tabular.isPartOfMultiColumn(j, i))
3140 idx_type const cell =
3141 tabular.cellIndex(j, i);
3142 int const w = tabular.columnWidth(cell);
3143 if (isCellSelected(cur, j, i))
3144 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3151 x += cellXPos(cur.idx());
3152 x += tabular.getBeginningOfTextInCell(cur.idx());
3153 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3158 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3159 row_type row, idx_type cell, bool erased) const
3161 int x2 = x + tabular.columnWidth(cell);
3162 bool on_off = false;
3163 ColorCode col = Color_tabularline;
3164 ColorCode onoffcol = Color_tabularonoffline;
3167 col = Color_deletedtext;
3168 onoffcol = Color_deletedtext;
3171 if (!tabular.topAlreadyDrawn(cell)) {
3172 on_off = !tabular.topLine(cell);
3173 pain.line(x, y - tabular.rowAscent(row),
3174 x2, y - tabular.rowAscent(row),
3175 on_off ? onoffcol : col,
3176 on_off ? Painter::line_onoffdash : Painter::line_solid);
3178 on_off = !tabular.bottomLine(cell);
3179 pain.line(x, y + tabular.rowDescent(row),
3180 x2, y + tabular.rowDescent(row),
3181 on_off ? onoffcol : col,
3182 on_off ? Painter::line_onoffdash : Painter::line_solid);
3183 if (!tabular.leftAlreadyDrawn(cell)) {
3184 on_off = !tabular.leftLine(cell);
3185 pain.line(x, y - tabular.rowAscent(row),
3186 x, y + tabular.rowDescent(row),
3187 on_off ? onoffcol : col,
3188 on_off ? Painter::line_onoffdash : Painter::line_solid);
3190 on_off = !tabular.rightLine(cell);
3191 pain.line(x2 - tabular.getAdditionalWidth(cell),
3192 y - tabular.rowAscent(row),
3193 x2 - tabular.getAdditionalWidth(cell),
3194 y + tabular.rowDescent(row),
3195 on_off ? onoffcol : col,
3196 on_off ? Painter::line_onoffdash : Painter::line_solid);
3200 docstring InsetTabular::editMessage() const
3202 return _("Opened table");
3206 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3208 //lyxerr << "InsetTabular::edit: " << this << endl;
3210 cur.setSelection(false);
3213 if (isRightToLeft(cur))
3214 cur.idx() = tabular.getLastCellInRow(0);
3220 if (isRightToLeft(cur))
3221 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3223 cur.idx() = tabular.numberofcells - 1;
3225 cur.pos() = cur.lastpos(); // FIXME crude guess
3227 // FIXME: this accesses the position cache before it is initialized
3229 //cur.bv().fitCursor();
3233 void InsetTabular::updateLabels(ParIterator const & it)
3235 // In a longtable, tell captions what the current float is
3236 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3237 string const saveflt = cnts.current_float();
3238 if (tabular.is_long_tabular)
3239 cnts.current_float("table");
3241 ParIterator it2 = it;
3243 size_t const end = it2.nargs();
3244 for ( ; it2.idx() < end; it2.top().forwardIdx())
3245 buffer().updateLabels(it2);
3248 if (tabular.is_long_tabular)
3249 cnts.current_float(saveflt);
3253 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3255 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3256 << "\n cur:" << cur);
3257 CursorSlice sl = cur.top();
3258 Cursor & bvcur = cur.bv().cursor();
3260 switch (cmd.action) {
3262 case LFUN_MOUSE_PRESS: {
3263 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3265 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3266 || cmd.x > xo(cur.bv()) + tabular.width()) {
3267 row_type r = rowFromY(cur, cmd.y);
3268 cur.idx() = tabular.getFirstCellInRow(r);
3271 cur.idx() = tabular.getLastCellInRow(r);
3272 cur.pos() = cur.lastpos();
3273 cur.setSelection(true);
3279 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3280 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3281 || cmd.y > y0 + tabular.height()) {
3282 col_type c = columnFromX(cur, cmd.x);
3283 cur.idx() = tabular.cellIndex(0, c);
3286 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3287 cur.pos() = cur.lastpos();
3288 cur.setSelection(true);
3293 // do not reset cursor/selection if we have selected
3294 // some cells (bug 2715).
3295 if (cmd.button() == mouse_button::button3
3296 && &bvcur.selBegin().inset() == this
3297 && bvcur.selIsMultiCell())
3300 // Let InsetTableCell do it
3301 cell(cur.idx())->dispatch(cur, cmd);
3304 case LFUN_MOUSE_MOTION:
3305 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3306 if (cmd.button() == mouse_button::button1) {
3307 // only accept motions to places not deeper nested than the real anchor
3308 if (!bvcur.anchor_.hasPart(cur)) {
3312 // select (additional) row
3314 row_type r = rowFromY(cur, cmd.y);
3315 cur.idx() = tabular.getLastCellInRow(r);
3316 // we need to reset the cursor's pit and pos now, as the old ones
3317 // may no longer be valid.
3320 bvcur.setCursor(cur);
3321 bvcur.setSelection(true);
3324 // select (additional) column
3326 col_type c = columnFromX(cur, cmd.x);
3327 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3328 // we need to reset the cursor's pit and pos now, as the old ones
3329 // may no longer be valid.
3332 bvcur.setCursor(cur);
3333 bvcur.setSelection(true);
3336 // only update if selection changes
3337 if (bvcur.idx() == cur.idx() &&
3338 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3340 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3341 bvcur.setCursor(cur);
3342 bvcur.setSelection(true);
3343 // if this is a multicell selection, we just set the cursor to
3344 // the beginning of the cell's text.
3345 if (bvcur.selIsMultiCell()) {
3346 bvcur.pit() = bvcur.lastpit();
3347 bvcur.pos() = bvcur.lastpos();
3352 case LFUN_MOUSE_RELEASE:
3357 case LFUN_CELL_BACKWARD:
3359 cur.setSelection(false);
3362 case LFUN_CELL_FORWARD:
3364 cur.setSelection(false);
3367 case LFUN_CHAR_FORWARD_SELECT:
3368 case LFUN_CHAR_FORWARD:
3369 case LFUN_CHAR_BACKWARD_SELECT:
3370 case LFUN_CHAR_BACKWARD:
3371 case LFUN_CHAR_RIGHT_SELECT:
3372 case LFUN_CHAR_RIGHT:
3373 case LFUN_CHAR_LEFT_SELECT:
3374 case LFUN_CHAR_LEFT: {
3375 // determine whether we move to next or previous cell, where to enter
3376 // the new cell from, and which command to "finish" (i.e., exit the
3379 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3380 FuncCode finish_lfun;
3382 if (cmd.action == LFUN_CHAR_FORWARD
3383 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3385 finish_lfun = LFUN_FINISHED_FORWARD;
3387 else if (cmd.action == LFUN_CHAR_BACKWARD
3388 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3390 finish_lfun = LFUN_FINISHED_BACKWARD;
3392 // LEFT or RIGHT commands --- the interpretation will depend on the
3393 // table's direction.
3395 bool const right = cmd.action == LFUN_CHAR_RIGHT
3396 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3397 next_cell = isRightToLeft(cur) != right;
3399 if (lyxrc.visual_cursor)
3400 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3402 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3405 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3406 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3407 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3408 cmd.action == LFUN_CHAR_LEFT_SELECT;
3410 // If we have a multicell selection or we're
3411 // not doing some LFUN_*_SELECT thing anyway...
3412 if (!cur.selIsMultiCell() || !select) {
3413 col_type const c = tabular.cellColumn(cur.idx());
3414 row_type const r = tabular.cellRow(cur.idx());
3415 // Are we trying to select the whole cell and is the whole cell
3416 // not yet selected?
3417 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3418 ((next_cell && cur.pit() == cur.lastpit()
3419 && cur.pos() == cur.lastpos())
3420 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3422 // ...try to dispatch to the cell's inset.
3423 cell(cur.idx())->dispatch(cur, cmd);
3425 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3427 // When we already have a selection we want to select the whole cell
3428 // before going to the next cell.
3429 if (select_whole && !empty_cell){
3430 getText(cur.idx())->selectAll(cur);
3435 // FIXME: When we support the selection of an empty cell, remove
3436 // the !empty_cell from this condition. For now we jump to the next
3437 // cell if the current cell is empty.
3438 if (cur.result().dispatched() && !empty_cell)
3442 // move to next/prev cell, as appropriate
3443 // note that we will always do this if we're selecting and we have
3444 // a multicell selection
3445 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3446 << " cell from: " << int(entry_from));
3448 moveNextCell(cur, entry_from);
3450 movePrevCell(cur, entry_from);
3451 // if we're exiting the table, call the appropriate FINISHED lfun
3452 if (sl == cur.top())
3453 cmd = FuncRequest(finish_lfun);
3460 case LFUN_DOWN_SELECT:
3462 if (!(cur.selection() && cur.selIsMultiCell()))
3463 cell(cur.idx())->dispatch(cur, cmd);
3465 cur.dispatched(); // override the cell's decision
3466 if (sl == cur.top()) {
3467 // if our Text didn't do anything to the cursor
3468 // then we try to put the cursor into the cell below
3469 // setting also the right targetX.
3470 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3471 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3472 cur.idx() = tabular.cellBelow(cur.idx());
3474 TextMetrics const & tm =
3475 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3476 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3479 if (sl == cur.top()) {
3480 // we trick it to go to forward after leaving the
3482 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3485 if (cur.selIsMultiCell()) {
3486 cur.pit() = cur.lastpit();
3487 cur.pos() = cur.lastpos();
3492 case LFUN_UP_SELECT:
3494 if (!(cur.selection() && cur.selIsMultiCell()))
3495 cell(cur.idx())->dispatch(cur, cmd);
3496 cur.dispatched(); // override the cell's decision
3497 if (sl == cur.top()) {
3498 // if our Text didn't do anything to the cursor
3499 // then we try to put the cursor into the cell above
3500 // setting also the right targetX.
3501 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3502 if (tabular.cellRow(cur.idx()) != 0) {
3503 cur.idx() = tabular.cellAbove(cur.idx());
3504 cur.pit() = cur.lastpit();
3505 Text const * text = cell(cur.idx())->getText(0);
3506 TextMetrics const & tm = cur.bv().textMetrics(text);
3507 ParagraphMetrics const & pm =
3508 tm.parMetrics(cur.lastpit());
3509 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3512 if (sl == cur.top()) {
3513 cmd = FuncRequest(LFUN_UP);
3516 if (cur.selIsMultiCell()) {
3518 cur.pos() = cur.lastpos();
3523 // case LFUN_SCREEN_DOWN: {
3524 // //if (hasSelection())
3525 // // cur.selection() = false;
3526 // col_type const col = tabular.cellColumn(cur.idx());
3527 // int const t = cur.bv().top_y() + cur.bv().height();
3528 // if (t < yo() + tabular.getHeightOfTabular()) {
3529 // cur.bv().scrollDocView(t);
3530 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3532 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3539 // case LFUN_SCREEN_UP: {
3540 // //if (hasSelection())
3541 // // cur.selection() = false;
3542 // col_type const col = tabular.cellColumn(cur.idx());
3543 // int const t = cur.bv().top_y() + cur.bv().height();
3545 // cur.bv().scrollDocView(t);
3549 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3553 // cur.par() = cur.lastpar();
3554 // cur.pos() = cur.lastpos();
3558 case LFUN_LAYOUT_TABULAR:
3559 cur.bv().showDialog("tabular", params2string(*this), this);
3562 case LFUN_INSET_DIALOG_UPDATE:
3563 cur.bv().updateDialog("tabular", params2string(*this));
3566 case LFUN_TABULAR_FEATURE:
3567 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3571 // insert file functions
3572 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3573 case LFUN_FILE_INSERT_PLAINTEXT: {
3575 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3576 FileName(to_utf8(cmd.argument())));
3579 cur.recordUndoInset(INSERT_UNDO);
3580 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3581 // content has been replaced,
3582 // so cursor might be invalid
3583 cur.pos() = cur.lastpos();
3584 cur.pit() = cur.lastpit();
3585 bvcur.setCursor(cur);
3592 if (cur.selIsMultiCell()) {
3593 if (copySelection(cur)) {
3594 cur.recordUndoInset(DELETE_UNDO);
3599 cell(cur.idx())->dispatch(cur, cmd);
3602 case LFUN_SELF_INSERT:
3603 if (cur.selIsMultiCell()) {
3604 cur.recordUndoInset(DELETE_UNDO);
3607 cell(cur.idx())->dispatch(cur, cmd);
3610 case LFUN_CHAR_DELETE_BACKWARD:
3611 case LFUN_CHAR_DELETE_FORWARD:
3612 if (cur.selIsMultiCell()) {
3613 cur.recordUndoInset(DELETE_UNDO);
3617 cell(cur.idx())->dispatch(cur, cmd);
3621 if (!cur.selection())
3623 if (cur.selIsMultiCell()) {
3627 cell(cur.idx())->dispatch(cur, cmd);
3630 case LFUN_CLIPBOARD_PASTE:
3631 case LFUN_PRIMARY_SELECTION_PASTE: {
3632 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3633 theClipboard().getAsText() :
3634 theSelection().get();
3637 // pass to InsertPlaintextString, but
3638 // only if we have multi-cell content
3639 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3640 cur.recordUndoInset(INSERT_UNDO);
3641 if (insertPlaintextString(cur.bv(), clip, false)) {
3642 // content has been replaced,
3643 // so cursor might be invalid
3644 cur.pos() = cur.lastpos();
3645 cur.pit() = cur.lastpit();
3646 bvcur.setCursor(cur);
3650 // Let the cell handle normal text
3651 cell(cur.idx())->dispatch(cur, cmd);
3656 if (!tabularStackDirty()) {
3657 cell(cur.idx())->dispatch(cur, cmd);
3660 if (theClipboard().isInternal() ||
3661 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3662 cur.recordUndoInset(INSERT_UNDO);
3663 pasteClipboard(cur);
3667 case LFUN_FONT_EMPH:
3668 case LFUN_FONT_BOLD:
3669 case LFUN_FONT_BOLDSYMBOL:
3670 case LFUN_FONT_ROMAN:
3671 case LFUN_FONT_NOUN:
3672 case LFUN_FONT_ITAL:
3673 case LFUN_FONT_FRAK:
3674 case LFUN_FONT_TYPEWRITER:
3675 case LFUN_FONT_SANS:
3676 case LFUN_TEXTSTYLE_APPLY:
3677 case LFUN_TEXTSTYLE_UPDATE:
3678 case LFUN_FONT_SIZE:
3679 case LFUN_FONT_UNDERLINE:
3681 case LFUN_WORD_CAPITALIZE:
3682 case LFUN_WORD_UPCASE:
3683 case LFUN_WORD_LOWCASE:
3684 case LFUN_CHARS_TRANSPOSE:
3685 if (cur.selIsMultiCell()) {
3688 getSelection(cur, rs, re, cs, ce);
3689 Cursor tmpcur = cur;
3690 for (row_type i = rs; i <= re; ++i) {
3691 for (col_type j = cs; j <= ce; ++j) {
3692 // cursor follows cell:
3693 tmpcur.idx() = tabular.cellIndex(i, j);
3694 // select this cell only:
3697 tmpcur.resetAnchor();
3698 tmpcur.pit() = tmpcur.lastpit();
3699 tmpcur.pos() = tmpcur.top().lastpos();
3700 tmpcur.setCursor(tmpcur);
3701 tmpcur.setSelection();
3702 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3707 cell(cur.idx())->dispatch(cur, cmd);
3711 // we try to handle this event in the insets dispatch function.
3712 cell(cur.idx())->dispatch(cur, cmd);
3718 // function sets an object as defined in func_status.h:
3719 // states OK, Unknown, Disabled, On, Off.
3720 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3721 FuncStatus & status) const
3723 switch (cmd.action) {
3724 case LFUN_TABULAR_FEATURE: {
3725 int action = Tabular::LAST_ACTION;
3727 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3728 string const tmp = tabularFeature[i].feature;
3729 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3730 action = tabularFeature[i].action;
3734 if (action == Tabular::LAST_ACTION) {
3736 status.unknown(true);
3740 string const argument
3741 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3743 row_type sel_row_start = 0;
3744 row_type sel_row_end = 0;
3745 col_type sel_col_start = 0;
3746 col_type sel_col_end = 0;
3747 Tabular::ltType dummyltt;
3750 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3753 case Tabular::SET_PWIDTH:
3754 case Tabular::SET_MPWIDTH:
3755 case Tabular::SET_SPECIAL_COLUMN:
3756 case Tabular::SET_SPECIAL_MULTI:
3757 case Tabular::APPEND_ROW:
3758 case Tabular::APPEND_COLUMN:
3759 case Tabular::DELETE_ROW:
3760 case Tabular::DELETE_COLUMN:
3761 case Tabular::COPY_ROW:
3762 case Tabular::COPY_COLUMN:
3763 case Tabular::SET_TOP_SPACE:
3764 case Tabular::SET_BOTTOM_SPACE:
3765 case Tabular::SET_INTERLINE_SPACE:
3769 case Tabular::MULTICOLUMN:
3770 status.setEnabled(sel_row_start == sel_row_end);
3771 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3774 case Tabular::SET_ALL_LINES:
3775 case Tabular::UNSET_ALL_LINES:
3776 case Tabular::SET_BORDER_LINES:
3777 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3780 case Tabular::TOGGLE_LINE_TOP:
3781 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3782 status.setOnOff(tabular.topLine(cur.idx()));
3785 case Tabular::TOGGLE_LINE_BOTTOM:
3786 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3787 status.setOnOff(tabular.bottomLine(cur.idx()));
3790 case Tabular::TOGGLE_LINE_LEFT:
3791 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3792 status.setOnOff(tabular.leftLine(cur.idx()));
3795 case Tabular::TOGGLE_LINE_RIGHT:
3796 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3797 status.setOnOff(tabular.rightLine(cur.idx()));
3800 case Tabular::M_ALIGN_LEFT:
3802 case Tabular::ALIGN_LEFT:
3803 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3806 case Tabular::M_ALIGN_RIGHT:
3808 case Tabular::ALIGN_RIGHT:
3809 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3812 case Tabular::M_ALIGN_CENTER:
3814 case Tabular::ALIGN_CENTER:
3815 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3818 case Tabular::ALIGN_BLOCK:
3819 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3820 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3823 case Tabular::M_VALIGN_TOP:
3825 case Tabular::VALIGN_TOP:
3827 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3830 case Tabular::M_VALIGN_BOTTOM:
3832 case Tabular::VALIGN_BOTTOM:
3834 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3837 case Tabular::M_VALIGN_MIDDLE:
3839 case Tabular::VALIGN_MIDDLE:
3841 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3844 case Tabular::SET_LONGTABULAR:
3845 status.setOnOff(tabular.is_long_tabular);
3848 case Tabular::UNSET_LONGTABULAR:
3849 status.setOnOff(!tabular.is_long_tabular);
3852 case Tabular::TOGGLE_ROTATE_TABULAR:
3853 case Tabular::SET_ROTATE_TABULAR:
3854 status.setOnOff(tabular.rotate);
3857 case Tabular::TABULAR_VALIGN_TOP:
3858 status.setOnOff(tabular.tabular_valignment
3859 == Tabular::LYX_VALIGN_TOP);
3861 case Tabular::TABULAR_VALIGN_MIDDLE:
3862 status.setOnOff(tabular.tabular_valignment
3863 == Tabular::LYX_VALIGN_MIDDLE);
3865 case Tabular::TABULAR_VALIGN_BOTTOM:
3866 status.setOnOff(tabular.tabular_valignment
3867 == Tabular::LYX_VALIGN_BOTTOM);
3870 case Tabular::UNSET_ROTATE_TABULAR:
3871 status.setOnOff(!tabular.rotate);
3874 case Tabular::TOGGLE_ROTATE_CELL:
3875 case Tabular::SET_ROTATE_CELL:
3876 status.setOnOff(!oneCellHasRotationState(false,
3877 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3880 case Tabular::UNSET_ROTATE_CELL:
3881 status.setOnOff(!oneCellHasRotationState(true,
3882 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3885 case Tabular::SET_USEBOX:
3886 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3889 case Tabular::SET_LTFIRSTHEAD:
3890 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3893 case Tabular::UNSET_LTFIRSTHEAD:
3894 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3897 case Tabular::SET_LTHEAD:
3898 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3901 case Tabular::UNSET_LTHEAD:
3902 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3905 case Tabular::SET_LTFOOT:
3906 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3909 case Tabular::UNSET_LTFOOT:
3910 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3913 case Tabular::SET_LTLASTFOOT:
3914 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3917 case Tabular::UNSET_LTLASTFOOT:
3918 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3921 case Tabular::SET_LTNEWPAGE:
3922 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3925 case Tabular::TOGGLE_LTCAPTION:
3926 status.setEnabled(sel_row_start == sel_row_end);
3927 status.setOnOff(tabular.ltCaption(sel_row_start));
3930 case Tabular::SET_BOOKTABS:
3931 status.setOnOff(tabular.use_booktabs);
3934 case Tabular::UNSET_BOOKTABS:
3935 status.setOnOff(!tabular.use_booktabs);
3940 status.setEnabled(false);
3946 // These are only enabled inside tabular
3947 case LFUN_CELL_BACKWARD:
3948 case LFUN_CELL_FORWARD:
3949 status.setEnabled(true);
3952 // disable these with multiple cells selected
3953 case LFUN_INSET_INSERT:
3954 case LFUN_TABULAR_INSERT:
3955 case LFUN_FLEX_INSERT:
3956 case LFUN_FLOAT_INSERT:
3957 case LFUN_FLOAT_WIDE_INSERT:
3958 case LFUN_FOOTNOTE_INSERT:
3959 case LFUN_MARGINALNOTE_INSERT:
3960 case LFUN_MATH_INSERT:
3961 case LFUN_MATH_MODE:
3962 case LFUN_MATH_MUTATE:
3963 case LFUN_MATH_DISPLAY:
3964 case LFUN_NOTE_INSERT:
3965 case LFUN_OPTIONAL_INSERT:
3966 case LFUN_BOX_INSERT:
3967 case LFUN_BRANCH_INSERT:
3968 case LFUN_PHANTOM_INSERT:
3969 case LFUN_WRAP_INSERT:
3970 case LFUN_ERT_INSERT: {
3971 if (cur.selIsMultiCell()) {
3972 status.setEnabled(false);
3975 return cell(cur.idx())->getStatus(cur, cmd, status);
3978 // disable in non-fixed-width cells
3979 case LFUN_NEWLINE_INSERT:
3980 case LFUN_BREAK_PARAGRAPH: {
3981 if (tabular.getPWidth(cur.idx()).zero()) {
3982 status.setEnabled(false);
3985 return cell(cur.idx())->getStatus(cur, cmd, status);
3988 case LFUN_NEWPAGE_INSERT:
3989 status.setEnabled(false);
3993 if (cur.selIsMultiCell()) {
3994 status.setEnabled(false);
3995 status.message(_("You cannot paste into a multicell selection."));
3998 if (tabularStackDirty() && theClipboard().isInternal()) {
3999 status.setEnabled(true);
4002 return cell(cur.idx())->getStatus(cur, cmd, status);
4004 case LFUN_INSET_MODIFY:
4005 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
4006 status.setEnabled(true);
4012 // we try to handle this event in the insets dispatch function.
4013 return cell(cur.idx())->getStatus(cur, cmd, status);
4018 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4020 return tabular.latex(os, runparams);
4024 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4026 os << '\n'; // output table on a new line
4027 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4028 tabular.plaintext(os, runparams, dp, false, 0);
4029 return PLAINTEXT_NEWLINE;
4033 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4038 // FIXME: Why not pass a proper DocIterator here?
4040 // if the table is inside a float it doesn't need the informaltable
4041 // wrapper. Search for it.
4042 for (master = owner(); master; master = master->owner())
4043 if (master->lyxCode() == FLOAT_CODE)
4048 os << "<informaltable>";
4051 ret += tabular.docbook(os, runparams);
4053 os << "</informaltable>";
4060 void InsetTabular::validate(LaTeXFeatures & features) const
4062 tabular.validate(features);
4066 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4068 return tabular.cellInset(idx);
4072 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4074 return tabular.cellInset(idx);
4078 void InsetTabular::cursorPos(BufferView const & bv,
4079 CursorSlice const & sl, bool boundary, int & x, int & y) const
4081 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4083 // y offset correction
4084 int const row = tabular.cellRow(sl.idx());
4085 for (int i = 0; i <= row; ++i) {
4087 y += tabular.rowAscent(i);
4088 y += tabular.getAdditionalHeight(i);
4091 y += tabular.rowDescent(i);
4094 // x offset correction
4095 int const col = tabular.cellColumn(sl.idx());
4096 int idx = tabular.cellIndex(row, 0);
4097 for (int j = 0; j < col; ++j) {
4098 if (tabular.isPartOfMultiColumn(row, j))
4100 x += tabular.columnWidth(idx);
4103 x += tabular.getBeginningOfTextInCell(idx);
4104 x += ADD_TO_TABULAR_WIDTH;
4109 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4113 Inset const & inset = *tabular.cellInset(cell);
4114 Point o = bv.coordCache().getInsets().xy(&inset);
4115 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
4116 int const xend = xbeg + tabular.columnWidth(cell);
4117 row_type const row = tabular.cellRow(cell);
4118 int const ybeg = o.y_ - tabular.rowAscent(row) -
4119 tabular.getAdditionalHeight(row);
4120 int const yend = o.y_ + tabular.rowDescent(row);
4132 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4133 // << " ybeg=" << ybeg << " yend=" << yend
4134 // << " xx=" << xx << " yy=" << yy
4135 // << " dist=" << xx + yy << endl;
4140 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4142 //lyxerr << "InsetTabular::editXY: " << this << endl;
4143 cur.setSelection(false);
4145 cur.idx() = getNearestCell(cur.bv(), x, y);
4147 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4151 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4153 cur.idx() = getNearestCell(cur.bv(), x, y);
4154 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4158 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4160 idx_type idx_min = 0;
4161 int dist_min = numeric_limits<int>::max();
4162 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4163 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4164 int const d = dist(bv, i, x, y);
4175 int InsetTabular::cellXPos(idx_type const cell) const
4179 for (; !tabular.isFirstCellInRow(c); --c)
4182 for (; c < cell; ++c)
4183 lx += tabular.columnWidth(c);
4189 void InsetTabular::resetPos(Cursor & cur) const
4191 BufferView & bv = cur.bv();
4192 int const maxwidth = bv.workWidth();
4194 int const scx_old = scx_;
4195 int const i = cur.find(this);
4200 int const X2 = maxwidth;
4201 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4202 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4203 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4206 scx_ = X1 + 20 - x1;
4208 scx_ = X2 - 20 - x2;
4213 // only update if offset changed
4214 if (scx_ != scx_old)
4215 cur.updateFlags(Update::Force | Update::FitCursor);
4219 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4221 if (isRightToLeft(cur)) {
4222 if (tabular.isFirstCellInRow(cur.idx())) {
4223 row_type const row = tabular.cellRow(cur.idx());
4224 if (row == tabular.row_info.size() - 1)
4226 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4233 if (tabular.isLastCell(cur.idx()))
4238 cur.boundary(false);
4240 if (cur.selIsMultiCell()) {
4241 cur.pit() = cur.lastpit();
4242 cur.pos() = cur.lastpos();
4250 // in visual mode, place cursor at extreme left or right
4252 switch(entry_from) {
4254 case ENTRY_DIRECTION_RIGHT:
4255 cur.posVisToRowExtremity(false /* !left */);
4257 case ENTRY_DIRECTION_LEFT:
4258 cur.posVisToRowExtremity(true /* left */);
4260 case ENTRY_DIRECTION_IGNORE:
4261 // nothing to do in this case
4270 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4272 if (isRightToLeft(cur)) {
4273 if (tabular.isLastCellInRow(cur.idx())) {
4274 row_type const row = tabular.cellRow(cur.idx());
4277 cur.idx() = tabular.getFirstCellInRow(row);
4278 cur.idx() = tabular.cellAbove(cur.idx());
4280 if (tabular.isLastCell(cur.idx()))
4285 if (cur.idx() == 0) // first cell
4290 if (cur.selIsMultiCell()) {
4291 cur.pit() = cur.lastpit();
4292 cur.pos() = cur.lastpos();
4297 cur.pit() = cur.lastpit();
4298 cur.pos() = cur.lastpos();
4300 // in visual mode, place cursor at extreme left or right
4302 switch(entry_from) {
4304 case ENTRY_DIRECTION_RIGHT:
4305 cur.posVisToRowExtremity(false /* !left */);
4307 case ENTRY_DIRECTION_LEFT:
4308 cur.posVisToRowExtremity(true /* left */);
4310 case ENTRY_DIRECTION_IGNORE:
4311 // nothing to do in this case
4320 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4322 Tabular::Feature action = Tabular::LAST_ACTION;
4325 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4326 string const tmp = tabularFeature[i].feature;
4328 if (tmp == what.substr(0, tmp.length())) {
4329 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4330 //tabularFeatures[i].feature.length()))
4331 action = tabularFeature[i].action;
4335 if (action == Tabular::LAST_ACTION)
4339 ltrim(what.substr(tabularFeature[i].feature.length()));
4340 tabularFeatures(cur, action, val);
4345 static void checkLongtableSpecial(Tabular::ltType & ltt,
4346 string const & special, bool & flag)
4348 if (special == "dl_above") {
4351 } else if (special == "dl_below") {
4352 ltt.bottomDL = flag;
4354 } else if (special == "empty") {
4363 bool InsetTabular::oneCellHasRotationState(bool rotated,
4364 row_type row_start, row_type row_end,
4365 col_type col_start, col_type col_end) const {
4367 for (row_type i = row_start; i <= row_end; ++i) {
4368 for (col_type j = col_start; j <= col_end; ++j) {
4369 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4378 void InsetTabular::tabularFeatures(Cursor & cur,
4379 Tabular::Feature feature, string const & value)
4381 col_type sel_col_start;
4382 col_type sel_col_end;
4383 row_type sel_row_start;
4384 row_type sel_row_end;
4385 bool setLines = false;
4386 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4387 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4391 case Tabular::M_ALIGN_LEFT:
4392 case Tabular::ALIGN_LEFT:
4393 setAlign = LYX_ALIGN_LEFT;
4396 case Tabular::M_ALIGN_RIGHT:
4397 case Tabular::ALIGN_RIGHT:
4398 setAlign = LYX_ALIGN_RIGHT;
4401 case Tabular::M_ALIGN_CENTER:
4402 case Tabular::ALIGN_CENTER:
4403 setAlign = LYX_ALIGN_CENTER;
4406 case Tabular::ALIGN_BLOCK:
4407 setAlign = LYX_ALIGN_BLOCK;
4410 case Tabular::M_VALIGN_TOP:
4411 case Tabular::VALIGN_TOP:
4412 setVAlign = Tabular::LYX_VALIGN_TOP;
4415 case Tabular::M_VALIGN_BOTTOM:
4416 case Tabular::VALIGN_BOTTOM:
4417 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4420 case Tabular::M_VALIGN_MIDDLE:
4421 case Tabular::VALIGN_MIDDLE:
4422 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4429 cur.recordUndoInset(ATOMIC_UNDO);
4431 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4432 row_type const row = tabular.cellRow(cur.idx());
4433 col_type const column = tabular.cellColumn(cur.idx());
4435 Tabular::ltType ltt;
4439 case Tabular::SET_PWIDTH: {
4440 Length const len(value);
4441 tabular.setColumnPWidth(cur, cur.idx(), len);
4443 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4444 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4448 case Tabular::SET_MPWIDTH:
4449 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4452 case Tabular::SET_SPECIAL_COLUMN:
4453 case Tabular::SET_SPECIAL_MULTI:
4454 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4457 case Tabular::APPEND_ROW:
4458 // append the row into the tabular
4459 tabular.appendRow(cur.idx());
4462 case Tabular::APPEND_COLUMN:
4463 // append the column into the tabular
4464 tabular.appendColumn(cur.idx());
4465 cur.idx() = tabular.cellIndex(row, column);
4468 case Tabular::DELETE_ROW:
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4470 tabular.deleteRow(sel_row_start);
4471 if (sel_row_start >= tabular.row_info.size())
4473 cur.idx() = tabular.cellIndex(sel_row_start, column);
4476 cur.setSelection(false);
4479 case Tabular::DELETE_COLUMN:
4480 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4481 tabular.deleteColumn(sel_col_start);
4482 if (sel_col_start >= tabular.column_info.size())
4484 cur.idx() = tabular.cellIndex(row, sel_col_start);
4487 cur.setSelection(false);
4490 case Tabular::COPY_ROW:
4491 tabular.copyRow(row);
4494 case Tabular::COPY_COLUMN:
4495 tabular.copyColumn(column);
4496 cur.idx() = tabular.cellIndex(row, column);
4499 case Tabular::TOGGLE_LINE_TOP: {
4500 bool lineSet = !tabular.topLine(cur.idx());
4501 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4502 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4503 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4507 case Tabular::TOGGLE_LINE_BOTTOM: {
4508 bool lineSet = !tabular.bottomLine(cur.idx());
4509 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4510 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4511 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4515 case Tabular::TOGGLE_LINE_LEFT: {
4516 bool lineSet = !tabular.leftLine(cur.idx());
4517 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4518 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4519 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4523 case Tabular::TOGGLE_LINE_RIGHT: {
4524 bool lineSet = !tabular.rightLine(cur.idx());
4525 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4526 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4527 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4531 case Tabular::M_ALIGN_LEFT:
4532 case Tabular::M_ALIGN_RIGHT:
4533 case Tabular::M_ALIGN_CENTER:
4535 case Tabular::ALIGN_LEFT:
4536 case Tabular::ALIGN_RIGHT:
4537 case Tabular::ALIGN_CENTER:
4538 case Tabular::ALIGN_BLOCK:
4539 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4540 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4541 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4544 case Tabular::M_VALIGN_TOP:
4545 case Tabular::M_VALIGN_BOTTOM:
4546 case Tabular::M_VALIGN_MIDDLE:
4548 case Tabular::VALIGN_TOP:
4549 case Tabular::VALIGN_BOTTOM:
4550 case Tabular::VALIGN_MIDDLE:
4551 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4552 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4553 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4556 case Tabular::MULTICOLUMN: {
4557 if (!cur.selection()) {
4558 // just multicol for one single cell
4559 // check whether we are completely in a multicol
4560 if (tabular.isMultiColumn(cur.idx()))
4561 tabular.unsetMultiColumn(cur.idx());
4563 tabular.setMultiColumn(cur.idx(), 1);
4566 // we have a selection so this means we just add all this
4567 // cells to form a multicolumn cell
4568 idx_type const s_start = cur.selBegin().idx();
4569 idx_type const s_end = cur.selEnd().idx();
4570 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4571 cur.idx() = s_start;
4574 cur.setSelection(false);
4578 case Tabular::SET_ALL_LINES:
4580 case Tabular::UNSET_ALL_LINES:
4581 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4582 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4583 tabular.setAllLines(
4584 tabular.cellIndex(i,j), setLines);
4587 case Tabular::SET_BORDER_LINES:
4588 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4589 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4590 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4592 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4593 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4594 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4598 case Tabular::SET_LONGTABULAR:
4599 tabular.is_long_tabular = true;
4602 case Tabular::UNSET_LONGTABULAR:
4603 tabular.is_long_tabular = false;
4606 case Tabular::SET_ROTATE_TABULAR:
4607 tabular.rotate = true;
4610 case Tabular::UNSET_ROTATE_TABULAR:
4611 tabular.rotate = false;
4614 case Tabular::TOGGLE_ROTATE_TABULAR:
4615 tabular.rotate = !tabular.rotate;
4618 case Tabular::TABULAR_VALIGN_TOP:
4619 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
4622 case Tabular::TABULAR_VALIGN_MIDDLE:
4623 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
4626 case Tabular::TABULAR_VALIGN_BOTTOM:
4627 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
4630 case Tabular::SET_ROTATE_CELL:
4631 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4632 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4633 tabular.setRotateCell(
4634 tabular.cellIndex(i, j), true);
4637 case Tabular::UNSET_ROTATE_CELL:
4638 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4639 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4640 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4643 case Tabular::TOGGLE_ROTATE_CELL:
4645 bool oneNotRotated = oneCellHasRotationState(false,
4646 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4648 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4649 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4650 tabular.setRotateCell(tabular.cellIndex(i, j),
4655 case Tabular::SET_USEBOX: {
4656 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4657 if (val == tabular.getUsebox(cur.idx()))
4658 val = Tabular::BOX_NONE;
4659 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4660 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4661 tabular.setUsebox(tabular.cellIndex(i, j), val);
4665 case Tabular::UNSET_LTFIRSTHEAD:
4667 case Tabular::SET_LTFIRSTHEAD:
4668 tabular.getRowOfLTFirstHead(row, ltt);
4669 checkLongtableSpecial(ltt, value, flag);
4670 tabular.setLTHead(row, flag, ltt, true);
4673 case Tabular::UNSET_LTHEAD:
4675 case Tabular::SET_LTHEAD:
4676 tabular.getRowOfLTHead(row, ltt);
4677 checkLongtableSpecial(ltt, value, flag);
4678 tabular.setLTHead(row, flag, ltt, false);
4681 case Tabular::UNSET_LTFOOT:
4683 case Tabular::SET_LTFOOT:
4684 tabular.getRowOfLTFoot(row, ltt);
4685 checkLongtableSpecial(ltt, value, flag);
4686 tabular.setLTFoot(row, flag, ltt, false);
4689 case Tabular::UNSET_LTLASTFOOT:
4691 case Tabular::SET_LTLASTFOOT:
4692 tabular.getRowOfLTLastFoot(row, ltt);
4693 checkLongtableSpecial(ltt, value, flag);
4694 tabular.setLTFoot(row, flag, ltt, true);
4697 case Tabular::SET_LTNEWPAGE:
4698 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4701 case Tabular::TOGGLE_LTCAPTION:
4702 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4705 cur.setSelection(false);
4708 case Tabular::SET_BOOKTABS:
4709 tabular.use_booktabs = true;
4712 case Tabular::UNSET_BOOKTABS:
4713 tabular.use_booktabs = false;
4716 case Tabular::SET_TOP_SPACE: {
4718 if (value == "default")
4719 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4720 tabular.row_info[i].top_space_default = true;
4721 else if (isValidLength(value, &len))
4722 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4723 tabular.row_info[i].top_space_default = false;
4724 tabular.row_info[i].top_space = len;
4727 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4728 tabular.row_info[i].top_space_default = false;
4729 tabular.row_info[i].top_space = len;
4734 case Tabular::SET_BOTTOM_SPACE: {
4736 if (value == "default")
4737 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4738 tabular.row_info[i].bottom_space_default = true;
4739 else if (isValidLength(value, &len))
4740 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4741 tabular.row_info[i].bottom_space_default = false;
4742 tabular.row_info[i].bottom_space = len;
4745 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4746 tabular.row_info[i].bottom_space_default = false;
4747 tabular.row_info[i].bottom_space = len;
4752 case Tabular::SET_INTERLINE_SPACE: {
4754 if (value == "default")
4755 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4756 tabular.row_info[i].interline_space_default = true;
4757 else if (isValidLength(value, &len))
4758 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4759 tabular.row_info[i].interline_space_default = false;
4760 tabular.row_info[i].interline_space = len;
4763 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4764 tabular.row_info[i].interline_space_default = false;
4765 tabular.row_info[i].interline_space = len;
4770 // dummy stuff just to avoid warnings
4771 case Tabular::LAST_ACTION:
4777 bool InsetTabular::showInsetDialog(BufferView * bv) const
4779 bv->showDialog("tabular", params2string(*this),
4780 const_cast<InsetTabular *>(this));
4785 void InsetTabular::openLayoutDialog(BufferView * bv) const
4787 bv->showDialog("tabular", params2string(*this),
4788 const_cast<InsetTabular *>(this));
4792 bool InsetTabular::copySelection(Cursor & cur)
4794 if (!cur.selection())
4799 getSelection(cur, rs, re, cs, ce);
4801 paste_tabular.reset(new Tabular(tabular));
4803 for (row_type i = 0; i < rs; ++i)
4804 paste_tabular->deleteRow(0);
4806 row_type const rows = re - rs + 1;
4807 while (paste_tabular->row_info.size() > rows)
4808 paste_tabular->deleteRow(rows);
4810 for (col_type i = 0; i < cs; ++i)
4811 paste_tabular->deleteColumn(0);
4813 col_type const columns = ce - cs + 1;
4814 while (paste_tabular->column_info.size() > columns)
4815 paste_tabular->deleteColumn(columns);
4817 paste_tabular->setBuffer(tabular.buffer());
4819 odocstringstream os;
4820 OutputParams const runparams(0);
4821 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4822 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4823 cap::copySelection(cur, os.str());
4825 // mark tabular stack dirty
4826 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4827 // when we (hopefully) have a one-for-all paste mechanism.
4828 // This must be called after cap::copySelection.
4829 dirtyTabularStack(true);
4835 bool InsetTabular::pasteClipboard(Cursor & cur)
4839 col_type const actcol = tabular.cellColumn(cur.idx());
4840 row_type const actrow = tabular.cellRow(cur.idx());
4841 for (row_type r1 = 0, r2 = actrow;
4842 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4844 for (col_type c1 = 0, c2 = actcol;
4845 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4847 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4848 tabular.isPartOfMultiColumn(r2, c2))
4850 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4854 if (tabular.isPartOfMultiColumn(r2, c2)) {
4858 shared_ptr<InsetTableCell> inset(
4859 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4860 tabular.setCellInset(r2, c2, inset);
4861 // FIXME: change tracking (MG)
4862 inset->setChange(Change(buffer().params().trackChanges ?
4863 Change::INSERTED : Change::UNCHANGED));
4871 void InsetTabular::cutSelection(Cursor & cur)
4873 if (!cur.selection())
4878 getSelection(cur, rs, re, cs, ce);
4879 for (row_type i = rs; i <= re; ++i) {
4880 for (col_type j = cs; j <= ce; ++j) {
4881 shared_ptr<InsetTableCell> t
4882 = cell(tabular.cellIndex(i, j));
4883 if (buffer().params().trackChanges)
4884 // FIXME: Change tracking (MG)
4885 t->setChange(Change(Change::DELETED));
4891 // cursor position might be invalid now
4892 if (cur.pit() > cur.lastpit())
4893 cur.pit() = cur.lastpit();
4894 if (cur.pos() > cur.lastpos())
4895 cur.pos() = cur.lastpos();
4896 cur.clearSelection();
4900 bool InsetTabular::isRightToLeft(Cursor & cur) const
4902 LASSERT(cur.depth() > 1, /**/);
4903 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4904 pos_type const parentpos = cur[cur.depth() - 2].pos();
4905 return parentpar.getFontSettings(buffer().params(),
4906 parentpos).language()->rightToLeft();
4909 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4912 LASSERT(stidx <= enidx, return docstring());
4914 col_type const col1 = tabular.cellColumn(stidx);
4915 col_type const col2 = tabular.cellColumn(enidx);
4916 row_type const row1 = tabular.cellRow(stidx);
4917 row_type const row2 = tabular.cellRow(enidx);
4919 for (col_type col = col1; col <= col2; col++)
4920 for (row_type row = row1; row <= row2; row++) {
4925 retval += tabular.cellInset(row, col)->asString(intoInsets);
4931 void InsetTabular::getSelection(Cursor & cur,
4932 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4934 CursorSlice const & beg = cur.selBegin();
4935 CursorSlice const & end = cur.selEnd();
4936 cs = tabular.cellColumn(beg.idx());
4937 ce = tabular.cellColumn(end.idx());
4940 cs = tabular.cellColumn(end.idx());
4942 ce = tabular.cellRightColumn(end.idx());
4945 rs = tabular.cellRow(beg.idx());
4946 re = tabular.cellRow(end.idx());
4952 Text * InsetTabular::getText(int idx) const
4954 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4958 void InsetTabular::setChange(Change const & change)
4960 for (idx_type idx = 0; idx < nargs(); ++idx)
4961 cell(idx)->setChange(change);
4965 void InsetTabular::acceptChanges(BufferParams const & bparams)
4967 for (idx_type idx = 0; idx < nargs(); ++idx)
4968 cell(idx)->acceptChanges(bparams);
4972 void InsetTabular::rejectChanges(BufferParams const & bparams)
4974 for (idx_type idx = 0; idx < nargs(); ++idx)
4975 cell(idx)->rejectChanges(bparams);
4979 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4981 return tabular.getPWidth(cell).zero();
4985 bool InsetTabular::forcePlainLayout(idx_type cell) const
4987 return !tabular.getPWidth(cell).zero();
4991 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4994 if (buf.length() <= 0)
4999 col_type maxCols = 1;
5000 size_t const len = buf.length();
5004 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5012 maxCols = max(cols, maxCols);
5018 maxCols = max(cols, maxCols);
5024 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
5025 loctab = paste_tabular.get();
5027 dirtyTabularStack(true);
5030 cell = bv.cursor().idx();
5031 ocol = tabular.cellColumn(cell);
5032 row = tabular.cellRow(cell);
5036 idx_type const cells = loctab->numberofcells;
5039 rows = loctab->row_info.size();
5040 col_type const columns = loctab->column_info.size();
5042 while (cell < cells && p < len && row < rows &&
5043 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5049 // we can only set this if we are not too far right
5050 if (cols < columns) {
5051 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5052 Font const font = bv.textMetrics(&inset->text()).
5054 inset->setText(buf.substr(op, p - op), font,
5055 buffer().params().trackChanges);
5061 // we can only set this if we are not too far right
5062 if (cols < columns) {
5063 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5064 Font const font = bv.textMetrics(&inset->text()).
5066 inset->setText(buf.substr(op, p - op), font,
5067 buffer().params().trackChanges);
5072 cell = loctab->cellIndex(row, cols);
5078 // check for the last cell if there is no trailing '\n'
5079 if (cell < cells && op < len) {
5080 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5081 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5082 inset->setText(buf.substr(op, len - op), font,
5083 buffer().params().trackChanges);
5089 void InsetTabular::addPreview(PreviewLoader & loader) const
5091 row_type const rows = tabular.row_info.size();
5092 col_type const columns = tabular.column_info.size();
5093 for (row_type i = 0; i < rows; ++i) {
5094 for (col_type j = 0; j < columns; ++j)
5095 tabular.cellInset(i, j)->addPreview(loader);
5100 bool InsetTabular::completionSupported(Cursor const & cur) const
5102 Cursor const & bvCur = cur.bv().cursor();
5103 if (&bvCur.inset() != this)
5105 return cur.text()->completionSupported(cur);
5109 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5111 return completionSupported(cur);
5115 bool InsetTabular::automaticInlineCompletion() const
5117 return lyxrc.completion_inline_text;
5121 bool InsetTabular::automaticPopupCompletion() const
5123 return lyxrc.completion_popup_text;
5127 bool InsetTabular::showCompletionCursor() const
5129 return lyxrc.completion_cursor_text;
5133 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5135 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5139 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5141 if (!completionSupported(cur))
5143 return cur.text()->completionPrefix(cur);
5147 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5149 if (!completionSupported(cur))
5152 return cur.text()->insertCompletion(cur, s, finished);
5156 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5157 Dimension & dim) const
5159 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5160 tm.completionPosAndDim(cur, x, y, dim);
5164 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5166 istringstream data(in);
5168 lex.setStream(data);
5175 if (!lex || token != "tabular") {
5176 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5180 // This is part of the inset proper that is usually swallowed
5181 // by Buffer::readInset
5183 if (!lex || token != "Tabular") {
5184 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5192 string InsetTabular::params2string(InsetTabular const & inset)
5195 data << "tabular" << ' ';
5197 data << "\\end_inset\n";