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 cellInset(cellIndex(i, j)).get()->setContentAlignment(
1428 cell_info[i][j].alignment);
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 col_type const ncols = column_info.size();
1806 vector<bool> topline;
1808 for (col_type c = 0; c < ncols; ++c) {
1809 topline.push_back(topLine(cellIndex(row, c)));
1814 if ((row == 0 && nset == 0) || (row > 0 && nset != ncols))
1817 if (nset == ncols) {
1819 os << (row == 0 ? "\\toprule " : "\\midrule ");
1823 } else if (row == 0) {
1824 for (col_type c = 0; c < ncols; ++c) {
1826 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1827 while (c < ncols && topline[c])
1838 int Tabular::TeXBottomHLine(odocstream & os, row_type row) const
1840 bool lastrow = row == row_info.size() - 1;
1841 col_type const ncols = column_info.size();
1842 vector<bool> bottomline, topline;
1843 bool nextrowset = true;
1844 for (col_type c = 0; c < ncols; ++c) {
1845 bottomline.push_back(bottomLine(cellIndex(row, c)));
1846 topline.push_back(!lastrow && topLine(cellIndex(row + 1, c)));
1847 nextrowset &= topline[c];
1851 for (col_type c = 0; c < ncols; ++c) {
1853 bottomline[c] = bottomline[c] || topline[c];
1858 if (nset == 0 || (nextrowset && nset != ncols))
1861 if (nset == ncols) {
1863 os << (lastrow ? "\\bottomrule" : "\\midrule");
1867 for (col_type c = 0; c < ncols; ++c) {
1868 if (bottomline[c]) {
1869 os << (use_booktabs ? "\\cmidrule{" : "\\cline{") << c + 1 << '-';
1870 while (c < ncols && bottomline[c])
1881 int Tabular::TeXCellPreamble(odocstream & os, idx_type cell, bool & ismulticol) const
1884 row_type const r = cellRow(cell);
1885 if (is_long_tabular && row_info[r].caption)
1888 Tabular::VAlignment valign = getVAlignment(cell, !isMultiColumn(cell));
1889 LyXAlignment align = getAlignment(cell, !isMultiColumn(cell));
1890 // figure out how to set the lines
1891 // we always set double lines to the right of the cell
1892 col_type const c = cellColumn(cell);
1893 col_type const nextcol = c + columnSpan(cell);
1894 bool colright = columnRightLine(c);
1895 bool colleft = columnLeftLine(c);
1896 bool nextcolleft = nextcol < column_info.size() && columnLeftLine(nextcol);
1897 bool nextcellleft = nextcol < column_info.size()
1898 && leftLine(cellIndex(r, nextcol));
1899 bool coldouble = colright && nextcolleft;
1900 bool celldouble = rightLine(cell) && nextcellleft;
1901 ismulticol = isMultiColumn(cell)
1902 || (c == 0 && colleft != leftLine(cell))
1903 || ((colright || nextcolleft) && !rightLine(cell) && !nextcellleft)
1904 || (!colright && !nextcolleft && (rightLine(cell) || nextcellleft))
1905 || (coldouble != celldouble);
1907 os << "\\multicolumn{" << columnSpan(cell) << "}{";
1908 if (c ==0 && leftLine(cell))
1910 if (!cellInfo(cell).align_special.empty()) {
1911 os << cellInfo(cell).align_special;
1913 if (!getPWidth(cell).zero()) {
1915 case LYX_ALIGN_LEFT:
1916 os << ">{\\raggedright}";
1918 case LYX_ALIGN_RIGHT:
1919 os << ">{\\raggedleft}";
1921 case LYX_ALIGN_CENTER:
1922 os << ">{\\centering}";
1928 case LYX_VALIGN_TOP:
1931 case LYX_VALIGN_MIDDLE:
1934 case LYX_VALIGN_BOTTOM:
1939 << from_ascii(getPWidth(cell).asLatexString())
1943 case LYX_ALIGN_LEFT:
1946 case LYX_ALIGN_RIGHT:
1953 } // end if else !getPWidth
1954 } // end if else !cellinfo_of_cell
1955 if (rightLine(cell) || nextcellleft)
1958 // add extra vertical line if we want a double one
1962 if (getRotateCell(cell)) {
1963 os << "\\begin{sideways}\n";
1966 if (getUsebox(cell) == BOX_PARBOX) {
1969 case LYX_VALIGN_TOP:
1972 case LYX_VALIGN_MIDDLE:
1975 case LYX_VALIGN_BOTTOM:
1979 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
1981 } else if (getUsebox(cell) == BOX_MINIPAGE) {
1982 os << "\\begin{minipage}[";
1984 case LYX_VALIGN_TOP:
1987 case LYX_VALIGN_MIDDLE:
1990 case LYX_VALIGN_BOTTOM:
1994 os << "]{" << from_ascii(getPWidth(cell).asLatexString())
2002 int Tabular::TeXCellPostamble(odocstream & os, idx_type cell, bool ismulticol) const
2005 row_type const r = cellRow(cell);
2006 if (is_long_tabular && row_info[r].caption)
2010 if (getUsebox(cell) == BOX_PARBOX)
2012 else if (getUsebox(cell) == BOX_MINIPAGE) {
2013 os << "%\n\\end{minipage}";
2016 if (getRotateCell(cell)) {
2017 os << "%\n\\end{sideways}";
2027 int Tabular::TeXLongtableHeaderFooter(odocstream & os,
2028 OutputParams const & runparams) const
2030 if (!is_long_tabular)
2034 // output header info
2036 if (endhead.topDL) {
2040 for (row_type i = 0; i < row_info.size(); ++i) {
2041 if (row_info[i].endhead) {
2042 ret += TeXRow(os, i, runparams);
2045 if (endhead.bottomDL) {
2049 os << "\\endhead\n";
2051 if (endfirsthead.empty) {
2052 os << "\\endfirsthead\n";
2056 // output firstheader info
2057 if (haveLTFirstHead()) {
2058 if (endfirsthead.topDL) {
2062 for (row_type i = 0; i < row_info.size(); ++i) {
2063 if (row_info[i].endfirsthead) {
2064 ret += TeXRow(os, i, runparams);
2067 if (endfirsthead.bottomDL) {
2071 os << "\\endfirsthead\n";
2074 // output footer info
2076 if (endfoot.topDL) {
2080 for (row_type i = 0; i < row_info.size(); ++i) {
2081 if (row_info[i].endfoot) {
2082 ret += TeXRow(os, i, runparams);
2085 if (endfoot.bottomDL) {
2089 os << "\\endfoot\n";
2091 if (endlastfoot.empty) {
2092 os << "\\endlastfoot\n";
2096 // output lastfooter info
2097 if (haveLTLastFoot()) {
2098 if (endlastfoot.topDL) {
2102 for (row_type i = 0; i < row_info.size(); ++i) {
2103 if (row_info[i].endlastfoot) {
2104 ret += TeXRow(os, i, runparams);
2107 if (endlastfoot.bottomDL) {
2111 os << "\\endlastfoot\n";
2118 bool Tabular::isValidRow(row_type row) const
2120 if (!is_long_tabular)
2122 return !row_info[row].endhead && !row_info[row].endfirsthead &&
2123 !row_info[row].endfoot && !row_info[row].endlastfoot;
2127 int Tabular::TeXRow(odocstream & os, row_type i,
2128 OutputParams const & runparams) const
2130 idx_type cell = cellIndex(i, 0);
2131 int ret = TeXTopHLine(os, i);
2132 if (row_info[i].top_space_default) {
2134 os << "\\addlinespace\n";
2136 os << "\\noalign{\\vskip\\doublerulesep}\n";
2138 } else if(!row_info[i].top_space.zero()) {
2140 os << "\\addlinespace["
2141 << from_ascii(row_info[i].top_space.asLatexString())
2144 os << "\\noalign{\\vskip"
2145 << from_ascii(row_info[i].top_space.asLatexString())
2150 bool ismulticol = false;
2151 for (col_type j = 0; j < column_info.size(); ++j) {
2152 if (isPartOfMultiColumn(i, j))
2154 ret += TeXCellPreamble(os, cell, ismulticol);
2155 shared_ptr<InsetTableCell> inset = cellInset(cell);
2157 Paragraph const & par = inset->paragraphs().front();
2158 bool rtl = par.isRTL(buffer().params())
2160 && getPWidth(cell).zero();
2164 par.getParLanguage(buffer().params())->lang();
2165 if (lang == "farsi")
2167 else if (lang == "arabic_arabi")
2169 // currently, remaning RTL languages are
2170 // arabic_arabtex and hebrew
2174 // pass to the OutputParams that we are in a cell and
2175 // which alignment we have set.
2176 // InsetNewline needs this context information.
2177 OutputParams newrp = runparams;
2178 newrp.inTableCell = (getAlignment(cell) == LYX_ALIGN_BLOCK)
2179 ? OutputParams::PLAIN
2180 : OutputParams::ALIGNED;
2181 ret += inset->latex(os, newrp);
2185 ret += TeXCellPostamble(os, cell, ismulticol);
2186 if (!isLastCellInRow(cell)) { // not last cell in row
2191 os << "\\tabularnewline";
2192 if (row_info[i].bottom_space_default) {
2194 os << "\\addlinespace";
2196 os << "[\\doublerulesep]";
2197 } else if (!row_info[i].bottom_space.zero()) {
2199 os << "\\addlinespace";
2201 << from_ascii(row_info[i].bottom_space.asLatexString())
2206 ret += TeXBottomHLine(os, i);
2207 if (row_info[i].interline_space_default) {
2209 os << "\\addlinespace\n";
2211 os << "\\noalign{\\vskip\\doublerulesep}\n";
2213 } else if (!row_info[i].interline_space.zero()) {
2215 os << "\\addlinespace["
2216 << from_ascii(row_info[i].interline_space.asLatexString())
2219 os << "\\noalign{\\vskip"
2220 << from_ascii(row_info[i].interline_space.asLatexString())
2228 int Tabular::latex(odocstream & os, OutputParams const & runparams) const
2232 //+---------------------------------------------------------------------
2233 //+ first the opening preamble +
2234 //+---------------------------------------------------------------------
2237 os << "\\begin{sideways}\n";
2240 if (is_long_tabular)
2241 os << "\\begin{longtable}{";
2243 os << "\\begin{tabular}{";
2245 for (col_type i = 0; i < column_info.size(); ++i) {
2246 if (columnLeftLine(i))
2248 if (!column_info[i].align_special.empty()) {
2249 os << column_info[i].align_special;
2251 if (!column_info[i].p_width.zero()) {
2252 switch (column_info[i].alignment) {
2253 case LYX_ALIGN_LEFT:
2254 os << ">{\\raggedright}";
2256 case LYX_ALIGN_RIGHT:
2257 os << ">{\\raggedleft}";
2259 case LYX_ALIGN_CENTER:
2260 os << ">{\\centering}";
2262 case LYX_ALIGN_NONE:
2263 case LYX_ALIGN_BLOCK:
2264 case LYX_ALIGN_LAYOUT:
2265 case LYX_ALIGN_SPECIAL:
2269 switch (column_info[i].valignment) {
2270 case LYX_VALIGN_TOP:
2273 case LYX_VALIGN_MIDDLE:
2276 case LYX_VALIGN_BOTTOM:
2281 << from_ascii(column_info[i].p_width.asLatexString())
2284 switch (column_info[i].alignment) {
2285 case LYX_ALIGN_LEFT:
2288 case LYX_ALIGN_RIGHT:
2295 } // end if else !column_info[i].p_width
2296 } // end if else !column_info[i].align_special
2297 if (columnRightLine(i))
2303 ret += TeXLongtableHeaderFooter(os, runparams);
2305 //+---------------------------------------------------------------------
2306 //+ the single row and columns (cells) +
2307 //+---------------------------------------------------------------------
2309 for (row_type i = 0; i < row_info.size(); ++i) {
2310 if (isValidRow(i)) {
2311 ret += TeXRow(os, i, runparams);
2312 if (is_long_tabular && row_info[i].newpage) {
2313 os << "\\newpage\n";
2319 //+---------------------------------------------------------------------
2320 //+ the closing of the tabular +
2321 //+---------------------------------------------------------------------
2323 if (is_long_tabular)
2324 os << "\\end{longtable}";
2326 os << "\\end{tabular}";
2328 os << "\n\\end{sideways}";
2336 int Tabular::docbookRow(odocstream & os, row_type row,
2337 OutputParams const & runparams) const
2340 idx_type cell = getFirstCellInRow(row);
2343 for (col_type j = 0; j < column_info.size(); ++j) {
2344 if (isPartOfMultiColumn(row, j))
2347 os << "<entry align=\"";
2348 switch (getAlignment(cell)) {
2349 case LYX_ALIGN_LEFT:
2352 case LYX_ALIGN_RIGHT:
2360 os << "\" valign=\"";
2361 switch (getVAlignment(cell)) {
2362 case LYX_VALIGN_TOP:
2365 case LYX_VALIGN_BOTTOM:
2368 case LYX_VALIGN_MIDDLE:
2373 if (isMultiColumn(cell)) {
2374 os << " namest=\"col" << j << "\" ";
2375 os << "nameend=\"col" << j + columnSpan(cell) - 1<< '"';
2379 ret += cellInset(cell)->docbook(os, runparams);
2388 int Tabular::docbook(odocstream & os, OutputParams const & runparams) const
2392 //+---------------------------------------------------------------------
2393 //+ first the opening preamble +
2394 //+---------------------------------------------------------------------
2396 os << "<tgroup cols=\"" << column_info.size()
2397 << "\" colsep=\"1\" rowsep=\"1\">\n";
2399 for (col_type i = 0; i < column_info.size(); ++i) {
2400 os << "<colspec colname=\"col" << i << "\" align=\"";
2401 switch (column_info[i].alignment) {
2402 case LYX_ALIGN_LEFT:
2405 case LYX_ALIGN_RIGHT:
2413 if (runparams.flavor == OutputParams::XML)
2419 //+---------------------------------------------------------------------
2420 //+ Long Tabular case +
2421 //+---------------------------------------------------------------------
2423 // output header info
2424 if (haveLTHead() || haveLTFirstHead()) {
2427 for (row_type i = 0; i < row_info.size(); ++i) {
2428 if (row_info[i].endhead || row_info[i].endfirsthead) {
2429 ret += docbookRow(os, i, runparams);
2435 // output footer info
2436 if (haveLTFoot() || haveLTLastFoot()) {
2439 for (row_type i = 0; i < row_info.size(); ++i) {
2440 if (row_info[i].endfoot || row_info[i].endlastfoot) {
2441 ret += docbookRow(os, i, runparams);
2448 //+---------------------------------------------------------------------
2449 //+ the single row and columns (cells) +
2450 //+---------------------------------------------------------------------
2454 for (row_type i = 0; i < row_info.size(); ++i) {
2455 if (isValidRow(i)) {
2456 ret += docbookRow(os, i, runparams);
2461 //+---------------------------------------------------------------------
2462 //+ the closing of the tabular +
2463 //+---------------------------------------------------------------------
2472 bool Tabular::plaintextTopHLine(odocstream & os, row_type row,
2473 vector<unsigned int> const & clen) const
2475 idx_type const fcell = getFirstCellInRow(row);
2476 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2479 for (idx_type i = fcell; i < n; ++i) {
2489 for (idx_type i = fcell; i < n; ++i) {
2500 col_type column = cellColumn(i);
2501 int len = clen[column];
2502 while (column < column_info.size() - 1
2503 && isPartOfMultiColumn(row, ++column))
2504 len += clen[column] + 4;
2505 os << docstring(len, ch);
2520 bool Tabular::plaintextBottomHLine(odocstream & os, row_type row,
2521 vector<unsigned int> const & clen) const
2523 idx_type const fcell = getFirstCellInRow(row);
2524 idx_type const n = numberOfCellsInRow(fcell) + fcell;
2527 for (idx_type i = fcell; i < n; ++i) {
2528 if (bottomLine(i)) {
2537 for (idx_type i = fcell; i < n; ++i) {
2538 if (bottomLine(i)) {
2548 col_type column = cellColumn(i);
2549 int len = clen[column];
2550 while (column < column_info.size() -1
2551 && isPartOfMultiColumn(row, ++column))
2552 len += clen[column] + 4;
2553 os << docstring(len, ch);
2554 if (bottomLine(i)) {
2568 void Tabular::plaintextPrintCell(odocstream & os,
2569 OutputParams const & runparams,
2570 idx_type cell, row_type row, col_type column,
2571 vector<unsigned int> const & clen,
2572 bool onlydata) const
2574 odocstringstream sstr;
2575 cellInset(cell)->plaintext(sstr, runparams);
2587 unsigned int len1 = sstr.str().length();
2588 unsigned int len2 = clen[column];
2589 while (column < column_info.size() -1
2590 && isPartOfMultiColumn(row, ++column))
2591 len2 += clen[column] + 4;
2594 switch (getAlignment(cell)) {
2596 case LYX_ALIGN_LEFT:
2599 case LYX_ALIGN_RIGHT:
2603 case LYX_ALIGN_CENTER:
2609 os << docstring(len1, ' ') << sstr.str()
2610 << docstring(len2, ' ');
2612 if (rightLine(cell))
2619 void Tabular::plaintext(odocstream & os,
2620 OutputParams const & runparams, int const depth,
2621 bool onlydata, char_type delim) const
2623 // first calculate the width of the single columns
2624 vector<unsigned int> clen(column_info.size());
2627 // first all non (real) multicolumn cells!
2628 for (col_type j = 0; j < column_info.size(); ++j) {
2630 for (row_type i = 0; i < row_info.size(); ++i) {
2631 idx_type cell = cellIndex(i, j);
2632 if (isMultiColumnReal(cell))
2634 odocstringstream sstr;
2635 cellInset(cell)->plaintext(sstr, runparams);
2636 if (clen[j] < sstr.str().length())
2637 clen[j] = sstr.str().length();
2640 // then all (real) multicolumn cells!
2641 for (col_type j = 0; j < column_info.size(); ++j) {
2642 for (row_type i = 0; i < row_info.size(); ++i) {
2643 idx_type cell = cellIndex(i, j);
2644 if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
2646 odocstringstream sstr;
2647 cellInset(cell)->plaintext(sstr, runparams);
2648 int len = int(sstr.str().length());
2649 idx_type const n = columnSpan(cell);
2650 for (col_type k = j; len > 0 && k < j + n - 1; ++k)
2652 if (len > int(clen[j + n - 1]))
2653 clen[j + n - 1] = len;
2658 for (row_type i = 0; i < row_info.size(); ++i) {
2659 if (!onlydata && plaintextTopHLine(os, i, clen))
2660 os << docstring(depth * 2, ' ');
2661 for (col_type j = 0; j < column_info.size(); ++j) {
2662 if (isPartOfMultiColumn(i, j))
2664 if (onlydata && j > 0)
2665 // we don't use operator<< for single UCS4 character.
2666 // see explanation in docstream.h
2668 plaintextPrintCell(os, runparams, cell, i, j, clen, onlydata);
2673 os << docstring(depth * 2, ' ');
2674 if (plaintextBottomHLine(os, i, clen))
2675 os << docstring(depth * 2, ' ');
2681 shared_ptr<InsetTableCell> Tabular::cellInset(idx_type cell) const
2683 return cell_info[cellRow(cell)][cellColumn(cell)].inset;
2687 shared_ptr<InsetTableCell> Tabular::cellInset(row_type row,
2688 col_type column) const
2690 return cell_info[row][column].inset;
2694 void Tabular::setCellInset(row_type row, col_type column,
2695 shared_ptr<InsetTableCell> ins) const
2697 CellData & cd = cell_info[row][column];
2702 void Tabular::validate(LaTeXFeatures & features) const
2704 features.require("NeedTabularnewline");
2706 features.require("booktabs");
2707 if (is_long_tabular)
2708 features.require("longtable");
2710 features.require("rotating");
2711 for (idx_type cell = 0; cell < numberofcells; ++cell) {
2712 if (getVAlignment(cell) != LYX_VALIGN_TOP
2713 || !getPWidth(cell).zero())
2714 features.require("array");
2715 cellInset(cell)->validate(features);
2720 Tabular::BoxType Tabular::useParbox(idx_type cell) const
2722 ParagraphList const & parlist = cellInset(cell)->paragraphs();
2723 ParagraphList::const_iterator cit = parlist.begin();
2724 ParagraphList::const_iterator end = parlist.end();
2726 for (; cit != end; ++cit)
2727 for (int i = 0; i < cit->size(); ++i)
2728 if (cit->isNewline(i))
2735 /////////////////////////////////////////////////////////////////////
2739 /////////////////////////////////////////////////////////////////////
2741 InsetTableCell::InsetTableCell(Buffer & buf)
2742 : InsetText(buf), isFixedWidth(false),
2743 contentAlign(LYX_ALIGN_CENTER)
2747 bool InsetTableCell::forcePlainLayout(idx_type) const
2749 return !isFixedWidth;
2753 bool InsetTableCell::allowParagraphCustomization(idx_type) const
2755 return isFixedWidth;
2759 bool InsetTableCell::getStatus(Cursor & cur, FuncRequest const & cmd,
2760 FuncStatus & status) const
2763 switch (cmd.action) {
2765 enabled = !forcePlainLayout();
2767 case LFUN_LAYOUT_PARAGRAPH:
2768 enabled = allowParagraphCustomization();
2771 return InsetText::getStatus(cur, cmd, status);
2773 status.setEnabled(enabled);
2777 docstring InsetTableCell::asString(bool intoInsets)
2780 if (paragraphs().empty())
2782 ParagraphList::const_iterator it = paragraphs().begin();
2783 ParagraphList::const_iterator en = paragraphs().end();
2785 for (; it != en; ++it) {
2790 retval += it->asString(intoInsets ? AS_STR_INSETS : AS_STR_NONE);
2797 /////////////////////////////////////////////////////////////////////
2801 /////////////////////////////////////////////////////////////////////
2803 InsetTabular::InsetTabular(Buffer & buf, row_type rows,
2805 : tabular(buf, max(rows, row_type(1)), max(columns, col_type(1))), scx_(0),
2806 rowselect_(false), colselect_(false)
2808 setBuffer(buf); // FIXME: remove later
2812 InsetTabular::InsetTabular(InsetTabular const & tab)
2813 : Inset(tab), tabular(tab.tabular), scx_(0)
2818 InsetTabular::~InsetTabular()
2820 hideDialogs("tabular", this);
2824 void InsetTabular::setBuffer(Buffer & buf)
2826 tabular.setBuffer(buf);
2827 Inset::setBuffer(buf);
2831 bool InsetTabular::insetAllowed(InsetCode code) const
2833 if (code == MATHMACRO_CODE)
2840 void InsetTabular::write(ostream & os) const
2842 os << "Tabular" << endl;
2847 docstring InsetTabular::contextMenu(BufferView const &, int, int) const
2849 // FIXME: depending on the selection state, we could offer a different menu.
2850 return from_ascii("context-tabular");
2854 void InsetTabular::read(Lexer & lex)
2856 //bool const old_format = (lex.getString() == "\\LyXTable");
2864 string token = lex.getString();
2865 while (lex && token != "\\end_inset") {
2867 token = lex.getString();
2870 lex.printError("Missing \\end_inset at this point. ");
2874 int InsetTabular::rowFromY(Cursor & cur, int y) const
2876 // top y coordinate of tabular
2877 int h = yo(cur.bv()) - tabular.rowAscent(0);
2878 size_t nrows = tabular.row_info.size();
2880 for (; r < nrows && y > h; ++r) {
2881 h += tabular.rowAscent(r);
2882 h += tabular.rowDescent(r);
2883 h += tabular.getAdditionalHeight(r);
2889 int InsetTabular::columnFromX(Cursor & cur, int x) const
2891 // left x coordinate of tabular
2892 int w = xo(cur.bv()) + ADD_TO_TABULAR_WIDTH;
2893 size_t ncols = tabular.column_info.size();
2895 for (; c < ncols && x > w; ++c)
2896 w += tabular.columnWidth(c);
2901 void InsetTabular::metrics(MetricsInfo & mi, Dimension & dim) const
2903 //lyxerr << "InsetTabular::metrics: " << mi.base.bv << " width: " <<
2904 // mi.base.textwidth << "\n";
2907 LASSERT(false, /**/);
2911 for (idx_type cell = 0; i < tabular.row_info.size(); ++i) {
2914 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2915 if (tabular.isPartOfMultiColumn(i, j))
2916 // Multicolumn cell, but not first one
2921 if (tabular.cell_info[i][j].multicolumn ==
2922 Tabular::CELL_BEGIN_OF_MULTICOLUMN)
2923 p_width = tabular.cellInfo(cell).p_width;
2925 p_width = tabular.column_info[j].p_width;
2926 if (!p_width.zero())
2927 m.base.textwidth = p_width.inPixels(mi.base.textwidth);
2928 tabular.cellInset(cell)->metrics(m, dim);
2929 if (!p_width.zero())
2930 dim.wid = m.base.textwidth;
2931 tabular.setCellWidth(cell, dim.wid);
2932 maxAsc = max(maxAsc, dim.asc);
2933 maxDesc = max(maxDesc, dim.des);
2936 int const top_space = tabular.row_info[i].top_space_default ?
2937 default_line_space :
2938 tabular.row_info[i].top_space.inPixels(mi.base.textwidth);
2939 tabular.setRowAscent(i, maxAsc + ADD_TO_HEIGHT + top_space);
2940 int const bottom_space = tabular.row_info[i].bottom_space_default ?
2941 default_line_space :
2942 tabular.row_info[i].bottom_space.inPixels(mi.base.textwidth);
2943 tabular.setRowDescent(i, maxDesc + ADD_TO_HEIGHT + bottom_space);
2945 tabular.updateColumnWidths();
2946 dim.asc = tabular.rowAscent(0);
2947 dim.des = tabular.height() - dim.asc;
2948 dim.wid = tabular.width() + 2 * ADD_TO_TABULAR_WIDTH;
2952 void InsetTabular::draw(PainterInfo & pi, int x, int y) const
2954 //lyxerr << "InsetTabular::draw: " << x << " " << y << endl;
2955 BufferView * bv = pi.base.bv;
2957 // FIXME: As the full backrgound is painted in drawSelection(),
2958 // we have no choice but to do a full repaint for the Text cells.
2959 pi.full_repaint = true;
2961 resetPos(bv->cursor());
2964 x += ADD_TO_TABULAR_WIDTH;
2966 bool const original_drawing_state = pi.pain.isDrawingEnabled();
2969 first_visible_cell = Tabular::npos;
2970 for (row_type i = 0; i < tabular.row_info.size(); ++i) {
2972 int const a = tabular.rowAscent(i);
2973 int const d = tabular.rowDescent(i);
2974 idx = tabular.cellIndex(i, 0);
2975 for (col_type j = 0; j < tabular.column_info.size(); ++j) {
2976 if (tabular.isPartOfMultiColumn(i, j))
2978 if (first_visible_cell == Tabular::npos)
2979 first_visible_cell = idx;
2981 int const cx = nx + tabular.getBeginningOfTextInCell(idx);
2982 // Cache the Inset position.
2983 bv->coordCache().insets().add(cell(idx).get(), cx, y);
2984 if (nx + tabular.columnWidth(idx) < 0
2985 || nx > bv->workWidth()
2987 || y - a > bv->workHeight()) {
2988 pi.pain.setDrawingEnabled(false);
2989 cell(idx)->draw(pi, cx, y);
2990 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2991 pi.pain.setDrawingEnabled(original_drawing_state);
2993 cell(idx)->draw(pi, cx, y);
2994 drawCellLines(pi.pain, nx, y, i, idx, pi.erased_);
2996 nx += tabular.columnWidth(idx);
3000 if (i + 1 < tabular.row_info.size())
3001 y += d + tabular.rowAscent(i + 1) +
3002 tabular.getAdditionalHeight(i + 1);
3007 void InsetTabular::drawSelection(PainterInfo & pi, int x, int y) const
3009 Cursor & cur = pi.base.bv->cursor();
3011 x += scx_ + ADD_TO_TABULAR_WIDTH;
3013 // FIXME: it is wrong to completely paint the background
3014 // if we want to do single row painting.
3016 // Paint background of current tabular
3017 int const w = tabular.width();
3018 int const h = tabular.height();
3019 int yy = y - tabular.rowAscent(0);
3020 pi.pain.fillRectangle(x, yy, w, h, backgroundColor());
3022 if (!cur.selection())
3024 if (&cur.inset() != this)
3030 if (cur.selIsMultiCell()) {
3033 getSelection(cur, rs, re, cs, ce);
3034 y -= tabular.rowAscent(0);
3035 for (row_type j = 0; j < tabular.row_info.size(); ++j) {
3036 int const a = tabular.rowAscent(j);
3037 int const h = a + tabular.rowDescent(j);
3039 y += tabular.getAdditionalHeight(j);
3040 for (col_type i = 0; i < tabular.column_info.size(); ++i) {
3041 if (tabular.isPartOfMultiColumn(j, i))
3043 idx_type const cell =
3044 tabular.cellIndex(j, i);
3045 int const w = tabular.columnWidth(cell);
3046 if (i >= cs && i <= ce && j >= rs && j <= re)
3047 pi.pain.fillRectangle(xx, y, w, h,
3055 x += cellXPos(cur.idx());
3056 x += tabular.getBeginningOfTextInCell(cur.idx());
3057 cell(cur.idx())->drawSelection(pi, x, 0 /* ignored */);
3062 void InsetTabular::drawCellLines(Painter & pain, int x, int y,
3063 row_type row, idx_type cell, bool erased) const
3065 int x2 = x + tabular.columnWidth(cell);
3066 bool on_off = false;
3067 ColorCode col = Color_tabularline;
3068 ColorCode onoffcol = Color_tabularonoffline;
3071 col = Color_deletedtext;
3072 onoffcol = Color_deletedtext;
3075 if (!tabular.topAlreadyDrawn(cell)) {
3076 on_off = !tabular.topLine(cell);
3077 pain.line(x, y - tabular.rowAscent(row),
3078 x2, y - tabular.rowAscent(row),
3079 on_off ? onoffcol : col,
3080 on_off ? Painter::line_onoffdash : Painter::line_solid);
3082 on_off = !tabular.bottomLine(cell);
3083 pain.line(x, y + tabular.rowDescent(row),
3084 x2, y + tabular.rowDescent(row),
3085 on_off ? onoffcol : col,
3086 on_off ? Painter::line_onoffdash : Painter::line_solid);
3087 if (!tabular.leftAlreadyDrawn(cell)) {
3088 on_off = !tabular.leftLine(cell);
3089 pain.line(x, y - tabular.rowAscent(row),
3090 x, y + tabular.rowDescent(row),
3091 on_off ? onoffcol : col,
3092 on_off ? Painter::line_onoffdash : Painter::line_solid);
3094 on_off = !tabular.rightLine(cell);
3095 pain.line(x2 - tabular.getAdditionalWidth(cell),
3096 y - tabular.rowAscent(row),
3097 x2 - tabular.getAdditionalWidth(cell),
3098 y + tabular.rowDescent(row),
3099 on_off ? onoffcol : col,
3100 on_off ? Painter::line_onoffdash : Painter::line_solid);
3104 docstring InsetTabular::editMessage() const
3106 return _("Opened table");
3110 void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
3112 //lyxerr << "InsetTabular::edit: " << this << endl;
3114 cur.selection() = false;
3117 if (isRightToLeft(cur))
3118 cur.idx() = tabular.getLastCellInRow(0);
3124 if (isRightToLeft(cur))
3125 cur.idx() = tabular.getFirstCellInRow(tabular.row_info.size() - 1);
3127 cur.idx() = tabular.numberofcells - 1;
3129 cur.pos() = cur.lastpos(); // FIXME crude guess
3131 // FIXME: this accesses the position cache before it is initialized
3133 //cur.bv().fitCursor();
3137 void InsetTabular::updateLabels(ParIterator const & it)
3139 // In a longtable, tell captions what the current float is
3140 Counters & cnts = buffer().params().documentClass().counters();
3141 string const saveflt = cnts.current_float();
3142 if (tabular.is_long_tabular)
3143 cnts.current_float("table");
3145 ParIterator it2 = it;
3147 size_t const end = it2.nargs();
3148 for ( ; it2.idx() < end; it2.top().forwardIdx())
3149 lyx::updateLabels(buffer(), it2);
3152 if (tabular.is_long_tabular)
3153 cnts.current_float(saveflt);
3157 void InsetTabular::doDispatch(Cursor & cur, FuncRequest & cmd)
3159 LYXERR(Debug::DEBUG, "# InsetTabular::doDispatch: cmd: " << cmd
3160 << "\n cur:" << cur);
3161 CursorSlice sl = cur.top();
3162 Cursor & bvcur = cur.bv().cursor();
3164 switch (cmd.action) {
3166 case LFUN_MOUSE_PRESS: {
3167 //lyxerr << "# InsetTabular::MousePress\n" << cur.bv().cursor() << endl;
3169 if (cmd.x < xo(cur.bv()) + ADD_TO_TABULAR_WIDTH
3170 || cmd.x > xo(cur.bv()) + tabular.width()) {
3171 row_type r = rowFromY(cur, cmd.y);
3172 cur.idx() = tabular.getFirstCellInRow(r);
3175 cur.idx() = tabular.getLastCellInRow(r);
3176 cur.pos() = cur.lastpos();
3177 cur.selection() = true;
3183 int const y0 = yo(cur.bv()) - tabular.rowAscent(0);
3184 if (cmd.y < y0 + ADD_TO_TABULAR_WIDTH
3185 || cmd.y > y0 + tabular.height()) {
3186 col_type c = columnFromX(cur, cmd.x);
3187 cur.idx() = tabular.cellIndex(0, c);
3190 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3191 cur.pos() = cur.lastpos();
3192 cur.selection() = true;
3197 // do not reset cursor/selection if we have selected
3198 // some cells (bug 2715).
3199 if (cmd.button() == mouse_button::button3
3200 && &bvcur.selBegin().inset() == this
3201 && bvcur.selIsMultiCell())
3204 // Let InsetTableCell do it
3205 cell(cur.idx())->dispatch(cur, cmd);
3208 case LFUN_MOUSE_MOTION:
3209 //lyxerr << "# InsetTabular::MouseMotion\n" << bvcur << endl;
3210 if (cmd.button() == mouse_button::button1) {
3211 // only accept motions to places not deeper nested than the real anchor
3212 if (!bvcur.anchor_.hasPart(cur)) {
3216 // select (additional) row
3218 row_type r = rowFromY(cur, cmd.y);
3219 cur.idx() = tabular.getLastCellInRow(r);
3220 // we need to reset the cursor's pit and pos now, as the old ones
3221 // may no longer be valid.
3224 bvcur.setCursor(cur);
3225 bvcur.selection() = true;
3228 // select (additional) column
3230 col_type c = columnFromX(cur, cmd.x);
3231 cur.idx() = tabular.cellIndex(tabular.row_info.size() - 1, c);
3232 // we need to reset the cursor's pit and pos now, as the old ones
3233 // may no longer be valid.
3236 bvcur.setCursor(cur);
3237 bvcur.selection() = true;
3240 // only update if selection changes
3241 if (bvcur.idx() == cur.idx() &&
3242 !(bvcur.anchor_.idx() == cur.idx() && bvcur.pos() != cur.pos()))
3244 setCursorFromCoordinates(cur, cmd.x, cmd.y);
3245 bvcur.setCursor(cur);
3246 bvcur.selection() = true;
3247 // if this is a multicell selection, we just set the cursor to
3248 // the beginning of the cell's text.
3249 if (bvcur.selIsMultiCell()) {
3256 case LFUN_MOUSE_RELEASE:
3261 case LFUN_CELL_BACKWARD:
3263 cur.selection() = false;
3266 case LFUN_CELL_FORWARD:
3268 cur.selection() = false;
3271 case LFUN_CHAR_FORWARD_SELECT:
3272 case LFUN_CHAR_FORWARD:
3273 case LFUN_CHAR_BACKWARD_SELECT:
3274 case LFUN_CHAR_BACKWARD:
3275 case LFUN_CHAR_RIGHT_SELECT:
3276 case LFUN_CHAR_RIGHT:
3277 case LFUN_CHAR_LEFT_SELECT:
3278 case LFUN_CHAR_LEFT: {
3279 // determine whether we move to next or previous cell, where to enter
3280 // the new cell from, and which command to "finish" (i.e., exit the
3283 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE;
3284 FuncCode finish_lfun;
3286 if (cmd.action == LFUN_CHAR_FORWARD
3287 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
3289 finish_lfun = LFUN_FINISHED_FORWARD;
3291 else if (cmd.action == LFUN_CHAR_BACKWARD
3292 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
3294 finish_lfun = LFUN_FINISHED_BACKWARD;
3296 // LEFT or RIGHT commands --- the interpretation will depend on the
3297 // table's direction.
3299 bool right = (cmd.action == LFUN_CHAR_RIGHT
3300 || cmd.action == LFUN_CHAR_RIGHT_SELECT);
3301 next_cell = (isRightToLeft(cur) != right);
3303 if (lyxrc.visual_cursor) {
3304 entry_from = right ? ENTRY_DIRECTION_LEFT:ENTRY_DIRECTION_RIGHT;
3308 finish_lfun = LFUN_FINISHED_RIGHT;
3310 finish_lfun = LFUN_FINISHED_LEFT;
3313 // if we don't have a multicell selection...
3314 if (!cur.selIsMultiCell() ||
3315 // ...or we're not doing some LFUN_*_SELECT thing, anyway...
3316 (cmd.action != LFUN_CHAR_FORWARD_SELECT &&
3317 cmd.action != LFUN_CHAR_BACKWARD_SELECT &&
3318 cmd.action != LFUN_CHAR_RIGHT_SELECT &&
3319 cmd.action != LFUN_CHAR_LEFT_SELECT)) {
3320 // ...try to dispatch to the cell's inset.
3321 cell(cur.idx())->dispatch(cur, cmd);
3322 if (cur.result().dispatched())
3325 // move to next/prev cell, as appropriate
3326 // note that we will always do this if we're selecting and we have
3327 // a multicell selection
3328 LYXERR(Debug::RTL, "entering " << (next_cell ? "next" : "previous")
3329 << " cell from: " << int(entry_from));
3331 moveNextCell(cur, entry_from);
3333 movePrevCell(cur, entry_from);
3334 // if we're exiting the table, call the appropriate FINISHED lfun
3335 if (sl == cur.top())
3336 cmd = FuncRequest(finish_lfun);
3343 case LFUN_DOWN_SELECT:
3345 cell(cur.idx())->dispatch(cur, cmd);
3346 cur.dispatched(); // override the cell's decision
3347 if (sl == cur.top())
3348 // if our Text didn't do anything to the cursor
3349 // then we try to put the cursor into the cell below
3350 // setting also the right targetX.
3351 if (tabular.cellRow(cur.idx()) != tabular.row_info.size() - 1) {
3352 cur.idx() = tabular.cellBelow(cur.idx());
3354 TextMetrics const & tm =
3355 cur.bv().textMetrics(cell(cur.idx())->getText(0));
3356 cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX());
3358 if (sl == cur.top()) {
3359 // we trick it to go to forward after leaving the
3361 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
3366 case LFUN_UP_SELECT:
3368 cell(cur.idx())->dispatch(cur, cmd);
3369 cur.dispatched(); // override the cell's decision
3370 if (sl == cur.top())
3371 // if our Text didn't do anything to the cursor
3372 // then we try to put the cursor into the cell above
3373 // setting also the right targetX.
3374 if (tabular.cellRow(cur.idx()) != 0) {
3375 cur.idx() = tabular.cellAbove(cur.idx());
3376 cur.pit() = cur.lastpit();
3377 Text const * text = cell(cur.idx())->getText(0);
3378 TextMetrics const & tm = cur.bv().textMetrics(text);
3379 ParagraphMetrics const & pm =
3380 tm.parMetrics(cur.lastpit());
3381 cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX());
3383 if (sl == cur.top()) {
3384 cmd = FuncRequest(LFUN_UP);
3389 // case LFUN_SCREEN_DOWN: {
3390 // //if (hasSelection())
3391 // // cur.selection() = false;
3392 // col_type const col = tabular.cellColumn(cur.idx());
3393 // int const t = cur.bv().top_y() + cur.bv().height();
3394 // if (t < yo() + tabular.getHeightOfTabular()) {
3395 // cur.bv().scrollDocView(t);
3396 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3398 // cur.idx() = tabular.getFirstCellInRow(tabular.rows() - 1) + col;
3405 // case LFUN_SCREEN_UP: {
3406 // //if (hasSelection())
3407 // // cur.selection() = false;
3408 // col_type const col = tabular.cellColumn(cur.idx());
3409 // int const t = cur.bv().top_y() + cur.bv().height();
3411 // cur.bv().scrollDocView(t);
3415 // cur.idx() = tabular.cellBelow(first_visible_cell) + col;
3419 // cur.par() = cur.lastpar();
3420 // cur.pos() = cur.lastpos();
3424 case LFUN_LAYOUT_TABULAR:
3425 cur.bv().showDialog("tabular", params2string(*this), this);
3428 case LFUN_INSET_DIALOG_UPDATE:
3429 cur.bv().updateDialog("tabular", params2string(*this));
3432 case LFUN_TABULAR_FEATURE:
3433 if (!tabularFeatures(cur, to_utf8(cmd.argument())))
3437 // insert file functions
3438 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
3439 case LFUN_FILE_INSERT_PLAINTEXT: {
3441 docstring const tmpstr = cur.bv().contentsOfPlaintextFile(
3442 FileName(to_utf8(cmd.argument())));
3445 cur.recordUndoInset(INSERT_UNDO);
3446 if (insertPlaintextString(cur.bv(), tmpstr, false)) {
3447 // content has been replaced,
3448 // so cursor might be invalid
3449 cur.pos() = cur.lastpos();
3450 cur.pit() = cur.lastpit();
3451 bvcur.setCursor(cur);
3458 if (cur.selIsMultiCell()) {
3459 if (copySelection(cur)) {
3460 cur.recordUndoInset(DELETE_UNDO);
3465 cell(cur.idx())->dispatch(cur, cmd);
3468 case LFUN_CHAR_DELETE_BACKWARD:
3469 case LFUN_CHAR_DELETE_FORWARD:
3470 if (cur.selIsMultiCell()) {
3471 cur.recordUndoInset(DELETE_UNDO);
3475 cell(cur.idx())->dispatch(cur, cmd);
3479 if (!cur.selection())
3481 if (cur.selIsMultiCell()) {
3485 cell(cur.idx())->dispatch(cur, cmd);
3488 case LFUN_CLIPBOARD_PASTE:
3489 case LFUN_PRIMARY_SELECTION_PASTE: {
3490 docstring const clip = (cmd.action == LFUN_CLIPBOARD_PASTE) ?
3491 theClipboard().getAsText() :
3492 theSelection().get();
3495 // pass to InsertPlaintextString, but
3496 // only if we have multi-cell content
3497 if (clip.find_first_of(from_ascii("\t\n")) != docstring::npos) {
3498 cur.recordUndoInset(INSERT_UNDO);
3499 if (insertPlaintextString(cur.bv(), clip, false)) {
3500 // content has been replaced,
3501 // so cursor might be invalid
3502 cur.pos() = cur.lastpos();
3503 cur.pit() = cur.lastpit();
3504 bvcur.setCursor(cur);
3508 // Let the cell handle normal text
3509 cell(cur.idx())->dispatch(cur, cmd);
3514 if (!tabularStackDirty()) {
3515 cell(cur.idx())->dispatch(cur, cmd);
3518 if (theClipboard().isInternal() ||
3519 !theClipboard().hasInternal() && theClipboard().hasLyXContents()) {
3520 cur.recordUndoInset(INSERT_UNDO);
3521 pasteClipboard(cur);
3525 case LFUN_FONT_EMPH:
3526 case LFUN_FONT_BOLD:
3527 case LFUN_FONT_ROMAN:
3528 case LFUN_FONT_NOUN:
3529 case LFUN_FONT_ITAL:
3530 case LFUN_FONT_FRAK:
3531 case LFUN_FONT_TYPEWRITER:
3532 case LFUN_FONT_SANS:
3533 case LFUN_TEXTSTYLE_APPLY:
3534 case LFUN_TEXTSTYLE_UPDATE:
3535 case LFUN_FONT_SIZE:
3536 case LFUN_FONT_UNDERLINE:
3538 case LFUN_WORD_CAPITALIZE:
3539 case LFUN_WORD_UPCASE:
3540 case LFUN_WORD_LOWCASE:
3541 case LFUN_CHARS_TRANSPOSE:
3542 if (cur.selIsMultiCell()) {
3545 getSelection(cur, rs, re, cs, ce);
3546 Cursor tmpcur = cur;
3547 for (row_type i = rs; i <= re; ++i) {
3548 for (col_type j = cs; j <= ce; ++j) {
3549 // cursor follows cell:
3550 tmpcur.idx() = tabular.cellIndex(i, j);
3551 // select this cell only:
3554 tmpcur.resetAnchor();
3555 tmpcur.pit() = tmpcur.lastpit();
3556 tmpcur.pos() = tmpcur.top().lastpos();
3557 tmpcur.setCursor(tmpcur);
3558 tmpcur.setSelection();
3559 cell(tmpcur.idx())->dispatch(tmpcur, cmd);
3564 cell(cur.idx())->dispatch(cur, cmd);
3568 // we try to handle this event in the insets dispatch function.
3569 cell(cur.idx())->dispatch(cur, cmd);
3575 // function sets an object as defined in func_status.h:
3576 // states OK, Unknown, Disabled, On, Off.
3577 bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
3578 FuncStatus & status) const
3580 switch (cmd.action) {
3581 case LFUN_TABULAR_FEATURE: {
3582 int action = Tabular::LAST_ACTION;
3584 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
3585 string const tmp = tabularFeature[i].feature;
3586 if (tmp == to_utf8(cmd.argument()).substr(0, tmp.length())) {
3587 action = tabularFeature[i].action;
3591 if (action == Tabular::LAST_ACTION) {
3593 status.unknown(true);
3597 string const argument
3598 = ltrim(to_utf8(cmd.argument()).substr(tabularFeature[i].feature.length()));
3600 row_type sel_row_start = 0;
3601 row_type sel_row_end = 0;
3602 col_type sel_col_start = 0;
3603 col_type sel_col_end = 0;
3604 Tabular::ltType dummyltt;
3607 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
3610 case Tabular::SET_PWIDTH:
3611 case Tabular::SET_MPWIDTH:
3612 case Tabular::SET_SPECIAL_COLUMN:
3613 case Tabular::SET_SPECIAL_MULTI:
3614 case Tabular::APPEND_ROW:
3615 case Tabular::APPEND_COLUMN:
3616 case Tabular::DELETE_ROW:
3617 case Tabular::DELETE_COLUMN:
3618 case Tabular::COPY_ROW:
3619 case Tabular::COPY_COLUMN:
3620 case Tabular::SET_TOP_SPACE:
3621 case Tabular::SET_BOTTOM_SPACE:
3622 case Tabular::SET_INTERLINE_SPACE:
3626 case Tabular::MULTICOLUMN:
3627 status.setEnabled(sel_row_start == sel_row_end);
3628 status.setOnOff(tabular.isMultiColumn(cur.idx()));
3631 case Tabular::SET_ALL_LINES:
3632 case Tabular::UNSET_ALL_LINES:
3633 case Tabular::SET_BORDER_LINES:
3634 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3637 case Tabular::TOGGLE_LINE_TOP:
3638 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3639 status.setOnOff(tabular.topLine(cur.idx()));
3642 case Tabular::TOGGLE_LINE_BOTTOM:
3643 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3644 status.setOnOff(tabular.bottomLine(cur.idx()));
3647 case Tabular::TOGGLE_LINE_LEFT:
3648 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3649 status.setOnOff(tabular.leftLine(cur.idx()));
3652 case Tabular::TOGGLE_LINE_RIGHT:
3653 status.setEnabled(!tabular.ltCaption(tabular.cellRow(cur.idx())));
3654 status.setOnOff(tabular.rightLine(cur.idx()));
3657 case Tabular::M_ALIGN_LEFT:
3659 case Tabular::ALIGN_LEFT:
3660 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_LEFT);
3663 case Tabular::M_ALIGN_RIGHT:
3665 case Tabular::ALIGN_RIGHT:
3666 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_RIGHT);
3669 case Tabular::M_ALIGN_CENTER:
3671 case Tabular::ALIGN_CENTER:
3672 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_CENTER);
3675 case Tabular::ALIGN_BLOCK:
3676 status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
3677 status.setOnOff(tabular.getAlignment(cur.idx(), flag) == LYX_ALIGN_BLOCK);
3680 case Tabular::M_VALIGN_TOP:
3682 case Tabular::VALIGN_TOP:
3684 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
3687 case Tabular::M_VALIGN_BOTTOM:
3689 case Tabular::VALIGN_BOTTOM:
3691 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
3694 case Tabular::M_VALIGN_MIDDLE:
3696 case Tabular::VALIGN_MIDDLE:
3698 tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
3701 case Tabular::SET_LONGTABULAR:
3702 status.setOnOff(tabular.is_long_tabular);
3705 case Tabular::UNSET_LONGTABULAR:
3706 status.setOnOff(!tabular.is_long_tabular);
3709 case Tabular::TOGGLE_ROTATE_TABULAR:
3710 case Tabular::SET_ROTATE_TABULAR:
3711 status.setOnOff(tabular.rotate);
3714 case Tabular::UNSET_ROTATE_TABULAR:
3715 status.setOnOff(!tabular.rotate);
3718 case Tabular::TOGGLE_ROTATE_CELL:
3719 case Tabular::SET_ROTATE_CELL:
3720 status.setOnOff(!oneCellHasRotationState(false,
3721 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3724 case Tabular::UNSET_ROTATE_CELL:
3725 status.setOnOff(!oneCellHasRotationState(true,
3726 sel_row_start, sel_row_end, sel_col_start, sel_col_end));
3729 case Tabular::SET_USEBOX:
3730 status.setOnOff(convert<int>(argument) == tabular.getUsebox(cur.idx()));
3733 case Tabular::SET_LTFIRSTHEAD:
3734 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3737 case Tabular::UNSET_LTFIRSTHEAD:
3738 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3741 case Tabular::SET_LTHEAD:
3742 status.setOnOff(tabular.getRowOfLTHead(sel_row_start, dummyltt));
3745 case Tabular::UNSET_LTHEAD:
3746 status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
3749 case Tabular::SET_LTFOOT:
3750 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3753 case Tabular::UNSET_LTFOOT:
3754 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3757 case Tabular::SET_LTLASTFOOT:
3758 status.setOnOff(tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3761 case Tabular::UNSET_LTLASTFOOT:
3762 status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
3765 case Tabular::SET_LTNEWPAGE:
3766 status.setOnOff(tabular.getLTNewPage(sel_row_start));
3769 case Tabular::TOGGLE_LTCAPTION:
3770 status.setEnabled(sel_row_start == sel_row_end);
3771 status.setOnOff(tabular.ltCaption(sel_row_start));
3774 case Tabular::SET_BOOKTABS:
3775 status.setOnOff(tabular.use_booktabs);
3778 case Tabular::UNSET_BOOKTABS:
3779 status.setOnOff(!tabular.use_booktabs);
3784 status.setEnabled(false);
3790 // These are only enabled inside tabular
3791 case LFUN_CELL_BACKWARD:
3792 case LFUN_CELL_FORWARD:
3793 status.setEnabled(true);
3796 // disable these with multiple cells selected
3797 case LFUN_INSET_INSERT:
3798 case LFUN_TABULAR_INSERT:
3799 case LFUN_FLEX_INSERT:
3800 case LFUN_FLOAT_INSERT:
3801 case LFUN_FLOAT_WIDE_INSERT:
3802 case LFUN_FOOTNOTE_INSERT:
3803 case LFUN_MARGINALNOTE_INSERT:
3804 case LFUN_MATH_INSERT:
3805 case LFUN_MATH_MODE:
3806 case LFUN_MATH_MUTATE:
3807 case LFUN_MATH_DISPLAY:
3808 case LFUN_NOTE_INSERT:
3809 case LFUN_OPTIONAL_INSERT:
3810 case LFUN_BOX_INSERT:
3811 case LFUN_BRANCH_INSERT:
3812 case LFUN_WRAP_INSERT:
3813 case LFUN_ERT_INSERT: {
3814 if (cur.selIsMultiCell()) {
3815 status.setEnabled(false);
3818 return cell(cur.idx())->getStatus(cur, cmd, status);
3821 // disable in non-fixed-width cells
3822 case LFUN_NEWLINE_INSERT:
3823 case LFUN_BREAK_PARAGRAPH: {
3824 if (tabular.getPWidth(cur.idx()).zero()) {
3825 status.setEnabled(false);
3828 return cell(cur.idx())->getStatus(cur, cmd, status);
3831 case LFUN_NEWPAGE_INSERT:
3832 status.setEnabled(false);
3836 if (cur.selIsMultiCell()) {
3837 status.setEnabled(false);
3838 status.message(_("You cannot paste into a multicell selection."));
3841 if (tabularStackDirty() && theClipboard().isInternal()) {
3842 status.setEnabled(true);
3845 return cell(cur.idx())->getStatus(cur, cmd, status);
3847 case LFUN_INSET_MODIFY:
3848 if (insetCode(cmd.getArg(0)) == TABULAR_CODE) {
3849 status.setEnabled(true);
3855 // we try to handle this event in the insets dispatch function.
3856 return cell(cur.idx())->getStatus(cur, cmd, status);
3861 int InsetTabular::latex(odocstream & os, OutputParams const & runparams) const
3863 return tabular.latex(os, runparams);
3867 int InsetTabular::plaintext(odocstream & os, OutputParams const & runparams) const
3869 os << '\n'; // output table on a new line
3870 int const dp = runparams.linelen > 0 ? runparams.depth : 0;
3871 tabular.plaintext(os, runparams, dp, false, 0);
3872 return PLAINTEXT_NEWLINE;
3876 int InsetTabular::docbook(odocstream & os, OutputParams const & runparams) const
3881 // FIXME: Why not pass a proper DocIterator here?
3883 // if the table is inside a float it doesn't need the informaltable
3884 // wrapper. Search for it.
3885 for (master = owner(); master; master = master->owner())
3886 if (master->lyxCode() == FLOAT_CODE)
3891 os << "<informaltable>";
3894 ret += tabular.docbook(os, runparams);
3896 os << "</informaltable>";
3903 void InsetTabular::validate(LaTeXFeatures & features) const
3905 tabular.validate(features);
3909 shared_ptr<InsetTableCell const> InsetTabular::cell(idx_type idx) const
3911 return tabular.cellInset(idx);
3915 shared_ptr<InsetTableCell> InsetTabular::cell(idx_type idx)
3917 return tabular.cellInset(idx);
3921 void InsetTabular::cursorPos(BufferView const & bv,
3922 CursorSlice const & sl, bool boundary, int & x, int & y) const
3924 cell(sl.idx())->cursorPos(bv, sl, boundary, x, y);
3926 // y offset correction
3927 int const row = tabular.cellRow(sl.idx());
3928 for (int i = 0; i <= row; ++i) {
3930 y += tabular.rowAscent(i);
3931 y += tabular.getAdditionalHeight(i);
3934 y += tabular.rowDescent(i);
3937 // x offset correction
3938 int const col = tabular.cellColumn(sl.idx());
3939 int idx = tabular.cellIndex(row, 0);
3940 for (int j = 0; j < col; ++j) {
3941 if (tabular.isPartOfMultiColumn(row, j))
3943 x += tabular.columnWidth(idx);
3946 x += tabular.getBeginningOfTextInCell(idx);
3947 x += ADD_TO_TABULAR_WIDTH;
3952 int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
3956 Inset const & inset = *tabular.cellInset(cell);
3957 Point o = bv.coordCache().getInsets().xy(&inset);
3958 int const xbeg = o.x_ - tabular.getBeginningOfTextInCell(cell);
3959 int const xend = xbeg + tabular.columnWidth(cell);
3960 row_type const row = tabular.cellRow(cell);
3961 int const ybeg = o.y_ - tabular.rowAscent(row) -
3962 tabular.getAdditionalHeight(row);
3963 int const yend = o.y_ + tabular.rowDescent(row);
3975 //lyxerr << " xbeg=" << xbeg << " xend=" << xend
3976 // << " ybeg=" << ybeg << " yend=" << yend
3977 // << " xx=" << xx << " yy=" << yy
3978 // << " dist=" << xx + yy << endl;
3983 Inset * InsetTabular::editXY(Cursor & cur, int x, int y)
3985 //lyxerr << "InsetTabular::editXY: " << this << endl;
3986 cur.selection() = false;
3988 cur.idx() = getNearestCell(cur.bv(), x, y);
3990 return cur.bv().textMetrics(&cell(cur.idx())->text()).editXY(cur, x, y);
3994 void InsetTabular::setCursorFromCoordinates(Cursor & cur, int x, int y) const
3996 cur.idx() = getNearestCell(cur.bv(), x, y);
3997 cur.bv().textMetrics(&cell(cur.idx())->text()).setCursorFromCoordinates(cur, x, y);
4001 InsetTabular::idx_type InsetTabular::getNearestCell(BufferView & bv, int x, int y) const
4003 idx_type idx_min = 0;
4004 int dist_min = numeric_limits<int>::max();
4005 for (idx_type i = 0, n = nargs(); i != n; ++i) {
4006 if (bv.coordCache().getInsets().has(tabular.cellInset(i).get())) {
4007 int const d = dist(bv, i, x, y);
4018 int InsetTabular::cellXPos(idx_type const cell) const
4022 for (; !tabular.isFirstCellInRow(c); --c)
4025 for (; c < cell; ++c)
4026 lx += tabular.columnWidth(c);
4032 void InsetTabular::resetPos(Cursor & cur) const
4034 BufferView & bv = cur.bv();
4035 int const maxwidth = bv.workWidth();
4037 int const scx_old = scx_;
4038 int const i = cur.find(this);
4043 int const X2 = maxwidth;
4044 int const offset = ADD_TO_TABULAR_WIDTH + 2;
4045 int const x1 = xo(cur.bv()) + cellXPos(cur[i].idx()) + offset;
4046 int const x2 = x1 + tabular.columnWidth(cur[i].idx());
4049 scx_ = X1 + 20 - x1;
4051 scx_ = X2 - 20 - x2;
4056 // only update if offset changed
4057 if (scx_ != scx_old)
4058 cur.updateFlags(Update::Force | Update::FitCursor);
4062 void InsetTabular::moveNextCell(Cursor & cur, EntryDirection entry_from)
4064 if (isRightToLeft(cur)) {
4065 if (tabular.isFirstCellInRow(cur.idx())) {
4066 row_type const row = tabular.cellRow(cur.idx());
4067 if (row == tabular.row_info.size() - 1)
4069 cur.idx() = tabular.cellBelow(tabular.getLastCellInRow(row));
4076 if (tabular.isLastCell(cur.idx()))
4082 cur.boundary(false);
4084 // in visual mode, place cursor at extreme left or right
4086 switch(entry_from) {
4088 case ENTRY_DIRECTION_RIGHT:
4089 cur.posVisToRowExtremity(false /* !left */);
4091 case ENTRY_DIRECTION_LEFT:
4092 cur.posVisToRowExtremity(true /* left */);
4094 case ENTRY_DIRECTION_IGNORE:
4095 // nothing to do in this case
4104 void InsetTabular::movePrevCell(Cursor & cur, EntryDirection entry_from)
4106 if (isRightToLeft(cur)) {
4107 if (tabular.isLastCellInRow(cur.idx())) {
4108 row_type const row = tabular.cellRow(cur.idx());
4111 cur.idx() = tabular.getFirstCellInRow(row);
4112 cur.idx() = tabular.cellAbove(cur.idx());
4114 if (tabular.isLastCell(cur.idx()))
4119 if (cur.idx() == 0) // first cell
4123 cur.pit() = cur.lastpit();
4124 cur.pos() = cur.lastpos();
4126 // in visual mode, place cursor at extreme left or right
4128 switch(entry_from) {
4130 case ENTRY_DIRECTION_RIGHT:
4131 cur.posVisToRowExtremity(false /* !left */);
4133 case ENTRY_DIRECTION_LEFT:
4134 cur.posVisToRowExtremity(true /* left */);
4136 case ENTRY_DIRECTION_IGNORE:
4137 // nothing to do in this case
4146 bool InsetTabular::tabularFeatures(Cursor & cur, string const & what)
4148 Tabular::Feature action = Tabular::LAST_ACTION;
4151 for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
4152 string const tmp = tabularFeature[i].feature;
4154 if (tmp == what.substr(0, tmp.length())) {
4155 //if (!compare(tabularFeatures[i].feature.c_str(), what.c_str(),
4156 //tabularFeatures[i].feature.length()))
4157 action = tabularFeature[i].action;
4161 if (action == Tabular::LAST_ACTION)
4165 ltrim(what.substr(tabularFeature[i].feature.length()));
4166 tabularFeatures(cur, action, val);
4171 static void checkLongtableSpecial(Tabular::ltType & ltt,
4172 string const & special, bool & flag)
4174 if (special == "dl_above") {
4177 } else if (special == "dl_below") {
4178 ltt.bottomDL = flag;
4180 } else if (special == "empty") {
4189 bool InsetTabular::oneCellHasRotationState(bool rotated,
4190 row_type row_start, row_type row_end,
4191 col_type col_start, col_type col_end) const {
4193 for (row_type i = row_start; i <= row_end; ++i) {
4194 for (col_type j = col_start; j <= col_end; ++j) {
4195 if (tabular.getRotateCell(tabular.cellIndex(i, j))
4204 void InsetTabular::tabularFeatures(Cursor & cur,
4205 Tabular::Feature feature, string const & value)
4207 col_type sel_col_start;
4208 col_type sel_col_end;
4209 row_type sel_row_start;
4210 row_type sel_row_end;
4211 bool setLines = false;
4212 LyXAlignment setAlign = LYX_ALIGN_LEFT;
4213 Tabular::VAlignment setVAlign = Tabular::LYX_VALIGN_TOP;
4217 case Tabular::M_ALIGN_LEFT:
4218 case Tabular::ALIGN_LEFT:
4219 setAlign = LYX_ALIGN_LEFT;
4222 case Tabular::M_ALIGN_RIGHT:
4223 case Tabular::ALIGN_RIGHT:
4224 setAlign = LYX_ALIGN_RIGHT;
4227 case Tabular::M_ALIGN_CENTER:
4228 case Tabular::ALIGN_CENTER:
4229 setAlign = LYX_ALIGN_CENTER;
4232 case Tabular::ALIGN_BLOCK:
4233 setAlign = LYX_ALIGN_BLOCK;
4236 case Tabular::M_VALIGN_TOP:
4237 case Tabular::VALIGN_TOP:
4238 setVAlign = Tabular::LYX_VALIGN_TOP;
4241 case Tabular::M_VALIGN_BOTTOM:
4242 case Tabular::VALIGN_BOTTOM:
4243 setVAlign = Tabular::LYX_VALIGN_BOTTOM;
4246 case Tabular::M_VALIGN_MIDDLE:
4247 case Tabular::VALIGN_MIDDLE:
4248 setVAlign = Tabular::LYX_VALIGN_MIDDLE;
4255 cur.recordUndoInset(ATOMIC_UNDO);
4257 getSelection(cur, sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4258 row_type const row = tabular.cellRow(cur.idx());
4259 col_type const column = tabular.cellColumn(cur.idx());
4261 Tabular::ltType ltt;
4265 case Tabular::SET_PWIDTH: {
4266 Length const len(value);
4267 tabular.setColumnPWidth(cur, cur.idx(), len);
4269 && tabular.getAlignment(cur.idx(), true) == LYX_ALIGN_BLOCK)
4270 tabularFeatures(cur, Tabular::ALIGN_CENTER, string());
4274 case Tabular::SET_MPWIDTH:
4275 tabular.setMColumnPWidth(cur, cur.idx(), Length(value));
4278 case Tabular::SET_SPECIAL_COLUMN:
4279 case Tabular::SET_SPECIAL_MULTI:
4280 tabular.setAlignSpecial(cur.idx(), from_utf8(value), feature);
4283 case Tabular::APPEND_ROW:
4284 // append the row into the tabular
4285 tabular.appendRow(cur.idx());
4288 case Tabular::APPEND_COLUMN:
4289 // append the column into the tabular
4290 tabular.appendColumn(cur.idx());
4291 cur.idx() = tabular.cellIndex(row, column);
4294 case Tabular::DELETE_ROW:
4295 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4296 tabular.deleteRow(sel_row_start);
4297 if (sel_row_start >= tabular.row_info.size())
4299 cur.idx() = tabular.cellIndex(sel_row_start, column);
4302 cur.selection() = false;
4305 case Tabular::DELETE_COLUMN:
4306 for (col_type i = sel_col_start; i <= sel_col_end; ++i)
4307 tabular.deleteColumn(sel_col_start);
4308 if (sel_col_start >= tabular.column_info.size())
4310 cur.idx() = tabular.cellIndex(row, sel_col_start);
4313 cur.selection() = false;
4316 case Tabular::COPY_ROW:
4317 tabular.copyRow(row);
4320 case Tabular::COPY_COLUMN:
4321 tabular.copyColumn(column);
4322 cur.idx() = tabular.cellIndex(row, column);
4325 case Tabular::TOGGLE_LINE_TOP: {
4326 bool lineSet = !tabular.topLine(cur.idx());
4327 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4328 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4329 tabular.setTopLine(tabular.cellIndex(i, j), lineSet);
4333 case Tabular::TOGGLE_LINE_BOTTOM: {
4334 bool lineSet = !tabular.bottomLine(cur.idx());
4335 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4336 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4337 tabular.setBottomLine(tabular.cellIndex(i, j), lineSet);
4341 case Tabular::TOGGLE_LINE_LEFT: {
4342 bool lineSet = !tabular.leftLine(cur.idx());
4343 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4344 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4345 tabular.setLeftLine(tabular.cellIndex(i, j), lineSet);
4349 case Tabular::TOGGLE_LINE_RIGHT: {
4350 bool lineSet = !tabular.rightLine(cur.idx());
4351 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4352 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4353 tabular.setRightLine(tabular.cellIndex(i, j), lineSet);
4357 case Tabular::M_ALIGN_LEFT:
4358 case Tabular::M_ALIGN_RIGHT:
4359 case Tabular::M_ALIGN_CENTER:
4361 case Tabular::ALIGN_LEFT:
4362 case Tabular::ALIGN_RIGHT:
4363 case Tabular::ALIGN_CENTER:
4364 case Tabular::ALIGN_BLOCK:
4365 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4366 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4367 tabular.setAlignment(tabular.cellIndex(i, j), setAlign, flag);
4370 case Tabular::M_VALIGN_TOP:
4371 case Tabular::M_VALIGN_BOTTOM:
4372 case Tabular::M_VALIGN_MIDDLE:
4374 case Tabular::VALIGN_TOP:
4375 case Tabular::VALIGN_BOTTOM:
4376 case Tabular::VALIGN_MIDDLE:
4377 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4378 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4379 tabular.setVAlignment(tabular.cellIndex(i, j), setVAlign, flag);
4382 case Tabular::MULTICOLUMN: {
4383 if (!cur.selection()) {
4384 // just multicol for one single cell
4385 // check whether we are completely in a multicol
4386 if (tabular.isMultiColumn(cur.idx()))
4387 tabular.unsetMultiColumn(cur.idx());
4389 tabular.setMultiColumn(cur.idx(), 1);
4392 // we have a selection so this means we just add all this
4393 // cells to form a multicolumn cell
4394 idx_type const s_start = cur.selBegin().idx();
4395 idx_type const s_end = cur.selEnd().idx();
4396 tabular.setMultiColumn(s_start, s_end - s_start + 1);
4397 cur.idx() = s_start;
4400 cur.selection() = false;
4404 case Tabular::SET_ALL_LINES:
4406 case Tabular::UNSET_ALL_LINES:
4407 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4408 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4409 tabular.setAllLines(
4410 tabular.cellIndex(i,j), setLines);
4413 case Tabular::SET_BORDER_LINES:
4414 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4415 tabular.setLeftLine(tabular.cellIndex(i, sel_col_start), true);
4416 tabular.setRightLine(tabular.cellIndex(i, sel_col_end), true);
4418 for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
4419 tabular.setTopLine(tabular.cellIndex(sel_row_start, j), true);
4420 tabular.setBottomLine(tabular.cellIndex(sel_row_end, j), true);
4424 case Tabular::SET_LONGTABULAR:
4425 tabular.is_long_tabular = true;
4428 case Tabular::UNSET_LONGTABULAR:
4429 tabular.is_long_tabular = false;
4432 case Tabular::SET_ROTATE_TABULAR:
4433 tabular.rotate = true;
4436 case Tabular::UNSET_ROTATE_TABULAR:
4437 tabular.rotate = false;
4440 case Tabular::TOGGLE_ROTATE_TABULAR:
4441 tabular.rotate = !tabular.rotate;
4444 case Tabular::SET_ROTATE_CELL:
4445 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4446 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4447 tabular.setRotateCell(
4448 tabular.cellIndex(i, j), true);
4451 case Tabular::UNSET_ROTATE_CELL:
4452 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4453 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4454 tabular.setRotateCell(tabular.cellIndex(i, j), false);
4457 case Tabular::TOGGLE_ROTATE_CELL:
4459 bool oneNotRotated = oneCellHasRotationState(false,
4460 sel_row_start, sel_row_end, sel_col_start, sel_col_end);
4462 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4463 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4464 tabular.setRotateCell(tabular.cellIndex(i, j),
4469 case Tabular::SET_USEBOX: {
4470 Tabular::BoxType val = Tabular::BoxType(convert<int>(value));
4471 if (val == tabular.getUsebox(cur.idx()))
4472 val = Tabular::BOX_NONE;
4473 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4474 for (col_type j = sel_col_start; j <= sel_col_end; ++j)
4475 tabular.setUsebox(tabular.cellIndex(i, j), val);
4479 case Tabular::UNSET_LTFIRSTHEAD:
4481 case Tabular::SET_LTFIRSTHEAD:
4482 tabular.getRowOfLTFirstHead(row, ltt);
4483 checkLongtableSpecial(ltt, value, flag);
4484 tabular.setLTHead(row, flag, ltt, true);
4487 case Tabular::UNSET_LTHEAD:
4489 case Tabular::SET_LTHEAD:
4490 tabular.getRowOfLTHead(row, ltt);
4491 checkLongtableSpecial(ltt, value, flag);
4492 tabular.setLTHead(row, flag, ltt, false);
4495 case Tabular::UNSET_LTFOOT:
4497 case Tabular::SET_LTFOOT:
4498 tabular.getRowOfLTFoot(row, ltt);
4499 checkLongtableSpecial(ltt, value, flag);
4500 tabular.setLTFoot(row, flag, ltt, false);
4503 case Tabular::UNSET_LTLASTFOOT:
4505 case Tabular::SET_LTLASTFOOT:
4506 tabular.getRowOfLTLastFoot(row, ltt);
4507 checkLongtableSpecial(ltt, value, flag);
4508 tabular.setLTFoot(row, flag, ltt, true);
4511 case Tabular::SET_LTNEWPAGE:
4512 tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
4515 case Tabular::TOGGLE_LTCAPTION:
4516 cur.idx() = tabular.setLTCaption(row, !tabular.ltCaption(row));
4519 cur.selection() = false;
4522 case Tabular::SET_BOOKTABS:
4523 tabular.use_booktabs = true;
4526 case Tabular::UNSET_BOOKTABS:
4527 tabular.use_booktabs = false;
4530 case Tabular::SET_TOP_SPACE: {
4532 if (value == "default")
4533 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4534 tabular.row_info[i].top_space_default = true;
4535 else if (isValidLength(value, &len))
4536 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4537 tabular.row_info[i].top_space_default = false;
4538 tabular.row_info[i].top_space = len;
4541 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4542 tabular.row_info[i].top_space_default = false;
4543 tabular.row_info[i].top_space = len;
4548 case Tabular::SET_BOTTOM_SPACE: {
4550 if (value == "default")
4551 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4552 tabular.row_info[i].bottom_space_default = true;
4553 else if (isValidLength(value, &len))
4554 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4555 tabular.row_info[i].bottom_space_default = false;
4556 tabular.row_info[i].bottom_space = len;
4559 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4560 tabular.row_info[i].bottom_space_default = false;
4561 tabular.row_info[i].bottom_space = len;
4566 case Tabular::SET_INTERLINE_SPACE: {
4568 if (value == "default")
4569 for (row_type i = sel_row_start; i <= sel_row_end; ++i)
4570 tabular.row_info[i].interline_space_default = true;
4571 else if (isValidLength(value, &len))
4572 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4573 tabular.row_info[i].interline_space_default = false;
4574 tabular.row_info[i].interline_space = len;
4577 for (row_type i = sel_row_start; i <= sel_row_end; ++i) {
4578 tabular.row_info[i].interline_space_default = false;
4579 tabular.row_info[i].interline_space = len;
4584 // dummy stuff just to avoid warnings
4585 case Tabular::LAST_ACTION:
4591 bool InsetTabular::showInsetDialog(BufferView * bv) const
4593 bv->showDialog("tabular", params2string(*this),
4594 const_cast<InsetTabular *>(this));
4599 void InsetTabular::openLayoutDialog(BufferView * bv) const
4601 bv->showDialog("tabular", params2string(*this),
4602 const_cast<InsetTabular *>(this));
4606 bool InsetTabular::copySelection(Cursor & cur)
4608 if (!cur.selection())
4613 getSelection(cur, rs, re, cs, ce);
4615 paste_tabular.reset(new Tabular(tabular));
4617 for (row_type i = 0; i < rs; ++i)
4618 paste_tabular->deleteRow(0);
4620 row_type const rows = re - rs + 1;
4621 while (paste_tabular->row_info.size() > rows)
4622 paste_tabular->deleteRow(rows);
4624 for (col_type i = 0; i < cs; ++i)
4625 paste_tabular->deleteColumn(0);
4627 col_type const columns = ce - cs + 1;
4628 while (paste_tabular->column_info.size() > columns)
4629 paste_tabular->deleteColumn(columns);
4631 paste_tabular->setBuffer(tabular.buffer());
4633 odocstringstream os;
4634 OutputParams const runparams(0);
4635 paste_tabular->plaintext(os, runparams, 0, true, '\t');
4636 // Needed for the "Edit->Paste recent" menu and the system clipboard.
4637 cap::copySelection(cur, os.str());
4639 // mark tabular stack dirty
4640 // FIXME: this is a workaround for bug 1919. Should be removed for 1.5,
4641 // when we (hopefully) have a one-for-all paste mechanism.
4642 // This must be called after cap::copySelection.
4643 dirtyTabularStack(true);
4649 bool InsetTabular::pasteClipboard(Cursor & cur)
4653 col_type const actcol = tabular.cellColumn(cur.idx());
4654 row_type const actrow = tabular.cellRow(cur.idx());
4655 for (row_type r1 = 0, r2 = actrow;
4656 r1 < paste_tabular->row_info.size() && r2 < tabular.row_info.size();
4658 for (col_type c1 = 0, c2 = actcol;
4659 c1 < paste_tabular->column_info.size() && c2 < tabular.column_info.size();
4661 if (paste_tabular->isPartOfMultiColumn(r1, c1) &&
4662 tabular.isPartOfMultiColumn(r2, c2))
4664 if (paste_tabular->isPartOfMultiColumn(r1, c1)) {
4668 if (tabular.isPartOfMultiColumn(r2, c2)) {
4672 shared_ptr<InsetTableCell> inset(
4673 new InsetTableCell(*paste_tabular->cellInset(r1, c1)));
4674 tabular.setCellInset(r2, c2, inset);
4675 // FIXME: change tracking (MG)
4676 inset->setChange(Change(cur.buffer().params().trackChanges ?
4677 Change::INSERTED : Change::UNCHANGED));
4685 void InsetTabular::cutSelection(Cursor & cur)
4687 if (!cur.selection())
4692 getSelection(cur, rs, re, cs, ce);
4693 for (row_type i = rs; i <= re; ++i) {
4694 for (col_type j = cs; j <= ce; ++j) {
4695 shared_ptr<InsetTableCell> t
4696 = cell(tabular.cellIndex(i, j));
4697 if (cur.buffer().params().trackChanges)
4698 // FIXME: Change tracking (MG)
4699 t->setChange(Change(Change::DELETED));
4705 // cursor position might be invalid now
4706 if (cur.pit() > cur.lastpit())
4707 cur.pit() = cur.lastpit();
4708 if (cur.pos() > cur.lastpos())
4709 cur.pos() = cur.lastpos();
4710 cur.clearSelection();
4714 bool InsetTabular::isRightToLeft(Cursor & cur) const
4716 LASSERT(cur.depth() > 1, /**/);
4717 Paragraph const & parentpar = cur[cur.depth() - 2].paragraph();
4718 pos_type const parentpos = cur[cur.depth() - 2].pos();
4719 return parentpar.getFontSettings(cur.bv().buffer().params(),
4720 parentpos).language()->rightToLeft();
4723 docstring InsetTabular::asString(idx_type stidx, idx_type enidx,
4726 LASSERT(stidx <= enidx, return docstring());
4728 col_type const col1 = tabular.cellColumn(stidx);
4729 col_type const col2 = tabular.cellColumn(enidx);
4730 row_type const row1 = tabular.cellRow(stidx);
4731 row_type const row2 = tabular.cellRow(enidx);
4733 for (col_type col = col1; col <= col2; col++)
4734 for (row_type row = row1; row <= row2; row++) {
4739 retval += tabular.cellInset(row, col)->asString(intoInsets);
4745 void InsetTabular::getSelection(Cursor & cur,
4746 row_type & rs, row_type & re, col_type & cs, col_type & ce) const
4748 CursorSlice const & beg = cur.selBegin();
4749 CursorSlice const & end = cur.selEnd();
4750 cs = tabular.cellColumn(beg.idx());
4751 ce = tabular.cellColumn(end.idx());
4754 cs = tabular.cellColumn(end.idx());
4756 ce = tabular.cellRightColumn(end.idx());
4759 rs = tabular.cellRow(beg.idx());
4760 re = tabular.cellRow(end.idx());
4766 Text * InsetTabular::getText(int idx) const
4768 return size_t(idx) < nargs() ? cell(idx)->getText(0) : 0;
4772 void InsetTabular::setChange(Change const & change)
4774 for (idx_type idx = 0; idx < nargs(); ++idx)
4775 cell(idx)->setChange(change);
4779 void InsetTabular::acceptChanges(BufferParams const & bparams)
4781 for (idx_type idx = 0; idx < nargs(); ++idx)
4782 cell(idx)->acceptChanges(bparams);
4786 void InsetTabular::rejectChanges(BufferParams const & bparams)
4788 for (idx_type idx = 0; idx < nargs(); ++idx)
4789 cell(idx)->rejectChanges(bparams);
4793 bool InsetTabular::allowParagraphCustomization(idx_type cell) const
4795 return tabular.getPWidth(cell).zero();
4799 bool InsetTabular::forcePlainLayout(idx_type cell) const
4801 return !tabular.getPWidth(cell).zero();
4805 bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf,
4808 if (buf.length() <= 0)
4813 col_type maxCols = 1;
4814 size_t const len = buf.length();
4818 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos) {
4826 maxCols = max(cols, maxCols);
4832 maxCols = max(cols, maxCols);
4838 paste_tabular.reset(new Tabular(buffer(), rows, maxCols));
4839 loctab = paste_tabular.get();
4841 dirtyTabularStack(true);
4844 cell = bv.cursor().idx();
4845 ocol = tabular.cellColumn(cell);
4846 row = tabular.cellRow(cell);
4850 idx_type const cells = loctab->numberofcells;
4853 rows = loctab->row_info.size();
4854 col_type const columns = loctab->column_info.size();
4856 while (cell < cells && p < len && row < rows &&
4857 (p = buf.find_first_of(from_ascii("\t\n"), p)) != docstring::npos)
4863 // we can only set this if we are not too far right
4864 if (cols < columns) {
4865 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4866 Font const font = bv.textMetrics(&inset->text()).
4868 inset->setText(buf.substr(op, p - op), font,
4869 buffer().params().trackChanges);
4875 // we can only set this if we are not too far right
4876 if (cols < columns) {
4877 shared_ptr<InsetTableCell> inset = tabular.cellInset(cell);
4878 Font const font = bv.textMetrics(&inset->text()).
4880 inset->setText(buf.substr(op, p - op), font,
4881 buffer().params().trackChanges);
4886 cell = loctab->cellIndex(row, cols);
4892 // check for the last cell if there is no trailing '\n'
4893 if (cell < cells && op < len) {
4894 shared_ptr<InsetTableCell> inset = loctab->cellInset(cell);
4895 Font const font = bv.textMetrics(&inset->text()).displayFont(0, 0);
4896 inset->setText(buf.substr(op, len - op), font,
4897 buffer().params().trackChanges);
4903 void InsetTabular::addPreview(PreviewLoader & loader) const
4905 row_type const rows = tabular.row_info.size();
4906 col_type const columns = tabular.column_info.size();
4907 for (row_type i = 0; i < rows; ++i) {
4908 for (col_type j = 0; j < columns; ++j)
4909 tabular.cellInset(i, j)->addPreview(loader);
4914 bool InsetTabular::completionSupported(Cursor const & cur) const
4916 Cursor const & bvCur = cur.bv().cursor();
4917 if (&bvCur.inset() != this)
4919 return cur.text()->completionSupported(cur);
4923 bool InsetTabular::inlineCompletionSupported(Cursor const & cur) const
4925 return completionSupported(cur);
4929 bool InsetTabular::automaticInlineCompletion() const
4931 return lyxrc.completion_inline_text;
4935 bool InsetTabular::automaticPopupCompletion() const
4937 return lyxrc.completion_popup_text;
4941 bool InsetTabular::showCompletionCursor() const
4943 return lyxrc.completion_cursor_text;
4947 CompletionList const * InsetTabular::createCompletionList(Cursor const & cur) const
4949 return completionSupported(cur) ? cur.text()->createCompletionList(cur) : 0;
4953 docstring InsetTabular::completionPrefix(Cursor const & cur) const
4955 if (!completionSupported(cur))
4957 return cur.text()->completionPrefix(cur);
4961 bool InsetTabular::insertCompletion(Cursor & cur, docstring const & s, bool finished)
4963 if (!completionSupported(cur))
4966 return cur.text()->insertCompletion(cur, s, finished);
4970 void InsetTabular::completionPosAndDim(Cursor const & cur, int & x, int & y,
4971 Dimension & dim) const
4973 TextMetrics const & tm = cur.bv().textMetrics(cur.text());
4974 tm.completionPosAndDim(cur, x, y, dim);
4978 void InsetTabular::string2params(string const & in, InsetTabular & inset)
4980 istringstream data(in);
4982 lex.setStream(data);
4989 if (!lex || token != "tabular") {
4990 LYXERR0("Expected arg 1 to be \"tabular\" in " << in);
4994 // This is part of the inset proper that is usually swallowed
4995 // by Buffer::readInset
4997 if (!lex || token != "Tabular") {
4998 LYXERR0("Expected arg 2 to be \"Tabular\" in " << in);
5006 string InsetTabular::params2string(InsetTabular const & inset)
5009 data << "tabular" << ' ';
5011 data << "\\end_inset\n";