2 * \file InsetTabular.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
13 * \author Jürgen Vigna
15 * Full author contact details are available in file CREDITS.
20 #include "InsetTabular.h"
22 #include "buffer_funcs.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "CoordCache.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "OutputParams.h"
40 #include "paragraph_funcs.h"
41 #include "Paragraph.h"
42 #include "ParagraphParameters.h"
43 #include "ParIterator.h"
44 #include "TextClass.h"
45 #include "TextMetrics.h"
47 #include "frontends/Application.h"
48 #include "frontends/alert.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Painter.h"
51 #include "frontends/Selection.h"
53 #include "support/convert.h"
54 #include "support/debug.h"
55 #include "support/docstream.h"
56 #include "support/FileName.h"
57 #include "support/gettext.h"
58 #include "support/lassert.h"
59 #include "support/lstrings.h"
61 #include <boost/scoped_ptr.hpp>
69 using namespace lyx::support;
71 using boost::shared_ptr;
72 using boost::dynamic_pointer_cast;
77 using cap::dirtyTabularStack;
78 using cap::tabularStackDirty;
80 using graphics::PreviewLoader;
82 using frontend::Painter;
83 using frontend::Clipboard;
85 namespace Alert = frontend::Alert;
90 int const ADD_TO_HEIGHT = 2; // in cell
91 int const ADD_TO_TABULAR_WIDTH = 6; // horiz space before and after the table
92 int const default_line_space = 10; // ?
93 int const WIDTH_OF_LINE = 5; // space between double lines
97 boost::scoped_ptr<Tabular> paste_tabular;
100 struct TabularFeature {
101 Tabular::Feature action;
106 TabularFeature tabularFeature[] =
108 { Tabular::APPEND_ROW, "append-row" },
109 { Tabular::APPEND_COLUMN, "append-column" },
110 { Tabular::DELETE_ROW, "delete-row" },
111 { Tabular::DELETE_COLUMN, "delete-column" },
112 { Tabular::COPY_ROW, "copy-row" },
113 { Tabular::COPY_COLUMN, "copy-column" },
114 { Tabular::TOGGLE_LINE_TOP, "toggle-line-top" },
115 { Tabular::TOGGLE_LINE_BOTTOM, "toggle-line-bottom" },
116 { Tabular::TOGGLE_LINE_LEFT, "toggle-line-left" },
117 { Tabular::TOGGLE_LINE_RIGHT, "toggle-line-right" },
118 { Tabular::ALIGN_LEFT, "align-left" },
119 { Tabular::ALIGN_RIGHT, "align-right" },
120 { Tabular::ALIGN_CENTER, "align-center" },
121 { Tabular::ALIGN_BLOCK, "align-block" },
122 { Tabular::VALIGN_TOP, "valign-top" },
123 { Tabular::VALIGN_BOTTOM, "valign-bottom" },
124 { Tabular::VALIGN_MIDDLE, "valign-middle" },
125 { Tabular::M_ALIGN_LEFT, "m-align-left" },
126 { Tabular::M_ALIGN_RIGHT, "m-align-right" },
127 { Tabular::M_ALIGN_CENTER, "m-align-center" },
128 { Tabular::M_VALIGN_TOP, "m-valign-top" },
129 { Tabular::M_VALIGN_BOTTOM, "m-valign-bottom" },
130 { Tabular::M_VALIGN_MIDDLE, "m-valign-middle" },
131 { Tabular::MULTICOLUMN, "multicolumn" },
132 { Tabular::SET_ALL_LINES, "set-all-lines" },
133 { Tabular::UNSET_ALL_LINES, "unset-all-lines" },
134 { Tabular::SET_LONGTABULAR, "set-longtabular" },
135 { Tabular::UNSET_LONGTABULAR, "unset-longtabular" },
136 { Tabular::SET_PWIDTH, "set-pwidth" },
137 { Tabular::SET_MPWIDTH, "set-mpwidth" },
138 { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular" },
139 { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular" },
140 { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular" },
141 { Tabular::SET_ROTATE_CELL, "set-rotate-cell" },
142 { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell" },
143 { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell" },
144 { Tabular::SET_USEBOX, "set-usebox" },
145 { Tabular::SET_LTHEAD, "set-lthead" },
146 { Tabular::UNSET_LTHEAD, "unset-lthead" },
147 { Tabular::SET_LTFIRSTHEAD, "set-ltfirsthead" },
148 { Tabular::UNSET_LTFIRSTHEAD, "unset-ltfirsthead" },
149 { Tabular::SET_LTFOOT, "set-ltfoot" },
150 { Tabular::UNSET_LTFOOT, "unset-ltfoot" },
151 { Tabular::SET_LTLASTFOOT, "set-ltlastfoot" },
152 { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot" },
153 { Tabular::SET_LTNEWPAGE, "set-ltnewpage" },
154 { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption" },
155 { Tabular::SET_SPECIAL_COLUMN, "set-special-column" },
156 { Tabular::SET_SPECIAL_MULTI, "set-special-multi" },
157 { Tabular::SET_BOOKTABS, "set-booktabs" },
158 { Tabular::UNSET_BOOKTABS, "unset-booktabs" },
159 { Tabular::SET_TOP_SPACE, "set-top-space" },
160 { Tabular::SET_BOTTOM_SPACE, "set-bottom-space" },
161 { Tabular::SET_INTERLINE_SPACE, "set-interline-space" },
162 { Tabular::SET_BORDER_LINES, "set-border-lines" },
163 { Tabular::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 & buf)
478 multicolumn(Tabular::CELL_NORMAL),
479 alignment(LYX_ALIGN_CENTER),
480 valignment(LYX_VALIGN_TOP),
487 inset(new InsetTableCell(buf))
489 inset->setBuffer(const_cast<Buffer &>(buf));
490 inset->paragraphs().back().setLayout(buf.params().documentClass().plainLayout());
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()))
512 Tabular::CellData & Tabular::CellData::operator=(CellData cs)
518 void Tabular::CellData::swap(CellData & rhs)
520 std::swap(cellno, rhs.cellno);
521 std::swap(width, rhs.width);
522 std::swap(multicolumn, rhs.multicolumn);
523 std::swap(alignment, rhs.alignment);
524 std::swap(valignment, rhs.valignment);
525 std::swap(top_line, rhs.top_line);
526 std::swap(bottom_line, rhs.bottom_line);
527 std::swap(left_line, rhs.left_line);
528 std::swap(right_line, rhs.right_line);
529 std::swap(usebox, rhs.usebox);
530 std::swap(rotate, rhs.rotate);
531 std::swap(align_special, rhs.align_special);
532 p_width.swap(rhs.p_width);
533 inset.swap(rhs.inset);
537 Tabular::RowData::RowData()
540 top_space_default(false),
541 bottom_space_default(false),
542 interline_space_default(false),
552 Tabular::ColumnData::ColumnData()
553 : alignment(LYX_ALIGN_CENTER),
554 valignment(LYX_VALIGN_TOP),
560 Tabular::ltType::ltType()
567 Tabular::Tabular(Buffer & buffer, row_type rows_arg, col_type columns_arg)
569 init(buffer, rows_arg, columns_arg);
573 void Tabular::setBuffer(Buffer & buffer)
576 size_t row_count = row_info.size();
577 size_t column_count = column_info.size();
578 // set silly default lines
579 for (row_type i = 0; i < row_count; ++i)
580 for (col_type j = 0; j < column_count; ++j)
581 cell_info[i][j].inset->setBuffer(*buffer_);
585 // activates all lines and sets all widths to 0
586 void Tabular::init(Buffer & buf, row_type rows_arg,
587 col_type columns_arg)
590 row_info = row_vector(rows_arg);
591 column_info = column_vector(columns_arg);
592 cell_info = cell_vvector(rows_arg, cell_vector(columns_arg, CellData(buf)));
593 row_info.reserve(10);
594 column_info.reserve(10);
595 cell_info.reserve(100);
597 is_long_tabular = false;
599 use_booktabs = false;
600 size_t row_count = row_info.size();
601 size_t column_count = column_info.size();
602 // set silly default lines
603 for (row_type i = 0; i < row_count; ++i)
604 for (col_type j = 0; j < column_count; ++j) {
605 cell_info[i][j].inset->setBuffer(*buffer_);
606 cell_info[i][j].top_line = true;
607 cell_info[i][j].left_line = true;
608 cell_info[i][j].bottom_line = i == 0 || i == row_count - 1;
609 cell_info[i][j].right_line = j == column_count - 1;
614 void Tabular::appendRow(idx_type const cell)
616 BufferParams const & bp = buffer().params();
617 row_type const row = cellRow(cell);
619 row_vector::iterator rit = row_info.begin() + row;
620 row_info.insert(rit, RowData());
621 // now set the values of the row before
622 row_info[row] = row_info[row + 1];
624 row_type const nrows = row_info.size();
625 col_type const ncols = column_info.size();
627 cell_vvector old(nrows - 1);
628 for (row_type i = 0; i < nrows - 1; ++i)
629 swap(cell_info[i], old[i]);
631 cell_info = cell_vvector(nrows, cell_vector(ncols, CellData(buffer())));
633 for (row_type i = 0; i <= row; ++i)
634 swap(cell_info[i], old[i]);
635 for (row_type i = row + 2; i < nrows; ++i)
636 swap(cell_info[i], old[i - 1]);
639 for (col_type c = 0; c < ncols; ++c) {
640 // inherit line settings
641 idx_type const i = cellIndex(row + 1, c);
642 idx_type const j = cellIndex(row, c);
643 setLeftLine(i, leftLine(j));
644 setRightLine(i, rightLine(j));
645 setTopLine(i, topLine(j));
646 if (topLine(j) && bottomLine(j)) {
647 setBottomLine(i, true);
648 setBottomLine(j, false);
650 // mark track changes
652 cellInfo(i).inset->setChange(Change(Change::INSERTED));
657 void Tabular::deleteRow(row_type const row)
659 // Not allowed to delete last row
660 if (row_info.size() == 1)
663 row_info.erase(row_info.begin() + row);
664 cell_info.erase(cell_info.begin() + row);
669 void Tabular::copyRow(row_type const row)
671 row_info.insert(row_info.begin() + row, row_info[row]);
672 cell_info.insert(cell_info.begin() + row, cell_info[row]);
674 if (buffer().params().trackChanges)
675 for (col_type j = 0; j < column_info.size(); ++j)
676 cell_info[row + 1][j].inset->setChange(Change(Change::INSERTED));
682 void Tabular::appendColumn(idx_type const cell)
684 col_type const c = cellColumn(cell);
685 column_vector::iterator cit = column_info.begin() + c + 1;
686 column_info.insert(cit, ColumnData());
687 row_type const nrows = row_info.size();
688 // set the column values of the column before
689 column_info[c + 1] = column_info[c];
691 for (row_type r = 0; r < nrows; ++r) {
692 cell_info[r].insert(cell_info[r].begin() + c + 1,
694 if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
695 cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
697 cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
700 for (row_type r = 0; r < nrows; ++r) {
701 // inherit line settings
702 idx_type const i = cellIndex(r, c + 1);
703 idx_type const j = cellIndex(r, c);
704 setBottomLine(i, bottomLine(j));
705 setTopLine(i, topLine(j));
706 setLeftLine(i, leftLine(j));
707 if (rightLine(j) && rightLine(j)) {
708 setRightLine(i, true);
709 setRightLine(j, false);
712 cellInfo(i).inset->clear();
713 if (buffer().params().trackChanges)
714 cellInfo(i).inset->setChange(Change(Change::INSERTED));
719 void Tabular::deleteColumn(col_type const column)
721 // Not allowed to delete last column
722 if (column_info.size() == 1)
725 column_info.erase(column_info.begin() + column);
726 size_t row_count = row_info.size();
727 for (row_type i = 0; i < row_count; ++i) {
728 // Care about multicolumn cells
729 if (column + 1 < column_info.size() &&
730 cell_info[i][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN &&
731 cell_info[i][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN) {
732 cell_info[i][column + 1].multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
734 cell_info[i].erase(cell_info[i].begin() + column);
740 void Tabular::copyColumn(col_type const column)
742 BufferParams const & bp = buffer().params();
743 column_info.insert(column_info.begin() + column, column_info[column]);
745 size_t row_count = row_info.size();
746 for (row_type i = 0; i < row_count; ++i)
747 cell_info[i].insert(cell_info[i].begin() + column, cell_info[i][column]);
750 for (row_type i = 0; i < row_count; ++i)
751 cell_info[i][column + 1].inset->setChange(Change(Change::INSERTED));
756 void Tabular::updateIndexes()
758 col_type ncols = column_info.size();
759 row_type nrows = row_info.size();
761 for (row_type row = 0; row < nrows; ++row)
762 for (col_type column = 0; column < ncols; ++column) {
763 if (!isPartOfMultiColumn(row, column))
765 cell_info[row][column].cellno = numberofcells - 1;
768 rowofcell.resize(numberofcells);
769 columnofcell.resize(numberofcells);
771 for (row_type row = 0; row < nrows; ++row)
772 for (col_type column = 0; column < ncols; ++column) {
773 if (isPartOfMultiColumn(row, column))
776 columnofcell[i] = column;
777 setFixedWidth(row, column);
783 Tabular::idx_type Tabular::numberOfCellsInRow(idx_type const cell) const
785 row_type const row = cellRow(cell);
787 for (col_type i = 0; i < column_info.size(); ++i)
788 if (cell_info[row][i].multicolumn != Tabular::CELL_PART_OF_MULTICOLUMN)
794 bool Tabular::topLine(idx_type const cell) const
796 return cellInfo(cell).top_line;
800 bool Tabular::bottomLine(idx_type const cell) const
802 return cellInfo(cell).bottom_line;
806 bool Tabular::leftLine(idx_type cell) const
810 return cellInfo(cell).left_line;
814 bool Tabular::rightLine(idx_type cell) const
818 return cellInfo(cell).right_line;
822 bool Tabular::topAlreadyDrawn(idx_type cell) const
824 row_type row = cellRow(cell);
827 idx_type i = cellIndex(row - 1, cellColumn(cell));
828 return !rowTopLine(row) && bottomLine(i);
832 bool Tabular::leftAlreadyDrawn(idx_type cell) const
834 col_type col = cellColumn(cell);
837 idx_type i = cellIndex(cellRow(cell), col - 1);
838 return rightLine(i) && !leftLine(cell);
842 bool Tabular::isLastRow(idx_type cell) const
844 return cellRow(cell) == row_info.size() - 1;
848 int Tabular::getAdditionalHeight(row_type row) const
850 if (!row || row >= row_info.size())
853 int const interline_space = row_info[row - 1].interline_space_default ?
855 row_info[row - 1].interline_space.inPixels(width());
856 if (rowTopLine(row) && rowBottomLine(row - 1))
857 return interline_space + WIDTH_OF_LINE;
858 return interline_space;
862 int Tabular::getAdditionalWidth(idx_type cell) const
864 col_type const nextcol = cellColumn(cell) + columnSpan(cell);
866 && nextcol < column_info.size() && leftLine(cellIndex(cellRow(cell), nextcol)))
867 return WIDTH_OF_LINE;
872 int Tabular::columnWidth(idx_type cell) const
875 col_type const span = columnSpan(cell);
876 col_type const col = cellColumn(cell);
877 for(col_type c = col; c < col + span ; ++c)
878 w += column_info[c].width;
883 bool Tabular::updateColumnWidths()
885 col_type const ncols = column_info.size();
886 row_type const nrows = row_info.size();
888 // for each col get max of single col cells
889 for(col_type c = 0; c < ncols; ++c) {
891 for(row_type r = 0; r < nrows; ++r) {
892 idx_type const i = cellIndex(r, c);
893 if (columnSpan(i) == 1)
894 new_width = max(new_width, cellInfo(i).width);
897 if (column_info[c].width != new_width) {
898 column_info[c].width = new_width;
902 // update col widths to fit merged cells
903 for(col_type c = 0; c < ncols; ++c)
904 for(row_type r = 0; r < nrows; ++r) {
905 idx_type const i = cellIndex(r, c);
906 int const span = columnSpan(i);
907 if (span == 1 || c > cellColumn(i))
911 for(col_type j = c; j < c + span ; ++j)
912 old_width += column_info[j].width;
914 if (cellInfo(i).width > old_width) {
915 column_info[c + span - 1].width += cellInfo(i).width - old_width;
924 int Tabular::width() const
926 col_type const ncols = column_info.size();
928 for (col_type i = 0; i < ncols; ++i)
929 width += column_info[i].width;
934 void Tabular::setCellWidth(idx_type cell, int new_width)
936 cellInfo(cell).width = new_width + 2 * WIDTH_OF_LINE
937 + getAdditionalWidth(cell);
941 void Tabular::setAlignment(idx_type cell, LyXAlignment align,
944 if (!isMultiColumn(cell) || onlycolumn)
945 column_info[cellColumn(cell)].alignment = align;
947 cellInfo(cell).alignment = align;
948 cellInset(cell).get()->setContentAlignment(align);
952 void Tabular::setVAlignment(idx_type cell, VAlignment align,
955 if (!isMultiColumn(cell) || onlycolumn)
956 column_info[cellColumn(cell)].valignment = align;
958 cellInfo(cell).valignment = align;
965 * Allow line and paragraph breaks for fixed width cells or disallow them,
966 * merge cell paragraphs and reset layout to standard for variable width
969 void toggleFixedWidth(Cursor & cur, InsetTableCell * inset, bool fixedWidth)
971 inset->setAutoBreakRows(fixedWidth);
972 inset->toggleFixedWidth(fixedWidth);
976 // merge all paragraphs to one
977 BufferParams const & bp = cur.bv().buffer().params();
978 while (inset->paragraphs().size() > 1)
979 mergeParagraph(bp, inset->paragraphs(), 0);
983 // undo information has already been recorded
984 inset->getText(0)->setLayout(cur.bv().buffer(), 0, cur.lastpit() + 1,
985 bp.documentClass().plainLayoutName());
992 void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
993 Length const & width)
995 col_type const j = cellColumn(cell);
997 column_info[j].p_width = width;
998 for (row_type i = 0; i < row_info.size(); ++i) {
999 idx_type const cell = cellIndex(i, j);
1000 // because of multicolumns
1001 toggleFixedWidth(cur, cellInset(cell).get(),
1002 !getPWidth(cell).zero());
1004 // cur paragraph can become invalid after paragraphs were merged
1005 if (cur.pit() > cur.lastpit())
1006 cur.pit() = cur.lastpit();
1007 // cur position can become invalid after newlines were removed
1008 if (cur.pos() > cur.lastpos())
1009 cur.pos() = cur.lastpos();
1013 bool Tabular::setFixedWidth(row_type r, col_type c)
1015 bool const multicol = cell_info[r][c].multicolumn != CELL_NORMAL;
1016 if ((!column_info[c].p_width.zero() && !multicol)
1017 || (multicol && !cell_info[r][c].p_width.zero())) {
1018 cell_info[r][c].inset->toggleFixedWidth(true);
1025 bool Tabular::setMColumnPWidth(Cursor & cur, idx_type cell,
1026 Length const & width)
1028 if (!isMultiColumn(cell))
1031 cellInfo(cell).p_width = width;
1032 toggleFixedWidth(cur, cellInset(cell).get(), !width.zero());
1033 // cur paragraph can become invalid after paragraphs were merged
1034 if (cur.pit() > cur.lastpit())
1035 cur.pit() = cur.lastpit();
1036 // cur position can become invalid after newlines were removed
1037 if (cur.pos() > cur.lastpos())
1038 cur.pos() = cur.lastpos();
1043 void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
1044 Tabular::Feature what)
1046 if (what == SET_SPECIAL_MULTI)
1047 cellInfo(cell).align_special = special;
1049 column_info[cellColumn(cell)].align_special = special;
1053 void Tabular::setAllLines(idx_type cell, bool line)
1055 setTopLine(cell, line);
1056 setBottomLine(cell, line);
1057 setRightLine(cell, line);
1058 setLeftLine(cell, line);
1062 void Tabular::setTopLine(idx_type i, bool line)
1064 cellInfo(i).top_line = line;
1068 void Tabular::setBottomLine(idx_type i, bool line)
1070 cellInfo(i).bottom_line = line;
1074 void Tabular::setLeftLine(idx_type cell, bool line)
1076 cellInfo(cell).left_line = line;
1080 void Tabular::setRightLine(idx_type cell, bool line)
1082 cellInfo(cell).right_line = line;
1085 bool Tabular::rowTopLine(row_type r) const
1087 idx_type i0 = getFirstCellInRow(r);
1088 idx_type i1 = getLastCellInRow(r);
1089 bool all_rows_set = true;
1090 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1091 all_rows_set = cellInfo(j).top_line;
1092 return all_rows_set;
1096 bool Tabular::rowBottomLine(row_type r) const
1098 idx_type i0 = getFirstCellInRow(r);
1099 idx_type i1 = getLastCellInRow(r);
1100 bool all_rows_set = true;
1101 for (idx_type j = i0; all_rows_set && j <= i1; ++j)
1102 all_rows_set = cellInfo(j).bottom_line;
1103 return all_rows_set;
1107 bool Tabular::columnLeftLine(col_type c) const
1114 row_type const nrows = row_info.size();
1115 for (row_type r = 0; r < nrows; ++r) {
1116 idx_type i = cellIndex(r, c);
1117 if (c == cellColumn(i)) {
1119 bool right = c > 0 && cellInfo(cellIndex(r, c - 1)).right_line;
1120 if (cellInfo(i).left_line || right)
1124 return 2 * nrows_left >= total;
1128 bool Tabular::columnRightLine(col_type c) const
1133 int nrows_right = 0;
1135 row_type const nrows = row_info.size();
1136 for (row_type r = 0; r < nrows; ++r) {
1137 idx_type i = cellIndex(r, c);
1138 if (c == cellColumn(i) + columnSpan(i) - 1) {
1140 bool left = c + 1 < column_info.size()
1141 && cellInfo(cellIndex(r, c + 1)).left_line
1142 || c + 1 == column_info.size();
1143 if (cellInfo(i).right_line && left)
1147 return 2 * nrows_right >= total;
1151 LyXAlignment Tabular::getAlignment(idx_type cell, bool onlycolumn) const
1153 if (!onlycolumn && isMultiColumn(cell))
1154 return cellInfo(cell).alignment;
1155 return column_info[cellColumn(cell)].alignment;
1160 Tabular::getVAlignment(idx_type cell, bool onlycolumn) const
1162 if (!onlycolumn && isMultiColumn(cell))
1163 return cellInfo(cell).valignment;
1164 return column_info[cellColumn(cell)].valignment;
1168 Length const Tabular::getPWidth(idx_type cell) const
1170 if (isMultiColumn(cell))
1171 return cellInfo(cell).p_width;
1172 return column_info[cellColumn(cell)].p_width;
1176 int Tabular::cellWidth(idx_type cell) const
1178 return cellInfo(cell).width;
1182 int Tabular::getBeginningOfTextInCell(idx_type cell) const
1186 switch (getAlignment(cell)) {
1187 case LYX_ALIGN_CENTER:
1188 x += (columnWidth(cell) - cellWidth(cell)) / 2;
1190 case LYX_ALIGN_RIGHT:
1191 x += columnWidth(cell) - cellWidth(cell);
1192 // + getAdditionalWidth(cell);
1195 // LYX_ALIGN_LEFT: nothing :-)
1199 // the LaTeX Way :-(
1205 bool Tabular::isFirstCellInRow(idx_type cell) const
1207 return cellColumn(cell) == 0;
1211 Tabular::idx_type Tabular::getFirstCellInRow(row_type row) const
1213 if (row > row_info.size() - 1)
1214 row = row_info.size() - 1;
1215 return cell_info[row][0].cellno;
1219 bool Tabular::isLastCellInRow(idx_type cell) const
1221 return cellRightColumn(cell) == column_info.size() - 1;
1225 Tabular::idx_type Tabular::getLastCellInRow(row_type row) const
1227 if (row > row_info.size() - 1)
1228 row = row_info.size() - 1;
1229 return cell_info[row][column_info.size() - 1].cellno;
1233 Tabular::row_type Tabular::cellRow(idx_type cell) const
1235 if (cell >= numberofcells)
1236 return row_info.size() - 1;
1239 return rowofcell[cell];
1243 Tabular::col_type Tabular::cellColumn(idx_type cell) const
1245 if (cell >= numberofcells)
1246 return column_info.size() - 1;
1249 return columnofcell[cell];
1253 Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
1255 row_type const row = cellRow(cell);
1256 col_type column = cellColumn(cell);
1257 while (column < column_info.size() - 1 &&
1258 cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
1264 void Tabular::write(ostream & os) const
1268 << write_attribute("version", 3)
1269 << write_attribute("rows", row_info.size())
1270 << write_attribute("columns", column_info.size())
1272 // global longtable options
1274 << write_attribute("rotate", rotate)
1275 << write_attribute("booktabs", use_booktabs)
1276 << write_attribute("islongtable", is_long_tabular)
1277 << write_attribute("firstHeadTopDL", endfirsthead.topDL)
1278 << write_attribute("firstHeadBottomDL", endfirsthead.bottomDL)
1279 << write_attribute("firstHeadEmpty", endfirsthead.empty)
1280 << write_attribute("headTopDL", endhead.topDL)
1281 << write_attribute("headBottomDL", endhead.bottomDL)
1282 << write_attribute("footTopDL", endfoot.topDL)
1283 << write_attribute("footBottomDL", endfoot.bottomDL)
1284 << write_attribute("lastFootTopDL", endlastfoot.topDL)
1285 << write_attribute("lastFootBottomDL", endlastfoot.bottomDL)
1286 << write_attribute("lastFootEmpty", endlastfoot.empty)
1288 for (col_type j = 0; j < column_info.size(); ++j) {
1290 << write_attribute("alignment", column_info[j].alignment)
1291 << write_attribute("valignment", column_info[j].valignment)
1292 << write_attribute("width", column_info[j].p_width.asString())
1293 << write_attribute("special", column_info[j].align_special)
1296 for (row_type i = 0; i < row_info.size(); ++i) {
1297 static const string def("default");
1299 if (row_info[i].top_space_default)
1300 os << write_attribute("topspace", def);
1302 os << write_attribute("topspace", row_info[i].top_space);
1303 if (row_info[i].bottom_space_default)
1304 os << write_attribute("bottomspace", def);
1306 os << write_attribute("bottomspace", row_info[i].bottom_space);
1307 if (row_info[i].interline_space_default)
1308 os << write_attribute("interlinespace", def);
1310 os << write_attribute("interlinespace", row_info[i].interline_space);
1311 os << write_attribute("endhead", row_info[i].endhead)
1312 << write_attribute("endfirsthead", row_info[i].endfirsthead)
1313 << write_attribute("endfoot", row_info[i].endfoot)
1314 << write_attribute("endlastfoot", row_info[i].endlastfoot)
1315 << write_attribute("newpage", row_info[i].newpage)
1316 << write_attribute("caption", row_info[i].caption)
1318 for (col_type j = 0; j < column_info.size(); ++j) {
1320 << write_attribute("multicolumn", cell_info[i][j].multicolumn)
1321 << write_attribute("alignment", cell_info[i][j].alignment)
1322 << write_attribute("valignment", cell_info[i][j].valignment)
1323 << write_attribute("topline", cell_info[i][j].top_line)
1324 << write_attribute("bottomline", cell_info[i][j].bottom_line)
1325 << write_attribute("leftline", cell_info[i][j].left_line)
1326 << write_attribute("rightline", cell_info[i][j].right_line)
1327 << write_attribute("rotate", cell_info[i][j].rotate)
1328 << write_attribute("usebox", cell_info[i][j].usebox)
1329 << write_attribute("width", cell_info[i][j].p_width)
1330 << write_attribute("special", cell_info[i][j].align_special)
1332 os << "\\begin_inset ";
1333 cell_info[i][j].inset->write(os);
1334 os << "\n\\end_inset\n"
1339 os << "</lyxtabular>\n";
1343 void Tabular::read(Lexer & lex)
1346 istream & is = lex.getStream();
1348 l_getline(is, line);
1349 if (!prefixIs(line, "<lyxtabular ") && !prefixIs(line, "<Tabular ")) {
1350 LASSERT(false, /**/);
1355 if (!getTokenValue(line, "version", version))
1357 LASSERT(version >= 2, /**/);
1360 if (!getTokenValue(line, "rows", rows_arg))
1363 if (!getTokenValue(line, "columns", columns_arg))
1365 init(buffer(), rows_arg, columns_arg);
1366 l_getline(is, line);
1367 if (!prefixIs(line, "<features")) {
1368 lyxerr << "Wrong tabular format (expected <features ...> got"
1369 << line << ')' << endl;
1372 getTokenValue(line, "rotate", rotate);
1373 getTokenValue(line, "booktabs", use_booktabs);
1374 getTokenValue(line, "islongtable", is_long_tabular);
1375 getTokenValue(line, "firstHeadTopDL", endfirsthead.topDL);
1376 getTokenValue(line, "firstHeadBottomDL", endfirsthead.bottomDL);
1377 getTokenValue(line, "firstHeadEmpty", endfirsthead.empty);
1378 getTokenValue(line, "headTopDL", endhead.topDL);
1379 getTokenValue(line, "headBottomDL", endhead.bottomDL);
1380 getTokenValue(line, "footTopDL", endfoot.topDL);
1381 getTokenValue(line, "footBottomDL", endfoot.bottomDL);
1382 getTokenValue(line, "lastFootTopDL", endlastfoot.topDL);
1383 getTokenValue(line, "lastFootBottomDL", endlastfoot.bottomDL);
1384 getTokenValue(line, "lastFootEmpty", endlastfoot.empty);
1386 for (col_type j = 0; j < column_info.size(); ++j) {
1388 if (!prefixIs(line,"<column")) {
1389 lyxerr << "Wrong tabular format (expected <column ...> got"
1390 << line << ')' << endl;
1393 getTokenValue(line, "alignment", column_info[j].alignment);
1394 getTokenValue(line, "valignment", column_info[j].valignment);
1395 getTokenValue(line, "width", column_info[j].p_width);
1396 getTokenValue(line, "special", column_info[j].align_special);
1399 for (row_type i = 0; i < row_info.size(); ++i) {
1400 l_getline(is, line);
1401 if (!prefixIs(line, "<row")) {
1402 lyxerr << "Wrong tabular format (expected <row ...> got"
1403 << line << ')' << endl;
1406 getTokenValue(line, "topspace", row_info[i].top_space,
1407 row_info[i].top_space_default);
1408 getTokenValue(line, "bottomspace", row_info[i].bottom_space,
1409 row_info[i].bottom_space_default);
1410 getTokenValue(line, "interlinespace", row_info[i].interline_space,
1411 row_info[i].interline_space_default);
1412 getTokenValue(line, "endfirsthead", row_info[i].endfirsthead);
1413 getTokenValue(line, "endhead", row_info[i].endhead);
1414 getTokenValue(line, "endfoot", row_info[i].endfoot);
1415 getTokenValue(line, "endlastfoot", row_info[i].endlastfoot);
1416 getTokenValue(line, "newpage", row_info[i].newpage);
1417 getTokenValue(line, "caption", row_info[i].caption);
1418 for (col_type j = 0; j < column_info.size(); ++j) {
1419 l_getline(is, line);
1420 if (!prefixIs(line, "<cell")) {
1421 lyxerr << "Wrong tabular format (expected <cell ...> got"
1422 << line << ')' << endl;
1425 getTokenValue(line, "multicolumn", cell_info[i][j].multicolumn);
1426 getTokenValue(line, "alignment", cell_info[i][j].alignment);
1427 setAlignment(cellIndex(i, j), cell_info[i][j].alignment,
1428 cell_info[i][j].multicolumn);
1429 getTokenValue(line, "valignment", cell_info[i][j].valignment);
1430 getTokenValue(line, "topline", cell_info[i][j].top_line);
1431 getTokenValue(line, "bottomline", cell_info[i][j].bottom_line);
1432 getTokenValue(line, "leftline", cell_info[i][j].left_line);
1433 getTokenValue(line, "rightline", cell_info[i][j].right_line);
1434 getTokenValue(line, "rotate", cell_info[i][j].rotate);
1435 getTokenValue(line, "usebox", cell_info[i][j].usebox);
1436 getTokenValue(line, "width", cell_info[i][j].p_width);
1438 getTokenValue(line, "special", cell_info[i][j].align_special);
1439 l_getline(is, line);
1440 if (prefixIs(line, "\\begin_inset")) {
1441 cell_info[i][j].inset->setBuffer(*buffer_);
1442 cell_info[i][j].inset->read(lex);
1443 l_getline(is, line);
1445 if (!prefixIs(line, "</cell>")) {
1446 lyxerr << "Wrong tabular format (expected </cell> got"
1447 << line << ')' << endl;
1451 l_getline(is, line);
1452 if (!prefixIs(line, "</row>")) {
1453 lyxerr << "Wrong tabular format (expected </row> got"
1454 << line << ')' << endl;
1458 while (!prefixIs(line, "</lyxtabular>")) {
1459 l_getline(is, line);
1465 bool Tabular::isMultiColumn(idx_type cell) const
1467 return cellInfo(cell).multicolumn != CELL_NORMAL;
1471 bool Tabular::isMultiColumnReal(idx_type cell) const
1473 return cellColumn(cell) != cellRightColumn(cell) &&
1474 cellInfo(cell).multicolumn != CELL_NORMAL;
1478 Tabular::CellData & Tabular::cellInfo(idx_type cell) const
1480 return cell_info[cellRow(cell)][cellColumn(cell)];
1484 void Tabular::setMultiColumn(idx_type cell, idx_type number)
1486 CellData & cs = cellInfo(cell);
1487 cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
1488 cs.alignment = column_info[cellColumn(cell)].alignment;
1489 setRightLine(cell, rightLine(cell + number - 1));
1490 for (idx_type i = 1; i < number; ++i) {
1491 CellData & cs1 = cellInfo(cell + i);
1492 cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
1493 cs.inset->appendParagraphs(cs1.inset->paragraphs());
1500 Tabular::idx_type Tabular::columnSpan(idx_type cell) const
1502 row_type const row = cellRow(cell);
1503 col_type const ncols = column_info.size();
1504 idx_type result = 1;
1505 col_type column = cellColumn(cell) + 1;
1506 while (column < ncols && isPartOfMultiColumn(row, column)) {
1514 Tabular::idx_type Tabular::unsetMultiColumn(idx_type cell)
1516 row_type const row = cellRow(cell);
1517 col_type column = cellColumn(cell);
1519 idx_type result = 0;
1521 if (cell_info[row][column].multicolumn == CELL_BEGIN_OF_MULTICOLUMN) {
1522 cell_info[row][column].multicolumn = CELL_NORMAL;
1524 while (column < column_info.size() &&
1525 cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN)
1527 cell_info[row][column].multicolumn = CELL_NORMAL;
1537 void Tabular::setRotateCell(idx_type cell, bool flag)
1539 cellInfo(cell).rotate = flag;
1543 bool Tabular::getRotateCell(idx_type cell) const
1545 return cellInfo(cell).rotate;
1549 bool Tabular::needRotating() const
1553 for (row_type i = 0; i < row_info.size(); ++i)
1554 for (col_type j = 0; j < column_info.size(); ++j)
1555 if (cell_info[i][j].rotate)
1561 bool Tabular::isLastCell(idx_type cell) const
1563 if (cell + 1 < numberofcells)
1569 Tabular::idx_type Tabular::cellAbove(idx_type cell) const
1571 if (cellRow(cell) > 0)
1572 return cell_info[cellRow(cell)-1][cellColumn(cell)].cellno;
1577 Tabular::idx_type Tabular::cellBelow(idx_type cell) const
1579 if (cellRow(cell) + 1 < row_info.size())
1580 return cell_info[cellRow(cell)+1][cellColumn(cell)].cellno;
1585 Tabular::idx_type Tabular::cellIndex(row_type row,
1586 col_type column) const
1588 BOOST_ASSERT(column != npos && column < column_info.size()
1589 && row != npos && row < row_info.size());
1590 return cell_info[row][column].cellno;
1594 void Tabular::setUsebox(idx_type cell, BoxType type)
1596 cellInfo(cell).usebox = type;
1600 // FIXME: Remove this routine because we cannot insert \parboxes when the user
1601 // adds line breaks, see bug 4886.
1602 Tabular::BoxType Tabular::getUsebox(idx_type cell) const
1604 if ((!column_info[cellColumn(cell)].p_width.zero() && !isMultiColumn(cell)) ||
1605 (isMultiColumn(cell) && !cellInfo(cell).p_width.zero()))
1607 if (cellInfo(cell).usebox > 1)
1608 return cellInfo(cell).usebox;
1609 return useParbox(cell);
1614 // This are functions used for the longtable support
1616 void Tabular::setLTHead(row_type row, bool flag, ltType const & hd,
1622 row_info[row].endfirsthead = flag;
1626 row_info[row].endhead = flag;
1631 bool Tabular::getRowOfLTHead(row_type row, ltType & hd) const
1634 hd.set = haveLTHead();
1635 return row_info[row].endhead;
1639 bool Tabular::getRowOfLTFirstHead(row_type row, ltType & hd) const
1642 hd.set = haveLTFirstHead();
1643 return row_info[row].endfirsthead;
1647 void Tabular::setLTFoot(row_type row, bool flag, ltType const & fd,
1653 row_info[row].endlastfoot = flag;
1657 row_info[row].endfoot = flag;
1662 bool Tabular::getRowOfLTFoot(row_type row, ltType & fd) const
1665 fd.set = haveLTFoot();
1666 return row_info[row].endfoot;
1670 bool Tabular::getRowOfLTLastFoot(row_type row, ltType & fd) const
1673 fd.set = haveLTLastFoot();
1674 return row_info[row].endlastfoot;
1678 void Tabular::setLTNewPage(row_type row, bool what)
1680 row_info[row].newpage = what;
1684 bool Tabular::getLTNewPage(row_type row) const
1686 return row_info[row].newpage;
1690 bool Tabular::haveLTHead() const
1692 for (row_type i = 0; i < row_info.size(); ++i)
1693 if (row_info[i].endhead)
1699 bool Tabular::haveLTFirstHead() const
1701 if (endfirsthead.empty)
1703 for (row_type i = 0; i < row_info.size(); ++i)
1704 if (row_info[i].endfirsthead)
1710 bool Tabular::haveLTFoot() const
1712 for (row_type i = 0; i < row_info.size(); ++i)
1713 if (row_info[i].endfoot)
1719 bool Tabular::haveLTLastFoot() const
1721 if (endlastfoot.empty)
1723 for (row_type i = 0; i < row_info.size(); ++i)
1724 if (row_info[i].endlastfoot)
1730 Tabular::idx_type Tabular::setLTCaption(row_type row, bool what)
1732 idx_type i = getFirstCellInRow(row);
1734 setMultiColumn(i, column_info.size());
1735 setTopLine(i, false);
1736 setBottomLine(i, false);
1737 setLeftLine(i, false);
1738 setRightLine(i, false);
1740 unsetMultiColumn(i);
1741 row_info[row].caption = what;
1746 bool Tabular::ltCaption(row_type row) const
1748 return row_info[row].caption;
1752 // end longtable support functions
1754 void Tabular::setRowAscent(row_type row, int height)
1756 if (row >= row_info.size() || row_info[row].ascent == height)
1758 row_info[row].ascent = height;
1762 void Tabular::setRowDescent(row_type row, int height)
1764 if (row >= row_info.size() || row_info[row].descent == height)
1766 row_info[row].descent = height;
1770 int Tabular::rowAscent(row_type row) const
1772 if (row >= row_info.size())
1774 return row_info[row].ascent;
1778 int Tabular::rowDescent(row_type row) const
1780 LASSERT(row < row_info.size(), /**/);
1781 return row_info[row].descent;
1785 int Tabular::height() const
1788 for (row_type row = 0; row < row_info.size(); ++row)
1789 height += rowAscent(row) + rowDescent(row) +
1790 getAdditionalHeight(row);
1795 bool Tabular::isPartOfMultiColumn(row_type row, col_type column) const
1797 LASSERT(row < row_info.size(), /**/);
1798 LASSERT(column < column_info.size(), /**/);
1799 return cell_info[row][column].multicolumn == CELL_PART_OF_MULTICOLUMN;
1803 int Tabular::TeXTopHLine(odocstream & os, row_type row) const
1805 // FIXME: assert or return 0 as in TeXBottomHLine()?
1806 LASSERT(row != npos, /**/);
1807 LASSERT(row < row_info.size(), /**/);
1809 idx_type const fcell = getFirstCellInRow(row);
1810 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1813 for (idx_type i = fcell; i < n; ++i) {
1817 if (use_booktabs && row == 0) {
1820 } else if (tmp == n - fcell) {
1821 os << (use_booktabs ? "\\midrule " : "\\hline ");
1823 for (idx_type i = fcell; i < n; ++i) {
1825 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1826 << cellColumn(i) + 1
1828 << cellRightColumn(i) + 1
1840 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1842 // FIXME: return 0 or assert as in TeXTopHLine()?
1843 if (row == npos || row >= row_info.size())
1846 idx_type const fcell = getFirstCellInRow(row);
1847 idx_type const n = numberOfCellsInRow(fcell) + fcell;
1850 for (idx_type i = fcell; i < n; ++i) {
1854 if (use_booktabs && row == row_info.size() - 1) {
1855 if (bottomLine(fcell))
1856 os << "\\bottomrule";
1857 } else if (tmp == n - fcell) {
1858 os << (use_booktabs ? "\\midrule" : "\\hline");
1860 for (idx_type i = fcell; i < n; ++i) {
1861 if (bottomLine(i)) {
1862 os << (use_booktabs ? "\\cmidrule{" : "\\cline{")
1863 << cellColumn(i) + 1
1865 << cellRightColumn(i) + 1
1877 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1880 row_type const r = cellRow(cell);
1881 if (is_long_tabular && row_info[r].caption)
1884 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1885 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1886 // figure out how to set the lines
1887 // we always set double lines to the right of the cell
1888 col_type const c = cellColumn(cell);
1889 col_type const nextcol = c + columnSpan(cell);
1890 bool colright = columnRightLine(c);
1891 bool colleft = columnLeftLine(c);
1892 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1893 bool nextcellleft = nextcol < column_info.size()
1894 && leftLine(cellIndex(r, nextcol));
1895 bool coldouble = colright && nextcolleft;
1896 bool celldouble = rightLine(cell) && nextcellleft;
1897 ismulticol = isMultiColumn(cell)
1898 || (c == 0 && colleft != leftLine(cell))
1899 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1900 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1901 || (coldouble != celldouble);
1903 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1904 if (c ==0 && leftLine(cell))
1906 if (!cellInfo(cell).align_special.empty()) {
1907 os << cellInfo(cell).align_special;
1909 if (!getPWidth(cell).zero()) {
1911 case LYX_ALIGN_LEFT:
1912 os << ">{\\raggedright}";
1914 case LYX_ALIGN_RIGHT:
1915 os << ">{\\raggedleft}";
1917 case LYX_ALIGN_CENTER:
1918 os << ">{\\centering}";
1924 case LYX_VALIGN_TOP:
1927 case LYX_VALIGN_MIDDLE:
1930 case LYX_VALIGN_BOTTOM:
1935 << from_ascii(getPWidth(cell).asLatexString())
1939 case LYX_ALIGN_LEFT:
1942 case LYX_ALIGN_RIGHT:
1949 } // end if else !getPWidth
1950 } // end if else !cellinfo_of_cell
1951 if (rightLine(cell) || nextcellleft)
1954 // add extra vertical line if we want a double one
1958 if (getRotateCell(cell)) {
1959 os << "\\begin{sideways}\n";
1962 if (getUsebox(cell) == BOX_PARBOX) {
1965 case LYX_VALIGN_TOP:
1968 case LYX_VALIGN_MIDDLE:
1971 case LYX_VALIGN_BOTTOM:
1975 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1977 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1978 os << "\\begin{minipage}[";
1980 case LYX_VALIGN_TOP:
1983 case LYX_VALIGN_MIDDLE:
1986 case LYX_VALIGN_BOTTOM:
1990 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1998 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2001 row_type const r = cellRow(cell);
2002 if (is_long_tabular && row_info[r].caption)
2006 if (getUsebox(cell) == BOX_PARBOX)
2008 else if (getUsebox(cell) == BOX_MINIPAGE) {
2009 os << "%\n\\end{minipage}";
2012 if (getRotateCell(cell)) {
2013 os << "%\n\\end{sideways}";
2023 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2024 OutputParams const & runparams) const
2026 if (!is_long_tabular)
2030 // output header info
2032 if (endhead.topDL) {
2036 for (row_type i = 0; i < row_info.size(); ++i) {
2037 if (row_info[i].endhead) {
2038 ret += TeXRow(os, i, runparams);
2041 if (endhead.bottomDL) {
2045 os << "\\endhead\n";
2047 if (endfirsthead.empty) {
2048 os << "\\endfirsthead\n";
2052 // output firstheader info
2053 if (haveLTFirstHead()) {
2054 if (endfirsthead.topDL) {
2058 for (row_type i = 0; i < row_info.size(); ++i) {
2059 if (row_info[i].endfirsthead) {
2060 ret += TeXRow(os, i, runparams);
2063 if (endfirsthead.bottomDL) {
2067 os << "\\endfirsthead\n";
2070 // output footer info
2072 if (endfoot.topDL) {
2076 for (row_type i = 0; i < row_info.size(); ++i) {
2077 if (row_info[i].endfoot) {
2078 ret += TeXRow(os, i, runparams);
2081 if (endfoot.bottomDL) {
2085 os << "\\endfoot\n";
2087 if (endlastfoot.empty) {
2088 os << "\\endlastfoot\n";
2092 // output lastfooter info
2093 if (haveLTLastFoot()) {
2094 if (endlastfoot.topDL) {
2098 for (row_type i = 0; i < row_info.size(); ++i) {
2099 if (row_info[i].endlastfoot) {
2100 ret += TeXRow(os, i, runparams);
2103 if (endlastfoot.bottomDL) {
2107 os << "\\endlastfoot\n";
2114 bool Tabular::isValidRow(row_type row) const
2116 if (!is_long_tabular)
2118 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2119 !row_info[row].endfoot && !row_info[row].endlastfoot;
2123 int Tabular::TeXRow(odocstream & os, row_type i,
2124 OutputParams const & runparams) const
2126 idx_type cell = cellIndex(i, 0);
2127 int ret = TeXTopHLine(os, i);
2128 if (row_info[i].top_space_default) {
2130 os << "\\addlinespace\n";
2132 os << "\\noalign{\\vskip\\doublerulesep}\n";
2134 } else if(!row_info[i].top_space.zero()) {
2136 os << "\\addlinespace["
2137 << from_ascii(row_info[i].top_space.asLatexString())
2140 os << "\\noalign{\\vskip"
2141 << from_ascii(row_info[i].top_space.asLatexString())
2146 bool ismulticol = false;
2147 for (col_type j = 0; j < column_info.size(); ++j) {
2148 if (isPartOfMultiColumn(i, j))
2150 ret += TeXCellPreamble(os, cell, ismulticol);
2151 shared_ptr<InsetTableCell> inset = cellInset(cell);
2153 Paragraph const & par = inset->paragraphs().front();
2154 bool rtl = par.isRTL(buffer().params())
2156 && getPWidth(cell).zero();
2160 par.getParLanguage(buffer().params())->lang();
2161 if (lang == "farsi")
2163 else if (lang == "arabic_arabi")
2165 // currently, remaning RTL languages are
2166 // arabic_arabtex and hebrew
2170 // pass to the OutputParams that we are in a cell and
2171 // which alignment we have set.
2172 // InsetNewline needs this context information.
2173 OutputParams newrp = runparams;
2174 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2175 ? OutputParams::PLAIN
2176 : OutputParams::ALIGNED;
2177 ret += inset->latex(os, newrp);
2181 ret += TeXCellPostamble(os, cell, ismulticol);
2182 if (!isLastCellInRow(cell)) { // not last cell in row
2187 os << "\\tabularnewline";
2188 if (row_info[i].bottom_space_default) {
2190 os << "\\addlinespace";
2192 os << "[\\doublerulesep]";
2193 } else if (!row_info[i].bottom_space.zero()) {
2195 os << "\\addlinespace";
2197 << from_ascii(row_info[i].bottom_space.asLatexString())
2202 ret += TeXBottomHLine(os, i);
2203 if (row_info[i].interline_space_default) {
2205 os << "\\addlinespace\n";
2207 os << "\\noalign{\\vskip\\doublerulesep}\n";
2209 } else if (!row_info[i].interline_space.zero()) {
2211 os << "\\addlinespace["
2212 << from_ascii(row_info[i].interline_space.asLatexString())
2215 os << "\\noalign{\\vskip"
2216 << from_ascii(row_info[i].interline_space.asLatexString())
2224 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2228 //+---------------------------------------------------------------------
2229 //+ first the opening preamble +
2230 //+---------------------------------------------------------------------
2233 os << "\\begin{sideways}\n";
2236 if (is_long_tabular)
2237 os << "\\begin{longtable}{";
2239 os << "\\begin{tabular}{";
2241 for (col_type i = 0; i < column_info.size(); ++i) {
2242 if (columnLeftLine(i))
2244 if (!column_info[i].align_special.empty()) {
2245 os << column_info[i].align_special;
2247 if (!column_info[i].p_width.zero()) {
2248 switch (column_info[i].alignment) {
2249 case LYX_ALIGN_LEFT:
2250 os << ">{\\raggedright}";
2252 case LYX_ALIGN_RIGHT:
2253 os << ">{\\raggedleft}";
2255 case LYX_ALIGN_CENTER:
2256 os << ">{\\centering}";
2258 case LYX_ALIGN_NONE:
2259 case LYX_ALIGN_BLOCK:
2260 case LYX_ALIGN_LAYOUT:
2261 case LYX_ALIGN_SPECIAL:
2265 switch (column_info[i].valignment) {
2266 case LYX_VALIGN_TOP:
2269 case LYX_VALIGN_MIDDLE:
2272 case LYX_VALIGN_BOTTOM:
2277 << from_ascii(column_info[i].p_width.asLatexString())
2280 switch (column_info[i].alignment) {
2281 case LYX_ALIGN_LEFT:
2284 case LYX_ALIGN_RIGHT:
2291 } // end if else !column_info[i].p_width
2292 } // end if else !column_info[i].align_special
2293 if (columnRightLine(i))
2299 ret += TeXLongtableHeaderFooter(os, runparams);
2301 //+---------------------------------------------------------------------
2302 //+ the single row and columns (cells) +
2303 //+---------------------------------------------------------------------
2305 for (row_type i = 0; i < row_info.size(); ++i) {
2306 if (isValidRow(i)) {
2307 ret += TeXRow(os, i, runparams);
2308 if (is_long_tabular && row_info[i].newpage) {
2309 os << "\\newpage\n";
2315 //+---------------------------------------------------------------------
2316 //+ the closing of the tabular +
2317 //+---------------------------------------------------------------------
2319 if (is_long_tabular)
2320 os << "\\end{longtable}";
2322 os << "\\end{tabular}";
2324 os << "\n\\end{sideways}";
2332 int Tabular::docbookRow(odocstream & os, row_type row,
2333 OutputParams const & runparams) const
2336 idx_type cell = getFirstCellInRow(row);
2339 for (col_type j = 0; j < column_info.size(); ++j) {
2340 if (isPartOfMultiColumn(row, j))
2343 os << "<entry align=\"";
2344 switch (getAlignment(cell)) {
2345 case LYX_ALIGN_LEFT:
2348 case LYX_ALIGN_RIGHT:
2356 os << "\" valign=\"";
2357 switch (getVAlignment(cell)) {
2358 case LYX_VALIGN_TOP:
2361 case LYX_VALIGN_BOTTOM:
2364 case LYX_VALIGN_MIDDLE:
2369 if (isMultiColumn(cell)) {
2370 os << " namest=\"col" << j << "\" ";
2371 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2375 ret += cellInset(cell)->docbook(os, runparams);
2384 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2388 //+---------------------------------------------------------------------
2389 //+ first the opening preamble +
2390 //+---------------------------------------------------------------------
2392 os << "<tgroup cols=\"" << column_info.size()
2393 << "\" colsep=\"1\" rowsep=\"1\">\n";
2395 for (col_type i = 0; i < column_info.size(); ++i) {
2396 os << "<colspec colname=\"col" << i << "\" align=\"";
2397 switch (column_info[i].alignment) {
2398 case LYX_ALIGN_LEFT:
2401 case LYX_ALIGN_RIGHT:
2409 if (runparams.flavor == OutputParams::XML)
2415 //+---------------------------------------------------------------------
2416 //+ Long Tabular case +
2417 //+---------------------------------------------------------------------
2419 // output header info
2420 if (haveLTHead() || haveLTFirstHead()) {
2423 for (row_type i = 0; i < row_info.size(); ++i) {
2424 if (row_info[i].endhead || row_info[i].endfirsthead) {
2425 ret += docbookRow(os, i, runparams);
2431 // output footer info
2432 if (haveLTFoot() || haveLTLastFoot()) {
2435 for (row_type i = 0; i < row_info.size(); ++i) {
2436 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2437 ret += docbookRow(os, i, runparams);
2444 //+---------------------------------------------------------------------
2445 //+ the single row and columns (cells) +
2446 //+---------------------------------------------------------------------
2450 for (row_type i = 0; i < row_info.size(); ++i) {
2451 if (isValidRow(i)) {
2452 ret += docbookRow(os, i, runparams);
2457 //+---------------------------------------------------------------------
2458 //+ the closing of the tabular +
2459 //+---------------------------------------------------------------------
2468 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2469 vector<unsigned int> const & clen) const
2471 idx_type const fcell = getFirstCellInRow(row);
2472 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2475 for (idx_type i = fcell; i < n; ++i) {
2485 for (idx_type i = fcell; i < n; ++i) {
2496 col_type column = cellColumn(i);
2497 int len = clen[column];
2498 while (column < column_info.size() - 1
2499 && isPartOfMultiColumn(row, ++column))
2500 len += clen[column] + 4;
2501 os << docstring(len, ch);
2516 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2517 vector<unsigned int> const & clen) const
2519 idx_type const fcell = getFirstCellInRow(row);
2520 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2523 for (idx_type i = fcell; i < n; ++i) {
2524 if (bottomLine(i)) {
2533 for (idx_type i = fcell; i < n; ++i) {
2534 if (bottomLine(i)) {
2544 col_type column = cellColumn(i);
2545 int len = clen[column];
2546 while (column < column_info.size() -1
2547 && isPartOfMultiColumn(row, ++column))
2548 len += clen[column] + 4;
2549 os << docstring(len, ch);
2550 if (bottomLine(i)) {
2564 void Tabular::plaintextPrintCell(odocstream & os,
2565 OutputParams const & runparams,
2566 idx_type cell, row_type row, col_type column,
2567 vector<unsigned int> const & clen,
2568 bool onlydata) const
2570 odocstringstream sstr;
2571 cellInset(cell)->plaintext(sstr, runparams);
2583 unsigned int len1 = sstr.str().length();
2584 unsigned int len2 = clen[column];
2585 while (column < column_info.size() -1
2586 && isPartOfMultiColumn(row, ++column))
2587 len2 += clen[column] + 4;
2590 switch (getAlignment(cell)) {
2592 case LYX_ALIGN_LEFT:
2595 case LYX_ALIGN_RIGHT:
2599 case LYX_ALIGN_CENTER:
2605 os << docstring(len1, ' ') << sstr.str()
2606 << docstring(len2, ' ');
2608 if (rightLine(cell))
2615 void Tabular::plaintext(odocstream & os,
2616 OutputParams const & runparams, int const depth,
2617 bool onlydata, char_type delim) const
2619 // first calculate the width of the single columns
2620 vector<unsigned int> clen(column_info.size());
2623 // first all non (real) multicolumn cells!
2624 for (col_type j = 0; j < column_info.size(); ++j) {
2626 for (row_type i = 0; i < row_info.size(); ++i) {
2627 idx_type cell = cellIndex(i, j);
2628 if (isMultiColumnReal(cell))
2630 odocstringstream sstr;
2631 cellInset(cell)->plaintext(sstr, runparams);
2632 if (clen[j] < sstr.str().length())
2633 clen[j] = sstr.str().length();
2636 // then all (real) multicolumn cells!
2637 for (col_type j = 0; j < column_info.size(); ++j) {
2638 for (row_type i = 0; i < row_info.size(); ++i) {
2639 idx_type cell = cellIndex(i, j);
2640 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2642 odocstringstream sstr;
2643 cellInset(cell)->plaintext(sstr, runparams);
2644 int len = int(sstr.str().length());
2645 idx_type const n = columnSpan(cell);
2646 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2648 if (len > int(clen[j + n - 1]))
2649 clen[j + n - 1] = len;
2654 for (row_type i = 0; i < row_info.size(); ++i) {
2655 if (!onlydata && plaintextTopHLine(os, i, clen))
2656 os << docstring(depth * 2, ' ');
2657 for (col_type j = 0; j < column_info.size(); ++j) {
2658 if (isPartOfMultiColumn(i, j))
2660 if (onlydata && j > 0)
2661 // we don't use operator<< for single UCS4 character.
2662 // see explanation in docstream.h
2664 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2669 os << docstring(depth * 2, ' ');
2670 if (plaintextBottomHLine(os, i, clen))
2671 os << docstring(depth * 2, ' ');
2677 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2679 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2683 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2684 col_type column) const
2686 return cell_info[row][column].inset;
2690 void Tabular::setCellInset(row_type row, col_type column,
2691 shared_ptr<InsetTableCell> ins) const
2693 CellData & cd = cell_info[row][column];
2698 void Tabular::validate(LaTeXFeatures & features) const
2700 features.require("NeedTabularnewline");
2702 features.require("booktabs");
2703 if (is_long_tabular)
2704 features.require("longtable");
2706 features.require("rotating");
2707 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2708 if (getVAlignment(cell) != LYX_VALIGN_TOP
2709 || !getPWidth(cell).zero())
2710 features.require("array");
2711 cellInset(cell)->validate(features);
2716 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2718 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2719 ParagraphList::const_iterator cit = parlist.begin();
2720 ParagraphList::const_iterator end = parlist.end();
2722 for (; cit != end; ++cit)
2723 for (int i = 0; i < cit->size(); ++i)
2724 if (cit->isNewline(i))
2731 /////////////////////////////////////////////////////////////////////
2735 /////////////////////////////////////////////////////////////////////
2737 InsetTableCell::InsetTableCell(Buffer & buf)
2738 : InsetText(buf), isFixedWidth(false),
2739 contentAlign(LYX_ALIGN_CENTER)
2743 bool InsetTableCell::forcePlainLayout(idx_type) const
2745 return !isFixedWidth;
2749 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2751 return isFixedWidth;
2755 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2756 FuncStatus & status) const
2759 switch (cmd.action) {
2761 enabled = !forcePlainLayout();
2763 case LFUN_LAYOUT_PARAGRAPH:
2764 enabled = allowParagraphCustomization();
2767 return InsetText::getStatus(cur, cmd, status);
2769 status.setEnabled(enabled);
2773 docstring InsetTableCell::asString(bool intoInsets)
2776 if (paragraphs().empty())
2778 ParagraphList::const_iterator it = paragraphs().begin();
2779 ParagraphList::const_iterator en = paragraphs().end();
2781 for (; it != en; ++it) {
2786 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2793 /////////////////////////////////////////////////////////////////////
2797 /////////////////////////////////////////////////////////////////////
2799 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2801 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2802 rowselect_(false), colselect_(false)
2804 setBuffer(buf); // FIXME: remove later
2808 InsetTabular::InsetTabular(InsetTabular const & tab)
2809 : Inset(tab), tabular(tab.tabular), scx_(0)
2814 InsetTabular::~InsetTabular()
2816 hideDialogs("tabular", this);
2820 void InsetTabular::setBuffer(Buffer & buf)
2822 tabular.setBuffer(buf);
2823 Inset::setBuffer(buf);
2827 bool InsetTabular::insetAllowed(InsetCode code) const
2829 if (code == MATHMACRO_CODE)
2836 void InsetTabular::write(ostream & os) const
2838 os << "Tabular" << endl;
2843 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2845 // FIXME: depending on the selection state, we could offer a different menu.
2846 return from_ascii("context-tabular");
2850 void InsetTabular::read(Lexer & lex)
2852 //bool const old_format = (lex.getString() == "\\LyXTable");
2860 string token = lex.getString();
2861 while (lex && token != "\\end_inset") {
2863 token = lex.getString();
2866 lex.printError("Missing \\end_inset at this point. ");
2870 int InsetTabular::rowFromY(Cursor & cur, int y) const
2872 // top y coordinate of tabular
2873 int h = yo(cur.bv()) - tabular.rowAscent(0);
2874 size_t nrows = tabular.row_info.size();
2876 for (; r < nrows && y > h; ++r) {
2877 h += tabular.rowAscent(r);
2878 h += tabular.rowDescent(r);
2879 h += tabular.getAdditionalHeight(r);
2885 int InsetTabular::columnFromX(Cursor & cur, int x) const
2887 // left x coordinate of tabular
2888 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2889 size_t ncols = tabular.column_info.size();
2891 for (; c < ncols && x > w; ++c)
2892 w += tabular.columnWidth(c);
2897 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2899 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2900 // mi.base.textwidth << "\n";
2903 LASSERT(false, /**/);
2907 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2910 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2911 if (tabular.isPartOfMultiColumn(i, j))
2912 // Multicolumn cell, but not first one
2917 if (tabular.cell_info[i][j].multicolumn ==
2918 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2919 p_width = tabular.cellInfo(cell).p_width;
2921 p_width = tabular.column_info[j].p_width;
2922 if (!p_width.zero())
2923 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2924 tabular.cellInset(cell)->metrics(m, dim);
2925 if (!p_width.zero())
2926 dim.wid = m.base.textwidth;
2927 tabular.setCellWidth(cell, dim.wid);
2928 maxAsc = max(maxAsc, dim.asc);
2929 maxDesc = max(maxDesc, dim.des);
2932 int const top_space = tabular.row_info[i].top_space_default ?
2933 default_line_space :
2934 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2935 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2936 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2937 default_line_space :
2938 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2939 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2941 tabular.updateColumnWidths();
2942 dim.asc = tabular.rowAscent(0);
2943 dim.des = tabular.height() - dim.asc;
2944 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2948 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2950 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2951 BufferView * bv = pi.base.bv;
2953 // FIXME: As the full backrgound is painted in drawSelection(),
2954 // we have no choice but to do a full repaint for the Text cells.
2955 pi.full_repaint = true;
2957 resetPos(bv->cursor());
2960 x += ADD_TO_TABULAR_WIDTH;
2962 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2965 first_visible_cell = Tabular::npos;
2966 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2968 int const a = tabular.rowAscent(i);
2969 int const d = tabular.rowDescent(i);
2970 idx = tabular.cellIndex(i, 0);
2971 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2972 if (tabular.isPartOfMultiColumn(i, j))
2974 if (first_visible_cell == Tabular::npos)
2975 first_visible_cell = idx;
2977 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2978 // Cache the Inset position.
2979 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2980 if (nx + tabular.columnWidth(idx) < 0
2981 || nx > bv->workWidth()
2983 || y - a > bv->workHeight()) {
2984 pi.pain.setDrawingEnabled(false);
2985 cell(idx)->draw(pi, cx, y);
2986 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2987 pi.pain.setDrawingEnabled(original_drawing_state);
2989 cell(idx)->draw(pi, cx, y);
2990 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2992 nx += tabular.columnWidth(idx);
2996 if (i + 1 < tabular.row_info.size())
2997 y += d + tabular.rowAscent(i + 1) +
2998 tabular.getAdditionalHeight(i + 1);
3003 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3005 Cursor & cur = pi.base.bv->cursor();
3007 x += scx_ + ADD_TO_TABULAR_WIDTH;
3009 // FIXME: it is wrong to completely paint the background
3010 // if we want to do single row painting.
3012 // Paint background of current tabular
3013 int const w = tabular.width();
3014 int const h = tabular.height();
3015 int yy = y - tabular.rowAscent(0);
3016 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3018 if (!cur.selection())
3020 if (&cur.inset() != this)
3026 if (cur.selIsMultiCell()) {
3029 getSelection(cur, rs, re, cs, ce);
3030 y -= tabular.rowAscent(0);
3031 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3032 int const a = tabular.rowAscent(j);
3033 int const h = a + tabular.rowDescent(j);
3035 y += tabular.getAdditionalHeight(j);
3036 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3037 if (tabular.isPartOfMultiColumn(j, i))
3039 idx_type const cell =
3040 tabular.cellIndex(j, i);
3041 int const w = tabular.columnWidth(cell);
3042 if (i >= cs && i <= ce && j >= rs && j <= re)
3043 pi.pain.fillRectangle(xx, y, w, h,
3051 x += cellXPos(cur.idx());
3052 x += tabular.getBeginningOfTextInCell(cur.idx());
3053 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3058 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3059 row_type row, idx_type cell, bool erased) const
3061 int x2 = x + tabular.columnWidth(cell);
3062 bool on_off = false;
3063 ColorCode col = Color_tabularline;
3064 ColorCode onoffcol = Color_tabularonoffline;
3067 col = Color_deletedtext;
3068 onoffcol = Color_deletedtext;
3071 if (!tabular.topAlreadyDrawn(cell)) {
3072 on_off = !tabular.topLine(cell);
3073 pain.line(x, y - tabular.rowAscent(row),
3074 x2, y - tabular.rowAscent(row),
3075 on_off ? onoffcol : col,
3076 on_off ? Painter::line_onoffdash : Painter::line_solid);
3078 on_off = !tabular.bottomLine(cell);
3079 pain.line(x, y + tabular.rowDescent(row),
3080 x2, y + tabular.rowDescent(row),
3081 on_off ? onoffcol : col,
3082 on_off ? Painter::line_onoffdash : Painter::line_solid);
3083 if (!tabular.leftAlreadyDrawn(cell)) {
3084 on_off = !tabular.leftLine(cell);
3085 pain.line(x, y - tabular.rowAscent(row),
3086 x, y + tabular.rowDescent(row),
3087 on_off ? onoffcol : col,
3088 on_off ? Painter::line_onoffdash : Painter::line_solid);
3090 on_off = !tabular.rightLine(cell);
3091 pain.line(x2 - tabular.getAdditionalWidth(cell),
3092 y - tabular.rowAscent(row),
3093 x2 - tabular.getAdditionalWidth(cell),
3094 y + tabular.rowDescent(row),
3095 on_off ? onoffcol : col,
3096 on_off ? Painter::line_onoffdash : Painter::line_solid);
3100 docstring InsetTabular::editMessage() const
3102 return _("Opened table");
3106 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3108 //lyxerr << "InsetTabular::edit: " << this << endl;
3110 cur.selection() = false;
3113 if (isRightToLeft(cur))
3114 cur.idx() = tabular.getLastCellInRow(0);
3120 if (isRightToLeft(cur))
3121 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3123 cur.idx() = tabular.numberofcells - 1;
3125 cur.pos() = cur.lastpos(); // FIXME crude guess
3127 // FIXME: this accesses the position cache before it is initialized
3129 //cur.bv().fitCursor();
3133 void InsetTabular::updateLabels(ParIterator const & it)
3135 // In a longtable, tell captions what the current float is
3136 Counters & cnts = buffer().params().documentClass().counters();
3137 string const saveflt = cnts.current_float();
3138 if (tabular.is_long_tabular)
3139 cnts.current_float("table");
3141 ParIterator it2 = it;
3143 size_t const end = it2.nargs();
3144 for ( ; it2.idx() < end; it2.top().forwardIdx())
3145 lyx::updateLabels(buffer(), it2);
3148 if (tabular.is_long_tabular)
3149 cnts.current_float(saveflt);
3153 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3155 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3156 << "\n cur:" << cur);
3157 CursorSlice sl = cur.top();
3158 Cursor & bvcur = cur.bv().cursor();
3160 switch (cmd.action) {
3162 case LFUN_MOUSE_PRESS: {
3163 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3165 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3166 || cmd.x > xo(cur.bv()) + tabular.width()) {
3167 row_type r = rowFromY(cur, cmd.y);
3168 cur.idx() = tabular.getFirstCellInRow(r);
3171 cur.idx() = tabular.getLastCellInRow(r);
3172 cur.pos() = cur.lastpos();
3173 cur.selection() = true;
3179 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3180 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3181 || cmd.y > y0 + tabular.height()) {
3182 col_type c = columnFromX(cur, cmd.x);
3183 cur.idx() = tabular.cellIndex(0, c);
3186 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3187 cur.pos() = cur.lastpos();
3188 cur.selection() = true;
3193 // do not reset cursor/selection if we have selected
3194 // some cells (bug 2715).
3195 if (cmd.button() == mouse_button::button3
3196 && &bvcur.selBegin().inset() == this
3197 && bvcur.selIsMultiCell())
3200 // Let InsetTableCell do it
3201 cell(cur.idx())->dispatch(cur, cmd);
3204 case LFUN_MOUSE_MOTION:
3205 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3206 if (cmd.button() == mouse_button::button1) {
3207 // only accept motions to places not deeper nested than the real anchor
3208 if (!bvcur.anchor_.hasPart(cur)) {
3212 // select (additional) row
3214 row_type r = rowFromY(cur, cmd.y);
3215 cur.idx() = tabular.getLastCellInRow(r);
3216 // we need to reset the cursor's pit and pos now, as the old ones
3217 // may no longer be valid.
3220 bvcur.setCursor(cur);
3221 bvcur.selection() = true;
3224 // select (additional) column
3226 col_type c = columnFromX(cur, cmd.x);
3227 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3228 // we need to reset the cursor's pit and pos now, as the old ones
3229 // may no longer be valid.
3232 bvcur.setCursor(cur);
3233 bvcur.selection() = true;
3236 // only update if selection changes
3237 if (bvcur.idx() == cur.idx() &&
3238 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3240 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3241 bvcur.setCursor(cur);
3242 bvcur.selection() = true;
3243 // if this is a multicell selection, we just set the cursor to
3244 // the beginning of the cell's text.
3245 if (bvcur.selIsMultiCell()) {
3252 case LFUN_MOUSE_RELEASE:
3257 case LFUN_CELL_BACKWARD:
3259 cur.selection() = false;
3262 case LFUN_CELL_FORWARD:
3264 cur.selection() = false;
3267 case LFUN_CHAR_FORWARD_SELECT:
3268 case LFUN_CHAR_FORWARD:
3269 case LFUN_CHAR_BACKWARD_SELECT:
3270 case LFUN_CHAR_BACKWARD:
3271 case LFUN_CHAR_RIGHT_SELECT:
3272 case LFUN_CHAR_RIGHT:
3273 case LFUN_CHAR_LEFT_SELECT:
3274 case LFUN_CHAR_LEFT: {
3275 // determine whether we move to next or previous cell, where to enter
3276 // the new cell from, and which command to "finish" (i.e., exit the
3279 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3280 FuncCode finish_lfun;
3282 if (cmd.action == LFUN_CHAR_FORWARD
3283 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3285 finish_lfun = LFUN_FINISHED_FORWARD;
3287 else if (cmd.action == LFUN_CHAR_BACKWARD
3288 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3290 finish_lfun = LFUN_FINISHED_BACKWARD;
3292 // LEFT or RIGHT commands --- the interpretation will depend on the
3293 // table's direction.
3295 bool right = (cmd.action == LFUN_CHAR_RIGHT
3296 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3297 next_cell = (isRightToLeft(cur) != right);
3299 if (lyxrc.visual_cursor) {
3300 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3304 finish_lfun = LFUN_FINISHED_RIGHT;
3306 finish_lfun = LFUN_FINISHED_LEFT;
3309 // if we don't have a multicell selection...
3310 if (!cur.selIsMultiCell() ||
3311 // ...or we're not doing some LFUN_*_SELECT thing, anyway...
3312 (cmd.action != LFUN_CHAR_FORWARD_SELECT &&
3313 cmd.action != LFUN_CHAR_BACKWARD_SELECT &&
3314 cmd.action != LFUN_CHAR_RIGHT_SELECT &&
3315 cmd.action != LFUN_CHAR_LEFT_SELECT)) {
3316 // ...try to dispatch to the cell's inset.
3317 cell(cur.idx())->dispatch(cur, cmd);
3318 if (cur.result().dispatched())
3321 // move to next/prev cell, as appropriate
3322 // note that we will always do this if we're selecting and we have
3323 // a multicell selection
3324 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3325 << " cell from: " << int(entry_from));
3327 moveNextCell(cur, entry_from);
3329 movePrevCell(cur, entry_from);
3330 // if we're exiting the table, call the appropriate FINISHED lfun
3331 if (sl == cur.top())
3332 cmd = FuncRequest(finish_lfun);
3339 case LFUN_DOWN_SELECT:
3341 cell(cur.idx())->dispatch(cur, cmd);
3342 cur.dispatched(); // override the cell's decision
3343 if (sl == cur.top())
3344 // if our Text didn't do anything to the cursor
3345 // then we try to put the cursor into the cell below
3346 // setting also the right targetX.
3347 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3348 cur.idx() = tabular.cellBelow(cur.idx());
3350 TextMetrics const & tm =
3351 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3352 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3354 if (sl == cur.top()) {
3355 // we trick it to go to forward after leaving the
3357 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3362 case LFUN_UP_SELECT:
3364 cell(cur.idx())->dispatch(cur, cmd);
3365 cur.dispatched(); // override the cell's decision
3366 if (sl == cur.top())
3367 // if our Text didn't do anything to the cursor
3368 // then we try to put the cursor into the cell above
3369 // setting also the right targetX.
3370 if (tabular.cellRow(cur.idx()) != 0) {
3371 cur.idx() = tabular.cellAbove(cur.idx());
3372 cur.pit() = cur.lastpit();
3373 Text const * text = cell(cur.idx())->getText(0);
3374 TextMetrics const & tm = cur.bv().textMetrics(text);
3375 ParagraphMetrics const & pm =
3376 tm.parMetrics(cur.lastpit());
3377 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3379 if (sl == cur.top()) {
3380 cmd = FuncRequest(LFUN_UP);
3385 // case LFUN_SCREEN_DOWN: {
3386 // //if (hasSelection())
3387 // // cur.selection() = false;
3388 // col_type const col = tabular.cellColumn(cur.idx());
3389 // int const t = cur.bv().top_y() + cur.bv().height();
3390 // if (t < yo() + tabular.getHeightOfTabular()) {
3391 // cur.bv().scrollDocView(t);
3392 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3394 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3401 // case LFUN_SCREEN_UP: {
3402 // //if (hasSelection())
3403 // // cur.selection() = false;
3404 // col_type const col = tabular.cellColumn(cur.idx());
3405 // int const t = cur.bv().top_y() + cur.bv().height();
3407 // cur.bv().scrollDocView(t);
3411 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3415 // cur.par() = cur.lastpar();
3416 // cur.pos() = cur.lastpos();
3420 case LFUN_LAYOUT_TABULAR:
3421 cur.bv().showDialog("tabular", params2string(*this), this);
3424 case LFUN_INSET_DIALOG_UPDATE:
3425 cur.bv().updateDialog("tabular", params2string(*this));
3428 case LFUN_TABULAR_FEATURE:
3429 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3433 // insert file functions
3434 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3435 case LFUN_FILE_INSERT_PLAINTEXT: {
3437 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3438 FileName(to_utf8(cmd.argument())));
3441 cur.recordUndoInset(INSERT_UNDO);
3442 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3443 // content has been replaced,
3444 // so cursor might be invalid
3445 cur.pos() = cur.lastpos();
3446 cur.pit() = cur.lastpit();
3447 bvcur.setCursor(cur);
3454 if (cur.selIsMultiCell()) {
3455 if (copySelection(cur)) {
3456 cur.recordUndoInset(DELETE_UNDO);
3461 cell(cur.idx())->dispatch(cur, cmd);
3464 case LFUN_CHAR_DELETE_BACKWARD:
3465 case LFUN_CHAR_DELETE_FORWARD:
3466 if (cur.selIsMultiCell()) {
3467 cur.recordUndoInset(DELETE_UNDO);
3471 cell(cur.idx())->dispatch(cur, cmd);
3475 if (!cur.selection())
3477 if (cur.selIsMultiCell()) {
3481 cell(cur.idx())->dispatch(cur, cmd);
3484 case LFUN_CLIPBOARD_PASTE:
3485 case LFUN_PRIMARY_SELECTION_PASTE: {
3486 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3487 theClipboard().getAsText() :
3488 theSelection().get();
3491 // pass to InsertPlaintextString, but
3492 // only if we have multi-cell content
3493 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3494 cur.recordUndoInset(INSERT_UNDO);
3495 if (insertPlaintextString(cur.bv(), clip, false)) {
3496 // content has been replaced,
3497 // so cursor might be invalid
3498 cur.pos() = cur.lastpos();
3499 cur.pit() = cur.lastpit();
3500 bvcur.setCursor(cur);
3504 // Let the cell handle normal text
3505 cell(cur.idx())->dispatch(cur, cmd);
3510 if (!tabularStackDirty()) {
3511 cell(cur.idx())->dispatch(cur, cmd);
3514 if (theClipboard().isInternal() ||
3515 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3516 cur.recordUndoInset(INSERT_UNDO);
3517 pasteClipboard(cur);
3521 case LFUN_FONT_EMPH:
3522 case LFUN_FONT_BOLD:
3523 case LFUN_FONT_ROMAN:
3524 case LFUN_FONT_NOUN:
3525 case LFUN_FONT_ITAL:
3526 case LFUN_FONT_FRAK:
3527 case LFUN_FONT_TYPEWRITER:
3528 case LFUN_FONT_SANS:
3529 case LFUN_TEXTSTYLE_APPLY:
3530 case LFUN_TEXTSTYLE_UPDATE:
3531 case LFUN_FONT_SIZE:
3532 case LFUN_FONT_UNDERLINE:
3534 case LFUN_WORD_CAPITALIZE:
3535 case LFUN_WORD_UPCASE:
3536 case LFUN_WORD_LOWCASE:
3537 case LFUN_CHARS_TRANSPOSE:
3538 if (cur.selIsMultiCell()) {
3541 getSelection(cur, rs, re, cs, ce);
3542 Cursor tmpcur = cur;
3543 for (row_type i = rs; i <= re; ++i) {
3544 for (col_type j = cs; j <= ce; ++j) {
3545 // cursor follows cell:
3546 tmpcur.idx() = tabular.cellIndex(i, j);
3547 // select this cell only:
3550 tmpcur.resetAnchor();
3551 tmpcur.pit() = tmpcur.lastpit();
3552 tmpcur.pos() = tmpcur.top().lastpos();
3553 tmpcur.setCursor(tmpcur);
3554 tmpcur.setSelection();
3555 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3560 cell(cur.idx())->dispatch(cur, cmd);
3564 // we try to handle this event in the insets dispatch function.
3565 cell(cur.idx())->dispatch(cur, cmd);
3571 // function sets an object as defined in func_status.h:
3572 // states OK, Unknown, Disabled, On, Off.
3573 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3574 FuncStatus & status) const
3576 switch (cmd.action) {
3577 case LFUN_TABULAR_FEATURE: {
3578 int action = Tabular::LAST_ACTION;
3580 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3581 string const tmp = tabularFeature[i].feature;
3582 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3583 action = tabularFeature[i].action;
3587 if (action == Tabular::LAST_ACTION) {
3589 status.unknown(true);
3593 string const argument
3594 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3596 row_type sel_row_start = 0;
3597 row_type sel_row_end = 0;
3598 col_type sel_col_start = 0;
3599 col_type sel_col_end = 0;
3600 Tabular::ltType dummyltt;
3603 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3606 case Tabular::SET_PWIDTH:
3607 case Tabular::SET_MPWIDTH:
3608 case Tabular::SET_SPECIAL_COLUMN:
3609 case Tabular::SET_SPECIAL_MULTI:
3610 case Tabular::APPEND_ROW:
3611 case Tabular::APPEND_COLUMN:
3612 case Tabular::DELETE_ROW:
3613 case Tabular::DELETE_COLUMN:
3614 case Tabular::COPY_ROW:
3615 case Tabular::COPY_COLUMN:
3616 case Tabular::SET_TOP_SPACE:
3617 case Tabular::SET_BOTTOM_SPACE:
3618 case Tabular::SET_INTERLINE_SPACE:
3622 case Tabular::MULTICOLUMN:
3623 status.setEnabled(sel_row_start == sel_row_end);
3624 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3627 case Tabular::SET_ALL_LINES:
3628 case Tabular::UNSET_ALL_LINES:
3629 case Tabular::SET_BORDER_LINES:
3630 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3633 case Tabular::TOGGLE_LINE_TOP:
3634 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3635 status.setOnOff(tabular.topLine(cur.idx()));
3638 case Tabular::TOGGLE_LINE_BOTTOM:
3639 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3640 status.setOnOff(tabular.bottomLine(cur.idx()));
3643 case Tabular::TOGGLE_LINE_LEFT:
3644 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3645 status.setOnOff(tabular.leftLine(cur.idx()));
3648 case Tabular::TOGGLE_LINE_RIGHT:
3649 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3650 status.setOnOff(tabular.rightLine(cur.idx()));
3653 case Tabular::M_ALIGN_LEFT:
3655 case Tabular::ALIGN_LEFT:
3656 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3659 case Tabular::M_ALIGN_RIGHT:
3661 case Tabular::ALIGN_RIGHT:
3662 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3665 case Tabular::M_ALIGN_CENTER:
3667 case Tabular::ALIGN_CENTER:
3668 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3671 case Tabular::ALIGN_BLOCK:
3672 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3673 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3676 case Tabular::M_VALIGN_TOP:
3678 case Tabular::VALIGN_TOP:
3680 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3683 case Tabular::M_VALIGN_BOTTOM:
3685 case Tabular::VALIGN_BOTTOM:
3687 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3690 case Tabular::M_VALIGN_MIDDLE:
3692 case Tabular::VALIGN_MIDDLE:
3694 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3697 case Tabular::SET_LONGTABULAR:
3698 status.setOnOff(tabular.is_long_tabular);
3701 case Tabular::UNSET_LONGTABULAR:
3702 status.setOnOff(!tabular.is_long_tabular);
3705 case Tabular::TOGGLE_ROTATE_TABULAR:
3706 case Tabular::SET_ROTATE_TABULAR:
3707 status.setOnOff(tabular.rotate);
3710 case Tabular::UNSET_ROTATE_TABULAR:
3711 status.setOnOff(!tabular.rotate);
3714 case Tabular::TOGGLE_ROTATE_CELL:
3715 case Tabular::SET_ROTATE_CELL:
3716 status.setOnOff(!oneCellHasRotationState(false,
3717 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3720 case Tabular::UNSET_ROTATE_CELL:
3721 status.setOnOff(!oneCellHasRotationState(true,
3722 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3725 case Tabular::SET_USEBOX:
3726 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3729 case Tabular::SET_LTFIRSTHEAD:
3730 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3733 case Tabular::UNSET_LTFIRSTHEAD:
3734 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3737 case Tabular::SET_LTHEAD:
3738 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3741 case Tabular::UNSET_LTHEAD:
3742 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3745 case Tabular::SET_LTFOOT:
3746 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3749 case Tabular::UNSET_LTFOOT:
3750 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3753 case Tabular::SET_LTLASTFOOT:
3754 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3757 case Tabular::UNSET_LTLASTFOOT:
3758 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3761 case Tabular::SET_LTNEWPAGE:
3762 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3765 case Tabular::TOGGLE_LTCAPTION:
3766 status.setEnabled(sel_row_start == sel_row_end);
3767 status.setOnOff(tabular.ltCaption(sel_row_start));
3770 case Tabular::SET_BOOKTABS:
3771 status.setOnOff(tabular.use_booktabs);
3774 case Tabular::UNSET_BOOKTABS:
3775 status.setOnOff(!tabular.use_booktabs);
3780 status.setEnabled(false);
3786 // These are only enabled inside tabular
3787 case LFUN_CELL_BACKWARD:
3788 case LFUN_CELL_FORWARD:
3789 status.setEnabled(true);
3792 // disable these with multiple cells selected
3793 case LFUN_INSET_INSERT:
3794 case LFUN_TABULAR_INSERT:
3795 case LFUN_FLEX_INSERT:
3796 case LFUN_FLOAT_INSERT:
3797 case LFUN_FLOAT_WIDE_INSERT:
3798 case LFUN_FOOTNOTE_INSERT:
3799 case LFUN_MARGINALNOTE_INSERT:
3800 case LFUN_MATH_INSERT:
3801 case LFUN_MATH_MODE:
3802 case LFUN_MATH_MUTATE:
3803 case LFUN_MATH_DISPLAY:
3804 case LFUN_NOTE_INSERT:
3805 case LFUN_OPTIONAL_INSERT:
3806 case LFUN_BOX_INSERT:
3807 case LFUN_BRANCH_INSERT:
3808 case LFUN_WRAP_INSERT:
3809 case LFUN_ERT_INSERT: {
3810 if (cur.selIsMultiCell()) {
3811 status.setEnabled(false);
3814 return cell(cur.idx())->getStatus(cur, cmd, status);
3817 // disable in non-fixed-width cells
3818 case LFUN_NEWLINE_INSERT:
3819 case LFUN_BREAK_PARAGRAPH: {
3820 if (tabular.getPWidth(cur.idx()).zero()) {
3821 status.setEnabled(false);
3824 return cell(cur.idx())->getStatus(cur, cmd, status);
3827 case LFUN_NEWPAGE_INSERT:
3828 status.setEnabled(false);
3832 if (cur.selIsMultiCell()) {
3833 status.setEnabled(false);
3834 status.message(_("You cannot paste into a multicell selection."));
3837 if (tabularStackDirty() && theClipboard().isInternal()) {
3838 status.setEnabled(true);
3841 return cell(cur.idx())->getStatus(cur, cmd, status);
3843 case LFUN_INSET_MODIFY:
3844 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3845 status.setEnabled(true);
3851 // we try to handle this event in the insets dispatch function.
3852 return cell(cur.idx())->getStatus(cur, cmd, status);
3857 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3859 return tabular.latex(os, runparams);
3863 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3865 os << '\n'; // output table on a new line
3866 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3867 tabular.plaintext(os, runparams, dp, false, 0);
3868 return PLAINTEXT_NEWLINE;
3872 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3877 // FIXME: Why not pass a proper DocIterator here?
3879 // if the table is inside a float it doesn't need the informaltable
3880 // wrapper. Search for it.
3881 for (master = owner(); master; master = master->owner())
3882 if (master->lyxCode() == FLOAT_CODE)
3887 os << "<informaltable>";
3890 ret += tabular.docbook(os, runparams);
3892 os << "</informaltable>";
3899 void InsetTabular::validate(LaTeXFeatures & features) const
3901 tabular.validate(features);
3905 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3907 return tabular.cellInset(idx);
3911 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3913 return tabular.cellInset(idx);
3917 void InsetTabular::cursorPos(BufferView const & bv,
3918 CursorSlice const & sl, bool boundary, int & x, int & y) const
3920 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3922 // y offset correction
3923 int const row = tabular.cellRow(sl.idx());
3924 for (int i = 0; i <= row; ++i) {
3926 y += tabular.rowAscent(i);
3927 y += tabular.getAdditionalHeight(i);
3930 y += tabular.rowDescent(i);
3933 // x offset correction
3934 int const col = tabular.cellColumn(sl.idx());
3935 int idx = tabular.cellIndex(row, 0);
3936 for (int j = 0; j < col; ++j) {
3937 if (tabular.isPartOfMultiColumn(row, j))
3939 x += tabular.columnWidth(idx);
3942 x += tabular.getBeginningOfTextInCell(idx);
3943 x += ADD_TO_TABULAR_WIDTH;
3948 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3952 Inset const & inset = *tabular.cellInset(cell);
3953 Point o = bv.coordCache().getInsets().xy(&inset);
3954 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3955 int const xend = xbeg + tabular.columnWidth(cell);
3956 row_type const row = tabular.cellRow(cell);
3957 int const ybeg = o.y_ - tabular.rowAscent(row) -
3958 tabular.getAdditionalHeight(row);
3959 int const yend = o.y_ + tabular.rowDescent(row);
3971 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3972 // << " ybeg=" << ybeg << " yend=" << yend
3973 // << " xx=" << xx << " yy=" << yy
3974 // << " dist=" << xx + yy << endl;
3979 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3981 //lyxerr << "InsetTabular::editXY: " << this << endl;
3982 cur.selection() = false;
3984 cur.idx() = getNearestCell(cur.bv(), x, y);
3986 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
3990 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3992 cur.idx() = getNearestCell(cur.bv(), x, y);
3993 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
3997 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
3999 idx_type idx_min = 0;
4000 int dist_min = numeric_limits<int>::max();
4001 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4002 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4003 int const d = dist(bv, i, x, y);
4014 int InsetTabular::cellXPos(idx_type const cell) const
4018 for (; !tabular.isFirstCellInRow(c); --c)
4021 for (; c < cell; ++c)
4022 lx += tabular.columnWidth(c);
4028 void InsetTabular::resetPos(Cursor & cur) const
4030 BufferView & bv = cur.bv();
4031 int const maxwidth = bv.workWidth();
4033 int const scx_old = scx_;
4034 int const i = cur.find(this);
4039 int const X2 = maxwidth;
4040 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4041 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4042 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4045 scx_ = X1 + 20 - x1;
4047 scx_ = X2 - 20 - x2;
4052 // only update if offset changed
4053 if (scx_ != scx_old)
4054 cur.updateFlags(Update::Force | Update::FitCursor);
4058 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4060 if (isRightToLeft(cur)) {
4061 if (tabular.isFirstCellInRow(cur.idx())) {
4062 row_type const row = tabular.cellRow(cur.idx());
4063 if (row == tabular.row_info.size() - 1)
4065 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4072 if (tabular.isLastCell(cur.idx()))
4078 cur.boundary(false);
4080 // in visual mode, place cursor at extreme left or right
4082 switch(entry_from) {
4084 case ENTRY_DIRECTION_RIGHT:
4085 cur.posVisToRowExtremity(false /* !left */);
4087 case ENTRY_DIRECTION_LEFT:
4088 cur.posVisToRowExtremity(true /* left */);
4090 case ENTRY_DIRECTION_IGNORE:
4091 // nothing to do in this case
4100 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4102 if (isRightToLeft(cur)) {
4103 if (tabular.isLastCellInRow(cur.idx())) {
4104 row_type const row = tabular.cellRow(cur.idx());
4107 cur.idx() = tabular.getFirstCellInRow(row);
4108 cur.idx() = tabular.cellAbove(cur.idx());
4110 if (tabular.isLastCell(cur.idx()))
4115 if (cur.idx() == 0) // first cell
4119 cur.pit() = cur.lastpit();
4120 cur.pos() = cur.lastpos();
4122 // in visual mode, place cursor at extreme left or right
4124 switch(entry_from) {
4126 case ENTRY_DIRECTION_RIGHT:
4127 cur.posVisToRowExtremity(false /* !left */);
4129 case ENTRY_DIRECTION_LEFT:
4130 cur.posVisToRowExtremity(true /* left */);
4132 case ENTRY_DIRECTION_IGNORE:
4133 // nothing to do in this case
4142 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4144 Tabular::Feature action = Tabular::LAST_ACTION;
4147 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4148 string const tmp = tabularFeature[i].feature;
4150 if (tmp == what.substr(0, tmp.length())) {
4151 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4152 //tabularFeatures[i].feature.length()))
4153 action = tabularFeature[i].action;
4157 if (action == Tabular::LAST_ACTION)
4161 ltrim(what.substr(tabularFeature[i].feature.length()));
4162 tabularFeatures(cur, action, val);
4167 static void checkLongtableSpecial(Tabular::ltType & ltt,
4168 string const & special, bool & flag)
4170 if (special == "dl_above") {
4173 } else if (special == "dl_below") {
4174 ltt.bottomDL = flag;
4176 } else if (special == "empty") {
4185 bool InsetTabular::oneCellHasRotationState(bool rotated,
4186 row_type row_start, row_type row_end,
4187 col_type col_start, col_type col_end) const {
4189 for (row_type i = row_start; i <= row_end; ++i) {
4190 for (col_type j = col_start; j <= col_end; ++j) {
4191 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4200 void InsetTabular::tabularFeatures(Cursor & cur,
4201 Tabular::Feature feature, string const & value)
4203 col_type sel_col_start;
4204 col_type sel_col_end;
4205 row_type sel_row_start;
4206 row_type sel_row_end;
4207 bool setLines = false;
4208 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4209 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4213 case Tabular::M_ALIGN_LEFT:
4214 case Tabular::ALIGN_LEFT:
4215 setAlign = LYX_ALIGN_LEFT;
4218 case Tabular::M_ALIGN_RIGHT:
4219 case Tabular::ALIGN_RIGHT:
4220 setAlign = LYX_ALIGN_RIGHT;
4223 case Tabular::M_ALIGN_CENTER:
4224 case Tabular::ALIGN_CENTER:
4225 setAlign = LYX_ALIGN_CENTER;
4228 case Tabular::ALIGN_BLOCK:
4229 setAlign = LYX_ALIGN_BLOCK;
4232 case Tabular::M_VALIGN_TOP:
4233 case Tabular::VALIGN_TOP:
4234 setVAlign = Tabular::LYX_VALIGN_TOP;
4237 case Tabular::M_VALIGN_BOTTOM:
4238 case Tabular::VALIGN_BOTTOM:
4239 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4242 case Tabular::M_VALIGN_MIDDLE:
4243 case Tabular::VALIGN_MIDDLE:
4244 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4251 cur.recordUndoInset(ATOMIC_UNDO);
4253 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4254 row_type const row = tabular.cellRow(cur.idx());
4255 col_type const column = tabular.cellColumn(cur.idx());
4257 Tabular::ltType ltt;
4261 case Tabular::SET_PWIDTH: {
4262 Length const len(value);
4263 tabular.setColumnPWidth(cur, cur.idx(), len);
4265 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4266 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4270 case Tabular::SET_MPWIDTH:
4271 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4274 case Tabular::SET_SPECIAL_COLUMN:
4275 case Tabular::SET_SPECIAL_MULTI:
4276 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4279 case Tabular::APPEND_ROW:
4280 // append the row into the tabular
4281 tabular.appendRow(cur.idx());
4284 case Tabular::APPEND_COLUMN:
4285 // append the column into the tabular
4286 tabular.appendColumn(cur.idx());
4287 cur.idx() = tabular.cellIndex(row, column);
4290 case Tabular::DELETE_ROW:
4291 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4292 tabular.deleteRow(sel_row_start);
4293 if (sel_row_start >= tabular.row_info.size())
4295 cur.idx() = tabular.cellIndex(sel_row_start, column);
4298 cur.selection() = false;
4301 case Tabular::DELETE_COLUMN:
4302 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4303 tabular.deleteColumn(sel_col_start);
4304 if (sel_col_start >= tabular.column_info.size())
4306 cur.idx() = tabular.cellIndex(row, sel_col_start);
4309 cur.selection() = false;
4312 case Tabular::COPY_ROW:
4313 tabular.copyRow(row);
4316 case Tabular::COPY_COLUMN:
4317 tabular.copyColumn(column);
4318 cur.idx() = tabular.cellIndex(row, column);
4321 case Tabular::TOGGLE_LINE_TOP: {
4322 bool lineSet = !tabular.topLine(cur.idx());
4323 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4324 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4325 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4329 case Tabular::TOGGLE_LINE_BOTTOM: {
4330 bool lineSet = !tabular.bottomLine(cur.idx());
4331 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4332 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4333 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4337 case Tabular::TOGGLE_LINE_LEFT: {
4338 bool lineSet = !tabular.leftLine(cur.idx());
4339 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4340 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4341 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4345 case Tabular::TOGGLE_LINE_RIGHT: {
4346 bool lineSet = !tabular.rightLine(cur.idx());
4347 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4348 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4349 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4353 case Tabular::M_ALIGN_LEFT:
4354 case Tabular::M_ALIGN_RIGHT:
4355 case Tabular::M_ALIGN_CENTER:
4357 case Tabular::ALIGN_LEFT:
4358 case Tabular::ALIGN_RIGHT:
4359 case Tabular::ALIGN_CENTER:
4360 case Tabular::ALIGN_BLOCK:
4361 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4362 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4363 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4366 case Tabular::M_VALIGN_TOP:
4367 case Tabular::M_VALIGN_BOTTOM:
4368 case Tabular::M_VALIGN_MIDDLE:
4370 case Tabular::VALIGN_TOP:
4371 case Tabular::VALIGN_BOTTOM:
4372 case Tabular::VALIGN_MIDDLE:
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.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4378 case Tabular::MULTICOLUMN: {
4379 if (!cur.selection()) {
4380 // just multicol for one single cell
4381 // check whether we are completely in a multicol
4382 if (tabular.isMultiColumn(cur.idx()))
4383 tabular.unsetMultiColumn(cur.idx());
4385 tabular.setMultiColumn(cur.idx(), 1);
4388 // we have a selection so this means we just add all this
4389 // cells to form a multicolumn cell
4390 idx_type const s_start = cur.selBegin().idx();
4391 idx_type const s_end = cur.selEnd().idx();
4392 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4393 cur.idx() = s_start;
4396 cur.selection() = false;
4400 case Tabular::SET_ALL_LINES:
4402 case Tabular::UNSET_ALL_LINES:
4403 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4404 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4405 tabular.setAllLines(
4406 tabular.cellIndex(i,j), setLines);
4409 case Tabular::SET_BORDER_LINES:
4410 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4411 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4412 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4414 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4415 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4416 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4420 case Tabular::SET_LONGTABULAR:
4421 tabular.is_long_tabular = true;
4424 case Tabular::UNSET_LONGTABULAR:
4425 tabular.is_long_tabular = false;
4428 case Tabular::SET_ROTATE_TABULAR:
4429 tabular.rotate = true;
4432 case Tabular::UNSET_ROTATE_TABULAR:
4433 tabular.rotate = false;
4436 case Tabular::TOGGLE_ROTATE_TABULAR:
4437 tabular.rotate = !tabular.rotate;
4440 case Tabular::SET_ROTATE_CELL:
4441 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4442 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4443 tabular.setRotateCell(
4444 tabular.cellIndex(i, j), true);
4447 case Tabular::UNSET_ROTATE_CELL:
4448 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4449 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4450 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4453 case Tabular::TOGGLE_ROTATE_CELL:
4455 bool oneNotRotated = oneCellHasRotationState(false,
4456 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4458 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4459 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4460 tabular.setRotateCell(tabular.cellIndex(i, j),
4465 case Tabular::SET_USEBOX: {
4466 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4467 if (val == tabular.getUsebox(cur.idx()))
4468 val = Tabular::BOX_NONE;
4469 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4470 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4471 tabular.setUsebox(tabular.cellIndex(i, j), val);
4475 case Tabular::UNSET_LTFIRSTHEAD:
4477 case Tabular::SET_LTFIRSTHEAD:
4478 tabular.getRowOfLTFirstHead(row, ltt);
4479 checkLongtableSpecial(ltt, value, flag);
4480 tabular.setLTHead(row, flag, ltt, true);
4483 case Tabular::UNSET_LTHEAD:
4485 case Tabular::SET_LTHEAD:
4486 tabular.getRowOfLTHead(row, ltt);
4487 checkLongtableSpecial(ltt, value, flag);
4488 tabular.setLTHead(row, flag, ltt, false);
4491 case Tabular::UNSET_LTFOOT:
4493 case Tabular::SET_LTFOOT:
4494 tabular.getRowOfLTFoot(row, ltt);
4495 checkLongtableSpecial(ltt, value, flag);
4496 tabular.setLTFoot(row, flag, ltt, false);
4499 case Tabular::UNSET_LTLASTFOOT:
4501 case Tabular::SET_LTLASTFOOT:
4502 tabular.getRowOfLTLastFoot(row, ltt);
4503 checkLongtableSpecial(ltt, value, flag);
4504 tabular.setLTFoot(row, flag, ltt, true);
4507 case Tabular::SET_LTNEWPAGE:
4508 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4511 case Tabular::TOGGLE_LTCAPTION:
4512 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4515 cur.selection() = false;
4518 case Tabular::SET_BOOKTABS:
4519 tabular.use_booktabs = true;
4522 case Tabular::UNSET_BOOKTABS:
4523 tabular.use_booktabs = false;
4526 case Tabular::SET_TOP_SPACE: {
4528 if (value == "default")
4529 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4530 tabular.row_info[i].top_space_default = true;
4531 else if (isValidLength(value, &len))
4532 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4533 tabular.row_info[i].top_space_default = false;
4534 tabular.row_info[i].top_space = len;
4537 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4538 tabular.row_info[i].top_space_default = false;
4539 tabular.row_info[i].top_space = len;
4544 case Tabular::SET_BOTTOM_SPACE: {
4546 if (value == "default")
4547 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4548 tabular.row_info[i].bottom_space_default = true;
4549 else if (isValidLength(value, &len))
4550 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4551 tabular.row_info[i].bottom_space_default = false;
4552 tabular.row_info[i].bottom_space = len;
4555 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4556 tabular.row_info[i].bottom_space_default = false;
4557 tabular.row_info[i].bottom_space = len;
4562 case Tabular::SET_INTERLINE_SPACE: {
4564 if (value == "default")
4565 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4566 tabular.row_info[i].interline_space_default = true;
4567 else if (isValidLength(value, &len))
4568 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4569 tabular.row_info[i].interline_space_default = false;
4570 tabular.row_info[i].interline_space = len;
4573 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4574 tabular.row_info[i].interline_space_default = false;
4575 tabular.row_info[i].interline_space = len;
4580 // dummy stuff just to avoid warnings
4581 case Tabular::LAST_ACTION:
4587 bool InsetTabular::showInsetDialog(BufferView * bv) const
4589 bv->showDialog("tabular", params2string(*this),
4590 const_cast<InsetTabular *>(this));
4595 void InsetTabular::openLayoutDialog(BufferView * bv) const
4597 bv->showDialog("tabular", params2string(*this),
4598 const_cast<InsetTabular *>(this));
4602 bool InsetTabular::copySelection(Cursor & cur)
4604 if (!cur.selection())
4609 getSelection(cur, rs, re, cs, ce);
4611 paste_tabular.reset(new Tabular(tabular));
4613 for (row_type i = 0; i < rs; ++i)
4614 paste_tabular->deleteRow(0);
4616 row_type const rows = re - rs + 1;
4617 while (paste_tabular->row_info.size() > rows)
4618 paste_tabular->deleteRow(rows);
4620 for (col_type i = 0; i < cs; ++i)
4621 paste_tabular->deleteColumn(0);
4623 col_type const columns = ce - cs + 1;
4624 while (paste_tabular->column_info.size() > columns)
4625 paste_tabular->deleteColumn(columns);
4627 paste_tabular->setBuffer(tabular.buffer());
4629 odocstringstream os;
4630 OutputParams const runparams(0);
4631 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4632 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4633 cap::copySelection(cur, os.str());
4635 // mark tabular stack dirty
4636 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4637 // when we (hopefully) have a one-for-all paste mechanism.
4638 // This must be called after cap::copySelection.
4639 dirtyTabularStack(true);
4645 bool InsetTabular::pasteClipboard(Cursor & cur)
4649 col_type const actcol = tabular.cellColumn(cur.idx());
4650 row_type const actrow = tabular.cellRow(cur.idx());
4651 for (row_type r1 = 0, r2 = actrow;
4652 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4654 for (col_type c1 = 0, c2 = actcol;
4655 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4657 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4658 tabular.isPartOfMultiColumn(r2, c2))
4660 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4664 if (tabular.isPartOfMultiColumn(r2, c2)) {
4668 shared_ptr<InsetTableCell> inset(
4669 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4670 tabular.setCellInset(r2, c2, inset);
4671 // FIXME: change tracking (MG)
4672 inset->setChange(Change(cur.buffer().params().trackChanges ?
4673 Change::INSERTED : Change::UNCHANGED));
4681 void InsetTabular::cutSelection(Cursor & cur)
4683 if (!cur.selection())
4688 getSelection(cur, rs, re, cs, ce);
4689 for (row_type i = rs; i <= re; ++i) {
4690 for (col_type j = cs; j <= ce; ++j) {
4691 shared_ptr<InsetTableCell> t
4692 = cell(tabular.cellIndex(i, j));
4693 if (cur.buffer().params().trackChanges)
4694 // FIXME: Change tracking (MG)
4695 t->setChange(Change(Change::DELETED));
4701 // cursor position might be invalid now
4702 if (cur.pit() > cur.lastpit())
4703 cur.pit() = cur.lastpit();
4704 if (cur.pos() > cur.lastpos())
4705 cur.pos() = cur.lastpos();
4706 cur.clearSelection();
4710 bool InsetTabular::isRightToLeft(Cursor & cur) const
4712 LASSERT(cur.depth() > 1, /**/);
4713 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4714 pos_type const parentpos = cur[cur.depth() - 2].pos();
4715 return parentpar.getFontSettings(cur.bv().buffer().params(),
4716 parentpos).language()->rightToLeft();
4719 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4722 LASSERT(stidx <= enidx, return docstring());
4724 col_type const col1 = tabular.cellColumn(stidx);
4725 col_type const col2 = tabular.cellColumn(enidx);
4726 row_type const row1 = tabular.cellRow(stidx);
4727 row_type const row2 = tabular.cellRow(enidx);
4729 for (col_type col = col1; col <= col2; col++)
4730 for (row_type row = row1; row <= row2; row++) {
4735 retval += tabular.cellInset(row, col)->asString(intoInsets);
4741 void InsetTabular::getSelection(Cursor & cur,
4742 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4744 CursorSlice const & beg = cur.selBegin();
4745 CursorSlice const & end = cur.selEnd();
4746 cs = tabular.cellColumn(beg.idx());
4747 ce = tabular.cellColumn(end.idx());
4750 cs = tabular.cellColumn(end.idx());
4752 ce = tabular.cellRightColumn(end.idx());
4755 rs = tabular.cellRow(beg.idx());
4756 re = tabular.cellRow(end.idx());
4762 Text * InsetTabular::getText(int idx) const
4764 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4768 void InsetTabular::setChange(Change const & change)
4770 for (idx_type idx = 0; idx < nargs(); ++idx)
4771 cell(idx)->setChange(change);
4775 void InsetTabular::acceptChanges(BufferParams const & bparams)
4777 for (idx_type idx = 0; idx < nargs(); ++idx)
4778 cell(idx)->acceptChanges(bparams);
4782 void InsetTabular::rejectChanges(BufferParams const & bparams)
4784 for (idx_type idx = 0; idx < nargs(); ++idx)
4785 cell(idx)->rejectChanges(bparams);
4789 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4791 return tabular.getPWidth(cell).zero();
4795 bool InsetTabular::forcePlainLayout(idx_type cell) const
4797 return !tabular.getPWidth(cell).zero();
4801 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4804 if (buf.length() <= 0)
4809 col_type maxCols = 1;
4810 size_t const len = buf.length();
4814 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4822 maxCols = max(cols, maxCols);
4828 maxCols = max(cols, maxCols);
4834 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4835 loctab = paste_tabular.get();
4837 dirtyTabularStack(true);
4840 cell = bv.cursor().idx();
4841 ocol = tabular.cellColumn(cell);
4842 row = tabular.cellRow(cell);
4846 idx_type const cells = loctab->numberofcells;
4849 rows = loctab->row_info.size();
4850 col_type const columns = loctab->column_info.size();
4852 while (cell < cells && p < len && row < rows &&
4853 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4859 // we can only set this if we are not too far right
4860 if (cols < columns) {
4861 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4862 Font const font = bv.textMetrics(&inset->text()).
4864 inset->setText(buf.substr(op, p - op), font,
4865 buffer().params().trackChanges);
4871 // we can only set this if we are not too far right
4872 if (cols < columns) {
4873 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4874 Font const font = bv.textMetrics(&inset->text()).
4876 inset->setText(buf.substr(op, p - op), font,
4877 buffer().params().trackChanges);
4882 cell = loctab->cellIndex(row, cols);
4888 // check for the last cell if there is no trailing '\n'
4889 if (cell < cells && op < len) {
4890 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4891 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
4892 inset->setText(buf.substr(op, len - op), font,
4893 buffer().params().trackChanges);
4899 void InsetTabular::addPreview(PreviewLoader & loader) const
4901 row_type const rows = tabular.row_info.size();
4902 col_type const columns = tabular.column_info.size();
4903 for (row_type i = 0; i < rows; ++i) {
4904 for (col_type j = 0; j < columns; ++j)
4905 tabular.cellInset(i, j)->addPreview(loader);
4910 bool InsetTabular::completionSupported(Cursor const & cur) const
4912 Cursor const & bvCur = cur.bv().cursor();
4913 if (&bvCur.inset() != this)
4915 return cur.text()->completionSupported(cur);
4919 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4921 return completionSupported(cur);
4925 bool InsetTabular::automaticInlineCompletion() const
4927 return lyxrc.completion_inline_text;
4931 bool InsetTabular::automaticPopupCompletion() const
4933 return lyxrc.completion_popup_text;
4937 bool InsetTabular::showCompletionCursor() const
4939 return lyxrc.completion_cursor_text;
4943 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4945 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4949 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4951 if (!completionSupported(cur))
4953 return cur.text()->completionPrefix(cur);
4957 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4959 if (!completionSupported(cur))
4962 return cur.text()->insertCompletion(cur, s, finished);
4966 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4967 Dimension & dim) const
4969 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4970 tm.completionPosAndDim(cur, x, y, dim);
4974 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4976 istringstream data(in);
4978 lex.setStream(data);
4985 if (!lex || token != "tabular") {
4986 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4990 // This is part of the inset proper that is usually swallowed
4991 // by Buffer::readInset
4993 if (!lex || token != "Tabular") {
4994 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5002 string InsetTabular::params2string(InsetTabular const & inset)
5005 data << "tabular" << ' ';
5007 data << "\\end_inset\n";