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 "output_xhtml.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "ParIterator.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
48 #include "frontends/Application.h"
49 #include "frontends/alert.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Painter.h"
52 #include "frontends/Selection.h"
54 #include "support/convert.h"
55 #include "support/debug.h"
56 #include "support/docstream.h"
57 #include "support/FileName.h"
58 #include "support/gettext.h"
59 #include "support/lassert.h"
60 #include "support/lstrings.h"
62 #include <boost/scoped_ptr.hpp>
70 using namespace lyx::support;
72 using boost::shared_ptr;
73 using boost::dynamic_pointer_cast;
78 using cap::dirtyTabularStack;
79 using cap::tabularStackDirty;
81 using graphics::PreviewLoader;
83 using frontend::Painter;
84 using frontend::Clipboard;
86 namespace Alert = frontend::Alert;
91 int const ADD_TO_HEIGHT = 2; // in cell
92 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
93 int const default_line_space = 10; // ?
94 int const WIDTH_OF_LINE = 5; // space between double lines
98 boost::scoped_ptr<Tabular> paste_tabular;
101 struct TabularFeature {
102 Tabular::Feature action;
107 TabularFeature tabularFeature[] =
109 { Tabular::APPEND_ROW, "append-row" },
110 { Tabular::APPEND_COLUMN, "append-column" },
111 { Tabular::DELETE_ROW, "delete-row" },
112 { Tabular::DELETE_COLUMN, "delete-column" },
113 { Tabular::COPY_ROW, "copy-row" },
114 { Tabular::COPY_COLUMN, "copy-column" },
115 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
116 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
117 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
118 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
119 { Tabular::ALIGN_LEFT, "align-left" },
120 { Tabular::ALIGN_RIGHT, "align-right" },
121 { Tabular::ALIGN_CENTER, "align-center" },
122 { Tabular::ALIGN_BLOCK, "align-block" },
123 { Tabular::VALIGN_TOP, "valign-top" },
124 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
125 { Tabular::VALIGN_MIDDLE, "valign-middle" },
126 { Tabular::M_ALIGN_LEFT, "m-align-left" },
127 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
128 { Tabular::M_ALIGN_CENTER, "m-align-center" },
129 { Tabular::M_VALIGN_TOP, "m-valign-top" },
130 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
131 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
132 { Tabular::MULTICOLUMN, "multicolumn" },
133 { Tabular::SET_ALL_LINES, "set-all-lines" },
134 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
135 { Tabular::SET_LONGTABULAR, "set-longtabular" },
136 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
137 { Tabular::SET_PWIDTH, "set-pwidth" },
138 { Tabular::SET_MPWIDTH, "set-mpwidth" },
139 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
140 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
141 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
142 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
143 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
144 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
145 { Tabular::SET_USEBOX, "set-usebox" },
146 { Tabular::SET_LTHEAD, "set-lthead" },
147 { Tabular::UNSET_LTHEAD, "unset-lthead" },
148 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
149 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
150 { Tabular::SET_LTFOOT, "set-ltfoot" },
151 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
152 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
153 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
154 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
155 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption" },
156 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
157 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
158 { Tabular::SET_BOOKTABS, "set-booktabs" },
159 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
160 { Tabular::SET_TOP_SPACE, "set-top-space" },
161 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
162 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
163 { Tabular::SET_BORDER_LINES, "set-border-lines" },
164 { Tabular::TABULAR_VALIGN_TOP, "tabular-valign-top"},
165 { Tabular::TABULAR_VALIGN_MIDDLE, "tabular-valign-middle"},
166 { Tabular::TABULAR_VALIGN_BOTTOM, "tabular-valign-bottom"},
167 { Tabular::LONGTABULAR_ALIGN_LEFT, "longtabular-align-left" },
168 { Tabular::LONGTABULAR_ALIGN_CENTER, "longtabular-align-center" },
169 { Tabular::LONGTABULAR_ALIGN_RIGHT, "longtabular-align-right" },
170 { Tabular::LAST_ACTION, "" }
174 class FeatureEqual : public unary_function<TabularFeature, bool> {
176 FeatureEqual(Tabular::Feature feature)
177 : feature_(feature) {}
178 bool operator()(TabularFeature const & tf) const {
179 return tf.action == feature_;
182 Tabular::Feature feature_;
187 string const write_attribute(string const & name, T const & t)
189 string const s = tostr(t);
190 return s.empty() ? s : " " + name + "=\"" + s + "\"";
194 string const write_attribute(string const & name, string const & t)
196 return t.empty() ? t : " " + name + "=\"" + t + "\"";
201 string const write_attribute(string const & name, docstring const & t)
203 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
208 string const write_attribute(string const & name, bool const & b)
210 // we write only true attribute values so we remove a bit of the
211 // file format bloat for tabulars.
212 return b ? write_attribute(name, convert<string>(b)) : string();
217 string const write_attribute(string const & name, int const & i)
219 // we write only true attribute values so we remove a bit of the
220 // file format bloat for tabulars.
221 return i ? write_attribute(name, convert<string>(i)) : string();
226 string const write_attribute(string const & name, Tabular::idx_type const & i)
228 // we write only true attribute values so we remove a bit of the
229 // file format bloat for tabulars.
230 return i ? write_attribute(name, convert<string>(i)) : string();
235 string const write_attribute(string const & name, Length const & value)
237 // we write only the value if we really have one same reson as above.
238 return value.zero() ? string() : write_attribute(name, value.asString());
242 string const tostr(LyXAlignment const & num)
247 case LYX_ALIGN_BLOCK:
251 case LYX_ALIGN_CENTER:
253 case LYX_ALIGN_RIGHT:
255 case LYX_ALIGN_LAYOUT:
257 case LYX_ALIGN_SPECIAL:
264 string const tostr(Tabular::HAlignment const & num)
267 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
269 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
271 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
278 string const tostr(Tabular::VAlignment const & num)
281 case Tabular::LYX_VALIGN_TOP:
283 case Tabular::LYX_VALIGN_MIDDLE:
285 case Tabular::LYX_VALIGN_BOTTOM:
292 string const tostr(Tabular::BoxType const & num)
295 case Tabular::BOX_NONE:
297 case Tabular::BOX_PARBOX:
299 case Tabular::BOX_MINIPAGE:
306 // I would have liked a fromstr template a lot better. (Lgb)
307 bool string2type(string const str, LyXAlignment & num)
310 num = LYX_ALIGN_NONE;
311 else if (str == "block")
312 num = LYX_ALIGN_BLOCK;
313 else if (str == "left")
314 num = LYX_ALIGN_LEFT;
315 else if (str == "center")
316 num = LYX_ALIGN_CENTER;
317 else if (str == "right")
318 num = LYX_ALIGN_RIGHT;
325 bool string2type(string const str, Tabular::HAlignment & num)
328 num = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
329 else if (str == "center" )
330 num = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
331 else if (str == "right")
332 num = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
339 bool string2type(string const str, Tabular::VAlignment & num)
342 num = Tabular::LYX_VALIGN_TOP;
343 else if (str == "middle" )
344 num = Tabular::LYX_VALIGN_MIDDLE;
345 else if (str == "bottom")
346 num = Tabular::LYX_VALIGN_BOTTOM;
353 bool string2type(string const str, Tabular::BoxType & num)
356 num = Tabular::BOX_NONE;
357 else if (str == "parbox")
358 num = Tabular::BOX_PARBOX;
359 else if (str == "minipage")
360 num = Tabular::BOX_MINIPAGE;
367 bool string2type(string const str, bool & num)
371 else if (str == "false")
379 bool getTokenValue(string const & str, char const * token, string & ret)
382 size_t token_length = strlen(token);
383 size_t pos = str.find(token);
385 if (pos == string::npos || pos + token_length + 1 >= str.length()
386 || str[pos + token_length] != '=')
388 pos += token_length + 1;
390 if (ch != '"' && ch != '\'') { // only read till next space
394 while (pos < str.length() - 1 && str[++pos] != ch)
401 bool getTokenValue(string const & str, char const * token, docstring & ret)
404 bool const success = getTokenValue(str, token, tmp);
405 ret = from_utf8(tmp);
410 bool getTokenValue(string const & str, char const * token, int & num)
414 if (!getTokenValue(str, token, tmp))
416 num = convert<int>(tmp);
421 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
424 return getTokenValue(str, token, tmp) && string2type(tmp, num);
428 bool getTokenValue(string const & str, char const * token,
429 Tabular::HAlignment & num)
432 return getTokenValue(str, token, tmp) && string2type(tmp, num);
436 bool getTokenValue(string const & str, char const * token,
437 Tabular::VAlignment & num)
440 return getTokenValue(str, token, tmp) && string2type(tmp, num);
444 bool getTokenValue(string const & str, char const * token,
445 Tabular::BoxType & num)
448 return getTokenValue(str, token, tmp) && string2type(tmp, num);
452 bool getTokenValue(string const & str, char const * token, bool & flag)
454 // set the flag always to false as this should be the default for bools
455 // not in the file-format.
458 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
462 bool getTokenValue(string const & str, char const * token, Length & len)
464 // set the length to be zero() as default as this it should be if not
465 // in the file format.
468 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
472 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
477 if (!getTokenValue(str, token, tmp))
479 if (tmp == "default") {
483 return isValidLength(tmp, &len);
487 void l_getline(istream & is, string & str)
490 while (str.empty()) {
492 if (!str.empty() && str[str.length() - 1] == '\r')
493 str.erase(str.length() - 1);
500 string const featureAsString(Tabular::Feature feature)
502 TabularFeature * end = tabularFeature +
503 sizeof(tabularFeature) / sizeof(TabularFeature);
504 TabularFeature * it = find_if(tabularFeature, end,
505 FeatureEqual(feature));
506 return (it == end) ? string() : it->feature;
511 /////////////////////////////////////////////////////////////////////
515 /////////////////////////////////////////////////////////////////////
518 Tabular::CellData::CellData(Buffer * buf)
521 multicolumn(Tabular::CELL_NORMAL),
522 alignment(LYX_ALIGN_CENTER),
523 valignment(LYX_VALIGN_TOP),
530 inset(new InsetTableCell(buf))
532 inset->setBuffer(*buf);
536 Tabular::CellData::CellData(CellData const & cs)
539 multicolumn(cs.multicolumn),
540 alignment(cs.alignment),
541 valignment(cs.valignment),
542 top_line(cs.top_line),
543 bottom_line(cs.bottom_line),
544 left_line(cs.left_line),
545 right_line(cs.right_line),
548 align_special(cs.align_special),
550 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
554 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
560 void Tabular::CellData::swap(CellData & rhs)
562 std::swap(cellno, rhs.cellno);
563 std::swap(width, rhs.width);
564 std::swap(multicolumn, rhs.multicolumn);
565 std::swap(alignment, rhs.alignment);
566 std::swap(valignment, rhs.valignment);
567 std::swap(top_line, rhs.top_line);
568 std::swap(bottom_line, rhs.bottom_line);
569 std::swap(left_line, rhs.left_line);
570 std::swap(right_line, rhs.right_line);
571 std::swap(usebox, rhs.usebox);
572 std::swap(rotate, rhs.rotate);
573 std::swap(align_special, rhs.align_special);
574 p_width.swap(rhs.p_width);
575 inset.swap(rhs.inset);
579 Tabular::RowData::RowData()
582 top_space_default(false),
583 bottom_space_default(false),
584 interline_space_default(false),
594 Tabular::ColumnData::ColumnData()
595 : alignment(LYX_ALIGN_CENTER),
596 valignment(LYX_VALIGN_TOP),
602 Tabular::ltType::ltType()
609 Tabular::Tabular(Buffer * buffer, row_type rows_arg, col_type columns_arg)
611 init(buffer, rows_arg, columns_arg);
615 void Tabular::setBuffer(Buffer & buffer)
618 size_t row_count = row_info.size();
619 size_t column_count = column_info.size();
620 // set silly default lines
621 for (row_type i = 0; i < row_count; ++i)
622 for (col_type j = 0; j < column_count; ++j)
623 cell_info[i][j].inset->setBuffer(*buffer_);
627 // activates all lines and sets all widths to 0
628 void Tabular::init(Buffer * buf, row_type rows_arg,
629 col_type columns_arg)
632 row_info = row_vector(rows_arg);
633 column_info = column_vector(columns_arg);
634 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
635 row_info.reserve(10);
636 column_info.reserve(10);
637 cell_info.reserve(100);
639 is_long_tabular = false;
640 tabular_valignment = LYX_VALIGN_MIDDLE;
641 longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
643 use_booktabs = false;
644 size_t row_count = row_info.size();
645 size_t column_count = column_info.size();
646 // set silly default lines
647 for (row_type i = 0; i < row_count; ++i)
648 for (col_type j = 0; j < column_count; ++j) {
649 cell_info[i][j].inset->setBuffer(*buffer_);
650 cell_info[i][j].top_line = true;
651 cell_info[i][j].left_line = true;
652 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
653 cell_info[i][j].right_line = j == column_count - 1;
658 void Tabular::appendRow(idx_type const cell)
660 BufferParams const & bp = buffer_->params();
661 row_type const row = cellRow(cell);
663 row_vector::iterator rit = row_info.begin() + row;
664 row_info.insert(rit, RowData());
665 // now set the values of the row before
666 row_info[row] = row_info[row + 1];
668 row_type const nrows = row_info.size();
669 col_type const ncols = column_info.size();
671 cell_vvector old(nrows - 1);
672 for (row_type i = 0; i < nrows - 1; ++i)
673 swap(cell_info[i], old[i]);
675 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer_)));
677 for (row_type i = 0; i <= row; ++i)
678 swap(cell_info[i], old[i]);
679 for (row_type i = row + 2; i < nrows; ++i)
680 swap(cell_info[i], old[i - 1]);
683 for (col_type c = 0; c < ncols; ++c) {
684 // inherit line settings
685 idx_type const i = cellIndex(row + 1, c);
686 idx_type const j = cellIndex(row, c);
687 setLeftLine(i, leftLine(j));
688 setRightLine(i, rightLine(j));
689 setTopLine(i, topLine(j));
690 if (topLine(j) && bottomLine(j)) {
691 setBottomLine(i, true);
692 setBottomLine(j, false);
694 // mark track changes
696 cellInfo(i).inset->setChange(Change(Change::INSERTED));
701 void Tabular::deleteRow(row_type const row)
703 // Not allowed to delete last row
704 if (row_info.size() == 1)
707 row_info.erase(row_info.begin() + row);
708 cell_info.erase(cell_info.begin() + row);
713 void Tabular::copyRow(row_type const row)
715 row_info.insert(row_info.begin() + row, row_info[row]);
716 cell_info.insert(cell_info.begin() + row, cell_info[row]);
718 if (buffer().params().trackChanges)
719 for (col_type j = 0; j < column_info.size(); ++j)
720 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
726 void Tabular::appendColumn(idx_type const cell)
728 col_type const c = cellColumn(cell);
729 column_vector::iterator cit = column_info.begin() + c + 1;
730 column_info.insert(cit, ColumnData());
731 row_type const nrows = row_info.size();
732 // set the column values of the column before
733 column_info[c + 1] = column_info[c];
735 for (row_type r = 0; r < nrows; ++r) {
736 cell_info[r].insert(cell_info[r].begin() + c + 1,
739 // FIXME: This code does not work. It deletes the cell's content and
740 // it triggers an assertion if the cursor is at pos > 0.
741 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
742 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
744 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
748 for (row_type r = 0; r < nrows; ++r) {
749 // inherit line settings
750 idx_type const i = cellIndex(r, c + 1);
751 idx_type const j = cellIndex(r, c);
752 setBottomLine(i, bottomLine(j));
753 setTopLine(i, topLine(j));
754 setLeftLine(i, leftLine(j));
755 if (rightLine(j) && rightLine(j)) {
756 setRightLine(i, true);
757 setRightLine(j, false);
760 cellInfo(i).inset->clear();
761 if (buffer().params().trackChanges)
762 cellInfo(i).inset->setChange(Change(Change::INSERTED));
767 void Tabular::deleteColumn(col_type const column)
769 // Not allowed to delete last column
770 if (column_info.size() == 1)
773 size_t const row_count = row_info.size();
774 for (row_type i = 0; i < row_count; ++i) {
775 // Care about multicolumn cells
776 if (column + 1 < column_info.size() &&
777 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
778 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
779 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
781 cell_info[i].erase(cell_info[i].begin() + column);
783 column_info.erase(column_info.begin() + column);
788 void Tabular::copyColumn(col_type const column)
790 BufferParams const & bp = buffer().params();
791 column_info.insert(column_info.begin() + column, column_info[column]);
793 size_t row_count = row_info.size();
794 for (row_type i = 0; i < row_count; ++i)
795 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
798 for (row_type i = 0; i < row_count; ++i)
799 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
804 void Tabular::updateIndexes()
807 col_type ncols = column_info.size();
808 row_type nrows = row_info.size();
810 for (row_type row = 0; row < nrows; ++row)
811 for (col_type column = 0; column < ncols; ++column) {
812 if (!isPartOfMultiColumn(row, column))
814 cell_info[row][column].cellno = numberofcells - 1;
817 rowofcell.resize(numberofcells);
818 columnofcell.resize(numberofcells);
820 for (row_type row = 0; row < nrows; ++row)
821 for (col_type column = 0; column < ncols; ++column) {
822 if (isPartOfMultiColumn(row, column))
825 columnofcell[i] = column;
826 setFixedWidth(row, column);
827 updateContentAlignment(row, column);
833 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
835 row_type const row = cellRow(cell);
837 for (col_type i = 0; i < column_info.size(); ++i)
838 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
844 bool Tabular::topLine(idx_type const cell) const
846 return cellInfo(cell).top_line;
850 bool Tabular::bottomLine(idx_type const cell) const
852 return cellInfo(cell).bottom_line;
856 bool Tabular::leftLine(idx_type cell) const
860 return cellInfo(cell).left_line;
864 bool Tabular::rightLine(idx_type cell) const
868 return cellInfo(cell).right_line;
872 bool Tabular::topAlreadyDrawn(idx_type cell) const
874 row_type row = cellRow(cell);
877 idx_type i = cellIndex(row - 1, cellColumn(cell));
878 return !rowTopLine(row) && bottomLine(i);
882 bool Tabular::leftAlreadyDrawn(idx_type cell) const
884 col_type col = cellColumn(cell);
887 idx_type i = cellIndex(cellRow(cell), col - 1);
888 return rightLine(i) && !leftLine(cell);
892 bool Tabular::isLastRow(idx_type cell) const
894 return cellRow(cell) == row_info.size() - 1;
898 int Tabular::getAdditionalHeight(row_type row) const
900 if (!row || row >= row_info.size())
903 int const interline_space = row_info[row - 1].interline_space_default ?
905 row_info[row - 1].interline_space.inPixels(width());
906 if (rowTopLine(row) && rowBottomLine(row - 1))
907 return interline_space + WIDTH_OF_LINE;
908 return interline_space;
912 int Tabular::getAdditionalWidth(idx_type cell) const
914 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
916 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
917 return WIDTH_OF_LINE;
922 int Tabular::columnWidth(idx_type cell) const
925 col_type const span = columnSpan(cell);
926 col_type const col = cellColumn(cell);
927 for(col_type c = col; c < col + span ; ++c)
928 w += column_info[c].width;
933 bool Tabular::updateColumnWidths()
935 col_type const ncols = column_info.size();
936 row_type const nrows = row_info.size();
938 // for each col get max of single col cells
939 for(col_type c = 0; c < ncols; ++c) {
941 for(row_type r = 0; r < nrows; ++r) {
942 idx_type const i = cellIndex(r, c);
943 if (columnSpan(i) == 1)
944 new_width = max(new_width, cellInfo(i).width);
947 if (column_info[c].width != new_width) {
948 column_info[c].width = new_width;
952 // update col widths to fit merged cells
953 for(col_type c = 0; c < ncols; ++c)
954 for(row_type r = 0; r < nrows; ++r) {
955 idx_type const i = cellIndex(r, c);
956 int const span = columnSpan(i);
957 if (span == 1 || c > cellColumn(i))
961 for(col_type j = c; j < c + span ; ++j)
962 old_width += column_info[j].width;
964 if (cellInfo(i).width > old_width) {
965 column_info[c + span - 1].width += cellInfo(i).width - old_width;
974 int Tabular::width() const
976 col_type const ncols = column_info.size();
978 for (col_type i = 0; i < ncols; ++i)
979 width += column_info[i].width;
984 void Tabular::setCellWidth(idx_type cell, int new_width)
986 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
987 + getAdditionalWidth(cell);
991 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
994 if (!isMultiColumn(cell) || onlycolumn)
995 column_info[cellColumn(cell)].alignment = align;
997 cellInfo(cell).alignment = align;
998 cellInset(cell).get()->setContentAlignment(align);
1002 void Tabular::setVAlignment(idx_type cell, VAlignment align,
1005 if (!isMultiColumn(cell) || onlycolumn)
1006 column_info[cellColumn(cell)].valignment = align;
1008 cellInfo(cell).valignment = align;
1015 * Allow line and paragraph breaks for fixed width cells or disallow them,
1016 * merge cell paragraphs and reset layout to standard for variable width
1019 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
1021 inset->setAutoBreakRows(fixedWidth);
1022 inset->toggleFixedWidth(fixedWidth);
1026 // merge all paragraphs to one
1027 BufferParams const & bp = cur.bv().buffer().params();
1028 while (inset->paragraphs().size() > 1)
1029 mergeParagraph(bp, inset->paragraphs(), 0);
1033 // undo information has already been recorded
1034 inset->getText(0)->setLayout(0, cur.lastpit() + 1,
1035 bp.documentClass().plainLayoutName());
1042 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
1043 Length const & width)
1045 col_type const j = cellColumn(cell);
1047 column_info[j].p_width = width;
1048 for (row_type i = 0; i < row_info.size(); ++i) {
1049 idx_type const cell = cellIndex(i, j);
1050 // because of multicolumns
1051 toggleFixedWidth(cur, cellInset(cell).get(),
1052 !getPWidth(cell).zero());
1054 // cur paragraph can become invalid after paragraphs were merged
1055 if (cur.pit() > cur.lastpit())
1056 cur.pit() = cur.lastpit();
1057 // cur position can become invalid after newlines were removed
1058 if (cur.pos() > cur.lastpos())
1059 cur.pos() = cur.lastpos();
1063 bool Tabular::setFixedWidth(row_type r, col_type c)
1065 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1066 bool const fixed_width = (!column_info[c].p_width.zero() && !multicol)
1067 || (multicol && !cell_info[r][c].p_width.zero());
1068 cell_info[r][c].inset->toggleFixedWidth(fixed_width);
1073 void Tabular::updateContentAlignment(row_type r, col_type c)
1075 cell_info[r][c].inset->setContentAlignment(
1076 getAlignment(cellIndex(r, c)));
1080 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1081 Length const & width)
1083 if (!isMultiColumn(cell))
1086 cellInfo(cell).p_width = width;
1087 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1088 // cur paragraph can become invalid after paragraphs were merged
1089 if (cur.pit() > cur.lastpit())
1090 cur.pit() = cur.lastpit();
1091 // cur position can become invalid after newlines were removed
1092 if (cur.pos() > cur.lastpos())
1093 cur.pos() = cur.lastpos();
1098 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1099 Tabular::Feature what)
1101 if (what == SET_SPECIAL_MULTI)
1102 cellInfo(cell).align_special = special;
1104 column_info[cellColumn(cell)].align_special = special;
1108 void Tabular::setAllLines(idx_type cell, bool line)
1110 setTopLine(cell, line);
1111 setBottomLine(cell, line);
1112 setRightLine(cell, line);
1113 setLeftLine(cell, line);
1117 void Tabular::setTopLine(idx_type i, bool line)
1119 cellInfo(i).top_line = line;
1123 void Tabular::setBottomLine(idx_type i, bool line)
1125 cellInfo(i).bottom_line = line;
1129 void Tabular::setLeftLine(idx_type cell, bool line)
1131 cellInfo(cell).left_line = line;
1135 void Tabular::setRightLine(idx_type cell, bool line)
1137 cellInfo(cell).right_line = line;
1140 bool Tabular::rowTopLine(row_type r) const
1142 idx_type i0 = getFirstCellInRow(r);
1143 idx_type i1 = getLastCellInRow(r);
1144 bool all_rows_set = true;
1145 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1146 all_rows_set = cellInfo(j).top_line;
1147 return all_rows_set;
1151 bool Tabular::rowBottomLine(row_type r) const
1153 idx_type i0 = getFirstCellInRow(r);
1154 idx_type i1 = getLastCellInRow(r);
1155 bool all_rows_set = true;
1156 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1157 all_rows_set = cellInfo(j).bottom_line;
1158 return all_rows_set;
1162 bool Tabular::columnLeftLine(col_type c) const
1169 row_type const nrows = row_info.size();
1170 for (row_type r = 0; r < nrows; ++r) {
1171 idx_type i = cellIndex(r, c);
1172 if (c == cellColumn(i)) {
1174 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1175 if (cellInfo(i).left_line || right)
1179 return 2 * nrows_left >= total;
1183 bool Tabular::columnRightLine(col_type c) const
1188 int nrows_right = 0;
1190 row_type const nrows = row_info.size();
1191 for (row_type r = 0; r < nrows; ++r) {
1192 idx_type i = cellIndex(r, c);
1193 if (c == cellColumn(i) + columnSpan(i) - 1) {
1195 bool left = (c + 1 < column_info.size()
1196 && cellInfo(cellIndex(r, c + 1)).left_line)
1197 || c + 1 == column_info.size();
1198 if (cellInfo(i).right_line && left)
1202 return 2 * nrows_right >= total;
1206 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1208 if (!onlycolumn && isMultiColumn(cell))
1209 return cellInfo(cell).alignment;
1210 return column_info[cellColumn(cell)].alignment;
1215 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1217 if (!onlycolumn && isMultiColumn(cell))
1218 return cellInfo(cell).valignment;
1219 return column_info[cellColumn(cell)].valignment;
1223 Length const Tabular::getPWidth(idx_type cell) const
1225 if (isMultiColumn(cell))
1226 return cellInfo(cell).p_width;
1227 return column_info[cellColumn(cell)].p_width;
1231 int Tabular::cellWidth(idx_type cell) const
1233 return cellInfo(cell).width;
1237 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1241 switch (getAlignment(cell)) {
1242 case LYX_ALIGN_CENTER:
1243 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1245 case LYX_ALIGN_RIGHT:
1246 x += columnWidth(cell) - cellWidth(cell);
1247 // + getAdditionalWidth(cell);
1250 // LYX_ALIGN_LEFT: nothing :-)
1254 // the LaTeX Way :-(
1260 bool Tabular::isFirstCellInRow(idx_type cell) const
1262 return cellColumn(cell) == 0;
1266 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1268 if (row > row_info.size() - 1)
1269 row = row_info.size() - 1;
1270 return cell_info[row][0].cellno;
1274 bool Tabular::isLastCellInRow(idx_type cell) const
1276 return cellRightColumn(cell) == column_info.size() - 1;
1280 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1282 if (row > row_info.size() - 1)
1283 row = row_info.size() - 1;
1284 return cell_info[row][column_info.size() - 1].cellno;
1288 Tabular::row_type Tabular::cellRow(idx_type cell) const
1290 if (cell >= numberofcells)
1291 return row_info.size() - 1;
1294 return rowofcell[cell];
1298 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1300 if (cell >= numberofcells)
1301 return column_info.size() - 1;
1304 return columnofcell[cell];
1308 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1310 row_type const row = cellRow(cell);
1311 col_type column = cellColumn(cell);
1312 while (column < column_info.size() - 1 &&
1313 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1319 void Tabular::write(ostream & os) const
1323 << write_attribute("version", 3)
1324 << write_attribute("rows", row_info.size())
1325 << write_attribute("columns", column_info.size())
1327 // global longtable options
1329 << write_attribute("rotate", rotate)
1330 << write_attribute("booktabs", use_booktabs)
1331 << write_attribute("islongtable", is_long_tabular)
1332 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1333 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1334 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1335 << write_attribute("headTopDL", endhead.topDL)
1336 << write_attribute("headBottomDL", endhead.bottomDL)
1337 << write_attribute("footTopDL", endfoot.topDL)
1338 << write_attribute("footBottomDL", endfoot.bottomDL)
1339 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1340 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1341 << write_attribute("lastFootEmpty", endlastfoot.empty);
1342 // longtables cannot be aligned vertically
1343 if (!is_long_tabular)
1344 os << write_attribute("tabularvalignment", tabular_valignment);
1345 if (is_long_tabular)
1346 os << write_attribute("longtabularalignment",
1347 longtabular_alignment);
1349 for (col_type j = 0; j < column_info.size(); ++j) {
1351 << write_attribute("alignment", column_info[j].alignment)
1352 << write_attribute("valignment", column_info[j].valignment)
1353 << write_attribute("width", column_info[j].p_width.asString())
1354 << write_attribute("special", column_info[j].align_special)
1357 for (row_type i = 0; i < row_info.size(); ++i) {
1358 static const string def("default");
1360 if (row_info[i].top_space_default)
1361 os << write_attribute("topspace", def);
1363 os << write_attribute("topspace", row_info[i].top_space);
1364 if (row_info[i].bottom_space_default)
1365 os << write_attribute("bottomspace", def);
1367 os << write_attribute("bottomspace", row_info[i].bottom_space);
1368 if (row_info[i].interline_space_default)
1369 os << write_attribute("interlinespace", def);
1371 os << write_attribute("interlinespace", row_info[i].interline_space);
1372 os << write_attribute("endhead", row_info[i].endhead)
1373 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1374 << write_attribute("endfoot", row_info[i].endfoot)
1375 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1376 << write_attribute("newpage", row_info[i].newpage)
1377 << write_attribute("caption", row_info[i].caption)
1379 for (col_type j = 0; j < column_info.size(); ++j) {
1381 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1382 << write_attribute("alignment", cell_info[i][j].alignment)
1383 << write_attribute("valignment", cell_info[i][j].valignment)
1384 << write_attribute("topline", cell_info[i][j].top_line)
1385 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1386 << write_attribute("leftline", cell_info[i][j].left_line)
1387 << write_attribute("rightline", cell_info[i][j].right_line)
1388 << write_attribute("rotate", cell_info[i][j].rotate)
1389 << write_attribute("usebox", cell_info[i][j].usebox)
1390 << write_attribute("width", cell_info[i][j].p_width)
1391 << write_attribute("special", cell_info[i][j].align_special)
1393 os << "\\begin_inset ";
1394 cell_info[i][j].inset->write(os);
1395 os << "\n\\end_inset\n"
1400 os << "</lyxtabular>\n";
1404 void Tabular::read(Lexer & lex)
1407 istream & is = lex.getStream();
1409 l_getline(is, line);
1410 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1411 LASSERT(false, /**/);
1416 if (!getTokenValue(line, "version", version))
1418 LASSERT(version >= 2, /**/);
1421 if (!getTokenValue(line, "rows", rows_arg))
1424 if (!getTokenValue(line, "columns", columns_arg))
1426 init(buffer_, rows_arg, columns_arg);
1427 l_getline(is, line);
1428 if (!prefixIs(line, "<features")) {
1429 lyxerr << "Wrong tabular format (expected <features ...> got"
1430 << line << ')' << endl;
1433 getTokenValue(line, "rotate", rotate);
1434 getTokenValue(line, "booktabs", use_booktabs);
1435 getTokenValue(line, "islongtable", is_long_tabular);
1436 getTokenValue(line, "tabularvalignment", tabular_valignment);
1437 getTokenValue(line, "longtabularalignment", longtabular_alignment);
1438 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1439 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1440 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1441 getTokenValue(line, "headTopDL", endhead.topDL);
1442 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1443 getTokenValue(line, "footTopDL", endfoot.topDL);
1444 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1445 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1446 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1447 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1449 for (col_type j = 0; j < column_info.size(); ++j) {
1451 if (!prefixIs(line,"<column")) {
1452 lyxerr << "Wrong tabular format (expected <column ...> got"
1453 << line << ')' << endl;
1456 getTokenValue(line, "alignment", column_info[j].alignment);
1457 getTokenValue(line, "valignment", column_info[j].valignment);
1458 getTokenValue(line, "width", column_info[j].p_width);
1459 getTokenValue(line, "special", column_info[j].align_special);
1462 for (row_type i = 0; i < row_info.size(); ++i) {
1463 l_getline(is, line);
1464 if (!prefixIs(line, "<row")) {
1465 lyxerr << "Wrong tabular format (expected <row ...> got"
1466 << line << ')' << endl;
1469 getTokenValue(line, "topspace", row_info[i].top_space,
1470 row_info[i].top_space_default);
1471 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1472 row_info[i].bottom_space_default);
1473 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1474 row_info[i].interline_space_default);
1475 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1476 getTokenValue(line, "endhead", row_info[i].endhead);
1477 getTokenValue(line, "endfoot", row_info[i].endfoot);
1478 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1479 getTokenValue(line, "newpage", row_info[i].newpage);
1480 getTokenValue(line, "caption", row_info[i].caption);
1481 for (col_type j = 0; j < column_info.size(); ++j) {
1482 l_getline(is, line);
1483 if (!prefixIs(line, "<cell")) {
1484 lyxerr << "Wrong tabular format (expected <cell ...> got"
1485 << line << ')' << endl;
1488 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1489 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1490 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1491 getTokenValue(line, "topline", cell_info[i][j].top_line);
1492 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1493 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1494 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1495 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1496 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1497 getTokenValue(line, "width", cell_info[i][j].p_width);
1499 getTokenValue(line, "special", cell_info[i][j].align_special);
1500 l_getline(is, line);
1501 if (prefixIs(line, "\\begin_inset")) {
1502 cell_info[i][j].inset->setBuffer(*buffer_);
1503 cell_info[i][j].inset->read(lex);
1504 l_getline(is, line);
1506 if (!prefixIs(line, "</cell>")) {
1507 lyxerr << "Wrong tabular format (expected </cell> got"
1508 << line << ')' << endl;
1512 l_getline(is, line);
1513 if (!prefixIs(line, "</row>")) {
1514 lyxerr << "Wrong tabular format (expected </row> got"
1515 << line << ')' << endl;
1519 while (!prefixIs(line, "</lyxtabular>")) {
1520 l_getline(is, line);
1526 bool Tabular::isMultiColumn(idx_type cell) const
1528 return cellInfo(cell).multicolumn != CELL_NORMAL;
1532 bool Tabular::isMultiColumnReal(idx_type cell) const
1534 return cellColumn(cell) != cellRightColumn(cell) &&
1535 cellInfo(cell).multicolumn != CELL_NORMAL;
1539 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1541 return cell_info[cellRow(cell)][cellColumn(cell)];
1545 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1547 CellData & cs = cellInfo(cell);
1548 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1549 cs.alignment = column_info[cellColumn(cell)].alignment;
1550 setRightLine(cell, rightLine(cell + number - 1));
1551 for (idx_type i = 1; i < number; ++i) {
1552 CellData & cs1 = cellInfo(cell + i);
1553 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1554 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1561 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1563 row_type const row = cellRow(cell);
1564 col_type const ncols = column_info.size();
1565 idx_type result = 1;
1566 col_type column = cellColumn(cell) + 1;
1567 while (column < ncols && isPartOfMultiColumn(row, column)) {
1575 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1577 row_type const row = cellRow(cell);
1578 col_type column = cellColumn(cell);
1580 idx_type result = 0;
1582 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1583 cell_info[row][column].multicolumn = CELL_NORMAL;
1585 while (column < column_info.size() &&
1586 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1588 cell_info[row][column].multicolumn = CELL_NORMAL;
1598 void Tabular::setRotateCell(idx_type cell, bool flag)
1600 cellInfo(cell).rotate = flag;
1604 bool Tabular::getRotateCell(idx_type cell) const
1606 return cellInfo(cell).rotate;
1610 bool Tabular::needRotating() const
1614 for (row_type i = 0; i < row_info.size(); ++i)
1615 for (col_type j = 0; j < column_info.size(); ++j)
1616 if (cell_info[i][j].rotate)
1622 bool Tabular::isLastCell(idx_type cell) const
1624 if (cell + 1 < numberofcells)
1630 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1632 if (cellRow(cell) > 0)
1633 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1638 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1640 if (cellRow(cell) + 1 < row_info.size())
1641 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1646 Tabular::idx_type Tabular::cellIndex(row_type row,
1647 col_type column) const
1649 BOOST_ASSERT(column != npos && column < column_info.size()
1650 && row != npos && row < row_info.size());
1651 return cell_info[row][column].cellno;
1655 void Tabular::setUsebox(idx_type cell, BoxType type)
1657 cellInfo(cell).usebox = type;
1661 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1662 // adds line breaks, see bug 4886.
1663 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1665 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1666 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1668 if (cellInfo(cell).usebox > 1)
1669 return cellInfo(cell).usebox;
1670 return useParbox(cell);
1675 // This are functions used for the longtable support
1677 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1683 row_info[row].endfirsthead = flag;
1687 row_info[row].endhead = flag;
1692 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1695 hd.set = haveLTHead();
1696 return row_info[row].endhead;
1700 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1703 hd.set = haveLTFirstHead();
1704 return row_info[row].endfirsthead;
1708 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1714 row_info[row].endlastfoot = flag;
1718 row_info[row].endfoot = flag;
1723 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1726 fd.set = haveLTFoot();
1727 return row_info[row].endfoot;
1731 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1734 fd.set = haveLTLastFoot();
1735 return row_info[row].endlastfoot;
1739 void Tabular::setLTNewPage(row_type row, bool what)
1741 row_info[row].newpage = what;
1745 bool Tabular::getLTNewPage(row_type row) const
1747 return row_info[row].newpage;
1751 bool Tabular::haveLTHead() const
1753 for (row_type i = 0; i < row_info.size(); ++i)
1754 if (row_info[i].endhead)
1760 bool Tabular::haveLTFirstHead() const
1762 if (endfirsthead.empty)
1764 for (row_type i = 0; i < row_info.size(); ++i)
1765 if (row_info[i].endfirsthead)
1771 bool Tabular::haveLTFoot() const
1773 for (row_type i = 0; i < row_info.size(); ++i)
1774 if (row_info[i].endfoot)
1780 bool Tabular::haveLTLastFoot() const
1782 if (endlastfoot.empty)
1784 for (row_type i = 0; i < row_info.size(); ++i)
1785 if (row_info[i].endlastfoot)
1791 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1793 idx_type i = getFirstCellInRow(row);
1795 setMultiColumn(i, numberOfCellsInRow(i));
1796 setTopLine(i, false);
1797 setBottomLine(i, false);
1798 setLeftLine(i, false);
1799 setRightLine(i, false);
1801 unsetMultiColumn(i);
1802 // When unsetting a caption row, also all existing
1803 // captions in this row must be dissolved.
1805 row_info[row].caption = what;
1810 bool Tabular::ltCaption(row_type row) const
1812 return row_info[row].caption;
1816 bool Tabular::haveLTCaption() const
1818 for (row_type i = 0; i < row_info.size(); ++i)
1819 if (row_info[i].caption)
1825 // end longtable support functions
1827 void Tabular::setRowAscent(row_type row, int height)
1829 if (row >= row_info.size() || row_info[row].ascent == height)
1831 row_info[row].ascent = height;
1835 void Tabular::setRowDescent(row_type row, int height)
1837 if (row >= row_info.size() || row_info[row].descent == height)
1839 row_info[row].descent = height;
1843 int Tabular::rowAscent(row_type row) const
1845 if (row >= row_info.size())
1847 return row_info[row].ascent;
1851 int Tabular::rowDescent(row_type row) const
1853 LASSERT(row < row_info.size(), /**/);
1854 return row_info[row].descent;
1858 int Tabular::height() const
1861 for (row_type row = 0; row < row_info.size(); ++row)
1862 height += rowAscent(row) + rowDescent(row) +
1863 getAdditionalHeight(row);
1868 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1870 LASSERT(row < row_info.size(), /**/);
1871 LASSERT(column < column_info.size(), /**/);
1872 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1876 int Tabular::TeXTopHLine(odocstream & os, row_type row, string const lang) const
1878 // we only output complete row lines and the 1st row here, the rest
1879 // is done in Tabular::TeXBottomHLine(...)
1881 // get for each column the topline (if any)
1882 col_type const ncols = column_info.size();
1883 vector<bool> topline;
1885 for (col_type c = 0; c < ncols; ++c) {
1886 topline.push_back(topLine(cellIndex(row, c)));
1891 // do nothing if empty first row, or incomplete row line after
1892 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1895 // only output complete row lines and the 1st row's clines
1896 if (nset == ncols) {
1898 os << (row == 0 ? "\\toprule " : "\\midrule ");
1902 } else if (row == 0) {
1903 for (col_type c = 0; c < ncols; ++c) {
1905 //babel makes the "-" character an active one, so we have to suppress this here
1906 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1907 if (lang == "slovak" || lang == "czech")
1908 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1909 "\\expandafter\\cline\\expandafter{\\expandafter")
1910 << c + 1 << "\\string-";
1912 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1913 // get to last column of line span
1914 while (c < ncols && topline[c])
1925 int Tabular::TeXBottomHLine(odocstream & os, row_type row, string const lang) const
1927 // we output bottomlines of row r and the toplines of row r+1
1928 // if the latter do not span the whole tabular
1930 // get the bottomlines of row r, and toplines in next row
1931 bool lastrow = row == row_info.size() - 1;
1932 col_type const ncols = column_info.size();
1933 vector<bool> bottomline, topline;
1934 bool nextrowset = true;
1935 for (col_type c = 0; c < ncols; ++c) {
1936 bottomline.push_back(bottomLine(cellIndex(row, c)));
1937 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1938 nextrowset &= topline[c];
1941 // combine this row's bottom lines and next row's toplines if necessary
1943 for (col_type c = 0; c < ncols; ++c) {
1945 bottomline[c] = bottomline[c] || topline[c];
1950 // do nothing if empty, OR incomplete row line with a topline in next row
1951 if (nset == 0 || (nextrowset && nset != ncols))
1954 if (nset == ncols) {
1956 os << (lastrow ? "\\bottomrule" : "\\midrule");
1960 for (col_type c = 0; c < ncols; ++c) {
1961 if (bottomline[c]) {
1962 //babel makes the "-" character an active one, so we have to suppress this here
1963 //see http://groups.google.com/group/comp.text.tex/browse_thread/thread/af769424a4a0f289#
1964 if (lang == "slovak" || lang == "czech")
1965 os << (use_booktabs ? "\\expandafter\\cmidrule\\expandafter{\\expandafter" :
1966 "\\expandafter\\cline\\expandafter{\\expandafter")
1967 << c + 1 << "\\string-";
1969 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1970 // get to last column of line span
1971 while (c < ncols && bottomline[c])
1982 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1985 row_type const r = cellRow(cell);
1986 if (is_long_tabular && row_info[r].caption)
1989 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1990 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1991 // figure out how to set the lines
1992 // we always set double lines to the right of the cell
1993 col_type const c = cellColumn(cell);
1994 col_type const nextcol = c + columnSpan(cell);
1995 bool colright = columnRightLine(c);
1996 bool colleft = columnLeftLine(c);
1997 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1998 bool nextcellleft = nextcol < column_info.size()
1999 && leftLine(cellIndex(r, nextcol));
2000 bool coldouble = colright && nextcolleft;
2001 bool celldouble = rightLine(cell) && nextcellleft;
2002 ismulticol = isMultiColumn(cell)
2003 || (c == 0 && colleft != leftLine(cell))
2004 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
2005 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
2006 || (coldouble != celldouble);
2008 os << "\\multicolumn{" << columnSpan(cell) << "}{";
2009 if (c ==0 && leftLine(cell))
2011 if (!cellInfo(cell).align_special.empty()) {
2012 os << cellInfo(cell).align_special;
2014 if (!getPWidth(cell).zero()) {
2016 case LYX_ALIGN_LEFT:
2017 os << ">{\\raggedright}";
2019 case LYX_ALIGN_RIGHT:
2020 os << ">{\\raggedleft}";
2022 case LYX_ALIGN_CENTER:
2023 os << ">{\\centering}";
2029 case LYX_VALIGN_TOP:
2032 case LYX_VALIGN_MIDDLE:
2035 case LYX_VALIGN_BOTTOM:
2040 << from_ascii(getPWidth(cell).asLatexString())
2044 case LYX_ALIGN_LEFT:
2047 case LYX_ALIGN_RIGHT:
2054 } // end if else !getPWidth
2055 } // end if else !cellinfo_of_cell
2056 if (rightLine(cell) || nextcellleft)
2059 // add extra vertical line if we want a double one
2063 if (getRotateCell(cell)) {
2064 os << "\\begin{sideways}\n";
2067 if (getUsebox(cell) == BOX_PARBOX) {
2070 case LYX_VALIGN_TOP:
2073 case LYX_VALIGN_MIDDLE:
2076 case LYX_VALIGN_BOTTOM:
2080 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2082 } else if (getUsebox(cell) == BOX_MINIPAGE) {
2083 os << "\\begin{minipage}[";
2085 case LYX_VALIGN_TOP:
2088 case LYX_VALIGN_MIDDLE:
2091 case LYX_VALIGN_BOTTOM:
2095 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2103 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2106 row_type const r = cellRow(cell);
2107 if (is_long_tabular && row_info[r].caption)
2111 if (getUsebox(cell) == BOX_PARBOX)
2113 else if (getUsebox(cell) == BOX_MINIPAGE) {
2114 os << "%\n\\end{minipage}";
2117 if (getRotateCell(cell)) {
2118 os << "%\n\\end{sideways}";
2128 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2129 OutputParams const & runparams) const
2131 if (!is_long_tabular)
2136 // the caption must be output before the headers
2137 if (haveLTCaption()) {
2138 for (row_type i = 0; i < row_info.size(); ++i) {
2139 if (row_info[i].caption) {
2140 ret += TeXRow(os, i, runparams);
2144 // output first header info
2145 // first header must be output before the header, otherwise the
2146 // correct caption placement becomes really weird
2147 if (haveLTFirstHead()) {
2148 if (endfirsthead.topDL) {
2152 for (row_type i = 0; i < row_info.size(); ++i) {
2153 if (row_info[i].endfirsthead) {
2154 ret += TeXRow(os, i, runparams);
2157 if (endfirsthead.bottomDL) {
2161 os << "\\endfirsthead\n";
2164 // output header info
2166 if (endfirsthead.empty && !haveLTFirstHead()) {
2167 os << "\\endfirsthead\n";
2170 if (endhead.topDL) {
2174 for (row_type i = 0; i < row_info.size(); ++i) {
2175 if (row_info[i].endhead) {
2176 ret += TeXRow(os, i, runparams);
2179 if (endhead.bottomDL) {
2183 os << "\\endhead\n";
2186 // output footer info
2188 if (endfoot.topDL) {
2192 for (row_type i = 0; i < row_info.size(); ++i) {
2193 if (row_info[i].endfoot) {
2194 ret += TeXRow(os, i, runparams);
2197 if (endfoot.bottomDL) {
2201 os << "\\endfoot\n";
2203 if (endlastfoot.empty && !haveLTLastFoot()) {
2204 os << "\\endlastfoot\n";
2208 // output lastfooter info
2209 if (haveLTLastFoot()) {
2210 if (endlastfoot.topDL) {
2214 for (row_type i = 0; i < row_info.size(); ++i) {
2215 if (row_info[i].endlastfoot) {
2216 ret += TeXRow(os, i, runparams);
2219 if (endlastfoot.bottomDL) {
2223 os << "\\endlastfoot\n";
2230 bool Tabular::isValidRow(row_type row) const
2232 if (!is_long_tabular)
2234 return !row_info[row].endhead && !row_info[row].endfirsthead
2235 && !row_info[row].endfoot && !row_info[row].endlastfoot
2236 && !row_info[row].caption;
2240 int Tabular::TeXRow(odocstream & os, row_type i,
2241 OutputParams const & runparams) const
2243 idx_type cell = cellIndex(i, 0);
2244 shared_ptr<InsetTableCell> inset = cellInset(cell);
2245 Paragraph const & par = inset->paragraphs().front();
2246 string const lang = par.getParLanguage(buffer().params())->lang();
2248 //output the top line
2249 int ret = TeXTopHLine(os, i, lang);
2251 if (row_info[i].top_space_default) {
2253 os << "\\addlinespace\n";
2255 os << "\\noalign{\\vskip\\doublerulesep}\n";
2257 } else if(!row_info[i].top_space.zero()) {
2259 os << "\\addlinespace["
2260 << from_ascii(row_info[i].top_space.asLatexString())
2263 os << "\\noalign{\\vskip"
2264 << from_ascii(row_info[i].top_space.asLatexString())
2269 bool ismulticol = false;
2270 for (col_type j = 0; j < column_info.size(); ++j) {
2271 if (isPartOfMultiColumn(i, j))
2273 ret += TeXCellPreamble(os, cell, ismulticol);
2274 shared_ptr<InsetTableCell> inset = cellInset(cell);
2276 Paragraph const & par = inset->paragraphs().front();
2277 bool rtl = par.isRTL(buffer().params())
2279 && getPWidth(cell).zero();
2283 par.getParLanguage(buffer().params())->lang();
2284 if (lang == "farsi")
2286 else if (lang == "arabic_arabi")
2288 // currently, remaning RTL languages are
2289 // arabic_arabtex and hebrew
2293 // pass to the OutputParams that we are in a cell and
2294 // which alignment we have set.
2295 // InsetNewline needs this context information.
2296 OutputParams newrp(runparams);
2297 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2298 ? OutputParams::PLAIN
2299 : OutputParams::ALIGNED;
2300 ret += inset->latex(os, newrp);
2301 runparams.encoding = newrp.encoding;
2305 ret += TeXCellPostamble(os, cell, ismulticol);
2306 if (!isLastCellInRow(cell)) { // not last cell in row
2311 if (row_info[i].caption && !endfirsthead.empty && !haveLTFirstHead())
2312 // if no first header and no empty first header is used,
2313 // the caption needs to be terminated by \endfirsthead
2315 os << "\\endfirsthead";
2317 os << "\\tabularnewline";
2318 if (row_info[i].bottom_space_default) {
2320 os << "\\addlinespace";
2322 os << "[\\doublerulesep]";
2323 } else if (!row_info[i].bottom_space.zero()) {
2325 os << "\\addlinespace";
2327 << from_ascii(row_info[i].bottom_space.asLatexString())
2333 //output the bottom line
2334 ret += TeXBottomHLine(os, i, lang);
2336 if (row_info[i].interline_space_default) {
2338 os << "\\addlinespace\n";
2340 os << "\\noalign{\\vskip\\doublerulesep}\n";
2342 } else if (!row_info[i].interline_space.zero()) {
2344 os << "\\addlinespace["
2345 << from_ascii(row_info[i].interline_space.asLatexString())
2348 os << "\\noalign{\\vskip"
2349 << from_ascii(row_info[i].interline_space.asLatexString())
2357 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2361 //+---------------------------------------------------------------------
2362 //+ first the opening preamble +
2363 //+---------------------------------------------------------------------
2366 os << "\\begin{sideways}\n";
2369 if (is_long_tabular) {
2370 os << "\\begin{longtable}";
2371 switch (longtabular_alignment) {
2372 case LYX_LONGTABULAR_ALIGN_LEFT:
2375 case LYX_LONGTABULAR_ALIGN_CENTER:
2377 case LYX_LONGTABULAR_ALIGN_RIGHT:
2382 os << "\\begin{tabular}";
2383 switch (tabular_valignment) {
2384 case LYX_VALIGN_TOP:
2387 case LYX_VALIGN_MIDDLE:
2389 case LYX_VALIGN_BOTTOM:
2397 for (col_type i = 0; i < column_info.size(); ++i) {
2398 if (columnLeftLine(i))
2400 if (!column_info[i].align_special.empty()) {
2401 os << column_info[i].align_special;
2403 if (!column_info[i].p_width.zero()) {
2404 switch (column_info[i].alignment) {
2405 case LYX_ALIGN_LEFT:
2406 os << ">{\\raggedright}";
2408 case LYX_ALIGN_RIGHT:
2409 os << ">{\\raggedleft}";
2411 case LYX_ALIGN_CENTER:
2412 os << ">{\\centering}";
2414 case LYX_ALIGN_NONE:
2415 case LYX_ALIGN_BLOCK:
2416 case LYX_ALIGN_LAYOUT:
2417 case LYX_ALIGN_SPECIAL:
2421 switch (column_info[i].valignment) {
2422 case LYX_VALIGN_TOP:
2425 case LYX_VALIGN_MIDDLE:
2428 case LYX_VALIGN_BOTTOM:
2433 << from_ascii(column_info[i].p_width.asLatexString())
2436 switch (column_info[i].alignment) {
2437 case LYX_ALIGN_LEFT:
2440 case LYX_ALIGN_RIGHT:
2447 } // end if else !column_info[i].p_width
2448 } // end if else !column_info[i].align_special
2449 if (columnRightLine(i))
2455 ret += TeXLongtableHeaderFooter(os, runparams);
2457 //+---------------------------------------------------------------------
2458 //+ the single row and columns (cells) +
2459 //+---------------------------------------------------------------------
2461 for (row_type i = 0; i < row_info.size(); ++i) {
2462 if (isValidRow(i)) {
2463 ret += TeXRow(os, i, runparams);
2464 if (is_long_tabular && row_info[i].newpage) {
2465 os << "\\newpage\n";
2471 //+---------------------------------------------------------------------
2472 //+ the closing of the tabular +
2473 //+---------------------------------------------------------------------
2475 if (is_long_tabular)
2476 os << "\\end{longtable}";
2478 os << "\\end{tabular}";
2480 os << "\n\\end{sideways}";
2488 int Tabular::docbookRow(odocstream & os, row_type row,
2489 OutputParams const & runparams) const
2492 idx_type cell = getFirstCellInRow(row);
2495 for (col_type j = 0; j < column_info.size(); ++j) {
2496 if (isPartOfMultiColumn(row, j))
2499 os << "<entry align=\"";
2500 switch (getAlignment(cell)) {
2501 case LYX_ALIGN_LEFT:
2504 case LYX_ALIGN_RIGHT:
2512 os << "\" valign=\"";
2513 switch (getVAlignment(cell)) {
2514 case LYX_VALIGN_TOP:
2517 case LYX_VALIGN_BOTTOM:
2520 case LYX_VALIGN_MIDDLE:
2525 if (isMultiColumn(cell)) {
2526 os << " namest=\"col" << j << "\" ";
2527 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2531 ret += cellInset(cell)->docbook(os, runparams);
2540 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2544 //+---------------------------------------------------------------------
2545 //+ first the opening preamble +
2546 //+---------------------------------------------------------------------
2548 os << "<tgroup cols=\"" << column_info.size()
2549 << "\" colsep=\"1\" rowsep=\"1\">\n";
2551 for (col_type i = 0; i < column_info.size(); ++i) {
2552 os << "<colspec colname=\"col" << i << "\" align=\"";
2553 switch (column_info[i].alignment) {
2554 case LYX_ALIGN_LEFT:
2557 case LYX_ALIGN_RIGHT:
2565 if (runparams.flavor == OutputParams::XML)
2571 //+---------------------------------------------------------------------
2572 //+ Long Tabular case +
2573 //+---------------------------------------------------------------------
2575 // output caption info
2576 if (haveLTCaption()) {
2577 os << "<caption>\n";
2579 for (row_type i = 0; i < row_info.size(); ++i) {
2580 if (row_info[i].caption) {
2581 ret += docbookRow(os, i, runparams);
2584 os << "</caption>\n";
2587 // output header info
2588 if (haveLTHead() || haveLTFirstHead()) {
2591 for (row_type i = 0; i < row_info.size(); ++i) {
2592 if (row_info[i].endhead || row_info[i].endfirsthead) {
2593 ret += docbookRow(os, i, runparams);
2599 // output footer info
2600 if (haveLTFoot() || haveLTLastFoot()) {
2603 for (row_type i = 0; i < row_info.size(); ++i) {
2604 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2605 ret += docbookRow(os, i, runparams);
2612 //+---------------------------------------------------------------------
2613 //+ the single row and columns (cells) +
2614 //+---------------------------------------------------------------------
2618 for (row_type i = 0; i < row_info.size(); ++i) {
2619 if (isValidRow(i)) {
2620 ret += docbookRow(os, i, runparams);
2625 //+---------------------------------------------------------------------
2626 //+ the closing of the tabular +
2627 //+---------------------------------------------------------------------
2636 docstring Tabular::xhtmlRow(XHTMLStream & xs, row_type row,
2637 OutputParams const & runparams) const
2640 idx_type cell = getFirstCellInRow(row);
2642 xs << html::StartTag("tr");
2643 for (col_type j = 0; j < column_info.size(); ++j) {
2644 if (isPartOfMultiColumn(row, j))
2649 switch (getAlignment(cell)) {
2650 case LYX_ALIGN_LEFT:
2653 case LYX_ALIGN_RIGHT:
2661 attr << " valign='";
2662 switch (getVAlignment(cell)) {
2663 case LYX_VALIGN_TOP:
2666 case LYX_VALIGN_BOTTOM:
2669 case LYX_VALIGN_MIDDLE:
2674 if (isMultiColumn(cell))
2675 attr << " colspan='" << columnSpan(cell) << "'";
2677 xs << html::StartTag("td", attr.str());
2678 ret += cellInset(cell)->xhtml(xs, runparams);
2679 xs << html::EndTag("td");
2682 xs << html::EndTag("tr");
2687 docstring Tabular::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
2690 // It's unclear to me if we need to mess with the long table stuff.
2691 // We can borrow that too from docbook, if so.
2693 xs << html::StartTag("tbody");
2694 for (row_type i = 0; i < row_info.size(); ++i) {
2695 if (isValidRow(i)) {
2696 ret += xhtmlRow(xs, i, runparams);
2699 xs << html::EndTag("tbody");
2704 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2705 vector<unsigned int> const & clen) const
2707 idx_type const fcell = getFirstCellInRow(row);
2708 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2711 for (idx_type i = fcell; i < n; ++i) {
2721 for (idx_type i = fcell; i < n; ++i) {
2732 col_type column = cellColumn(i);
2733 int len = clen[column];
2734 while (column < column_info.size() - 1
2735 && isPartOfMultiColumn(row, ++column))
2736 len += clen[column] + 4;
2737 os << docstring(len, ch);
2752 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2753 vector<unsigned int> const & clen) const
2755 idx_type const fcell = getFirstCellInRow(row);
2756 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2759 for (idx_type i = fcell; i < n; ++i) {
2760 if (bottomLine(i)) {
2769 for (idx_type i = fcell; i < n; ++i) {
2770 if (bottomLine(i)) {
2780 col_type column = cellColumn(i);
2781 int len = clen[column];
2782 while (column < column_info.size() -1
2783 && isPartOfMultiColumn(row, ++column))
2784 len += clen[column] + 4;
2785 os << docstring(len, ch);
2786 if (bottomLine(i)) {
2800 void Tabular::plaintextPrintCell(odocstream & os,
2801 OutputParams const & runparams,
2802 idx_type cell, row_type row, col_type column,
2803 vector<unsigned int> const & clen,
2804 bool onlydata) const
2806 odocstringstream sstr;
2807 cellInset(cell)->plaintext(sstr, runparams);
2819 unsigned int len1 = sstr.str().length();
2820 unsigned int len2 = clen[column];
2821 while (column < column_info.size() -1
2822 && isPartOfMultiColumn(row, ++column))
2823 len2 += clen[column] + 4;
2826 switch (getAlignment(cell)) {
2828 case LYX_ALIGN_LEFT:
2831 case LYX_ALIGN_RIGHT:
2835 case LYX_ALIGN_CENTER:
2841 os << docstring(len1, ' ') << sstr.str()
2842 << docstring(len2, ' ');
2844 if (rightLine(cell))
2851 void Tabular::plaintext(odocstream & os,
2852 OutputParams const & runparams, int const depth,
2853 bool onlydata, char_type delim) const
2855 // first calculate the width of the single columns
2856 vector<unsigned int> clen(column_info.size());
2859 // first all non (real) multicolumn cells!
2860 for (col_type j = 0; j < column_info.size(); ++j) {
2862 for (row_type i = 0; i < row_info.size(); ++i) {
2863 idx_type cell = cellIndex(i, j);
2864 if (isMultiColumnReal(cell))
2866 odocstringstream sstr;
2867 cellInset(cell)->plaintext(sstr, runparams);
2868 if (clen[j] < sstr.str().length())
2869 clen[j] = sstr.str().length();
2872 // then all (real) multicolumn cells!
2873 for (col_type j = 0; j < column_info.size(); ++j) {
2874 for (row_type i = 0; i < row_info.size(); ++i) {
2875 idx_type cell = cellIndex(i, j);
2876 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2878 odocstringstream sstr;
2879 cellInset(cell)->plaintext(sstr, runparams);
2880 int len = int(sstr.str().length());
2881 idx_type const n = columnSpan(cell);
2882 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2884 if (len > int(clen[j + n - 1]))
2885 clen[j + n - 1] = len;
2890 for (row_type i = 0; i < row_info.size(); ++i) {
2891 if (!onlydata && plaintextTopHLine(os, i, clen))
2892 os << docstring(depth * 2, ' ');
2893 for (col_type j = 0; j < column_info.size(); ++j) {
2894 if (isPartOfMultiColumn(i, j))
2896 if (onlydata && j > 0)
2897 // we don't use operator<< for single UCS4 character.
2898 // see explanation in docstream.h
2900 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2905 os << docstring(depth * 2, ' ');
2906 if (plaintextBottomHLine(os, i, clen))
2907 os << docstring(depth * 2, ' ');
2913 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2915 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2919 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2920 col_type column) const
2922 return cell_info[row][column].inset;
2926 void Tabular::setCellInset(row_type row, col_type column,
2927 shared_ptr<InsetTableCell> ins) const
2929 CellData & cd = cell_info[row][column];
2934 void Tabular::validate(LaTeXFeatures & features) const
2936 features.require("NeedTabularnewline");
2938 features.require("booktabs");
2939 if (is_long_tabular)
2940 features.require("longtable");
2942 features.require("rotating");
2943 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2944 if (getVAlignment(cell) != LYX_VALIGN_TOP
2945 || !getPWidth(cell).zero())
2946 features.require("array");
2947 cellInset(cell)->validate(features);
2952 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2954 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2955 ParagraphList::const_iterator cit = parlist.begin();
2956 ParagraphList::const_iterator end = parlist.end();
2958 for (; cit != end; ++cit)
2959 for (int i = 0; i < cit->size(); ++i)
2960 if (cit->isNewline(i))
2967 /////////////////////////////////////////////////////////////////////
2971 /////////////////////////////////////////////////////////////////////
2973 InsetTableCell::InsetTableCell(Buffer * buf)
2974 : InsetText(buf, InsetText::PlainLayout), isFixedWidth(false),
2975 contentAlign(LYX_ALIGN_CENTER)
2979 bool InsetTableCell::forcePlainLayout(idx_type) const
2981 return !isFixedWidth;
2985 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2987 return isFixedWidth;
2991 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2992 FuncStatus & status) const
2995 switch (cmd.action) {
2997 enabled = !forcePlainLayout();
2999 case LFUN_LAYOUT_PARAGRAPH:
3000 enabled = allowParagraphCustomization();
3003 return InsetText::getStatus(cur, cmd, status);
3005 status.setEnabled(enabled);
3009 docstring InsetTableCell::asString(bool intoInsets)
3012 if (paragraphs().empty())
3014 ParagraphList::const_iterator it = paragraphs().begin();
3015 ParagraphList::const_iterator en = paragraphs().end();
3017 for (; it != en; ++it) {
3022 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
3028 docstring InsetTableCell::xhtml(XHTMLStream & xs, OutputParams const & rp) const
3031 return InsetText::insetAsXHTML(xs, rp, InsetText::JustText);
3032 return InsetText::xhtml(xs, rp);
3037 /////////////////////////////////////////////////////////////////////
3041 /////////////////////////////////////////////////////////////////////
3043 InsetTabular::InsetTabular(Buffer * buf, row_type rows,
3045 : Inset(buf), tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
3046 rowselect_(false), colselect_(false)
3051 InsetTabular::InsetTabular(InsetTabular const & tab)
3052 : Inset(tab), tabular(tab.tabular), scx_(0)
3057 InsetTabular::~InsetTabular()
3059 hideDialogs("tabular", this);
3063 void InsetTabular::setBuffer(Buffer & buf)
3065 tabular.setBuffer(buf);
3066 Inset::setBuffer(buf);
3070 bool InsetTabular::insetAllowed(InsetCode code) const
3072 if (code == MATHMACRO_CODE
3073 || (code == CAPTION_CODE && !tabular.is_long_tabular))
3080 void InsetTabular::write(ostream & os) const
3082 os << "Tabular" << endl;
3087 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
3089 // FIXME: depending on the selection state, we could offer a different menu.
3090 return from_ascii("context-tabular");
3094 void InsetTabular::read(Lexer & lex)
3096 //bool const old_format = (lex.getString() == "\\LyXTable");
3104 string token = lex.getString();
3105 while (lex && token != "\\end_inset") {
3107 token = lex.getString();
3110 lex.printError("Missing \\end_inset at this point. ");
3114 int InsetTabular::rowFromY(Cursor & cur, int y) const
3116 // top y coordinate of tabular
3117 int h = yo(cur.bv()) - tabular.rowAscent(0);
3118 size_t nrows = tabular.row_info.size();
3120 for (; r < nrows && y > h; ++r) {
3121 h += tabular.rowAscent(r);
3122 h += tabular.rowDescent(r);
3123 h += tabular.getAdditionalHeight(r);
3129 int InsetTabular::columnFromX(Cursor & cur, int x) const
3131 // left x coordinate of tabular
3132 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
3133 size_t ncols = tabular.column_info.size();
3135 for (; c < ncols && x > w; ++c)
3136 w += tabular.columnWidth(c);
3141 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
3143 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
3144 // mi.base.textwidth << "\n";
3147 LASSERT(false, /**/);
3151 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
3154 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3155 if (tabular.isPartOfMultiColumn(i, j))
3156 // Multicolumn cell, but not first one
3161 if (tabular.cell_info[i][j].multicolumn ==
3162 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
3163 p_width = tabular.cellInfo(cell).p_width;
3165 p_width = tabular.column_info[j].p_width;
3166 if (!p_width.zero())
3167 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
3168 tabular.cellInset(cell)->metrics(m, dim);
3169 if (!p_width.zero())
3170 dim.wid = m.base.textwidth;
3171 tabular.setCellWidth(cell, dim.wid);
3172 maxAsc = max(maxAsc, dim.asc);
3173 maxDesc = max(maxDesc, dim.des);
3176 int const top_space = tabular.row_info[i].top_space_default ?
3177 default_line_space :
3178 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
3179 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
3180 int const bottom_space = tabular.row_info[i].bottom_space_default ?
3181 default_line_space :
3182 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
3183 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
3185 tabular.updateColumnWidths();
3186 dim.asc = tabular.rowAscent(0);
3187 dim.des = tabular.height() - dim.asc;
3188 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
3191 bool InsetTabular::isCellSelected(Cursor & cur, row_type row, col_type col)
3194 if (&cur.inset() == this && cur.selection()) {
3195 if (cur.selIsMultiCell()) {
3198 getSelection(cur, rs, re, cs, ce);
3200 if (col >= cs && col <= ce && row >= rs && row <= re)
3203 if (col == tabular.cellColumn(cur.idx())
3204 && row == tabular.cellRow(cur.idx())) {
3205 CursorSlice const & beg = cur.selBegin();
3206 CursorSlice const & end = cur.selEnd();
3208 if ((end.lastpos() > 0 || end.lastpit() > 0)
3209 && end.pos() == end.lastpos() && beg.pos() == 0
3210 && end.pit() == end.lastpit() && beg.pit() == 0)
3218 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
3220 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
3221 BufferView * bv = pi.base.bv;
3222 Cursor & cur = pi.base.bv->cursor();
3224 // FIXME: As the full background is painted in drawSelection(),
3225 // we have no choice but to do a full repaint for the Text cells.
3226 pi.full_repaint = true;
3228 resetPos(bv->cursor());
3231 x += ADD_TO_TABULAR_WIDTH;
3233 bool const original_drawing_state = pi.pain.isDrawingEnabled();
3234 bool const original_selection_state = pi.selected;
3237 first_visible_cell = Tabular::npos;
3238 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
3240 int const a = tabular.rowAscent(i);
3241 int const d = tabular.rowDescent(i);
3242 idx = tabular.cellIndex(i, 0);
3243 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
3244 if (tabular.isPartOfMultiColumn(i, j))
3246 if (first_visible_cell == Tabular::npos)
3247 first_visible_cell = idx;
3249 pi.selected |= isCellSelected(cur, i, j);
3250 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
3251 // Cache the Inset position.
3252 bv->coordCache().insets().add(cell(idx).get(), cx, y);
3253 if (nx + tabular.columnWidth(idx) < 0
3254 || nx > bv->workWidth()
3256 || y - a > bv->workHeight()) {
3257 pi.pain.setDrawingEnabled(false);
3258 cell(idx)->draw(pi, cx, y);
3259 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3260 pi.pain.setDrawingEnabled(original_drawing_state);
3262 cell(idx)->draw(pi, cx, y);
3263 drawCellLines(pi.pain, nx, y, i, idx, pi.change_);
3265 nx += tabular.columnWidth(idx);
3267 pi.selected = original_selection_state;
3270 if (i + 1 < tabular.row_info.size())
3271 y += d + tabular.rowAscent(i + 1) +
3272 tabular.getAdditionalHeight(i + 1);
3277 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3279 Cursor & cur = pi.base.bv->cursor();
3282 x += scx_ + ADD_TO_TABULAR_WIDTH;
3284 // FIXME: it is wrong to completely paint the background
3285 // if we want to do single row painting.
3287 // Paint background of current tabular
3288 int const w = tabular.width();
3289 int const h = tabular.height();
3290 int yy = y - tabular.rowAscent(0);
3291 pi.pain.fillRectangle(x, yy, w, h, pi.backgroundColor(this));
3293 if (!cur.selection())
3295 if (&cur.inset() != this)
3300 bool const full_cell_selected = isCellSelected(cur,
3301 tabular.cellRow(cur.idx()), tabular.cellColumn(cur.idx()));
3303 if (cur.selIsMultiCell() || full_cell_selected) {
3304 y -= tabular.rowAscent(0);
3305 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3306 int const a = tabular.rowAscent(j);
3307 int const h = a + tabular.rowDescent(j);
3309 y += tabular.getAdditionalHeight(j);
3310 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3311 if (tabular.isPartOfMultiColumn(j, i))
3313 idx_type const cell =
3314 tabular.cellIndex(j, i);
3315 int const w = tabular.columnWidth(cell);
3316 if (isCellSelected(cur, j, i))
3317 pi.pain.fillRectangle(xx, y, w, h, Color_selection);
3324 x += cellXPos(cur.idx());
3325 x += tabular.getBeginningOfTextInCell(cur.idx());
3326 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3331 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3332 row_type row, idx_type cell, Change const & change) const
3334 int x2 = x + tabular.columnWidth(cell);
3335 bool on_off = false;
3336 Color col = Color_tabularline;
3337 Color onoffcol = Color_tabularonoffline;
3339 if (change.changed()) {
3340 col = change.color();
3341 onoffcol = change.color();
3344 if (!tabular.topAlreadyDrawn(cell)) {
3345 on_off = !tabular.topLine(cell);
3346 pain.line(x, y - tabular.rowAscent(row),
3347 x2, y - tabular.rowAscent(row),
3348 on_off ? onoffcol : col,
3349 on_off ? Painter::line_onoffdash : Painter::line_solid);
3351 on_off = !tabular.bottomLine(cell);
3352 pain.line(x, y + tabular.rowDescent(row),
3353 x2, y + tabular.rowDescent(row),
3354 on_off ? onoffcol : col,
3355 on_off ? Painter::line_onoffdash : Painter::line_solid);
3356 if (!tabular.leftAlreadyDrawn(cell)) {
3357 on_off = !tabular.leftLine(cell);
3358 pain.line(x, y - tabular.rowAscent(row),
3359 x, y + tabular.rowDescent(row),
3360 on_off ? onoffcol : col,
3361 on_off ? Painter::line_onoffdash : Painter::line_solid);
3363 on_off = !tabular.rightLine(cell);
3364 pain.line(x2 - tabular.getAdditionalWidth(cell),
3365 y - tabular.rowAscent(row),
3366 x2 - tabular.getAdditionalWidth(cell),
3367 y + tabular.rowDescent(row),
3368 on_off ? onoffcol : col,
3369 on_off ? Painter::line_onoffdash : Painter::line_solid);
3373 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3375 //lyxerr << "InsetTabular::edit: " << this << endl;
3377 cur.setSelection(false);
3380 if (isRightToLeft(cur))
3381 cur.idx() = tabular.getLastCellInRow(0);
3387 if (isRightToLeft(cur))
3388 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3390 cur.idx() = tabular.numberofcells - 1;
3392 cur.pos() = cur.lastpos(); // FIXME crude guess
3394 cur.setCurrentFont();
3395 // FIXME: this accesses the position cache before it is initialized
3397 //cur.bv().fitCursor();
3401 void InsetTabular::updateLabels(ParIterator const & it, UpdateType utype)
3403 // In a longtable, tell captions what the current float is
3404 Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
3405 string const saveflt = cnts.current_float();
3406 if (tabular.is_long_tabular)
3407 cnts.current_float("table");
3409 ParIterator it2 = it;
3411 size_t const end = it2.nargs();
3412 for ( ; it2.idx() < end; it2.top().forwardIdx())
3413 buffer().updateLabels(it2, utype);
3416 if (tabular.is_long_tabular)
3417 cnts.current_float(saveflt);
3421 void InsetTabular::addToToc(DocIterator const & cpit)
3423 DocIterator dit = cpit;
3425 size_t const end = dit.nargs();
3426 for ( ; dit.idx() < end; dit.top().forwardIdx())
3427 cell(dit.idx())->addToToc(dit);
3431 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3433 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3434 << "\n cur:" << cur);
3435 CursorSlice sl = cur.top();
3436 Cursor & bvcur = cur.bv().cursor();
3438 switch (cmd.action) {
3440 case LFUN_MOUSE_PRESS: {
3441 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3443 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3444 || cmd.x > xo(cur.bv()) + tabular.width()) {
3445 row_type r = rowFromY(cur, cmd.y);
3446 cur.idx() = tabular.getFirstCellInRow(r);
3449 cur.idx() = tabular.getLastCellInRow(r);
3450 cur.pos() = cur.lastpos();
3451 cur.setSelection(true);
3457 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3458 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3459 || cmd.y > y0 + tabular.height()) {
3460 col_type c = columnFromX(cur, cmd.x);
3461 cur.idx() = tabular.cellIndex(0, c);
3464 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3465 cur.pos() = cur.lastpos();
3466 cur.setSelection(true);
3471 // do not reset cursor/selection if we have selected
3472 // some cells (bug 2715).
3473 if (cmd.button() == mouse_button::button3
3474 && &bvcur.selBegin().inset() == this
3475 && bvcur.selIsMultiCell())
3478 // Let InsetTableCell do it
3479 cell(cur.idx())->dispatch(cur, cmd);
3482 case LFUN_MOUSE_MOTION:
3483 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3484 if (cmd.button() == mouse_button::button1) {
3485 // only accept motions to places not deeper nested than the real anchor
3486 if (!bvcur.anchor_.hasPart(cur)) {
3490 // select (additional) row
3492 row_type r = rowFromY(cur, cmd.y);
3493 cur.idx() = tabular.getLastCellInRow(r);
3494 // we need to reset the cursor's pit and pos now, as the old ones
3495 // may no longer be valid.
3498 bvcur.setCursor(cur);
3499 bvcur.setSelection(true);
3502 // select (additional) column
3504 col_type c = columnFromX(cur, cmd.x);
3505 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3506 // we need to reset the cursor's pit and pos now, as the old ones
3507 // may no longer be valid.
3510 bvcur.setCursor(cur);
3511 bvcur.setSelection(true);
3514 // only update if selection changes
3515 if (bvcur.idx() == cur.idx() &&
3516 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3518 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3519 bvcur.setCursor(cur);
3520 bvcur.setSelection(true);
3521 // if this is a multicell selection, we just set the cursor to
3522 // the beginning of the cell's text.
3523 if (bvcur.selIsMultiCell()) {
3524 bvcur.pit() = bvcur.lastpit();
3525 bvcur.pos() = bvcur.lastpos();
3530 case LFUN_MOUSE_RELEASE:
3535 case LFUN_CELL_BACKWARD:
3537 cur.setSelection(false);
3540 case LFUN_CELL_FORWARD:
3542 cur.setSelection(false);
3545 case LFUN_CHAR_FORWARD_SELECT:
3546 case LFUN_CHAR_FORWARD:
3547 case LFUN_CHAR_BACKWARD_SELECT:
3548 case LFUN_CHAR_BACKWARD:
3549 case LFUN_CHAR_RIGHT_SELECT:
3550 case LFUN_CHAR_RIGHT:
3551 case LFUN_CHAR_LEFT_SELECT:
3552 case LFUN_CHAR_LEFT: {
3553 // determine whether we move to next or previous cell, where to enter
3554 // the new cell from, and which command to "finish" (i.e., exit the
3557 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3558 FuncCode finish_lfun;
3560 if (cmd.action == LFUN_CHAR_FORWARD
3561 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3563 finish_lfun = LFUN_FINISHED_FORWARD;
3565 else if (cmd.action == LFUN_CHAR_BACKWARD
3566 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3568 finish_lfun = LFUN_FINISHED_BACKWARD;
3570 // LEFT or RIGHT commands --- the interpretation will depend on the
3571 // table's direction.
3573 bool const right = cmd.action == LFUN_CHAR_RIGHT
3574 || cmd.action == LFUN_CHAR_RIGHT_SELECT;
3575 next_cell = isRightToLeft(cur) != right;
3577 if (lyxrc.visual_cursor)
3578 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3580 finish_lfun = right ? LFUN_FINISHED_RIGHT : LFUN_FINISHED_LEFT;
3583 bool const select = cmd.action == LFUN_CHAR_FORWARD_SELECT ||
3584 cmd.action == LFUN_CHAR_BACKWARD_SELECT ||
3585 cmd.action == LFUN_CHAR_RIGHT_SELECT ||
3586 cmd.action == LFUN_CHAR_LEFT_SELECT;
3588 // If we have a multicell selection or we're
3589 // not doing some LFUN_*_SELECT thing anyway...
3590 if (!cur.selIsMultiCell() || !select) {
3591 col_type const c = tabular.cellColumn(cur.idx());
3592 row_type const r = tabular.cellRow(cur.idx());
3593 // Are we trying to select the whole cell and is the whole cell
3594 // not yet selected?
3595 bool const select_whole = select && !isCellSelected(cur, r, c) &&
3596 ((next_cell && cur.pit() == cur.lastpit()
3597 && cur.pos() == cur.lastpos())
3598 || (!next_cell && cur.pit() == 0 && cur.pos() == 0));
3600 bool const empty_cell = cur.lastpos() == 0 && cur.lastpit() == 0;
3602 // ...try to dispatch to the cell's inset.
3603 cell(cur.idx())->dispatch(cur, cmd);
3605 // When we already have a selection we want to select the whole cell
3606 // before going to the next cell.
3607 if (select_whole && !empty_cell){
3608 getText(cur.idx())->selectAll(cur);
3613 // FIXME: When we support the selection of an empty cell, remove
3614 // the !empty_cell from this condition. For now we jump to the next
3615 // cell if the current cell is empty.
3616 if (cur.result().dispatched() && !empty_cell)
3620 // move to next/prev cell, as appropriate
3621 // note that we will always do this if we're selecting and we have
3622 // a multicell selection
3623 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3624 << " cell from: " << int(entry_from));
3626 moveNextCell(cur, entry_from);
3628 movePrevCell(cur, entry_from);
3629 // if we're exiting the table, call the appropriate FINISHED lfun
3630 if (sl == cur.top())
3631 cmd = FuncRequest(finish_lfun);
3638 case LFUN_DOWN_SELECT:
3640 if (!(cur.selection() && cur.selIsMultiCell()))
3641 cell(cur.idx())->dispatch(cur, cmd);
3643 cur.dispatched(); // override the cell's decision
3644 if (sl == cur.top()) {
3645 // if our Text didn't do anything to the cursor
3646 // then we try to put the cursor into the cell below
3647 // setting also the right targetX.
3648 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
3649 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3650 cur.idx() = tabular.cellBelow(cur.idx());
3652 TextMetrics const & tm =
3653 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3654 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3655 cur.setCurrentFont();
3658 if (sl == cur.top()) {
3659 // we trick it to go to forward after leaving the
3661 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3664 if (cur.selIsMultiCell()) {
3665 cur.pit() = cur.lastpit();
3666 cur.pos() = cur.lastpos();
3667 cur.setCurrentFont();
3672 case LFUN_UP_SELECT:
3674 if (!(cur.selection() && cur.selIsMultiCell()))
3675 cell(cur.idx())->dispatch(cur, cmd);
3676 cur.dispatched(); // override the cell's decision
3677 if (sl == cur.top()) {
3678 // if our Text didn't do anything to the cursor
3679 // then we try to put the cursor into the cell above
3680 // setting also the right targetX.
3681 cur.selHandle(cmd.action == LFUN_UP_SELECT);
3682 if (tabular.cellRow(cur.idx()) != 0) {
3683 cur.idx() = tabular.cellAbove(cur.idx());
3684 cur.pit() = cur.lastpit();
3685 Text const * text = cell(cur.idx())->getText(0);
3686 TextMetrics const & tm = cur.bv().textMetrics(text);
3687 ParagraphMetrics const & pm =
3688 tm.parMetrics(cur.lastpit());
3689 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3690 cur.setCurrentFont();
3693 if (sl == cur.top()) {
3694 cmd = FuncRequest(LFUN_UP);
3697 if (cur.selIsMultiCell()) {
3699 cur.pos() = cur.lastpos();
3700 cur.setCurrentFont();
3705 // case LFUN_SCREEN_DOWN: {
3706 // //if (hasSelection())
3707 // // cur.selection() = false;
3708 // col_type const col = tabular.cellColumn(cur.idx());
3709 // int const t = cur.bv().top_y() + cur.bv().height();
3710 // if (t < yo() + tabular.getHeightOfTabular()) {
3711 // cur.bv().scrollDocView(t);
3712 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3714 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3721 // case LFUN_SCREEN_UP: {
3722 // //if (hasSelection())
3723 // // cur.selection() = false;
3724 // col_type const col = tabular.cellColumn(cur.idx());
3725 // int const t = cur.bv().top_y() + cur.bv().height();
3727 // cur.bv().scrollDocView(t);
3731 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3735 // cur.par() = cur.lastpar();
3736 // cur.pos() = cur.lastpos();
3740 case LFUN_LAYOUT_TABULAR:
3741 cur.bv().showDialog("tabular", params2string(*this), this);
3744 case LFUN_INSET_DIALOG_UPDATE:
3745 cur.bv().updateDialog("tabular", params2string(*this));
3748 case LFUN_TABULAR_FEATURE:
3749 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3753 // insert file functions
3754 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3755 case LFUN_FILE_INSERT_PLAINTEXT:
3757 if (FileName::isAbsolute(to_utf8(cmd.argument()))) {
3758 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3759 FileName(to_utf8(cmd.argument())));
3762 cur.recordUndoInset(INSERT_UNDO);
3763 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3764 // content has been replaced,
3765 // so cursor might be invalid
3766 cur.pos() = cur.lastpos();
3767 cur.pit() = cur.lastpit();
3768 bvcur.setCursor(cur);
3775 if (cur.selIsMultiCell()) {
3776 if (copySelection(cur)) {
3777 cur.recordUndoInset(DELETE_UNDO);
3781 cell(cur.idx())->dispatch(cur, cmd);
3784 case LFUN_SELF_INSERT:
3785 if (cur.selIsMultiCell()) {
3786 cur.recordUndoInset(DELETE_UNDO);
3789 cell(cur.idx())->dispatch(cur, cmd);
3792 case LFUN_CHAR_DELETE_BACKWARD:
3793 case LFUN_CHAR_DELETE_FORWARD:
3794 if (cur.selIsMultiCell()) {
3795 cur.recordUndoInset(DELETE_UNDO);
3798 cell(cur.idx())->dispatch(cur, cmd);
3802 if (!cur.selection())
3804 if (cur.selIsMultiCell()) {
3808 cell(cur.idx())->dispatch(cur, cmd);
3811 case LFUN_CLIPBOARD_PASTE:
3812 case LFUN_PRIMARY_SELECTION_PASTE: {
3813 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3814 theClipboard().getAsText() :
3815 theSelection().get();
3818 // pass to InsertPlaintextString, but
3819 // only if we have multi-cell content
3820 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3821 cur.recordUndoInset(INSERT_UNDO);
3822 if (insertPlaintextString(cur.bv(), clip, false)) {
3823 // content has been replaced,
3824 // so cursor might be invalid
3825 cur.pos() = cur.lastpos();
3826 cur.pit() = cur.lastpit();
3827 bvcur.setCursor(cur);
3831 // Let the cell handle normal text
3832 cell(cur.idx())->dispatch(cur, cmd);
3837 if (!tabularStackDirty()) {
3838 if (!cur.selIsMultiCell())
3839 cell(cur.idx())->dispatch(cur, cmd);
3842 if (theClipboard().isInternal() ||
3843 (!theClipboard().hasInternal() && theClipboard().hasLyXContents())) {
3844 cur.recordUndoInset(INSERT_UNDO);
3845 pasteClipboard(cur);
3849 case LFUN_FONT_EMPH:
3850 case LFUN_FONT_BOLD:
3851 case LFUN_FONT_BOLDSYMBOL:
3852 case LFUN_FONT_ROMAN:
3853 case LFUN_FONT_NOUN:
3854 case LFUN_FONT_ITAL:
3855 case LFUN_FONT_FRAK:
3856 case LFUN_FONT_TYPEWRITER:
3857 case LFUN_FONT_SANS:
3858 case LFUN_TEXTSTYLE_APPLY:
3859 case LFUN_TEXTSTYLE_UPDATE:
3860 case LFUN_FONT_SIZE:
3861 case LFUN_FONT_UNDERLINE:
3862 case LFUN_FONT_STRIKEOUT:
3863 case LFUN_FONT_UULINE:
3864 case LFUN_FONT_UWAVE:
3866 case LFUN_WORD_CAPITALIZE:
3867 case LFUN_WORD_UPCASE:
3868 case LFUN_WORD_LOWCASE:
3869 case LFUN_CHARS_TRANSPOSE:
3870 if (cur.selIsMultiCell()) {
3873 getSelection(cur, rs, re, cs, ce);
3874 Cursor tmpcur = cur;
3875 for (row_type i = rs; i <= re; ++i) {
3876 for (col_type j = cs; j <= ce; ++j) {
3877 // cursor follows cell:
3878 tmpcur.idx() = tabular.cellIndex(i, j);
3879 // select this cell only:
3882 tmpcur.resetAnchor();
3883 tmpcur.pit() = tmpcur.lastpit();
3884 tmpcur.pos() = tmpcur.top().lastpos();
3885 tmpcur.setCursor(tmpcur);
3886 tmpcur.setSelection();
3887 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3892 cell(cur.idx())->dispatch(cur, cmd);
3896 case LFUN_INSET_SETTINGS:
3897 // relay this lfun to Inset, not to the cell.
3898 Inset::doDispatch(cur, cmd);
3902 // we try to handle this event in the insets dispatch function.
3903 cell(cur.idx())->dispatch(cur, cmd);
3909 // function sets an object as defined in func_status.h:
3910 // states OK, Unknown, Disabled, On, Off.
3911 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3912 FuncStatus & status) const
3914 switch (cmd.action) {
3915 case LFUN_TABULAR_FEATURE: {
3916 int action = Tabular::LAST_ACTION;
3918 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3919 string const tmp = tabularFeature[i].feature;
3920 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3921 action = tabularFeature[i].action;
3925 if (action == Tabular::LAST_ACTION) {
3927 status.unknown(true);
3931 string const argument
3932 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3934 row_type sel_row_start = 0;
3935 row_type sel_row_end = 0;
3936 col_type sel_col_start = 0;
3937 col_type sel_col_end = 0;
3938 Tabular::ltType dummyltt;
3941 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3944 case Tabular::SET_PWIDTH:
3945 case Tabular::SET_MPWIDTH:
3946 case Tabular::SET_SPECIAL_COLUMN:
3947 case Tabular::SET_SPECIAL_MULTI:
3948 case Tabular::APPEND_ROW:
3949 case Tabular::APPEND_COLUMN:
3950 case Tabular::DELETE_ROW:
3951 case Tabular::DELETE_COLUMN:
3952 case Tabular::COPY_ROW:
3953 case Tabular::COPY_COLUMN:
3954 case Tabular::SET_TOP_SPACE:
3955 case Tabular::SET_BOTTOM_SPACE:
3956 case Tabular::SET_INTERLINE_SPACE:
3960 case Tabular::MULTICOLUMN:
3961 // If a row is set as longtable caption, it must not be allowed
3962 // to unset that this row is a multicolumn.
3963 status.setEnabled(sel_row_start == sel_row_end
3964 && !tabular.ltCaption(tabular.cellRow(cur.idx())));
3965 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3968 case Tabular::SET_ALL_LINES:
3969 case Tabular::UNSET_ALL_LINES:
3970 case Tabular::SET_BORDER_LINES:
3971 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3974 case Tabular::TOGGLE_LINE_TOP:
3975 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3976 status.setOnOff(tabular.topLine(cur.idx()));
3979 case Tabular::TOGGLE_LINE_BOTTOM:
3980 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3981 status.setOnOff(tabular.bottomLine(cur.idx()));
3984 case Tabular::TOGGLE_LINE_LEFT:
3985 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3986 status.setOnOff(tabular.leftLine(cur.idx()));
3989 case Tabular::TOGGLE_LINE_RIGHT:
3990 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3991 status.setOnOff(tabular.rightLine(cur.idx()));
3994 case Tabular::M_ALIGN_LEFT:
3996 case Tabular::ALIGN_LEFT:
3997 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
4000 case Tabular::M_ALIGN_RIGHT:
4002 case Tabular::ALIGN_RIGHT:
4003 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
4006 case Tabular::M_ALIGN_CENTER:
4008 case Tabular::ALIGN_CENTER:
4009 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
4012 case Tabular::ALIGN_BLOCK:
4013 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
4014 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
4017 case Tabular::M_VALIGN_TOP:
4019 case Tabular::VALIGN_TOP:
4021 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
4024 case Tabular::M_VALIGN_BOTTOM:
4026 case Tabular::VALIGN_BOTTOM:
4028 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
4031 case Tabular::M_VALIGN_MIDDLE:
4033 case Tabular::VALIGN_MIDDLE:
4035 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
4038 case Tabular::SET_LONGTABULAR:
4039 status.setOnOff(tabular.is_long_tabular);
4042 case Tabular::UNSET_LONGTABULAR:
4043 status.setOnOff(!tabular.is_long_tabular);
4046 case Tabular::TOGGLE_ROTATE_TABULAR:
4047 case Tabular::SET_ROTATE_TABULAR:
4048 status.setOnOff(tabular.rotate);
4051 case Tabular::TABULAR_VALIGN_TOP:
4052 status.setOnOff(tabular.tabular_valignment
4053 == Tabular::LYX_VALIGN_TOP);
4055 case Tabular::TABULAR_VALIGN_MIDDLE:
4056 status.setOnOff(tabular.tabular_valignment
4057 == Tabular::LYX_VALIGN_MIDDLE);
4059 case Tabular::TABULAR_VALIGN_BOTTOM:
4060 status.setOnOff(tabular.tabular_valignment
4061 == Tabular::LYX_VALIGN_BOTTOM);
4064 case Tabular::LONGTABULAR_ALIGN_LEFT:
4065 status.setOnOff(tabular.longtabular_alignment
4066 == Tabular::LYX_LONGTABULAR_ALIGN_LEFT);
4068 case Tabular::LONGTABULAR_ALIGN_CENTER:
4069 status.setOnOff(tabular.longtabular_alignment
4070 == Tabular::LYX_LONGTABULAR_ALIGN_CENTER);
4072 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4073 status.setOnOff(tabular.longtabular_alignment
4074 == Tabular::LYX_LONGTABULAR_ALIGN_RIGHT);
4077 case Tabular::UNSET_ROTATE_TABULAR:
4078 status.setOnOff(!tabular.rotate);
4081 case Tabular::TOGGLE_ROTATE_CELL:
4082 case Tabular::SET_ROTATE_CELL:
4083 status.setOnOff(!oneCellHasRotationState(false,
4084 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4087 case Tabular::UNSET_ROTATE_CELL:
4088 status.setOnOff(!oneCellHasRotationState(true,
4089 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
4092 case Tabular::SET_USEBOX:
4093 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
4096 // every row can only be one thing:
4097 // either a footer or header or caption
4098 case Tabular::SET_LTFIRSTHEAD:
4099 status.setEnabled(sel_row_start == sel_row_end
4100 && !tabular.ltCaption(sel_row_start));
4101 status.setOnOff(tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4104 case Tabular::UNSET_LTFIRSTHEAD:
4105 status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
4108 case Tabular::SET_LTHEAD:
4109 status.setEnabled(sel_row_start == sel_row_end
4110 && !tabular.ltCaption(sel_row_start));
4111 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
4114 case Tabular::UNSET_LTHEAD:
4115 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
4118 case Tabular::SET_LTFOOT:
4119 status.setEnabled(sel_row_start == sel_row_end
4120 && !tabular.ltCaption(sel_row_start));
4121 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4124 case Tabular::UNSET_LTFOOT:
4125 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
4128 case Tabular::SET_LTLASTFOOT:
4129 status.setEnabled(sel_row_start == sel_row_end
4130 && !tabular.ltCaption(sel_row_start));
4131 status.setOnOff(tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4134 case Tabular::UNSET_LTLASTFOOT:
4135 status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
4138 case Tabular::SET_LTNEWPAGE:
4139 status.setOnOff(tabular.getLTNewPage(sel_row_start));
4142 // only one row can be the caption
4143 case Tabular::TOGGLE_LTCAPTION:
4144 status.setEnabled(sel_row_start == sel_row_end
4145 && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
4146 && !tabular.getRowOfLTHead(sel_row_start, dummyltt)
4147 && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
4148 && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
4149 && (!tabular.haveLTCaption()
4150 || tabular.ltCaption(sel_row_start)));
4151 status.setOnOff(tabular.ltCaption(sel_row_start));
4154 case Tabular::SET_BOOKTABS:
4155 status.setOnOff(tabular.use_booktabs);
4158 case Tabular::UNSET_BOOKTABS:
4159 status.setOnOff(!tabular.use_booktabs);
4164 status.setEnabled(false);
4170 // These are only enabled inside tabular
4171 case LFUN_CELL_BACKWARD:
4172 case LFUN_CELL_FORWARD:
4173 status.setEnabled(true);
4176 // disable these with multiple cells selected
4177 case LFUN_INSET_INSERT:
4178 case LFUN_TABULAR_INSERT:
4179 case LFUN_FLEX_INSERT:
4180 case LFUN_FLOAT_INSERT:
4181 case LFUN_FLOAT_WIDE_INSERT:
4182 case LFUN_FOOTNOTE_INSERT:
4183 case LFUN_MARGINALNOTE_INSERT:
4184 case LFUN_MATH_INSERT:
4185 case LFUN_MATH_MODE:
4186 case LFUN_MATH_MUTATE:
4187 case LFUN_MATH_DISPLAY:
4188 case LFUN_NOTE_INSERT:
4189 case LFUN_OPTIONAL_INSERT:
4190 case LFUN_BOX_INSERT:
4191 case LFUN_BRANCH_INSERT:
4192 case LFUN_PHANTOM_INSERT:
4193 case LFUN_WRAP_INSERT:
4194 case LFUN_ERT_INSERT: {
4195 if (cur.selIsMultiCell()) {
4196 status.setEnabled(false);
4199 return cell(cur.idx())->getStatus(cur, cmd, status);
4202 // disable in non-fixed-width cells
4203 case LFUN_NEWLINE_INSERT:
4204 case LFUN_BREAK_PARAGRAPH: {
4205 if (tabular.getPWidth(cur.idx()).zero()) {
4206 status.setEnabled(false);
4209 return cell(cur.idx())->getStatus(cur, cmd, status);
4212 case LFUN_NEWPAGE_INSERT:
4213 status.setEnabled(false);
4217 if (tabularStackDirty() && theClipboard().isInternal()) {
4218 if (cur.selIsMultiCell()) {
4221 getSelection(cur, rs, re, cs, ce);
4222 if (paste_tabular && paste_tabular->column_info.size() == ce - cs + 1
4223 && paste_tabular->row_info.size() == re - rs + 1)
4224 status.setEnabled(true);
4226 status.setEnabled(false);
4227 status.message(_("Selection size should match clipboard content."));
4230 status.setEnabled(true);
4233 return cell(cur.idx())->getStatus(cur, cmd, status);
4235 case LFUN_INSET_SETTINGS:
4236 // relay this lfun to Inset, not to the cell.
4237 return Inset::getStatus(cur, cmd, status);
4239 case LFUN_INSET_MODIFY:
4240 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
4241 status.setEnabled(true);
4247 // we try to handle this event in the insets dispatch function.
4248 return cell(cur.idx())->getStatus(cur, cmd, status);
4253 Inset::DisplayType InsetTabular::display() const
4255 if (tabular.is_long_tabular) {
4256 switch (tabular.longtabular_alignment) {
4257 case Tabular::LYX_LONGTABULAR_ALIGN_LEFT:
4259 case Tabular::LYX_LONGTABULAR_ALIGN_CENTER:
4261 case Tabular::LYX_LONGTABULAR_ALIGN_RIGHT:
4271 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
4273 return tabular.latex(os, runparams);
4277 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
4279 os << '\n'; // output table on a new line
4280 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
4281 tabular.plaintext(os, runparams, dp, false, 0);
4282 return PLAINTEXT_NEWLINE;
4286 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
4291 // FIXME: Why not pass a proper DocIterator here?
4293 // if the table is inside a float it doesn't need the informaltable
4294 // wrapper. Search for it.
4295 for (master = owner(); master; master = master->owner())
4296 if (master->lyxCode() == FLOAT_CODE)
4301 os << "<informaltable>";
4304 ret += tabular.docbook(os, runparams);
4306 os << "</informaltable>";
4313 docstring InsetTabular::xhtml(XHTMLStream & xs, OutputParams const & rp) const
4316 // It'd be better to be able to get this from an InsetLayout, but at present
4317 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4318 xs << html::StartTag("table");
4319 docstring ret = tabular.xhtml(xs, rp);
4320 xs << html::EndTag("table");
4325 void InsetTabular::validate(LaTeXFeatures & features) const
4327 tabular.validate(features);
4329 // It'd be better to be able to get this from an InsetLayout, but at present
4330 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
4331 if (features.runparams().flavor == OutputParams::HTML)
4332 features.addPreambleSnippet("<style type=\"text/css\">\n"
4333 "table { border: 1px solid black; display: inline-block; }\n"
4334 "td { border: 1px solid black; padding: 0.5ex; }\n"
4339 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
4341 return tabular.cellInset(idx);
4345 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
4347 return tabular.cellInset(idx);
4351 void InsetTabular::cursorPos(BufferView const & bv,
4352 CursorSlice const & sl, bool boundary, int & x, int & y) const
4354 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
4356 // y offset correction
4357 int const row = tabular.cellRow(sl.idx());
4358 for (int i = 0; i <= row; ++i) {
4360 y += tabular.rowAscent(i);
4361 y += tabular.getAdditionalHeight(i);
4364 y += tabular.rowDescent(i);
4367 // x offset correction
4368 int const col = tabular.cellColumn(sl.idx());
4369 int idx = tabular.cellIndex(row, 0);
4370 for (int j = 0; j < col; ++j) {
4371 if (tabular.isPartOfMultiColumn(row, j))
4373 x += tabular.columnWidth(idx);
4376 x += tabular.getBeginningOfTextInCell(idx);
4377 x += ADD_TO_TABULAR_WIDTH;
4382 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
4386 Inset const & inset = *tabular.cellInset(cell);
4387 Point o = bv.coordCache().getInsets().xy(&inset);
4388 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
4389 int const xend = xbeg + tabular.columnWidth(cell);
4390 row_type const row = tabular.cellRow(cell);
4391 int const ybeg = o.y_ - tabular.rowAscent(row) -
4392 tabular.getAdditionalHeight(row);
4393 int const yend = o.y_ + tabular.rowDescent(row);
4405 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
4406 // << " ybeg=" << ybeg << " yend=" << yend
4407 // << " xx=" << xx << " yy=" << yy
4408 // << " dist=" << xx + yy << endl;
4413 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
4415 //lyxerr << "InsetTabular::editXY: " << this << endl;
4416 cur.setSelection(false);
4418 cur.idx() = getNearestCell(cur.bv(), x, y);
4420 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
4424 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
4426 cur.idx() = getNearestCell(cur.bv(), x, y);
4427 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4431 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4433 idx_type idx_min = 0;
4434 int dist_min = numeric_limits<int>::max();
4435 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4436 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4437 int const d = dist(bv, i, x, y);
4448 int InsetTabular::cellXPos(idx_type const cell) const
4452 for (; !tabular.isFirstCellInRow(c); --c)
4455 for (; c < cell; ++c)
4456 lx += tabular.columnWidth(c);
4462 void InsetTabular::resetPos(Cursor & cur) const
4464 BufferView & bv = cur.bv();
4465 int const maxwidth = bv.workWidth();
4467 int const scx_old = scx_;
4468 int const i = cur.find(this);
4473 int const X2 = maxwidth;
4474 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4475 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4476 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4479 scx_ = X1 + 20 - x1;
4481 scx_ = X2 - 20 - x2;
4486 // only update if offset changed
4487 if (scx_ != scx_old)
4488 cur.updateFlags(Update::Force | Update::FitCursor);
4492 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4494 if (isRightToLeft(cur)) {
4495 if (tabular.isFirstCellInRow(cur.idx())) {
4496 row_type const row = tabular.cellRow(cur.idx());
4497 if (row == tabular.row_info.size() - 1)
4499 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4506 if (tabular.isLastCell(cur.idx()))
4511 cur.boundary(false);
4513 if (cur.selIsMultiCell()) {
4514 cur.pit() = cur.lastpit();
4515 cur.pos() = cur.lastpos();
4523 // in visual mode, place cursor at extreme left or right
4525 switch(entry_from) {
4527 case ENTRY_DIRECTION_RIGHT:
4528 cur.posVisToRowExtremity(false /* !left */);
4530 case ENTRY_DIRECTION_LEFT:
4531 cur.posVisToRowExtremity(true /* left */);
4533 case ENTRY_DIRECTION_IGNORE:
4534 // nothing to do in this case
4538 cur.setCurrentFont();
4543 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4545 if (isRightToLeft(cur)) {
4546 if (tabular.isLastCellInRow(cur.idx())) {
4547 row_type const row = tabular.cellRow(cur.idx());
4550 cur.idx() = tabular.getFirstCellInRow(row);
4551 cur.idx() = tabular.cellAbove(cur.idx());
4553 if (tabular.isLastCell(cur.idx()))
4558 if (cur.idx() == 0) // first cell
4563 if (cur.selIsMultiCell()) {
4564 cur.pit() = cur.lastpit();
4565 cur.pos() = cur.lastpos();
4570 cur.pit() = cur.lastpit();
4571 cur.pos() = cur.lastpos();
4573 // in visual mode, place cursor at extreme left or right
4575 switch(entry_from) {
4577 case ENTRY_DIRECTION_RIGHT:
4578 cur.posVisToRowExtremity(false /* !left */);
4580 case ENTRY_DIRECTION_LEFT:
4581 cur.posVisToRowExtremity(true /* left */);
4583 case ENTRY_DIRECTION_IGNORE:
4584 // nothing to do in this case
4588 cur.setCurrentFont();
4593 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4595 Tabular::Feature action = Tabular::LAST_ACTION;
4598 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4599 string const tmp = tabularFeature[i].feature;
4601 if (tmp == what.substr(0, tmp.length())) {
4602 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4603 //tabularFeatures[i].feature.length()))
4604 action = tabularFeature[i].action;
4608 if (action == Tabular::LAST_ACTION)
4612 ltrim(what.substr(tabularFeature[i].feature.length()));
4613 tabularFeatures(cur, action, val);
4618 static void checkLongtableSpecial(Tabular::ltType & ltt,
4619 string const & special, bool & flag)
4621 if (special == "dl_above") {
4624 } else if (special == "dl_below") {
4625 ltt.bottomDL = flag;
4627 } else if (special == "empty") {
4636 bool InsetTabular::oneCellHasRotationState(bool rotated,
4637 row_type row_start, row_type row_end,
4638 col_type col_start, col_type col_end) const {
4640 for (row_type i = row_start; i <= row_end; ++i) {
4641 for (col_type j = col_start; j <= col_end; ++j) {
4642 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4651 void InsetTabular::tabularFeatures(Cursor & cur,
4652 Tabular::Feature feature, string const & value)
4654 col_type sel_col_start;
4655 col_type sel_col_end;
4656 row_type sel_row_start;
4657 row_type sel_row_end;
4658 bool setLines = false;
4659 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4660 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4664 case Tabular::M_ALIGN_LEFT:
4665 case Tabular::ALIGN_LEFT:
4666 setAlign = LYX_ALIGN_LEFT;
4669 case Tabular::M_ALIGN_RIGHT:
4670 case Tabular::ALIGN_RIGHT:
4671 setAlign = LYX_ALIGN_RIGHT;
4674 case Tabular::M_ALIGN_CENTER:
4675 case Tabular::ALIGN_CENTER:
4676 setAlign = LYX_ALIGN_CENTER;
4679 case Tabular::ALIGN_BLOCK:
4680 setAlign = LYX_ALIGN_BLOCK;
4683 case Tabular::M_VALIGN_TOP:
4684 case Tabular::VALIGN_TOP:
4685 setVAlign = Tabular::LYX_VALIGN_TOP;
4688 case Tabular::M_VALIGN_BOTTOM:
4689 case Tabular::VALIGN_BOTTOM:
4690 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4693 case Tabular::M_VALIGN_MIDDLE:
4694 case Tabular::VALIGN_MIDDLE:
4695 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4702 cur.recordUndoInset(ATOMIC_UNDO);
4704 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4705 row_type const row = tabular.cellRow(cur.idx());
4706 col_type const column = tabular.cellColumn(cur.idx());
4708 Tabular::ltType ltt;
4712 case Tabular::SET_PWIDTH: {
4713 Length const len(value);
4714 tabular.setColumnPWidth(cur, cur.idx(), len);
4716 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4717 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4721 case Tabular::SET_MPWIDTH:
4722 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4725 case Tabular::SET_SPECIAL_COLUMN:
4726 case Tabular::SET_SPECIAL_MULTI:
4727 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4730 case Tabular::APPEND_ROW:
4731 // append the row into the tabular
4732 tabular.appendRow(cur.idx());
4735 case Tabular::APPEND_COLUMN:
4736 // append the column into the tabular
4737 tabular.appendColumn(cur.idx());
4738 cur.idx() = tabular.cellIndex(row, column);
4741 case Tabular::DELETE_ROW:
4742 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4743 tabular.deleteRow(sel_row_start);
4744 if (sel_row_start >= tabular.row_info.size())
4746 cur.idx() = tabular.cellIndex(sel_row_start, column);
4749 cur.setSelection(false);
4752 case Tabular::DELETE_COLUMN:
4753 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4754 tabular.deleteColumn(sel_col_start);
4755 if (sel_col_start >= tabular.column_info.size())
4757 cur.idx() = tabular.cellIndex(row, sel_col_start);
4760 cur.setSelection(false);
4763 case Tabular::COPY_ROW:
4764 tabular.copyRow(row);
4767 case Tabular::COPY_COLUMN:
4768 tabular.copyColumn(column);
4769 cur.idx() = tabular.cellIndex(row, column);
4772 case Tabular::TOGGLE_LINE_TOP: {
4773 bool lineSet = !tabular.topLine(cur.idx());
4774 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4775 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4776 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4780 case Tabular::TOGGLE_LINE_BOTTOM: {
4781 bool lineSet = !tabular.bottomLine(cur.idx());
4782 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4783 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4784 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4788 case Tabular::TOGGLE_LINE_LEFT: {
4789 bool lineSet = !tabular.leftLine(cur.idx());
4790 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4791 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4792 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4796 case Tabular::TOGGLE_LINE_RIGHT: {
4797 bool lineSet = !tabular.rightLine(cur.idx());
4798 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4799 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4800 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4804 case Tabular::M_ALIGN_LEFT:
4805 case Tabular::M_ALIGN_RIGHT:
4806 case Tabular::M_ALIGN_CENTER:
4808 case Tabular::ALIGN_LEFT:
4809 case Tabular::ALIGN_RIGHT:
4810 case Tabular::ALIGN_CENTER:
4811 case Tabular::ALIGN_BLOCK:
4812 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4813 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4814 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4817 case Tabular::M_VALIGN_TOP:
4818 case Tabular::M_VALIGN_BOTTOM:
4819 case Tabular::M_VALIGN_MIDDLE:
4821 case Tabular::VALIGN_TOP:
4822 case Tabular::VALIGN_BOTTOM:
4823 case Tabular::VALIGN_MIDDLE:
4824 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4825 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4826 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4829 case Tabular::MULTICOLUMN: {
4830 if (!cur.selection()) {
4831 // just multicol for one single cell
4832 // check whether we are completely in a multicol
4833 if (tabular.isMultiColumn(cur.idx()))
4834 tabular.unsetMultiColumn(cur.idx());
4836 tabular.setMultiColumn(cur.idx(), 1);
4839 // we have a selection so this means we just add all this
4840 // cells to form a multicolumn cell
4841 idx_type const s_start = cur.selBegin().idx();
4842 idx_type const s_end = cur.selEnd().idx();
4843 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4844 cur.idx() = s_start;
4847 cur.setSelection(false);
4851 case Tabular::SET_ALL_LINES:
4853 case Tabular::UNSET_ALL_LINES:
4854 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4855 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4856 tabular.setAllLines(
4857 tabular.cellIndex(i,j), setLines);
4860 case Tabular::SET_BORDER_LINES:
4861 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4862 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4863 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4865 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4866 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4867 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4871 case Tabular::SET_LONGTABULAR:
4872 tabular.is_long_tabular = true;
4875 case Tabular::UNSET_LONGTABULAR:
4876 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
4877 if (tabular.ltCaption(i)) {
4878 cur.idx() = tabular.cellIndex(i, 0);
4881 tabularFeatures(cur, Tabular::TOGGLE_LTCAPTION);
4884 tabular.is_long_tabular = false;
4887 case Tabular::SET_ROTATE_TABULAR:
4888 tabular.rotate = true;
4891 case Tabular::UNSET_ROTATE_TABULAR:
4892 tabular.rotate = false;
4895 case Tabular::TOGGLE_ROTATE_TABULAR:
4896 tabular.rotate = !tabular.rotate;
4899 case Tabular::TABULAR_VALIGN_TOP:
4900 tabular.tabular_valignment = Tabular::LYX_VALIGN_TOP;
4903 case Tabular::TABULAR_VALIGN_MIDDLE:
4904 tabular.tabular_valignment = Tabular::LYX_VALIGN_MIDDLE;
4907 case Tabular::TABULAR_VALIGN_BOTTOM:
4908 tabular.tabular_valignment = Tabular::LYX_VALIGN_BOTTOM;
4911 case Tabular::LONGTABULAR_ALIGN_LEFT:
4912 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_LEFT;
4915 case Tabular::LONGTABULAR_ALIGN_CENTER:
4916 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_CENTER;
4919 case Tabular::LONGTABULAR_ALIGN_RIGHT:
4920 tabular.longtabular_alignment = Tabular::LYX_LONGTABULAR_ALIGN_RIGHT;
4925 case Tabular::SET_ROTATE_CELL:
4926 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4927 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4928 tabular.setRotateCell(
4929 tabular.cellIndex(i, j), true);
4932 case Tabular::UNSET_ROTATE_CELL:
4933 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4934 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4935 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4938 case Tabular::TOGGLE_ROTATE_CELL:
4940 bool oneNotRotated = oneCellHasRotationState(false,
4941 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4943 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4944 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4945 tabular.setRotateCell(tabular.cellIndex(i, j),
4950 case Tabular::SET_USEBOX: {
4951 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4952 if (val == tabular.getUsebox(cur.idx()))
4953 val = Tabular::BOX_NONE;
4954 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4955 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4956 tabular.setUsebox(tabular.cellIndex(i, j), val);
4960 case Tabular::UNSET_LTFIRSTHEAD:
4962 case Tabular::SET_LTFIRSTHEAD:
4963 tabular.getRowOfLTFirstHead(row, ltt);
4964 checkLongtableSpecial(ltt, value, flag);
4965 tabular.setLTHead(row, flag, ltt, true);
4968 case Tabular::UNSET_LTHEAD:
4970 case Tabular::SET_LTHEAD:
4971 tabular.getRowOfLTHead(row, ltt);
4972 checkLongtableSpecial(ltt, value, flag);
4973 tabular.setLTHead(row, flag, ltt, false);
4976 case Tabular::UNSET_LTFOOT:
4978 case Tabular::SET_LTFOOT:
4979 tabular.getRowOfLTFoot(row, ltt);
4980 checkLongtableSpecial(ltt, value, flag);
4981 tabular.setLTFoot(row, flag, ltt, false);
4984 case Tabular::UNSET_LTLASTFOOT:
4986 case Tabular::SET_LTLASTFOOT:
4987 tabular.getRowOfLTLastFoot(row, ltt);
4988 checkLongtableSpecial(ltt, value, flag);
4989 tabular.setLTFoot(row, flag, ltt, true);
4992 case Tabular::SET_LTNEWPAGE:
4993 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4996 case Tabular::TOGGLE_LTCAPTION: {
4997 bool const set = !tabular.ltCaption(row);
4998 cur.idx() = tabular.setLTCaption(row, set);
5001 cur.setSelection(false);
5004 // When a row is set as caption, then also insert
5005 // a caption. Otherwise the LaTeX output is broken.
5006 lyx::dispatch(FuncRequest(LFUN_INSET_SELECT_ALL));
5007 lyx::dispatch(FuncRequest(LFUN_CAPTION_INSERT));
5009 FuncRequest fr(LFUN_INSET_DISSOLVE, "caption");
5010 if (lyx::getStatus(fr).enabled())
5016 case Tabular::SET_BOOKTABS:
5017 tabular.use_booktabs = true;
5020 case Tabular::UNSET_BOOKTABS:
5021 tabular.use_booktabs = false;
5024 case Tabular::SET_TOP_SPACE: {
5026 if (value == "default")
5027 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5028 tabular.row_info[i].top_space_default = true;
5029 else if (isValidLength(value, &len))
5030 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5031 tabular.row_info[i].top_space_default = false;
5032 tabular.row_info[i].top_space = len;
5035 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5036 tabular.row_info[i].top_space_default = false;
5037 tabular.row_info[i].top_space = len;
5042 case Tabular::SET_BOTTOM_SPACE: {
5044 if (value == "default")
5045 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5046 tabular.row_info[i].bottom_space_default = true;
5047 else if (isValidLength(value, &len))
5048 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5049 tabular.row_info[i].bottom_space_default = false;
5050 tabular.row_info[i].bottom_space = len;
5053 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5054 tabular.row_info[i].bottom_space_default = false;
5055 tabular.row_info[i].bottom_space = len;
5060 case Tabular::SET_INTERLINE_SPACE: {
5062 if (value == "default")
5063 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
5064 tabular.row_info[i].interline_space_default = true;
5065 else if (isValidLength(value, &len))
5066 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5067 tabular.row_info[i].interline_space_default = false;
5068 tabular.row_info[i].interline_space = len;
5071 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
5072 tabular.row_info[i].interline_space_default = false;
5073 tabular.row_info[i].interline_space = len;
5078 // dummy stuff just to avoid warnings
5079 case Tabular::LAST_ACTION:
5085 bool InsetTabular::showInsetDialog(BufferView * bv) const
5087 bv->showDialog("tabular", params2string(*this),
5088 const_cast<InsetTabular *>(this));
5093 void InsetTabular::openLayoutDialog(BufferView * bv) const
5095 bv->showDialog("tabular", params2string(*this),
5096 const_cast<InsetTabular *>(this));
5100 bool InsetTabular::copySelection(Cursor & cur)
5102 if (!cur.selection())
5107 getSelection(cur, rs, re, cs, ce);
5109 paste_tabular.reset(new Tabular(tabular));
5111 for (row_type i = 0; i < rs; ++i)
5112 paste_tabular->deleteRow(0);
5114 row_type const rows = re - rs + 1;
5115 while (paste_tabular->row_info.size() > rows)
5116 paste_tabular->deleteRow(rows);
5118 for (col_type i = 0; i < cs; ++i)
5119 paste_tabular->deleteColumn(0);
5121 col_type const columns = ce - cs + 1;
5122 while (paste_tabular->column_info.size() > columns)
5123 paste_tabular->deleteColumn(columns);
5125 paste_tabular->setBuffer(tabular.buffer());
5127 odocstringstream os;
5128 OutputParams const runparams(0);
5129 paste_tabular->plaintext(os, runparams, 0, true, '\t');
5130 // Needed for the "Edit->Paste recent" menu and the system clipboard.
5131 cap::copySelection(cur, os.str());
5133 // mark tabular stack dirty
5134 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
5135 // when we (hopefully) have a one-for-all paste mechanism.
5136 // This must be called after cap::copySelection.
5137 dirtyTabularStack(true);
5143 bool InsetTabular::pasteClipboard(Cursor & cur)
5147 col_type actcol = tabular.cellColumn(cur.idx());
5148 row_type actrow = tabular.cellRow(cur.idx());
5150 if (cur.selIsMultiCell()) {
5153 getSelection(cur, actrow, re, actcol, ce);
5156 for (row_type r1 = 0, r2 = actrow;
5157 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
5159 for (col_type c1 = 0, c2 = actcol;
5160 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
5162 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
5163 tabular.isPartOfMultiColumn(r2, c2))
5165 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
5169 if (tabular.isPartOfMultiColumn(r2, c2)) {
5173 shared_ptr<InsetTableCell> inset(
5174 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
5175 tabular.setCellInset(r2, c2, inset);
5176 // FIXME: change tracking (MG)
5177 inset->setChange(Change(buffer().params().trackChanges ?
5178 Change::INSERTED : Change::UNCHANGED));
5186 void InsetTabular::cutSelection(Cursor & cur)
5188 if (!cur.selection())
5193 getSelection(cur, rs, re, cs, ce);
5194 for (row_type i = rs; i <= re; ++i) {
5195 for (col_type j = cs; j <= ce; ++j) {
5196 shared_ptr<InsetTableCell> t
5197 = cell(tabular.cellIndex(i, j));
5198 if (buffer().params().trackChanges)
5199 // FIXME: Change tracking (MG)
5200 t->setChange(Change(Change::DELETED));
5206 // cursor position might be invalid now
5207 if (cur.pit() > cur.lastpit())
5208 cur.pit() = cur.lastpit();
5209 if (cur.pos() > cur.lastpos())
5210 cur.pos() = cur.lastpos();
5211 cur.clearSelection();
5215 bool InsetTabular::isRightToLeft(Cursor & cur) const
5217 LASSERT(cur.depth() > 1, /**/);
5218 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
5219 pos_type const parentpos = cur[cur.depth() - 2].pos();
5220 return parentpar.getFontSettings(buffer().params(),
5221 parentpos).language()->rightToLeft();
5224 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
5227 LASSERT(stidx <= enidx, return docstring());
5229 col_type const col1 = tabular.cellColumn(stidx);
5230 col_type const col2 = tabular.cellColumn(enidx);
5231 row_type const row1 = tabular.cellRow(stidx);
5232 row_type const row2 = tabular.cellRow(enidx);
5234 for (col_type col = col1; col <= col2; col++)
5235 for (row_type row = row1; row <= row2; row++) {
5240 retval += tabular.cellInset(row, col)->asString(intoInsets);
5246 void InsetTabular::getSelection(Cursor & cur,
5247 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
5249 CursorSlice const & beg = cur.selBegin();
5250 CursorSlice const & end = cur.selEnd();
5251 cs = tabular.cellColumn(beg.idx());
5252 ce = tabular.cellColumn(end.idx());
5255 cs = tabular.cellColumn(end.idx());
5257 ce = tabular.cellRightColumn(end.idx());
5260 rs = tabular.cellRow(beg.idx());
5261 re = tabular.cellRow(end.idx());
5267 Text * InsetTabular::getText(int idx) const
5269 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
5273 void InsetTabular::setChange(Change const & change)
5275 for (idx_type idx = 0; idx < nargs(); ++idx)
5276 cell(idx)->setChange(change);
5280 void InsetTabular::acceptChanges()
5282 for (idx_type idx = 0; idx < nargs(); ++idx)
5283 cell(idx)->acceptChanges();
5287 void InsetTabular::rejectChanges()
5289 for (idx_type idx = 0; idx < nargs(); ++idx)
5290 cell(idx)->rejectChanges();
5294 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
5296 return tabular.getPWidth(cell).zero();
5300 bool InsetTabular::forcePlainLayout(idx_type cell) const
5302 return !tabular.getPWidth(cell).zero();
5306 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
5309 if (buf.length() <= 0)
5314 col_type maxCols = 1;
5315 size_t const len = buf.length();
5319 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
5327 maxCols = max(cols, maxCols);
5333 maxCols = max(cols, maxCols);
5339 paste_tabular.reset(new Tabular(buffer_, rows, maxCols));
5340 loctab = paste_tabular.get();
5342 dirtyTabularStack(true);
5345 cell = bv.cursor().idx();
5346 ocol = tabular.cellColumn(cell);
5347 row = tabular.cellRow(cell);
5351 idx_type const cells = loctab->numberofcells;
5354 rows = loctab->row_info.size();
5355 col_type const columns = loctab->column_info.size();
5357 while (cell < cells && p < len && row < rows &&
5358 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
5364 // we can only set this if we are not too far right
5365 if (cols < columns) {
5366 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5367 Font const font = bv.textMetrics(&inset->text()).
5369 inset->setText(buf.substr(op, p - op), font,
5370 buffer().params().trackChanges);
5376 // we can only set this if we are not too far right
5377 if (cols < columns) {
5378 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
5379 Font const font = bv.textMetrics(&inset->text()).
5381 inset->setText(buf.substr(op, p - op), font,
5382 buffer().params().trackChanges);
5387 cell = loctab->cellIndex(row, cols);
5393 // check for the last cell if there is no trailing '\n'
5394 if (cell < cells && op < len) {
5395 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
5396 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
5397 inset->setText(buf.substr(op, len - op), font,
5398 buffer().params().trackChanges);
5404 void InsetTabular::addPreview(DocIterator const & inset_pos,
5405 PreviewLoader & loader) const
5407 row_type const rows = tabular.row_info.size();
5408 col_type const columns = tabular.column_info.size();
5409 DocIterator cell_pos = inset_pos;
5411 cell_pos.push_back(CursorSlice(*const_cast<InsetTabular *>(this)));
5412 for (row_type i = 0; i < rows; ++i) {
5413 for (col_type j = 0; j < columns; ++j) {
5414 cell_pos.top().idx() = tabular.cellIndex(i, j);
5415 tabular.cellInset(i, j)->addPreview(cell_pos, loader);
5421 bool InsetTabular::completionSupported(Cursor const & cur) const
5423 Cursor const & bvCur = cur.bv().cursor();
5424 if (&bvCur.inset() != this)
5426 return cur.text()->completionSupported(cur);
5430 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
5432 return completionSupported(cur);
5436 bool InsetTabular::automaticInlineCompletion() const
5438 return lyxrc.completion_inline_text;
5442 bool InsetTabular::automaticPopupCompletion() const
5444 return lyxrc.completion_popup_text;
5448 bool InsetTabular::showCompletionCursor() const
5450 return lyxrc.completion_cursor_text;
5454 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
5456 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
5460 docstring InsetTabular::completionPrefix(Cursor const & cur) const
5462 if (!completionSupported(cur))
5464 return cur.text()->completionPrefix(cur);
5468 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
5470 if (!completionSupported(cur))
5473 return cur.text()->insertCompletion(cur, s, finished);
5477 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
5478 Dimension & dim) const
5480 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
5481 tm.completionPosAndDim(cur, x, y, dim);
5485 void InsetTabular::string2params(string const & in, InsetTabular & inset)
5487 istringstream data(in);
5489 lex.setStream(data);
5496 if (!lex || token != "tabular") {
5497 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
5501 // This is part of the inset proper that is usually swallowed
5502 // by Buffer::readInset
5504 if (!lex || token != "Tabular") {
5505 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5513 string InsetTabular::params2string(InsetTabular const & inset)
5516 data << "tabular" << ' ';
5518 data << "\\end_inset\n";