2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
53 #include "support/lassert.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/lstrings.h"
61 #include <boost/scoped_ptr.hpp>
69 using namespace lyx::support;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
106 TabularFeature tabularFeature[] =
108 { Tabular::APPEND_ROW, "append-row" },
109 { Tabular::APPEND_COLUMN, "append-column" },
110 { Tabular::DELETE_ROW, "delete-row" },
111 { Tabular::DELETE_COLUMN, "delete-column" },
112 { Tabular::COPY_ROW, "copy-row" },
113 { Tabular::COPY_COLUMN, "copy-column" },
114 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
115 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
116 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
117 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
118 { Tabular::ALIGN_LEFT, "align-left" },
119 { Tabular::ALIGN_RIGHT, "align-right" },
120 { Tabular::ALIGN_CENTER, "align-center" },
121 { Tabular::ALIGN_BLOCK, "align-block" },
122 { Tabular::VALIGN_TOP, "valign-top" },
123 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
124 { Tabular::VALIGN_MIDDLE, "valign-middle" },
125 { Tabular::M_ALIGN_LEFT, "m-align-left" },
126 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
127 { Tabular::M_ALIGN_CENTER, "m-align-center" },
128 { Tabular::M_VALIGN_TOP, "m-valign-top" },
129 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
130 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
131 { Tabular::MULTICOLUMN, "multicolumn" },
132 { Tabular::SET_ALL_LINES, "set-all-lines" },
133 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
134 { Tabular::SET_LONGTABULAR, "set-longtabular" },
135 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
136 { Tabular::SET_PWIDTH, "set-pwidth" },
137 { Tabular::SET_MPWIDTH, "set-mpwidth" },
138 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
139 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
140 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
141 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
142 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
143 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
144 { Tabular::SET_USEBOX, "set-usebox" },
145 { Tabular::SET_LTHEAD, "set-lthead" },
146 { Tabular::UNSET_LTHEAD, "unset-lthead" },
147 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
148 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
149 { Tabular::SET_LTFOOT, "set-ltfoot" },
150 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
151 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
152 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
153 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
154 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::SET_BORDER_LINES, "set-border-lines" },
163 { Tabular::LAST_ACTION, "" }
167 class FeatureEqual : public unary_function<TabularFeature, bool> {
169 FeatureEqual(Tabular::Feature feature)
170 : feature_(feature) {}
171 bool operator()(TabularFeature const & tf) const {
172 return tf.action == feature_;
175 Tabular::Feature feature_;
180 string const write_attribute(string const & name, T const & t)
182 string const s = tostr(t);
183 return s.empty() ? s : " " + name + "=\"" + s + "\"";
187 string const write_attribute(string const & name, string const & t)
189 return t.empty() ? t : " " + name + "=\"" + t + "\"";
194 string const write_attribute(string const & name, docstring const & t)
196 return t.empty() ? string() : " " + name + "=\"" + to_utf8(t) + "\"";
201 string const write_attribute(string const & name, bool const & b)
203 // we write only true attribute values so we remove a bit of the
204 // file format bloat for tabulars.
205 return b ? write_attribute(name, convert<string>(b)) : string();
210 string const write_attribute(string const & name, int const & i)
212 // we write only true attribute values so we remove a bit of the
213 // file format bloat for tabulars.
214 return i ? write_attribute(name, convert<string>(i)) : string();
219 string const write_attribute(string const & name, Tabular::idx_type const & i)
221 // we write only true attribute values so we remove a bit of the
222 // file format bloat for tabulars.
223 return i ? write_attribute(name, convert<string>(i)) : string();
228 string const write_attribute(string const & name, Length const & value)
230 // we write only the value if we really have one same reson as above.
231 return value.zero() ? string() : write_attribute(name, value.asString());
235 string const tostr(LyXAlignment const & num)
240 case LYX_ALIGN_BLOCK:
244 case LYX_ALIGN_CENTER:
246 case LYX_ALIGN_RIGHT:
248 case LYX_ALIGN_LAYOUT:
250 case LYX_ALIGN_SPECIAL:
257 string const tostr(Tabular::VAlignment const & num)
260 case Tabular::LYX_VALIGN_TOP:
262 case Tabular::LYX_VALIGN_MIDDLE:
264 case Tabular::LYX_VALIGN_BOTTOM:
271 string const tostr(Tabular::BoxType const & num)
274 case Tabular::BOX_NONE:
276 case Tabular::BOX_PARBOX:
278 case Tabular::BOX_MINIPAGE:
285 // I would have liked a fromstr template a lot better. (Lgb)
286 bool string2type(string const str, LyXAlignment & num)
289 num = LYX_ALIGN_NONE;
290 else if (str == "block")
291 num = LYX_ALIGN_BLOCK;
292 else if (str == "left")
293 num = LYX_ALIGN_LEFT;
294 else if (str == "center")
295 num = LYX_ALIGN_CENTER;
296 else if (str == "right")
297 num = LYX_ALIGN_RIGHT;
304 bool string2type(string const str, Tabular::VAlignment & num)
307 num = Tabular::LYX_VALIGN_TOP;
308 else if (str == "middle" )
309 num = Tabular::LYX_VALIGN_MIDDLE;
310 else if (str == "bottom")
311 num = Tabular::LYX_VALIGN_BOTTOM;
318 bool string2type(string const str, Tabular::BoxType & num)
321 num = Tabular::BOX_NONE;
322 else if (str == "parbox")
323 num = Tabular::BOX_PARBOX;
324 else if (str == "minipage")
325 num = Tabular::BOX_MINIPAGE;
332 bool string2type(string const str, bool & num)
336 else if (str == "false")
344 bool getTokenValue(string const & str, char const * token, string & ret)
347 size_t token_length = strlen(token);
348 size_t pos = str.find(token);
350 if (pos == string::npos || pos + token_length + 1 >= str.length()
351 || str[pos + token_length] != '=')
353 pos += token_length + 1;
355 if (ch != '"' && ch != '\'') { // only read till next space
359 while (pos < str.length() - 1 && str[++pos] != ch)
366 bool getTokenValue(string const & str, char const * token, docstring & ret)
369 bool const success = getTokenValue(str, token, tmp);
370 ret = from_utf8(tmp);
375 bool getTokenValue(string const & str, char const * token, int & num)
379 if (!getTokenValue(str, token, tmp))
381 num = convert<int>(tmp);
386 bool getTokenValue(string const & str, char const * token, LyXAlignment & num)
389 return getTokenValue(str, token, tmp) && string2type(tmp, num);
393 bool getTokenValue(string const & str, char const * token,
394 Tabular::VAlignment & num)
397 return getTokenValue(str, token, tmp) && string2type(tmp, num);
401 bool getTokenValue(string const & str, char const * token,
402 Tabular::BoxType & num)
405 return getTokenValue(str, token, tmp) && string2type(tmp, num);
409 bool getTokenValue(string const & str, char const * token, bool & flag)
411 // set the flag always to false as this should be the default for bools
412 // not in the file-format.
415 return getTokenValue(str, token, tmp) && string2type(tmp, flag);
419 bool getTokenValue(string const & str, char const * token, Length & len)
421 // set the length to be zero() as default as this it should be if not
422 // in the file format.
425 return getTokenValue(str, token, tmp) && isValidLength(tmp, &len);
429 bool getTokenValue(string const & str, char const * token, Length & len, bool & flag)
434 if (!getTokenValue(str, token, tmp))
436 if (tmp == "default") {
440 return isValidLength(tmp, &len);
444 void l_getline(istream & is, string & str)
447 while (str.empty()) {
449 if (!str.empty() && str[str.length() - 1] == '\r')
450 str.erase(str.length() - 1);
457 string const featureAsString(Tabular::Feature feature)
459 TabularFeature * end = tabularFeature +
460 sizeof(tabularFeature) / sizeof(TabularFeature);
461 TabularFeature * it = find_if(tabularFeature, end,
462 FeatureEqual(feature));
463 return (it == end) ? string() : it->feature;
468 /////////////////////////////////////////////////////////////////////
472 /////////////////////////////////////////////////////////////////////
475 Tabular::CellData::CellData(Buffer const & buf, Tabular const & table)
478 multicolumn(Tabular::CELL_NORMAL),
479 alignment(LYX_ALIGN_CENTER),
480 valignment(LYX_VALIGN_TOP),
487 inset(new InsetTableCell(buf, this, &table))
489 inset->setBuffer(const_cast<Buffer &>(buf));
490 inset->paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
494 Tabular::CellData::CellData(CellData const & cs)
497 multicolumn(cs.multicolumn),
498 alignment(cs.alignment),
499 valignment(cs.valignment),
500 top_line(cs.top_line),
501 bottom_line(cs.bottom_line),
502 left_line(cs.left_line),
503 right_line(cs.right_line),
506 align_special(cs.align_special),
508 inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
510 inset->setCellData(this);
514 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
521 void Tabular::CellData::swap(CellData & rhs)
523 std::swap(cellno, rhs.cellno);
524 std::swap(width, rhs.width);
525 std::swap(multicolumn, rhs.multicolumn);
526 std::swap(alignment, rhs.alignment);
527 std::swap(valignment, rhs.valignment);
528 std::swap(top_line, rhs.top_line);
529 std::swap(bottom_line, rhs.bottom_line);
530 std::swap(left_line, rhs.left_line);
531 std::swap(right_line, rhs.right_line);
532 std::swap(usebox, rhs.usebox);
533 std::swap(rotate, rhs.rotate);
534 std::swap(align_special, rhs.align_special);
535 p_width.swap(rhs.p_width);
536 inset.swap(rhs.inset);
540 Tabular::RowData::RowData()
543 top_space_default(false),
544 bottom_space_default(false),
545 interline_space_default(false),
555 Tabular::ColumnData::ColumnData()
556 : alignment(LYX_ALIGN_CENTER),
557 valignment(LYX_VALIGN_TOP),
563 Tabular::ltType::ltType()
570 Tabular::Tabular(Buffer const & buffer, row_type rows_arg, col_type columns_arg)
572 init(buffer, rows_arg, columns_arg);
576 // activates all lines and sets all widths to 0
577 void Tabular::init(Buffer const & buf, row_type rows_arg,
578 col_type columns_arg)
581 row_info = row_vector(rows_arg);
582 column_info = column_vector(columns_arg);
583 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf, *this)));
584 row_info.reserve(10);
585 column_info.reserve(10);
586 cell_info.reserve(100);
588 is_long_tabular = false;
590 use_booktabs = false;
591 size_t row_count = row_info.size();
592 size_t column_count = column_info.size();
593 // set silly default lines
594 for (row_type i = 0; i < row_count; ++i)
595 for (col_type j = 0; j < column_count; ++j) {
596 cell_info[i][j].top_line = true;
597 cell_info[i][j].left_line = true;
598 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
599 cell_info[i][j].right_line = j == column_count - 1;
604 void Tabular::appendRow(idx_type const cell)
606 BufferParams const & bp = buffer().params();
607 row_type const row = cellRow(cell);
609 row_vector::iterator rit = row_info.begin() + row;
610 row_info.insert(rit, RowData());
611 // now set the values of the row before
612 row_info[row] = row_info[row + 1];
614 row_type const nrows = row_info.size();
615 col_type const ncols = column_info.size();
617 cell_vvector old(nrows - 1);
618 for (row_type i = 0; i < nrows - 1; ++i)
619 swap(cell_info[i], old[i]);
621 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer(), *this)));
623 for (row_type i = 0; i <= row; ++i)
624 swap(cell_info[i], old[i]);
625 for (row_type i = row + 2; i < nrows; ++i)
626 swap(cell_info[i], old[i - 1]);
629 for (col_type c = 0; c < ncols; ++c) {
630 // inherit line settings
631 idx_type const i = cellIndex(row + 1, c);
632 idx_type const j = cellIndex(row, c);
633 setLeftLine(i, leftLine(j));
634 setRightLine(i, rightLine(j));
635 setTopLine(i, topLine(j));
636 if (topLine(j) && bottomLine(j)) {
637 setBottomLine(i, true);
638 setBottomLine(j, false);
640 // mark track changes
642 cellInfo(i).inset->setChange(Change(Change::INSERTED));
647 void Tabular::deleteRow(row_type const row)
649 // Not allowed to delete last row
650 if (row_info.size() == 1)
653 row_info.erase(row_info.begin() + row);
654 cell_info.erase(cell_info.begin() + row);
659 void Tabular::copyRow(row_type const row)
661 row_info.insert(row_info.begin() + row, row_info[row]);
662 cell_info.insert(cell_info.begin() + row, cell_info[row]);
664 if (buffer().params().trackChanges)
665 for (col_type j = 0; j < column_info.size(); ++j)
666 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
672 void Tabular::appendColumn(idx_type const cell)
674 col_type const c = cellColumn(cell);
675 column_vector::iterator cit = column_info.begin() + c + 1;
676 column_info.insert(cit, ColumnData());
677 row_type const nrows = row_info.size();
678 // set the column values of the column before
679 column_info[c + 1] = column_info[c];
681 for (row_type r = 0; r < nrows; ++r) {
682 cell_info[r].insert(cell_info[r].begin() + c + 1,
683 CellData(buffer(), *this));
684 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
685 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
687 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
690 for (row_type r = 0; r < nrows; ++r) {
691 // inherit line settings
692 idx_type const i = cellIndex(r, c + 1);
693 idx_type const j = cellIndex(r, c);
694 setBottomLine(i, bottomLine(j));
695 setTopLine(i, topLine(j));
696 setLeftLine(i, leftLine(j));
697 if (rightLine(j) && rightLine(j)) {
698 setRightLine(i, true);
699 setRightLine(j, false);
702 cellInfo(i).inset->clear();
703 if (buffer().params().trackChanges)
704 cellInfo(i).inset->setChange(Change(Change::INSERTED));
709 void Tabular::deleteColumn(col_type const column)
711 // Not allowed to delete last column
712 if (column_info.size() == 1)
715 column_info.erase(column_info.begin() + column);
716 size_t row_count = row_info.size();
717 for (row_type i = 0; i < row_count; ++i) {
718 // Care about multicolumn cells
719 if (column + 1 < column_info.size() &&
720 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
721 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
722 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
724 cell_info[i].erase(cell_info[i].begin() + column);
730 void Tabular::copyColumn(col_type const column)
732 BufferParams const & bp = buffer().params();
733 column_info.insert(column_info.begin() + column, column_info[column]);
735 size_t row_count = row_info.size();
736 for (row_type i = 0; i < row_count; ++i)
737 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
740 for (row_type i = 0; i < row_count; ++i)
741 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
746 void Tabular::updateIndexes()
748 col_type ncols = column_info.size();
749 row_type nrows = row_info.size();
751 for (row_type row = 0; row < nrows; ++row)
752 for (col_type column = 0; column < ncols; ++column) {
753 if (!isPartOfMultiColumn(row, column))
755 cell_info[row][column].cellno = numberofcells - 1;
758 rowofcell.resize(numberofcells);
759 columnofcell.resize(numberofcells);
761 for (row_type row = 0; row < nrows; ++row)
762 for (col_type column = 0; column < ncols; ++column) {
763 if (isPartOfMultiColumn(row, column))
766 columnofcell[i] = column;
772 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
774 row_type const row = cellRow(cell);
776 for (col_type i = 0; i < column_info.size(); ++i)
777 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
783 bool Tabular::topLine(idx_type const cell) const
785 return cellInfo(cell).top_line;
789 bool Tabular::bottomLine(idx_type const cell) const
791 return cellInfo(cell).bottom_line;
795 bool Tabular::leftLine(idx_type cell) const
799 return cellInfo(cell).left_line;
803 bool Tabular::rightLine(idx_type cell) const
807 return cellInfo(cell).right_line;
811 bool Tabular::topAlreadyDrawn(idx_type cell) const
813 row_type row = cellRow(cell);
816 idx_type i = cellIndex(row - 1, cellColumn(cell));
817 return !rowTopLine(row) && bottomLine(i);
821 bool Tabular::leftAlreadyDrawn(idx_type cell) const
823 col_type col = cellColumn(cell);
826 idx_type i = cellIndex(cellRow(cell), col - 1);
827 return rightLine(i) && !leftLine(cell);
831 bool Tabular::isLastRow(idx_type cell) const
833 return cellRow(cell) == row_info.size() - 1;
837 int Tabular::getAdditionalHeight(row_type row) const
839 if (!row || row >= row_info.size())
842 int const interline_space = row_info[row - 1].interline_space_default ?
844 row_info[row - 1].interline_space.inPixels(width());
845 if (rowTopLine(row) && rowBottomLine(row - 1))
846 return interline_space + WIDTH_OF_LINE;
847 return interline_space;
851 int Tabular::getAdditionalWidth(idx_type cell) const
853 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
855 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
856 return WIDTH_OF_LINE;
861 int Tabular::columnWidth(idx_type cell) const
864 col_type const span = columnSpan(cell);
865 col_type const col = cellColumn(cell);
866 for(col_type c = col; c < col + span ; ++c)
867 w += column_info[c].width;
872 bool Tabular::updateColumnWidths()
874 col_type const ncols = column_info.size();
875 row_type const nrows = row_info.size();
877 // for each col get max of single col cells
878 for(col_type c = 0; c < ncols; ++c) {
880 for(row_type r = 0; r < nrows; ++r) {
881 idx_type const i = cellIndex(r, c);
882 if (columnSpan(i) == 1)
883 new_width = max(new_width, cellInfo(i).width);
886 if (column_info[c].width != new_width) {
887 column_info[c].width = new_width;
891 // update col widths to fit merged cells
892 for(col_type c = 0; c < ncols; ++c)
893 for(row_type r = 0; r < nrows; ++r) {
894 idx_type const i = cellIndex(r, c);
895 int const span = columnSpan(i);
896 if (span == 1 || c > cellColumn(i))
900 for(col_type j = c; j < c + span ; ++j)
901 old_width += column_info[j].width;
903 if (cellInfo(i).width > old_width) {
904 column_info[c + span - 1].width += cellInfo(i).width - old_width;
913 int Tabular::width() const
915 col_type const ncols = column_info.size();
917 for (col_type i = 0; i < ncols; ++i)
918 width += column_info[i].width;
923 void Tabular::setCellWidth(idx_type cell, int new_width)
925 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
926 + getAdditionalWidth(cell);
930 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
933 if (!isMultiColumn(cell) || onlycolumn)
934 column_info[cellColumn(cell)].alignment = align;
936 cellInfo(cell).alignment = align;
940 void Tabular::setVAlignment(idx_type cell, VAlignment align,
943 if (!isMultiColumn(cell) || onlycolumn)
944 column_info[cellColumn(cell)].valignment = align;
946 cellInfo(cell).valignment = align;
953 * Allow line and paragraph breaks for fixed width cells or disallow them,
954 * merge cell paragraphs and reset layout to standard for variable width
957 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
959 inset->setAutoBreakRows(fixedWidth);
963 // merge all paragraphs to one
964 BufferParams const & bp = cur.bv().buffer().params();
965 while (inset->paragraphs().size() > 1)
966 mergeParagraph(bp, inset->paragraphs(), 0);
970 // undo information has already been recorded
971 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
972 bp.documentClass().emptyLayoutName());
979 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
980 Length const & width)
982 col_type const j = cellColumn(cell);
984 column_info[j].p_width = width;
985 for (row_type i = 0; i < row_info.size(); ++i) {
986 idx_type const cell = cellIndex(i, j);
987 // because of multicolumns
988 toggleFixedWidth(cur, cellInset(cell).get(),
989 !getPWidth(cell).zero());
991 // cur paragraph can become invalid after paragraphs were merged
992 if (cur.pit() > cur.lastpit())
993 cur.pit() = cur.lastpit();
994 // cur position can become invalid after newlines were removed
995 if (cur.pos() > cur.lastpos())
996 cur.pos() = cur.lastpos();
1000 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1001 Length const & width)
1003 if (!isMultiColumn(cell))
1006 cellInfo(cell).p_width = width;
1007 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1008 // cur paragraph can become invalid after paragraphs were merged
1009 if (cur.pit() > cur.lastpit())
1010 cur.pit() = cur.lastpit();
1011 // cur position can become invalid after newlines were removed
1012 if (cur.pos() > cur.lastpos())
1013 cur.pos() = cur.lastpos();
1018 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1019 Tabular::Feature what)
1021 if (what == SET_SPECIAL_MULTI)
1022 cellInfo(cell).align_special = special;
1024 column_info[cellColumn(cell)].align_special = special;
1028 void Tabular::setAllLines(idx_type cell, bool line)
1030 setTopLine(cell, line);
1031 setBottomLine(cell, line);
1032 setRightLine(cell, line);
1033 setLeftLine(cell, line);
1037 void Tabular::setTopLine(idx_type i, bool line)
1039 cellInfo(i).top_line = line;
1043 void Tabular::setBottomLine(idx_type i, bool line)
1045 cellInfo(i).bottom_line = line;
1049 void Tabular::setLeftLine(idx_type cell, bool line)
1051 cellInfo(cell).left_line = line;
1055 void Tabular::setRightLine(idx_type cell, bool line)
1057 cellInfo(cell).right_line = line;
1060 bool Tabular::rowTopLine(row_type r) const
1062 idx_type i0 = getFirstCellInRow(r);
1063 idx_type i1 = getLastCellInRow(r);
1064 bool all_rows_set = true;
1065 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1066 all_rows_set = cellInfo(j).top_line;
1067 return all_rows_set;
1071 bool Tabular::rowBottomLine(row_type r) const
1073 idx_type i0 = getFirstCellInRow(r);
1074 idx_type i1 = getLastCellInRow(r);
1075 bool all_rows_set = true;
1076 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1077 all_rows_set = cellInfo(j).bottom_line;
1078 return all_rows_set;
1082 bool Tabular::columnLeftLine(col_type c) const
1089 row_type const nrows = row_info.size();
1090 for (row_type r = 0; r < nrows; ++r) {
1091 idx_type i = cellIndex(r, c);
1092 if (c == cellColumn(i)) {
1094 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1095 if (cellInfo(i).left_line || right)
1099 return nrows_left >= total / 2;
1103 bool Tabular::columnRightLine(col_type c) const
1108 int nrows_right = 0;
1110 row_type const nrows = row_info.size();
1111 for (row_type r = 0; r < nrows; ++r) {
1112 idx_type i = cellIndex(r, c);
1113 if (c == cellColumn(i) + columnSpan(i) - 1) {
1115 bool left = c + 1 < column_info.size()
1116 && cellInfo(cellIndex(r, c + 1)).left_line
1117 || c + 1 == column_info.size();
1118 if (cellInfo(i).right_line && left)
1122 return nrows_right >= total / 2;
1126 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1128 if (!onlycolumn && isMultiColumn(cell))
1129 return cellInfo(cell).alignment;
1130 return column_info[cellColumn(cell)].alignment;
1135 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1137 if (!onlycolumn && isMultiColumn(cell))
1138 return cellInfo(cell).valignment;
1139 return column_info[cellColumn(cell)].valignment;
1143 Length const Tabular::getPWidth(idx_type cell) const
1145 if (isMultiColumn(cell))
1146 return cellInfo(cell).p_width;
1147 return column_info[cellColumn(cell)].p_width;
1151 int Tabular::cellWidth(idx_type cell) const
1153 return cellInfo(cell).width;
1157 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1161 switch (getAlignment(cell)) {
1162 case LYX_ALIGN_CENTER:
1163 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1165 case LYX_ALIGN_RIGHT:
1166 x += columnWidth(cell) - cellWidth(cell);
1167 // + getAdditionalWidth(cell);
1170 // LYX_ALIGN_LEFT: nothing :-)
1174 // the LaTeX Way :-(
1180 bool Tabular::isFirstCellInRow(idx_type cell) const
1182 return cellColumn(cell) == 0;
1186 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1188 if (row > row_info.size() - 1)
1189 row = row_info.size() - 1;
1190 return cell_info[row][0].cellno;
1194 bool Tabular::isLastCellInRow(idx_type cell) const
1196 return cellRightColumn(cell) == column_info.size() - 1;
1200 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1202 if (row > row_info.size() - 1)
1203 row = row_info.size() - 1;
1204 return cell_info[row][column_info.size() - 1].cellno;
1208 Tabular::row_type Tabular::cellRow(idx_type cell) const
1210 if (cell >= numberofcells)
1211 return row_info.size() - 1;
1214 return rowofcell[cell];
1218 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1220 if (cell >= numberofcells)
1221 return column_info.size() - 1;
1224 return columnofcell[cell];
1228 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1230 row_type const row = cellRow(cell);
1231 col_type column = cellColumn(cell);
1232 while (column < column_info.size() - 1 &&
1233 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1239 void Tabular::write(ostream & os) const
1243 << write_attribute("version", 3)
1244 << write_attribute("rows", row_info.size())
1245 << write_attribute("columns", column_info.size())
1247 // global longtable options
1249 << write_attribute("rotate", rotate)
1250 << write_attribute("booktabs", use_booktabs)
1251 << write_attribute("islongtable", is_long_tabular)
1252 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1253 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1254 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1255 << write_attribute("headTopDL", endhead.topDL)
1256 << write_attribute("headBottomDL", endhead.bottomDL)
1257 << write_attribute("footTopDL", endfoot.topDL)
1258 << write_attribute("footBottomDL", endfoot.bottomDL)
1259 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1260 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1261 << write_attribute("lastFootEmpty", endlastfoot.empty)
1263 for (col_type j = 0; j < column_info.size(); ++j) {
1265 << write_attribute("alignment", column_info[j].alignment)
1266 << write_attribute("valignment", column_info[j].valignment)
1267 << write_attribute("width", column_info[j].p_width.asString())
1268 << write_attribute("special", column_info[j].align_special)
1271 for (row_type i = 0; i < row_info.size(); ++i) {
1272 static const string def("default");
1274 if (row_info[i].top_space_default)
1275 os << write_attribute("topspace", def);
1277 os << write_attribute("topspace", row_info[i].top_space);
1278 if (row_info[i].bottom_space_default)
1279 os << write_attribute("bottomspace", def);
1281 os << write_attribute("bottomspace", row_info[i].bottom_space);
1282 if (row_info[i].interline_space_default)
1283 os << write_attribute("interlinespace", def);
1285 os << write_attribute("interlinespace", row_info[i].interline_space);
1286 os << write_attribute("endhead", row_info[i].endhead)
1287 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1288 << write_attribute("endfoot", row_info[i].endfoot)
1289 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1290 << write_attribute("newpage", row_info[i].newpage)
1291 << write_attribute("caption", row_info[i].caption)
1293 for (col_type j = 0; j < column_info.size(); ++j) {
1295 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1296 << write_attribute("alignment", cell_info[i][j].alignment)
1297 << write_attribute("valignment", cell_info[i][j].valignment)
1298 << write_attribute("topline", cell_info[i][j].top_line)
1299 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1300 << write_attribute("leftline", cell_info[i][j].left_line)
1301 << write_attribute("rightline", cell_info[i][j].right_line)
1302 << write_attribute("rotate", cell_info[i][j].rotate)
1303 << write_attribute("usebox", cell_info[i][j].usebox)
1304 << write_attribute("width", cell_info[i][j].p_width)
1305 << write_attribute("special", cell_info[i][j].align_special)
1307 os << "\\begin_inset ";
1308 cell_info[i][j].inset->write(os);
1309 os << "\n\\end_inset\n"
1314 os << "</lyxtabular>\n";
1318 void Tabular::read(Lexer & lex)
1321 istream & is = lex.getStream();
1323 l_getline(is, line);
1324 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1325 LASSERT(false, /**/);
1330 if (!getTokenValue(line, "version", version))
1332 LASSERT(version >= 2, /**/);
1335 if (!getTokenValue(line, "rows", rows_arg))
1338 if (!getTokenValue(line, "columns", columns_arg))
1340 init(buffer(), rows_arg, columns_arg);
1341 l_getline(is, line);
1342 if (!prefixIs(line, "<features")) {
1343 lyxerr << "Wrong tabular format (expected <features ...> got"
1344 << line << ')' << endl;
1347 getTokenValue(line, "rotate", rotate);
1348 getTokenValue(line, "booktabs", use_booktabs);
1349 getTokenValue(line, "islongtable", is_long_tabular);
1350 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1351 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1352 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1353 getTokenValue(line, "headTopDL", endhead.topDL);
1354 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1355 getTokenValue(line, "footTopDL", endfoot.topDL);
1356 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1357 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1358 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1359 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1361 for (col_type j = 0; j < column_info.size(); ++j) {
1363 if (!prefixIs(line,"<column")) {
1364 lyxerr << "Wrong tabular format (expected <column ...> got"
1365 << line << ')' << endl;
1368 getTokenValue(line, "alignment", column_info[j].alignment);
1369 getTokenValue(line, "valignment", column_info[j].valignment);
1370 getTokenValue(line, "width", column_info[j].p_width);
1371 getTokenValue(line, "special", column_info[j].align_special);
1374 for (row_type i = 0; i < row_info.size(); ++i) {
1375 l_getline(is, line);
1376 if (!prefixIs(line, "<row")) {
1377 lyxerr << "Wrong tabular format (expected <row ...> got"
1378 << line << ')' << endl;
1381 getTokenValue(line, "topspace", row_info[i].top_space,
1382 row_info[i].top_space_default);
1383 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1384 row_info[i].bottom_space_default);
1385 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1386 row_info[i].interline_space_default);
1387 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1388 getTokenValue(line, "endhead", row_info[i].endhead);
1389 getTokenValue(line, "endfoot", row_info[i].endfoot);
1390 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1391 getTokenValue(line, "newpage", row_info[i].newpage);
1392 getTokenValue(line, "caption", row_info[i].caption);
1393 for (col_type j = 0; j < column_info.size(); ++j) {
1394 l_getline(is, line);
1395 if (!prefixIs(line, "<cell")) {
1396 lyxerr << "Wrong tabular format (expected <cell ...> got"
1397 << line << ')' << endl;
1400 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1401 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1402 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1403 getTokenValue(line, "topline", cell_info[i][j].top_line);
1404 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1405 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1406 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1407 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1408 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1409 getTokenValue(line, "width", cell_info[i][j].p_width);
1410 getTokenValue(line, "special", cell_info[i][j].align_special);
1411 l_getline(is, line);
1412 if (prefixIs(line, "\\begin_inset")) {
1413 cell_info[i][j].inset->read(lex);
1414 l_getline(is, line);
1416 if (!prefixIs(line, "</cell>")) {
1417 lyxerr << "Wrong tabular format (expected </cell> got"
1418 << line << ')' << endl;
1422 l_getline(is, line);
1423 if (!prefixIs(line, "</row>")) {
1424 lyxerr << "Wrong tabular format (expected </row> got"
1425 << line << ')' << endl;
1429 while (!prefixIs(line, "</lyxtabular>")) {
1430 l_getline(is, line);
1436 bool Tabular::isMultiColumn(idx_type cell) const
1438 return cellInfo(cell).multicolumn != CELL_NORMAL;
1442 bool Tabular::isMultiColumnReal(idx_type cell) const
1444 return cellColumn(cell) != cellRightColumn(cell) &&
1445 cellInfo(cell).multicolumn != CELL_NORMAL;
1449 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1451 return cell_info[cellRow(cell)][cellColumn(cell)];
1455 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1457 CellData & cs = cellInfo(cell);
1458 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1459 cs.alignment = column_info[cellColumn(cell)].alignment;
1460 setRightLine(cell, rightLine(cell + number - 1));
1461 for (idx_type i = 1; i < number; ++i) {
1462 CellData & cs1 = cellInfo(cell + i);
1463 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1464 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1471 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1473 row_type const row = cellRow(cell);
1474 col_type const ncols = column_info.size();
1475 idx_type result = 1;
1476 col_type column = cellColumn(cell) + 1;
1477 while (column < ncols && isPartOfMultiColumn(row, column)) {
1485 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1487 row_type const row = cellRow(cell);
1488 col_type column = cellColumn(cell);
1490 idx_type result = 0;
1492 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1493 cell_info[row][column].multicolumn = CELL_NORMAL;
1495 while (column < column_info.size() &&
1496 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1498 cell_info[row][column].multicolumn = CELL_NORMAL;
1508 void Tabular::setRotateCell(idx_type cell, bool flag)
1510 cellInfo(cell).rotate = flag;
1514 bool Tabular::getRotateCell(idx_type cell) const
1516 return cellInfo(cell).rotate;
1520 bool Tabular::needRotating() const
1524 for (row_type i = 0; i < row_info.size(); ++i)
1525 for (col_type j = 0; j < column_info.size(); ++j)
1526 if (cell_info[i][j].rotate)
1532 bool Tabular::isLastCell(idx_type cell) const
1534 if (cell + 1 < numberofcells)
1540 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1542 if (cellRow(cell) > 0)
1543 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1548 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1550 if (cellRow(cell) + 1 < row_info.size())
1551 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1556 Tabular::idx_type Tabular::cellIndex(row_type row,
1557 col_type column) const
1559 BOOST_ASSERT(column != npos && column < column_info.size()
1560 && row != npos && row < row_info.size());
1561 return cell_info[row][column].cellno;
1565 void Tabular::setUsebox(idx_type cell, BoxType type)
1567 cellInfo(cell).usebox = type;
1571 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1573 if (column_info[cellColumn(cell)].p_width.zero() &&
1574 !(isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1576 if (cellInfo(cell).usebox > 1)
1577 return cellInfo(cell).usebox;
1578 return useParbox(cell);
1583 // This are functions used for the longtable support
1585 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1591 row_info[row].endfirsthead = flag;
1595 row_info[row].endhead = flag;
1600 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1603 hd.set = haveLTHead();
1604 return row_info[row].endhead;
1608 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1611 hd.set = haveLTFirstHead();
1612 return row_info[row].endfirsthead;
1616 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1622 row_info[row].endlastfoot = flag;
1626 row_info[row].endfoot = flag;
1631 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1634 fd.set = haveLTFoot();
1635 return row_info[row].endfoot;
1639 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1642 fd.set = haveLTLastFoot();
1643 return row_info[row].endlastfoot;
1647 void Tabular::setLTNewPage(row_type row, bool what)
1649 row_info[row].newpage = what;
1653 bool Tabular::getLTNewPage(row_type row) const
1655 return row_info[row].newpage;
1659 bool Tabular::haveLTHead() const
1661 for (row_type i = 0; i < row_info.size(); ++i)
1662 if (row_info[i].endhead)
1668 bool Tabular::haveLTFirstHead() const
1670 if (endfirsthead.empty)
1672 for (row_type i = 0; i < row_info.size(); ++i)
1673 if (row_info[i].endfirsthead)
1679 bool Tabular::haveLTFoot() const
1681 for (row_type i = 0; i < row_info.size(); ++i)
1682 if (row_info[i].endfoot)
1688 bool Tabular::haveLTLastFoot() const
1690 if (endlastfoot.empty)
1692 for (row_type i = 0; i < row_info.size(); ++i)
1693 if (row_info[i].endlastfoot)
1699 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1701 idx_type i = getFirstCellInRow(row);
1703 setMultiColumn(i, column_info.size());
1704 setTopLine(i, false);
1705 setBottomLine(i, false);
1706 setLeftLine(i, false);
1707 setRightLine(i, false);
1709 unsetMultiColumn(i);
1710 row_info[row].caption = what;
1715 bool Tabular::ltCaption(row_type row) const
1717 return row_info[row].caption;
1721 // end longtable support functions
1723 void Tabular::setRowAscent(row_type row, int height)
1725 if (row >= row_info.size() || row_info[row].ascent == height)
1727 row_info[row].ascent = height;
1731 void Tabular::setRowDescent(row_type row, int height)
1733 if (row >= row_info.size() || row_info[row].descent == height)
1735 row_info[row].descent = height;
1739 int Tabular::rowAscent(row_type row) const
1741 if (row >= row_info.size())
1743 return row_info[row].ascent;
1747 int Tabular::rowDescent(row_type row) const
1749 LASSERT(row < row_info.size(), /**/);
1750 return row_info[row].descent;
1754 int Tabular::height() const
1757 for (row_type row = 0; row < row_info.size(); ++row)
1758 height += rowAscent(row) + rowDescent(row) +
1759 getAdditionalHeight(row);
1764 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1766 LASSERT(row < row_info.size(), /**/);
1767 LASSERT(column < column_info.size(), /**/);
1768 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1772 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1774 // FIXME: assert or return 0 as in TeXBottomHLine()?
1775 LASSERT(row != npos, /**/);
1776 LASSERT(row < row_info.size(), /**/);
1778 idx_type const fcell = getFirstCellInRow(row);
1779 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1782 for (idx_type i = fcell; i < n; ++i) {
1786 if (use_booktabs && row == 0) {
1789 } else if (tmp == n - fcell) {
1790 os << (use_booktabs ? "\\midrule " : "\\hline ");
1792 for (idx_type i = fcell; i < n; ++i) {
1794 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1795 << cellColumn(i) + 1
1797 << cellRightColumn(i) + 1
1809 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1811 // FIXME: return 0 or assert as in TeXTopHLine()?
1812 if (row == npos || row >= row_info.size())
1815 idx_type const fcell = getFirstCellInRow(row);
1816 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1819 for (idx_type i = fcell; i < n; ++i) {
1823 if (use_booktabs && row == row_info.size() - 1) {
1824 if (bottomLine(fcell))
1825 os << "\\bottomrule";
1826 } else if (tmp == n - fcell) {
1827 os << (use_booktabs ? "\\midrule" : "\\hline");
1829 for (idx_type i = fcell; i < n; ++i) {
1830 if (bottomLine(i)) {
1831 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1832 << cellColumn(i) + 1
1834 << cellRightColumn(i) + 1
1846 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1849 row_type const r = cellRow(cell);
1850 if (is_long_tabular && row_info[r].caption)
1853 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1854 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1855 // figure out how to set the lines
1856 // we always set double lines to the right of the cell
1857 col_type const c = cellColumn(cell);
1858 col_type const nextcol = c + columnSpan(cell);
1859 bool colright = columnRightLine(c);
1860 bool colleft = columnLeftLine(c);
1861 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1862 bool nextcellleft = nextcol < column_info.size()
1863 && leftLine(cellIndex(r, nextcol));
1864 bool coldouble = colright && nextcolleft;
1865 bool celldouble = rightLine(cell) && nextcellleft;
1866 bool prevcellright = c > 0 && rightLine(cellIndex(r, c - 1));
1867 ismulticol = isMultiColumn(cell)
1868 || (c == 0 && colleft != leftLine(cell))
1869 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1870 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1871 || (coldouble != celldouble);
1873 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1874 if (leftLine(cell) || prevcellright)
1876 if (!cellInfo(cell).align_special.empty()) {
1877 os << cellInfo(cell).align_special;
1879 if (!getPWidth(cell).zero()) {
1881 case LYX_VALIGN_TOP:
1884 case LYX_VALIGN_MIDDLE:
1887 case LYX_VALIGN_BOTTOM:
1892 << from_ascii(getPWidth(cell).asLatexString())
1896 case LYX_ALIGN_LEFT:
1899 case LYX_ALIGN_RIGHT:
1906 } // end if else !getPWidth
1907 } // end if else !cellinfo_of_cell
1908 if (rightLine(cell) || nextcellleft)
1911 // add extra vertical line if we want a double one
1915 if (getRotateCell(cell)) {
1916 os << "\\begin{sideways}\n";
1919 if (getUsebox(cell) == BOX_PARBOX) {
1922 case LYX_VALIGN_TOP:
1925 case LYX_VALIGN_MIDDLE:
1928 case LYX_VALIGN_BOTTOM:
1932 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1934 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1935 os << "\\begin{minipage}[";
1937 case LYX_VALIGN_TOP:
1940 case LYX_VALIGN_MIDDLE:
1943 case LYX_VALIGN_BOTTOM:
1947 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1955 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
1958 row_type const r = cellRow(cell);
1959 if (is_long_tabular && row_info[r].caption)
1963 if (getUsebox(cell) == BOX_PARBOX)
1965 else if (getUsebox(cell) == BOX_MINIPAGE) {
1966 os << "%\n\\end{minipage}";
1969 if (getRotateCell(cell)) {
1970 os << "%\n\\end{sideways}";
1980 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
1981 OutputParams const & runparams) const
1983 if (!is_long_tabular)
1987 // output header info
1989 if (endhead.topDL) {
1993 for (row_type i = 0; i < row_info.size(); ++i) {
1994 if (row_info[i].endhead) {
1995 ret += TeXRow(os, i, runparams);
1998 if (endhead.bottomDL) {
2002 os << "\\endhead\n";
2004 if (endfirsthead.empty) {
2005 os << "\\endfirsthead\n";
2009 // output firstheader info
2010 if (haveLTFirstHead()) {
2011 if (endfirsthead.topDL) {
2015 for (row_type i = 0; i < row_info.size(); ++i) {
2016 if (row_info[i].endfirsthead) {
2017 ret += TeXRow(os, i, runparams);
2020 if (endfirsthead.bottomDL) {
2024 os << "\\endfirsthead\n";
2027 // output footer info
2029 if (endfoot.topDL) {
2033 for (row_type i = 0; i < row_info.size(); ++i) {
2034 if (row_info[i].endfoot) {
2035 ret += TeXRow(os, i, runparams);
2038 if (endfoot.bottomDL) {
2042 os << "\\endfoot\n";
2044 if (endlastfoot.empty) {
2045 os << "\\endlastfoot\n";
2049 // output lastfooter info
2050 if (haveLTLastFoot()) {
2051 if (endlastfoot.topDL) {
2055 for (row_type i = 0; i < row_info.size(); ++i) {
2056 if (row_info[i].endlastfoot) {
2057 ret += TeXRow(os, i, runparams);
2060 if (endlastfoot.bottomDL) {
2064 os << "\\endlastfoot\n";
2071 bool Tabular::isValidRow(row_type row) const
2073 if (!is_long_tabular)
2075 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2076 !row_info[row].endfoot && !row_info[row].endlastfoot;
2080 int Tabular::TeXRow(odocstream & os, row_type i,
2081 OutputParams const & runparams) const
2083 idx_type cell = cellIndex(i, 0);
2084 int ret = TeXTopHLine(os, i);
2085 if (row_info[i].top_space_default) {
2087 os << "\\addlinespace\n";
2089 os << "\\noalign{\\vskip\\doublerulesep}\n";
2091 } else if(!row_info[i].top_space.zero()) {
2093 os << "\\addlinespace["
2094 << from_ascii(row_info[i].top_space.asLatexString())
2097 os << "\\noalign{\\vskip"
2098 << from_ascii(row_info[i].top_space.asLatexString())
2103 bool ismulticol = false;
2104 for (col_type j = 0; j < column_info.size(); ++j) {
2105 if (isPartOfMultiColumn(i, j))
2107 ret += TeXCellPreamble(os, cell, ismulticol);
2108 shared_ptr<InsetTableCell> inset = cellInset(cell);
2110 Paragraph const & par = inset->paragraphs().front();
2111 bool rtl = par.isRTL(buffer().params())
2113 && getPWidth(cell).zero();
2116 if (par.getParLanguage(buffer().params())->lang() ==
2119 else if (par.getParLanguage(buffer().params())->lang() == "arabic_arabi")
2121 // currently, remaning RTL languages are arabic_arabtex and hebrew
2125 ret += inset->latex(os, runparams);
2129 ret += TeXCellPostamble(os, cell, ismulticol);
2130 if (!isLastCellInRow(cell)) { // not last cell in row
2135 os << "\\tabularnewline";
2136 if (row_info[i].bottom_space_default) {
2138 os << "\\addlinespace";
2140 os << "[\\doublerulesep]";
2141 } else if (!row_info[i].bottom_space.zero()) {
2143 os << "\\addlinespace";
2145 << from_ascii(row_info[i].bottom_space.asLatexString())
2150 ret += TeXBottomHLine(os, i);
2151 if (row_info[i].interline_space_default) {
2153 os << "\\addlinespace\n";
2155 os << "\\noalign{\\vskip\\doublerulesep}\n";
2157 } else if (!row_info[i].interline_space.zero()) {
2159 os << "\\addlinespace["
2160 << from_ascii(row_info[i].interline_space.asLatexString())
2163 os << "\\noalign{\\vskip"
2164 << from_ascii(row_info[i].interline_space.asLatexString())
2172 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2176 //+---------------------------------------------------------------------
2177 //+ first the opening preamble +
2178 //+---------------------------------------------------------------------
2181 os << "\\begin{sideways}\n";
2184 if (is_long_tabular)
2185 os << "\\begin{longtable}{";
2187 os << "\\begin{tabular}{";
2189 for (col_type i = 0; i < column_info.size(); ++i) {
2190 if (columnLeftLine(i))
2192 if (!column_info[i].align_special.empty()) {
2193 os << column_info[i].align_special;
2195 if (!column_info[i].p_width.zero()) {
2196 switch (column_info[i].alignment) {
2197 case LYX_ALIGN_LEFT:
2198 os << ">{\\raggedright}";
2200 case LYX_ALIGN_RIGHT:
2201 os << ">{\\raggedleft}";
2203 case LYX_ALIGN_CENTER:
2204 os << ">{\\centering}";
2206 case LYX_ALIGN_NONE:
2207 case LYX_ALIGN_BLOCK:
2208 case LYX_ALIGN_LAYOUT:
2209 case LYX_ALIGN_SPECIAL:
2213 switch (column_info[i].valignment) {
2214 case LYX_VALIGN_TOP:
2217 case LYX_VALIGN_MIDDLE:
2220 case LYX_VALIGN_BOTTOM:
2225 << from_ascii(column_info[i].p_width.asLatexString())
2228 switch (column_info[i].alignment) {
2229 case LYX_ALIGN_LEFT:
2232 case LYX_ALIGN_RIGHT:
2239 } // end if else !column_info[i].p_width
2240 } // end if else !column_info[i].align_special
2241 if (columnRightLine(i))
2247 ret += TeXLongtableHeaderFooter(os, runparams);
2249 //+---------------------------------------------------------------------
2250 //+ the single row and columns (cells) +
2251 //+---------------------------------------------------------------------
2253 for (row_type i = 0; i < row_info.size(); ++i) {
2254 if (isValidRow(i)) {
2255 ret += TeXRow(os, i, runparams);
2256 if (is_long_tabular && row_info[i].newpage) {
2257 os << "\\newpage\n";
2263 //+---------------------------------------------------------------------
2264 //+ the closing of the tabular +
2265 //+---------------------------------------------------------------------
2267 if (is_long_tabular)
2268 os << "\\end{longtable}";
2270 os << "\\end{tabular}";
2272 os << "\n\\end{sideways}";
2280 int Tabular::docbookRow(odocstream & os, row_type row,
2281 OutputParams const & runparams) const
2284 idx_type cell = getFirstCellInRow(row);
2287 for (col_type j = 0; j < column_info.size(); ++j) {
2288 if (isPartOfMultiColumn(row, j))
2291 os << "<entry align=\"";
2292 switch (getAlignment(cell)) {
2293 case LYX_ALIGN_LEFT:
2296 case LYX_ALIGN_RIGHT:
2304 os << "\" valign=\"";
2305 switch (getVAlignment(cell)) {
2306 case LYX_VALIGN_TOP:
2309 case LYX_VALIGN_BOTTOM:
2312 case LYX_VALIGN_MIDDLE:
2317 if (isMultiColumn(cell)) {
2318 os << " namest=\"col" << j << "\" ";
2319 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2323 ret += cellInset(cell)->docbook(os, runparams);
2332 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2336 //+---------------------------------------------------------------------
2337 //+ first the opening preamble +
2338 //+---------------------------------------------------------------------
2340 os << "<tgroup cols=\"" << column_info.size()
2341 << "\" colsep=\"1\" rowsep=\"1\">\n";
2343 for (col_type i = 0; i < column_info.size(); ++i) {
2344 os << "<colspec colname=\"col" << i << "\" align=\"";
2345 switch (column_info[i].alignment) {
2346 case LYX_ALIGN_LEFT:
2349 case LYX_ALIGN_RIGHT:
2357 if (runparams.flavor == OutputParams::XML)
2363 //+---------------------------------------------------------------------
2364 //+ Long Tabular case +
2365 //+---------------------------------------------------------------------
2367 // output header info
2368 if (haveLTHead() || haveLTFirstHead()) {
2371 for (row_type i = 0; i < row_info.size(); ++i) {
2372 if (row_info[i].endhead || row_info[i].endfirsthead) {
2373 ret += docbookRow(os, i, runparams);
2379 // output footer info
2380 if (haveLTFoot() || haveLTLastFoot()) {
2383 for (row_type i = 0; i < row_info.size(); ++i) {
2384 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2385 ret += docbookRow(os, i, runparams);
2392 //+---------------------------------------------------------------------
2393 //+ the single row and columns (cells) +
2394 //+---------------------------------------------------------------------
2398 for (row_type i = 0; i < row_info.size(); ++i) {
2399 if (isValidRow(i)) {
2400 ret += docbookRow(os, i, runparams);
2405 //+---------------------------------------------------------------------
2406 //+ the closing of the tabular +
2407 //+---------------------------------------------------------------------
2416 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2417 vector<unsigned int> const & clen) const
2419 idx_type const fcell = getFirstCellInRow(row);
2420 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2423 for (idx_type i = fcell; i < n; ++i) {
2433 for (idx_type i = fcell; i < n; ++i) {
2444 col_type column = cellColumn(i);
2445 int len = clen[column];
2446 while (column < column_info.size() - 1
2447 && isPartOfMultiColumn(row, ++column))
2448 len += clen[column] + 4;
2449 os << docstring(len, ch);
2464 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2465 vector<unsigned int> const & clen) const
2467 idx_type const fcell = getFirstCellInRow(row);
2468 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2471 for (idx_type i = fcell; i < n; ++i) {
2472 if (bottomLine(i)) {
2481 for (idx_type i = fcell; i < n; ++i) {
2482 if (bottomLine(i)) {
2492 col_type column = cellColumn(i);
2493 int len = clen[column];
2494 while (column < column_info.size() -1
2495 && isPartOfMultiColumn(row, ++column))
2496 len += clen[column] + 4;
2497 os << docstring(len, ch);
2498 if (bottomLine(i)) {
2512 void Tabular::plaintextPrintCell(odocstream & os,
2513 OutputParams const & runparams,
2514 idx_type cell, row_type row, col_type column,
2515 vector<unsigned int> const & clen,
2516 bool onlydata) const
2518 odocstringstream sstr;
2519 cellInset(cell)->plaintext(sstr, runparams);
2531 unsigned int len1 = sstr.str().length();
2532 unsigned int len2 = clen[column];
2533 while (column < column_info.size() -1
2534 && isPartOfMultiColumn(row, ++column))
2535 len2 += clen[column] + 4;
2538 switch (getAlignment(cell)) {
2540 case LYX_ALIGN_LEFT:
2543 case LYX_ALIGN_RIGHT:
2547 case LYX_ALIGN_CENTER:
2553 os << docstring(len1, ' ') << sstr.str()
2554 << docstring(len2, ' ');
2556 if (rightLine(cell))
2563 void Tabular::plaintext(odocstream & os,
2564 OutputParams const & runparams, int const depth,
2565 bool onlydata, char_type delim) const
2567 // first calculate the width of the single columns
2568 vector<unsigned int> clen(column_info.size());
2571 // first all non (real) multicolumn cells!
2572 for (col_type j = 0; j < column_info.size(); ++j) {
2574 for (row_type i = 0; i < row_info.size(); ++i) {
2575 idx_type cell = cellIndex(i, j);
2576 if (isMultiColumnReal(cell))
2578 odocstringstream sstr;
2579 cellInset(cell)->plaintext(sstr, runparams);
2580 if (clen[j] < sstr.str().length())
2581 clen[j] = sstr.str().length();
2584 // then all (real) multicolumn cells!
2585 for (col_type j = 0; j < column_info.size(); ++j) {
2586 for (row_type i = 0; i < row_info.size(); ++i) {
2587 idx_type cell = cellIndex(i, j);
2588 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2590 odocstringstream sstr;
2591 cellInset(cell)->plaintext(sstr, runparams);
2592 int len = int(sstr.str().length());
2593 idx_type const n = columnSpan(cell);
2594 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2596 if (len > int(clen[j + n - 1]))
2597 clen[j + n - 1] = len;
2602 for (row_type i = 0; i < row_info.size(); ++i) {
2603 if (!onlydata && plaintextTopHLine(os, i, clen))
2604 os << docstring(depth * 2, ' ');
2605 for (col_type j = 0; j < column_info.size(); ++j) {
2606 if (isPartOfMultiColumn(i, j))
2608 if (onlydata && j > 0)
2609 // we don't use operator<< for single UCS4 character.
2610 // see explanation in docstream.h
2612 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2617 os << docstring(depth * 2, ' ');
2618 if (plaintextBottomHLine(os, i, clen))
2619 os << docstring(depth * 2, ' ');
2625 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2627 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2631 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2632 col_type column) const
2634 return cell_info[row][column].inset;
2638 void Tabular::setCellInset(row_type row, col_type column,
2639 shared_ptr<InsetTableCell> ins) const
2641 CellData & cd = cell_info[row][column];
2643 // reset the InsetTableCell's pointers
2644 ins->setCellData(&cd);
2645 ins->setTabular(this);
2649 Tabular::idx_type Tabular::cellFromInset(Inset const * inset) const
2651 // is this inset part of the tabular?
2653 lyxerr << "Error: this is not a cell of the tabular!" << endl;
2654 LASSERT(false, /**/);
2657 for (idx_type cell = 0, n = numberofcells; cell < n; ++cell)
2658 if (cellInset(cell).get() == inset) {
2659 LYXERR(Debug::INSETTEXT, "Tabular::cellFromInset: "
2660 << "cell=" << cell);
2664 // We should have found a cell at this point
2665 lyxerr << "Tabular::cellFromInset: Cell of inset "
2666 << inset << " not found!" << endl;
2667 LASSERT(false, /**/);
2673 void Tabular::validate(LaTeXFeatures & features) const
2675 features.require("NeedTabularnewline");
2677 features.require("booktabs");
2678 if (is_long_tabular)
2679 features.require("longtable");
2681 features.require("rotating");
2682 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2683 if (getVAlignment(cell) != LYX_VALIGN_TOP ||
2684 (!getPWidth(cell).zero() && !isMultiColumn(cell)))
2685 features.require("array");
2686 cellInset(cell)->validate(features);
2691 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2693 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2694 ParagraphList::const_iterator cit = parlist.begin();
2695 ParagraphList::const_iterator end = parlist.end();
2697 for (; cit != end; ++cit)
2698 for (int i = 0; i < cit->size(); ++i)
2699 if (cit->isNewline(i))
2706 /////////////////////////////////////////////////////////////////////
2710 /////////////////////////////////////////////////////////////////////
2712 InsetTableCell::InsetTableCell(Buffer const & buf,
2713 Tabular::CellData const * cell, Tabular const * table)
2714 : InsetText(buf), cell_data_(cell), table_(table)
2718 bool InsetTableCell::forceEmptyLayout(idx_type) const
2720 LASSERT(table_, /**/);
2721 LASSERT(cell_data_, /**/);
2722 return table_->getPWidth(cell_data_->cellno).zero();
2725 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2727 LASSERT(table_, /**/);
2728 LASSERT(cell_data_, /**/);
2729 return !table_->getPWidth(cell_data_->cellno).zero();
2732 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2733 FuncStatus & status) const
2736 switch (cmd.action) {
2738 enabled = !forceEmptyLayout();
2740 case LFUN_LAYOUT_PARAGRAPH:
2741 enabled = allowParagraphCustomization();
2744 return InsetText::getStatus(cur, cmd, status);
2746 status.enabled(enabled);
2750 /////////////////////////////////////////////////////////////////////
2754 /////////////////////////////////////////////////////////////////////
2756 InsetTabular::InsetTabular(Buffer const & buf, row_type rows,
2758 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2759 rowselect_(false), colselect_(false)
2761 setBuffer(const_cast<Buffer &>(buf)); // FIXME: remove later
2765 InsetTabular::InsetTabular(InsetTabular const & tab)
2766 : Inset(tab), tabular(tab.tabular), scx_(0)
2768 setBuffer(const_cast<Buffer &>(tab.buffer())); // FIXME: remove later
2772 InsetTabular::~InsetTabular()
2774 hideDialogs("tabular", this);
2778 bool InsetTabular::insetAllowed(InsetCode code) const
2780 if (code == MATHMACRO_CODE)
2787 void InsetTabular::write(ostream & os) const
2789 os << "Tabular" << endl;
2794 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2796 // FIXME: depending on the selection state, we could offer a different menu.
2797 return from_ascii("context-tabular");
2801 void InsetTabular::read(Lexer & lex)
2803 //bool const old_format = (lex.getString() == "\\LyXTable");
2811 string token = lex.getString();
2812 while (lex && token != "\\end_inset") {
2814 token = lex.getString();
2817 lex.printError("Missing \\end_inset at this point. ");
2821 int InsetTabular::rowFromY(Cursor & cur, int y) const
2823 // top y coordinate of tabular
2824 int h = yo(cur.bv()) - tabular.rowAscent(0);
2825 size_t nrows = tabular.row_info.size();
2827 for (; r < nrows && y > h; ++r) {
2828 h += tabular.rowAscent(r);
2829 h += tabular.rowDescent(r);
2830 h += tabular.getAdditionalHeight(r);
2836 int InsetTabular::columnFromX(Cursor & cur, int x) const
2838 // left x coordinate of tabular
2839 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2840 size_t ncols = tabular.column_info.size();
2842 for (; c < ncols && x > w; ++c)
2843 w += tabular.columnWidth(c);
2848 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2850 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2851 // mi.base.textwidth << "\n";
2854 LASSERT(false, /**/);
2858 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2861 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2862 if (tabular.isPartOfMultiColumn(i, j))
2863 // Multicolumn cell, but not first one
2868 if (tabular.cell_info[i][j].multicolumn ==
2869 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2870 p_width = tabular.cellInfo(cell).p_width;
2872 p_width = tabular.column_info[j].p_width;
2873 if (!p_width.zero())
2874 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2875 tabular.cellInset(cell)->metrics(m, dim);
2876 if (!p_width.zero())
2877 dim.wid = m.base.textwidth;
2878 tabular.setCellWidth(cell, dim.wid);
2879 maxAsc = max(maxAsc, dim.asc);
2880 maxDesc = max(maxDesc, dim.des);
2883 int const top_space = tabular.row_info[i].top_space_default ?
2884 default_line_space :
2885 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2886 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2887 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2888 default_line_space :
2889 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2890 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2892 tabular.updateColumnWidths();
2893 dim.asc = tabular.rowAscent(0);
2894 dim.des = tabular.height() - dim.asc;
2895 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2899 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2901 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2902 BufferView * bv = pi.base.bv;
2904 // FIXME: As the full backrgound is painted in drawSelection(),
2905 // we have no choice but to do a full repaint for the Text cells.
2906 pi.full_repaint = true;
2908 resetPos(bv->cursor());
2911 x += ADD_TO_TABULAR_WIDTH;
2913 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2916 first_visible_cell = Tabular::npos;
2917 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2919 int const a = tabular.rowAscent(i);
2920 int const d = tabular.rowDescent(i);
2921 idx = tabular.cellIndex(i, 0);
2922 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2923 if (tabular.isPartOfMultiColumn(i, j))
2925 if (first_visible_cell == Tabular::npos)
2926 first_visible_cell = idx;
2928 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2929 // Cache the Inset position.
2930 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2931 if (nx + tabular.columnWidth(idx) < 0
2932 || nx > bv->workWidth()
2934 || y - a > bv->workHeight()) {
2935 pi.pain.setDrawingEnabled(false);
2936 cell(idx)->draw(pi, cx, y);
2937 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2938 pi.pain.setDrawingEnabled(original_drawing_state);
2940 cell(idx)->draw(pi, cx, y);
2941 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2943 nx += tabular.columnWidth(idx);
2947 if (i + 1 < tabular.row_info.size())
2948 y += d + tabular.rowAscent(i + 1) +
2949 tabular.getAdditionalHeight(i + 1);
2954 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
2956 Cursor & cur = pi.base.bv->cursor();
2958 x += scx_ + ADD_TO_TABULAR_WIDTH;
2960 // FIXME: it is wrong to completely paint the background
2961 // if we want to do single row painting.
2963 // Paint background of current tabular
2964 int const w = tabular.width();
2965 int const h = tabular.height();
2966 int yy = y - tabular.rowAscent(0);
2967 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
2969 if (!cur.selection())
2971 if (&cur.inset() != this)
2977 if (tablemode(cur)) {
2980 getSelection(cur, rs, re, cs, ce);
2981 y -= tabular.rowAscent(0);
2982 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
2983 int const a = tabular.rowAscent(j);
2984 int const h = a + tabular.rowDescent(j);
2986 y += tabular.getAdditionalHeight(j);
2987 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
2988 if (tabular.isPartOfMultiColumn(j, i))
2990 idx_type const cell =
2991 tabular.cellIndex(j, i);
2992 int const w = tabular.columnWidth(cell);
2993 if (i >= cs && i <= ce && j >= rs && j <= re)
2994 pi.pain.fillRectangle(xx, y, w, h,
3002 x += cellXPos(cur.idx());
3003 x += tabular.getBeginningOfTextInCell(cur.idx());
3004 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3009 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3010 row_type row, idx_type cell, bool erased) const
3012 int x2 = x + tabular.columnWidth(cell);
3013 bool on_off = false;
3014 ColorCode col = Color_tabularline;
3015 ColorCode onoffcol = Color_tabularonoffline;
3018 col = Color_deletedtext;
3019 onoffcol = Color_deletedtext;
3022 if (!tabular.topAlreadyDrawn(cell)) {
3023 on_off = !tabular.topLine(cell);
3024 pain.line(x, y - tabular.rowAscent(row),
3025 x2, y - tabular.rowAscent(row),
3026 on_off ? onoffcol : col,
3027 on_off ? Painter::line_onoffdash : Painter::line_solid);
3029 on_off = !tabular.bottomLine(cell);
3030 pain.line(x, y + tabular.rowDescent(row),
3031 x2, y + tabular.rowDescent(row),
3032 on_off ? onoffcol : col,
3033 on_off ? Painter::line_onoffdash : Painter::line_solid);
3034 if (!tabular.leftAlreadyDrawn(cell)) {
3035 on_off = !tabular.leftLine(cell);
3036 pain.line(x, y - tabular.rowAscent(row),
3037 x, y + tabular.rowDescent(row),
3038 on_off ? onoffcol : col,
3039 on_off ? Painter::line_onoffdash : Painter::line_solid);
3041 on_off = !tabular.rightLine(cell);
3042 pain.line(x2 - tabular.getAdditionalWidth(cell),
3043 y - tabular.rowAscent(row),
3044 x2 - tabular.getAdditionalWidth(cell),
3045 y + tabular.rowDescent(row),
3046 on_off ? onoffcol : col,
3047 on_off ? Painter::line_onoffdash : Painter::line_solid);
3051 docstring InsetTabular::editMessage() const
3053 return _("Opened table");
3057 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3059 //lyxerr << "InsetTabular::edit: " << this << endl;
3061 cur.selection() = false;
3064 if (isRightToLeft(cur))
3065 cur.idx() = tabular.getLastCellInRow(0);
3071 if (isRightToLeft(cur))
3072 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3074 cur.idx() = tabular.numberofcells - 1;
3076 cur.pos() = cur.lastpos(); // FIXME crude guess
3078 // FIXME: this accesses the position cache before it is initialized
3080 //cur.bv().fitCursor();
3084 void InsetTabular::updateLabels(ParIterator const & it)
3086 // In a longtable, tell captions what the current float is
3087 Counters & cnts = buffer().params().documentClass().counters();
3088 string const saveflt = cnts.current_float();
3089 if (tabular.is_long_tabular)
3090 cnts.current_float("table");
3092 ParIterator it2 = it;
3094 size_t const end = it2.nargs();
3095 for ( ; it2.idx() < end; it2.top().forwardIdx())
3096 lyx::updateLabels(buffer(), it2);
3099 if (tabular.is_long_tabular)
3100 cnts.current_float(saveflt);
3104 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3106 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3107 << "\n cur:" << cur);
3108 CursorSlice sl = cur.top();
3109 Cursor & bvcur = cur.bv().cursor();
3111 switch (cmd.action) {
3113 case LFUN_MOUSE_PRESS: {
3114 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3116 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3117 || cmd.x > xo(cur.bv()) + tabular.width()) {
3118 row_type r = rowFromY(cur, cmd.y);
3119 cur.idx() = tabular.getFirstCellInRow(r);
3122 cur.idx() = tabular.getLastCellInRow(r);
3123 cur.pos() = cur.lastpos();
3124 cur.selection() = true;
3130 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3131 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3132 || cmd.y > y0 + tabular.height()) {
3133 col_type c = columnFromX(cur, cmd.x);
3134 cur.idx() = tabular.cellIndex(0, c);
3137 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3138 cur.pos() = cur.lastpos();
3139 cur.selection() = true;
3144 // do not reset cursor/selection if we have selected
3145 // some cells (bug 2715).
3146 if (cmd.button() == mouse_button::button3
3147 && &bvcur.selBegin().inset() == this
3148 && tablemode(bvcur))
3151 // Let InsetTableCell do it
3152 cell(cur.idx())->dispatch(cur, cmd);
3155 case LFUN_MOUSE_MOTION:
3156 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3157 if (cmd.button() == mouse_button::button1) {
3158 // only accept motions to places not deeper nested than the real anchor
3159 if (!bvcur.anchor_.hasPart(cur)) {
3163 // select (additional) row
3165 row_type r = rowFromY(cur, cmd.y);
3166 cur.idx() = tabular.getLastCellInRow(r);
3167 bvcur.setCursor(cur);
3168 bvcur.selection() = true;
3171 // select (additional) column
3173 col_type c = columnFromX(cur, cmd.x);
3174 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3175 bvcur.setCursor(cur);
3176 bvcur.selection() = true;
3179 // only update if selection changes
3180 if (bvcur.idx() == cur.idx() &&
3181 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3183 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3184 bvcur.setCursor(cur);
3185 bvcur.selection() = true;
3189 case LFUN_MOUSE_RELEASE:
3194 case LFUN_CELL_BACKWARD:
3196 cur.selection() = false;
3199 case LFUN_CELL_FORWARD:
3201 cur.selection() = false;
3203 case LFUN_CHAR_FORWARD_SELECT:
3204 case LFUN_CHAR_FORWARD:
3205 case LFUN_CHAR_BACKWARD_SELECT:
3206 case LFUN_CHAR_BACKWARD:
3207 case LFUN_CHAR_RIGHT_SELECT:
3208 case LFUN_CHAR_RIGHT:
3209 case LFUN_CHAR_LEFT_SELECT:
3210 case LFUN_CHAR_LEFT: {
3212 // determine whether we move to next or previous cell, where to enter
3213 // the new cell from, and which command to "finish" (i.e., exit the
3216 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3217 FuncCode finish_lfun;
3219 if (cmd.action == LFUN_CHAR_FORWARD
3220 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3222 finish_lfun = LFUN_FINISHED_FORWARD;
3224 else if (cmd.action == LFUN_CHAR_BACKWARD
3225 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3227 finish_lfun = LFUN_FINISHED_BACKWARD;
3229 // LEFT or RIGHT commands --- the interpretation will depend on the
3230 // table's direction.
3232 bool right = (cmd.action == LFUN_CHAR_RIGHT
3233 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3234 next_cell = (isRightToLeft(cur) != right);
3236 if (lyxrc.visual_cursor) {
3237 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3241 finish_lfun = LFUN_FINISHED_RIGHT;
3243 finish_lfun = LFUN_FINISHED_LEFT;
3247 // finally, now that we know what we want to do, do it!
3248 cell(cur.idx())->dispatch(cur, cmd);
3249 if (!cur.result().dispatched()) {
3250 // move to next/prev cell, as appropriate
3251 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3252 << " cell from: " << int(entry_from));
3254 moveNextCell(cur, entry_from);
3256 movePrevCell(cur, entry_from);
3257 // if we're exiting the table, call the appropriate FINISHED lfun
3258 if (sl == cur.top())
3259 cmd = FuncRequest(finish_lfun);
3267 case LFUN_DOWN_SELECT:
3269 cell(cur.idx())->dispatch(cur, cmd);
3270 cur.dispatched(); // override the cell's decision
3271 if (sl == cur.top())
3272 // if our Text didn't do anything to the cursor
3273 // then we try to put the cursor into the cell below
3274 // setting also the right targetX.
3275 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3276 cur.idx() = tabular.cellBelow(cur.idx());
3278 TextMetrics const & tm =
3279 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3280 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3282 if (sl == cur.top()) {
3283 // we trick it to go to forward after leaving the
3285 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3290 case LFUN_UP_SELECT:
3292 cell(cur.idx())->dispatch(cur, cmd);
3293 cur.dispatched(); // override the cell's decision
3294 if (sl == cur.top())
3295 // if our Text didn't do anything to the cursor
3296 // then we try to put the cursor into the cell above
3297 // setting also the right targetX.
3298 if (tabular.cellRow(cur.idx()) != 0) {
3299 cur.idx() = tabular.cellAbove(cur.idx());
3300 cur.pit() = cur.lastpit();
3301 Text const * text = cell(cur.idx())->getText(0);
3302 TextMetrics const & tm = cur.bv().textMetrics(text);
3303 ParagraphMetrics const & pm =
3304 tm.parMetrics(cur.lastpit());
3305 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3307 if (sl == cur.top()) {
3308 cmd = FuncRequest(LFUN_UP);
3313 // case LFUN_SCREEN_DOWN: {
3314 // //if (hasSelection())
3315 // // cur.selection() = false;
3316 // col_type const col = tabular.cellColumn(cur.idx());
3317 // int const t = cur.bv().top_y() + cur.bv().height();
3318 // if (t < yo() + tabular.getHeightOfTabular()) {
3319 // cur.bv().scrollDocView(t);
3320 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3322 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3329 // case LFUN_SCREEN_UP: {
3330 // //if (hasSelection())
3331 // // cur.selection() = false;
3332 // col_type const col = tabular.cellColumn(cur.idx());
3333 // int const t = cur.bv().top_y() + cur.bv().height();
3335 // cur.bv().scrollDocView(t);
3339 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3343 // cur.par() = cur.lastpar();
3344 // cur.pos() = cur.lastpos();
3348 case LFUN_LAYOUT_TABULAR:
3349 cur.bv().showDialog("tabular", params2string(*this), this);
3352 case LFUN_INSET_DIALOG_UPDATE:
3353 cur.bv().updateDialog("tabular", params2string(*this));
3356 case LFUN_TABULAR_FEATURE:
3357 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3361 // insert file functions
3362 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3363 case LFUN_FILE_INSERT_PLAINTEXT: {
3365 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3366 FileName(to_utf8(cmd.argument())));
3369 cur.recordUndoInset(INSERT_UNDO);
3370 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3371 // content has been replaced,
3372 // so cursor might be invalid
3373 cur.pos() = cur.lastpos();
3374 cur.pit() = cur.lastpit();
3375 bvcur.setCursor(cur);
3382 if (tablemode(cur)) {
3383 if (copySelection(cur)) {
3384 cur.recordUndoInset(DELETE_UNDO);
3389 cell(cur.idx())->dispatch(cur, cmd);
3392 case LFUN_CHAR_DELETE_BACKWARD:
3393 case LFUN_CHAR_DELETE_FORWARD:
3394 if (tablemode(cur)) {
3395 cur.recordUndoInset(DELETE_UNDO);
3399 cell(cur.idx())->dispatch(cur, cmd);
3403 if (!cur.selection())
3405 if (tablemode(cur)) {
3409 cell(cur.idx())->dispatch(cur, cmd);
3412 case LFUN_CLIPBOARD_PASTE:
3413 case LFUN_PRIMARY_SELECTION_PASTE: {
3414 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3415 theClipboard().getAsText() :
3416 theSelection().get();
3419 // pass to InsertPlaintextString, but
3420 // only if we have multi-cell content
3421 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3422 cur.recordUndoInset(INSERT_UNDO);
3423 if (insertPlaintextString(cur.bv(), clip, false)) {
3424 // content has been replaced,
3425 // so cursor might be invalid
3426 cur.pos() = cur.lastpos();
3427 cur.pit() = cur.lastpit();
3428 bvcur.setCursor(cur);
3432 // Let the cell handle normal text
3433 cell(cur.idx())->dispatch(cur, cmd);
3438 if (tabularStackDirty() && theClipboard().isInternal() ||
3439 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3440 cur.recordUndoInset(INSERT_UNDO);
3441 pasteClipboard(cur);
3444 cell(cur.idx())->dispatch(cur, cmd);
3447 case LFUN_FONT_EMPH:
3448 case LFUN_FONT_BOLD:
3449 case LFUN_FONT_ROMAN:
3450 case LFUN_FONT_NOUN:
3451 case LFUN_FONT_ITAL:
3452 case LFUN_FONT_FRAK:
3453 case LFUN_FONT_TYPEWRITER:
3454 case LFUN_FONT_SANS:
3455 case LFUN_FONT_FREE_APPLY:
3456 case LFUN_FONT_FREE_UPDATE:
3457 case LFUN_FONT_SIZE:
3458 case LFUN_FONT_UNDERLINE:
3460 case LFUN_WORD_CAPITALIZE:
3461 case LFUN_WORD_UPCASE:
3462 case LFUN_WORD_LOWCASE:
3463 case LFUN_CHARS_TRANSPOSE:
3464 if (tablemode(cur)) {
3467 getSelection(cur, rs, re, cs, ce);
3468 Cursor tmpcur = cur;
3469 for (row_type i = rs; i <= re; ++i) {
3470 for (col_type j = cs; j <= ce; ++j) {
3471 // cursor follows cell:
3472 tmpcur.idx() = tabular.cellIndex(i, j);
3473 // select this cell only:
3476 tmpcur.resetAnchor();
3477 tmpcur.pit() = tmpcur.lastpit();
3478 tmpcur.pos() = tmpcur.top().lastpos();
3479 tmpcur.setCursor(tmpcur);
3480 tmpcur.setSelection();
3481 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3486 cell(cur.idx())->dispatch(cur, cmd);
3490 // we try to handle this event in the insets dispatch function.
3491 cell(cur.idx())->dispatch(cur, cmd);
3497 // function sets an object as defined in func_status.h:
3498 // states OK, Unknown, Disabled, On, Off.
3499 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3500 FuncStatus & status) const
3502 switch (cmd.action) {
3503 case LFUN_TABULAR_FEATURE: {
3504 int action = Tabular::LAST_ACTION;
3506 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3507 string const tmp = tabularFeature[i].feature;
3508 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3509 action = tabularFeature[i].action;
3513 if (action == Tabular::LAST_ACTION) {
3515 status.unknown(true);
3519 string const argument
3520 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3522 row_type sel_row_start = 0;
3523 row_type sel_row_end = 0;
3524 col_type sel_col_start = 0;
3525 col_type sel_col_end = 0;
3526 Tabular::ltType dummyltt;
3529 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3532 case Tabular::SET_PWIDTH:
3533 case Tabular::SET_MPWIDTH:
3534 case Tabular::SET_SPECIAL_COLUMN:
3535 case Tabular::SET_SPECIAL_MULTI:
3536 case Tabular::APPEND_ROW:
3537 case Tabular::APPEND_COLUMN:
3538 case Tabular::DELETE_ROW:
3539 case Tabular::DELETE_COLUMN:
3540 case Tabular::COPY_ROW:
3541 case Tabular::COPY_COLUMN:
3542 case Tabular::SET_TOP_SPACE:
3543 case Tabular::SET_BOTTOM_SPACE:
3544 case Tabular::SET_INTERLINE_SPACE:
3548 case Tabular::MULTICOLUMN:
3549 status.enabled(sel_row_start == sel_row_end);
3550 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3553 case Tabular::SET_ALL_LINES:
3554 case Tabular::UNSET_ALL_LINES:
3555 case Tabular::SET_BORDER_LINES:
3556 status.enabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3559 case Tabular::TOGGLE_LINE_TOP:
3560 status.enabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3561 status.setOnOff(tabular.topLine(cur.idx()));
3564 case Tabular::TOGGLE_LINE_BOTTOM:
3565 status.enabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3566 status.setOnOff(tabular.bottomLine(cur.idx()));
3569 case Tabular::TOGGLE_LINE_LEFT:
3570 status.enabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3571 status.setOnOff(tabular.leftLine(cur.idx()));
3574 case Tabular::TOGGLE_LINE_RIGHT:
3575 status.enabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3576 status.setOnOff(tabular.rightLine(cur.idx()));
3579 case Tabular::M_ALIGN_LEFT:
3581 case Tabular::ALIGN_LEFT:
3582 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3585 case Tabular::M_ALIGN_RIGHT:
3587 case Tabular::ALIGN_RIGHT:
3588 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3591 case Tabular::M_ALIGN_CENTER:
3593 case Tabular::ALIGN_CENTER:
3594 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3597 case Tabular::ALIGN_BLOCK:
3598 status.enabled(!tabular.getPWidth(cur.idx()).zero());
3599 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3602 case Tabular::M_VALIGN_TOP:
3604 case Tabular::VALIGN_TOP:
3606 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3609 case Tabular::M_VALIGN_BOTTOM:
3611 case Tabular::VALIGN_BOTTOM:
3613 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3616 case Tabular::M_VALIGN_MIDDLE:
3618 case Tabular::VALIGN_MIDDLE:
3620 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3623 case Tabular::SET_LONGTABULAR:
3624 status.setOnOff(tabular.is_long_tabular);
3627 case Tabular::UNSET_LONGTABULAR:
3628 status.setOnOff(!tabular.is_long_tabular);
3631 case Tabular::TOGGLE_ROTATE_TABULAR:
3632 case Tabular::SET_ROTATE_TABULAR:
3633 status.setOnOff(tabular.rotate);
3636 case Tabular::UNSET_ROTATE_TABULAR:
3637 status.setOnOff(!tabular.rotate);
3640 case Tabular::TOGGLE_ROTATE_CELL:
3641 case Tabular::SET_ROTATE_CELL:
3642 status.setOnOff(!oneCellHasRotationState(false,
3643 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3646 case Tabular::UNSET_ROTATE_CELL:
3647 status.setOnOff(!oneCellHasRotationState(true,
3648 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3651 case Tabular::SET_USEBOX:
3652 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3655 case Tabular::SET_LTFIRSTHEAD:
3656 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3659 case Tabular::UNSET_LTFIRSTHEAD:
3660 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3663 case Tabular::SET_LTHEAD:
3664 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3667 case Tabular::UNSET_LTHEAD:
3668 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3671 case Tabular::SET_LTFOOT:
3672 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3675 case Tabular::UNSET_LTFOOT:
3676 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3679 case Tabular::SET_LTLASTFOOT:
3680 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3683 case Tabular::UNSET_LTLASTFOOT:
3684 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3687 case Tabular::SET_LTNEWPAGE:
3688 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3691 case Tabular::TOGGLE_LTCAPTION:
3692 status.enabled(sel_row_start == sel_row_end);
3693 status.setOnOff(tabular.ltCaption(sel_row_start));
3696 case Tabular::SET_BOOKTABS:
3697 status.setOnOff(tabular.use_booktabs);
3700 case Tabular::UNSET_BOOKTABS:
3701 status.setOnOff(!tabular.use_booktabs);
3706 status.enabled(false);
3712 // These are only enabled inside tabular
3713 case LFUN_CELL_BACKWARD:
3714 case LFUN_CELL_FORWARD:
3715 status.enabled(true);
3718 // disable these with multiple cells selected
3719 case LFUN_INSET_INSERT:
3720 case LFUN_TABULAR_INSERT:
3721 case LFUN_FLEX_INSERT:
3722 case LFUN_FLOAT_INSERT:
3723 case LFUN_FLOAT_WIDE_INSERT:
3724 case LFUN_FOOTNOTE_INSERT:
3725 case LFUN_MARGINALNOTE_INSERT:
3726 case LFUN_MATH_INSERT:
3727 case LFUN_MATH_MODE:
3728 case LFUN_MATH_MUTATE:
3729 case LFUN_MATH_DISPLAY:
3730 case LFUN_NOTE_INSERT:
3731 case LFUN_OPTIONAL_INSERT:
3732 case LFUN_BOX_INSERT:
3733 case LFUN_BRANCH_INSERT:
3734 case LFUN_WRAP_INSERT:
3735 case LFUN_ERT_INSERT: {
3736 if (tablemode(cur)) {
3737 status.enabled(false);
3740 return cell(cur.idx())->getStatus(cur, cmd, status);
3743 // disable in non-fixed-width cells
3744 case LFUN_NEWLINE_INSERT:
3745 case LFUN_BREAK_PARAGRAPH:
3746 case LFUN_BREAK_PARAGRAPH_SKIP: {
3747 if (tabular.getPWidth(cur.idx()).zero()) {
3748 status.enabled(false);
3751 return cell(cur.idx())->getStatus(cur, cmd, status);
3755 if (tabularStackDirty() && theClipboard().isInternal()) {
3756 status.enabled(true);
3759 return cell(cur.idx())->getStatus(cur, cmd, status);
3761 case LFUN_INSET_MODIFY:
3762 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3763 status.enabled(true);
3769 // we try to handle this event in the insets dispatch function.
3770 return cell(cur.idx())->getStatus(cur, cmd, status);
3775 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3777 return tabular.latex(os, runparams);
3781 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3783 os << '\n'; // output table on a new line
3784 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3785 tabular.plaintext(os, runparams, dp, false, 0);
3786 return PLAINTEXT_NEWLINE;
3790 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3795 // FIXME: Why not pass a proper DocIterator here?
3797 // if the table is inside a float it doesn't need the informaltable
3798 // wrapper. Search for it.
3799 for (master = owner(); master; master = master->owner())
3800 if (master->lyxCode() == FLOAT_CODE)
3805 os << "<informaltable>";
3808 ret += tabular.docbook(os, runparams);
3810 os << "</informaltable>";
3817 void InsetTabular::validate(LaTeXFeatures & features) const
3819 tabular.validate(features);
3823 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3825 return tabular.cellInset(idx);
3829 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3831 return tabular.cellInset(idx);
3835 void InsetTabular::cursorPos(BufferView const & bv,
3836 CursorSlice const & sl, bool boundary, int & x, int & y) const
3838 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3840 // y offset correction
3841 int const row = tabular.cellRow(sl.idx());
3842 for (int i = 0; i <= row; ++i) {
3844 y += tabular.rowAscent(i);
3845 y += tabular.getAdditionalHeight(i);
3848 y += tabular.rowDescent(i);
3851 // x offset correction
3852 int const col = tabular.cellColumn(sl.idx());
3853 int idx = tabular.cellIndex(row, 0);
3854 for (int j = 0; j < col; ++j) {
3855 if (tabular.isPartOfMultiColumn(row, j))
3857 x += tabular.columnWidth(idx);
3860 x += tabular.getBeginningOfTextInCell(idx);
3861 x += ADD_TO_TABULAR_WIDTH;
3866 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3870 Inset const & inset = *tabular.cellInset(cell);
3871 Point o = bv.coordCache().getInsets().xy(&inset);
3872 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3873 int const xend = xbeg + tabular.columnWidth(cell);
3874 row_type const row = tabular.cellRow(cell);
3875 int const ybeg = o.y_ - tabular.rowAscent(row) -
3876 tabular.getAdditionalHeight(row);
3877 int const yend = o.y_ + tabular.rowDescent(row);
3889 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3890 // << " ybeg=" << ybeg << " yend=" << yend
3891 // << " xx=" << xx << " yy=" << yy
3892 // << " dist=" << xx + yy << endl;
3897 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3899 //lyxerr << "InsetTabular::editXY: " << this << endl;
3900 cur.selection() = false;
3902 cur.idx() = getNearestCell(cur.bv(), x, y);
3904 return cur.bv().textMetrics(&cell(cur.idx())->text_).editXY(cur, x, y);
3908 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3910 cur.idx() = getNearestCell(cur.bv(), x, y);
3911 cur.bv().textMetrics(&cell(cur.idx())->text_).setCursorFromCoordinates(cur, x, y);
3915 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3917 idx_type idx_min = 0;
3918 int dist_min = numeric_limits<int>::max();
3919 for (idx_type i = 0, n = nargs(); i != n; ++i) {
3920 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
3921 int const d = dist(bv, i, x, y);
3932 int InsetTabular::cellXPos(idx_type const cell) const
3936 for (; !tabular.isFirstCellInRow(c); --c)
3939 for (; c < cell; ++c)
3940 lx += tabular.columnWidth(c);
3946 void InsetTabular::resetPos(Cursor & cur) const
3948 BufferView & bv = cur.bv();
3949 int const maxwidth = bv.workWidth();
3951 if (&cur.inset() != this) {
3955 int const X2 = maxwidth;
3956 int const offset = ADD_TO_TABULAR_WIDTH + 2;
3957 int const x1 = xo(cur.bv()) + cellXPos(cur.idx()) + offset;
3958 int const x2 = x1 + tabular.columnWidth(cur.idx());
3961 scx_ = X1 + 20 - x1;
3963 scx_ = X2 - 20 - x2;
3968 cur.updateFlags(Update::Force | Update::FitCursor);
3972 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
3974 if (isRightToLeft(cur)) {
3975 if (tabular.isFirstCellInRow(cur.idx())) {
3976 row_type const row = tabular.cellRow(cur.idx());
3977 if (row == tabular.row_info.size() - 1)
3979 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
3986 if (tabular.isLastCell(cur.idx()))
3992 cur.boundary(false);
3994 // in visual mode, place cursor at extreme left or right
3996 switch(entry_from) {
3998 case ENTRY_DIRECTION_RIGHT:
3999 cur.posVisToRowExtremity(false /* !left */);
4001 case ENTRY_DIRECTION_LEFT:
4002 cur.posVisToRowExtremity(true /* left */);
4004 case ENTRY_DIRECTION_IGNORE:
4005 // nothing to do in this case
4014 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4016 if (isRightToLeft(cur)) {
4017 if (tabular.isLastCellInRow(cur.idx())) {
4018 row_type const row = tabular.cellRow(cur.idx());
4021 cur.idx() = tabular.getFirstCellInRow(row);
4022 cur.idx() = tabular.cellAbove(cur.idx());
4024 if (tabular.isLastCell(cur.idx()))
4029 if (cur.idx() == 0) // first cell
4033 cur.pit() = cur.lastpit();
4034 cur.pos() = cur.lastpos();
4036 // in visual mode, place cursor at extreme left or right
4038 switch(entry_from) {
4040 case ENTRY_DIRECTION_RIGHT:
4041 cur.posVisToRowExtremity(false /* !left */);
4043 case ENTRY_DIRECTION_LEFT:
4044 cur.posVisToRowExtremity(true /* left */);
4046 case ENTRY_DIRECTION_IGNORE:
4047 // nothing to do in this case
4052 // FIXME: this accesses the position cache before it is initialized
4057 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4059 Tabular::Feature action = Tabular::LAST_ACTION;
4062 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4063 string const tmp = tabularFeature[i].feature;
4065 if (tmp == what.substr(0, tmp.length())) {
4066 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4067 //tabularFeatures[i].feature.length()))
4068 action = tabularFeature[i].action;
4072 if (action == Tabular::LAST_ACTION)
4076 ltrim(what.substr(tabularFeature[i].feature.length()));
4077 tabularFeatures(cur, action, val);
4082 static void checkLongtableSpecial(Tabular::ltType & ltt,
4083 string const & special, bool & flag)
4085 if (special == "dl_above") {
4088 } else if (special == "dl_below") {
4089 ltt.bottomDL = flag;
4091 } else if (special == "empty") {
4100 bool InsetTabular::oneCellHasRotationState(bool rotated,
4101 row_type row_start, row_type row_end,
4102 col_type col_start, col_type col_end) const {
4104 for (row_type i = row_start; i <= row_end; ++i) {
4105 for (col_type j = col_start; j <= col_end; ++j) {
4106 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4115 void InsetTabular::tabularFeatures(Cursor & cur,
4116 Tabular::Feature feature, string const & value)
4118 col_type sel_col_start;
4119 col_type sel_col_end;
4120 row_type sel_row_start;
4121 row_type sel_row_end;
4122 bool setLines = false;
4123 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4124 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4128 case Tabular::M_ALIGN_LEFT:
4129 case Tabular::ALIGN_LEFT:
4130 setAlign = LYX_ALIGN_LEFT;
4133 case Tabular::M_ALIGN_RIGHT:
4134 case Tabular::ALIGN_RIGHT:
4135 setAlign = LYX_ALIGN_RIGHT;
4138 case Tabular::M_ALIGN_CENTER:
4139 case Tabular::ALIGN_CENTER:
4140 setAlign = LYX_ALIGN_CENTER;
4143 case Tabular::ALIGN_BLOCK:
4144 setAlign = LYX_ALIGN_BLOCK;
4147 case Tabular::M_VALIGN_TOP:
4148 case Tabular::VALIGN_TOP:
4149 setVAlign = Tabular::LYX_VALIGN_TOP;
4152 case Tabular::M_VALIGN_BOTTOM:
4153 case Tabular::VALIGN_BOTTOM:
4154 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4157 case Tabular::M_VALIGN_MIDDLE:
4158 case Tabular::VALIGN_MIDDLE:
4159 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4166 cur.recordUndoInset(ATOMIC_UNDO);
4168 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4169 row_type const row = tabular.cellRow(cur.idx());
4170 col_type const column = tabular.cellColumn(cur.idx());
4172 Tabular::ltType ltt;
4176 case Tabular::SET_PWIDTH: {
4177 Length const len(value);
4178 tabular.setColumnPWidth(cur, cur.idx(), len);
4180 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4181 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4185 case Tabular::SET_MPWIDTH:
4186 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4189 case Tabular::SET_SPECIAL_COLUMN:
4190 case Tabular::SET_SPECIAL_MULTI:
4191 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4194 case Tabular::APPEND_ROW:
4195 // append the row into the tabular
4196 tabular.appendRow(cur.idx());
4199 case Tabular::APPEND_COLUMN:
4200 // append the column into the tabular
4201 tabular.appendColumn(cur.idx());
4202 cur.idx() = tabular.cellIndex(row, column);
4205 case Tabular::DELETE_ROW:
4206 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4207 tabular.deleteRow(sel_row_start);
4208 if (sel_row_start >= tabular.row_info.size())
4210 cur.idx() = tabular.cellIndex(sel_row_start, column);
4213 cur.selection() = false;
4216 case Tabular::DELETE_COLUMN:
4217 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4218 tabular.deleteColumn(sel_col_start);
4219 if (sel_col_start >= tabular.column_info.size())
4221 cur.idx() = tabular.cellIndex(row, sel_col_start);
4224 cur.selection() = false;
4227 case Tabular::COPY_ROW:
4228 tabular.copyRow(row);
4231 case Tabular::COPY_COLUMN:
4232 tabular.copyColumn(column);
4233 cur.idx() = tabular.cellIndex(row, column);
4236 case Tabular::TOGGLE_LINE_TOP: {
4237 bool lineSet = !tabular.topLine(cur.idx());
4238 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4239 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4240 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4244 case Tabular::TOGGLE_LINE_BOTTOM: {
4245 bool lineSet = !tabular.bottomLine(cur.idx());
4246 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4247 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4248 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4252 case Tabular::TOGGLE_LINE_LEFT: {
4253 bool lineSet = !tabular.leftLine(cur.idx());
4254 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4255 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4256 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4260 case Tabular::TOGGLE_LINE_RIGHT: {
4261 bool lineSet = !tabular.rightLine(cur.idx());
4262 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4263 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4264 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4268 case Tabular::M_ALIGN_LEFT:
4269 case Tabular::M_ALIGN_RIGHT:
4270 case Tabular::M_ALIGN_CENTER:
4272 case Tabular::ALIGN_LEFT:
4273 case Tabular::ALIGN_RIGHT:
4274 case Tabular::ALIGN_CENTER:
4275 case Tabular::ALIGN_BLOCK:
4276 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4277 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4278 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4281 case Tabular::M_VALIGN_TOP:
4282 case Tabular::M_VALIGN_BOTTOM:
4283 case Tabular::M_VALIGN_MIDDLE:
4285 case Tabular::VALIGN_TOP:
4286 case Tabular::VALIGN_BOTTOM:
4287 case Tabular::VALIGN_MIDDLE:
4288 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4289 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4290 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4293 case Tabular::MULTICOLUMN: {
4294 if (!cur.selection()) {
4295 // just multicol for one single cell
4296 // check whether we are completely in a multicol
4297 if (tabular.isMultiColumn(cur.idx()))
4298 tabular.unsetMultiColumn(cur.idx());
4300 tabular.setMultiColumn(cur.idx(), 1);
4303 // we have a selection so this means we just add all this
4304 // cells to form a multicolumn cell
4305 idx_type const s_start = cur.selBegin().idx();
4306 idx_type const s_end = cur.selEnd().idx();
4307 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4308 cur.idx() = s_start;
4311 cur.selection() = false;
4315 case Tabular::SET_ALL_LINES:
4317 case Tabular::UNSET_ALL_LINES:
4318 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4319 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4320 tabular.setAllLines(
4321 tabular.cellIndex(i,j), setLines);
4324 case Tabular::SET_BORDER_LINES:
4325 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4326 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4327 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4329 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4330 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4331 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4335 case Tabular::SET_LONGTABULAR:
4336 tabular.is_long_tabular = true;
4339 case Tabular::UNSET_LONGTABULAR:
4340 tabular.is_long_tabular = false;
4343 case Tabular::SET_ROTATE_TABULAR:
4344 tabular.rotate = true;
4347 case Tabular::UNSET_ROTATE_TABULAR:
4348 tabular.rotate = false;
4351 case Tabular::TOGGLE_ROTATE_TABULAR:
4352 tabular.rotate = !tabular.rotate;
4355 case Tabular::SET_ROTATE_CELL:
4356 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4357 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4358 tabular.setRotateCell(
4359 tabular.cellIndex(i, j), true);
4362 case Tabular::UNSET_ROTATE_CELL:
4363 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4364 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4365 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4368 case Tabular::TOGGLE_ROTATE_CELL:
4370 bool oneNotRotated = oneCellHasRotationState(false,
4371 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4373 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4374 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4375 tabular.setRotateCell(tabular.cellIndex(i, j),
4380 case Tabular::SET_USEBOX: {
4381 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4382 if (val == tabular.getUsebox(cur.idx()))
4383 val = Tabular::BOX_NONE;
4384 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4385 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4386 tabular.setUsebox(tabular.cellIndex(i, j), val);
4390 case Tabular::UNSET_LTFIRSTHEAD:
4392 case Tabular::SET_LTFIRSTHEAD:
4393 tabular.getRowOfLTFirstHead(row, ltt);
4394 checkLongtableSpecial(ltt, value, flag);
4395 tabular.setLTHead(row, flag, ltt, true);
4398 case Tabular::UNSET_LTHEAD:
4400 case Tabular::SET_LTHEAD:
4401 tabular.getRowOfLTHead(row, ltt);
4402 checkLongtableSpecial(ltt, value, flag);
4403 tabular.setLTHead(row, flag, ltt, false);
4406 case Tabular::UNSET_LTFOOT:
4408 case Tabular::SET_LTFOOT:
4409 tabular.getRowOfLTFoot(row, ltt);
4410 checkLongtableSpecial(ltt, value, flag);
4411 tabular.setLTFoot(row, flag, ltt, false);
4414 case Tabular::UNSET_LTLASTFOOT:
4416 case Tabular::SET_LTLASTFOOT:
4417 tabular.getRowOfLTLastFoot(row, ltt);
4418 checkLongtableSpecial(ltt, value, flag);
4419 tabular.setLTFoot(row, flag, ltt, true);
4422 case Tabular::SET_LTNEWPAGE:
4423 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4426 case Tabular::TOGGLE_LTCAPTION:
4427 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4430 cur.selection() = false;
4433 case Tabular::SET_BOOKTABS:
4434 tabular.use_booktabs = true;
4437 case Tabular::UNSET_BOOKTABS:
4438 tabular.use_booktabs = false;
4441 case Tabular::SET_TOP_SPACE: {
4443 if (value == "default")
4444 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4445 tabular.row_info[i].top_space_default = true;
4446 else if (isValidLength(value, &len))
4447 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4448 tabular.row_info[i].top_space_default = false;
4449 tabular.row_info[i].top_space = len;
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4453 tabular.row_info[i].top_space_default = false;
4454 tabular.row_info[i].top_space = len;
4459 case Tabular::SET_BOTTOM_SPACE: {
4461 if (value == "default")
4462 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4463 tabular.row_info[i].bottom_space_default = true;
4464 else if (isValidLength(value, &len))
4465 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4466 tabular.row_info[i].bottom_space_default = false;
4467 tabular.row_info[i].bottom_space = len;
4470 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4471 tabular.row_info[i].bottom_space_default = false;
4472 tabular.row_info[i].bottom_space = len;
4477 case Tabular::SET_INTERLINE_SPACE: {
4479 if (value == "default")
4480 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4481 tabular.row_info[i].interline_space_default = true;
4482 else if (isValidLength(value, &len))
4483 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4484 tabular.row_info[i].interline_space_default = false;
4485 tabular.row_info[i].interline_space = len;
4488 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4489 tabular.row_info[i].interline_space_default = false;
4490 tabular.row_info[i].interline_space = len;
4495 // dummy stuff just to avoid warnings
4496 case Tabular::LAST_ACTION:
4502 bool InsetTabular::showInsetDialog(BufferView * bv) const
4504 bv->showDialog("tabular", params2string(*this),
4505 const_cast<InsetTabular *>(this));
4510 void InsetTabular::openLayoutDialog(BufferView * bv) const
4512 bv->showDialog("tabular", params2string(*this),
4513 const_cast<InsetTabular *>(this));
4517 bool InsetTabular::copySelection(Cursor & cur)
4519 if (!cur.selection())
4524 getSelection(cur, rs, re, cs, ce);
4526 paste_tabular.reset(new Tabular(tabular));
4528 for (row_type i = 0; i < rs; ++i)
4529 paste_tabular->deleteRow(0);
4531 row_type const rows = re - rs + 1;
4532 while (paste_tabular->row_info.size() > rows)
4533 paste_tabular->deleteRow(rows);
4535 for (col_type i = 0; i < cs; ++i)
4536 paste_tabular->deleteColumn(0);
4538 col_type const columns = ce - cs + 1;
4539 while (paste_tabular->column_info.size() > columns)
4540 paste_tabular->deleteColumn(columns);
4542 // We clear all the InsetTableCell pointers, since they
4543 // might now become invalid and there is no point in having
4544 // them point to temporary things in paste_tabular.
4545 for (row_type i = 0; i < paste_tabular->row_info.size(); ++i)
4546 for (col_type j = 0; j < paste_tabular->column_info.size(); ++j) {
4547 paste_tabular->cellInset(i,j)->setCellData(0);
4548 paste_tabular->cellInset(i,j)->setTabular(0);
4551 odocstringstream os;
4552 OutputParams const runparams(0);
4553 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4554 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4555 cap::copySelection(cur, os.str());
4557 // mark tabular stack dirty
4558 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4559 // when we (hopefully) have a one-for-all paste mechanism.
4560 // This must be called after cap::copySelection.
4561 dirtyTabularStack(true);
4567 bool InsetTabular::pasteClipboard(Cursor & cur)
4571 col_type const actcol = tabular.cellColumn(cur.idx());
4572 row_type const actrow = tabular.cellRow(cur.idx());
4573 for (row_type r1 = 0, r2 = actrow;
4574 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4576 for (col_type c1 = 0, c2 = actcol;
4577 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4579 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4580 tabular.isPartOfMultiColumn(r2, c2))
4582 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4586 if (tabular.isPartOfMultiColumn(r2, c2)) {
4590 shared_ptr<InsetTableCell> inset(
4591 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4592 // note that setCellInset will call InsetTableCell::setCellData()
4593 // and InsetTableCell::setTabular()
4594 tabular.setCellInset(r2, c2, inset);
4595 // FIXME: change tracking (MG)
4596 inset->setChange(Change(cur.buffer().params().trackChanges ?
4597 Change::INSERTED : Change::UNCHANGED));
4605 void InsetTabular::cutSelection(Cursor & cur)
4607 if (!cur.selection())
4612 getSelection(cur, rs, re, cs, ce);
4613 for (row_type i = rs; i <= re; ++i) {
4614 for (col_type j = cs; j <= ce; ++j) {
4615 shared_ptr<InsetTableCell> t
4616 = cell(tabular.cellIndex(i, j));
4617 if (cur.buffer().params().trackChanges)
4618 // FIXME: Change tracking (MG)
4619 t->setChange(Change(Change::DELETED));
4625 // cursor position might be invalid now
4626 if (cur.pit() > cur.lastpit())
4627 cur.pit() = cur.lastpit();
4628 if (cur.pos() > cur.lastpos())
4629 cur.pos() = cur.lastpos();
4630 cur.clearSelection();
4634 bool InsetTabular::isRightToLeft(Cursor & cur) const
4636 LASSERT(cur.depth() > 1, /**/);
4637 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4638 pos_type const parentpos = cur[cur.depth() - 2].pos();
4639 return parentpar.getFontSettings(cur.bv().buffer().params(),
4640 parentpos).language()->rightToLeft();
4644 void InsetTabular::getSelection(Cursor & cur,
4645 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4647 CursorSlice const & beg = cur.selBegin();
4648 CursorSlice const & end = cur.selEnd();
4649 cs = tabular.cellColumn(beg.idx());
4650 ce = tabular.cellColumn(end.idx());
4653 cs = tabular.cellColumn(end.idx());
4655 ce = tabular.cellRightColumn(end.idx());
4658 rs = tabular.cellRow(beg.idx());
4659 re = tabular.cellRow(end.idx());
4665 Text * InsetTabular::getText(int idx) const
4667 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4671 void InsetTabular::setChange(Change const & change)
4673 for (idx_type idx = 0; idx < nargs(); ++idx)
4674 cell(idx)->setChange(change);
4678 void InsetTabular::acceptChanges(BufferParams const & bparams)
4680 for (idx_type idx = 0; idx < nargs(); ++idx)
4681 cell(idx)->acceptChanges(bparams);
4685 void InsetTabular::rejectChanges(BufferParams const & bparams)
4687 for (idx_type idx = 0; idx < nargs(); ++idx)
4688 cell(idx)->rejectChanges(bparams);
4692 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4694 return tabular.getPWidth(cell).zero();
4698 bool InsetTabular::forceEmptyLayout(idx_type cell) const
4700 return !tabular.getPWidth(cell).zero();
4704 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4707 if (buf.length() <= 0)
4712 col_type maxCols = 1;
4713 size_t const len = buf.length();
4717 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4725 maxCols = max(cols, maxCols);
4731 maxCols = max(cols, maxCols);
4737 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4738 loctab = paste_tabular.get();
4740 dirtyTabularStack(true);
4743 cell = bv.cursor().idx();
4744 ocol = tabular.cellColumn(cell);
4745 row = tabular.cellRow(cell);
4749 idx_type const cells = loctab->numberofcells;
4752 rows = loctab->row_info.size();
4753 col_type const columns = loctab->column_info.size();
4755 while (cell < cells && p < len && row < rows &&
4756 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4762 // we can only set this if we are not too far right
4763 if (cols < columns) {
4764 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4765 Font const font = bv.textMetrics(&inset->text_).
4767 inset->setText(buf.substr(op, p - op), font,
4768 buffer().params().trackChanges);
4774 // we can only set this if we are not too far right
4775 if (cols < columns) {
4776 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4777 Font const font = bv.textMetrics(&inset->text_).
4779 inset->setText(buf.substr(op, p - op), font,
4780 buffer().params().trackChanges);
4785 cell = loctab->cellIndex(row, cols);
4791 // check for the last cell if there is no trailing '\n'
4792 if (cell < cells && op < len) {
4793 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4794 Font const font = bv.textMetrics(&inset->text_).displayFont(0, 0);
4795 inset->setText(buf.substr(op, len - op), font,
4796 buffer().params().trackChanges);
4802 void InsetTabular::addPreview(PreviewLoader & loader) const
4804 row_type const rows = tabular.row_info.size();
4805 col_type const columns = tabular.column_info.size();
4806 for (row_type i = 0; i < rows; ++i) {
4807 for (col_type j = 0; j < columns; ++j)
4808 tabular.cellInset(i, j)->addPreview(loader);
4813 bool InsetTabular::tablemode(Cursor & cur) const
4815 return cur.selection() && cur.selBegin().idx() != cur.selEnd().idx();
4819 bool InsetTabular::completionSupported(Cursor const & cur) const
4821 Cursor const & bvCur = cur.bv().cursor();
4822 if (&bvCur.inset() != this)
4824 return cur.text()->completionSupported(cur);
4828 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4830 return completionSupported(cur);
4834 bool InsetTabular::automaticInlineCompletion() const
4836 return lyxrc.completion_inline_text;
4840 bool InsetTabular::automaticPopupCompletion() const
4842 return lyxrc.completion_popup_text;
4846 bool InsetTabular::showCompletionCursor() const
4848 return lyxrc.completion_cursor_text;
4852 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4854 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4858 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4860 if (!completionSupported(cur))
4862 return cur.text()->completionPrefix(cur);
4866 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4868 if (!completionSupported(cur))
4871 return cur.text()->insertCompletion(cur, s, finished);
4875 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4876 Dimension & dim) const
4878 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4879 tm.completionPosAndDim(cur, x, y, dim);
4883 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4885 istringstream data(in);
4887 lex.setStream(data);
4894 if (!lex || token != "tabular") {
4895 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4899 // This is part of the inset proper that is usually swallowed
4900 // by Buffer::readInset
4902 if (!lex || token != "Tabular") {
4903 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
4911 string InsetTabular::params2string(InsetTabular const & inset)
4914 data << "tabular" << ' ';
4916 data << "\\end_inset\n";