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
16 * Full author contact details are available in file CREDITS.
21 #include "InsetTabular.h"
23 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "CoordCache.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "FuncRequest.h"
33 #include "FuncStatus.h"
35 #include "LaTeXFeatures.h"
39 #include "MetricsInfo.h"
40 #include "OutputParams.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::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left" },
167 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center" },
168 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right" },
169 { Tabular::LAST_ACTION, "" }
173 class FeatureEqual : public unary_function<TabularFeature, bool> {
175 FeatureEqual(Tabular::Feature feature)
176 : feature_(feature) {}
177 bool operator()(TabularFeature const & tf) const {
178 return tf.action == feature_;
181 Tabular::Feature feature_;
186 string const write_attribute(string const & name, T const & t)
188 string const s = tostr(t);
189 return s.empty() ? s : " " + name + "=\"" + s + "\"";
193 string const write_attribute(string const & name, string const & t)
195 return t.empty() ? t : " " + name + "=\"" + t + "\"";
200 string const write_attribute(string const & name, docstring const & t)
202 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
207 string const write_attribute(string const & name, bool const & b)
209 // we write only true attribute values so we remove a bit of the
210 // file format bloat for tabulars.
211 return b ? write_attribute(name, convert<string>(b)) : string();
216 string const write_attribute(string const & name, int const & i)
218 // we write only true attribute values so we remove a bit of the
219 // file format bloat for tabulars.
220 return i ? write_attribute(name, convert<string>(i)) : string();
225 string const write_attribute(string const & name, Tabular::idx_type const & i)
227 // we write only true attribute values so we remove a bit of the
228 // file format bloat for tabulars.
229 return i ? write_attribute(name, convert<string>(i)) : string();
234 string const write_attribute(string const & name, Length const & value)
236 // we write only the value if we really have one same reson as above.
237 return value.zero() ? string() : write_attribute(name, value.asString());
241 string const tostr(LyXAlignment const & num)
246 case LYX_ALIGN_BLOCK:
250 case LYX_ALIGN_CENTER:
252 case LYX_ALIGN_RIGHT:
254 case LYX_ALIGN_LAYOUT:
256 case LYX_ALIGN_SPECIAL:
263 string const tostr(Tabular::HAlignment const & num)
266 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
268 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
270 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
277 string const tostr(Tabular::VAlignment const & num)
280 case Tabular::LYX_VALIGN_TOP:
282 case Tabular::LYX_VALIGN_MIDDLE:
284 case Tabular::LYX_VALIGN_BOTTOM:
291 string const tostr(Tabular::BoxType const & num)
294 case Tabular::BOX_NONE:
296 case Tabular::BOX_PARBOX:
298 case Tabular::BOX_MINIPAGE:
305 // I would have liked a fromstr template a lot better. (Lgb)
306 bool string2type(string const str, LyXAlignment & num)
309 num = LYX_ALIGN_NONE;
310 else if (str == "block")
311 num = LYX_ALIGN_BLOCK;
312 else if (str == "left")
313 num = LYX_ALIGN_LEFT;
314 else if (str == "center")
315 num = LYX_ALIGN_CENTER;
316 else if (str == "right")
317 num = LYX_ALIGN_RIGHT;
324 bool string2type(string const str, Tabular::HAlignment & num)
327 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
328 else if (str == "center" )
329 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
330 else if (str == "right")
331 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
338 bool string2type(string const str, Tabular::VAlignment & num)
341 num = Tabular::LYX_VALIGN_TOP;
342 else if (str == "middle" )
343 num = Tabular::LYX_VALIGN_MIDDLE;
344 else if (str == "bottom")
345 num = Tabular::LYX_VALIGN_BOTTOM;
352 bool string2type(string const str, Tabular::BoxType & num)
355 num = Tabular::BOX_NONE;
356 else if (str == "parbox")
357 num = Tabular::BOX_PARBOX;
358 else if (str == "minipage")
359 num = Tabular::BOX_MINIPAGE;
366 bool string2type(string const str, bool & num)
370 else if (str == "false")
378 bool getTokenValue(string const & str, char const * token, string & ret)
381 size_t token_length = strlen(token);
382 size_t pos = str.find(token);
384 if (pos == string::npos || pos + token_length + 1 >= str.length()
385 || str[pos + token_length] != '=')
387 pos += token_length + 1;
389 if (ch != '"' && ch != '\'') { // only read till next space
393 while (pos < str.length() - 1 && str[++pos] != ch)
400 bool getTokenValue(string const & str, char const * token, docstring & ret)
403 bool const success = getTokenValue(str, token, tmp);
404 ret = from_utf8(tmp);
409 bool getTokenValue(string const & str, char const * token, int & num)
413 if (!getTokenValue(str, token, tmp))
415 num = convert<int>(tmp);
420 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
423 return getTokenValue(str, token, tmp) && string2type(tmp, num);
427 bool getTokenValue(string const & str, char const * token,
428 Tabular::HAlignment & num)
431 return getTokenValue(str, token, tmp) && string2type(tmp, num);
435 bool getTokenValue(string const & str, char const * token,
436 Tabular::VAlignment & num)
439 return getTokenValue(str, token, tmp) && string2type(tmp, num);
443 bool getTokenValue(string const & str, char const * token,
444 Tabular::BoxType & num)
447 return getTokenValue(str, token, tmp) && string2type(tmp, num);
451 bool getTokenValue(string const & str, char const * token, bool & flag)
453 // set the flag always to false as this should be the default for bools
454 // not in the file-format.
457 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
461 bool getTokenValue(string const & str, char const * token, Length & len)
463 // set the length to be zero() as default as this it should be if not
464 // in the file format.
467 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
471 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
476 if (!getTokenValue(str, token, tmp))
478 if (tmp == "default") {
482 return isValidLength(tmp, &len);
486 void l_getline(istream & is, string & str)
489 while (str.empty()) {
491 if (!str.empty() && str[str.length() - 1] == '\r')
492 str.erase(str.length() - 1);
499 string const featureAsString(Tabular::Feature feature)
501 TabularFeature * end = tabularFeature +
502 sizeof(tabularFeature) / sizeof(TabularFeature);
503 TabularFeature * it = find_if(tabularFeature, end,
504 FeatureEqual(feature));
505 return (it == end) ? string() : it->feature;
510 /////////////////////////////////////////////////////////////////////
514 /////////////////////////////////////////////////////////////////////
517 Tabular::CellData::CellData(Buffer * buf)
520 multicolumn(Tabular::CELL_NORMAL),
521 alignment(LYX_ALIGN_CENTER),
522 valignment(LYX_VALIGN_TOP),
529 inset(new InsetTableCell(buf))
531 inset->setBuffer(*buf);
535 Tabular::CellData::CellData(CellData const & cs)
538 multicolumn(cs.multicolumn),
539 alignment(cs.alignment),
540 valignment(cs.valignment),
541 top_line(cs.top_line),
542 bottom_line(cs.bottom_line),
543 left_line(cs.left_line),
544 right_line(cs.right_line),
547 align_special(cs.align_special),
549 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
553 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
559 void Tabular::CellData::swap(CellData & rhs)
561 std::swap(cellno, rhs.cellno);
562 std::swap(width, rhs.width);
563 std::swap(multicolumn, rhs.multicolumn);
564 std::swap(alignment, rhs.alignment);
565 std::swap(valignment, rhs.valignment);
566 std::swap(top_line, rhs.top_line);
567 std::swap(bottom_line, rhs.bottom_line);
568 std::swap(left_line, rhs.left_line);
569 std::swap(right_line, rhs.right_line);
570 std::swap(usebox, rhs.usebox);
571 std::swap(rotate, rhs.rotate);
572 std::swap(align_special, rhs.align_special);
573 p_width.swap(rhs.p_width);
574 inset.swap(rhs.inset);
578 Tabular::RowData::RowData()
581 top_space_default(false),
582 bottom_space_default(false),
583 interline_space_default(false),
593 Tabular::ColumnData::ColumnData()
594 : alignment(LYX_ALIGN_CENTER),
595 valignment(LYX_VALIGN_TOP),
601 Tabular::ltType::ltType()
608 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
610 init(buffer, rows_arg, columns_arg);
614 void Tabular::setBuffer(Buffer & buffer)
617 size_t row_count = row_info.size();
618 size_t column_count = column_info.size();
619 // set silly default lines
620 for (row_type i = 0; i < row_count; ++i)
621 for (col_type j = 0; j < column_count; ++j)
622 cell_info[i][j].inset->setBuffer(*buffer_);
626 // activates all lines and sets all widths to 0
627 void Tabular::init(Buffer * buf, row_type rows_arg,
628 col_type columns_arg)
631 row_info = row_vector(rows_arg);
632 column_info = column_vector(columns_arg);
633 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
634 row_info.reserve(10);
635 column_info.reserve(10);
636 cell_info.reserve(100);
638 is_long_tabular = false;
639 tabular_valignment = LYX_VALIGN_MIDDLE;
640 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
642 use_booktabs = false;
643 size_t row_count = row_info.size();
644 size_t column_count = column_info.size();
645 // set silly default lines
646 for (row_type i = 0; i < row_count; ++i)
647 for (col_type j = 0; j < column_count; ++j) {
648 cell_info[i][j].inset->setBuffer(*buffer_);
649 cell_info[i][j].top_line = true;
650 cell_info[i][j].left_line = true;
651 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
652 cell_info[i][j].right_line = j == column_count - 1;
657 void Tabular::appendRow(idx_type const cell)
659 BufferParams const & bp = buffer_->params();
660 row_type const row = cellRow(cell);
662 row_vector::iterator rit = row_info.begin() + row;
663 row_info.insert(rit, RowData());
664 // now set the values of the row before
665 row_info[row] = row_info[row + 1];
667 row_type const nrows = row_info.size();
668 col_type const ncols = column_info.size();
670 cell_vvector old(nrows - 1);
671 for (row_type i = 0; i < nrows - 1; ++i)
672 swap(cell_info[i], old[i]);
674 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer_)));
676 for (row_type i = 0; i <= row; ++i)
677 swap(cell_info[i], old[i]);
678 for (row_type i = row + 2; i < nrows; ++i)
679 swap(cell_info[i], old[i - 1]);
682 for (col_type c = 0; c < ncols; ++c) {
683 // inherit line settings
684 idx_type const i = cellIndex(row + 1, c);
685 idx_type const j = cellIndex(row, c);
686 setLeftLine(i, leftLine(j));
687 setRightLine(i, rightLine(j));
688 setTopLine(i, topLine(j));
689 if (topLine(j) && bottomLine(j)) {
690 setBottomLine(i, true);
691 setBottomLine(j, false);
693 // mark track changes
695 cellInfo(i).inset->setChange(Change(Change::INSERTED));
700 void Tabular::deleteRow(row_type const row)
702 // Not allowed to delete last row
703 if (row_info.size() == 1)
706 row_info.erase(row_info.begin() + row);
707 cell_info.erase(cell_info.begin() + row);
712 void Tabular::copyRow(row_type const row)
714 row_info.insert(row_info.begin() + row, row_info[row]);
715 cell_info.insert(cell_info.begin() + row, cell_info[row]);
717 if (buffer().params().trackChanges)
718 for (col_type j = 0; j < column_info.size(); ++j)
719 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
725 void Tabular::appendColumn(idx_type const cell)
727 col_type const c = cellColumn(cell);
728 column_vector::iterator cit = column_info.begin() + c + 1;
729 column_info.insert(cit, ColumnData());
730 row_type const nrows = row_info.size();
731 // set the column values of the column before
732 column_info[c + 1] = column_info[c];
734 for (row_type r = 0; r < nrows; ++r) {
735 cell_info[r].insert(cell_info[r].begin() + c + 1,
738 // FIXME: This code does not work. It deletes the cell's content and
739 // it triggers an assertion if the cursor is at pos > 0.
740 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
741 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
743 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
747 for (row_type r = 0; r < nrows; ++r) {
748 // inherit line settings
749 idx_type const i = cellIndex(r, c + 1);
750 idx_type const j = cellIndex(r, c);
751 setBottomLine(i, bottomLine(j));
752 setTopLine(i, topLine(j));
753 setLeftLine(i, leftLine(j));
754 if (rightLine(j) && rightLine(j)) {
755 setRightLine(i, true);
756 setRightLine(j, false);
759 cellInfo(i).inset->clear();
760 if (buffer().params().trackChanges)
761 cellInfo(i).inset->setChange(Change(Change::INSERTED));
766 void Tabular::deleteColumn(col_type const column)
768 // Not allowed to delete last column
769 if (column_info.size() == 1)
772 size_t const row_count = row_info.size();
773 for (row_type i = 0; i < row_count; ++i) {
774 // Care about multicolumn cells
775 if (column + 1 < column_info.size() &&
776 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
777 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
778 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
780 cell_info[i].erase(cell_info[i].begin() + column);
782 column_info.erase(column_info.begin() + column);
787 void Tabular::copyColumn(col_type const column)
789 BufferParams const & bp = buffer().params();
790 column_info.insert(column_info.begin() + column, column_info[column]);
792 size_t row_count = row_info.size();
793 for (row_type i = 0; i < row_count; ++i)
794 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
797 for (row_type i = 0; i < row_count; ++i)
798 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
803 void Tabular::updateIndexes()
806 col_type ncols = column_info.size();
807 row_type nrows = row_info.size();
809 for (row_type row = 0; row < nrows; ++row)
810 for (col_type column = 0; column < ncols; ++column) {
811 if (!isPartOfMultiColumn(row, column))
813 cell_info[row][column].cellno = numberofcells - 1;
816 rowofcell.resize(numberofcells);
817 columnofcell.resize(numberofcells);
819 for (row_type row = 0; row < nrows; ++row)
820 for (col_type column = 0; column < ncols; ++column) {
821 if (isPartOfMultiColumn(row, column))
824 columnofcell[i] = column;
825 setFixedWidth(row, column);
826 updateContentAlignment(row, column);
832 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
834 row_type const row = cellRow(cell);
836 for (col_type i = 0; i < column_info.size(); ++i)
837 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
843 bool Tabular::topLine(idx_type const cell) const
845 return cellInfo(cell).top_line;
849 bool Tabular::bottomLine(idx_type const cell) const
851 return cellInfo(cell).bottom_line;
855 bool Tabular::leftLine(idx_type cell) const
859 return cellInfo(cell).left_line;
863 bool Tabular::rightLine(idx_type cell) const
867 return cellInfo(cell).right_line;
871 bool Tabular::topAlreadyDrawn(idx_type cell) const
873 row_type row = cellRow(cell);
876 idx_type i = cellIndex(row - 1, cellColumn(cell));
877 return !rowTopLine(row) && bottomLine(i);
881 bool Tabular::leftAlreadyDrawn(idx_type cell) const
883 col_type col = cellColumn(cell);
886 idx_type i = cellIndex(cellRow(cell), col - 1);
887 return rightLine(i) && !leftLine(cell);
891 bool Tabular::isLastRow(idx_type cell) const
893 return cellRow(cell) == row_info.size() - 1;
897 int Tabular::getAdditionalHeight(row_type row) const
899 if (!row || row >= row_info.size())
902 int const interline_space = row_info[row - 1].interline_space_default ?
904 row_info[row - 1].interline_space.inPixels(width());
905 if (rowTopLine(row) && rowBottomLine(row - 1))
906 return interline_space + WIDTH_OF_LINE;
907 return interline_space;
911 int Tabular::getAdditionalWidth(idx_type cell) const
913 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
915 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
916 return WIDTH_OF_LINE;
921 int Tabular::columnWidth(idx_type cell) const
924 col_type const span = columnSpan(cell);
925 col_type const col = cellColumn(cell);
926 for(col_type c = col; c < col + span ; ++c)
927 w += column_info[c].width;
932 bool Tabular::updateColumnWidths()
934 col_type const ncols = column_info.size();
935 row_type const nrows = row_info.size();
937 // for each col get max of single col cells
938 for(col_type c = 0; c < ncols; ++c) {
940 for(row_type r = 0; r < nrows; ++r) {
941 idx_type const i = cellIndex(r, c);
942 if (columnSpan(i) == 1)
943 new_width = max(new_width, cellInfo(i).width);
946 if (column_info[c].width != new_width) {
947 column_info[c].width = new_width;
951 // update col widths to fit merged cells
952 for(col_type c = 0; c < ncols; ++c)
953 for(row_type r = 0; r < nrows; ++r) {
954 idx_type const i = cellIndex(r, c);
955 int const span = columnSpan(i);
956 if (span == 1 || c > cellColumn(i))
960 for(col_type j = c; j < c + span ; ++j)
961 old_width += column_info[j].width;
963 if (cellInfo(i).width > old_width) {
964 column_info[c + span - 1].width += cellInfo(i).width - old_width;
973 int Tabular::width() const
975 col_type const ncols = column_info.size();
977 for (col_type i = 0; i < ncols; ++i)
978 width += column_info[i].width;
983 void Tabular::setCellWidth(idx_type cell, int new_width)
985 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
986 + getAdditionalWidth(cell);
990 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
993 if (!isMultiColumn(cell) || onlycolumn)
994 column_info[cellColumn(cell)].alignment = align;
996 cellInfo(cell).alignment = align;
997 cellInset(cell).get()->setContentAlignment(align);
1001 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1004 if (!isMultiColumn(cell) || onlycolumn)
1005 column_info[cellColumn(cell)].valignment = align;
1007 cellInfo(cell).valignment = align;
1014 * Allow line and paragraph breaks for fixed width cells or disallow them,
1015 * merge cell paragraphs and reset layout to standard for variable width
1018 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1020 inset->setAutoBreakRows(fixedWidth);
1021 inset->toggleFixedWidth(fixedWidth);
1025 // merge all paragraphs to one
1026 BufferParams const & bp = cur.bv().buffer().params();
1027 while (inset->paragraphs().size() > 1)
1028 mergeParagraph(bp, inset->paragraphs(), 0);
1032 // undo information has already been recorded
1033 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1034 bp.documentClass().plainLayoutName());
1041 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1042 Length const & width)
1044 col_type const j = cellColumn(cell);
1046 column_info[j].p_width = width;
1047 for (row_type i = 0; i < row_info.size(); ++i) {
1048 idx_type const cell = cellIndex(i, j);
1049 // because of multicolumns
1050 toggleFixedWidth(cur, cellInset(cell).get(),
1051 !getPWidth(cell).zero());
1053 // cur paragraph can become invalid after paragraphs were merged
1054 if (cur.pit() > cur.lastpit())
1055 cur.pit() = cur.lastpit();
1056 // cur position can become invalid after newlines were removed
1057 if (cur.pos() > cur.lastpos())
1058 cur.pos() = cur.lastpos();
1062 bool Tabular::setFixedWidth(row_type r, col_type c)
1064 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1065 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1066 || (multicol && !cell_info[r][c].p_width.zero());
1067 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1072 void Tabular::updateContentAlignment(row_type r, col_type c)
1074 cell_info[r][c].inset->setContentAlignment(
1075 getAlignment(cellIndex(r, c)));
1079 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1080 Length const & width)
1082 if (!isMultiColumn(cell))
1085 cellInfo(cell).p_width = width;
1086 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1087 // cur paragraph can become invalid after paragraphs were merged
1088 if (cur.pit() > cur.lastpit())
1089 cur.pit() = cur.lastpit();
1090 // cur position can become invalid after newlines were removed
1091 if (cur.pos() > cur.lastpos())
1092 cur.pos() = cur.lastpos();
1097 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1098 Tabular::Feature what)
1100 if (what == SET_SPECIAL_MULTI)
1101 cellInfo(cell).align_special = special;
1103 column_info[cellColumn(cell)].align_special = special;
1107 void Tabular::setAllLines(idx_type cell, bool line)
1109 setTopLine(cell, line);
1110 setBottomLine(cell, line);
1111 setRightLine(cell, line);
1112 setLeftLine(cell, line);
1116 void Tabular::setTopLine(idx_type i, bool line)
1118 cellInfo(i).top_line = line;
1122 void Tabular::setBottomLine(idx_type i, bool line)
1124 cellInfo(i).bottom_line = line;
1128 void Tabular::setLeftLine(idx_type cell, bool line)
1130 cellInfo(cell).left_line = line;
1134 void Tabular::setRightLine(idx_type cell, bool line)
1136 cellInfo(cell).right_line = line;
1139 bool Tabular::rowTopLine(row_type r) const
1141 idx_type i0 = getFirstCellInRow(r);
1142 idx_type i1 = getLastCellInRow(r);
1143 bool all_rows_set = true;
1144 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1145 all_rows_set = cellInfo(j).top_line;
1146 return all_rows_set;
1150 bool Tabular::rowBottomLine(row_type r) const
1152 idx_type i0 = getFirstCellInRow(r);
1153 idx_type i1 = getLastCellInRow(r);
1154 bool all_rows_set = true;
1155 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1156 all_rows_set = cellInfo(j).bottom_line;
1157 return all_rows_set;
1161 bool Tabular::columnLeftLine(col_type c) const
1168 row_type const nrows = row_info.size();
1169 for (row_type r = 0; r < nrows; ++r) {
1170 idx_type i = cellIndex(r, c);
1171 if (c == cellColumn(i)) {
1173 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1174 if (cellInfo(i).left_line || right)
1178 return 2 * nrows_left >= total;
1182 bool Tabular::columnRightLine(col_type c) const
1187 int nrows_right = 0;
1189 row_type const nrows = row_info.size();
1190 for (row_type r = 0; r < nrows; ++r) {
1191 idx_type i = cellIndex(r, c);
1192 if (c == cellColumn(i) + columnSpan(i) - 1) {
1194 bool left = (c + 1 < column_info.size()
1195 && cellInfo(cellIndex(r, c + 1)).left_line)
1196 || c + 1 == column_info.size();
1197 if (cellInfo(i).right_line && left)
1201 return 2 * nrows_right >= total;
1205 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1207 if (!onlycolumn && isMultiColumn(cell))
1208 return cellInfo(cell).alignment;
1209 return column_info[cellColumn(cell)].alignment;
1214 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1216 if (!onlycolumn && isMultiColumn(cell))
1217 return cellInfo(cell).valignment;
1218 return column_info[cellColumn(cell)].valignment;
1222 Length const Tabular::getPWidth(idx_type cell) const
1224 if (isMultiColumn(cell))
1225 return cellInfo(cell).p_width;
1226 return column_info[cellColumn(cell)].p_width;
1230 int Tabular::cellWidth(idx_type cell) const
1232 return cellInfo(cell).width;
1236 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1240 switch (getAlignment(cell)) {
1241 case LYX_ALIGN_CENTER:
1242 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1244 case LYX_ALIGN_RIGHT:
1245 x += columnWidth(cell) - cellWidth(cell);
1246 // + getAdditionalWidth(cell);
1249 // LYX_ALIGN_LEFT: nothing :-)
1253 // the LaTeX Way :-(
1259 bool Tabular::isFirstCellInRow(idx_type cell) const
1261 return cellColumn(cell) == 0;
1265 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1267 if (row > row_info.size() - 1)
1268 row = row_info.size() - 1;
1269 return cell_info[row][0].cellno;
1273 bool Tabular::isLastCellInRow(idx_type cell) const
1275 return cellRightColumn(cell) == column_info.size() - 1;
1279 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1281 if (row > row_info.size() - 1)
1282 row = row_info.size() - 1;
1283 return cell_info[row][column_info.size() - 1].cellno;
1287 Tabular::row_type Tabular::cellRow(idx_type cell) const
1289 if (cell >= numberofcells)
1290 return row_info.size() - 1;
1293 return rowofcell[cell];
1297 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1299 if (cell >= numberofcells)
1300 return column_info.size() - 1;
1303 return columnofcell[cell];
1307 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1309 row_type const row = cellRow(cell);
1310 col_type column = cellColumn(cell);
1311 while (column < column_info.size() - 1 &&
1312 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1318 void Tabular::write(ostream & os) const
1322 << write_attribute("version", 3)
1323 << write_attribute("rows", row_info.size())
1324 << write_attribute("columns", column_info.size())
1326 // global longtable options
1328 << write_attribute("rotate", rotate)
1329 << write_attribute("booktabs", use_booktabs)
1330 << write_attribute("islongtable", is_long_tabular)
1331 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1332 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1333 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1334 << write_attribute("headTopDL", endhead.topDL)
1335 << write_attribute("headBottomDL", endhead.bottomDL)
1336 << write_attribute("footTopDL", endfoot.topDL)
1337 << write_attribute("footBottomDL", endfoot.bottomDL)
1338 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1339 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1340 << write_attribute("lastFootEmpty", endlastfoot.empty);
1341 // longtables cannot be aligned vertically
1342 if (!is_long_tabular)
1343 os << write_attribute("tabularvalignment", tabular_valignment);
1344 if (is_long_tabular)
1345 os << write_attribute("longtabularalignment",
1346 longtabular_alignment);
1348 for (col_type j = 0; j < column_info.size(); ++j) {
1350 << write_attribute("alignment", column_info[j].alignment)
1351 << write_attribute("valignment", column_info[j].valignment)
1352 << write_attribute("width", column_info[j].p_width.asString())
1353 << write_attribute("special", column_info[j].align_special)
1356 for (row_type i = 0; i < row_info.size(); ++i) {
1357 static const string def("default");
1359 if (row_info[i].top_space_default)
1360 os << write_attribute("topspace", def);
1362 os << write_attribute("topspace", row_info[i].top_space);
1363 if (row_info[i].bottom_space_default)
1364 os << write_attribute("bottomspace", def);
1366 os << write_attribute("bottomspace", row_info[i].bottom_space);
1367 if (row_info[i].interline_space_default)
1368 os << write_attribute("interlinespace", def);
1370 os << write_attribute("interlinespace", row_info[i].interline_space);
1371 os << write_attribute("endhead", row_info[i].endhead)
1372 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1373 << write_attribute("endfoot", row_info[i].endfoot)
1374 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1375 << write_attribute("newpage", row_info[i].newpage)
1376 << write_attribute("caption", row_info[i].caption)
1378 for (col_type j = 0; j < column_info.size(); ++j) {
1380 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1381 << write_attribute("alignment", cell_info[i][j].alignment)
1382 << write_attribute("valignment", cell_info[i][j].valignment)
1383 << write_attribute("topline", cell_info[i][j].top_line)
1384 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1385 << write_attribute("leftline", cell_info[i][j].left_line)
1386 << write_attribute("rightline", cell_info[i][j].right_line)
1387 << write_attribute("rotate", cell_info[i][j].rotate)
1388 << write_attribute("usebox", cell_info[i][j].usebox)
1389 << write_attribute("width", cell_info[i][j].p_width)
1390 << write_attribute("special", cell_info[i][j].align_special)
1392 os << "\\begin_inset ";
1393 cell_info[i][j].inset->write(os);
1394 os << "\n\\end_inset\n"
1399 os << "</lyxtabular>\n";
1403 void Tabular::read(Lexer & lex)
1406 istream & is = lex.getStream();
1408 l_getline(is, line);
1409 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1410 LASSERT(false, /**/);
1415 if (!getTokenValue(line, "version", version))
1417 LASSERT(version >= 2, /**/);
1420 if (!getTokenValue(line, "rows", rows_arg))
1423 if (!getTokenValue(line, "columns", columns_arg))
1425 init(buffer_, rows_arg, columns_arg);
1426 l_getline(is, line);
1427 if (!prefixIs(line, "<features")) {
1428 lyxerr << "Wrong tabular format (expected <features ...> got"
1429 << line << ')' << endl;
1432 getTokenValue(line, "rotate", rotate);
1433 getTokenValue(line, "booktabs", use_booktabs);
1434 getTokenValue(line, "islongtable", is_long_tabular);
1435 getTokenValue(line, "tabularvalignment", tabular_valignment);
1436 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1437 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1438 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1439 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1440 getTokenValue(line, "headTopDL", endhead.topDL);
1441 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1442 getTokenValue(line, "footTopDL", endfoot.topDL);
1443 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1444 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1445 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1446 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1448 for (col_type j = 0; j < column_info.size(); ++j) {
1450 if (!prefixIs(line,"<column")) {
1451 lyxerr << "Wrong tabular format (expected <column ...> got"
1452 << line << ')' << endl;
1455 getTokenValue(line, "alignment", column_info[j].alignment);
1456 getTokenValue(line, "valignment", column_info[j].valignment);
1457 getTokenValue(line, "width", column_info[j].p_width);
1458 getTokenValue(line, "special", column_info[j].align_special);
1461 for (row_type i = 0; i < row_info.size(); ++i) {
1462 l_getline(is, line);
1463 if (!prefixIs(line, "<row")) {
1464 lyxerr << "Wrong tabular format (expected <row ...> got"
1465 << line << ')' << endl;
1468 getTokenValue(line, "topspace", row_info[i].top_space,
1469 row_info[i].top_space_default);
1470 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1471 row_info[i].bottom_space_default);
1472 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1473 row_info[i].interline_space_default);
1474 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1475 getTokenValue(line, "endhead", row_info[i].endhead);
1476 getTokenValue(line, "endfoot", row_info[i].endfoot);
1477 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1478 getTokenValue(line, "newpage", row_info[i].newpage);
1479 getTokenValue(line, "caption", row_info[i].caption);
1480 for (col_type j = 0; j < column_info.size(); ++j) {
1481 l_getline(is, line);
1482 if (!prefixIs(line, "<cell")) {
1483 lyxerr << "Wrong tabular format (expected <cell ...> got"
1484 << line << ')' << endl;
1487 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1488 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1489 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1490 getTokenValue(line, "topline", cell_info[i][j].top_line);
1491 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1492 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1493 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1494 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1495 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1496 getTokenValue(line, "width", cell_info[i][j].p_width);
1498 getTokenValue(line, "special", cell_info[i][j].align_special);
1499 l_getline(is, line);
1500 if (prefixIs(line, "\\begin_inset")) {
1501 cell_info[i][j].inset->setBuffer(*buffer_);
1502 cell_info[i][j].inset->read(lex);
1503 l_getline(is, line);
1505 if (!prefixIs(line, "</cell>")) {
1506 lyxerr << "Wrong tabular format (expected </cell> got"
1507 << line << ')' << endl;
1511 l_getline(is, line);
1512 if (!prefixIs(line, "</row>")) {
1513 lyxerr << "Wrong tabular format (expected </row> got"
1514 << line << ')' << endl;
1518 while (!prefixIs(line, "</lyxtabular>")) {
1519 l_getline(is, line);
1525 bool Tabular::isMultiColumn(idx_type cell) const
1527 return cellInfo(cell).multicolumn != CELL_NORMAL;
1531 bool Tabular::isMultiColumnReal(idx_type cell) const
1533 return cellColumn(cell) != cellRightColumn(cell) &&
1534 cellInfo(cell).multicolumn != CELL_NORMAL;
1538 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1540 return cell_info[cellRow(cell)][cellColumn(cell)];
1544 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1546 CellData & cs = cellInfo(cell);
1547 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1548 cs.alignment = column_info[cellColumn(cell)].alignment;
1549 setRightLine(cell, rightLine(cell + number - 1));
1550 for (idx_type i = 1; i < number; ++i) {
1551 CellData & cs1 = cellInfo(cell + i);
1552 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1553 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1560 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1562 row_type const row = cellRow(cell);
1563 col_type const ncols = column_info.size();
1564 idx_type result = 1;
1565 col_type column = cellColumn(cell) + 1;
1566 while (column < ncols && isPartOfMultiColumn(row, column)) {
1574 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1576 row_type const row = cellRow(cell);
1577 col_type column = cellColumn(cell);
1579 idx_type result = 0;
1581 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1582 cell_info[row][column].multicolumn = CELL_NORMAL;
1584 while (column < column_info.size() &&
1585 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1587 cell_info[row][column].multicolumn = CELL_NORMAL;
1597 void Tabular::setRotateCell(idx_type cell, bool flag)
1599 cellInfo(cell).rotate = flag;
1603 bool Tabular::getRotateCell(idx_type cell) const
1605 return cellInfo(cell).rotate;
1609 bool Tabular::needRotating() const
1613 for (row_type i = 0; i < row_info.size(); ++i)
1614 for (col_type j = 0; j < column_info.size(); ++j)
1615 if (cell_info[i][j].rotate)
1621 bool Tabular::isLastCell(idx_type cell) const
1623 if (cell + 1 < numberofcells)
1629 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1631 if (cellRow(cell) > 0)
1632 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1637 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1639 if (cellRow(cell) + 1 < row_info.size())
1640 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1645 Tabular::idx_type Tabular::cellIndex(row_type row,
1646 col_type column) const
1648 BOOST_ASSERT(column != npos && column < column_info.size()
1649 && row != npos && row < row_info.size());
1650 return cell_info[row][column].cellno;
1654 void Tabular::setUsebox(idx_type cell, BoxType type)
1656 cellInfo(cell).usebox = type;
1660 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1661 // adds line breaks, see bug 4886.
1662 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1664 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1665 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1667 if (cellInfo(cell).usebox > 1)
1668 return cellInfo(cell).usebox;
1669 return useParbox(cell);
1674 // This are functions used for the longtable support
1676 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1682 row_info[row].endfirsthead = flag;
1686 row_info[row].endhead = flag;
1691 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1694 hd.set = haveLTHead();
1695 return row_info[row].endhead;
1699 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1702 hd.set = haveLTFirstHead();
1703 return row_info[row].endfirsthead;
1707 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1713 row_info[row].endlastfoot = flag;
1717 row_info[row].endfoot = flag;
1722 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1725 fd.set = haveLTFoot();
1726 return row_info[row].endfoot;
1730 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1733 fd.set = haveLTLastFoot();
1734 return row_info[row].endlastfoot;
1738 void Tabular::setLTNewPage(row_type row, bool what)
1740 row_info[row].newpage = what;
1744 bool Tabular::getLTNewPage(row_type row) const
1746 return row_info[row].newpage;
1750 bool Tabular::haveLTHead() const
1752 for (row_type i = 0; i < row_info.size(); ++i)
1753 if (row_info[i].endhead)
1759 bool Tabular::haveLTFirstHead() const
1761 if (endfirsthead.empty)
1763 for (row_type i = 0; i < row_info.size(); ++i)
1764 if (row_info[i].endfirsthead)
1770 bool Tabular::haveLTFoot() const
1772 for (row_type i = 0; i < row_info.size(); ++i)
1773 if (row_info[i].endfoot)
1779 bool Tabular::haveLTLastFoot() const
1781 if (endlastfoot.empty)
1783 for (row_type i = 0; i < row_info.size(); ++i)
1784 if (row_info[i].endlastfoot)
1790 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1792 idx_type i = getFirstCellInRow(row);
1794 setMultiColumn(i, numberOfCellsInRow(i));
1795 setTopLine(i, false);
1796 setBottomLine(i, false);
1797 setLeftLine(i, false);
1798 setRightLine(i, false);
1800 unsetMultiColumn(i);
1801 // When unsetting a caption row, also all existing
1802 // captions in this row must be dissolved.
1804 row_info[row].caption = what;
1809 bool Tabular::ltCaption(row_type row) const
1811 return row_info[row].caption;
1815 bool Tabular::haveLTCaption() const
1817 for (row_type i = 0; i < row_info.size(); ++i)
1818 if (row_info[i].caption)
1824 // end longtable support functions
1826 void Tabular::setRowAscent(row_type row, int height)
1828 if (row >= row_info.size() || row_info[row].ascent == height)
1830 row_info[row].ascent = height;
1834 void Tabular::setRowDescent(row_type row, int height)
1836 if (row >= row_info.size() || row_info[row].descent == height)
1838 row_info[row].descent = height;
1842 int Tabular::rowAscent(row_type row) const
1844 if (row >= row_info.size())
1846 return row_info[row].ascent;
1850 int Tabular::rowDescent(row_type row) const
1852 LASSERT(row < row_info.size(), /**/);
1853 return row_info[row].descent;
1857 int Tabular::height() const
1860 for (row_type row = 0; row < row_info.size(); ++row)
1861 height += rowAscent(row) + rowDescent(row) +
1862 getAdditionalHeight(row);
1867 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1869 LASSERT(row < row_info.size(), /**/);
1870 LASSERT(column < column_info.size(), /**/);
1871 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1875 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1877 // we only output complete row lines and the 1st row here, the rest
1878 // is done in Tabular::TeXBottomHLine(...)
1880 // get for each column the topline (if any)
1881 col_type const ncols = column_info.size();
1882 vector<bool> topline;
1884 for (col_type c = 0; c < ncols; ++c) {
1885 topline.push_back(topLine(cellIndex(row, c)));
1890 // do nothing if empty first row, or incomplete row line after
1891 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1894 // only output complete row lines and the 1st row's clines
1895 if (nset == ncols) {
1897 os << (row == 0 ? "\\toprule " : "\\midrule ");
1901 } else if (row == 0) {
1902 for (col_type c = 0; c < ncols; ++c) {
1904 //babel makes the "-" character an active one, so we have to suppress this here
1905 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1906 if (lang == "slovak" || lang == "czech")
1907 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1908 "\\expandafter\\cline\\expandafter{\\expandafter")
1909 << c + 1 << "\\string-";
1911 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1912 // get to last column of line span
1913 while (c < ncols && topline[c])
1924 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1926 // we output bottomlines of row r and the toplines of row r+1
1927 // if the latter do not span the whole tabular
1929 // get the bottomlines of row r, and toplines in next row
1930 bool lastrow = row == row_info.size() - 1;
1931 col_type const ncols = column_info.size();
1932 vector<bool> bottomline, topline;
1933 bool nextrowset = true;
1934 for (col_type c = 0; c < ncols; ++c) {
1935 bottomline.push_back(bottomLine(cellIndex(row, c)));
1936 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1937 nextrowset &= topline[c];
1940 // combine this row's bottom lines and next row's toplines if necessary
1942 for (col_type c = 0; c < ncols; ++c) {
1944 bottomline[c] = bottomline[c] || topline[c];
1949 // do nothing if empty, OR incomplete row line with a topline in next row
1950 if (nset == 0 || (nextrowset && nset != ncols))
1953 if (nset == ncols) {
1955 os << (lastrow ? "\\bottomrule" : "\\midrule");
1959 for (col_type c = 0; c < ncols; ++c) {
1960 if (bottomline[c]) {
1961 //babel makes the "-" character an active one, so we have to suppress this here
1962 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1963 if (lang == "slovak" || lang == "czech")
1964 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1965 "\\expandafter\\cline\\expandafter{\\expandafter")
1966 << c + 1 << "\\string-";
1968 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1969 // get to last column of line span
1970 while (c < ncols && bottomline[c])
1981 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1984 row_type const r = cellRow(cell);
1985 if (is_long_tabular && row_info[r].caption)
1988 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1989 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1990 // figure out how to set the lines
1991 // we always set double lines to the right of the cell
1992 col_type const c = cellColumn(cell);
1993 col_type const nextcol = c + columnSpan(cell);
1994 bool colright = columnRightLine(c);
1995 bool colleft = columnLeftLine(c);
1996 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1997 bool nextcellleft = nextcol < column_info.size()
1998 && leftLine(cellIndex(r, nextcol));
1999 bool coldouble = colright && nextcolleft;
2000 bool celldouble = rightLine(cell) && nextcellleft;
2001 ismulticol = isMultiColumn(cell)
2002 || (c == 0 && colleft != leftLine(cell))
2003 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2004 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2005 || (coldouble != celldouble);
2007 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2008 if (c ==0 && leftLine(cell))
2010 if (!cellInfo(cell).align_special.empty()) {
2011 os << cellInfo(cell).align_special;
2013 if (!getPWidth(cell).zero()) {
2015 case LYX_ALIGN_LEFT:
2016 os << ">{\\raggedright}";
2018 case LYX_ALIGN_RIGHT:
2019 os << ">{\\raggedleft}";
2021 case LYX_ALIGN_CENTER:
2022 os << ">{\\centering}";
2028 case LYX_VALIGN_TOP:
2031 case LYX_VALIGN_MIDDLE:
2034 case LYX_VALIGN_BOTTOM:
2039 << from_ascii(getPWidth(cell).asLatexString())
2043 case LYX_ALIGN_LEFT:
2046 case LYX_ALIGN_RIGHT:
2053 } // end if else !getPWidth
2054 } // end if else !cellinfo_of_cell
2055 if (rightLine(cell) || nextcellleft)
2058 // add extra vertical line if we want a double one
2062 if (getRotateCell(cell)) {
2063 os << "\\begin{sideways}\n";
2066 if (getUsebox(cell) == BOX_PARBOX) {
2069 case LYX_VALIGN_TOP:
2072 case LYX_VALIGN_MIDDLE:
2075 case LYX_VALIGN_BOTTOM:
2079 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2081 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2082 os << "\\begin{minipage}[";
2084 case LYX_VALIGN_TOP:
2087 case LYX_VALIGN_MIDDLE:
2090 case LYX_VALIGN_BOTTOM:
2094 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2102 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2105 row_type const r = cellRow(cell);
2106 if (is_long_tabular && row_info[r].caption)
2110 if (getUsebox(cell) == BOX_PARBOX)
2112 else if (getUsebox(cell) == BOX_MINIPAGE) {
2113 os << "%\n\\end{minipage}";
2116 if (getRotateCell(cell)) {
2117 os << "%\n\\end{sideways}";
2127 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2128 OutputParams const & runparams) const
2130 if (!is_long_tabular)
2135 // the caption must be output before the headers
2136 if (haveLTCaption()) {
2137 for (row_type i = 0; i < row_info.size(); ++i) {
2138 if (row_info[i].caption) {
2139 ret += TeXRow(os, i, runparams);
2143 // output first header info
2144 // first header must be output before the header, otherwise the
2145 // correct caption placement becomes really weird
2146 if (haveLTFirstHead()) {
2147 if (endfirsthead.topDL) {
2151 for (row_type i = 0; i < row_info.size(); ++i) {
2152 if (row_info[i].endfirsthead) {
2153 ret += TeXRow(os, i, runparams);
2156 if (endfirsthead.bottomDL) {
2160 os << "\\endfirsthead\n";
2163 // output header info
2165 if (endfirsthead.empty && !haveLTFirstHead()) {
2166 os << "\\endfirsthead\n";
2169 if (endhead.topDL) {
2173 for (row_type i = 0; i < row_info.size(); ++i) {
2174 if (row_info[i].endhead) {
2175 ret += TeXRow(os, i, runparams);
2178 if (endhead.bottomDL) {
2182 os << "\\endhead\n";
2185 // output footer info
2187 if (endfoot.topDL) {
2191 for (row_type i = 0; i < row_info.size(); ++i) {
2192 if (row_info[i].endfoot) {
2193 ret += TeXRow(os, i, runparams);
2196 if (endfoot.bottomDL) {
2200 os << "\\endfoot\n";
2202 if (endlastfoot.empty && !haveLTLastFoot()) {
2203 os << "\\endlastfoot\n";
2207 // output lastfooter info
2208 if (haveLTLastFoot()) {
2209 if (endlastfoot.topDL) {
2213 for (row_type i = 0; i < row_info.size(); ++i) {
2214 if (row_info[i].endlastfoot) {
2215 ret += TeXRow(os, i, runparams);
2218 if (endlastfoot.bottomDL) {
2222 os << "\\endlastfoot\n";
2229 bool Tabular::isValidRow(row_type row) const
2231 if (!is_long_tabular)
2233 return !row_info[row].endhead && !row_info[row].endfirsthead
2234 && !row_info[row].endfoot && !row_info[row].endlastfoot
2235 && !row_info[row].caption;
2239 int Tabular::TeXRow(odocstream & os, row_type i,
2240 OutputParams const & runparams) const
2242 idx_type cell = cellIndex(i, 0);
2243 shared_ptr<InsetTableCell> inset = cellInset(cell);
2244 Paragraph const & par = inset->paragraphs().front();
2245 string const lang = par.getParLanguage(buffer().params())->lang();
2247 //output the top line
2248 int ret = TeXTopHLine(os, i, lang);
2250 if (row_info[i].top_space_default) {
2252 os << "\\addlinespace\n";
2254 os << "\\noalign{\\vskip\\doublerulesep}\n";
2256 } else if(!row_info[i].top_space.zero()) {
2258 os << "\\addlinespace["
2259 << from_ascii(row_info[i].top_space.asLatexString())
2262 os << "\\noalign{\\vskip"
2263 << from_ascii(row_info[i].top_space.asLatexString())
2268 bool ismulticol = false;
2269 for (col_type j = 0; j < column_info.size(); ++j) {
2270 if (isPartOfMultiColumn(i, j))
2272 ret += TeXCellPreamble(os, cell, ismulticol);
2273 shared_ptr<InsetTableCell> inset = cellInset(cell);
2275 Paragraph const & par = inset->paragraphs().front();
2276 bool rtl = par.isRTL(buffer().params())
2278 && getPWidth(cell).zero();
2282 par.getParLanguage(buffer().params())->lang();
2283 if (lang == "farsi")
2285 else if (lang == "arabic_arabi")
2287 // currently, remaning RTL languages are
2288 // arabic_arabtex and hebrew
2292 // pass to the OutputParams that we are in a cell and
2293 // which alignment we have set.
2294 // InsetNewline needs this context information.
2295 OutputParams newrp(runparams);
2296 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2297 ? OutputParams::PLAIN
2298 : OutputParams::ALIGNED;
2299 ret += inset->latex(os, newrp);
2300 runparams.encoding = newrp.encoding;
2304 ret += TeXCellPostamble(os, cell, ismulticol);
2305 if (!isLastCellInRow(cell)) { // not last cell in row
2310 if (row_info[i].caption && !endfirsthead.empty && !haveLTFirstHead())
2311 // if no first header and no empty first header is used,
2312 // the caption needs to be terminated by \endfirsthead
2314 os << "\\endfirsthead";
2316 os << "\\tabularnewline";
2317 if (row_info[i].bottom_space_default) {
2319 os << "\\addlinespace";
2321 os << "[\\doublerulesep]";
2322 } else if (!row_info[i].bottom_space.zero()) {
2324 os << "\\addlinespace";
2326 << from_ascii(row_info[i].bottom_space.asLatexString())
2332 //output the bottom line
2333 ret += TeXBottomHLine(os, i, lang);
2335 if (row_info[i].interline_space_default) {
2337 os << "\\addlinespace\n";
2339 os << "\\noalign{\\vskip\\doublerulesep}\n";
2341 } else if (!row_info[i].interline_space.zero()) {
2343 os << "\\addlinespace["
2344 << from_ascii(row_info[i].interline_space.asLatexString())
2347 os << "\\noalign{\\vskip"
2348 << from_ascii(row_info[i].interline_space.asLatexString())
2356 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2360 //+---------------------------------------------------------------------
2361 //+ first the opening preamble +
2362 //+---------------------------------------------------------------------
2365 os << "\\begin{sideways}\n";
2368 if (is_long_tabular) {
2369 os << "\\begin{longtable}";
2370 switch (longtabular_alignment) {
2371 case LYX_LONGTABULAR_ALIGN_LEFT:
2374 case LYX_LONGTABULAR_ALIGN_CENTER:
2376 case LYX_LONGTABULAR_ALIGN_RIGHT:
2381 os << "\\begin{tabular}";
2382 switch (tabular_valignment) {
2383 case LYX_VALIGN_TOP:
2386 case LYX_VALIGN_MIDDLE:
2388 case LYX_VALIGN_BOTTOM:
2396 for (col_type i = 0; i < column_info.size(); ++i) {
2397 if (columnLeftLine(i))
2399 if (!column_info[i].align_special.empty()) {
2400 os << column_info[i].align_special;
2402 if (!column_info[i].p_width.zero()) {
2403 switch (column_info[i].alignment) {
2404 case LYX_ALIGN_LEFT:
2405 os << ">{\\raggedright}";
2407 case LYX_ALIGN_RIGHT:
2408 os << ">{\\raggedleft}";
2410 case LYX_ALIGN_CENTER:
2411 os << ">{\\centering}";
2413 case LYX_ALIGN_NONE:
2414 case LYX_ALIGN_BLOCK:
2415 case LYX_ALIGN_LAYOUT:
2416 case LYX_ALIGN_SPECIAL:
2420 switch (column_info[i].valignment) {
2421 case LYX_VALIGN_TOP:
2424 case LYX_VALIGN_MIDDLE:
2427 case LYX_VALIGN_BOTTOM:
2432 << from_ascii(column_info[i].p_width.asLatexString())
2435 switch (column_info[i].alignment) {
2436 case LYX_ALIGN_LEFT:
2439 case LYX_ALIGN_RIGHT:
2446 } // end if else !column_info[i].p_width
2447 } // end if else !column_info[i].align_special
2448 if (columnRightLine(i))
2454 ret += TeXLongtableHeaderFooter(os, runparams);
2456 //+---------------------------------------------------------------------
2457 //+ the single row and columns (cells) +
2458 //+---------------------------------------------------------------------
2460 for (row_type i = 0; i < row_info.size(); ++i) {
2461 if (isValidRow(i)) {
2462 ret += TeXRow(os, i, runparams);
2463 if (is_long_tabular && row_info[i].newpage) {
2464 os << "\\newpage\n";
2470 //+---------------------------------------------------------------------
2471 //+ the closing of the tabular +
2472 //+---------------------------------------------------------------------
2474 if (is_long_tabular)
2475 os << "\\end{longtable}";
2477 os << "\\end{tabular}";
2479 os << "\n\\end{sideways}";
2487 int Tabular::docbookRow(odocstream & os, row_type row,
2488 OutputParams const & runparams) const
2491 idx_type cell = getFirstCellInRow(row);
2494 for (col_type j = 0; j < column_info.size(); ++j) {
2495 if (isPartOfMultiColumn(row, j))
2498 os << "<entry align=\"";
2499 switch (getAlignment(cell)) {
2500 case LYX_ALIGN_LEFT:
2503 case LYX_ALIGN_RIGHT:
2511 os << "\" valign=\"";
2512 switch (getVAlignment(cell)) {
2513 case LYX_VALIGN_TOP:
2516 case LYX_VALIGN_BOTTOM:
2519 case LYX_VALIGN_MIDDLE:
2524 if (isMultiColumn(cell)) {
2525 os << " namest=\"col" << j << "\" ";
2526 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2530 ret += cellInset(cell)->docbook(os, runparams);
2539 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2543 //+---------------------------------------------------------------------
2544 //+ first the opening preamble +
2545 //+---------------------------------------------------------------------
2547 os << "<tgroup cols=\"" << column_info.size()
2548 << "\" colsep=\"1\" rowsep=\"1\">\n";
2550 for (col_type i = 0; i < column_info.size(); ++i) {
2551 os << "<colspec colname=\"col" << i << "\" align=\"";
2552 switch (column_info[i].alignment) {
2553 case LYX_ALIGN_LEFT:
2556 case LYX_ALIGN_RIGHT:
2564 if (runparams.flavor == OutputParams::XML)
2570 //+---------------------------------------------------------------------
2571 //+ Long Tabular case +
2572 //+---------------------------------------------------------------------
2574 // output caption info
2575 if (haveLTCaption()) {
2576 os << "<caption>\n";
2578 for (row_type i = 0; i < row_info.size(); ++i) {
2579 if (row_info[i].caption) {
2580 ret += docbookRow(os, i, runparams);
2583 os << "</caption>\n";
2586 // output header info
2587 if (haveLTHead() || haveLTFirstHead()) {
2590 for (row_type i = 0; i < row_info.size(); ++i) {
2591 if (row_info[i].endhead || row_info[i].endfirsthead) {
2592 ret += docbookRow(os, i, runparams);
2598 // output footer info
2599 if (haveLTFoot() || haveLTLastFoot()) {
2602 for (row_type i = 0; i < row_info.size(); ++i) {
2603 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2604 ret += docbookRow(os, i, runparams);
2611 //+---------------------------------------------------------------------
2612 //+ the single row and columns (cells) +
2613 //+---------------------------------------------------------------------
2617 for (row_type i = 0; i < row_info.size(); ++i) {
2618 if (isValidRow(i)) {
2619 ret += docbookRow(os, i, runparams);
2624 //+---------------------------------------------------------------------
2625 //+ the closing of the tabular +
2626 //+---------------------------------------------------------------------
2635 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2636 vector<unsigned int> const & clen) const
2638 idx_type const fcell = getFirstCellInRow(row);
2639 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2642 for (idx_type i = fcell; i < n; ++i) {
2652 for (idx_type i = fcell; i < n; ++i) {
2663 col_type column = cellColumn(i);
2664 int len = clen[column];
2665 while (column < column_info.size() - 1
2666 && isPartOfMultiColumn(row, ++column))
2667 len += clen[column] + 4;
2668 os << docstring(len, ch);
2683 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2684 vector<unsigned int> const & clen) const
2686 idx_type const fcell = getFirstCellInRow(row);
2687 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2690 for (idx_type i = fcell; i < n; ++i) {
2691 if (bottomLine(i)) {
2700 for (idx_type i = fcell; i < n; ++i) {
2701 if (bottomLine(i)) {
2711 col_type column = cellColumn(i);
2712 int len = clen[column];
2713 while (column < column_info.size() -1
2714 && isPartOfMultiColumn(row, ++column))
2715 len += clen[column] + 4;
2716 os << docstring(len, ch);
2717 if (bottomLine(i)) {
2731 void Tabular::plaintextPrintCell(odocstream & os,
2732 OutputParams const & runparams,
2733 idx_type cell, row_type row, col_type column,
2734 vector<unsigned int> const & clen,
2735 bool onlydata) const
2737 odocstringstream sstr;
2738 cellInset(cell)->plaintext(sstr, runparams);
2750 unsigned int len1 = sstr.str().length();
2751 unsigned int len2 = clen[column];
2752 while (column < column_info.size() -1
2753 && isPartOfMultiColumn(row, ++column))
2754 len2 += clen[column] + 4;
2757 switch (getAlignment(cell)) {
2759 case LYX_ALIGN_LEFT:
2762 case LYX_ALIGN_RIGHT:
2766 case LYX_ALIGN_CENTER:
2772 os << docstring(len1, ' ') << sstr.str()
2773 << docstring(len2, ' ');
2775 if (rightLine(cell))
2782 void Tabular::plaintext(odocstream & os,
2783 OutputParams const & runparams, int const depth,
2784 bool onlydata, char_type delim) const
2786 // first calculate the width of the single columns
2787 vector<unsigned int> clen(column_info.size());
2790 // first all non (real) multicolumn cells!
2791 for (col_type j = 0; j < column_info.size(); ++j) {
2793 for (row_type i = 0; i < row_info.size(); ++i) {
2794 idx_type cell = cellIndex(i, j);
2795 if (isMultiColumnReal(cell))
2797 odocstringstream sstr;
2798 cellInset(cell)->plaintext(sstr, runparams);
2799 if (clen[j] < sstr.str().length())
2800 clen[j] = sstr.str().length();
2803 // then all (real) multicolumn cells!
2804 for (col_type j = 0; j < column_info.size(); ++j) {
2805 for (row_type i = 0; i < row_info.size(); ++i) {
2806 idx_type cell = cellIndex(i, j);
2807 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2809 odocstringstream sstr;
2810 cellInset(cell)->plaintext(sstr, runparams);
2811 int len = int(sstr.str().length());
2812 idx_type const n = columnSpan(cell);
2813 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2815 if (len > int(clen[j + n - 1]))
2816 clen[j + n - 1] = len;
2821 for (row_type i = 0; i < row_info.size(); ++i) {
2822 if (!onlydata && plaintextTopHLine(os, i, clen))
2823 os << docstring(depth * 2, ' ');
2824 for (col_type j = 0; j < column_info.size(); ++j) {
2825 if (isPartOfMultiColumn(i, j))
2827 if (onlydata && j > 0)
2828 // we don't use operator<< for single UCS4 character.
2829 // see explanation in docstream.h
2831 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2836 os << docstring(depth * 2, ' ');
2837 if (plaintextBottomHLine(os, i, clen))
2838 os << docstring(depth * 2, ' ');
2844 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2846 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2850 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2851 col_type column) const
2853 return cell_info[row][column].inset;
2857 void Tabular::setCellInset(row_type row, col_type column,
2858 shared_ptr<InsetTableCell> ins) const
2860 CellData & cd = cell_info[row][column];
2865 void Tabular::validate(LaTeXFeatures & features) const
2867 features.require("NeedTabularnewline");
2869 features.require("booktabs");
2870 if (is_long_tabular)
2871 features.require("longtable");
2873 features.require("rotating");
2874 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2875 if (getVAlignment(cell) != LYX_VALIGN_TOP
2876 || !getPWidth(cell).zero())
2877 features.require("array");
2878 cellInset(cell)->validate(features);
2883 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2885 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2886 ParagraphList::const_iterator cit = parlist.begin();
2887 ParagraphList::const_iterator end = parlist.end();
2889 for (; cit != end; ++cit)
2890 for (int i = 0; i < cit->size(); ++i)
2891 if (cit->isNewline(i))
2898 /////////////////////////////////////////////////////////////////////
2902 /////////////////////////////////////////////////////////////////////
2904 InsetTableCell::InsetTableCell(Buffer * buf)
2905 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
2906 contentAlign(LYX_ALIGN_CENTER)
2910 bool InsetTableCell::forcePlainLayout(idx_type) const
2912 return !isFixedWidth;
2916 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2918 return isFixedWidth;
2922 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2923 FuncStatus & status) const
2926 switch (cmd.action) {
2928 enabled = !forcePlainLayout();
2930 case LFUN_LAYOUT_PARAGRAPH:
2931 enabled = allowParagraphCustomization();
2934 return InsetText::getStatus(cur, cmd, status);
2936 status.setEnabled(enabled);
2940 docstring InsetTableCell::asString(bool intoInsets)
2943 if (paragraphs().empty())
2945 ParagraphList::const_iterator it = paragraphs().begin();
2946 ParagraphList::const_iterator en = paragraphs().end();
2948 for (; it != en; ++it) {
2953 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2960 /////////////////////////////////////////////////////////////////////
2964 /////////////////////////////////////////////////////////////////////
2966 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
2968 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2969 rowselect_(false), colselect_(false)
2974 InsetTabular::InsetTabular(InsetTabular const & tab)
2975 : Inset(tab), tabular(tab.tabular), scx_(0)
2980 InsetTabular::~InsetTabular()
2982 hideDialogs("tabular", this);
2986 void InsetTabular::setBuffer(Buffer & buf)
2988 tabular.setBuffer(buf);
2989 Inset::setBuffer(buf);
2993 bool InsetTabular::insetAllowed(InsetCode code) const
2995 if (code == MATHMACRO_CODE
2996 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3003 void InsetTabular::write(ostream & os) const
3005 os << "Tabular" << endl;
3010 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3012 // FIXME: depending on the selection state, we could offer a different menu.
3013 return from_ascii("context-tabular");
3017 void InsetTabular::read(Lexer & lex)
3019 //bool const old_format = (lex.getString() == "\\LyXTable");
3027 string token = lex.getString();
3028 while (lex && token != "\\end_inset") {
3030 token = lex.getString();
3033 lex.printError("Missing \\end_inset at this point. ");
3037 int InsetTabular::rowFromY(Cursor & cur, int y) const
3039 // top y coordinate of tabular
3040 int h = yo(cur.bv()) - tabular.rowAscent(0);
3041 size_t nrows = tabular.row_info.size();
3043 for (; r < nrows && y > h; ++r) {
3044 h += tabular.rowAscent(r);
3045 h += tabular.rowDescent(r);
3046 h += tabular.getAdditionalHeight(r);
3052 int InsetTabular::columnFromX(Cursor & cur, int x) const
3054 // left x coordinate of tabular
3055 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3056 size_t ncols = tabular.column_info.size();
3058 for (; c < ncols && x > w; ++c)
3059 w += tabular.columnWidth(c);
3064 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3066 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3067 // mi.base.textwidth << "\n";
3070 LASSERT(false, /**/);
3074 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
3077 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3078 if (tabular.isPartOfMultiColumn(i, j))
3079 // Multicolumn cell, but not first one
3084 if (tabular.cell_info[i][j].multicolumn ==
3085 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3086 p_width = tabular.cellInfo(cell).p_width;
3088 p_width = tabular.column_info[j].p_width;
3089 if (!p_width.zero())
3090 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3091 tabular.cellInset(cell)->metrics(m, dim);
3092 if (!p_width.zero())
3093 dim.wid = m.base.textwidth;
3094 tabular.setCellWidth(cell, dim.wid);
3095 maxAsc = max(maxAsc, dim.asc);
3096 maxDesc = max(maxDesc, dim.des);
3099 int const top_space = tabular.row_info[i].top_space_default ?
3100 default_line_space :
3101 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3102 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3103 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3104 default_line_space :
3105 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3106 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3108 tabular.updateColumnWidths();
3109 dim.asc = tabular.rowAscent(0);
3110 dim.des = tabular.height() - dim.asc;
3111 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3114 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3117 if (&cur.inset() == this && cur.selection()) {
3118 if (cur.selIsMultiCell()) {
3121 getSelection(cur, rs, re, cs, ce);
3123 if (col >= cs && col <= ce && row >= rs && row <= re)
3126 if (col == tabular.cellColumn(cur.idx())
3127 && row == tabular.cellRow(cur.idx())) {
3128 CursorSlice const & beg = cur.selBegin();
3129 CursorSlice const & end = cur.selEnd();
3131 if ((end.lastpos() > 0 || end.lastpit() > 0)
3132 && end.pos() == end.lastpos() && beg.pos() == 0
3133 && end.pit() == end.lastpit() && beg.pit() == 0)
3141 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3143 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3144 BufferView * bv = pi.base.bv;
3145 Cursor & cur = pi.base.bv->cursor();
3147 // FIXME: As the full background is painted in drawSelection(),
3148 // we have no choice but to do a full repaint for the Text cells.
3149 pi.full_repaint = true;
3151 resetPos(bv->cursor());
3154 x += ADD_TO_TABULAR_WIDTH;
3156 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3157 bool const original_selection_state = pi.selected;
3160 first_visible_cell = Tabular::npos;
3161 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3163 int const a = tabular.rowAscent(i);
3164 int const d = tabular.rowDescent(i);
3165 idx = tabular.cellIndex(i, 0);
3166 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3167 if (tabular.isPartOfMultiColumn(i, j))
3169 if (first_visible_cell == Tabular::npos)
3170 first_visible_cell = idx;
3172 pi.selected |= isCellSelected(cur, i, j);
3173 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3174 // Cache the Inset position.
3175 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3176 if (nx + tabular.columnWidth(idx) < 0
3177 || nx > bv->workWidth()
3179 || y - a > bv->workHeight()) {
3180 pi.pain.setDrawingEnabled(false);
3181 cell(idx)->draw(pi, cx, y);
3182 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3183 pi.pain.setDrawingEnabled(original_drawing_state);
3185 cell(idx)->draw(pi, cx, y);
3186 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3188 nx += tabular.columnWidth(idx);
3190 pi.selected = original_selection_state;
3193 if (i + 1 < tabular.row_info.size())
3194 y += d + tabular.rowAscent(i + 1) +
3195 tabular.getAdditionalHeight(i + 1);
3200 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3202 Cursor & cur = pi.base.bv->cursor();
3205 x += scx_ + ADD_TO_TABULAR_WIDTH;
3207 // FIXME: it is wrong to completely paint the background
3208 // if we want to do single row painting.
3210 // Paint background of current tabular
3211 int const w = tabular.width();
3212 int const h = tabular.height();
3213 int yy = y - tabular.rowAscent(0);
3214 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3216 if (!cur.selection())
3218 if (&cur.inset() != this)
3223 bool const full_cell_selected = isCellSelected(cur,
3224 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3226 if (cur.selIsMultiCell() || full_cell_selected) {
3227 y -= tabular.rowAscent(0);
3228 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3229 int const a = tabular.rowAscent(j);
3230 int const h = a + tabular.rowDescent(j);
3232 y += tabular.getAdditionalHeight(j);
3233 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3234 if (tabular.isPartOfMultiColumn(j, i))
3236 idx_type const cell =
3237 tabular.cellIndex(j, i);
3238 int const w = tabular.columnWidth(cell);
3239 if (isCellSelected(cur, j, i))
3240 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3247 x += cellXPos(cur.idx());
3248 x += tabular.getBeginningOfTextInCell(cur.idx());
3249 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3254 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3255 row_type row, idx_type cell, Change const & change) const
3257 int x2 = x + tabular.columnWidth(cell);
3258 bool on_off = false;
3259 Color col = Color_tabularline;
3260 Color onoffcol = Color_tabularonoffline;
3262 if (change.changed()) {
3263 col = change.color();
3264 onoffcol = change.color();
3267 if (!tabular.topAlreadyDrawn(cell)) {
3268 on_off = !tabular.topLine(cell);
3269 pain.line(x, y - tabular.rowAscent(row),
3270 x2, y - tabular.rowAscent(row),
3271 on_off ? onoffcol : col,
3272 on_off ? Painter::line_onoffdash : Painter::line_solid);
3274 on_off = !tabular.bottomLine(cell);
3275 pain.line(x, y + tabular.rowDescent(row),
3276 x2, y + tabular.rowDescent(row),
3277 on_off ? onoffcol : col,
3278 on_off ? Painter::line_onoffdash : Painter::line_solid);
3279 if (!tabular.leftAlreadyDrawn(cell)) {
3280 on_off = !tabular.leftLine(cell);
3281 pain.line(x, y - tabular.rowAscent(row),
3282 x, y + tabular.rowDescent(row),
3283 on_off ? onoffcol : col,
3284 on_off ? Painter::line_onoffdash : Painter::line_solid);
3286 on_off = !tabular.rightLine(cell);
3287 pain.line(x2 - tabular.getAdditionalWidth(cell),
3288 y - tabular.rowAscent(row),
3289 x2 - tabular.getAdditionalWidth(cell),
3290 y + tabular.rowDescent(row),
3291 on_off ? onoffcol : col,
3292 on_off ? Painter::line_onoffdash : Painter::line_solid);
3296 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3298 //lyxerr << "InsetTabular::edit: " << this << endl;
3300 cur.setSelection(false);
3303 if (isRightToLeft(cur))
3304 cur.idx() = tabular.getLastCellInRow(0);
3310 if (isRightToLeft(cur))
3311 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3313 cur.idx() = tabular.numberofcells - 1;
3315 cur.pos() = cur.lastpos(); // FIXME crude guess
3317 // FIXME: this accesses the position cache before it is initialized
3319 //cur.bv().fitCursor();
3323 void InsetTabular::updateLabels(ParIterator const & it)
3325 // In a longtable, tell captions what the current float is
3326 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3327 string const saveflt = cnts.current_float();
3328 if (tabular.is_long_tabular)
3329 cnts.current_float("table");
3331 ParIterator it2 = it;
3333 size_t const end = it2.nargs();
3334 for ( ; it2.idx() < end; it2.top().forwardIdx())
3335 buffer().updateLabels(it2);
3338 if (tabular.is_long_tabular)
3339 cnts.current_float(saveflt);
3343 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3345 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3346 << "\n cur:" << cur);
3347 CursorSlice sl = cur.top();
3348 Cursor & bvcur = cur.bv().cursor();
3350 switch (cmd.action) {
3352 case LFUN_MOUSE_PRESS: {
3353 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3355 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3356 || cmd.x > xo(cur.bv()) + tabular.width()) {
3357 row_type r = rowFromY(cur, cmd.y);
3358 cur.idx() = tabular.getFirstCellInRow(r);
3361 cur.idx() = tabular.getLastCellInRow(r);
3362 cur.pos() = cur.lastpos();
3363 cur.setSelection(true);
3369 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3370 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3371 || cmd.y > y0 + tabular.height()) {
3372 col_type c = columnFromX(cur, cmd.x);
3373 cur.idx() = tabular.cellIndex(0, c);
3376 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3377 cur.pos() = cur.lastpos();
3378 cur.setSelection(true);
3383 // do not reset cursor/selection if we have selected
3384 // some cells (bug 2715).
3385 if (cmd.button() == mouse_button::button3
3386 && &bvcur.selBegin().inset() == this
3387 && bvcur.selIsMultiCell())
3390 // Let InsetTableCell do it
3391 cell(cur.idx())->dispatch(cur, cmd);
3394 case LFUN_MOUSE_MOTION:
3395 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3396 if (cmd.button() == mouse_button::button1) {
3397 // only accept motions to places not deeper nested than the real anchor
3398 if (!bvcur.anchor_.hasPart(cur)) {
3402 // select (additional) row
3404 row_type r = rowFromY(cur, cmd.y);
3405 cur.idx() = tabular.getLastCellInRow(r);
3406 // we need to reset the cursor's pit and pos now, as the old ones
3407 // may no longer be valid.
3410 bvcur.setCursor(cur);
3411 bvcur.setSelection(true);
3414 // select (additional) column
3416 col_type c = columnFromX(cur, cmd.x);
3417 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3418 // we need to reset the cursor's pit and pos now, as the old ones
3419 // may no longer be valid.
3422 bvcur.setCursor(cur);
3423 bvcur.setSelection(true);
3426 // only update if selection changes
3427 if (bvcur.idx() == cur.idx() &&
3428 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3430 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3431 bvcur.setCursor(cur);
3432 bvcur.setSelection(true);
3433 // if this is a multicell selection, we just set the cursor to
3434 // the beginning of the cell's text.
3435 if (bvcur.selIsMultiCell()) {
3436 bvcur.pit() = bvcur.lastpit();
3437 bvcur.pos() = bvcur.lastpos();
3442 case LFUN_MOUSE_RELEASE:
3447 case LFUN_CELL_BACKWARD:
3449 cur.setSelection(false);
3452 case LFUN_CELL_FORWARD:
3454 cur.setSelection(false);
3457 case LFUN_CHAR_FORWARD_SELECT:
3458 case LFUN_CHAR_FORWARD:
3459 case LFUN_CHAR_BACKWARD_SELECT:
3460 case LFUN_CHAR_BACKWARD:
3461 case LFUN_CHAR_RIGHT_SELECT:
3462 case LFUN_CHAR_RIGHT:
3463 case LFUN_CHAR_LEFT_SELECT:
3464 case LFUN_CHAR_LEFT: {
3465 // determine whether we move to next or previous cell, where to enter
3466 // the new cell from, and which command to "finish" (i.e., exit the
3469 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3470 FuncCode finish_lfun;
3472 if (cmd.action == LFUN_CHAR_FORWARD
3473 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3475 finish_lfun = LFUN_FINISHED_FORWARD;
3477 else if (cmd.action == LFUN_CHAR_BACKWARD
3478 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3480 finish_lfun = LFUN_FINISHED_BACKWARD;
3482 // LEFT or RIGHT commands --- the interpretation will depend on the
3483 // table's direction.
3485 bool const right = cmd.action == LFUN_CHAR_RIGHT
3486 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3487 next_cell = isRightToLeft(cur) != right;
3489 if (lyxrc.visual_cursor)
3490 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3492 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3495 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3496 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3497 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3498 cmd.action == LFUN_CHAR_LEFT_SELECT;
3500 // If we have a multicell selection or we're
3501 // not doing some LFUN_*_SELECT thing anyway...
3502 if (!cur.selIsMultiCell() || !select) {
3503 col_type const c = tabular.cellColumn(cur.idx());
3504 row_type const r = tabular.cellRow(cur.idx());
3505 // Are we trying to select the whole cell and is the whole cell
3506 // not yet selected?
3507 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3508 ((next_cell && cur.pit() == cur.lastpit()
3509 && cur.pos() == cur.lastpos())
3510 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3512 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3514 // ...try to dispatch to the cell's inset.
3515 cell(cur.idx())->dispatch(cur, cmd);
3517 // When we already have a selection we want to select the whole cell
3518 // before going to the next cell.
3519 if (select_whole && !empty_cell){
3520 getText(cur.idx())->selectAll(cur);
3525 // FIXME: When we support the selection of an empty cell, remove
3526 // the !empty_cell from this condition. For now we jump to the next
3527 // cell if the current cell is empty.
3528 if (cur.result().dispatched() && !empty_cell)
3532 // move to next/prev cell, as appropriate
3533 // note that we will always do this if we're selecting and we have
3534 // a multicell selection
3535 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3536 << " cell from: " << int(entry_from));
3538 moveNextCell(cur, entry_from);
3540 movePrevCell(cur, entry_from);
3541 // if we're exiting the table, call the appropriate FINISHED lfun
3542 if (sl == cur.top())
3543 cmd = FuncRequest(finish_lfun);
3550 case LFUN_DOWN_SELECT:
3552 if (!(cur.selection() && cur.selIsMultiCell()))
3553 cell(cur.idx())->dispatch(cur, cmd);
3555 cur.dispatched(); // override the cell's decision
3556 if (sl == cur.top()) {
3557 // if our Text didn't do anything to the cursor
3558 // then we try to put the cursor into the cell below
3559 // setting also the right targetX.
3560 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3561 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3562 cur.idx() = tabular.cellBelow(cur.idx());
3564 TextMetrics const & tm =
3565 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3566 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3569 if (sl == cur.top()) {
3570 // we trick it to go to forward after leaving the
3572 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3575 if (cur.selIsMultiCell()) {
3576 cur.pit() = cur.lastpit();
3577 cur.pos() = cur.lastpos();
3582 case LFUN_UP_SELECT:
3584 if (!(cur.selection() && cur.selIsMultiCell()))
3585 cell(cur.idx())->dispatch(cur, cmd);
3586 cur.dispatched(); // override the cell's decision
3587 if (sl == cur.top()) {
3588 // if our Text didn't do anything to the cursor
3589 // then we try to put the cursor into the cell above
3590 // setting also the right targetX.
3591 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3592 if (tabular.cellRow(cur.idx()) != 0) {
3593 cur.idx() = tabular.cellAbove(cur.idx());
3594 cur.pit() = cur.lastpit();
3595 Text const * text = cell(cur.idx())->getText(0);
3596 TextMetrics const & tm = cur.bv().textMetrics(text);
3597 ParagraphMetrics const & pm =
3598 tm.parMetrics(cur.lastpit());
3599 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3602 if (sl == cur.top()) {
3603 cmd = FuncRequest(LFUN_UP);
3606 if (cur.selIsMultiCell()) {
3608 cur.pos() = cur.lastpos();
3613 // case LFUN_SCREEN_DOWN: {
3614 // //if (hasSelection())
3615 // // cur.selection() = false;
3616 // col_type const col = tabular.cellColumn(cur.idx());
3617 // int const t = cur.bv().top_y() + cur.bv().height();
3618 // if (t < yo() + tabular.getHeightOfTabular()) {
3619 // cur.bv().scrollDocView(t);
3620 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3622 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3629 // case LFUN_SCREEN_UP: {
3630 // //if (hasSelection())
3631 // // cur.selection() = false;
3632 // col_type const col = tabular.cellColumn(cur.idx());
3633 // int const t = cur.bv().top_y() + cur.bv().height();
3635 // cur.bv().scrollDocView(t);
3639 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3643 // cur.par() = cur.lastpar();
3644 // cur.pos() = cur.lastpos();
3648 case LFUN_LAYOUT_TABULAR:
3649 cur.bv().showDialog("tabular", params2string(*this), this);
3652 case LFUN_INSET_DIALOG_UPDATE:
3653 cur.bv().updateDialog("tabular", params2string(*this));
3656 case LFUN_TABULAR_FEATURE:
3657 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3661 // insert file functions
3662 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3663 case LFUN_FILE_INSERT_PLAINTEXT:
3665 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3666 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3667 FileName(to_utf8(cmd.argument())));
3670 cur.recordUndoInset(INSERT_UNDO);
3671 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3672 // content has been replaced,
3673 // so cursor might be invalid
3674 cur.pos() = cur.lastpos();
3675 cur.pit() = cur.lastpit();
3676 bvcur.setCursor(cur);
3683 if (cur.selIsMultiCell()) {
3684 if (copySelection(cur)) {
3685 cur.recordUndoInset(DELETE_UNDO);
3689 cell(cur.idx())->dispatch(cur, cmd);
3692 case LFUN_SELF_INSERT:
3693 if (cur.selIsMultiCell()) {
3694 cur.recordUndoInset(DELETE_UNDO);
3697 cell(cur.idx())->dispatch(cur, cmd);
3700 case LFUN_CHAR_DELETE_BACKWARD:
3701 case LFUN_CHAR_DELETE_FORWARD:
3702 if (cur.selIsMultiCell()) {
3703 cur.recordUndoInset(DELETE_UNDO);
3706 cell(cur.idx())->dispatch(cur, cmd);
3710 if (!cur.selection())
3712 if (cur.selIsMultiCell()) {
3716 cell(cur.idx())->dispatch(cur, cmd);
3719 case LFUN_CLIPBOARD_PASTE:
3720 case LFUN_PRIMARY_SELECTION_PASTE: {
3721 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3722 theClipboard().getAsText() :
3723 theSelection().get();
3726 // pass to InsertPlaintextString, but
3727 // only if we have multi-cell content
3728 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3729 cur.recordUndoInset(INSERT_UNDO);
3730 if (insertPlaintextString(cur.bv(), clip, false)) {
3731 // content has been replaced,
3732 // so cursor might be invalid
3733 cur.pos() = cur.lastpos();
3734 cur.pit() = cur.lastpit();
3735 bvcur.setCursor(cur);
3739 // Let the cell handle normal text
3740 cell(cur.idx())->dispatch(cur, cmd);
3745 if (!tabularStackDirty()) {
3746 if (!cur.selIsMultiCell())
3747 cell(cur.idx())->dispatch(cur, cmd);
3750 if (theClipboard().isInternal() ||
3751 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3752 cur.recordUndoInset(INSERT_UNDO);
3753 pasteClipboard(cur);
3757 case LFUN_FONT_EMPH:
3758 case LFUN_FONT_BOLD:
3759 case LFUN_FONT_BOLDSYMBOL:
3760 case LFUN_FONT_ROMAN:
3761 case LFUN_FONT_NOUN:
3762 case LFUN_FONT_ITAL:
3763 case LFUN_FONT_FRAK:
3764 case LFUN_FONT_TYPEWRITER:
3765 case LFUN_FONT_SANS:
3766 case LFUN_TEXTSTYLE_APPLY:
3767 case LFUN_TEXTSTYLE_UPDATE:
3768 case LFUN_FONT_SIZE:
3769 case LFUN_FONT_UNDERLINE:
3770 case LFUN_FONT_STRIKEOUT:
3771 case LFUN_FONT_UULINE:
3772 case LFUN_FONT_UWAVE:
3774 case LFUN_WORD_CAPITALIZE:
3775 case LFUN_WORD_UPCASE:
3776 case LFUN_WORD_LOWCASE:
3777 case LFUN_CHARS_TRANSPOSE:
3778 if (cur.selIsMultiCell()) {
3781 getSelection(cur, rs, re, cs, ce);
3782 Cursor tmpcur = cur;
3783 for (row_type i = rs; i <= re; ++i) {
3784 for (col_type j = cs; j <= ce; ++j) {
3785 // cursor follows cell:
3786 tmpcur.idx() = tabular.cellIndex(i, j);
3787 // select this cell only:
3790 tmpcur.resetAnchor();
3791 tmpcur.pit() = tmpcur.lastpit();
3792 tmpcur.pos() = tmpcur.top().lastpos();
3793 tmpcur.setCursor(tmpcur);
3794 tmpcur.setSelection();
3795 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3800 cell(cur.idx())->dispatch(cur, cmd);
3804 case LFUN_INSET_SETTINGS:
3805 // relay this lfun to Inset, not to the cell.
3806 Inset::doDispatch(cur, cmd);
3810 // we try to handle this event in the insets dispatch function.
3811 cell(cur.idx())->dispatch(cur, cmd);
3817 // function sets an object as defined in func_status.h:
3818 // states OK, Unknown, Disabled, On, Off.
3819 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3820 FuncStatus & status) const
3822 switch (cmd.action) {
3823 case LFUN_TABULAR_FEATURE: {
3824 int action = Tabular::LAST_ACTION;
3826 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3827 string const tmp = tabularFeature[i].feature;
3828 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3829 action = tabularFeature[i].action;
3833 if (action == Tabular::LAST_ACTION) {
3835 status.unknown(true);
3839 string const argument
3840 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3842 row_type sel_row_start = 0;
3843 row_type sel_row_end = 0;
3844 col_type sel_col_start = 0;
3845 col_type sel_col_end = 0;
3846 Tabular::ltType dummyltt;
3849 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3852 case Tabular::SET_PWIDTH:
3853 case Tabular::SET_MPWIDTH:
3854 case Tabular::SET_SPECIAL_COLUMN:
3855 case Tabular::SET_SPECIAL_MULTI:
3856 case Tabular::APPEND_ROW:
3857 case Tabular::APPEND_COLUMN:
3858 case Tabular::DELETE_ROW:
3859 case Tabular::DELETE_COLUMN:
3860 case Tabular::COPY_ROW:
3861 case Tabular::COPY_COLUMN:
3862 case Tabular::SET_TOP_SPACE:
3863 case Tabular::SET_BOTTOM_SPACE:
3864 case Tabular::SET_INTERLINE_SPACE:
3868 case Tabular::MULTICOLUMN:
3869 // If a row is set as longtable caption, it must not be allowed
3870 // to unset that this row is a multicolumn.
3871 status.setEnabled(sel_row_start == sel_row_end
3872 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
3873 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3876 case Tabular::SET_ALL_LINES:
3877 case Tabular::UNSET_ALL_LINES:
3878 case Tabular::SET_BORDER_LINES:
3879 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3882 case Tabular::TOGGLE_LINE_TOP:
3883 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3884 status.setOnOff(tabular.topLine(cur.idx()));
3887 case Tabular::TOGGLE_LINE_BOTTOM:
3888 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3889 status.setOnOff(tabular.bottomLine(cur.idx()));
3892 case Tabular::TOGGLE_LINE_LEFT:
3893 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3894 status.setOnOff(tabular.leftLine(cur.idx()));
3897 case Tabular::TOGGLE_LINE_RIGHT:
3898 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3899 status.setOnOff(tabular.rightLine(cur.idx()));
3902 case Tabular::M_ALIGN_LEFT:
3904 case Tabular::ALIGN_LEFT:
3905 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3908 case Tabular::M_ALIGN_RIGHT:
3910 case Tabular::ALIGN_RIGHT:
3911 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3914 case Tabular::M_ALIGN_CENTER:
3916 case Tabular::ALIGN_CENTER:
3917 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3920 case Tabular::ALIGN_BLOCK:
3921 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3922 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3925 case Tabular::M_VALIGN_TOP:
3927 case Tabular::VALIGN_TOP:
3929 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3932 case Tabular::M_VALIGN_BOTTOM:
3934 case Tabular::VALIGN_BOTTOM:
3936 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3939 case Tabular::M_VALIGN_MIDDLE:
3941 case Tabular::VALIGN_MIDDLE:
3943 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3946 case Tabular::SET_LONGTABULAR:
3947 status.setOnOff(tabular.is_long_tabular);
3950 case Tabular::UNSET_LONGTABULAR:
3951 status.setOnOff(!tabular.is_long_tabular);
3954 case Tabular::TOGGLE_ROTATE_TABULAR:
3955 case Tabular::SET_ROTATE_TABULAR:
3956 status.setOnOff(tabular.rotate);
3959 case Tabular::TABULAR_VALIGN_TOP:
3960 status.setOnOff(tabular.tabular_valignment
3961 == Tabular::LYX_VALIGN_TOP);
3963 case Tabular::TABULAR_VALIGN_MIDDLE:
3964 status.setOnOff(tabular.tabular_valignment
3965 == Tabular::LYX_VALIGN_MIDDLE);
3967 case Tabular::TABULAR_VALIGN_BOTTOM:
3968 status.setOnOff(tabular.tabular_valignment
3969 == Tabular::LYX_VALIGN_BOTTOM);
3972 case Tabular::LONGTABULAR_ALIGN_LEFT:
3973 status.setOnOff(tabular.longtabular_alignment
3974 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
3976 case Tabular::LONGTABULAR_ALIGN_CENTER:
3977 status.setOnOff(tabular.longtabular_alignment
3978 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
3980 case Tabular::LONGTABULAR_ALIGN_RIGHT:
3981 status.setOnOff(tabular.longtabular_alignment
3982 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
3985 case Tabular::UNSET_ROTATE_TABULAR:
3986 status.setOnOff(!tabular.rotate);
3989 case Tabular::TOGGLE_ROTATE_CELL:
3990 case Tabular::SET_ROTATE_CELL:
3991 status.setOnOff(!oneCellHasRotationState(false,
3992 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3995 case Tabular::UNSET_ROTATE_CELL:
3996 status.setOnOff(!oneCellHasRotationState(true,
3997 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4000 case Tabular::SET_USEBOX:
4001 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4004 // every row can only be one thing:
4005 // either a footer or header or caption
4006 case Tabular::SET_LTFIRSTHEAD:
4007 status.setEnabled(sel_row_start == sel_row_end
4008 && !tabular.ltCaption(sel_row_start));
4009 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4012 case Tabular::UNSET_LTFIRSTHEAD:
4013 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4016 case Tabular::SET_LTHEAD:
4017 status.setEnabled(sel_row_start == sel_row_end
4018 && !tabular.ltCaption(sel_row_start));
4019 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4022 case Tabular::UNSET_LTHEAD:
4023 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4026 case Tabular::SET_LTFOOT:
4027 status.setEnabled(sel_row_start == sel_row_end
4028 && !tabular.ltCaption(sel_row_start));
4029 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4032 case Tabular::UNSET_LTFOOT:
4033 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4036 case Tabular::SET_LTLASTFOOT:
4037 status.setEnabled(sel_row_start == sel_row_end
4038 && !tabular.ltCaption(sel_row_start));
4039 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4042 case Tabular::UNSET_LTLASTFOOT:
4043 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4046 case Tabular::SET_LTNEWPAGE:
4047 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4050 // only one row can be the caption
4051 case Tabular::TOGGLE_LTCAPTION:
4052 status.setEnabled(sel_row_start == sel_row_end
4053 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4054 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4055 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4056 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4057 && (!tabular.haveLTCaption()
4058 || tabular.ltCaption(sel_row_start)));
4059 status.setOnOff(tabular.ltCaption(sel_row_start));
4062 case Tabular::SET_BOOKTABS:
4063 status.setOnOff(tabular.use_booktabs);
4066 case Tabular::UNSET_BOOKTABS:
4067 status.setOnOff(!tabular.use_booktabs);
4072 status.setEnabled(false);
4078 // These are only enabled inside tabular
4079 case LFUN_CELL_BACKWARD:
4080 case LFUN_CELL_FORWARD:
4081 status.setEnabled(true);
4084 // disable these with multiple cells selected
4085 case LFUN_INSET_INSERT:
4086 case LFUN_TABULAR_INSERT:
4087 case LFUN_FLEX_INSERT:
4088 case LFUN_FLOAT_INSERT:
4089 case LFUN_FLOAT_WIDE_INSERT:
4090 case LFUN_FOOTNOTE_INSERT:
4091 case LFUN_MARGINALNOTE_INSERT:
4092 case LFUN_MATH_INSERT:
4093 case LFUN_MATH_MODE:
4094 case LFUN_MATH_MUTATE:
4095 case LFUN_MATH_DISPLAY:
4096 case LFUN_NOTE_INSERT:
4097 case LFUN_OPTIONAL_INSERT:
4098 case LFUN_BOX_INSERT:
4099 case LFUN_BRANCH_INSERT:
4100 case LFUN_PHANTOM_INSERT:
4101 case LFUN_WRAP_INSERT:
4102 case LFUN_ERT_INSERT: {
4103 if (cur.selIsMultiCell()) {
4104 status.setEnabled(false);
4107 return cell(cur.idx())->getStatus(cur, cmd, status);
4110 // disable in non-fixed-width cells
4111 case LFUN_NEWLINE_INSERT:
4112 case LFUN_BREAK_PARAGRAPH: {
4113 if (tabular.getPWidth(cur.idx()).zero()) {
4114 status.setEnabled(false);
4117 return cell(cur.idx())->getStatus(cur, cmd, status);
4120 case LFUN_NEWPAGE_INSERT:
4121 status.setEnabled(false);
4125 if (tabularStackDirty() && theClipboard().isInternal()) {
4126 if (cur.selIsMultiCell()) {
4129 getSelection(cur, rs, re, cs, ce);
4130 if (paste_tabular && paste_tabular->column_info.size() == ce - cs + 1
4131 && paste_tabular->row_info.size() == re - rs + 1)
4132 status.setEnabled(true);
4134 status.setEnabled(false);
4135 status.message(_("Selection size should match clipboard content."));
4138 status.setEnabled(true);
4141 return cell(cur.idx())->getStatus(cur, cmd, status);
4143 case LFUN_INSET_SETTINGS:
4144 // relay this lfun to Inset, not to the cell.
4145 return Inset::getStatus(cur, cmd, status);
4147 case LFUN_INSET_MODIFY:
4148 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
4149 status.setEnabled(true);
4155 // we try to handle this event in the insets dispatch function.
4156 return cell(cur.idx())->getStatus(cur, cmd, status);
4161 Inset::DisplayType InsetTabular::display() const
4163 if (tabular.is_long_tabular) {
4164 switch (tabular.longtabular_alignment) {
4165 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4167 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4169 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4179 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4181 return tabular.latex(os, runparams);
4185 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4187 os << '\n'; // output table on a new line
4188 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4189 tabular.plaintext(os, runparams, dp, false, 0);
4190 return PLAINTEXT_NEWLINE;
4194 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4199 // FIXME: Why not pass a proper DocIterator here?
4201 // if the table is inside a float it doesn't need the informaltable
4202 // wrapper. Search for it.
4203 for (master = owner(); master; master = master->owner())
4204 if (master->lyxCode() == FLOAT_CODE)
4209 os << "<informaltable>";
4212 ret += tabular.docbook(os, runparams);
4214 os << "</informaltable>";
4221 void InsetTabular::validate(LaTeXFeatures & features) const
4223 tabular.validate(features);
4227 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4229 return tabular.cellInset(idx);
4233 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4235 return tabular.cellInset(idx);
4239 void InsetTabular::cursorPos(BufferView const & bv,
4240 CursorSlice const & sl, bool boundary, int & x, int & y) const
4242 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4244 // y offset correction
4245 int const row = tabular.cellRow(sl.idx());
4246 for (int i = 0; i <= row; ++i) {
4248 y += tabular.rowAscent(i);
4249 y += tabular.getAdditionalHeight(i);
4252 y += tabular.rowDescent(i);
4255 // x offset correction
4256 int const col = tabular.cellColumn(sl.idx());
4257 int idx = tabular.cellIndex(row, 0);
4258 for (int j = 0; j < col; ++j) {
4259 if (tabular.isPartOfMultiColumn(row, j))
4261 x += tabular.columnWidth(idx);
4264 x += tabular.getBeginningOfTextInCell(idx);
4265 x += ADD_TO_TABULAR_WIDTH;
4270 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4274 Inset const & inset = *tabular.cellInset(cell);
4275 Point o = bv.coordCache().getInsets().xy(&inset);
4276 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
4277 int const xend = xbeg + tabular.columnWidth(cell);
4278 row_type const row = tabular.cellRow(cell);
4279 int const ybeg = o.y_ - tabular.rowAscent(row) -
4280 tabular.getAdditionalHeight(row);
4281 int const yend = o.y_ + tabular.rowDescent(row);
4293 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4294 // << " ybeg=" << ybeg << " yend=" << yend
4295 // << " xx=" << xx << " yy=" << yy
4296 // << " dist=" << xx + yy << endl;
4301 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4303 //lyxerr << "InsetTabular::editXY: " << this << endl;
4304 cur.setSelection(false);
4306 cur.idx() = getNearestCell(cur.bv(), x, y);
4308 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4312 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4314 cur.idx() = getNearestCell(cur.bv(), x, y);
4315 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4319 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4321 idx_type idx_min = 0;
4322 int dist_min = numeric_limits<int>::max();
4323 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4324 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4325 int const d = dist(bv, i, x, y);
4336 int InsetTabular::cellXPos(idx_type const cell) const
4340 for (; !tabular.isFirstCellInRow(c); --c)
4343 for (; c < cell; ++c)
4344 lx += tabular.columnWidth(c);
4350 void InsetTabular::resetPos(Cursor & cur) const
4352 BufferView & bv = cur.bv();
4353 int const maxwidth = bv.workWidth();
4355 int const scx_old = scx_;
4356 int const i = cur.find(this);
4361 int const X2 = maxwidth;
4362 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4363 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4364 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4367 scx_ = X1 + 20 - x1;
4369 scx_ = X2 - 20 - x2;
4374 // only update if offset changed
4375 if (scx_ != scx_old)
4376 cur.updateFlags(Update::Force | Update::FitCursor);
4380 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4382 if (isRightToLeft(cur)) {
4383 if (tabular.isFirstCellInRow(cur.idx())) {
4384 row_type const row = tabular.cellRow(cur.idx());
4385 if (row == tabular.row_info.size() - 1)
4387 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4394 if (tabular.isLastCell(cur.idx()))
4399 cur.boundary(false);
4401 if (cur.selIsMultiCell()) {
4402 cur.pit() = cur.lastpit();
4403 cur.pos() = cur.lastpos();
4411 // in visual mode, place cursor at extreme left or right
4413 switch(entry_from) {
4415 case ENTRY_DIRECTION_RIGHT:
4416 cur.posVisToRowExtremity(false /* !left */);
4418 case ENTRY_DIRECTION_LEFT:
4419 cur.posVisToRowExtremity(true /* left */);
4421 case ENTRY_DIRECTION_IGNORE:
4422 // nothing to do in this case
4431 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4433 if (isRightToLeft(cur)) {
4434 if (tabular.isLastCellInRow(cur.idx())) {
4435 row_type const row = tabular.cellRow(cur.idx());
4438 cur.idx() = tabular.getFirstCellInRow(row);
4439 cur.idx() = tabular.cellAbove(cur.idx());
4441 if (tabular.isLastCell(cur.idx()))
4446 if (cur.idx() == 0) // first cell
4451 if (cur.selIsMultiCell()) {
4452 cur.pit() = cur.lastpit();
4453 cur.pos() = cur.lastpos();
4458 cur.pit() = cur.lastpit();
4459 cur.pos() = cur.lastpos();
4461 // in visual mode, place cursor at extreme left or right
4463 switch(entry_from) {
4465 case ENTRY_DIRECTION_RIGHT:
4466 cur.posVisToRowExtremity(false /* !left */);
4468 case ENTRY_DIRECTION_LEFT:
4469 cur.posVisToRowExtremity(true /* left */);
4471 case ENTRY_DIRECTION_IGNORE:
4472 // nothing to do in this case
4481 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4483 Tabular::Feature action = Tabular::LAST_ACTION;
4486 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4487 string const tmp = tabularFeature[i].feature;
4489 if (tmp == what.substr(0, tmp.length())) {
4490 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4491 //tabularFeatures[i].feature.length()))
4492 action = tabularFeature[i].action;
4496 if (action == Tabular::LAST_ACTION)
4500 ltrim(what.substr(tabularFeature[i].feature.length()));
4501 tabularFeatures(cur, action, val);
4506 static void checkLongtableSpecial(Tabular::ltType & ltt,
4507 string const & special, bool & flag)
4509 if (special == "dl_above") {
4512 } else if (special == "dl_below") {
4513 ltt.bottomDL = flag;
4515 } else if (special == "empty") {
4524 bool InsetTabular::oneCellHasRotationState(bool rotated,
4525 row_type row_start, row_type row_end,
4526 col_type col_start, col_type col_end) const {
4528 for (row_type i = row_start; i <= row_end; ++i) {
4529 for (col_type j = col_start; j <= col_end; ++j) {
4530 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4539 void InsetTabular::tabularFeatures(Cursor & cur,
4540 Tabular::Feature feature, string const & value)
4542 col_type sel_col_start;
4543 col_type sel_col_end;
4544 row_type sel_row_start;
4545 row_type sel_row_end;
4546 bool setLines = false;
4547 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4548 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4552 case Tabular::M_ALIGN_LEFT:
4553 case Tabular::ALIGN_LEFT:
4554 setAlign = LYX_ALIGN_LEFT;
4557 case Tabular::M_ALIGN_RIGHT:
4558 case Tabular::ALIGN_RIGHT:
4559 setAlign = LYX_ALIGN_RIGHT;
4562 case Tabular::M_ALIGN_CENTER:
4563 case Tabular::ALIGN_CENTER:
4564 setAlign = LYX_ALIGN_CENTER;
4567 case Tabular::ALIGN_BLOCK:
4568 setAlign = LYX_ALIGN_BLOCK;
4571 case Tabular::M_VALIGN_TOP:
4572 case Tabular::VALIGN_TOP:
4573 setVAlign = Tabular::LYX_VALIGN_TOP;
4576 case Tabular::M_VALIGN_BOTTOM:
4577 case Tabular::VALIGN_BOTTOM:
4578 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4581 case Tabular::M_VALIGN_MIDDLE:
4582 case Tabular::VALIGN_MIDDLE:
4583 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4590 cur.recordUndoInset(ATOMIC_UNDO);
4592 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4593 row_type const row = tabular.cellRow(cur.idx());
4594 col_type const column = tabular.cellColumn(cur.idx());
4596 Tabular::ltType ltt;
4600 case Tabular::SET_PWIDTH: {
4601 Length const len(value);
4602 tabular.setColumnPWidth(cur, cur.idx(), len);
4604 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4605 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4609 case Tabular::SET_MPWIDTH:
4610 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4613 case Tabular::SET_SPECIAL_COLUMN:
4614 case Tabular::SET_SPECIAL_MULTI:
4615 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4618 case Tabular::APPEND_ROW:
4619 // append the row into the tabular
4620 tabular.appendRow(cur.idx());
4623 case Tabular::APPEND_COLUMN:
4624 // append the column into the tabular
4625 tabular.appendColumn(cur.idx());
4626 cur.idx() = tabular.cellIndex(row, column);
4629 case Tabular::DELETE_ROW:
4630 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4631 tabular.deleteRow(sel_row_start);
4632 if (sel_row_start >= tabular.row_info.size())
4634 cur.idx() = tabular.cellIndex(sel_row_start, column);
4637 cur.setSelection(false);
4640 case Tabular::DELETE_COLUMN:
4641 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4642 tabular.deleteColumn(sel_col_start);
4643 if (sel_col_start >= tabular.column_info.size())
4645 cur.idx() = tabular.cellIndex(row, sel_col_start);
4648 cur.setSelection(false);
4651 case Tabular::COPY_ROW:
4652 tabular.copyRow(row);
4655 case Tabular::COPY_COLUMN:
4656 tabular.copyColumn(column);
4657 cur.idx() = tabular.cellIndex(row, column);
4660 case Tabular::TOGGLE_LINE_TOP: {
4661 bool lineSet = !tabular.topLine(cur.idx());
4662 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4663 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4664 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4668 case Tabular::TOGGLE_LINE_BOTTOM: {
4669 bool lineSet = !tabular.bottomLine(cur.idx());
4670 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4671 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4672 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4676 case Tabular::TOGGLE_LINE_LEFT: {
4677 bool lineSet = !tabular.leftLine(cur.idx());
4678 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4679 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4680 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4684 case Tabular::TOGGLE_LINE_RIGHT: {
4685 bool lineSet = !tabular.rightLine(cur.idx());
4686 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4687 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4688 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4692 case Tabular::M_ALIGN_LEFT:
4693 case Tabular::M_ALIGN_RIGHT:
4694 case Tabular::M_ALIGN_CENTER:
4696 case Tabular::ALIGN_LEFT:
4697 case Tabular::ALIGN_RIGHT:
4698 case Tabular::ALIGN_CENTER:
4699 case Tabular::ALIGN_BLOCK:
4700 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4701 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4702 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4705 case Tabular::M_VALIGN_TOP:
4706 case Tabular::M_VALIGN_BOTTOM:
4707 case Tabular::M_VALIGN_MIDDLE:
4709 case Tabular::VALIGN_TOP:
4710 case Tabular::VALIGN_BOTTOM:
4711 case Tabular::VALIGN_MIDDLE:
4712 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4713 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4714 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4717 case Tabular::MULTICOLUMN: {
4718 if (!cur.selection()) {
4719 // just multicol for one single cell
4720 // check whether we are completely in a multicol
4721 if (tabular.isMultiColumn(cur.idx()))
4722 tabular.unsetMultiColumn(cur.idx());
4724 tabular.setMultiColumn(cur.idx(), 1);
4727 // we have a selection so this means we just add all this
4728 // cells to form a multicolumn cell
4729 idx_type const s_start = cur.selBegin().idx();
4730 idx_type const s_end = cur.selEnd().idx();
4731 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4732 cur.idx() = s_start;
4735 cur.setSelection(false);
4739 case Tabular::SET_ALL_LINES:
4741 case Tabular::UNSET_ALL_LINES:
4742 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4743 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4744 tabular.setAllLines(
4745 tabular.cellIndex(i,j), setLines);
4748 case Tabular::SET_BORDER_LINES:
4749 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4750 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4751 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4753 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4754 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4755 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4759 case Tabular::SET_LONGTABULAR:
4760 tabular.is_long_tabular = true;
4763 case Tabular::UNSET_LONGTABULAR:
4764 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
4765 if (tabular.ltCaption(i)) {
4766 cur.idx() = tabular.cellIndex(i, 0);
4769 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
4772 tabular.is_long_tabular = false;
4775 case Tabular::SET_ROTATE_TABULAR:
4776 tabular.rotate = true;
4779 case Tabular::UNSET_ROTATE_TABULAR:
4780 tabular.rotate = false;
4783 case Tabular::TOGGLE_ROTATE_TABULAR:
4784 tabular.rotate = !tabular.rotate;
4787 case Tabular::TABULAR_VALIGN_TOP:
4788 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
4791 case Tabular::TABULAR_VALIGN_MIDDLE:
4792 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
4795 case Tabular::TABULAR_VALIGN_BOTTOM:
4796 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
4799 case Tabular::LONGTABULAR_ALIGN_LEFT:
4800 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
4803 case Tabular::LONGTABULAR_ALIGN_CENTER:
4804 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
4807 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4808 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
4813 case Tabular::SET_ROTATE_CELL:
4814 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4815 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4816 tabular.setRotateCell(
4817 tabular.cellIndex(i, j), true);
4820 case Tabular::UNSET_ROTATE_CELL:
4821 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4822 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4823 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4826 case Tabular::TOGGLE_ROTATE_CELL:
4828 bool oneNotRotated = oneCellHasRotationState(false,
4829 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4831 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4832 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4833 tabular.setRotateCell(tabular.cellIndex(i, j),
4838 case Tabular::SET_USEBOX: {
4839 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4840 if (val == tabular.getUsebox(cur.idx()))
4841 val = Tabular::BOX_NONE;
4842 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4843 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4844 tabular.setUsebox(tabular.cellIndex(i, j), val);
4848 case Tabular::UNSET_LTFIRSTHEAD:
4850 case Tabular::SET_LTFIRSTHEAD:
4851 tabular.getRowOfLTFirstHead(row, ltt);
4852 checkLongtableSpecial(ltt, value, flag);
4853 tabular.setLTHead(row, flag, ltt, true);
4856 case Tabular::UNSET_LTHEAD:
4858 case Tabular::SET_LTHEAD:
4859 tabular.getRowOfLTHead(row, ltt);
4860 checkLongtableSpecial(ltt, value, flag);
4861 tabular.setLTHead(row, flag, ltt, false);
4864 case Tabular::UNSET_LTFOOT:
4866 case Tabular::SET_LTFOOT:
4867 tabular.getRowOfLTFoot(row, ltt);
4868 checkLongtableSpecial(ltt, value, flag);
4869 tabular.setLTFoot(row, flag, ltt, false);
4872 case Tabular::UNSET_LTLASTFOOT:
4874 case Tabular::SET_LTLASTFOOT:
4875 tabular.getRowOfLTLastFoot(row, ltt);
4876 checkLongtableSpecial(ltt, value, flag);
4877 tabular.setLTFoot(row, flag, ltt, true);
4880 case Tabular::SET_LTNEWPAGE:
4881 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4884 case Tabular::TOGGLE_LTCAPTION: {
4885 bool const set = !tabular.ltCaption(row);
4886 cur.idx() = tabular.setLTCaption(row, set);
4889 cur.setSelection(false);
4892 // When a row is set as caption, then also insert
4893 // a caption. Otherwise the LaTeX output is broken.
4894 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
4895 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
4897 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
4898 if (lyx::getStatus(fr).enabled())
4904 case Tabular::SET_BOOKTABS:
4905 tabular.use_booktabs = true;
4908 case Tabular::UNSET_BOOKTABS:
4909 tabular.use_booktabs = false;
4912 case Tabular::SET_TOP_SPACE: {
4914 if (value == "default")
4915 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4916 tabular.row_info[i].top_space_default = true;
4917 else if (isValidLength(value, &len))
4918 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4919 tabular.row_info[i].top_space_default = false;
4920 tabular.row_info[i].top_space = len;
4923 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4924 tabular.row_info[i].top_space_default = false;
4925 tabular.row_info[i].top_space = len;
4930 case Tabular::SET_BOTTOM_SPACE: {
4932 if (value == "default")
4933 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4934 tabular.row_info[i].bottom_space_default = true;
4935 else if (isValidLength(value, &len))
4936 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4937 tabular.row_info[i].bottom_space_default = false;
4938 tabular.row_info[i].bottom_space = len;
4941 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4942 tabular.row_info[i].bottom_space_default = false;
4943 tabular.row_info[i].bottom_space = len;
4948 case Tabular::SET_INTERLINE_SPACE: {
4950 if (value == "default")
4951 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4952 tabular.row_info[i].interline_space_default = true;
4953 else if (isValidLength(value, &len))
4954 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4955 tabular.row_info[i].interline_space_default = false;
4956 tabular.row_info[i].interline_space = len;
4959 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4960 tabular.row_info[i].interline_space_default = false;
4961 tabular.row_info[i].interline_space = len;
4966 // dummy stuff just to avoid warnings
4967 case Tabular::LAST_ACTION:
4973 bool InsetTabular::showInsetDialog(BufferView * bv) const
4975 bv->showDialog("tabular", params2string(*this),
4976 const_cast<InsetTabular *>(this));
4981 void InsetTabular::openLayoutDialog(BufferView * bv) const
4983 bv->showDialog("tabular", params2string(*this),
4984 const_cast<InsetTabular *>(this));
4988 bool InsetTabular::copySelection(Cursor & cur)
4990 if (!cur.selection())
4995 getSelection(cur, rs, re, cs, ce);
4997 paste_tabular.reset(new Tabular(tabular));
4999 for (row_type i = 0; i < rs; ++i)
5000 paste_tabular->deleteRow(0);
5002 row_type const rows = re - rs + 1;
5003 while (paste_tabular->row_info.size() > rows)
5004 paste_tabular->deleteRow(rows);
5006 for (col_type i = 0; i < cs; ++i)
5007 paste_tabular->deleteColumn(0);
5009 col_type const columns = ce - cs + 1;
5010 while (paste_tabular->column_info.size() > columns)
5011 paste_tabular->deleteColumn(columns);
5013 paste_tabular->setBuffer(tabular.buffer());
5015 odocstringstream os;
5016 OutputParams const runparams(0);
5017 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5018 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5019 cap::copySelection(cur, os.str());
5021 // mark tabular stack dirty
5022 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5023 // when we (hopefully) have a one-for-all paste mechanism.
5024 // This must be called after cap::copySelection.
5025 dirtyTabularStack(true);
5031 bool InsetTabular::pasteClipboard(Cursor & cur)
5035 col_type actcol = tabular.cellColumn(cur.idx());
5036 row_type actrow = tabular.cellRow(cur.idx());
5038 if (cur.selIsMultiCell()) {
5041 getSelection(cur, actrow, re, actcol, ce);
5044 for (row_type r1 = 0, r2 = actrow;
5045 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
5047 for (col_type c1 = 0, c2 = actcol;
5048 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
5050 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5051 tabular.isPartOfMultiColumn(r2, c2))
5053 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5057 if (tabular.isPartOfMultiColumn(r2, c2)) {
5061 shared_ptr<InsetTableCell> inset(
5062 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5063 tabular.setCellInset(r2, c2, inset);
5064 // FIXME: change tracking (MG)
5065 inset->setChange(Change(buffer().params().trackChanges ?
5066 Change::INSERTED : Change::UNCHANGED));
5074 void InsetTabular::cutSelection(Cursor & cur)
5076 if (!cur.selection())
5081 getSelection(cur, rs, re, cs, ce);
5082 for (row_type i = rs; i <= re; ++i) {
5083 for (col_type j = cs; j <= ce; ++j) {
5084 shared_ptr<InsetTableCell> t
5085 = cell(tabular.cellIndex(i, j));
5086 if (buffer().params().trackChanges)
5087 // FIXME: Change tracking (MG)
5088 t->setChange(Change(Change::DELETED));
5094 // cursor position might be invalid now
5095 if (cur.pit() > cur.lastpit())
5096 cur.pit() = cur.lastpit();
5097 if (cur.pos() > cur.lastpos())
5098 cur.pos() = cur.lastpos();
5099 cur.clearSelection();
5103 bool InsetTabular::isRightToLeft(Cursor & cur) const
5105 LASSERT(cur.depth() > 1, /**/);
5106 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5107 pos_type const parentpos = cur[cur.depth() - 2].pos();
5108 return parentpar.getFontSettings(buffer().params(),
5109 parentpos).language()->rightToLeft();
5112 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5115 LASSERT(stidx <= enidx, return docstring());
5117 col_type const col1 = tabular.cellColumn(stidx);
5118 col_type const col2 = tabular.cellColumn(enidx);
5119 row_type const row1 = tabular.cellRow(stidx);
5120 row_type const row2 = tabular.cellRow(enidx);
5122 for (col_type col = col1; col <= col2; col++)
5123 for (row_type row = row1; row <= row2; row++) {
5128 retval += tabular.cellInset(row, col)->asString(intoInsets);
5134 void InsetTabular::getSelection(Cursor & cur,
5135 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5137 CursorSlice const & beg = cur.selBegin();
5138 CursorSlice const & end = cur.selEnd();
5139 cs = tabular.cellColumn(beg.idx());
5140 ce = tabular.cellColumn(end.idx());
5143 cs = tabular.cellColumn(end.idx());
5145 ce = tabular.cellRightColumn(end.idx());
5148 rs = tabular.cellRow(beg.idx());
5149 re = tabular.cellRow(end.idx());
5155 Text * InsetTabular::getText(int idx) const
5157 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5161 void InsetTabular::setChange(Change const & change)
5163 for (idx_type idx = 0; idx < nargs(); ++idx)
5164 cell(idx)->setChange(change);
5168 void InsetTabular::acceptChanges()
5170 for (idx_type idx = 0; idx < nargs(); ++idx)
5171 cell(idx)->acceptChanges();
5175 void InsetTabular::rejectChanges()
5177 for (idx_type idx = 0; idx < nargs(); ++idx)
5178 cell(idx)->rejectChanges();
5182 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5184 return tabular.getPWidth(cell).zero();
5188 bool InsetTabular::forcePlainLayout(idx_type cell) const
5190 return !tabular.getPWidth(cell).zero();
5194 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5197 if (buf.length() <= 0)
5202 col_type maxCols = 1;
5203 size_t const len = buf.length();
5207 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5215 maxCols = max(cols, maxCols);
5221 maxCols = max(cols, maxCols);
5227 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5228 loctab = paste_tabular.get();
5230 dirtyTabularStack(true);
5233 cell = bv.cursor().idx();
5234 ocol = tabular.cellColumn(cell);
5235 row = tabular.cellRow(cell);
5239 idx_type const cells = loctab->numberofcells;
5242 rows = loctab->row_info.size();
5243 col_type const columns = loctab->column_info.size();
5245 while (cell < cells && p < len && row < rows &&
5246 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5252 // we can only set this if we are not too far right
5253 if (cols < columns) {
5254 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5255 Font const font = bv.textMetrics(&inset->text()).
5257 inset->setText(buf.substr(op, p - op), font,
5258 buffer().params().trackChanges);
5264 // we can only set this if we are not too far right
5265 if (cols < columns) {
5266 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5267 Font const font = bv.textMetrics(&inset->text()).
5269 inset->setText(buf.substr(op, p - op), font,
5270 buffer().params().trackChanges);
5275 cell = loctab->cellIndex(row, cols);
5281 // check for the last cell if there is no trailing '\n'
5282 if (cell < cells && op < len) {
5283 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5284 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5285 inset->setText(buf.substr(op, len - op), font,
5286 buffer().params().trackChanges);
5292 void InsetTabular::addPreview(DocIterator const & inset_pos,
5293 PreviewLoader & loader) const
5295 row_type const rows = tabular.row_info.size();
5296 col_type const columns = tabular.column_info.size();
5297 DocIterator cell_pos = inset_pos;
5299 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5300 for (row_type i = 0; i < rows; ++i) {
5301 for (col_type j = 0; j < columns; ++j) {
5302 cell_pos.top().idx() = tabular.cellIndex(i, j);
5303 tabular.cellInset(i, j)->addPreview(cell_pos, loader);
5309 bool InsetTabular::completionSupported(Cursor const & cur) const
5311 Cursor const & bvCur = cur.bv().cursor();
5312 if (&bvCur.inset() != this)
5314 return cur.text()->completionSupported(cur);
5318 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5320 return completionSupported(cur);
5324 bool InsetTabular::automaticInlineCompletion() const
5326 return lyxrc.completion_inline_text;
5330 bool InsetTabular::automaticPopupCompletion() const
5332 return lyxrc.completion_popup_text;
5336 bool InsetTabular::showCompletionCursor() const
5338 return lyxrc.completion_cursor_text;
5342 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5344 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5348 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5350 if (!completionSupported(cur))
5352 return cur.text()->completionPrefix(cur);
5356 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5358 if (!completionSupported(cur))
5361 return cur.text()->insertCompletion(cur, s, finished);
5365 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5366 Dimension & dim) const
5368 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5369 tm.completionPosAndDim(cur, x, y, dim);
5373 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5375 istringstream data(in);
5377 lex.setStream(data);
5384 if (!lex || token != "tabular") {
5385 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5389 // This is part of the inset proper that is usually swallowed
5390 // by Buffer::readInset
5392 if (!lex || token != "Tabular") {
5393 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5401 string InsetTabular::params2string(InsetTabular const & inset)
5404 data << "tabular" << ' ';
5406 data << "\\end_inset\n";